This commit is contained in:
薇薇安 2026-01-22 20:09:53 +08:00
parent 5d7166d404
commit 43d54bad97

View File

@ -1181,41 +1181,41 @@ async def open_position_from_recommendation(
actual_margin = actual_notional / leverage actual_margin = actual_notional / leverage
# 保存交易记录 # 保存交易记录
trade_id = Trade.create( # trade_id = Trade.create(
account_id=account_id, # account_id=account_id,
symbol=symbol, # symbol=symbol,
side=direction, # side=direction,
quantity=executed_qty, # quantity=executed_qty,
entry_price=avg_price, # entry_price=avg_price,
leverage=leverage, # leverage=leverage,
entry_order_id=order_id, # entry_order_id=order_id,
entry_reason='manual_from_recommendation', # entry_reason='manual_from_recommendation',
notional_usdt=actual_notional, # notional_usdt=actual_notional,
margin_usdt=actual_margin, # margin_usdt=actual_margin,
stop_loss_price=stop_loss_price, # stop_loss_price=stop_loss_price,
# 如果有推荐中的止盈价,也可以传入,这里先不传 # # 如果有推荐中的止盈价,也可以传入,这里先不传
) # )
logger.info(f"✓ 交易记录已保存: trade_id={trade_id}") # logger.info(f"✓ 交易记录已保存: trade_id={trade_id}")
# 尝试挂止损/止盈保护单(如果系统支持) # 尝试挂止损/止盈保护单(如果系统支持)
try: # try:
# 这里可以调用 _ensure_exchange_sltp_for_symbol 来挂保护单 # # 这里可以调用 _ensure_exchange_sltp_for_symbol 来挂保护单
# 但需要先获取持仓信息来确定方向 # # 但需要先获取持仓信息来确定方向
positions = await client.get_open_positions() # positions = await client.get_open_positions()
position = next((p for p in positions if p['symbol'] == symbol), None) # position = next((p for p in positions if p['symbol'] == symbol), None)
if position: # if position:
# 可以在这里挂止损单,但需要知道 take_profit_price # # 可以在这里挂止损单,但需要知道 take_profit_price
# 暂时只记录止损价到数据库,由系统自动监控 # # 暂时只记录止损价到数据库,由系统自动监控
logger.info(f"止损价已记录到数据库: {stop_loss_price}") # logger.info(f"止损价已记录到数据库: {stop_loss_price}")
except Exception as e: # except Exception as e:
logger.warning(f"挂保护单失败(不影响开仓): {e}") # logger.warning(f"挂保护单失败(不影响开仓): {e}")
return { return {
"message": f"{symbol} 开仓成功", "message": f"{symbol} 开仓成功",
"symbol": symbol, "symbol": symbol,
"order_id": order_id, "order_id": order_id,
"trade_id": trade_id, "trade_id": None,
"quantity": executed_qty, "quantity": executed_qty,
"entry_price": avg_price, "entry_price": avg_price,
"notional_usdt": actual_notional, "notional_usdt": actual_notional,