# Delta 对冲优化指南 ## 问题诊断步骤 ### 1. 确认 Delta 检测是否生效 **检查日志关键字:** ```bash grep "Delta Assessment" logs/strategy.log grep "Delta exceeds tolerance" logs/strategy.log ``` **期望输出示例:** ``` 📊 Delta Assessment { symbol: 'BTC', totalPositionBTC: '0.00150', positionValueUSDC: '150.00', weightedDelta: '0.0075%', currentTolerance: '0.5000%', exceedsTolerance: '✅ NO', recommendation: 'hold' } ``` **诊断要点:** - ✅ `Delta Assessment` 日志每个周期都应出现 - ✅ `exceedsTolerance` 为 YES 时应触发对冲 - ❌ 如果没有这些日志,说明 Delta 评估未执行 --- ### 2. 验证对冲信号执行情况 **检查日志关键字:** ```bash grep "Executing signal" logs/strategy.log grep "Signal executed successfully" logs/strategy.log grep "execution failed" logs/strategy.log ``` **常见错误处理:** #### 错误 420: Position not found ``` ⚠️ Reduce-only order failed: position not found (skip retry) ``` **原因:** 试图减仓但账户无持仓 **处理:** SignalExecutor 已自动跳过重试,发出 `position_not_found` 事件 #### 错误 422: Insufficient margin ``` ❌ Order creation failed: Insufficient margin ``` **原因:** 保证金不足 **处理:** 需要减仓或增加保证金 --- ### 3. 核对配置与阈值 #### 当前配置(config/trading-strategy.json) ```json { "deltaRebalancing": { "enabled": true, "checkInterval": 60, "rebalanceThreshold": 0.05, // 5% - 触发再平衡 "maxDeltaDeviation": 0.001 // 0.1% - 目标偏差 } } ``` #### EnhancedDeltaController 配置(config/delta-strategy-config.json) ```json { "deltaControl": { "baseDeltaTolerance": 0.005, // 0.5% - 基础容忍度 "stableDeltaTolerance": 0.005, // 0.5% - 平稳市场 "volatileDeltaTolerance": 0.02, // 2% - 波动市场 "volatilityThreshold": 0.01 // 1% - 波动率阈值 } } ``` #### 优化建议 **问题:敞口持续存在** 1. **降低触发阈值(使用数量基准)** ```json { "deltaRebalancing": { "rebalanceThreshold": 0.01, // 从 5% 降到 1% "maxDeltaDeviation": 0.001 // 保持 0.1% }, "deltaControl": { "baseDeltaTolerance": 0.001, // 从 0.5% 降到 0.1% "stableDeltaTolerance": 0.001 // 从 0.5% 降到 0.1% } } ``` 2. **使用绝对数量阈值(更精确)** 修改策略引擎 Delta 检测逻辑: ```typescript // 当前:基于百分比 const exceedsTolerance = Math.abs(weightedDelta) > currentTolerance; // 建议:基于绝对 BTC 数量 const absoluteThreshold = 0.0001; // 0.0001 BTC = ~$10 USDC const exceedsTolerance = Math.abs(totalPositionBTC) > absoluteThreshold; ``` 3. **增加检查频率** ```json { "intervals": { "deltaMonitoring": 30, // 从 60 秒改为 30 秒 "volumeTrading": 3 // 从 5 秒改为 3 秒 } } ``` --- ### 4. 确认镜像减仓有效性 **检查日志关键字:** ```bash grep "mirror_reduce" logs/strategy.log grep "Reduce-only order" logs/strategy.log grep "订单已取消" logs/strategy.log ``` **镜像减仓流程:** 1. 保证金监控检测到高使用率(>75%) 2. 计算需要减仓的数量(释放30%保证金) 3. 生成 `mirror_reduce` 信号(Account A 和 Account B 同时减仓) 4. 执行 reduce-only 订单 **常见问题:** #### 问题:减仓信号未生成 **原因:** - 保证金监控未启用 - 阈值设置过高 **解决:** ```json { "dynamicPositionAdjustment": { "enabled": false, // ❌ 已禁用轮询 "maxMarginUsageThreshold": 0.75 } } ``` 改为使用 WebSocket 事件驱动(MonitoringManager): ```typescript // 已在 MonitoringManager 中实现 marginMonitor.on('margin_warning', async (data) => { // 自动生成镜像减仓信号 }); ``` #### 问题:减仓订单失败(420) **原因:** 无持仓可减 **解决:** 1. 在减仓前检查持仓状态 2. 跳过无持仓账户的减仓信号 --- ### 5. 实时输出净敞口 #### 增强日志输出 已在策略引擎中添加: ```typescript this.logger.info('📊 Delta Assessment', { symbol: this.config.symbol, totalPositionBTC: totalPositionBTC.toFixed(5), // 净敞口(BTC) positionValueUSDC: (totalPositionBTC * currentPrice).toFixed(2), // 净敞口(USDC) weightedDelta: (deltaAssessment.weightedDelta * 100).toFixed(4) + '%', currentTolerance: (deltaAssessment.currentTolerance * 100).toFixed(4) + '%', exceedsTolerance: deltaAssessment.exceedsTolerance ? '⚠️ YES' : '✅ NO', recommendation: deltaAssessment.recommendation, deltaByTimeframe: deltaAssessment.deltaByTimeframe }); ``` #### 实时监控命令 ```bash # 实时查看 Delta 评估 tail -f logs/strategy.log | grep "Delta Assessment" # 实时查看对冲信号 tail -f logs/strategy.log | grep -E "Delta exceeds|Hedging signal|Executing signal" # 实时查看净敞口 tail -f logs/strategy.log | grep "totalPositionBTC" ``` #### 使用诊断工具 ```bash # 运行 Delta 诊断脚本 npm run diagnose:delta # 或直接运行 tsx scripts/diagnose-delta.ts ``` --- ## 完整诊断流程 ### Step 1: 运行诊断脚本 ```bash tsx scripts/diagnose-delta.ts ``` **期望输出:** ``` 📋 诊断报告 ================================================================================ 时间: 2025-01-15T10:30:00.000Z 【Delta 检测】 启用状态: ✅ 已启用 触发阈值: 5.00% 当前 Delta: 0.0075% 是否超限: ✅ 否 【净敞口】 总 BTC: 0.00150 BTC 总 USDC: 10000.00 USDC 分账户: account1: 0.00080 BTC, 5000.00 USDC account2: 0.00070 BTC, 5000.00 USDC 【建议】 ✅ 配置正常 💡 建议启用详细日志 ``` ### Step 2: 检查实时日志 ```bash # 窗口1: 运行策略 npm run strategy:modular # 窗口2: 监控 Delta tail -f logs/strategy.log | grep "Delta Assessment" # 窗口3: 监控对冲 tail -f logs/strategy.log | grep -E "exceeds tolerance|Hedging|mirror_reduce" ``` ### Step 3: 验证对冲执行 **检查点:** 1. ✅ Delta Assessment 日志每个周期都出现 2. ✅ 当 `exceedsTolerance: YES` 时,看到 "Delta exceeds tolerance" 日志 3. ✅ 看到 "Executing signal" 和 "Signal executed successfully" 日志 4. ✅ 下一个周期的 `totalPositionBTC` 接近 0 **如果对冲未生效:** 1. 检查阈值是否过高(降低到 0.01) 2. 检查是否有执行错误(查看 error 日志) 3. 检查账户余额是否充足 ### Step 4: 调整配置 根据诊断结果调整配置: **敞口持续 >0.001 BTC:** ```json { "deltaRebalancing": { "rebalanceThreshold": 0.01, // 降低阈值 "maxDeltaDeviation": 0.0005 // 更严格目标 } } ``` **频繁误触发:** ```json { "deltaControl": { "volatileDeltaTolerance": 0.03, // 提高波动容忍度 "volatilityThreshold": 0.015 // 提高波动率阈值 } } ``` **对冲执行失败:** ```json { "signalExecutor": { "maxRetries": 5, // 增加重试次数 "retryDelay": 2000, // 增加重试延迟 "minOrderInterval": 1000 // 增加订单间隔 } } ``` --- ## 常见问题 FAQ ### Q1: Delta 评估日志显示正常,但敞口仍然存在? **A:** 可能原因: 1. **对冲信号未生成** - 检查 `exceedsTolerance` 是否为 YES 2. **对冲订单未执行** - 检查执行日志是否有错误 3. **阈值设置过宽** - 降低 `rebalanceThreshold` 到 0.01 4. **使用百分比而非绝对值** - 改用绝对 BTC 数量阈值 ### Q2: 看到 "Delta exceeds tolerance" 但没有对冲订单? **A:** 可能原因: 1. **HedgingDecisionModule 未生成对冲动作** - 检查日志中的 `hedging_decision` 事件 2. **SignalExecutor 未注册账户** - 确认所有账户都已注册 client 3. **对冲对识别失败** - 检查信号的 metadata 是否正确 ### Q3: 对冲订单返回 420 错误? **A:** 这是正常的,表示账户无持仓可减。SignalExecutor 已自动处理: ```typescript // 不重试 420 错误 if (errorMessage.includes('No position found for reduce-only order')) { this.logger.warn('⚠️ Reduce-only order failed: position not found (skip retry)'); this.emit('position_not_found', { accountId, symbol, signal }); return { success: false, ... }; } ``` ### Q4: 如何使用绝对数量而非百分比作为阈值? **A:** 修改策略引擎的 Delta 检测逻辑: ```typescript // src/strategies/SimpleStrategyEngine.ts // 当前逻辑(基于百分比) if (deltaAssessment.exceedsTolerance) { // 生成对冲信号 } // 改为绝对数量(推荐) const ABSOLUTE_THRESHOLD_BTC = 0.0001; // 0.0001 BTC ≈ $10 USDC if (Math.abs(totalPositionBTC) > ABSOLUTE_THRESHOLD_BTC) { // 生成对冲信号 const balanceAmount = Math.abs(totalPositionBTC); // 全部对冲 // ... } ``` ### Q5: 如何确认镜像减仓功能正常工作? **A:** 检查步骤: 1. 确认 MonitoringManager 已初始化并启用保证金监控 2. 手动触发高保证金场景(开大仓位) 3. 查看日志中的 `margin_warning` 或 `margin_critical` 事件 4. 确认生成了 `mirror_reduce` 信号 5. 验证两个账户都执行了 reduce-only 订单 --- ## 推荐配置(严格 Delta 中性) ### trading-strategy.json ```json { "deltaRebalancing": { "enabled": true, "checkInterval": 30, // 30秒检查一次 "rebalanceThreshold": 0.01, // 1% 触发(或使用绝对值 0.0001 BTC) "maxDeltaDeviation": 0.0005 // 0.05% 目标偏差 }, "intervals": { "volumeTrading": 3, // 3秒交易一次 "deltaMonitoring": 30, // 30秒监控一次 "orderCheck": 3 // 3秒检查一次订单 } } ``` ### delta-strategy-config.json ```json { "deltaControl": { "baseDeltaTolerance": 0.001, // 0.1% 基础容忍度(严格) "stableDeltaTolerance": 0.001, // 0.1% 平稳市场 "volatileDeltaTolerance": 0.02, // 2% 波动市场 "volatilityThreshold": 0.01, // 1% 波动率阈值 "deltaTimeframes": [60, 300, 3600], "deltaWeights": [0.5, 0.3, 0.2], "predictiveDelta": true, "imbalanceThreshold": 0.15 } } ``` --- ## 总结 完成以上检查后,如果敞口仍然存在,请提供以下日志片段: 1. **Delta 评估日志**(包含 `exceedsTolerance` 状态) 2. **对冲信号日志**(如果有) 3. **执行失败日志**(如果有) 4. **账户余额和持仓状态** 这些信息将帮助定位具体问题并进行针对性调整。