Hello ProRealCode community!
I’m still a bit new to coding and I need a bit of help with something. I have a mean reversion script here that essentially sells/buys the open if we’ve deviated a bit too much from the previous days 9pm candle open.
Going through the backtest results I’ve noticed that my target limit order was set at the wrong price level- instead of the target being yesterdays 9pm opening candle, it was todays 7am opening candle.
What I think might’ve happened is the “open” variables got a bit mixed up – I just don’t know how to fix this.
Any help you can give me with my code will be greatly appreciated. I’ve attached a screenshot with a little example of what went wrong.
defparam cumulateorders=false
// ---
amount = 3 //amount of lots/shares/contratcs for each order
stoploss = 30 //stoploss in points
previousDayTime = 210000 //open of the previous session's UK 21:00 candle
currentDayTime = 070000 //open of the UK 07:00 candle
// ---
if time=previousDayTime then
prev = open
endif
tcondition = time=currentDayTime
c1 = prev-open
if c1 > 0 then
c2=c1*1
endif
if c1 < 0 then
c2=c1*-1
endif
if tcondition and not onmarket and open<prev and c2=>stoploss then
buy amount contract at market
set target pprofit prev
set stop ploss stoploss
endif
if tcondition and not onmarket and open>prev and c2=>stoploss then
sellshort amount contract at market
set target pprofit prev
set stop ploss stoploss
endif
if LongOnMarket And time>210000 then
sell at market
endif
if ShortonMarket And time>210000 then
buy at market
endif
Thanks,
Aleks
Between lines 10 and 11 insert a new one (wich will become the new line 11):
PreviousPrev = Prev
whenevr you need to use today’s PREV, use PREV, whenever you need to use yesterday’s PREV use PREVIOUSPREV, instead.
Hi Roberto,
Thanks for taking the time to look into this issue. However, the issue persists. Here’s my code:
defparam cumulateorders=false
// ---
amount = 3 //amount of lots/shares/contratcs for each order
stoploss = 30 //stoploss in points
previousDayTime = 210000 //open of the previous session's UK 21:00 candle
currentDayTime = 070000 //open of the UK 07:00 candle
// ---
if time=previousDayTime then
PreviousPrev = Prev
Prev = open
endif
tcondition = time=currentDayTime
c1 = PreviousPrev-open
if c1 > 0 then
c2=c1*1
endif
if c1 < 0 then
c2=c1*-1
endif
if tcondition and not onmarket and PreviousPrev<Prev and c2=>stoploss then
buy amount contract at market
set target pprofit PreviousPrev
set stop ploss stoploss
endif
if tcondition and not onmarket and PreviousPrev>Prev and c2=>stoploss then
sellshort amount contract at market
set target pprofit PreviousPrev
set stop ploss stoploss
endif
if LongOnMarket And time>210000 then
sell at market
endif
if ShortonMarket And time>210000 then
buy at market
endif
Could you advise if I implemented the right changes?
Thanks,
Aleks
Since at line 10 you are using TIME, open will return the OPENing price od the candle that closes at 210000. If you need the opening price of the candle starting at 210000 then you’ll have to use OPENTIME, instead.
In line 5 you should use
30 * pipsize
(despite Dax makes no difference), but it’s good programming practice when you need to convert pips into price.
Lines 29 and 35 should read:
set target profit abs(PreviousPrev - close) //difference in price to use PROFIT
or
set target pprofit abs(PreviousPrev - close) / pipsize //difference in pips to use pPROFIT