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 CarlTrading, 05-11-2026, 05:56 AM
        0 responses
        56 views
        0 likes
        Last Post CarlTrading  
        Started by CarlTrading, 05-10-2026, 08:12 PM
        0 responses
        34 views
        0 likes
        Last Post CarlTrading  
        Started by Hwop38, 05-04-2026, 07:02 PM
        0 responses
        195 views
        0 likes
        Last Post Hwop38
        by Hwop38
         
        Started by CaptainJack, 04-24-2026, 11:07 PM
        0 responses
        359 views
        0 likes
        Last Post CaptainJack  
        Started by Mindset, 04-21-2026, 06:46 AM
        0 responses
        281 views
        0 likes
        Last Post Mindset
        by Mindset
         
        Working...
        X