Help with setting limit order to be previous days closing candle

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #134348 quote
    AleksLi
    Participant
    Junior

    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

    1.png 1.png
    #134362 quote
    robertogozzi
    Moderator
    Master

    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.

    #134386 quote
    AleksLi
    Participant
    Junior

    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

    2-1.jpg 2-1.jpg
    #134493 quote
    robertogozzi
    Moderator
    Master

    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
    Nicolas thanked this post
Viewing 4 posts - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.

Help with setting limit order to be previous days closing candle


ProOrder: Automated Strategies & Backtesting

New Reply
Author
author-avatar
AleksLi @aleksli Participant
Summary

This topic contains 3 replies,
has 2 voices, and was last updated by robertogozzi
5 years, 8 months ago.

Topic Details
Forum: ProOrder: Automated Strategies & Backtesting
Language: English
Started: 06/03/2020
Status: Active
Attachments: 2 files
Logo Logo
Loading...