Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Need suggestion on how to set trading time range for overnight session.

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Need suggestion on how to set trading time range for overnight session.

    Hi experts,
    My initial code was something like this
    int startTradeTime = 80000; //8am
    int endTradeTime = 153000; //3.30pm

    if ((ToTime(Time[0]) >= startTradeTime && ToTime(Time[0]) < endTradeTime) )
    { allows trading }​
    If I want to start at 9pm and goes until 3.30am, how shall I implement it ?



    edit: found solution.
    compare start and end time to establish if it's regular or overnight session.
    if regular and now is between start and end => proceed
    if overnight and now is not between end and start => proceed
    Last edited by elirion; 09-02-2023, 04:20 AM.

    #2
    Try this,

    Code:
    private bool IsGoodTime()
    {
        bool status = true;
    
        if (startTradeTime < endTradeTime)
            status = ToTime(Time[0]) >= startTradeTime && ToTime(Time[0]) < endTradeTime;
    
        if (startTradeTime > endTradeTime)
            status = ToTime(Time[0]) >= startTradeTime || ToTime(Time[0]) < endTradeTime;
    
        return status;
    }
    The idea is that if startTradeTime and endTradeTime are both zero,
    (actually, they must simply be equal to each other) then status remains
    true -- thus, if both zero, you're trading 24hrs with no restrictions.

    If start < end ... like 8am to 3pm .. it's handled as you would expect.
    eg, 80000 < 150000 means regular hours.

    If start > end ... like 8pm to 3am .. overnight is also handled correctly.
    eg, 200000 > 30000 means overnight session.

    Good reading in the attached files here.




    Last edited by bltdavid; 09-02-2023, 01:59 PM. Reason: typo

    Comment


      #3
      Originally posted by bltdavid View Post
      Try this,

      Code:
      private bool IsGoodTime()
      {
      bool status = true;
      
      if (startTradeTime < endTradeTime)
      status = ToTime(Time[0]) >= startTradeTime && ToTime(Time[0]) < endTradeTime;
      
      if (startTradeTime > endTradeTime)
      status = ToTime(Time[0]) >= startTradeTime || ToTime(Time[0]) < endTradeTime;
      
      return status;
      }
      The idea is that if startTradeTime and endTradeTime are both zero,
      (actually, they must simply be equal to each other) then status remains
      true -- thus, if both zero, you're trading 24hrs with no restrictions.

      If start < end ... like 8am to 3pm .. it's handled as you would expect.
      eg, 80000 > 150000 means regular hours.

      If start > end ... like 8pm to 3am .. overnight is also handled correctly.
      eg, 200000 > 30000 means overnight session.

      Good reading in the attached files here.




      Appreciate your input

      Comment


        #4
        Hello elirion,

        Thanks for your post.

        I am happy to see you found a solution to your question.

        Time Filters could be used to check if the current time is within a specified timeframe in the script as you have noted.

        See the 'How to Time Filters' section of this help guide page: https://ninjatrader.com/support/help...on_builder.htm
        <span class="name">Brandon H.</span><span class="title">NinjaTrader Customer Service</span><iframe name="sig" id="sigFrame" src="/support/forum/core/clientscript/Signature/signature.php" frameborder="0" border="0" cellspacing="0" style="border-style: none;width: 100%; height: 120px;"></iframe>

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by NullPointStrategies, Yesterday, 05:17 AM
        0 responses
        72 views
        0 likes
        Last Post NullPointStrategies  
        Started by argusthome, 03-08-2026, 10:06 AM
        0 responses
        143 views
        0 likes
        Last Post argusthome  
        Started by NabilKhattabi, 03-06-2026, 11:18 AM
        0 responses
        76 views
        0 likes
        Last Post NabilKhattabi  
        Started by Deep42, 03-06-2026, 12:28 AM
        0 responses
        47 views
        0 likes
        Last Post Deep42
        by Deep42
         
        Started by TheRealMorford, 03-05-2026, 06:15 PM
        0 responses
        51 views
        0 likes
        Last Post TheRealMorford  
        Working...
        X