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

为现有仅做多策略添加4小时SuperTrend指标过滤

解决方案:叠加4小时SuperTrend过滤器的5分钟做多策略

核心逻辑是通过request.security()函数跨时间框架获取4小时K线的SuperTrend状态,将其作为5分钟策略开仓的前置条件。以下是完整的Pine Script实现:

//@version=5
strategy("5min SuperTrend + 4h Filter Long Only", overlay=true, default_qty_type=strategy.cash, default_qty_value=10000, commission_type=strategy.commission.percent, commission_value=0.1)

// --- 定义SuperTrend计算函数 ---
f_supertrend(length, multiplier) =>
    atr = ta.atr(length)
    up = ta.close - multiplier * atr
    dn = ta.close + multiplier * atr
    trend = 1
    trend := ta.close > up[1] ? 1 : ta.close < dn[1] ? -1 : trend[1]
    st = trend == 1 ? up : dn
    [st, trend]

// --- 5分钟K线的SuperTrend参数与计算 ---
st_length_5min = input.int(10, title="5min SuperTrend Length")
st_multiplier_5min = input.float(3.0, title="5min SuperTrend Multiplier")
[st_5min, trend_5min] = f_supertrend(st_length_5min, st_multiplier_5min)

// --- 4小时K线的SuperTrend参数与跨周期获取 ---
st_length_4h = input.int(10, title="4h SuperTrend Length")
st_multiplier_4h = input.float(3.0, title="4h SuperTrend Multiplier")
// 跨时间框架请求4小时的SuperTrend趋势状态
[st_4h, trend_4h] = request.security(syminfo.tickerid, "240", f_supertrend(st_length_4h, st_multiplier_4h), gaps=barmerge.gaps_on, lookahead=barmerge.lookahead_off)

// --- 绘制指标 ---
plot(st_5min, color=trend_5min == 1 ? color.new(color.green, 0) : color.new(color.red, 0), title="5min SuperTrend")
plot(st_4h, color=trend_4h == 1 ? color.new(color.lime, 0) : color.new(color.orange, 0), title="4h SuperTrend", style=plot.style_circles)

// --- 交易条件 ---
// 开仓:5分钟SuperTrend转多 + 4小时SuperTrend处于看涨状态
long_condition = trend_5min == 1 and trend_5min[1] == -1 and trend_4h == 1
// 平仓:5分钟SuperTrend转空
exit_condition = trend_5min == -1 and trend_5min[1] == 1

// --- 执行交易 ---
if (long_condition)
    strategy.entry("Long", strategy.long)
if (exit_condition)
    strategy.close("Long")

关键细节说明

  • 跨周期数据获取:使用request.security()指定时间框架为"240"(代表4小时,单位分钟),确保获取真实的4小时K线数据,lookahead=barmerge.lookahead_off避免未来函数问题。
  • SuperTrend状态判断:自定义函数f_supertrend返回的trend值,1代表看涨(价格在SuperTrend线上方,对应绿色),-1代表看跌。
  • 开仓过滤:只有当5分钟级别的SuperTrend出现做多信号,且4小时级别的SuperTrend处于看涨状态时,才触发多单开仓。
  • 避免信号闪烁:通过对比当前和上一根K线的trend值判断趋势转折,减少虚假信号。

如果之前尝试失败,大概率是跨周期数据获取方式错误(比如时间框架参数写错、未禁用未来函数),或者SuperTrend状态判断逻辑不一致。可根据需求调整st_length和st_multiplier参数。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.09 02:58:35