How to code trade only trade hours if trading hours cross midnight ?

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #179059 quote
    SkippyTrader
    Participant
    New

    How to make the strategy trade throughout trade hours if the hours cross to the next day –  when the exchanges trade hours cross over midnight ?

    For example the DAX: Opens = 1800 AEDT,  Closes =0930 AEDT (the following day).

    How as such to code FLATBEFORE 1800, and FLATAFTER 09:30 (the next day) ?  Without having issue of having to do 2 different strategies and the lose of money from the spread by having to  sell/close at 235959 and then buy/open at 000001.

    Or is there any  way that strategies could be coded to avoid different times too, eg. for the USDJPY to be closed at end of USA trade (0630) until start of Japan trade (1130) to avoid whiplashes)

    Thanks in advance 🙂

    #179091 quote
    robertogozzi
    Moderator
    Master

    This will do:

    StartTime = 180000  //try also 110000
    EndTime   = 093000  //try also 160000
    TradeON   = 0
    IF OnMarket THEN
       SELL AT Market
    ENDIF
    IF StartTime <= EndTime THEN
       TradeON = (time >= StartTime) AND (Time <= EndTime)
    ELSE
       TradeON = ((time >= StartTime) AND (Time < 240000)) OR ((Time >= 000000) AND (Time <= EndTime))
    ENDIF
    IF Not OnMarket AND Tradeon then
       BUY AT Market
    ENDIF

    TradeON will be true whenever the candle is within the chosen time range.

    SkippyTrader thanked this post
    #179106 quote
    PeterSt
    Participant
    Master
    IF OnMarket THEN
       SELL AT Market
    ENDIF

    Roberto, what do you intend with this ? It looks like you were in a hurry somewhat. 🙂

    Or is there any  way that strategies could be coded to avoid different times too, eg. for the USDJPY to be closed at end of USA trade (0630) until start of Japan trade (1130) to avoid whiplashes)

    Assumed I understood correctly : I think I would dive into over-creativity and code something like

    TradeOn = 0
    TradeBailOut = 0
    InstrIsEURUSD = 0
    InstrIsEURGBP = 0
    InstrIsUSDJPY = 0
    
    If Close > 1.14 and Close < 1.19 then   // EUR/USD ?
      InstrIsEURUSD = 1
      TradeOn = 1
    Endif
    
    If Close > 0.81 and Close < 0.91 then   // EUR/GBP ?
      InstrIsEURGBP = 1
      TradeOn = 1
    Endif
    
    If Close > 106 and Close < 117 then     // USD/JPY ?
      InstrIsUSDJPY = 1
      TradeOn = 1
    Endif
    
    If TradeEURUSD then
      // Here the code for the EUR/USD TradeOn times and other pair specialties :
      // If ... etc. then
      //   TradeOn = 1
      // endif
    
      // Here the code for the EUR/USD Bail-out times :
      // If ... etc. then
      //   TradeBailOut = 1
      // endif
    endif
    
    If TradeEURGBP then
      // Here the code for the EUR/GBP TradeOn times and other pair specialties :
      // If ... etc. then
      //   TradeOn = 1
      // endif
    
      // Here the code for the EUR/GBP Bail-out times :
      // If ... etc. then
      //   TradeBailOut = 1
      // endif
    endif
    
    If TradeUSDJPY then
      // Here the code for the USD/JPY TradeOn times and other pair specialties :
      // If ... etc. then
      //   TradeOn = 1
      // endif
    
      // Here the code for the USD/JPY Bail-out times :
      // If ... etc. then
      //   TradeBailOut = 1
      // endif
    endif
    
    // Bail out before Settlement (end of day etc.) :
    
    If OnMarket then
    
      If TradeBailOut
        // My Sell At Market code.
      endif
    
      TradeOn = 0
    endif
    
    If TradeOn then
       // My General Fx Strategy code.
    endif

    IOW, welcome to my monster.

    This would work as long as I regularly revisit the code and actualise the bands to recognise the pairs. Obviously there should not be any overlap between the pairs.

    Please notice that in the end this will not be workable really, because of too many specifics for the pair. Thus, you won’t be able to capture all in the same strategy code. But this is up to you of course.

    Good luck !
    Peter

    SkippyTrader thanked this post
    #179108 quote
    robertogozzi
    Moderator
    Master

    My fault, lines 4-5-6 have no relation with the time question.

    I still had it in my mind from what I had written a few minutes before!

    They correct code is:

    StartTime = 180000  //try also 110000
    EndTime   = 093000  //try also 160000
    TradeON   = 0
    IF StartTime <= EndTime THEN
       TradeON = (time >= StartTime) AND (Time <= EndTime)
    ELSE
       TradeON = ((time >= StartTime) AND (Time < 240000)) OR ((Time >= 000000) AND (Time <= EndTime))
    ENDIF
    IF Not OnMarket AND Tradeon then
       BUY AT Market
    ENDIF
    SkippyTrader thanked this post
Viewing 4 posts - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.

How to code trade only trade hours if trading hours cross midnight ?


ProOrder: Automated Strategies & Backtesting

New Reply
Author
Summary

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

Topic Details
Forum: ProOrder: Automated Strategies & Backtesting
Language: English
Started: 10/05/2021
Status: Active
Attachments: No files
Logo Logo
Loading...