Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Restricting trading times

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

    Restricting trading times

    I want to restrict the times during which my strategy will execute trades so I have the following code in place

    if (ToTime(Time[0]) >= timeStart && ToTime(Time[0]) <= timeStop)
    {
    // execute strategy
    }

    where timeStart=94500 and timeStop=161000 for example. Why is it that the strategy is randomly started before the start time? Today for example the first trade was placed at
    9:10 AM.

    What am I doing wrong? Is three another way to set limits on trading times?

    #2
    Hi Seahn,

    Thank you for posting.

    The code looks good for creating a time filter.

    Who are you connected to? This is displayed in the lower left hand corner of the Control Center.

    What chart interval are you using?

    I would also try using the Print() function to print out what times you are getting from the code.
    Code:
    Print(Time[0]);

    I look forward to assisting you further
    Cal H.NinjaTrader Customer Service

    Comment


      #3
      I am connected to Interactive Brokers, using 25 minute charts. I will do some tracing using Print statements. The problem is intermittent so I will see if I can catch it.

      Thanks

      Comment


        #4
        I completed testing of this problem using trace Print statements. What I have found is that for my setup at least the ToTime(Time[0]) function returns basically completely unreliable data. I don't know if the issue is my particular setup or what but it is unusable. I have had to re-code using the C# DateTime structure:

        private DateTime now;
        private TimeSpan timeStart=new TimeSpan(9,45,0);
        private TimeSpan timeStop=new TimeSpan(16,10,0);

        now = DateTime.Now;
        if (now.TimeOfDay.CompareTo(timeStart)>0 && now.TimeOfDay.CompareTo(timeStop)<0)
        {
        // execute strategy
        }

        The code above obviously will not work in backtesting so I had to add code to check for historical strategy execution and continue to use ToTime(Time[0]) in that case which does appear to work.

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by Geovanny Suaza, 02-11-2026, 06:32 PM
        0 responses
        670 views
        0 likes
        Last Post Geovanny Suaza  
        Started by Geovanny Suaza, 02-11-2026, 05:51 PM
        0 responses
        379 views
        1 like
        Last Post Geovanny Suaza  
        Started by Mindset, 02-09-2026, 11:44 AM
        0 responses
        111 views
        0 likes
        Last Post Mindset
        by Mindset
         
        Started by Geovanny Suaza, 02-02-2026, 12:30 PM
        0 responses
        575 views
        1 like
        Last Post Geovanny Suaza  
        Started by RFrosty, 01-28-2026, 06:49 PM
        0 responses
        582 views
        1 like
        Last Post RFrosty
        by RFrosty
         
        Working...
        X