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

Williams Alligator指标信号过早问题求助:修正均线交叉触发时机

Williams Alligator指标信号过早问题修复

问题根源

你当前代码的核心问题是:绘图时给指标线加了偏移(offset),但信号判断用的是未偏移的原始指标值。

举个例子:Jaw线设置了offset=8,意味着第N根K线的Jaw值会被画在第N+8根K线的位置;而Lips线offset=3,第N根K线的Lips值会画在第N+3根K线的位置。但你直接用原始的lips和jaw判断交叉,后台会在第N根K线就触发信号,可图表上要等到第N+8根K线才会看到Jaw线,此时信号已经提前了好几个K线。

修复方案

将指标线的偏移逻辑同步到信号判断中,让交叉判断基于和图表显示一致的偏移后指标值,这样信号就会和视觉上的线交叉同步。

修改后的完整代码

indicator(title="Williams Alligator", shorttitle="Alligator", overlay=true, timeframe="", timeframe_gaps=true)
smma(src, length) =>
    smma =  0.0
    smma := na(smma[1]) ? ta.sma(src, length) : (smma[1] * (length - 1) + src) / length
    smma

// 参数设置
jawLength = input.int(13, minval=1, title="Jaw长度")
teethLength = input.int(8, minval=1, title="Teeth长度")
lipsLength = input.int(5, minval=1, title="Lips长度")
jawOffset = input(8, title="Jaw偏移量")
teethOffset = input(5, title="Teeth偏移量")
lipsOffset = input(3, title="Lips偏移量")

// 计算原始SMMA值
jaw = smma(hl2, jawLength)
teeth = smma(hl2, teethLength)
lips = smma(hl2, lipsLength)

// 计算与图表显示对齐的偏移后指标值
jaw_shifted = jaw[jawOffset]  // 对应绘图时的offset=jawOffset,取jawOffset周期前的数值
teeth_shifted = teeth[teethOffset]
lips_shifted = lips[lipsOffset]

// 绘制指标线(保留原偏移设置)
plot(jaw, "Jaw", offset = jawOffset, color=#2962FF)
plot(teeth, "Teeth", offset = teethOffset, color=#E91E63)
plot(lips, "Lips", offset = lipsOffset, color=#66BB6A)

// 基于偏移后的值判断交叉信号
buy_signal = ta.crossover(lips_shifted, jaw_shifted)
sell_signal = ta.crossunder(teeth_shifted, jaw_shifted)

// 绘制信号
plotshape(buy_signal, style = shape.labelup, location = location.belowbar, color = color.green, size = size.small, text="买入")
plotshape(sell_signal, style = shape.labeldown, location = location.abovebar, color = color.red, size = size.small, text="卖出")

关键修改点

  1. 新增jaw_shifted、teeth_shifted、lips_shifted变量,通过[offset]索引获取对应偏移周期前的指标值,让判断逻辑和图表显示的线位置完全对齐。
  2. 信号判断改用偏移后的变量,确保视觉上看到线交叉时才触发信号。
  3. 给信号标签添加了中文文本,更直观。

额外优化建议

如果想进一步过滤假信号,可以添加额外条件:

  • 要求交叉后指标线持续保持方向(比如连续2根K线确认)
  • 结合价格走势过滤,比如价格同时突破最近的高低点

示例过滤逻辑(可替换原buy_signal):

buy_signal = ta.crossover(lips_shifted, jaw_shifted) and lips_shifted[1] > jaw_shifted[1]

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.02 20:30:43