stock-ai-agent/backend/app/crypto_agent/executor/FEATURE_SUMMARY.md
2026-03-28 22:41:51 +08:00

7.5 KiB
Raw Blame History

执行器移动止损 + 飞书通知完成总结

CREATED: 2026-03-28

已完成的功能

1. 飞书通知集成

所有交易执行操作现在都会自动发送飞书通知:

修改的文件

通知类型

操作 成功通知 失败通知
开仓 (OPEN) 绿色卡片 红色卡片
平仓 (CLOSE) 绿色卡片(含盈亏) 红色卡片
撤单 (CANCEL) 绿色卡片 红色卡片
止盈止损 (TP_SL) 绿色卡片 ⚠️ 橙色卡片
持仓管理 (POSITION_MANAGEMENT) 🔵 蓝色卡片 -

通知内容

标题: ✅ [Bitget] 开仓成功 - BTC
内容:
  平台: Bitget
  交易对: BTC
  订单ID: 1234567890
  数量: 1 张
  价格: $85,000.00
  保证金: $170.00
  杠杆: 5x
  止损: $82,000.00
  止盈: $88,000.00
  订单类型: limit

详细文档: NOTIFICATION_FEATURE.md


2. 移动止损功能

所有平台已实现智能移动止损,当持仓盈利达到 2% 时自动将止损移动到入场价。

修改的文件

触发条件

if pnl_pct >= 2%:
    if side == 'buy' and current_sl < entry_price:
        # 做多:移动止损到入场价
        MOVE_SL  entry_price
    elif side == 'sell' and current_sl > entry_price:
        # 做空:移动止损到入场价
        MOVE_SL  entry_price

平台配置

平台 目标盈利 最大持仓 移动止损触发 移动到
Bitget 3% 6h 盈利 >= 2% 入场价
Hyperliquid 2.5% 4h 盈利 >= 2% 入场价
PaperTrading 3% 4h 盈利 >= 2% 入场价

执行流程

  1. 检查: _check_position_management_all_platforms() (每轮循环)
  2. 触发: 盈利达到 2% 且止损未移动
  3. 执行: 调用 executor.move_stop_loss()
  4. 通知:
    • 日志: "✅ 移动止损成功: BTC → $85,000.00"
    • 告警: "🔒 [Bitget] 移动止损"
    • 飞书: 蓝色卡片通知

详细文档: MOVE_STOP_LOSS_FEATURE.md


📂 新增文件

  1. NOTIFICATION_FEATURE.md - 飞书通知功能完整文档
  2. MOVE_STOP_LOSS_FEATURE.md - 移动止损功能完整文档
  3. FEISHU_NOTIFICATION_INTEGRATION.md (用户创建) - 飞书集成说明

🔧 技术实现

基类新增方法

# 1. 通知发送方法
async def send_execution_notification(operation, symbol, result, details)

# 2. 移动止损抽象方法
@abstractmethod
async def move_stop_loss(symbol, new_stop_loss, current_stop_loss)

各平台实现

Bitget

async def move_stop_loss(symbol, new_stop_loss, current_stop_loss):
    success = self.bitget.modify_sl_tp(
        symbol=symbol.replace('USDT', ''),
        stop_loss=new_stop_loss
    )
    return {'success': success}

API: bitget_trading_api_sdk.modify_sl_tp(symbol, stop_loss, take_profit)

Hyperliquid

async def move_stop_loss(symbol, new_stop_loss, current_stop_loss):
    result = self.hyperliquid.set_tp_sl(
        symbol=symbol.replace('USDT', ''),
        sl_price=new_stop_loss
    )
    return {'success': result.get('success', False)}

API: hyperliquid_trading_service.set_tp_sl(symbol, sl_price)

PaperTrading

async def move_stop_loss(symbol, new_stop_loss, current_stop_loss):
    orders = self.paper_trading.get_active_orders(symbol)
    success_count = 0

    for order in orders:
        result = self.paper_trading.update_order(
            order_id=order.order_id,
            stop_loss=new_stop_loss
        )
        if result.get('success'):
            success_count += 1

    return {'success': success_count > 0}

API: paper_trading_service.update_order(order_id, stop_loss)


🎯 使用示例

示例 1: Bitget 开仓 + 移动止损

1. 信号: BTC 做多, 置信度 85% (A级)
2. 决策:
   - 保证金: $170 (3% of $1074)
   - 杠杆: 5x
   - 入场价: $85,000
   - 止损: $82,000
   - 止盈: $88,000

3. 执行: Bitget 开仓 1 张合约
   → 📢 飞书通知: ✅ [Bitget] 开仓成功 - BTC

4. 价格上涨到 $87,000 (盈利 2.35%)
   → 系统触发移动止损
   → 执行: 移动止损到 $85,000
   → 📢 飞书通知: 🔒 [Bitget] 移动止损
      交易对: BTC
      新止损: $85,000.00
      原因: 盈利 2.35% >= 2%,移动止损到入场价

示例 2: Hyperliquid 平仓通知

1. 持仓: ETH 做多, 入场价 $3,500
2. 当前价: $3,600 (盈利 2.86%)
3. 触发: 持仓管理检查
4. 执行: 自动止盈平仓
   → 📢 飞书通知: ✅ [Hyperliquid] 平仓成功 - ETH
      平台: Hyperliquid
      交易对: ETH
      盈利: $100.00
      收益率: 2.86%
      平仓原因: 盈利 2.86% >= 2.5%

⚠️ 注意事项

1. 飞书 Webhook 配置

确保配置正确的飞书 Webhook URL:

# settings.py
feishu_paper_trading_webhook_url = "https://open.feishu.cn/open-apis/bot/v2/hook/..."
feishu_enabled = True

2. 止损设置

  • Bitget 和 Hyperliquid 需要在开仓时或开仓后设置止损
  • 移动止损只会修改已存在的止损订单
  • 如果没有初始止损,移动止损可能会失败

3. 异步调用

所有执行器方法都是异步的,需要使用 await:

result = await executor.move_stop_loss(symbol, new_sl)
await executor.send_execution_notification(operation, symbol, result)

📊 监控建议

关键指标

  1. 通知成功率: 飞书通知发送成功率
  2. 移动止损触发次数: 统计移动止损执行频率
  3. 移动止损效果: 移动止损后最终盈亏情况

日志监控

# 查看移动止损日志
grep "移动止损成功" logs/crypto_agent.log

# 查看飞书通知日志
grep "飞书消息发送成功" logs/crypto_agent.log

🚀 下一步计划

1. 测试

  • 测试各平台飞书通知是否正常
  • 测试移动止损逻辑是否正确触发
  • 测试失败场景的通知

2. 优化

  • 根据实际使用情况调整移动止损触发阈值
  • 优化飞书通知的展示格式
  • 添加通知失败重试机制

3. 扩展

  • 支持更多平台Binance, OKX 等)
  • 添加邮件/短信通知渠道
  • 实现分级移动止损2% 移到入场价, 4% 移到 +1% 等)

📖 相关文档


📝 变更日志

2026-03-28

  • 为所有平台添加飞书通知功能
  • 实现移动止损功能Bitget, Hyperliquid, PaperTrading
  • 集成持仓管理自动执行crypto_agent.py
  • 创建完整文档
  • 修复 crypto_agent.py 中的移动止损调用逻辑