MRYR
MRYR

Reputation: 70

Pine Script - Conditional Plot Style

I couldn't find an answer for this anywhere; not even in the official documentation. I've tried coding it myself but it's not working, so maybe it might not be possible.

In the example below, you can plot with conditional colors:

//STACKED EMAs
    MA1 = ta.ema(close, 5)
    MA2 = ta.ema(close, 8)
    MA3 = ta.ema(close, 13)
    MA4 = ta.ema(close, 21)
    MA5 = ta.ema(close, 34)
    MA_Stack_Up = (MA1 > MA2) and (MA2 > MA3) and (MA3 > MA4) and (MA4 > MA5)
    
    //CONDITIONS
    Uptrend = MA_Stack_Up
    Reversal = ((MA1 < MA2) and (MA2 > MA3)) or ((MA1 > MA2) and (MA2 < MA3))
    
    //COLOR CODING
    Bar_Color = Uptrend ? color.new(color.green, 25) : Reversal ? color.new(color.yellow, 25) : color.new(color.red, 25)

//PLOTS
plot(1, color=Bar_Color, style=plot.style_circles, linewidth=3)

Is it possible to have a conditional statement that changes the style of the plot?

Example:

B_Style = Cond1 ? plot.style_line : Cond2 ? plot.style_cross : plot.style_circles

//PLOTS
plot(1, color=Bar_Color, style=B_Style, linewidth=3)

Upvotes: 3

Views: 2192

Answers (1)

vitruvius
vitruvius

Reputation: 21372

It looks like it is not possible at the moment. A possible workaround would be using a variable as a condition to your plot() and having multiple plot calls for different styles.

//@version=5
indicator("My Script")

myRsi = ta.rsi(close, 14)
cond = myRsi < 40 ? 1 : myRsi < 60 ? 2 : na
plot(cond == 1 ? myRsi : na, style=plot.style_circles, linewidth=2)
plot(cond == 2 ? myRsi : na, style=plot.style_cross, linewidth=2)
hline(40, color=color.red)
hline(60, color=color.red)

enter image description here

Upvotes: 5

Related Questions