"""测试Binance API连接(使用SOCKS5代理)""" import sys import os sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) from data.fetcher import BinanceDataFetcher def main(): print("="*80) print("测试Binance API连接 - SOCKS5代理") print("="*80) # 测试连接 fetcher = BinanceDataFetcher() if fetcher.client is None: print("\n❌ API连接失败,将使用缓存数据") return print("\n✓ API连接成功!") # 获取当前价格 try: btc_price = fetcher.get_latest_price('BTC') eth_price = fetcher.get_latest_price('ETH') sol_price = fetcher.get_latest_price('SOL') print(f"\n当前价格:") print(f" BTC: ${btc_price:,.2f}") print(f" ETH: ${eth_price:,.2f}") print(f" SOL: ${sol_price:,.2f}") except Exception as e: print(f"\n❌ 获取价格失败: {e}") return # 获取历史数据(1天测试) try: print("\n正在获取BTC 1小时数据(最近1天)...") from datetime import datetime, timedelta end_date = datetime.now() start_date = end_date - timedelta(days=1) df = fetcher.fetch_ohlcv( 'BTC', '1h', start_date=start_date.strftime('%Y-%m-%d'), end_date=end_date.strftime('%Y-%m-%d'), use_cache=False # 强制从API获取 ) print(f"✓ 成功获取 {len(df)} 行数据") print(f"\n最新数据:") print(df.tail()) except Exception as e: print(f"\n❌ 获取历史数据失败: {e}") return print("\n" + "="*80) print("✓ Binance API测试通过!代理工作正常") print("="*80) if __name__ == "__main__": main()