'How do I stop multiple BUY/SELL signals from printing in a row? (Pine, Script, Trading, View, PineScript, TradingView)

I am trying to achieve a few things with my script...

  1. I only want to print a "BUY" signal if the previous signal was "SELL" (and vice-versa)
  2. I only want to print a "BUY" signal if it is less than the previous "SELL" signal. (and vice-versa)

I have been trying to find a solution to this issue all day. I don't understand how I can use "valuewhen" or "barssince" to achieve this.

I am new at scripting.

//Long
emalong = out1 > out2
macdlong = macd > signal and ((macd[1] < signal[1]) or (macd[2] < 
    signal[2]))
psarlong = psar < close and ((psar[1] > close[1]) or (psar[2] > 
    close[2]))

//Short
emashort = out1 < out2
macdshort = macd < signal and ((macd[1] > signal[1]) or (macd[2] 
    > signal[2]))
psarshort = psar > close and ((psar[1] < close[1]) or (psar[2] < 
    close[2]))

//Collect
longentry = emalong and macdlong and psarlong
shortentry = emashort and macdshort and psarshort

//Plot
plotshape(longentry, style=shape.circle, color=#26a69a, text="⬆", 
    textcolor=#ffffff, location=location.belowbar, 
    size=size.tiny)
plotshape(shortentry, style=shape.circle, color=#ef5350, 
    text="⬇", textcolor=#ffffff, location=location.abovebar, 
    size=size.tiny)


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source