|
|
@@ -38,7 +38,6 @@ logger = logging.getLogger("market_data_recorder")
|
|
|
LIGHTER_API_URL = "https://mainnet.zklighter.elliot.ai/api/v1/exchangeStats"
|
|
|
LIGHTER_ORDERBOOKS_URL = "https://mainnet.zklighter.elliot.ai/api/v1/orderBooks"
|
|
|
LIGHTER_WEBSOCKET_URL = "wss://mainnet.zklighter.elliot.ai/stream"
|
|
|
-BINANCE_API_URL = "https://fapi.binance.com/fapi/v2/ticker/price"
|
|
|
BINANCE_PREMIUM_INDEX_URL = "https://fapi.binance.com/fapi/v1/premiumIndex"
|
|
|
BINANCE_TICKER_PRICE_URL = "https://fapi.binance.com/fapi/v2/ticker/price"
|
|
|
|
|
|
@@ -86,42 +85,6 @@ async def fetch_lighter_orderbooks(session):
|
|
|
return None
|
|
|
|
|
|
|
|
|
-async def fetch_lighter_data(session):
|
|
|
- """从Lighter交易所获取行情数据"""
|
|
|
- try:
|
|
|
- # 设置代理参数
|
|
|
- proxy = 'http://' + PROXY_ADDRESS if PROXY_ADDRESS else None
|
|
|
- async with session.get(LIGHTER_API_URL, proxy=proxy) as response:
|
|
|
- if response.status == 200:
|
|
|
- data = await response.json()
|
|
|
- # logger.info(f"成功获取Lighter数据,包含 {len(data.get('order_book_stats', []))} 个交易对")
|
|
|
- return data
|
|
|
- else:
|
|
|
- logger.error(f"获取Lighter数据失败: HTTP {response.status}")
|
|
|
- return None
|
|
|
- except Exception as e:
|
|
|
- logger.error(f"获取Lighter数据时出错: {str(e)}")
|
|
|
- return None
|
|
|
-
|
|
|
-
|
|
|
-async def fetch_binance_data(session):
|
|
|
- """从Binance交易所获取行情数据"""
|
|
|
- try:
|
|
|
- # 设置代理参数
|
|
|
- proxy = 'http://' + PROXY_ADDRESS if PROXY_ADDRESS else None
|
|
|
- async with session.get(BINANCE_API_URL, proxy=proxy) as response:
|
|
|
- if response.status == 200:
|
|
|
- data = await response.json()
|
|
|
- # logger.info(f"成功获取Binance数据,包含 {len(data)} 个交易对")
|
|
|
- return data
|
|
|
- else:
|
|
|
- logger.error(f"获取Binance数据失败: HTTP {response.status}")
|
|
|
- return None
|
|
|
- except Exception as e:
|
|
|
- logger.error(f"获取Binance数据时出错: {str(e)}")
|
|
|
- return None
|
|
|
-
|
|
|
-
|
|
|
async def fetch_binance_premium_index(session):
|
|
|
"""从Binance获取标记价格数据"""
|
|
|
try:
|
|
|
@@ -155,7 +118,7 @@ async def fetch_binance_ticker_price(session):
|
|
|
|
|
|
|
|
|
async def handle_binance_data_collection():
|
|
|
- """处理Binance数据收集的主循环,每200ms请求一次"""
|
|
|
+ """处理Binance数据收集的主循环,每300ms请求一次"""
|
|
|
logger.info("开始Binance数据收集任务")
|
|
|
|
|
|
while True:
|
|
|
@@ -184,35 +147,14 @@ async def handle_binance_data_collection():
|
|
|
if symbol and price:
|
|
|
binance_data_cache['latest_prices'][symbol] = float(price)
|
|
|
|
|
|
- # 每200ms请求一次
|
|
|
- await asyncio.sleep(0.2)
|
|
|
+ # 每300ms请求一次
|
|
|
+ await asyncio.sleep(0.3)
|
|
|
|
|
|
except Exception as e:
|
|
|
logger.error(f"Binance数据收集出错: {str(e)}")
|
|
|
await asyncio.sleep(1) # 出错时等待1秒再重试
|
|
|
|
|
|
|
|
|
-async def fetch_all_data():
|
|
|
- """同时从两个交易所获取数据"""
|
|
|
- # 如果设置了代理,则使用代理
|
|
|
- # if PROXY_ADDRESS:
|
|
|
- # logger.info(f"使用代理: {PROXY_ADDRESS}")
|
|
|
-
|
|
|
- # 创建会话时设置代理
|
|
|
- connector = None
|
|
|
- if PROXY_ADDRESS:
|
|
|
- connector = aiohttp.TCPConnector(ssl=False)
|
|
|
-
|
|
|
- async with aiohttp.ClientSession(connector=connector) as session:
|
|
|
- lighter_task = asyncio.create_task(fetch_lighter_data(session))
|
|
|
- binance_task = asyncio.create_task(fetch_binance_data(session))
|
|
|
-
|
|
|
- # 等待两个任务完成
|
|
|
- lighter_data, binance_data = await asyncio.gather(lighter_task, binance_task)
|
|
|
-
|
|
|
- return lighter_data, binance_data
|
|
|
-
|
|
|
-
|
|
|
def update_market_id_mapping(orderbooks_data):
|
|
|
"""更新market_id到symbol的映射"""
|
|
|
global market_id_to_symbol
|