Files
crypto_trading/backend/app/market/binance.py
T
matitos 9c3bee87ac
CI/CD / backend (push) Successful in 5m18s
CI/CD / frontend (push) Successful in 5m2s
CI/CD / build (push) Successful in 34s
CI/CD / cleanup (push) Successful in 6s
CI/CD / deploy (push) Successful in 22s
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.
2026-09-18 17:02:33 +00:00

53 lines
1.5 KiB
Python

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()