为何1分钟周期无法显示4小时周期的crossover/crossunder,反之可行?
问题
在1分钟时间周期下,无法在表格中查看4小时周期发生的crossover(上穿)和crossunder(下穿)信号,但切换到4小时周期时,却能正常查看1分钟周期的交叉信号。问题表现为1分钟周期下4小时对应的表格列无信号显示,4小时周期下1分钟对应的表格列有信号显示。
对应的Pine Script代码如下:
input_daily = input.timeframe("1", "Daily Time Frame") input_mine = input.timeframe("240", "Daily Time Frame") // isBullish(timeframe) => s1 = ta.crossover(periodLine103, periodLine216) s2 = ta.crossunder(periodLine103, periodLine216) crossoverResult = request.security(syminfo.tickerid, timeframe, s1) and not request.security(syminfo.tickerid, timeframe, s2) crossoverResult // isBearish(timeframe) => s1 = ta.crossover(demo1, demo2) s2 = ta.crossunder(demo1, demo2) crossunderResult = request.security(syminfo.tickerid, timeframe, s2) and not request.security(syminfo.tickerid, timeframe , s1) crossunderResult tsmeee = isBullish(input_daily) tsmeee1 = isBearish(input_daily) TM240 = isBullish(input_mine) TM1240 = isBearish(input_mine) var table myTable = table.new(position = position.top_right, columns = 30, rows = 3, bgcolor = #e1e1e1, frame_color = #191919, frame_width = 1, border_width = 1, border_color = #232323) table.cell(myTable, column = 1, row = 0, text = "1") table.cell(myTable, column = 2, row = 0, text = "240") table.cell(myTable, column = 0, row = 0, text = "time", bgcolor = #f0f0f0) table.cell(myTable, column = 0, row = 1, text = "cross", bgcolor = #f0f0f0) if tsmeee table.cell(myTable, column=1, row=1, text="▲", text_color=color.rgb(26, 178, 59)) else if tsmeee1 table.cell(myTable, column=1, row=1, text="▼", text_color=color.rgb(151, 17, 17)) if TM240 table.cell(myTable, column=2, row=1, text="▲", text_color=color.rgb(26, 178, 59)) else if TM1240 table.cell(myTable, column=2, row=1, text="▼", text_color=color.rgb(151, 17, 17))
原因分析
跨周期信号传递的瞬时性问题
从低周期(1分钟)请求高周期(4小时)数据时,高周期的ta.crossover/ta.crossunder信号仅在高周期K线完成的瞬间触发一次(即该高周期K线的最后一根1分钟K线),后续所有1分钟K线都会接收到false值,导致表格无法持续显示信号状态。
反之,从高周期(4小时)请求低周期(1分钟)数据时,低周期的交叉信号会在高周期K线内的每一根1分钟K线触发时传递到高周期,因此能在表格中持续看到信号。信号逻辑未记录持续状态
代码中的isBullish/isBearish函数直接请求交叉信号的瞬时值,没有在目标周期内记录交叉发生后的持续状态。一旦高周期K线走完,瞬时信号消失,低周期就无法捕捉到之前的交叉事件。
修复方案
修改代码,在目标周期内用持久化变量记录交叉后的状态,确保信号能持续传递到低周期,同时调整表格更新逻辑避免状态丢失:
//@version=5 indicator("Cross Signal Table", overlay=true) // 补充示例指标线(需根据实际情况替换) periodLine103 = ta.sma(close, 103) periodLine216 = ta.sma(close, 216) demo1 = ta.sma(close, 50) demo2 = ta.sma(close, 200) input_1min = input.timeframe("1", "1分钟周期") input_4h = input.timeframe("240", "4小时周期") // 修正多信号判断:在目标周期内保持状态直到反向交叉 isBullish(timeframe) => var bool bullish_state = false // 仅在目标周期的最后一根K线更新状态 if barstate.islast cross_up = ta.crossover(periodLine103, periodLine216) cross_down = ta.crossunder(periodLine103, periodLine216) // 上穿时激活状态,下穿时取消状态 bullish_state := cross_up or (bullish_state and not cross_down) // 请求目标周期的持续状态 request.security(syminfo.tickerid, timeframe, bullish_state) isBearish(timeframe) => var bool bearish_state = false if barstate.islast cross_up = ta.crossover(demo1, demo2) cross_down = ta.crossunder(demo1, demo2) bearish_state := cross_down or (bearish_state and not cross_up) request.security(syminfo.tickerid, timeframe, bearish_state) // 获取各周期信号状态 signal_1min_bull = isBullish(input_1min) signal_1min_bear = isBearish(input_1min) signal_4h_bull = isBullish(input_4h) signal_4h_bear = isBearish(input_4h) // 创建表格 var table signal_table = table.new( position = position.top_right, columns = 3, rows = 3, bgcolor = #e1e1e1, frame_color = #191919, frame_width = 1, border_width = 1, border_color = #232323 ) // 表格表头 table.cell(signal_table, column=0, row=0, text="周期", bgcolor=#f0f0f0) table.cell(signal_table, column=1, row=0, text="1分钟") table.cell(signal_table, column=2, row=0, text="4小时") table.cell(signal_table, column=0, row=1, text="信号", bgcolor=#f0f0f0) // 实时更新表格内容,确保状态持续显示 table.cell( signal_table, column=1, row=1, text = signal_1min_bull ? "▲" : signal_1min_bear ? "▼" : "", text_color = signal_1min_bull ? color.rgb(26, 178, 59) : signal_1min_bear ? color.rgb(151, 17, 17) : color.gray ) table.cell( signal_table, column=2, row=1, text = signal_4h_bull ? "▲" : signal_4h_bear ? "▼" : "", text_color = signal_4h_bull ? color.rgb(26, 178, 59) : signal_4h_bear ? color.rgb(151, 17, 17) : color.gray )
关键改动说明
- 持久化状态记录:在目标周期内用
var定义状态变量,交叉信号触发后保持状态直到反向交叉出现,确保信号不会随K线走完而消失。 - 精准状态更新:用
barstate.islast限制仅在目标周期的最后一根K线更新状态,避免重复计算。 - 实时表格更新:直接根据信号状态赋值表格内容,替代原有的
if-else逻辑,确保每个周期的信号状态都能持续显示。
内容的提问来源于stack exchange,提问作者رضا رادمنش
相关产品推荐
相关产品推荐

