File size: 17,171 Bytes
fc115d5 | 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 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 | """
Binance Connector
Handles connectivity to Binance Testnet via ccxt.
"""
import os
import ccxt
import pandas as pd
import numpy as np
from datetime import datetime, timedelta
from typing import Optional, Dict, List, Any
import logging
import time
logger = logging.getLogger(__name__)
class BinanceConnector:
"""
Connector for Binance Spot Testnet using ccxt.
Handles:
- OHLCV data fetching
- Order placement and management
- Account balance queries
- Rate limiting
"""
def __init__(
self,
api_key: Optional[str] = None,
api_secret: Optional[str] = None,
testnet: bool = True,
rate_limit: bool = True,
):
"""
Initialize the Binance connector.
Args:
api_key: Binance API key
api_secret: Binance API secret
testnet: Whether to use testnet (default True)
rate_limit: Whether to enable rate limiting
"""
self.testnet = testnet
# Check if using Cloudflare Workers proxy to bypass geo-restrictions
proxy_url = os.getenv('BINANCE_TESTNET_PROXY_URL', '').strip() if testnet else None
# Initialize ccxt Binance
config = {
'apiKey': api_key,
'secret': api_secret,
'enableRateLimit': rate_limit,
'timeout': 10000, # 10-second timeout to prevent indefinite hangs
'options': {
'defaultType': 'spot',
'adjustForTimeDifference': True,
'recvWindow': 60000, # 60 seconds - max allowed by Binance (handles proxy latency)
'fetchCurrencies': False, # Disable fetching currency info (uses sapi)
'fetchMarkets': False, # Disable auto market loading (we use raw API calls)
'loadMarketsAlways': False, # Never auto-reload markets (prevents hang on testnet)
}
}
# Create exchange
self.exchange = ccxt.binance(config)
# Log proxy configuration if using one
if proxy_url:
proxy_url = proxy_url.rstrip('/')
logger.info(f"🌐 Will configure Cloudflare Workers proxy: {proxy_url}")
if testnet:
if proxy_url:
# Use Cloudflare Workers proxy
# ccxt expects urls['api'] to be a dict with public/private keys
# OR we can use the simpler approach: just replace the hostname
proxy_hostname = proxy_url.replace('https://', '').replace('http://', '').rstrip('/')
# First, set to testnet URLs
self.exchange.urls['api'] = self.exchange.urls['test']
# Then replace the hostname in all testnet URLs with our proxy
for key in self.exchange.urls['api']:
if isinstance(self.exchange.urls['api'][key], str):
# Replace testnet.binance.vision with our proxy hostname
self.exchange.urls['api'][key] = self.exchange.urls['api'][key].replace(
'testnet.binance.vision',
proxy_hostname
)
logger.info(f"✅ Using Cloudflare Workers proxy: {proxy_url}")
logger.info(f" Proxy hostname: {proxy_hostname}")
# Log all API URLs for debugging
for key, value in self.exchange.urls['api'].items():
if isinstance(value, str):
logger.info(f" {key}: {value}")
else:
# No proxy - try direct connection (may be geo-restricted)
# Auto-detect which testnet to use based on environment variable
# Default to legacy testnet (testnet.binance.vision)
use_legacy_testnet = os.getenv('USE_LEGACY_TESTNET', 'true').lower() == 'true'
if use_legacy_testnet:
# Legacy testnet (testnet.binance.vision) - use CCXT's built-in test URLs
self.exchange.urls['api'] = self.exchange.urls['test']
logger.info("Using legacy Binance testnet (testnet.binance.vision)")
else:
# New demo API (demo-api.binance.com) - use CCXT's built-in demo URLs
self.exchange.urls['api'] = self.exchange.urls['demo']
# Disable sapi endpoints (not supported on demo API)
# Redirect sapi to regular API to prevent errors
demo_spot_url = 'https://demo-api.binance.com/api/v3'
self.exchange.urls['api']['sapi'] = demo_spot_url
self.exchange.urls['api']['sapiV2'] = demo_spot_url
self.exchange.urls['api']['sapiV3'] = demo_spot_url
self.exchange.urls['api']['sapiV4'] = demo_spot_url
logger.info("Using new Binance Demo API (demo-api.binance.com)")
logger.warning("⚠️ No proxy configured - direct connection may fail due to geo-restrictions")
# Timeframe mapping
self.timeframe_map = {
'1m': 60,
'5m': 300,
'15m': 900,
'30m': 1800,
'1h': 3600,
'4h': 14400,
'1d': 86400,
}
def fetch_ohlcv(
self,
symbol: str = 'BTC/USDT',
timeframe: str = '1h',
since: Optional[datetime] = None,
limit: int = 500,
) -> pd.DataFrame:
"""
Fetch OHLCV candlestick data.
Args:
symbol: Trading pair (e.g., 'BTC/USDT')
timeframe: Candle timeframe (e.g., '1h', '4h', '1d')
since: Start datetime for data
limit: Maximum number of candles
Returns:
DataFrame with OHLCV data
"""
try:
# Convert symbol format: 'BTC/USDT' -> 'BTCUSDT'
market_symbol = symbol.replace('/', '')
# Prepare parameters
params = {
'symbol': market_symbol,
'interval': timeframe,
'limit': limit,
}
if since:
params['startTime'] = int(since.timestamp() * 1000)
# Use basic spot API klines endpoint
ohlcv = self.exchange.public_get_klines(params)
# Parse response
df = pd.DataFrame(
ohlcv,
columns=['timestamp', 'open', 'high', 'low', 'close', 'volume',
'close_time', 'quote_volume', 'trades', 'taker_buy_base',
'taker_buy_quote', 'ignore']
)
# Convert types and select relevant columns
df['timestamp'] = pd.to_datetime(pd.to_numeric(df['timestamp']), unit='ms')
df['open'] = pd.to_numeric(df['open'])
df['high'] = pd.to_numeric(df['high'])
df['low'] = pd.to_numeric(df['low'])
df['close'] = pd.to_numeric(df['close'])
df['volume'] = pd.to_numeric(df['volume'])
# Select and set index
df = df[['timestamp', 'open', 'high', 'low', 'close', 'volume']]
df.set_index('timestamp', inplace=True)
return df
except Exception as e:
logger.error(f"Error fetching OHLCV: {e}")
raise
def fetch_historical_data(
self,
symbol: str = 'BTC/USDT',
timeframe: str = '1h',
start_date: datetime = None,
end_date: datetime = None,
) -> pd.DataFrame:
"""
Fetch historical OHLCV data with pagination.
Args:
symbol: Trading pair
timeframe: Candle timeframe
start_date: Start datetime
end_date: End datetime
Returns:
DataFrame with all historical data
"""
if start_date is None:
start_date = datetime.now() - timedelta(days=365)
if end_date is None:
end_date = datetime.now()
all_data = []
current_date = start_date
# Calculate time delta per candle
tf_seconds = self.timeframe_map.get(timeframe, 3600)
candles_per_request = 1000
time_per_request = timedelta(seconds=tf_seconds * candles_per_request)
logger.info(f"Fetching historical data from {start_date} to {end_date}")
while current_date < end_date:
try:
df = self.fetch_ohlcv(
symbol=symbol,
timeframe=timeframe,
since=current_date,
limit=candles_per_request,
)
if len(df) == 0:
break
all_data.append(df)
# Move to next batch
current_date = df.index[-1].to_pydatetime() + timedelta(seconds=tf_seconds)
# Rate limiting - be nice to the API
time.sleep(0.5)
except Exception as e:
logger.error(f"Error fetching batch: {e}")
time.sleep(5)
continue
if not all_data:
return pd.DataFrame()
result = pd.concat(all_data)
result = result[~result.index.duplicated(keep='first')]
result = result[(result.index >= start_date) & (result.index <= end_date)]
result = result.sort_index()
logger.info(f"Fetched {len(result)} candles")
return result
def _fetch_account_info(self) -> Dict:
"""
Fetch account information using basic spot API.
Uses direct API call to avoid CCXT's sapi/margin dependencies.
"""
try:
# Use CCXT's private_get_account method which directly calls /api/v3/account
account = self.exchange.private_get_account()
return account
except Exception as e:
logger.error(f"Error fetching account info: {e}")
# Re-raise the exception so it can be handled upstream with full error details
raise
def get_balance(self, currency: str = 'USDT') -> float:
"""
Get account balance for a currency.
Args:
currency: Currency symbol (e.g., 'USDT', 'BTC')
Returns:
Available balance
"""
try:
# Use basic spot API account endpoint
account = self._fetch_account_info()
balances = account.get('balances', [])
for balance in balances:
if balance.get('asset') == currency:
return float(balance.get('free', 0.0))
return 0.0
except Exception as e:
logger.error(f"Error fetching balance: {e}")
return 0.0
def get_all_balances(self) -> Dict[str, Dict[str, float]]:
"""
Get all non-zero balances.
Returns:
Dictionary of currency -> {free, used, total}
"""
try:
# Use basic spot API account endpoint
account = self._fetch_account_info()
balances = account.get('balances', [])
non_zero = {}
for balance in balances:
asset = balance.get('asset')
free = float(balance.get('free', 0))
locked = float(balance.get('locked', 0))
total = free + locked
if total > 0:
non_zero[asset] = {
'free': free,
'used': locked,
'total': total
}
return non_zero
except Exception as e:
logger.error(f"Error fetching balances: {e}")
# Re-raise so we can see the actual error in the UI
raise
def get_ticker(self, symbol: str = 'BTC/USDT') -> Dict[str, Any]:
"""
Get current ticker data.
Args:
symbol: Trading pair
Returns:
Ticker data including last price, bid, ask, etc.
"""
try:
# Convert symbol format: 'BTC/USDT' -> 'BTCUSDT'
market_symbol = symbol.replace('/', '')
# Use basic spot API ticker endpoint
ticker = self.exchange.public_get_ticker_24hr({'symbol': market_symbol})
return {
'symbol': symbol,
'last': float(ticker['lastPrice']),
'bid': float(ticker['bidPrice']),
'ask': float(ticker['askPrice']),
'high': float(ticker['highPrice']),
'low': float(ticker['lowPrice']),
'volume': float(ticker['volume']),
'timestamp': datetime.now(),
}
except Exception as e:
logger.error(f"Error fetching ticker: {e}")
return {}
def place_market_order(
self,
symbol: str,
side: str,
amount: float,
) -> Optional[Dict]:
"""
Place a market order using the direct /api/v3/order endpoint.
Args:
symbol: Trading pair (e.g. 'BTC/USDT')
side: 'buy' or 'sell'
amount: Amount in base currency
Returns:
Order details or None if failed
"""
try:
market_symbol = symbol.replace('/', '')
params = {
'symbol': market_symbol,
'side': side.upper(),
'type': 'MARKET',
'quantity': amount,
}
order = self.exchange.private_post_order(params)
logger.info(f"Market order placed: {side} {amount} {symbol}")
return order
except Exception as e:
logger.error(f"Error placing order: {e}")
return None
def place_limit_order(
self,
symbol: str,
side: str,
amount: float,
price: float,
) -> Optional[Dict]:
"""
Place a limit order using the direct /api/v3/order endpoint.
Args:
symbol: Trading pair (e.g. 'BTC/USDT')
side: 'buy' or 'sell'
amount: Amount in base currency
price: Limit price
Returns:
Order details or None if failed
"""
try:
market_symbol = symbol.replace('/', '')
params = {
'symbol': market_symbol,
'side': side.upper(),
'type': 'LIMIT',
'quantity': amount,
'price': price,
'timeInForce': 'GTC',
}
order = self.exchange.private_post_order(params)
logger.info(f"Limit order placed: {side} {amount} {symbol} @ {price}")
return order
except Exception as e:
logger.error(f"Error placing order: {e}")
return None
def cancel_order(self, order_id: str, symbol: str) -> bool:
"""
Cancel an open order.
Args:
order_id: Order ID to cancel
symbol: Trading pair
Returns:
True if cancelled, False otherwise
"""
try:
self.exchange.cancel_order(order_id, symbol)
logger.info(f"Order {order_id} cancelled")
return True
except Exception as e:
logger.error(f"Error cancelling order: {e}")
return False
def get_open_orders(self, symbol: Optional[str] = None) -> List[Dict]:
"""
Get all open orders.
Args:
symbol: Optional trading pair to filter
Returns:
List of open orders
"""
try:
# Use basic spot API open orders endpoint
params = {}
if symbol:
# Convert symbol format: 'BTC/USDT' -> 'BTCUSDT'
params['symbol'] = symbol.replace('/', '')
orders = self.exchange.private_get_openorders(params)
return orders
except Exception as e:
logger.error(f"Error fetching orders: {e}")
return []
def test_connectivity(self) -> bool:
"""Test API connectivity."""
try:
self.exchange.fetch_time()
return True
except Exception as e:
logger.error(f"Connectivity test failed: {e}")
return False
def create_connector(config: Dict) -> BinanceConnector:
"""Factory function to create connector from config."""
import os
# Get API keys from environment or config
api_key = os.environ.get('BINANCE_TESTNET_API_KEY') or config.get('api_key')
api_secret = os.environ.get('BINANCE_TESTNET_API_SECRET') or config.get('api_secret')
return BinanceConnector(
api_key=api_key,
api_secret=api_secret,
testnet=config.get('testnet', True),
)
|