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