//@version=6 indicator("背了又背:最后一笔顶/底背离(单周期简化版)", shorttitle="LastDiv_Simple", overlay=true, max_labels_count=500) // 参数 pivot_left = input.int(3, "左侧枢轴K数", minval=1) pivot_right = input.int(3, "右侧枢轴K数", minval=1) fastlen = input.int(12, "MACD快线") slowlen = input.int(26, "MACD慢线") siglen = input.int(9, "MACD信号线") min_div_strength = input.float(0.6, "背离强度系数阈值", minval=0.1, maxval=1.0, step=0.05) need_double_div = input.bool(true, "必须『背了又背』(同向至少两次背离)") // MACD [dif, dea, hist] = ta.macd(close, fastlen, slowlen, siglen) // 枢轴 ph_val = ta.pivothigh(high, pivot_left, pivot_right) pl_val = ta.pivotlow(low, pivot_left, pivot_right) is_pivoth = not na(ph_val) is_pivotl = not na(pl_val) ph_price = is_pivoth ? ph_val : na pl_price = is_pivotl ? pl_val : na ph_osc = is_pivoth ? hist[pivot_right] : na pl_osc = is_pivotl ? hist[pivot_right] : na ph_bar = is_pivoth ? bar_index[pivot_right] : na pl_bar = is_pivotl ? bar_index[pivot_right] : na // 工具函数 // 隐形背离定义: // - 顶:价格创新高,但对应枢轴处没有正的能量柱(hist <= 0) // - 底:价格创新低,但对应枢轴处没有负的能量柱(hist >= 0) f_top_div(price1, price2, osc1, osc2) => price2 > price1 and osc2 <= 0 f_bottom_div(price1, price2, osc1, osc2) => price2 < price1 and osc2 >= 0 // 状态变量:记录连续同向背离 var float last_ph_price = na var float last_ph_osc = na var int last_ph_bar = na var float last_pl_price = na var float last_pl_osc = na var int last_pl_bar = na var float prev_div_top_price = na var float prev_div_top_osc = na var int prev_div_top_bar = na var float last_div_top_price = na var float last_div_top_osc = na var int last_div_top_bar = na var float prev_div_bot_price = na var float prev_div_bot_osc = na var int prev_div_bot_bar = na var float last_div_bot_price = na var float last_div_bot_osc = na var int last_div_bot_bar = na var int top_div_count_seq = 0 var int bot_div_count_seq = 0 new_top_div = false new_bot_div = false // 顶背离(隐形) if is_pivoth and not na(last_ph_price) if f_top_div(last_ph_price, ph_price, last_ph_osc, ph_osc) new_top_div := true // 更新链 prev_div_top_price := last_div_top_price prev_div_top_osc := last_div_top_osc prev_div_top_bar := last_div_top_bar last_div_top_price := ph_price last_div_top_osc := ph_osc last_div_top_bar := ph_bar top_div_count_seq += 1 else if ph_price > last_ph_price and ph_osc >= last_ph_osc top_div_count_seq := 0 if is_pivoth last_ph_price := ph_price last_ph_osc := ph_osc last_ph_bar := ph_bar // 底背离(隐形) if is_pivotl and not na(last_pl_price) if f_bottom_div(last_pl_price, pl_price, last_pl_osc, pl_osc) new_bot_div := true // 更新链 prev_div_bot_price := last_div_bot_price prev_div_bot_osc := last_div_bot_osc prev_div_bot_bar := last_div_bot_bar last_div_bot_price := pl_price last_div_bot_osc := pl_osc last_div_bot_bar := pl_bar bot_div_count_seq += 1 else if pl_price < last_pl_price and pl_osc <= last_pl_osc bot_div_count_seq := 0 if is_pivotl last_pl_price := pl_price last_pl_osc := pl_osc last_pl_bar := pl_bar // 只在『最后一笔』背了又背时画信号 need_count_top = need_double_div ? top_div_count_seq >= 2 : top_div_count_seq >= 1 need_count_bot = need_double_div ? bot_div_count_seq >= 2 : bot_div_count_seq >= 1 final_top_div = new_top_div and need_count_top final_bot_div = new_bot_div and need_count_bot plotshape( final_bot_div ,title="最后一笔底背离" ,location=location.belowbar ,style=shape.labelup ,color=color.new(color.green, 0) ,text="背了又背\n底终点" ,textcolor=color.white ,size=size.normal ) plotshape( final_top_div ,title="最后一笔顶背离" ,location=location.abovebar ,style=shape.labeldown ,color=color.new(color.red, 0) ,text="背了又背\n顶终点" ,textcolor=color.white ,size=size.normal )