1

I am learning Pine Script and I just "finished" my first indicator.

Everything seems to be doing it's job, except for the alerts.

I want to get just 1 alert when it triggers and then wait for the close. However, right now I seem to be getting multiple "open" alerts in a row and also multiple "close" alerts in a row.

What am I missing?

//@version=5
indicator("SMA + ATR Bands with RSI and Alerts", overlay=true)

// Calculate SMA over the past 200 bars
smaValue = ta.sma(close, 200)

// Calculate ATR over the past 7 bars
atrValue = ta.atr(7)

// Calculate upper and lower band values
upperBand = smaValue + (2 * atrValue)
lowerBand = smaValue - (2 * atrValue)

// Calculate RSI over the past 6 bars
rsiValue = ta.rsi(close, 6)

// Calculate buy and close buy signals
buySignal = ta.crossover(close, lowerBand) and (rsiValue > 50)
closeBuySignal = ta.crossunder(close, upperBand)

// Plot SMA, upper band, lower band, and RSI on the chart
plot(smaValue, "SMA", color = color.blue)
plot(upperBand, "Upper Band", color = color.green)
plot(lowerBand, "Lower Band", color = color.red)
plot(rsiValue, "RSI", color = color.purple)

// Plot buy and close buy signals
plotshape(
  buySignal ? high : na,
  title      = 'Long Label',
  text       = 'Long',
  textcolor  = color.white,
  color      = color.new(color.green, 0),
  style      = shape.labelup,
  size       = size.normal,
  location   = location.belowbar)
plotshape(
  closeBuySignal ? high : na,
  title      = 'Close Long Label',
  text       = 'Close Long',
  textcolor  = color.white,
  color      = color.new(#ff3131, 0),
  style      = shape.labeldown,
  size       = size.normal,
  location   = location.abovebar)

Here's the image of how it looks on chart: Text

Would really appreciate some help.

Thanks in advance!

Regards,

Molle

Molle de Jong
  • 31
  • 1
  • 5

1 Answers1

1

Your code is good, you simply have to record if you are already on a long or not to plot the signal.
I add this piece of code to trace if you are in Long or not :

var LongOpened = false
if buySignal and not(LongOpened)
    buySignal := true
    LongOpened := true
else
    buySignal := false
if closeBuySignal and LongOpened
    closeBuySignal := true
    LongOpened := false
else
    closeBuySignal := false

The result is : enter image description here

The complete code is :

//@version=5
indicator("SMA + ATR Bands with RSI and Alerts", overlay=true)

// Calculate SMA over the past 200 bars
smaValue = ta.sma(close, 200)

// Calculate ATR over the past 7 bars
atrValue = ta.atr(7)

// Calculate upper and lower band values
upperBand = smaValue + (2 * atrValue)
lowerBand = smaValue - (2 * atrValue)

// Calculate RSI over the past 6 bars
rsiValue = ta.rsi(close, 6)

// Calculate buy and close buy signals
buySignal = ta.crossover(close, lowerBand) and (rsiValue > 50)
closeBuySignal = ta.crossunder(close, upperBand)

// Plot SMA, upper band, lower band, and RSI on the chart
plot(smaValue, "SMA", color = color.blue)
plot(upperBand, "Upper Band", color = color.green)
plot(lowerBand, "Lower Band", color = color.red)
plot(rsiValue, "RSI", color = color.purple)

var LongOpened = false
if buySignal and not(LongOpened)
    buySignal := true
    LongOpened := true
else
    buySignal := false
if closeBuySignal and LongOpened
    closeBuySignal := true
    LongOpened := false
else
    closeBuySignal := false

// Plot buy and close buy signals
plotshape(
  buySignal ? high : na,
  title      = 'Long Label',
  text       = 'Long',
  textcolor  = color.white,
  color      = color.new(color.green, 0),
  style      = shape.labelup,
  size       = size.normal,
  location   = location.belowbar)
plotshape(
  closeBuySignal ? high : na,
  title      = 'Close Long Label',
  text       = 'Close Long',
  textcolor  = color.white,
  color      = color.new(#ff3131, 0),
  style      = shape.labeldown,
  size       = size.normal,
  location   = location.abovebar)
G.Lebret
  • 2,826
  • 2
  • 16
  • 27