关于 build-trading-strategies
用于根据详细要求生成加密货币交易和 Polymarket 策略代码的技能,面向已有明确策略构想的用户,并按相应交易逻辑生成 Python 代码。
主要功能
- 01生成加密货币策略代码
- 02生成 Polymarket 策略代码
- 03支持进场与退出规则
- 04包含仓位规模和风险逻辑
适用场景
- 01将明确的加密货币策略转为 Python 代码
- 02为 Polymarket 创建 YES/NO 代币交易逻辑
MCP 相关
通过 MCPSearch 选择 `mcp__workbench__create_strategy` 和 `mcp__workbench__create_prediction_market_strategy` 工具。
安装
复制下面的命令,在终端中运行。
npx skills add https:/ / github.com/ robonet-tech/ skills --skill build-trading-strategies安装后在 Claude Code 中输入/build-trading-strategies确认已加载。
详细介绍
构建交易策略
快速入门
此技能使用 AI 生成完整、可用于生产环境的策略代码。这是 Robonet 中成本最高的工具(每次生成 $1-$4.50)。
先加载工具:
Use MCPSearch to select: mcp__workbench__create_strategy
Use MCPSearch to select: mcp__workbench__create_prediction_market_strategy
基本用法:
create_strategy(
strategy_name="RSIMeanReversion_M",
description="Buy when RSI(14) < 30 and price at lower Bollinger Band (20,2).
Sell when RSI > 70 or price at middle Bollinger Band.
Stop loss at 2% below entry. Position size 90% of available margin."
)
返回可直接用于回测的完整 Python 策略代码。
适用场景:
- 你有明确的策略概念,并希望获得可运行的代码
- 你已探索过数据,并知道要使用哪些指标/交易对
- 你准备好投入高额开发费用($1-$4.50)
不适用场景:
- 你仍在探索想法 → 先使用
design-trading-strategies($0.05-$1.00) - 你有现有代码需要改进 → 使用
improve-trading-strategies($0.50-$3.00) - 你尚未检查数据是否可用 → 先使用
browse-robonet-data(免费-$0.001)
可用工具(2 个)
create_strategy
用途:使用 AI 生成完整的加密货币交易策略代码
参数:
strategy_name(必填,字符串):名称遵循{Name}_{RiskLevel}[_suffix]模式- 风险等级:H(高)、M(中)、L(低)
- 示例:"RSIMeanReversion_M"、"MomentumBreakout_H_v2"
description(必填,字符串):详细要求,包括:- 入场条件(具体指标值和阈值)
- 离场条件(止损、止盈、跟踪止损)
- 仓位规模(保证金使用比例)
- 风险管理(每笔交易的最大亏损)
- 要使用的指标(使用 browse-robonet-data 中的确切名称)
- 时间周期背景(5m 剥头皮交易与 1h 波段交易)
返回:完整的 Python 策略代码,包括:
should_long()- 检查是否满足做多入场条件should_short()- 检查是否满足做空入场条件go_long()- 按仓位规模执行做多入场go_short()- 按仓位规模执行做空入场- 可选方法:
on_open_position()、update_position()、should_cancel_entry()
定价:实际 LLM 成本 + 利润(最高 $4.50)
- 典型成本:$1.00-$3.00,具体取决于复杂程度
- Robonet 中成本最高的工具
执行时间:约 30-60 秒
适用场景:
- 构建新的加密货币永续合约交易策略
- 你有清晰、详细的要求
- 你已确认指标/交易对可用(browse-robonet-data)
- 你准备好投入高额费用执行操作
create_prediction_market_strategy
用途:生成包含 YES/NO 代币交易逻辑的 Polymarket 策略代码
参数:
strategy_name(必填,字符串):名称遵循与 create_strategy 相同的模式description(必填,字符串):YES/NO 代币逻辑的详细要求:- 买入 YES 代币的条件(概率阈值)
- 买入 NO 代币的条件
- 离场标准(利润目标、基于时间的离场)
- 仓位规模(每个市场的比例)
- 市场选择标准(类别、流动性要求)
返回:完整的 Python 策略代码,包括:
should_buy_yes()- 检查是否满足买入 YES 代币的条件should_buy_no()- 检查是否满足买入 NO 代币的条件go_yes()- 按仓位规模执行 YES 代币买入go_no()- 按仓位规模执行 NO 代币买入- 可选方法:
should_sell_yes()、should_sell_no()、on_market_resolution()
定价:实际 LLM 成本 + 利润(最高 $4.50)
- 典型成本:$1.00-$3.00
执行时间:约 30-60 秒
适用场景:
- 构建 Polymarket 预测市场策略
- 交易现实世界事件(政治、经济、体育)
- 希望根据概率分析持有 YES/NO 代币头寸
核心概念
Jesse 框架结构
所有加密货币策略都必须实现以下必需方法:
class MyStrategy(Strategy):
def should_long(self) -> bool:
"""Check if all conditions are met for long entry"""
# Return True to signal long entry opportunity
# Called every candle
def should_short(self) -> bool:
"""Check if all conditions are met for short entry"""
# Return True to signal short entry opportunity
# Called every candle
def go_long(self):
"""Execute long entry with position sizing"""
# Calculate position size (qty)
# Place buy order
# Set stop loss and take profit in on_open_position()
def go_short(self):
"""Execute short entry with position sizing"""
# Calculate position size (qty)
# Place sell order
# Set stop loss and take profit in on_open_position()
可选但建议实现的方法:
def on_open_position(self, order):
"""Set stop loss and take profit after entry"""
# Called when position opens
# Set self.stop_loss and self.take_profit
def update_position(self):
"""Update position (trailing stops, etc.)"""
# Called every candle while in position
# Modify stop loss for trailing stops
def should_cancel_entry(self) -> bool:
"""Cancel unfilled entry orders"""
# Return True to cancel pending entry order
策略命名约定
遵循此模式:{Name}_{RiskLevel}[_suffix]
风险级别:
- H(高):激进策略、高杠杆、止损紧,接受超过 20% 的回撤
- M(中):平衡策略、中等杠杆、标准止损,回撤为 10-20%
- L(低):保守策略、低杠杆、止损宽,回撤低于 10%
示例:
RSIMeanReversion_M- 基础策略,中等风险MomentumBreakout_H_optimized- 优化后的高风险策略TrendFollower_L_allora- 使用 Allora ML 增强的低风险策略BollingerBands_M_v2- 策略的第 2 版
命名的重要性:
- 帮助按风险概况整理策略
- 清晰的版本号(_v2、_v3)可跟踪演进
- 后缀(_optimized、_allora)表示增强功能
- 统一的命名便于筛选和比较
仓位大小模式
建议的仓位大小:可用保证金的 85-95%
常见方法:
1. 固定百分比(简单、可预测):
def go_long(self):
qty = utils.size_to_qty(self.balance * 0.90, self.price)
self.buy = qty, self.price
2. 基于波动率(根据市场状况调整):
def go_long(self):
atr = ta.atr(self.candles, period=14)
# Reduce size in high volatility
size_multiplier = 0.90 if atr < self.price * 0.02 else 0.70
qty = utils.size_to_qty(self.balance * size_multiplier, self.price)
self.buy = qty, self.price
3. 基于风险(根据止损距离确定仓位大小):
def go_long(self):
atr = ta.atr(self.candles, period=14)
stop_distance = atr * 2 # Stop at 2× ATR
# Risk 2% of balance per trade
risk_amount = self.balance * 0.02
qty = risk_amount / stop_distance
self.buy = qty, self.price
最佳实践:创建策略时,在描述中注明仓位大小的计算方法
风险管理要求
每个策略都应包含:
1. 止损(必需):
def on_open_position(self, order):
atr = ta.atr(self.candles, period=14)
# Stop at 2× ATR below entry (long) or above entry (short)
self.stop_loss = qty, self.price - (atr * 2) # Long
# or
self.stop_loss = qty, self.price + (atr * 2) # Short
2. 止盈(建议):
def on_open_position(self, order):
atr = ta.atr(self.candles, period=14)
# Target at 3× ATR (risk/reward = 1.5)
self.take_profit = qty, self.price + (atr * 3) # Long
3. 仓位大小(参见上文)
危险信号(应避免):
- 没有止损(下行风险无限)
- 止损过紧(距离入场价 <0.5%)——会被市场噪声触发
- 止损过宽(距离入场价 >5%)——每笔交易的风险过高
- 仓位大小 >保证金的 95%——应对保证金追缴的缓冲不足
- 没有止盈——仓位可能回吐收益
可用指标
可通过 jesse.indicators 使用 170+ 个技术指标:
描述策略要求时,请使用准确名称:
动量(16 个指标):
- RSI、MACD、Stochastic、ADX、CCI、MFI、ROC、Williams %R 等
趋势(12 个指标):
- EMA、SMA、DEMA、TEMA、WMA、Supertrend、Parabolic SAR、VWAP、HMA 等
波动率(8 个指标):
- Bollinger Bands、ATR、Keltner Channels、Donchian Channels、Standard Deviation 等
成交量(10 个指标):
- OBV、Volume Profile、Chaikin Money Flow 等
查找指标的方法:
(Use browse-robonet-data skill)
get_all_technical_indicators(category="momentum")
在策略描述中使用准确名称: ✓ “使用周期为 14 的 RSI” ✓ “使用周期为 20、标准差为 2 的 Bollinger Bands” ✗ “使用相对强度”(含义不明确) ✗ “使用 BB”(缩写不清楚)
最佳实践
成本管理
这是成本最高的工具($1-$4.50)。尽量避免浪费:
使用 create_strategy 前:
- ✅ 使用
browse-robonet-data浏览数据(确认可用的交易对和指标) - ✅ 可选:使用
design-trading-strategies生成想法(探索成本 $0.05-$1.00) - ✅ 预先写好详细需求
- ✅ 了解 Jesse 框架基础知识
避免以下高成本错误:
- ❌ 未检查指标是否可用就创建策略 → 浪费 $2.50
- ❌ 描述含糊(“构建一个出色的 BTC 策略”)→ 结果不佳,需要重新生成
- ❌ 需求不明确 → 代码不符合预期,浪费生成成本
- ❌ 未指定风险管理 → 需要添加止损和仓位设置后重新生成
节省成本的模式:
1. browse-robonet-data ($0.001) → Verify resources
2. design-trading-strategies ($0.30) → Explore 3 ideas
3. Pick best idea and refine description
4. create_strategy ($2.50) → Generate once, correctly
Total: $2.80 with high success rate
vs.
1. create_strategy ($2.50) → Vague requirements
2. Doesn't work, try again ($2.50)
3. Still not right ($2.50)
Total: $7.50 with frustration
编写有效的描述
优秀描述的构成:
Entry Conditions:
- Specific indicator with exact parameters
- Exact thresholds
- Multiple conditions with AND/OR logic
Exit Conditions:
- Stop loss method and distance
- Take profit method and target
- Trailing stop if applicable
Position Sizing:
- Percentage of margin to use
- Or risk-based sizing method
Risk Management:
- Maximum loss per trade
- Any position limits
Context:
- Timeframe (5m, 1h, 4h, 1d)
- Market regime (trending, ranging)
优秀描述示例:
"RSI Mean Reversion strategy for BTC-USDT on 1h timeframe.
ENTRY (Long):
- RSI(14) < 30 (oversold)
- Price touches lower Bollinger Band (20-period, 2 std dev)
- Confirm with volume: current volume > 1.2× 20-period average
EXIT (Long):
- Take profit: Price reaches middle Bollinger Band
- Stop loss: 2% below entry price
- Trailing stop: Once profit >3%, trail stop at 1.5% below highest price
POSITION SIZING:
- Use 90% of available margin per trade
- Single position at a time (no pyramiding)
RISK MANAGEMENT:
- Maximum loss: 2% of account per trade
- No new trades if in drawdown >10%"
糟糕描述示例:
"Build a profitable BTC strategy using RSI and Bollinger Bands"
问题:
- 未指定入场条件(RSI 值是多少?)
- 未指定出场条件(何时平仓?)
- 未指定仓位大小(承担多少风险?)
- 未指定时间周期(1m?1d?)
- 过于含糊 → 需要重新生成
验证清单
策略生成后,请确认代码包含:
- 所有必需的方法(should_long、should_short、go_long、go_short)
- 止损逻辑(位于 on_open_position 或 go_long/go_short 中)
- 止盈逻辑(推荐)
- 仓位大小计算(在 go_long/go_short 中计算 qty)
- 有效的指标调用(例如,
ta.rsi(self.candles, period=14)) - 与描述相符的正确入场/出场条件
- 没有语法错误(代码可运行)
- 指标参数合理(没有过度优化)
如果验证未通过:
- 使用
improve-trading-strategies技能修复问题($0.50-$3.00) - 比使用
create_strategy重新生成更便宜($1-$4.50)
时间周期注意事项
使策略逻辑与时间周期匹配:
超短线(1m-5m):
- 较紧的止损(0.2-0.5%)
- 快速出场(数分钟到数小时)
- 高频指标(较短周期)
- 注重执行和手续费
日内交易(15m-1h):
- 适中的止损(0.5-2%)
- 持仓数小时至 1 天
- 标准指标周期(14、20、50)
- 平衡交易频率与噪声
波段交易(4h-1d):
- 较宽的止损(2-5%)
- 持仓数天至数周
- 较长的指标周期(50、100、200)
- 注重大趋势
在描述中指定时间周期:
"For 1h timeframe..." (helps AI tune indicator parameters appropriately)
常见工作流
工作流 1:从头构建
目标:根据构想创建新策略
1. Explore data (use browse-robonet-data):
get_all_symbols() → Choose BTC-USDT
get_all_technical_indicators(category="momentum") → Pick RSI
get_all_technical_indicators(category="volatility") → Pick Bollinger Bands
2. Optional: Generate ideas (use design-trading-strategies):
generate_ideas(strategy_count=3) → Get concepts
Pick best concept as starting point
3. Write detailed description:
- Entry: RSI < 30 AND price at lower BB
- Exit: Price at middle BB OR stop loss 2%
- Sizing: 90% margin
- Timeframe: 1h
4. Create strategy:
create_strategy(
strategy_name="RSIMeanReversion_M",
description="[detailed description from step 3]"
)
5. Validate generated code:
- Check all required methods present
- Verify indicators match description
- Confirm risk management included
6. Test immediately (use test-trading-strategies):
run_backtest(strategy_name="RSIMeanReversion_M", ...)
费用:总计约 $2-4($0.30 构想 + $2.50 创建 + $0.001 测试)
工作流 2:根据构想构建
目标:将 AI 生成的构想转化为可运行的代码
1. Generate ideas (use design-trading-strategies):
generate_ideas(strategy_count=3)
Idea #2: "Bollinger Band Breakout"
Entry: Price breaks above upper BB with high volume
Exit: Price returns to middle BB
Uses: Bollinger Bands, Volume
2. Refine idea into detailed description:
"Bollinger Band Breakout strategy for ETH-USDT on 4h timeframe.
ENTRY (Long):
- Price closes above upper Bollinger Band (20, 2)
- Current volume > 1.5× 20-period average volume
- ADX(14) > 25 (confirm trend strength)
EXIT (Long):
- Price closes below middle Bollinger Band
- Or stop loss 3% below entry
- Or take profit at 9% above entry (3:1 reward:risk)
POSITION SIZING: 85% of margin
RISK: Max 3% loss per trade"
3. Create strategy:
create_strategy(
strategy_name="BollingerBreakout_H",
description="[detailed description from step 2]"
)
4. Test and validate:
run_backtest(strategy_name="BollingerBreakout_H", ...)
费用:约 $3($0.30 构想 + $2.50 创建 + $0.001 测试)
工作流 3:构建预测市场策略
目标:创建 Polymarket YES/NO 代币交易策略
1. Browse prediction markets (use browse-robonet-data):
get_data_availability(data_type="polymarket")
→ See available markets
2. Analyze market data:
get_prediction_market_data(condition_id="...")
→ Study YES/NO token price history
3. Write detailed description:
"Polymarket probability arbitrage strategy for crypto_rolling markets.
BUY YES TOKEN when:
- YES token price < 0.40 (implied 40% probability)
- Market has >$10k volume (sufficient liquidity)
- Time to resolution > 2 hours (avoid last-minute volatility)
BUY NO TOKEN when:
- NO token price < 0.40 (YES price > 0.60)
- Same liquidity and time criteria
EXIT:
- Sell when price reaches 0.55 (15% profit target)
- Or hold until market resolution
- Stop loss: Sell if price drops to 0.25 (37.5% loss)
POSITION SIZING: 5% of capital per market
MAX POSITIONS: 10 simultaneous markets"
4. Create prediction market strategy:
create_prediction_market_strategy(
strategy_name="PolymarketArbitrage_M",
description="[detailed description from step 3]"
)
5. Test on historical markets:
run_prediction_market_backtest(...)
费用:约 $2.50 + $0.001 测试 = $2.501
高级用法
多时间周期策略
在策略要求中描述更高时间周期的背景信息:
"ETH-USDT swing trading strategy on 1h timeframe with 4h trend filter.
HIGHER TIMEFRAME (4h):
- Only take long trades when 4h EMA(50) is rising
- Only take short trades when 4h EMA(50) is falling
ENTRY TIMEFRAME (1h):
- [standard entry conditions on 1h]
..."
AI 会生成用于检查更高时间周期条件的代码。
复杂入场逻辑
明确指定多项条件的精确逻辑:
"Entry requires ALL of these conditions (AND logic):
1. RSI(14) < 30
2. Price < Lower Bollinger Band (20, 2)
3. MACD histogram positive (bullish divergence)
4. Volume > 1.3× average
OR entry if these alternative conditions met:
1. Price makes higher low
2. RSI makes higher low (bullish divergence)
3. Volume surge (>2× average)"
只要说明清楚,AI 就能处理复杂的多条件逻辑。
动态仓位大小
在描述中指定自适应仓位大小:
"Position sizing based on volatility:
- When ATR(14) < 2% of price: Use 95% margin (low volatility)
- When ATR between 2-4%: Use 85% margin (normal)
- When ATR > 4%: Use 70% margin (high volatility)
This reduces risk during volatile periods."
故障排除
“生成的代码存在错误”
问题:策略代码无法运行或存在语法错误
解决方案:
- 使用
improve-trading-strategies技能中的refine_strategy修复错误 - 比重新生成更便宜($0.50-$3.00,而非 $1-$4.50)
- 在优化描述中注明确切的错误信息
“策略与描述不符”
问题:生成的逻辑与请求内容不同
解决方案:
- 描述可能存在歧义
- 使用
improve-trading-strategies技能优化特定部分 - 如果差异较大,可能需要用更清晰的描述重新生成
“指标不可用”
问题:策略使用了 Jesse 中不存在的指标
解决方案:
- 应先使用
browse-robonet-data确认指标是否可用 - 使用
refine_strategy替换为有效指标 - 检查指标名称拼写(RSI 而不是 rsi,MACD 而不是 macd)
“策略过于复杂”
问题:生成的代码过于复杂,包含 8 个或更多指标
解决方案:
- 简化描述(要求使用更少的指标)
- 使用
refine_strategy去除不必要的复杂性 - 复杂策略往往会过拟合,表现不佳
“未包含风险管理”
问题:生成的代码缺少止损或仓位大小计算
解决方案:
- 必须在描述中明确要求风险管理
- 使用
refine_strategy添加止损和仓位大小计算 - 始终明确指定:“止损设为 X%,仓位大小设为 Y%”
后续步骤
构建策略后:
测试策略(至关重要——接下来就做):
- 使用
test-trading-strategies技能进行回测 - 费用:每次回测 $0.001
- 在进行任何后续工作前验证性能
- 检查:夏普比率 >1.0、回撤 <20%、胜率 45-65%
改进策略(如有需要):
- 使用
improve-trading-strategies技能优化代码 - 费用:每次操作 $0.50-$4.00
- 比从头重新生成更便宜
- 选项:refine_strategy、optimize_strategy、enhance_with_allora
部署到生产环境(仅在充分测试后):
- 使用
deploy-live-trading技能(高风险) - 费用:$0.50 部署费
- 切勿在未进行广泛回测的情况下部署(建议至少回测 6 个月)
- 从小规模开始,并密切监控
总结
此技能可提供AI 驱动的策略代码生成:
- 2 个工具:create_strategy(加密货币)、create_prediction_market_strategy(Polymarket)
- 费用:每次生成 $1.00-$4.50(费用最高的工具)
- 执行时间:30-60 秒
- 输出:符合 Jesse 框架结构的生产就绪 Python 代码
核心原则:此工具费用高昂。使用前请做好充分准备:
- 浏览数据(确认资源可用)
- 可选:生成创意(以较低成本探索概念)
- 撰写详细描述(明确需求)
- 一次生成,确保正确
- 立即测试
重要警告:生成的代码可能存在错误,也可能不符合预期。部署前务必使用 test-trading-strategies 进行测试。切勿将未经测试的策略部署到实盘交易中。
优化成本:花 5 分钟准备(探索成本 $0-$0.30),可避免在无效生成上浪费数美元,并大幅提高成功率。