You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何编写基于单一标的信号交易另一标的的Pine Script策略?

基于跨标的信号的Pine Script策略实现(VIX信号交易SPY)

直接上可运行的Pine Script代码,核心是通过request.security()调用VIX的行情数据,再基于其穿越条件触发SPY的交易:

//@version=5
strategy("VIX信号交易SPY", overlay=true, default_qty_type=strategy.cash, default_qty_value=10000, commission_type=strategy.commission.percent, commission_value=0.1)

// 获取VIX的收盘价数据,与当前图表时间周期匹配
vix_close = request.security("CBOE:VIX", timeframe.period, close)

// 定义信号条件:VIX上穿35买入,下穿15卖出
buy_signal = ta.crossover(vix_close, 35)
sell_signal = ta.crossunder(vix_close, 15)

// 执行交易
if (buy_signal)
    strategy.entry("多单", strategy.long)

if (sell_signal)
    strategy.close("多单")

// 可选:在图表上标记信号点
plotshape(buy_signal, title="买入信号", location=location.belowbar, color=color.green, style=shape.labelup, text="BUY")
plotshape(sell_signal, title="卖出信号", location=location.abovebar, color=color.red, style=shape.labeldown, text="SELL")

关键代码解释

  • request.security("CBOE:VIX", timeframe.period, close):从CBOE获取VIX的实时/历史收盘价,timeframe.period确保VIX数据的时间周期和当前SPY图表一致,避免周期错配。
  • ta.crossover()/ta.crossunder():分别检测价格上穿、下穿阈值的经典函数,确保信号只在穿越瞬间触发一次。
  • 策略参数设置:default_qty_type=strategy.cash用固定金额开仓,commission_value模拟手续费,可根据需求调整。

注意事项

  1. 确保当前图表加载的是SPY标的,代码会自动拉取VIX数据,无需切换图表。
  2. 回测时注意时间范围:VIX数据从1990年左右开始,确保回测区间在有效数据范围内。
  3. 若需要更严格的持仓控制,可添加strategy.risk.allow_entry_in(strategy.long)限制只持有多单,避免反向开仓。

内容的提问来源于stack exchange,提问作者vikv

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.25 16:02:03