Imran Shaikh
Imran Shaikh

Reputation: 11

Signals repeating before reversal signal

I have written code with logic that Buy Signal (UpTriangle) will generate - when Price (Close) crossover 13EMA and next Bar exceed 'Crossover Bar's' high and vice versa for Sell Signal.

Problem is that Once I got Buy signal it is repeating before opposite signal generating. See the Chart below.

enter image description here

The Code is as follows...

//@version=5
indicator('Alert on MA Cross 1.0', overlay=true)

// MA Crossover condition
ma = ta.ema(close, 13)
xUp = ta.crossover(close, ma)
xDn = ta.crossunder(close, ma)

// Fixing High or Low of Crossover or Crossunder Bar
xUpHigh = xUp ? high : na
xDnLow = xDn ? low : na

//Signal after crossing High/Low of Crossover or Crossunder Bar
signalUp = high > xUpHigh[1] ? high : na
signalDn = low < xDnLow[1] ? low : na

// Alert Condition creation 
if signalUp
    alert('Price(' + str.tostring(close) + ') crossed over MA (' + str.tostring(ma) + ').', alert.freq_once_per_bar)
if signalDn
    alert('Price(' + str.tostring(close) + ') crossed under MA (' + str.tostring(ma) + ').', alert.freq_once_per_bar)

// Plotting MA and Signal
plot(ma)
plotshape(signalUp, style=shape.triangleup, location=location.absolute, color=color.new(color.green, 0), size=size.tiny, title='signalUp')
plotshape(signalDn, style=shape.triangledown, location=location.absolute, color=color.new(color.red, 0), size=size.tiny, title='signalDn')

// Trigger Alert Condition
alertcondition(signalUp or signalDn, title='MA Alert', message='Price x EMA Detected')

Upvotes: 0

Views: 643

Answers (1)

vitruvius
vitruvius

Reputation: 21342

Have a var that indicates if you are already long. Then a buy signal should only be allowed when you are not long, and a sell signal should only be allowed when you are already long.

//@version=5
indicator('Alert on MA Cross 1.0', overlay=true)

var isLong = false

// MA Crossover condition
ma = ta.ema(close, 13)
xUp = ta.crossover(close, ma)
xDn = ta.crossunder(close, ma)

// Fixing High or Low of Crossover or Crossunder Bar
xUpHigh = xUp ? high : na
xDnLow = xDn ? low : na

//Signal after crossing High/Low of Crossover or Crossunder Bar
signalUp = high > xUpHigh[1] ? high : na
signalDn = low < xDnLow[1] ? low : na

isBuy = not isLong and signalUp
isSell = isLong and signalDn

isLong := isBuy ? true : isSell ? false : isLong

// Alert Condition creation 
if isBuy
    alert('Price(' + str.tostring(close) + ') crossed over MA (' + str.tostring(ma) + ').', alert.freq_once_per_bar)
if isSell
    alert('Price(' + str.tostring(close) + ') crossed under MA (' + str.tostring(ma) + ').', alert.freq_once_per_bar)

// Plotting MA and Signal
plot(ma)
plotshape(isBuy, style=shape.triangleup, location=location.belowbar, color=color.new(color.green, 0), size=size.tiny, title='signalUp')
plotshape(isSell, style=shape.triangledown, location=location.abovebar, color=color.new(color.red, 0), size=size.tiny, title='signalDn')

// Trigger Alert Condition
alertcondition(isBuy or isSell, title='MA Alert', message='Price x EMA Detected')

enter image description here

Upvotes: 1

Related Questions