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 could have become 1 only 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
Available help for set stop loss:
LOSS
set stop loss expects a distance rather than a price level, and your rangemid variable is a price level. The corresponding distance would be, when the code is read at candle close inside your IF statement:
stopdistance= close-rangemid
set stop loss stopdistance
That’s stop distance for a buy, for a sellshort when rangemid is above at first it would be of course: stopdistance=rangemid-close
Of course, the candle close when code is read might be slightly different from next candle open when order is executed (the execution itself might suffer from slippage vs open too), but when you first set the stop, you don’t know yet entry execution so to define an initial stop distance, you can use candle close prior to entry execution, and if needed modify it in subsequent candles whether it’s to amend it just once after initial stop, or to create a custom dynamic trailing stop.
All other commands relevant to defining a stop can be found there if needed, clicking on “+ probacktest” in the left margin:
Welcome to ProBuilder Programming