|
|
@@ -17,6 +17,7 @@ import logging
|
|
|
import os
|
|
|
import requests
|
|
|
from datetime import datetime
|
|
|
+from strategy import TradingStrategy
|
|
|
|
|
|
# 配置日志
|
|
|
# 创建logs目录(如果不存在)
|
|
|
@@ -65,6 +66,9 @@ binance_data_cache = {
|
|
|
'latest_prices': {} # 存储最新价格数据 {symbol: price}
|
|
|
}
|
|
|
|
|
|
+# 全局策略实例
|
|
|
+trading_strategy = None
|
|
|
+
|
|
|
|
|
|
async def fetch_lighter_orderbooks(session):
|
|
|
"""从Lighter交易所获取orderBooks数据以建立market_id映射"""
|
|
|
@@ -117,6 +121,24 @@ async def fetch_binance_ticker_price(session):
|
|
|
return None
|
|
|
|
|
|
|
|
|
+def update_market_id_mapping(orderbooks_data):
|
|
|
+ """更新market_id到symbol的映射"""
|
|
|
+ global market_id_to_symbol
|
|
|
+ if not orderbooks_data:
|
|
|
+ return
|
|
|
+
|
|
|
+ try:
|
|
|
+ for orderbook in orderbooks_data:
|
|
|
+ market_id = orderbook.get('market_id')
|
|
|
+ symbol = orderbook.get('symbol')
|
|
|
+ if market_id is not None and symbol:
|
|
|
+ market_id_to_symbol[market_id] = symbol
|
|
|
+
|
|
|
+ logger.info(f"更新market_id映射,共 {len(market_id_to_symbol)} 个交易对,{market_id_to_symbol}")
|
|
|
+ except Exception as e:
|
|
|
+ logger.error(f"更新market_id映射时出错: {str(e)}")
|
|
|
+
|
|
|
+
|
|
|
async def handle_binance_data_collection():
|
|
|
"""处理Binance数据收集的主循环,每300ms请求一次"""
|
|
|
logger.info("开始Binance数据收集任务")
|
|
|
@@ -147,6 +169,9 @@ async def handle_binance_data_collection():
|
|
|
if symbol and price:
|
|
|
binance_data_cache['latest_prices'][symbol] = float(price)
|
|
|
|
|
|
+ # 触发策略更新
|
|
|
+ trigger_strategy_update()
|
|
|
+
|
|
|
# 每300ms请求一次
|
|
|
await asyncio.sleep(0.3)
|
|
|
|
|
|
@@ -155,24 +180,6 @@ async def handle_binance_data_collection():
|
|
|
await asyncio.sleep(1) # 出错时等待1秒再重试
|
|
|
|
|
|
|
|
|
-def update_market_id_mapping(orderbooks_data):
|
|
|
- """更新market_id到symbol的映射"""
|
|
|
- global market_id_to_symbol
|
|
|
- if not orderbooks_data:
|
|
|
- return
|
|
|
-
|
|
|
- try:
|
|
|
- for orderbook in orderbooks_data:
|
|
|
- market_id = orderbook.get('market_id')
|
|
|
- symbol = orderbook.get('symbol')
|
|
|
- if market_id is not None and symbol:
|
|
|
- market_id_to_symbol[market_id] = symbol
|
|
|
-
|
|
|
- logger.info(f"更新market_id映射,共 {len(market_id_to_symbol)} 个交易对,{market_id_to_symbol}")
|
|
|
- except Exception as e:
|
|
|
- logger.error(f"更新market_id映射时出错: {str(e)}")
|
|
|
-
|
|
|
-
|
|
|
async def handle_market_stats_websocket():
|
|
|
"""
|
|
|
处理Lighter Market Stats WebSocket连接
|
|
|
@@ -227,6 +234,9 @@ async def handle_market_stats_websocket():
|
|
|
|
|
|
symbol = market_id_to_symbol.get(market_id, f"UNKNOWN_{market_id}")
|
|
|
logger.debug(f"更新Market Stats缓存 - {symbol}(ID:{market_id}): mark_price={market_data.get('mark_price')}, last_trade_price={market_data.get('last_trade_price')}")
|
|
|
+
|
|
|
+ # # 触发策略更新
|
|
|
+ # trigger_strategy_update()
|
|
|
|
|
|
# 处理订阅确认消息
|
|
|
elif message_type == "subscribed/market_stats":
|
|
|
@@ -292,6 +302,39 @@ def write_batch_to_questdb(data_batch):
|
|
|
return False
|
|
|
|
|
|
|
|
|
+def trigger_strategy_update():
|
|
|
+ """
|
|
|
+ 触发策略更新,将最新的市场数据传递给策略
|
|
|
+ """
|
|
|
+ global trading_strategy
|
|
|
+
|
|
|
+ if trading_strategy is None:
|
|
|
+ return
|
|
|
+
|
|
|
+ # 遍历所有匹配的交易对,调用策略
|
|
|
+ for market_id, symbol in market_id_to_symbol.items():
|
|
|
+ if market_id not in market_stats_cache:
|
|
|
+ continue
|
|
|
+
|
|
|
+ binance_symbol = f"{symbol}USDT"
|
|
|
+ if (binance_symbol not in binance_data_cache['mark_prices'] or
|
|
|
+ binance_symbol not in binance_data_cache['latest_prices']):
|
|
|
+ continue
|
|
|
+
|
|
|
+ lighter_stats = market_stats_cache[market_id]
|
|
|
+ market_data = {
|
|
|
+ 'symbol': symbol,
|
|
|
+ 'binance_mark_price': binance_data_cache['mark_prices'][binance_symbol],
|
|
|
+ 'binance_price': binance_data_cache['latest_prices'][binance_symbol],
|
|
|
+ 'lighter_mark_price': lighter_stats.get('mark_price'),
|
|
|
+ 'lighter_price': lighter_stats.get('last_trade_price'),
|
|
|
+ 'timestamp': int(time.time() * 1000)
|
|
|
+ }
|
|
|
+
|
|
|
+ # 调用策略
|
|
|
+ trading_strategy.do_strategy(market_data)
|
|
|
+
|
|
|
+
|
|
|
def process_data():
|
|
|
"""
|
|
|
处理从两个交易所获取的数据,并将匹配的交易对数据保存到数据库
|
|
|
@@ -349,8 +392,14 @@ def process_data():
|
|
|
|
|
|
async def main():
|
|
|
"""运行数据收集循环的主函数"""
|
|
|
+ global trading_strategy
|
|
|
+
|
|
|
logger.info("正在启动行情数据记录器")
|
|
|
|
|
|
+ # 初始化策略
|
|
|
+ trading_strategy = TradingStrategy()
|
|
|
+ logger.info("交易策略已初始化")
|
|
|
+
|
|
|
# 添加每小时打印匹配交易对数量的变量
|
|
|
last_hourly_report_time = time.time()
|
|
|
hourly_matches_count = []
|