4小时Ichimoku云汇合条件判断异常求助
问题排查:4小时Ichimoku云状态在15分钟图上显示异常
问题背景
- 交易策略:基于15分钟时间框架(TF)入场,以4小时TF下价格相对于Ichimoku云的位置作为多空判断的核心条件
- 异常现象:15分钟图上的状态表格颜色随当前15分钟价格与云带的关系变化,未保持4小时TF的判断结果(例如4小时价格在云带上方时,15分钟图因当前15分钟价格在云带下方显示红色)
原代码
//@version=5 indicator(title="Ichimoku 4h cloud table", shorttitle="Ichimoku 4h cloud table", overlay=true) conversionPeriods = input.int(9, minval=1, title="Conversion Line Length") basePeriods = input.int(26, minval=1, title="Base Line Length") laggingSpan2Periods = input.int(52, minval=1, title="Leading Span B Length") displacement = input.int(26, minval=1, title="Lagging Span") donchian(len) => math.avg(ta.lowest(len), ta.highest(len)) conversionLine = donchian(conversionPeriods) baseLine = donchian(basePeriods) spanA = math.avg(conversionLine, baseLine) spanB = donchian(laggingSpan2Periods) //Get 4hr values [conversionLine4h, baseLine4h, laggingSpan2Periods4h] = request.security(syminfo.tickerid , "240", [conversionLine, baseLine, laggingSpan2Periods], lookahead = barmerge.lookahead_off) spanA4h = math.avg(conversionLine4h, baseLine4h) spanB4h = donchian(laggingSpan2Periods4h) // calculations for output 4h longCond4h = close > spanA4h[displacement-1] and close > spanB4h[displacement-1] shortCond4h = close < spanA4h[displacement-1] and close < spanB4h[displacement-1] status4hcloudbgc = longCond4h ? color.green : shortCond4h ? color.red : color.yellow
核心问题分析
- 价格判断错误:多空条件中使用了
close(当前15分钟K线收盘价),而非4小时TF的收盘价,导致判断逻辑被15分钟价格干扰 - 云带计算错误:
request.security获取的laggingSpan2Periods4h是输入的周期参数(52),不是4小时TF下计算出的spanB值,后续本地计算spanB4h完全错误 - 位移逻辑错误:云带的位移(displacement)应基于4小时的K线索引,而非15分钟K线的索引
修复后的代码
//@version=5 indicator(title="Ichimoku 4h cloud table", shorttitle="Ichimoku 4h cloud table", overlay=true) // 输入参数 conversionPeriods = input.int(9, minval=1, title="Conversion Line Length") basePeriods = input.int(26, minval=1, title="Base Line Length") laggingSpan2Periods = input.int(52, minval=1, title="Leading Span B Length") displacement = input.int(26, minval=1, title="Lagging Span") // 定义Donchian通道计算函数 donchian(len) => math.avg(ta.lowest(len), ta.highest(len)) // 直接在4小时TF中计算所需的Ichimoku指标和收盘价 [close4h, spanA4h, spanB4h] = request.security(syminfo.tickerid, "240", [ close, math.avg(donchian(conversionPeriods), donchian(basePeriods)), donchian(laggingSpan2Periods) ], lookahead = barmerge.lookahead_off) // 基于4小时收盘价和位移后的云带判断多空 longCond4h = close4h > spanA4h[displacement] and close4h > spanB4h[displacement] shortCond4h = close4h < spanA4h[displacement] and close4h < spanB4h[displacement] // 状态颜色赋值 status4hcloudbgc = longCond4h ? color.green : shortCond4h ? color.red : color.yellow // 示例:绘制表格显示状态(如果需要) var table t = table.new(position.bottom_right, 1, 1) table.cell(t, 0, 0, "4h Cloud Status", bgcolor=status4hcloudbgc)
修复说明
- 将Ichimoku云带的计算逻辑完全放在
request.security的4小时TF上下文里,确保获取的是正确的4小时云带值 - 使用
close4h(4小时收盘价)进行多空判断,彻底隔离15分钟价格的干扰 - 正确应用云带的位移:基于4小时K线的索引
spanA4h[displacement],符合Ichimoku指标的规则 - 新增了表格绘制的示例代码,方便直接查看状态
内容的提问来源于stack exchange,提问作者John
相关产品推荐
相关产品推荐

