Crypto trading bot: engine, brokers, backtester, dashboard, deploy
FastAPI backend with a single-strategy async engine, paper and Binance brokers, pure-python indicators, a backtester that shares the engine's portfolio math, and a Vue dashboard with a live WebSocket feed. Adds the Gitea Actions CI/CD workflow that deploys to Traefik via docker_default.
This commit is contained in:
1 parent
98028248c2
commit
9c3bee87ac
73 files changed
+9759
-1
No files matched your search
@@ -0,0 +1,16 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from app.config import Settings
|
||||
from app.market.base import MarketData
|
||||
from app.market.binance import BinanceMarketData
|
||||
from app.market.fake import FakeMarketData
|
||||
|
||||
|
||||
def build_market_data(settings: Settings) -> MarketData:
|
||||
exchange = settings.exchange.lower()
|
||||
if exchange in {"binance", "binanceus"}:
|
||||
return BinanceMarketData()
|
||||
raise ValueError(f"unsupported exchange: {settings.exchange}")
|
||||
|
||||
|
||||
__all__ = ["FakeMarketData", "MarketData", "build_market_data"]
|
||||
@@ -0,0 +1,20 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from abc import ABC, abstractmethod
|
||||
|
||||
from app.schemas import Candle
|
||||
|
||||
|
||||
class MarketData(ABC):
|
||||
"""Read-only market data source (candles + last price)."""
|
||||
|
||||
@abstractmethod
|
||||
async def fetch_candles(self, symbol: str, timeframe: str, limit: int) -> list[Candle]:
|
||||
raise NotImplementedError
|
||||
|
||||
@abstractmethod
|
||||
async def fetch_price(self, symbol: str) -> float:
|
||||
raise NotImplementedError
|
||||
|
||||
async def close(self) -> None:
|
||||
return None
|
||||
@@ -0,0 +1,52 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import httpx
|
||||
|
||||
from app.market.base import MarketData
|
||||
from app.schemas import Candle
|
||||
|
||||
BASE_URL = "https://api.binance.com"
|
||||
|
||||
|
||||
def to_exchange_symbol(symbol: str) -> str:
|
||||
return symbol.replace("/", "").upper()
|
||||
|
||||
|
||||
class BinanceMarketData(MarketData):
|
||||
"""Public Binance spot market data over REST (no API key required)."""
|
||||
|
||||
def __init__(self, base_url: str = BASE_URL, timeout: float = 10.0) -> None:
|
||||
self._client = httpx.AsyncClient(base_url=base_url, timeout=timeout)
|
||||
|
||||
async def fetch_candles(self, symbol: str, timeframe: str, limit: int) -> list[Candle]:
|
||||
response = await self._client.get(
|
||||
"/api/v3/klines",
|
||||
params={
|
||||
"symbol": to_exchange_symbol(symbol),
|
||||
"interval": timeframe,
|
||||
"limit": limit,
|
||||
},
|
||||
)
|
||||
response.raise_for_status()
|
||||
return [
|
||||
Candle(
|
||||
ts=int(row[0]),
|
||||
open=float(row[1]),
|
||||
high=float(row[2]),
|
||||
low=float(row[3]),
|
||||
close=float(row[4]),
|
||||
volume=float(row[5]),
|
||||
)
|
||||
for row in response.json()
|
||||
]
|
||||
|
||||
async def fetch_price(self, symbol: str) -> float:
|
||||
response = await self._client.get(
|
||||
"/api/v3/ticker/price",
|
||||
params={"symbol": to_exchange_symbol(symbol)},
|
||||
)
|
||||
response.raise_for_status()
|
||||
return float(response.json()["price"])
|
||||
|
||||
async def close(self) -> None:
|
||||
await self._client.aclose()
|
||||
@@ -0,0 +1,46 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import math
|
||||
from collections.abc import Sequence
|
||||
|
||||
from app.market.base import MarketData
|
||||
from app.schemas import Candle
|
||||
|
||||
|
||||
class FakeMarketData(MarketData):
|
||||
"""In-memory market data for tests and offline demos."""
|
||||
|
||||
def __init__(self, candles: Sequence[Candle] | None = None) -> None:
|
||||
self.candles: list[Candle] = list(candles or [])
|
||||
|
||||
@classmethod
|
||||
def from_closes(cls, closes: Sequence[float]) -> FakeMarketData:
|
||||
candles = [
|
||||
Candle(
|
||||
ts=1_700_000_000_000 + i * 60_000,
|
||||
open=close,
|
||||
high=close,
|
||||
low=close,
|
||||
close=close,
|
||||
volume=1.0,
|
||||
)
|
||||
for i, close in enumerate(closes)
|
||||
]
|
||||
return cls(candles)
|
||||
|
||||
@classmethod
|
||||
def sine_wave(cls, n: int = 200, amplitude: float = 100.0, period: int = 20) -> FakeMarketData:
|
||||
base = 1000.0
|
||||
closes = [base + amplitude * math.sin(2 * math.pi * i / period) for i in range(n)]
|
||||
return cls.from_closes(closes)
|
||||
|
||||
def append(self, candle: Candle) -> None:
|
||||
self.candles.append(candle)
|
||||
|
||||
async def fetch_candles(self, symbol: str, timeframe: str, limit: int) -> list[Candle]:
|
||||
return self.candles[-limit:]
|
||||
|
||||
async def fetch_price(self, symbol: str) -> float:
|
||||
if not self.candles:
|
||||
raise ValueError("no candles available")
|
||||
return self.candles[-1].close
|
||||
Reference in new issue
Block a user