hassan rezay
hassan rezay

Reputation: 25

how to make a conditional label to be executed and displayed all over the chart in pine script

I have this code that creates a label with some conditions, here my problem is that this label is created once in the chart, how can I make this code find all conditions and place a label there?

//@version=4
study(title="golden", overlay=true)
    
// Make a new label once
var label buy = label.new(x=bar_index, y=high + tr,textcolor=color.white, color=color.blue)

if(high < high[1]) and (close < open)
    if(high[1] > high[2]) and (low[1] > low[2]) and (close[1] > open[1])
        if(high[2] > high[3]) and (low[2] > low[3]) and (close[2] > open[2])
            if(high[3] > high[4]) and (low[3] > low[4]) and (close[3] > open[3])
                label.set_text(id=buy, text="buy")
                label.set_x(id=buy, x=bar_index)
                label.set_y(id=buy, y=high + tr)

Upvotes: 0

Views: 889

Answers (2)

Rohit Agarwal
Rohit Agarwal

Reputation: 1368

You can generate the label inside the condition so that it will be generated everytime the condition is true. Example below

//@version=4
study(title="golden", overlay=true,max_labels_count=500)
    

if(high < high[1]) and (close < open)
    if(high[1] > high[2]) and (low[1] > low[2]) and (close[1] > open[1])
        if(high[2] > high[3]) and (low[2] > low[3]) and (close[2] > open[2])
            if(high[3] > high[4]) and (low[3] > low[4]) and (close[3] > open[3])
                label.new(x=bar_index, y=high + tr,textcolor=color.white, color=color.blue,text="buy")

enter image description here

Upvotes: 1

Dave
Dave

Reputation: 865

Try this

//@version=4
study(title="golden", overlay=true)
    
// Make a new label once
var label buy = na

buy := label.new(x=bar_index, y=high + tr,textcolor=color.white, color=color.blue)

if(high < high[1]) and (close < open)
    if(high[1] > high[2]) and (low[1] > low[2]) and (close[1] > open[1])
        if(high[2] > high[3]) and (low[2] > low[3]) and (close[2] > open[2])
            if(high[3] > high[4]) and (low[3] > low[4]) and (close[3] > open[3])
                label.set_text(id=buy, text="buy")
                label.set_x(id=buy, x=bar_index)
                label.set_y(id=buy, y=high + tr)

Upvotes: 0

Related Questions