Conditions -
- I want to find the first green candle after any red candle.
- Once that is found I want to look for a second green candle after the first green candle.The high of the second green candle should be more than the high of the first green candle.
- After that I want to find another candle whose value reaches 10 points more than the high of the second green candle.
- As soon as this candle is found I want to enter a long trade on the very same candle.
- Note - There can be other green candles between the first green candle, the second green candle and the entry candle but there cannot be any red candles between them.Eg. In the below picture, you can see the first green candle is marked by down arrow. Now the next candle is also green since its close > open, but we won't consider it as the second green candle. Now the third green candle has a high > high of the first green candle and hence it will be our second green candle. Similarly we will look for the next green candle whose price reaches 10 points more than the second green candle. We consider this candle as the entry green candle.
- If red candle is found then conditions are invalidated.
I can manually backtest as I can see many instances of the conditions meetings my criteria but the code is finding no trades.Can you please check, where the code is wrong ?
//@version=5
strategy("Green and Red Candles Strategy", overlay=true, calc_on_every_tick = true)
var int firstGreen = na
var int secondGreen = na
var int entryGreen = na
var float entryPrice = na
isGreen = close > open
isRed = close < open
firstGreenFound = false
secondGreenFound = false
if isGreen and isRed[1]
firstGreen := bar_index
firstGreenFound := true
// Find second green candle after firstGreen with higher high, and no red candles in between
if not na(firstGreen) and bar_index > firstGreen
if isGreen and na(secondGreen)
if high > high[firstGreen]
secondGreen := bar_index
secondGreenFound := true
entryPrice := high[secondGreen] + 10
if not na(secondGreen) and bar_index > secondGreen
if isGreen and na(entryGreen)
if strategy.position_size < 1 and high > high[secondGreen] + 10
entryGreen := bar_index
plotFirstGreen = plot(firstGreen, title='First Green', color=color.blue)
plotSecondGreen = plot(secondGreen, title='Second Green', color=color.green)
plotEntryGreen = plot(entryGreen, title='Entry Green', color=color.red)
if not na(entryGreen)
// Check for red candles between firstGreen and entryGreen
for i = firstGreen + 1 to entryGreen - 1
if isRed[i]
entryGreen := na
break
if not na(entryGreen)
strategy.entry("Long", strategy.long)
firstRed = close < open and close[1] > open[1]
secondRed = firstRed and close < close[1] and low < low[1]
if strategy.position_size > 0 and (secondRed and low < low[1] - 10)
strategy.close("Long")
plotshape(firstGreenFound, color=color.green, style=shape.triangleup, location=location.belowbar)
plotshape(secondGreenFound, color=color.green, style=shape.triangleup, location=location.belowbar)