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, 03-31-2026, 09:41 PM
        1 response
        77 views
        1 like
        Last Post NinjaTrader_ChelseaB  
        Started by CarlTrading, 04-01-2026, 02:41 AM
        0 responses
        40 views
        0 likes
        Last Post CarlTrading  
        Started by CaptainJack, 03-31-2026, 11:44 PM
        0 responses
        63 views
        2 likes
        Last Post CaptainJack  
        Started by CarlTrading, 03-30-2026, 11:51 AM
        0 responses
        63 views
        0 likes
        Last Post CarlTrading  
        Started by CarlTrading, 03-30-2026, 11:48 AM
        0 responses
        53 views
        0 likes
        Last Post CarlTrading  
        Working...
        X