jamesvv
Pine Script Rookie
Pine Script Rookie
Posts: 1
Joined: December 14th, 2021
Contact: TradingView Profile

Else If Help

Trying to figure out how to turn off the sell label when price is above the 200D MA
as well as turn off the buy label if price is below the 200D ma

indicator("Redlight-Greenlight", overlay=true)


GreenLightMA = ta.sma(close, 7)
RedLightEMA = ta.ema(close, 47)
BullMarket = ta.sma(close, 200)

plot(GreenLightMA, color = color.green)
plot(RedLightEMA, color = color.red)
plot(BullMarket, color = color.orange)


buy = ta.crossover(GreenLightMA, RedLightEMA)
sell = ta.crossover(RedLightEMA, GreenLightMA)

if (buy)
lbl = label.new(bar_index, low, "Buy")
label.set_color(lbl, color.green)
label.set_yloc(lbl, yloc.belowbar)
label.set_style(lbl, label.style_label_up)

if (sell)
lbl = label.new(bar_index, low, "Sell")
label.set_color(lbl, color.red)
label.set_yloc(lbl, yloc.abovebar)
label.set_style(lbl, label.style_label_down)

dusktrader
Pine Script Scholar
Pine Script Scholar
Posts: 32
Joined: February 24th, 2021
Contact: TradingView Profile

Re: Else If Help

Hi James, I think this is what you want here. Just add another condition to your IF statements:

Code: Select all

if buy and close > BullMarket
    lbl = label.new(bar_index, low, "Buy")
    label.set_color(lbl, color.green)
    label.set_yloc(lbl, yloc.belowbar)
    label.set_style(lbl, label.style_label_up)

if sell and close < BullMarket
    lbl = label.new(bar_index, low, "Sell")
    label.set_color(lbl, color.red)
    label.set_yloc(lbl, yloc.abovebar)
    label.set_style(lbl, label.style_label_down)

Return to “Pine Script Q&A”