|
|
@@ -241,10 +241,60 @@ class TradingStrategy:
|
|
|
except Exception as e:
|
|
|
logger.error(f"预签名订单时发生错误: {str(e)}")
|
|
|
|
|
|
+ async def _update_bps_from_order_book(self, market_data):
|
|
|
+ """
|
|
|
+ 从order_book中提取买卖价并计算价差(bps)
|
|
|
+ 将结果存储到实例变量 current_ask_bps 和 current_bid_bps
|
|
|
+
|
|
|
+ Args:
|
|
|
+ market_data: 市场数据字典
|
|
|
+ """
|
|
|
+ try:
|
|
|
+ binance_price = market_data.get('binance_price')
|
|
|
+ order_book = market_data.get('lighter_order_book', {})
|
|
|
+
|
|
|
+ # 提取order_book中的ask1和bid1
|
|
|
+ lighter_bid = None
|
|
|
+ lighter_ask = None
|
|
|
+
|
|
|
+ if order_book:
|
|
|
+ # 从order_book中提取买卖价
|
|
|
+ if 'bids' in order_book and order_book['bids']:
|
|
|
+ best_bid = order_book.get_best_bid()
|
|
|
+ if isinstance(best_bid, dict) and 'price' in best_bid:
|
|
|
+ lighter_bid = float(best_bid['price'])
|
|
|
+
|
|
|
+ if 'asks' in order_book and order_book['asks']:
|
|
|
+ best_ask = order_book.get_best_ask()
|
|
|
+ if isinstance(best_ask, dict) and 'price' in best_ask:
|
|
|
+ lighter_ask = float(best_ask['price'])
|
|
|
+
|
|
|
+ # 计算价差(bps) - 分别计算ask_bps和bid_bps
|
|
|
+ ask_bps = None
|
|
|
+ bid_bps = None
|
|
|
+ if binance_price and lighter_ask:
|
|
|
+ binance_price_float = float(binance_price) if isinstance(binance_price, str) else binance_price
|
|
|
+ # ask_bps: lighter卖价相对于binance价格的价差
|
|
|
+ ask_bps = (lighter_ask - binance_price_float) / binance_price_float * 10000 if binance_price_float else 0
|
|
|
+
|
|
|
+ if binance_price and lighter_bid:
|
|
|
+ binance_price_float = float(binance_price) if isinstance(binance_price, str) else binance_price
|
|
|
+ # bid_bps: lighter买价相对于binance价格的价差
|
|
|
+ bid_bps = (lighter_bid - binance_price_float) / binance_price_float * 10000 if binance_price_float else 0
|
|
|
+
|
|
|
+ # 存储最新的价差数据到实例变量,用于开单逻辑判断
|
|
|
+ if ask_bps is not None:
|
|
|
+ self.current_ask_bps = ask_bps
|
|
|
+ if bid_bps is not None:
|
|
|
+ self.current_bid_bps = bid_bps
|
|
|
+
|
|
|
+ except Exception as e:
|
|
|
+ logger.error(f"更新价差数据失败: {e}")
|
|
|
+
|
|
|
async def do_strategy(self, market_data):
|
|
|
"""
|
|
|
执行策略逻辑
|
|
|
-
|
|
|
+
|
|
|
Args:
|
|
|
market_data: 包含市场数据的字典,格式:
|
|
|
{
|
|
|
@@ -256,12 +306,15 @@ class TradingStrategy:
|
|
|
"""
|
|
|
if not market_data:
|
|
|
return
|
|
|
-
|
|
|
+
|
|
|
symbol = market_data.get('symbol')
|
|
|
# 如果不是目标交易对,直接返回
|
|
|
if symbol != self.target_symbol:
|
|
|
return
|
|
|
-
|
|
|
+
|
|
|
+ # 在最前面计算和更新价差数据
|
|
|
+ await self._update_bps_from_order_book(market_data)
|
|
|
+
|
|
|
# 基于账户信息更新 current_position 和状态
|
|
|
await self._update_position_and_state(market_data)
|
|
|
|
|
|
@@ -382,46 +435,31 @@ class TradingStrategy:
|
|
|
symbol = market_data.get('symbol')
|
|
|
binance_price = market_data.get('binance_price')
|
|
|
order_book = market_data.get('lighter_order_book', {})
|
|
|
-
|
|
|
+
|
|
|
# 提取order_book中的ask1和bid1
|
|
|
lighter_bid = None
|
|
|
lighter_ask = None
|
|
|
-
|
|
|
+
|
|
|
if order_book:
|
|
|
# 从order_book中提取买卖价
|
|
|
if 'bids' in order_book and order_book['bids']:
|
|
|
- best_bid = order_book['bids'][0]
|
|
|
+ best_bid = order_book.get_best_bid()
|
|
|
if isinstance(best_bid, dict) and 'price' in best_bid:
|
|
|
lighter_bid = float(best_bid['price'])
|
|
|
-
|
|
|
+
|
|
|
if 'asks' in order_book and order_book['asks']:
|
|
|
- best_ask = order_book['asks'][0]
|
|
|
+ best_ask = order_book.get_best_ask()
|
|
|
if isinstance(best_ask, dict) and 'price' in best_ask:
|
|
|
lighter_ask = float(best_ask['price'])
|
|
|
-
|
|
|
- # 计算价差(bps) - 分别计算ask_bps和bid_bps
|
|
|
- ask_bps = None
|
|
|
- bid_bps = None
|
|
|
- if binance_price and lighter_ask:
|
|
|
- binance_price_float = float(binance_price) if isinstance(binance_price, str) else binance_price
|
|
|
- # ask_bps: lighter卖价相对于binance价格的价差
|
|
|
- ask_bps = (lighter_ask - binance_price_float) / binance_price_float * 10000 if binance_price_float else 0
|
|
|
-
|
|
|
- if binance_price and lighter_bid:
|
|
|
- binance_price_float = float(binance_price) if isinstance(binance_price, str) else binance_price
|
|
|
- # bid_bps: lighter买价相对于binance价格的价差
|
|
|
- bid_bps = (lighter_bid - binance_price_float) / binance_price_float * 10000 if binance_price_float else 0
|
|
|
-
|
|
|
- # 存储最新的价差数据到实例变量,用于开单逻辑判断
|
|
|
- if ask_bps is not None:
|
|
|
- self.current_ask_bps = ask_bps
|
|
|
- if bid_bps is not None:
|
|
|
- self.current_bid_bps = bid_bps
|
|
|
-
|
|
|
+
|
|
|
+ # 使用已经计算好的价差数据(在do_strategy早期已计算)
|
|
|
+ ask_bps = self.current_ask_bps
|
|
|
+ bid_bps = self.current_bid_bps
|
|
|
+
|
|
|
# 先打印数据看看效果,暂不记录到数据库
|
|
|
binance_price_float = float(binance_price) if binance_price else None
|
|
|
# print(f"价格数据: symbol={symbol}, binance_price={binance_price_float}, lighter_ask={lighter_ask}, lighter_bid={lighter_bid}, ask_bps={ask_bps}, bid_bps={bid_bps}")
|
|
|
-
|
|
|
+
|
|
|
self.database.record_price_data(
|
|
|
symbol=symbol,
|
|
|
binance_price=binance_price_float,
|
|
|
@@ -430,7 +468,7 @@ class TradingStrategy:
|
|
|
ask_bps=ask_bps,
|
|
|
bid_bps=bid_bps
|
|
|
)
|
|
|
-
|
|
|
+
|
|
|
except Exception as e:
|
|
|
logger.error(f"记录价格数据失败: {e}")
|
|
|
|