Reputation: 189
Conditions -
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)
Upvotes: 0
Views: 121
Reputation: 2616
Your script shows the naïve approach to the solution. Unfortunately Pine needs a bit of a head replacement surgery to neatly align with its concept (or time, cursing all along the way).
You can check bars in Pine in sequence although the indefinite number of green candles between the first candle and the entry candle complicates things a bit:
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © karatedog
//@version=5
indicator("Red+Greens", overlay = true)
// This will count the number of green bars from the current bar backwards and gives us a number. I'll use this as an index
any_green_bars = ta.barssince(open >= close)
// This will check if we have at least 3 green bars which is your prerequisite
condition_1 = any_green_bars >= 3
// This will check if the second green bar's High is higher than the first green bar's. The ternary operator will prevent the index go below 0.
condition_2 = condition_1 ? high[any_green_bars-2] > high[any_green_bars-1] : 0
// This will check if the third green bar's High is higher than the second's by the expected amount. I've put in 1%, on the EUR/HUF daily symbol. Adjust it for the symbol you want to test as 1% might be large for that. The ternary operator will prevent the index go below 0.
condition_3 = condition_1 ? high[0] > high[any_green_bars-1] * 1.01 : 0
summarized_condition = condition_1 and condition_2 and condition_3
plotchar(series = summarized_condition, title = 'Summarized conditions', char = '*', location = location.abovebar)
summarized_condition
will be either atomically true or false, recalculated on every candle. You don't need to check barindex or which came after which or check in-between red candles.
If you don't work with very exotic symbols, na()
function is not needed on basic OHLC data.
If you work on symbols that can have significant gaps, you should review (or re-think) how you want to define green
candles (look up Hollow Candles for some extra info).
Upvotes: 0
Reputation: 1362
What I can tell at first sight is that your history referencing is wrong. All occurances of eg. high[firstGreen]
or high[secondGreen]
are wrongfully declared.
firstGreen and secondGreen receive bar_index
values so what you're saying is "Give me the high from [secondGreen] bars back." You intend to get the high on the secondGreen bar though. So modify these definitions to high[bar_index - secondGreen]
.
After that gladly do some debugging with your plotshapes or labels on the places you expect a trade to open/close so that you can see if the strategy functionality works properly.
Upvotes: 1