0

Conditions -

  1. I want to find the first green candle after any red candle.
  2. 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.
  3. After that I want to find another candle whose value reaches 10 points more than the high of the second green candle.
  4. As soon as this candle is found I want to enter a long trade on the very same candle.
  5. 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. enter image description here
  6. 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)

2 Answers2

1

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.

elod008
  • 1,227
  • 1
  • 5
  • 15
0

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).

karatedog
  • 2,508
  • 19
  • 29
  • But the condition that there can be any number of green candles (but no red candles) between the first candle, the second candle and the entry candle must be true. From the above code it looks like the script checks for continuous three green candles if I am not wrong. Can it be modified to accomodate my conditions please ? – Knight of the Vale Jul 10 '23 at 13:07
  • I have mentioned the conditions clearly in bullet points for more clear understanding. Thanks @karatedog – Knight of the Vale Jul 10 '23 at 13:15
  • for comment - "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)." What I simply mean is that once the first green candle is found with a certain high, there can be other green candles after that whose close > open but their highs don't exceed the high of the first green candle. When this condition is met, that's what I will call my second green candle. – Knight of the Vale Jul 10 '23 at 13:18
  • @KnightoftheVale The code can work with any arbitrary number of green candles. That is why I put in `any_green_bars `. That will be set to a number that shows how many green candles you have backwards (it can be 6 or 9, whatever). And after that I'm working with the 1st Green candle, with the 2nd Green candle and with the last candle. To achieve that I use the `any_green_bars ` as an index. Apply this script to any symbol in Tradingview and check. Just be sure to adjust the current `1.01` (which defines 1% increase) – karatedog Jul 10 '23 at 14:18
  • @KnightoftheVale if the symbol you work with has significant gaps, it is possible that: Day 1 candle is 'green' as its close is higher than its open and Day 2 is the same, BUT Day 2 open and close are well below Day 1 (as there was some gap). In this case it is possible both candles are green AND Day 2's High is higher than Day 1's but Day 2's close is lower than Day 1's. You usually don't see this with crypto as it is 24/7 traded but assets that don't trade over the weekend can have surprises on Mondays. – karatedog Jul 10 '23 at 14:21
  • @KnightoftheVale Okay, the updated requirements changes a lot. To achieve what you want, finding conditions and saving states might be easier than my solution as there can be any number of green candles between the 1st and "2nd" which I did not expect. Also point 4.: You can only realize the condition when the candle is close (that is when you will have proper data on its High). This means your trade will start on the next candle's start. – karatedog Jul 10 '23 at 14:35
  • understood. About point 4, I was under the impression that in real time market, the "high" and "close" variables can provide real time price values and I was hoping that as soon as the "close := secondGreen + 10" condition is met then we can enter the trade immediately, i.e. at the same candle. Once the trade is entered and then even if the entry candle finally closes in red, then also it's fine. – Knight of the Vale Jul 10 '23 at 14:43
  • You can achieve that in two ways: Work with a very low time frame, like 1m or even 30sec, however "green" does not tell you as much on those time frames like on higher time frames. Or place a Limit BUY order with your broker with the calculated price so you don't need to manage the entry as it will be decoupled from your script's execution. Then check back later if it got executed or cancel it if certain invalidation period expired. – karatedog Jul 11 '23 at 09:24