Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

NT8 backtesting within a user-defined time window

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

    NT8 backtesting within a user-defined time window

    Hello

    I want to be able to set a time frame for my back test.

    When running my strategy live, my code for allowing the user to define the start and end times is working fine:

    Code:
    if ((DateTime.Now > StartTime)
    && (DateTime.Now < EndTime)){//make money}
    But when I run a backtest it is, of course, comparing to "Now" now, and not "Now" as it occurred within the backtest time series. So to even get a backtest to work I have to set the time window to include the current time otherwise it thinks it should not execute (in other words, if I have the window set to US market hours, and I try to back test at 10pm, it won't work unless I include 10pm in the time window of the strategy which "allows" it to backtest at that time).

    How would you recommend I change this 'if' statement so I could backtest my strategy as running only in the morning, for example. Or similarly, to be able to run aa backtest outside of the hours I am defining within the strategy?

    Thanks


    #2
    Hello Smoky,

    Thank you for the post.

    You would use the condition you have but you would need to stop using DateTime.Now. DateTime.Now represents your PC time and has no specific relation to NinjaScript processing. You could replace that with Time[0] which represents the current bar processing and its time.

    Time[0] could be used for historical or realtime processing, that always represents the current bar being processed. In realtime that would be the last closed bars time or the last ticks time depending on the Calculate setting.

    I look forward to being of further assistance.
    JesseNinjaTrader Customer Service

    Comment


      #3
      Thank you!

      Comment


        #4
        This did not work. Using Time[0] did not work. I used Time[0] instead of DateTime.Now per my code example above and the strategy never fired off once all day, whereas when I use Datetime.Now, it would work in real time, just not for historical backtesting.

        Should I use Time[0] for backtesting and DateTime.Now for real time operation?

        Thanks

        Comment


          #5
          Hello Smoky,

          Without more detail on what you tried it would be hard to say why it did not trade. The Time[0] would be correct for historical or realtime data because that represents the times for the bars that are being processed. DateTime.Now is your PC time, the time in the lower right of your start bar. That will technically work during realtime but holds no relation to the platform and how it processes data. You should try to always use the times that you can access in the platform over using the PC clock.

          To find out whats happening you could use a Print here to identify why the condition was not true or what specifically is happening when you use Time[0] in your script.

          Please let me know if I may be of additional assistance.
          JesseNinjaTrader Customer Service

          Comment


            #6
            I'll dig around but if I use the system's clock I can run my strategy in real time, and run crippled backtests. If I use Time[0] then nothing works.

            Comment


              #7
              Hello Smoky,

              Right because in realtime the PC clock would represent now which is also what realtime is. Historical data timestamps would be for the bars you are processing in historical data so that would hold no relation to your pc time of now.

              id suggest a print before doing any other testing to get a idea of whats happening. If you were still using what you originally posted as the condition you could try something like this:

              Code:
              Print(Time[0] + " - StartTime: " + StartTime); 
              
              if ((DateTime.Now > StartTime)
              && (DateTime.Now < EndTime)){//make money}
              If your start time and end times are specific dates and not something you are calculating in OnBarUpdate that's likely why its not becoming true, you could use ToTime if you are intending to make a range of hours to trade. https://ninjatrader.com/support/help...ightsub=totime

              Code:
              int StartTime = 74500;
              int EndTime = 134500;
              
              if (ToTime(Time[0]) >= StartTime && ToTime(Time[0]) <= EndTime)
              {
              // Strategy logic goes here
              }

              Please let me know if I may be of additional assistance.
              JesseNinjaTrader Customer Service

              Comment


                #8
                Thanks I will try that. I am collecting starttime and endtime from the built-in NinjaTrader.Gui.Tools.TimeEditorKey and assigning them as Datetime. I assume if they are comparable to datetime.now then the Time[0] should work as well. I will try using ToTime as I had not seen that yet.

                Whoops: what you are saying isn't working, error says I cannot compare int to system.Datetime
                Last edited by Smoky; 01-05-2021, 03:59 PM.

                Comment


                  #9
                  Hello Smoky,

                  The TimeEditorKey will generate a date time object of a specific date so thats likely where the problem is. You can pass that to ToTime if you wanted to extract just the hours.

                  In the sample I provided i used an int for the variables, if your StartTime/EndTime is a DateTime you would need to wrap that in ToTime

                  Code:
                  ToTime(StartTime)
                  You could then do the same and compare that against the int times like the sample.


                  I look forward to being of further assistance.
                  JesseNinjaTrader Customer Service

                  Comment


                    #10
                    That got it sorted. Thanks!

                    Comment

                    Latest Posts

                    Collapse

                    Topics Statistics Last Post
                    Started by jaybedreamin, Today, 05:56 PM
                    0 responses
                    3 views
                    0 likes
                    Last Post jaybedreamin  
                    Started by DJ888, 04-16-2024, 06:09 PM
                    6 responses
                    18 views
                    0 likes
                    Last Post DJ888
                    by DJ888
                     
                    Started by Jon17, Today, 04:33 PM
                    0 responses
                    1 view
                    0 likes
                    Last Post Jon17
                    by Jon17
                     
                    Started by Javierw.ok, Today, 04:12 PM
                    0 responses
                    6 views
                    0 likes
                    Last Post Javierw.ok  
                    Started by timmbbo, Today, 08:59 AM
                    2 responses
                    10 views
                    0 likes
                    Last Post bltdavid  
                    Working...
                    X