mcpField
T
test-trading-strategies

test-trading-strategies

github.com/robonet-tech数据与数据库156 次安装39 周前更新
代码仓库

关于 test-trading-strategies

Test Trading Strategies 是一项技能,可在部署前用历史数据评估加密货币及预测市场策略。它提供回测、绩效指标,并可查看近期结果以作比较,避免重复运行测试。

主要功能

  1. 01回测加密货币永续策略
  2. 02回测 Polymarket 策略
  3. 03报告收益与风险指标
  4. 04查看近期结果和权益曲线
  5. 05比较策略版本及测试区间

适用场景

  1. 01部署前筛查新策略
  2. 02确认策略修改是否改善表现
  3. 03比较不同时间区间的表现
  4. 04避免重复运行已有回测

MCP 相关

该技能使用 MCP 工具运行加密货币和预测市场回测,并获取近期结果。它引导用户通过 MCPSearch 选择所需工具。

安装

复制下面的命令,在终端中运行。

$npx skills add https://github.com/robonet-tech/skills --skill test-trading-strategies

安装后在 Claude Code 中输入/test-trading-strategies确认已加载。

详细介绍

测试交易策略

快速入门

此技能可在投入真实资金前,使用历史数据验证策略表现。测试快速(20–60 秒)、成本低($0.001),是安全交易的必要步骤。

先加载工具:

Use MCPSearch to select: mcp__workbench__run_backtest
Use MCPSearch to select: mcp__workbench__get_latest_backtest_results

基本回测:

run_backtest(
    strategy_name="MyStrategy",
    start_date="2024-01-01",
    end_date="2024-12-31",
    symbol="BTC-USDT",
    timeframe="1h"
)

在 20–40 秒内返回表现指标:

  • 夏普比率:1.4(风险调整后收益良好)
  • 最大回撤:12%(风险适中)
  • 胜率:52%(符合实际)
  • 盈利因子:1.8(盈利)

使用此技能的时机:

  • 构建新策略后(验证策略是否有效)
  • 改进策略后(确认改进效果)
  • 部署到实盘交易前(务必进行)
  • 比较多个策略版本
  • 测试不同参数

关键规则:部署前绝不可跳过对至少 6 个月数据的回测

可用工具(3)

run_backtest

用途:使用历史数据测试加密货币交易策略的表现

参数:

  • strategy_name(必填):要测试的策略
  • start_date(必填):开始日期(YYYY-MM-DD)
  • end_date(必填):结束日期(YYYY-MM-DD)
  • symbol(必填):交易对(例如:"BTC-USDT")
  • timeframe(必填):时间周期(1m、5m、15m、30m、1h、2h、4h、6h、8h、12h、1d)
  • config(可选):回测配置对象:
    • fee:每边交易手续费(默认:0.0005 = 0.05%)
    • slippage:每笔交易的滑点(默认:0.0005 = 0.05%)
    • leverage:仓位倍数(默认:1,最大:5)

返回:表现指标:

  • 净利润:以 USDC 计的总盈亏
  • 总收益率:收益百分比
  • 年化收益率:年化收益百分比
  • 夏普比率:风险调整后收益(业界标准指标)
  • 最大回撤:从峰值到谷值的最大跌幅
  • 胜率:盈利交易的百分比
  • 盈利因子:总盈利 / 总亏损
  • 交易统计:交易总数、平均交易时长、连续亏损次数
  • 权益曲线:随时间变化的余额(用于可视化)

定价:$0.001(几乎免费)

执行时间:约 20–40 秒

适用场景:测试 Hyperliquid 上的加密货币永续合约策略

run_prediction_market_backtest

用途:使用历史数据测试 Polymarket 预测市场策略

参数:

  • strategy_name(必填):要测试的 PolymarketStrategy
  • start_date(必填):开始日期(YYYY-MM-DD)
  • end_date(必填):结束日期(YYYY-MM-DD)
  • condition_id(用于单个市场):特定的 Polymarket condition ID
  • asset(用于滚动市场):资产代码("BTC"、"ETH")
  • interval(用于滚动市场):市场间隔("15m"、"1h")
  • initial_balance(可选):初始 USDC 余额(默认:10000)
  • timeframe(可选):执行时间周期(默认:1m)

返回:回测指标:

  • 盈亏
  • 胜率
  • YES/NO 代币的仓位历史
  • 市场结算结果

定价:$0.001

执行时间:约 20–60 秒

适用场景:测试 Polymarket 预测市场策略

get_latest_backtest_results

用途:查看近期回测结果,无需重新运行

参数:

  • strategy_name(可选):按策略名称筛选
  • limit(可选,1–100):结果数量(默认:10)
  • include_equity_curve(可选):包含权益曲线数据
  • equity_curve_max_points(可选,50–1000):曲线分辨率

返回:包含指标的近期回测记录列表

定价:免费

适用场景:检查是否已有回测结果、比较策略、避免重复回测

核心概念

表现指标解读

夏普比率(风险调整后收益):

Formula: (Mean Return - Risk-Free Rate) / Standard Deviation of Returns

Interpretation:
>2.0  → Excellent (very rare for algo strategies)
1.0-2.0 → Good (achievable with solid strategy)
0.5-1.0 → Acceptable (worth testing further)
<0.5  → Poor (likely not profitable after costs)

Why it matters:
- Accounts for volatility (high return with high volatility = lower Sharpe)
- Industry standard for comparing strategies
- More useful than total return alone

最大回撤(从峰值到谷值的最大跌幅):

Example: Strategy grows from $10k → $15k → $12k
Drawdown: ($15k - $12k) / $15k = 20%

Interpretation:
<10%  → Conservative (lower returns, safer)
10-20% → Moderate (balanced risk/reward)
20-40% → Aggressive (higher returns, higher risk)
>40%  → Very risky (difficult to recover from)

Why it matters:
- Measures worst-case scenario
- Predicts emotional difficulty of holding strategy
- 50% drawdown requires 100% return to recover

胜率(盈利交易的百分比):

Formula: (Winning Trades / Total Trades) × 100%

Interpretation:
45-65% → Realistic for most strategies
>70%  → Suspicious (possible overfitting or unrealistic fills)
<40%  → Needs improvement (unless very high profit factor)

Why it matters:
- High win rate doesn't guarantee profitability
- Can have 40% win rate but profitable (if winners > losers)
- Very high win rate (>75%) often indicates overfitting

Common misconception: Higher is always better
Reality: 40% win rate with 3:1 reward:risk is better than 60% win rate with 1:1

盈利因子(总盈利 / 总亏损):

Formula: Sum of All Winning Trades / Sum of All Losing Trades

Interpretation:
>2.0  → Excellent
1.5-2.0 → Good
1.2-1.5 → Acceptable
<1.2  → Marginal (risky to deploy)
<1.0  → Unprofitable (losses exceed profits)

Why it matters:
- Simple profitability measure
- <1.5 means small edge, vulnerable to slippage/fees
- Combines win rate and win size into single metric

Example:
10 trades: 6 winners ($100 each), 4 losers ($50 each)
Gross profit: $600, Gross loss: $200
Profit factor: $600 / $200 = 3.0 (excellent)

总收益率与年化收益率:

Total Return: 50% over 6 months
Annual Return: ~100% (extrapolated to 12 months)

Why both matter:
- Total return: Actual profit over test period
- Annual return: Standardized for comparison across time periods
- Longer test periods more reliable (6-12 months minimum)
测试方法

最低数据要求:

Quick test: 1-3 months
- Limited validation
- Use for initial screening only
- High risk of luck/overfitting

Standard test: 6-12 months (RECOMMENDED MINIMUM)
- Captures multiple market regimes
- Sufficient trades for statistical significance
- Industry standard for strategy validation

Robust test: 12-24 months
- Ideal for high-confidence validation
- Includes bull, bear, and ranging markets
- Best for strategies before live deployment

多周期测试(对验证稳健性至关重要):

1. Train period: 2024-01-01 to 2024-08-31
   run_backtest(..., start_date="2024-01-01", end_date="2024-08-31")
   → Sharpe: 1.5

2. Validation period: 2024-09-01 to 2024-12-31
   run_backtest(..., start_date="2024-09-01", end_date="2024-12-31")
   → Sharpe: 1.3

3. Compare:
   Performance similar → Robust strategy ✓
   Performance degraded significantly → Overfit to train period ✗

市场状态测试:

Test strategy across different market conditions:

1. Trending up (bull market): 2023-10 to 2024-03
   → Sharpe: 1.8

2. Trending down (bear market): 2024-04 to 2024-07
   → Sharpe: 0.9

3. Ranging (sideways): 2024-08 to 2024-12
   → Sharpe: 1.1

Analysis:
- Works well in all regimes ✓
- Or works in specific regime (trend-following good in trends)
- Fails in all regimes → Fundamentally broken ✗
危险信号(过拟合指标)

回测结果可能无法持续的警告信号:

1. 胜率高得不切实际(>70%):

Win rate: 82%
Problem: Markets are noisy; >70% suggests strategy memorized past data
Solution: Test on out-of-sample data; expect performance degradation

2. 交易次数过少(6 个月内 <20 次):

Total trades: 8 over 6 months
Problem: Not enough data for statistical significance; could be luck
Solution: Test longer period or adjust strategy to generate more trades

3. 回测表现出色,样本外表现糟糕:

Train period (Jan-Aug): Sharpe 2.5
Test period (Sep-Dec): Sharpe 0.3
Problem: Overfitted to training data
Solution: Simplify strategy, reduce parameters, test on more data

4. 表现集中在短时间内:

6-month test: 50% return
- Month 1-5: -5% return
- Month 6: 55% return (one lucky trade)
Problem: Performance driven by single event, not consistent edge
Solution: Analyze equity curve; look for consistent growth, not spikes

5. 策略复杂度与表现不匹配:

Strategy uses 12 indicators, 20+ parameters
Sharpe ratio: 1.3 (only modest improvement)
Problem: Complex strategies should dramatically outperform simple ones
Solution: Simplify; complexity without performance = overfitting

6. 回测完美,实盘交易却失败:

Backtest: Win rate 75%, Sharpe 2.3
Live: Win rate 45%, Sharpe 0.6
Problem: Backtest didn't account for slippage, fees, execution delays
Solution: Use realistic fees (0.05-0.1%), slippage (0.05-0.1%), and test on higher timeframes
部署前验证清单

部署到实盘交易前,请确认:

  • 回测时长:至少测试 6 个月(最好 12 个月以上)
  • 夏普比率:>1.0(最好 >1.5)
  • 最大回撤:<20%(可接受的风险水平)
  • 胜率:45-65%(合理范围)
  • 盈利因子:>1.5(具备足够优势)
  • 交易次数:测试期间 50 笔以上(具备统计显著性)
  • 多周期验证:在多个时间范围内测试,结果保持一致
  • 样本外测试:在开发过程中未使用的数据上表现良好
  • 市场环境测试:在不同市场环境下均可运行(或了解其失效时机)
  • 合理的手续费:配置为实际交易手续费(0.05-0.1%)
  • 合理的滑点:配置为预期滑点(0.05-0.1%)
  • 无危险信号:胜率不超过 70%、交易次数充足、表现稳定
  • 审查资金曲线:增长稳定,而非由单笔幸运交易驱动
  • 已验证风险管理:止损和仓位规模设置合理

如果任何一项未通过,请勿部署。请先改进策略。

最佳实践

配置最佳实践

合理设置手续费和滑点:

config = {
    "fee": 0.0005,      # 0.05% per trade (Hyperliquid taker fee)
    "slippage": 0.0005,  # 0.05% slippage (liquid markets)
    "leverage": 1        # Start with 1x (no leverage)
}

run_backtest(
    ...,
    config=config
)

合理配置的重要性:

Without fees/slippage:
- Backtest: 50% return, Sharpe 2.0
- Reality: Fees eat 5-10% of profit → 40% return, Sharpe 1.5

With realistic fees/slippage:
- Backtest: 40% return, Sharpe 1.5
- Reality: Matches expectation → 38-42% return

杠杆测试:

# Test without leverage first
run_backtest(..., config={"leverage": 1})
→ Sharpe: 1.5, Drawdown: 12%

# Then test with leverage (if deploying with leverage)
run_backtest(..., config={"leverage": 2})
→ Sharpe: 1.4, Drawdown: 24% (doubled)

Risk assessment:
- Leverage amplifies returns AND drawdowns
- 2x leverage doesn't mean 2x Sharpe (risk increases faster)
- Start deployment at 1x, increase cautiously
比较策略版本

系统化比较:

1. Backtest all versions on SAME date range:
   run_backtest(strategy_name="Strategy_v1", start_date="2024-01-01", end_date="2024-12-31", ...)
   run_backtest(strategy_name="Strategy_v2", start_date="2024-01-01", end_date="2024-12-31", ...)
   run_backtest(strategy_name="Strategy_v3", start_date="2024-01-01", end_date="2024-12-31", ...)

2. Compare all metrics (not just one):
   | Version | Sharpe | Drawdown | Win Rate | Profit Factor |
   |---------|--------|----------|----------|---------------|
   | v1      | 1.2    | 15%      | 50%      | 1.6           |
   | v2      | 1.5    | 12%      | 52%      | 1.8           |
   | v3      | 1.8    | 25%      | 48%      | 2.2           |

3. Analyze trade-offs:
   v1: Baseline (acceptable)
   v2: Better across all metrics ✓ (clear winner)
   v3: Higher Sharpe but excessive drawdown ✗ (too risky)

4. Decision:
   Deploy v2 (balanced improvement without excessive risk)
避免重复回测

检查是否已有回测结果:

1. Before running backtest:
   get_latest_backtest_results(strategy_name="MyStrategy")

2. Review results:
   - If recent backtest exists with same parameters → Use cached result
   - If parameters differ (date range, symbol, timeframe) → Run new backtest

3. Saves time and clutter:
   - Backtests are fast (20-40s) but avoiding duplicates is cleaner
   - Easier to find specific backtest results later

常见工作流

工作流 1:初步策略验证

目标:首次测试新创建的策略

1. Check data availability (use browse-robonet-data):
   get_data_availability(symbols=["BTC-USDT"], only_with_data=true)
   → Verify 6+ months of history available

2. Run initial backtest (6 months):
   run_backtest(
       strategy_name="NewStrategy",
       start_date="2024-06-01",
       end_date="2024-12-31",
       symbol="BTC-USDT",
       timeframe="1h",
       config={"fee": 0.0005, "slippage": 0.0005, "leverage": 1}
   )

3. Evaluate results:
   Sharpe: 1.3 ✓ (good)
   Drawdown: 14% ✓ (moderate)
   Win rate: 51% ✓ (realistic)
   Profit factor: 1.7 ✓ (profitable)
   Total trades: 87 ✓ (sufficient)

4. Decision:
   → Strong initial results
   → Proceed to multi-period validation (Workflow 2)

成本:$0.001(几乎免费)

工作流 2:多周期验证

目标:验证策略在不同时间段内的稳健性

1. Test Period 1 (Train):
   run_backtest(..., start_date="2024-01-01", end_date="2024-06-30")
   → Sharpe: 1.5, Drawdown: 12%

2. Test Period 2 (Validation):
   run_backtest(..., start_date="2024-07-01", end_date="2024-12-31")
   → Sharpe: 1.3, Drawdown: 15%

3. Compare:
   Period 2 slightly worse but consistent ✓
   Sharpe drop: 13% (acceptable variation)
   Drawdown increase: 3% (acceptable)

4. Test Period 3 (Recent):
   run_backtest(..., start_date="2024-10-01", end_date="2024-12-31")
   → Sharpe: 1.4, Drawdown: 11%

5. Analysis:
   Consistent performance across all periods ✓
   No significant degradation ✓
   Strategy is robust ✓

6. Decision:
   → Ready for deployment consideration
   → Review pre-deployment checklist

成本:$0.003(3 次回测)

工作流 3:改进前后测试

目标:验证改进是否确实有效

1. Baseline (before improvement):
   run_backtest(
       strategy_name="Strategy_original",
       start_date="2024-01-01",
       end_date="2024-12-31",
       ...
   )
   → Sharpe: 1.0, Drawdown: 18%, Win rate: 48%

2. Improve strategy (use improve-trading-strategies):
   refine_strategy(strategy_name="Strategy_original", changes="Add trailing stop", mode="new")

3. Test improvement:
   run_backtest(
       strategy_name="Strategy_original_refined",
       start_date="2024-01-01",  # SAME date range!
       end_date="2024-12-31",
       ...
   )
   → Sharpe: 1.3, Drawdown: 14%, Win rate: 52%

4. Compare (apples-to-apples on same data):
   Sharpe: +0.3 (+30%) ✓
   Drawdown: -4% (-22%) ✓
   Win rate: +4% (+8%) ✓
   → Clear improvement across all metrics

5. Validate on different period (avoid overfitting to test data):
   run_backtest(
       strategy_name="Strategy_original_refined",
       start_date="2023-07-01",  # Different period
       end_date="2023-12-31",
       ...
   )
   → Sharpe: 1.2 (still better than original's 1.0)
   → Improvement is real, not overfitted

6. Decision:
   → Keep improved version
   → Consider further optimization or deployment

成本:$0.003(3 次回测)

工作流 4:参数敏感性测试

目标:了解策略对参数的敏感程度

1. Baseline (default parameters):
   Strategy uses RSI(14) threshold of 30
   run_backtest(...) → Sharpe: 1.3

2. Test parameter variations:
   Create variants: RSI threshold 25, 30, 35

   run_backtest(strategy_name="Strategy_RSI25", ...) → Sharpe: 1.1
   run_backtest(strategy_name="Strategy_RSI30", ...) → Sharpe: 1.3
   run_backtest(strategy_name="Strategy_RSI35", ...) → Sharpe: 1.2

3. Analysis:
   Performance varies only slightly (1.1 to 1.3)
   → Strategy is robust (not overly sensitive to exact parameters) ✓

   vs. High sensitivity:
   RSI25: Sharpe 2.5
   RSI30: Sharpe 1.3
   RSI35: Sharpe 0.4
   → Overfitted to specific parameter value ✗

4. Decision:
   Robust strategy (small variation) → Safe to deploy
   Sensitive strategy (large variation) → Likely overfit, risky to deploy

成本:$0.003(3 次回测)

故障排除

“无可用数据”

问题:回测因“数据不足”而失败

解决方案:

1. Check data availability first (use browse-robonet-data):
   get_data_availability(symbols=["YOUR-SYMBOL"], only_with_data=true)

2. Adjust date range:
   - BTC-USDT, ETH-USDT: Available from 2020-present
   - Altcoins: Typically 6-24 months
   - Use date range within available data

3. Try different symbol:
   - BTC-USDT and ETH-USDT have longest history
   - Start testing on these, then expand to altcoins
“未生成交易”

问题:回测已完成,但没有执行任何交易

解决方案:

1. Entry conditions too restrictive:
   - Review strategy code (use browse-robonet-data: get_strategy_code)
   - Conditions may never be met simultaneously
   - Example: "RSI < 20 AND price > 200 EMA" (RSI rarely gets to 20)

2. Test on longer period:
   - 6 months may not have ideal conditions
   - Try 12-24 months

3. Adjust thresholds (use improve-trading-strategies):
   - Loosen entry conditions slightly
   - Example: Change "RSI < 25" to "RSI < 30"
“回测耗时超过 2 分钟”

问题:回测运行时间过长

解决方案:

1. Long date range + high-frequency timeframe:
   - 2+ years on 1m timeframe = slow
   - Solution: Test shorter range or use 5m/15m timeframe

2. Complex strategy with many indicators:
   - Some indicators are computationally expensive
   - Solution: Simplify strategy if possible

3. Normal for prediction markets:
   - run_prediction_market_backtest can take 30-60s
   - This is expected
“结果看起来好得不真实”

问题:夏普比率 >3.0、胜率 >75%、盈利因子 >5.0

解决方案:

1. Likely overfitted to historical data
2. Test on out-of-sample period (different dates)
3. Check for look-ahead bias (using future data)
4. Verify realistic fees and slippage configured
5. If too-good-to-be-true persists, be very skeptical
6. Start with tiny deployment size to validate in live market

后续步骤

策略回测完成后:

改进表现不佳的策略:

  • 使用 improve-trading-strategies skill 进行优化
  • 每次操作成本:$0.50-$4.00
  • 再次使用此技能测试改进效果

部署通过验证的策略(高风险):

  • 只有经过全面测试后,才能使用 deploy-live-trading skill
  • 部署费用:$0.50
  • 确认部署前清单中的所有项目均已通过
  • 从少量资金开始,并密切监控

浏览其他策略:

  • 使用 browse-robonet-data skill 查看现有策略
  • 将你的结果与其他策略进行比较
  • 借鉴表现优异的策略

摘要

此技能通过回测进行策略验证:

  • 3 个工具:run_backtest(加密货币)、run_prediction_market_backtest(Polymarket)、get_latest_backtest_results(缓存)
  • 成本:每次回测 $0.001(几乎免费)
  • 执行时间:20-60 秒
  • 用途:在投入资金前验证策略表现

核心原则:充分回测(6 个月以上,覆盖多个时段)是验证策略的唯一方法。过去的表现无法保证未来的结果,但过去缺乏良好表现则意味着未来必然亏损。

重要警告:切勿在未经回测的情况下部署策略。回测成本低廉($0.001)且速度快(20-60 秒)。部署未经测试的策略会让真实资金面临风险,而且几乎肯定会导致亏损。

部署前检查清单:确认夏普比率 >1.0、回撤 <20%、胜率 45-65%、利润因子 >1.5、交易次数 50 次以上、测试覆盖 6 个月以上、通过多时段验证、采用现实的费用和滑点假设,且不存在风险信号。如果有任何一项不达标,请在部署前改进策略。

最佳实践:测试 → 改进 → 测试 → 改进(迭代)。每次改进后,都应使用相同的数据进行新的回测,以确认确实有所提升,而非仅仅是噪声。

评论

支持 Markdown