Hi there,
I’m stuck with my coding at the moment. I put some info into the Code that describes what i try to do.
Unfortunately it is not working with Backtesting. Can perhaps someone take a look what i did wrong?
DEFPARAM CUMULATEORDERS = false
DEFPARAM FLATAFTER = 172500
/////////////////////////////////////////////////////////////////////
MaxTrades = 1
TimeOK = OpenTime < 120000
// should define the first Candle of the day
IF IntradayBarIndex = 0 THEN
RangeTop = High
RangeBottom = Low
RangeMid = (High + Low) / 2
LongTally = 0
ShortTally = 0
ENDIF
// Event A
IF Close > RangeTop THEN
EventA = 1
ENDIF
// if Event A triggered and AFTER THAT Event B should Trigger when the low breaks RangeTop
IF EventA = 1 AND Low < RangeTop THEN
EventB = 1
ENDIF
// if Event A & B triggered and AFTER THAT Event C should Trigger when close is over RangeTop again
IF EventA = 1 AND EventB = 1 AND Close > RangeTop THEN
EventC = 1
ENDIF
// buy when Event A, B & C are Triggered
IF EventA = 1 AND EventB = 1 AND EventC = 1 AND LongTally < MaxTrades AND TimeOK THEN
BUY 1 CONTRACTS AT MARKET
LongTally = LongTally +1
ENDIF
Hi,
I tried it, and the backtest took some trades. So I am guessing that with “it is not working”, you don’t talk about error message or a backtest not taking trade, but instead you mean the logic you want to implement with successive events triggered is not applied with this code?
I’ll assume your rangetop is meant to be defined only at first candle, and you run the backtest on same timeframe as this first candle you’re looking at, so I skip any suggestion updating rangetop as the day goes along.
First thing reading the code is: if you reinitialise rangetop and longtally at first candle of the day, it implies you start from fresh everyday, therefore the way you have defined events A,B, C becoming =1 within IF statements would imply you also need to reinitialise those to zero at beginning of day:
IF IntradayBarIndex = 0 THEN
RangeTop = High
RangeBottom = Low
RangeMid = (High + Low) / 2
LongTally = 0
ShortTally = 0
EventA = 0
EventB = 0
EventC = 0
ENDIF
Next, your “AFTER THAT” notes imply you want to detect the events one after the other, but not during the same candle. If that’s the case, I would code them from C to A rather than from A to C, to make sure when testing eventB that the variable eventA became 1 in an earlier candle and not in same candle just a few lines before (resp event C vs A and B):
// if Event A & B triggered and AFTER THAT Event C should Trigger when close is over RangeTop again
IF EventA = 1 AND EventB = 1 AND Close > RangeTop THEN
EventC = 1
ENDIF
// if Event A triggered and AFTER THAT Event B should Trigger when the low breaks RangeTop
IF EventA = 1 AND Low < RangeTop THEN
EventB = 1
ENDIF
// Event A
IF Close > RangeTop THEN
EventA = 1
ENDIF
Hopefully it might help, but it might not be the final solution, as debugging can already take several consecutive steps for oneself, and a few more steps when someone else is trying to be on same wavelength as what you are trying to code?
Hi,
wow, thanks for your answer! I forgot to set the Events to 0, thanks for the info!
The problem is exactly what you described. It always buy after the first candle closes.
I will try your advises, thanks again!
Thanks again, it works now! One thing i wonder is why i cannot set the stop loss to the RangeMid?
What did i miss this time?
// buy when Event A, B & C are Triggered
IF EventA = 1 AND EventB = 1 AND EventC = 1 AND LongTally < MaxTrades AND TimeOK THEN
BUY 1 CONTRACTS AT MARKET
SET STOP LOSS RangeMid
LongTally = LongTally +1
ENDIF