|
|
@@ -46,55 +46,17 @@ def on_message_depth(_ws, message):
|
|
|
predict_market_direction()
|
|
|
|
|
|
|
|
|
-def extract_features(order_book, trade):
|
|
|
- # 计算买卖盘差距(spread)
|
|
|
- best_bid = float(order_book['bids'][0][0])
|
|
|
- best_ask = float(order_book['asks'][0][0])
|
|
|
- spread = best_ask - best_bid
|
|
|
-
|
|
|
- # 计算买卖盘深度
|
|
|
- bid_depth = sum(float(bid[1]) for bid in order_book['bids'])
|
|
|
- ask_depth = sum(float(ask[1]) for ask in order_book['asks'])
|
|
|
-
|
|
|
- # 计算成交量和方向
|
|
|
- trade_volume = trade['qty']
|
|
|
- trade_side = 1 if trade['side'] == 'buy' else -1
|
|
|
-
|
|
|
- # 计算买卖盘数量
|
|
|
- bid_count = len(order_book['bids'])
|
|
|
- ask_count = len(order_book['asks'])
|
|
|
-
|
|
|
- # 计算时间特征
|
|
|
- timestamp = trade['timestamp'].timestamp()
|
|
|
-
|
|
|
- features = {
|
|
|
- 'spread': spread,
|
|
|
- 'bid_depth': bid_depth,
|
|
|
- 'ask_depth': ask_depth,
|
|
|
- 'trade_volume': trade_volume,
|
|
|
- 'trade_side': trade_side,
|
|
|
- 'bid_count': bid_count,
|
|
|
- 'ask_count': ask_count,
|
|
|
- 'timestamp': timestamp
|
|
|
- }
|
|
|
-
|
|
|
- return features
|
|
|
-
|
|
|
-
|
|
|
-def generate_label(current_price, future_price):
|
|
|
- return 1 if future_price > current_price else 0
|
|
|
-
|
|
|
-
|
|
|
def predict_market_direction():
|
|
|
global prediction_window
|
|
|
- if len(order_book_snapshots) == 0 or len(trade_data) == 0:
|
|
|
+ if len(trade_data) == 0:
|
|
|
return
|
|
|
|
|
|
- # 模拟一个简单的预测逻辑:如果卖一价高于买一价,则预测价格下跌,否则预测价格上涨
|
|
|
- latest_order_book = order_book_snapshots[-1]
|
|
|
- best_bid = float(latest_order_book['bids'][0][0])
|
|
|
- best_ask = float(latest_order_book['asks'][0][0])
|
|
|
- prediction = 1 if best_ask > best_bid else 0
|
|
|
+ # 统计过去100ms内的买卖交易数量
|
|
|
+ buy_count = sum(trade['qty'] for trade in trade_data if trade['side'] == 'buy')
|
|
|
+ sell_count = sum(trade['qty'] for trade in trade_data if trade['side'] == 'sell')
|
|
|
+
|
|
|
+ # 简单的预测逻辑:买单多则预测上涨,卖单多则预测下跌
|
|
|
+ prediction = 1 if buy_count > sell_count else 0
|
|
|
|
|
|
# 将预测结果添加到滑动窗口中
|
|
|
prediction_window.append(prediction)
|