File size: 9,708 Bytes
8b7b267 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 |
#!/usr/bin/env python3
"""
Binance Public API Client - REAL DATA ONLY
Fetches real OHLCV historical data from Binance
NO MOCK DATA - All data from live Binance API
"""
import httpx
import logging
from typing import Dict, Any, List, Optional
from datetime import datetime
from fastapi import HTTPException
logger = logging.getLogger(__name__)
class BinanceClient:
"""
Real Binance Public API Client
Primary source for real historical OHLCV candlestick data
"""
def __init__(self):
self.base_url = "https://api.binance.com/api/v3"
self.timeout = 15.0
# Timeframe mapping
self.timeframe_map = {
"1m": "1m",
"5m": "5m",
"15m": "15m",
"30m": "30m",
"1h": "1h",
"4h": "4h",
"1d": "1d",
"1w": "1w"
}
def _normalize_symbol(self, symbol: str) -> str:
"""Normalize symbol to Binance format (e.g., BTC -> BTCUSDT)"""
symbol = symbol.upper().strip()
# If already has USDT suffix, return as is
if symbol.endswith("USDT"):
return symbol
# Add USDT suffix
return f"{symbol}USDT"
async def get_ohlcv(
self,
symbol: str,
timeframe: str = "1h",
limit: int = 1000
) -> List[Dict[str, Any]]:
"""
Fetch REAL OHLCV candlestick data from Binance
Args:
symbol: Cryptocurrency symbol (e.g., "BTC", "ETH", "BTCUSDT")
timeframe: Time interval (1m, 5m, 15m, 30m, 1h, 4h, 1d, 1w)
limit: Maximum number of candles (max 1000)
Returns:
List of real OHLCV candles
"""
try:
# Normalize symbol
binance_symbol = self._normalize_symbol(symbol)
# Map timeframe
binance_interval = self.timeframe_map.get(timeframe, "1h")
# Limit to max 1000
limit = min(limit, 1000)
async with httpx.AsyncClient(timeout=self.timeout) as client:
response = await client.get(
f"{self.base_url}/klines",
params={
"symbol": binance_symbol,
"interval": binance_interval,
"limit": limit
}
)
response.raise_for_status()
klines = response.json()
# Transform Binance format to standard OHLCV format
ohlcv_data = []
for kline in klines:
# Binance kline format:
# [timestamp, open, high, low, close, volume, ...]
timestamp = int(kline[0])
open_price = float(kline[1])
high_price = float(kline[2])
low_price = float(kline[3])
close_price = float(kline[4])
volume = float(kline[5])
# Filter out invalid candles
if open_price > 0 and close_price > 0:
ohlcv_data.append({
"timestamp": timestamp,
"open": open_price,
"high": high_price,
"low": low_price,
"close": close_price,
"volume": volume
})
logger.info(
f"β
Binance: Fetched {len(ohlcv_data)} real candles "
f"for {binance_symbol} ({timeframe})"
)
return ohlcv_data
except httpx.HTTPStatusError as e:
if e.response.status_code == 400:
logger.error(f"β Binance: Invalid symbol or parameters: {symbol}")
raise HTTPException(
status_code=400,
detail=f"Invalid symbol or parameters: {symbol}"
)
elif e.response.status_code == 404:
logger.error(f"β Binance: Symbol not found: {binance_symbol}")
raise HTTPException(
status_code=404,
detail=f"Symbol not found on Binance: {symbol}"
)
elif e.response.status_code == 451:
logger.warning(
f"β οΈ Binance: HTTP 451 - Access restricted (geo-blocking or legal restrictions) for {binance_symbol}. "
f"Consider using alternative data sources or VPN."
)
raise HTTPException(
status_code=451,
detail=f"Binance API access restricted for your region. Please use alternative data sources (CoinGecko, CoinMarketCap)."
)
else:
logger.error(f"β Binance API HTTP error: {e}")
raise HTTPException(
status_code=503,
detail=f"Binance API temporarily unavailable: {str(e)}"
)
except httpx.HTTPError as e:
logger.error(f"β Binance API HTTP error: {e}")
raise HTTPException(
status_code=503,
detail=f"Binance API temporarily unavailable: {str(e)}"
)
except Exception as e:
logger.error(f"β Binance API failed: {e}")
raise HTTPException(
status_code=503,
detail=f"Failed to fetch real OHLCV data from Binance: {str(e)}"
)
async def get_ticker(self, symbol: str) -> Dict[str, Any]:
"""
Fetch REAL current ticker price
Args:
symbol: Cryptocurrency symbol (e.g., "BTC", "ETH", "BTCUSDT")
Returns:
Real ticker data with current price
"""
try:
binance_symbol = self._normalize_symbol(symbol)
async with httpx.AsyncClient(timeout=self.timeout) as client:
response = await client.get(
f"{self.base_url}/ticker/price",
params={"symbol": binance_symbol}
)
response.raise_for_status()
data = response.json()
return {
"symbol": binance_symbol,
"lastPrice": data.get("price", "0"),
"price": float(data.get("price", 0))
}
except httpx.HTTPStatusError as e:
if e.response.status_code == 400:
return None # Symbol not found
raise HTTPException(
status_code=503,
detail=f"Failed to fetch ticker from Binance: {str(e)}"
)
except Exception as e:
logger.error(f"β Binance ticker failed: {e}")
return None
async def get_24h_ticker(self, symbol: str) -> Dict[str, Any]:
"""
Fetch REAL 24-hour ticker price change statistics
Args:
symbol: Cryptocurrency symbol (e.g., "BTC", "ETH")
Returns:
Real 24-hour ticker data
"""
try:
binance_symbol = self._normalize_symbol(symbol)
async with httpx.AsyncClient(timeout=self.timeout) as client:
response = await client.get(
f"{self.base_url}/ticker/24hr",
params={"symbol": binance_symbol}
)
response.raise_for_status()
data = response.json()
# Transform to standard format
ticker = {
"symbol": symbol.upper().replace("USDT", ""),
"price": float(data.get("lastPrice", 0)),
"change24h": float(data.get("priceChange", 0)),
"changePercent24h": float(data.get("priceChangePercent", 0)),
"volume24h": float(data.get("volume", 0)),
"high24h": float(data.get("highPrice", 0)),
"low24h": float(data.get("lowPrice", 0)),
"source": "binance",
"timestamp": int(datetime.utcnow().timestamp() * 1000)
}
logger.info(f"β
Binance: Fetched real 24h ticker for {binance_symbol}")
return ticker
except httpx.HTTPStatusError as e:
if e.response.status_code == 451:
logger.warning(
f"β οΈ Binance: HTTP 451 - Access restricted (geo-blocking or legal restrictions). "
f"Consider using alternative data sources."
)
raise HTTPException(
status_code=451,
detail=f"Binance API access restricted for your region. Please use alternative data sources (CoinGecko, CoinMarketCap)."
)
logger.error(f"β Binance ticker error: {e}")
raise HTTPException(
status_code=503,
detail=f"Failed to fetch ticker from Binance: {str(e)}"
)
except Exception as e:
logger.error(f"β Binance ticker failed: {e}")
raise HTTPException(
status_code=503,
detail=f"Failed to fetch real ticker data: {str(e)}"
)
# Global instance
binance_client = BinanceClient()
__all__ = ["BinanceClient", "binance_client"]
|