I have created a code where i create a sell order if previous candle is above 5ema and do not touch 5ema and current candle price is less than low of previous candle. However in few candles i see that the sell order is triggered even when the previous candle crossed below 5ema.
/@version=5
strategy("New Gan Sell Signal 5EMA", overlay=true)
// Calculate 5 EMA
emaLength = 5
emaValue = ta.ema(close, emaLength)
previousCandleClose = close[1]
previousCandleOpen = open[1]
previousCandleHigh = high[1]
prevLow = low[1] // Get the low of the previous candle
// Check if previous candle's close and open were above the 5 EMA
previousCandleAboveEMA = previousCandleClose > emaValue and previousCandleOpen > emaValue
// Check if current price is below previous low
currentPriceBelowPreviousLow = close < prevLow
//Check if previous candle price is greater than ema value previouscandlelow = prevLow > emaValue
// Generate sell signal sellSignal = previousCandleAboveEMA and currentPriceBelowPreviousLow and previouscandlelow
// Plotting the 5 EMA and sell signal plot(emaValue, color=color.blue, title="5 EMA") plotshape(sellSignal, style=shape.triangledown, color=color.red, size=size.small, title="Sell Signal")
It looks like you have an error in your code on the following line. You are checking the previous low vs the current ema value. You want to check it against the previous ema value. Updated line here
previouscandlelow = prevLow > emaValue[1]