条件仅运行一次(而不是在所有柱上)

问题描述 投票:0回答:1

检查条件 gap(high-low) > 0.1%(多次满足)时,标签仅获得 渲染一次(而不是在 25 个柱回溯内的相关柱上)。
请提供解决方案。

代码:
历史酒吧

//@version=5
indicator("PriceMomemtum",overlay = true,max_bars_back = 25)


gap = (math.abs(high - low)/low ) * 100 
//var gap = (math.abs(high - low)/low ) * 100 

if gap > 0.1
    var lbl = label.new(x = bar_index,y = na , text = na ,text_font_family = font.family_default ,xloc = xloc.bar_index,yloc =yloc.abovebar,style = label.style_arrowdown ,textcolor = color.white,size =size.small,textalign = text.align_left,tooltip = na)
    label.set_text(lbl,str.tostring(gap,"#.00")+"%")
    label.set_xy(lbl,bar_index,high )

实时酒吧

//@version=5

indicator("PriceMomemtum",overlay = true,max_bars_back = 25)


if barstate.isrealtime
    gap = (math.abs(high - low)/low ) * 100 
    //var gap = (math.abs(high - low)/low ) * 100 
    if gap > 0.1
        var lbl = label.new(x = bar_index,y = na , text = na ,text_font_family = font.family_default ,xloc = xloc.bar_index,yloc =yloc.abovebar,style = label.style_arrowdown ,textcolor = color.white,size =size.small,textalign = text.align_left,tooltip = na)
        label.set_text(lbl,str.tostring(gap,"#.00")+"%")
        label.set_xy(lbl,bar_index,high )

        alert(str.tostring(time(syminfo.timezone)) + "(PriceMomentum)", alert.freq_once_per_bar)

pine-script pine-script-v5
1个回答
0
投票

定义“lbl”变量而不使用“var”。

结果:
result

© www.soinside.com 2019 - 2024. All rights reserved.