I am trying to code a backtest of a strategy, where I need to count the green candles to get my exit signal. Exit is on close of the first red candle after 3 green candles. Any ideas as to how I code that? I only have part of an idea – something with fractions and true or false. But even if that works – which it doesn’t right now – I still haven’t solved the exit on the red candle. Hope to hear your ideas. Half solutions or quater solutions are more than welcome!
Thank you and hope you write some great stuff today 🙂
Sthg like that…
myred = Close<Open
my3green = summation[3](Close>Open)=3
mysequence = my3green[1] and myred
if longonmarket and mysequence then
sell at market
endif
Hi JC
Thank you! That is so cool. Had no clue the summation could be used like that. And of course the red is as simple as that.
Thank you!!
Hi again
I’ve tested it on several instruments and I think it comes back with an exit after 3 green bars and one red in a row. Do you have any ideas for 3 green bars mixed with red bars (and then one red after that). There can be red bars mixed in between the 3 green ones.
All the best.
How many reds allowed between 1st and 2nd green, and between 2nd and 3rd green?
As many as it takes. Let’s say 15.
Ok, I’ve made the 15 a parameter Nredmax you can change if you want to. You just have to define your entry conditions before trying it. To be tested.
Nredmax=15
Nsequencemax=Nredmax+4// 3 greens + red at the end + Nredmax in between the 3 greens
green= close>open
red= close<open
if not longonmarket and yourentryconditions then // define your entry conditions
buy 1 contract at market
endif
if longonmarket and not longonmarket[1] then
barstart=barindex
endif
if summation[2](longonmarket)=2 and green[1] and red then
bartotal=barindex-barstart+1
vendre = (bartotal<=Nsequencemax and summation[bartotal](green)>=3) or (bartotal>Nsequencemax and summation[Nsequencemax](green)>=3)
if vendre then
sell at market
endif
endif
Thanks for taking the time to do this!