Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

Partner 728x90

Collapse

Backtesting and ToTime

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

    Backtesting and ToTime

    HI there, I am working on a strategy that test different time ranges using variables:

    private int rangeStart = 104000; //range start time;
    private int rangeEnd = 110000; //range end Time;

    When I run a backtest checking 10 minute intervals (1000) I get results for times like:

    104000
    105000
    106000
    107000
    108000
    109000

    I would assume that 106000 should have the same results as 110000 but they don't. Should they? I am not entirely sure how this is being calculated.

    Also is there a way to bypass tests on these non-existent times? a return; of some sort?

    Thanks,


    #2
    After typing the question I thought about it and created a copy of the strategy and thought I would give the return a try.

    Not exactly the result I wanted but does return Zero trades on these outside times. also added a return for if the rangeEnd >rangeStart also added notes in the Properties to indicate the min/max ranges.

    if((rangeStart>105000 &&rangeStart<110000)||(rangeStart>115000 &&rangeStart<120000))
    return;
    if((rangeEnd>105900 &&rangeEnd<110000)||(rangeEnd>115900 &&rangeEnd<120000)||(rangeEnd>125900 &&rangeEnd<130000))
    return;
    if (rangeEnd>rangeStart)
    return;

    Comment


      #3
      Hello stalt, thanks for your post.

      This reference sample shows how to limit trading hours in a script:


      Were you able to resolve your inquiry with the code you posted?

      I look forward to your reply.
      Chris L.NinjaTrader Customer Service

      Comment


        #4
        Yes I was able to limit the time. using return to stop the script from running when backtesting, but the Zero results still turn up in the results. not that big a deal I guess, this sure does speed up the backtesting though when you can remove erroneous times( like range start 109000) from running at the start of the script in ONBarUpdate.

        I am still curious how the script is reading 106000 if not equating it to 110000?
        Last edited by stalt; 05-17-2019, 09:02 AM.

        Comment


          #5
          Hello stalt, thanks for your reply.

          I wouldn't expect ToTime it to give a time of 106000. Can you post the code that is giving this output?

          I look forward to your reply.
          Chris L.NinjaTrader Customer Service

          Comment


            #6
            Maybe I wasn't clear. I have created 2 variables for range Start and Range End. They are both accessible when using the strategy analyzer.

            If I use the optimizer and RangeStart is set to:

            Min Value 100000
            Max Value 110000
            incriment 1000

            The following values are tested

            100000
            101000
            102000
            103000
            104000
            105000
            106000
            107000
            108000
            109000
            110000

            My question is what time is it actually being tested for 106000,107000,108000,109000?



            Comment


              #7
              Hi stalt,

              A value of 106000 would mean 10:60 AM, ToTimes should not be outputting these values. Is your code producing these values?

              I look forward to your reply.
              Chris L.NinjaTrader Customer Service

              Comment


                #8
                Yes it is.

                Here are my variables:

                private int rangeStart = 103000; //range start time;
                private int rangeEnd = 110000; //range end Time;

                Here are their properties

                [Description("Range Start Time")]
                [Category("Parameters")]
                [Gui.Design.DisplayName("Com: Range Start")]
                public int RangeStart
                {
                get { return this.rangeStart; }
                set { this.rangeStart = value; }
                }

                [Description("Range End Time")]
                [Category("Parameters")]
                [Gui.Design.DisplayName("Com: Range End")]
                public int RangeEnd
                {
                get { return this.rangeEnd; }
                set { this.rangeEnd = value; }
                }

                I have uploaded an image of the strat analyzer with the Paramaters and an instance where a range end of 109000 was tested. If you look in the Optimizer window you will see varios 108000 values and 107000 values.


                Comment


                  #9
                  Your parameters are not well defined for purposes of optimizing.

                  You can't do what you want to do because integer time 106000 should be normalized as 110000,
                  107000 should be normalized as 111000, etc.

                  The Strategy Analyzer doesn't know these integers are really "time integers" so it doesn't know how to
                  normalize them correctly: for ex, minutes above "59" mean the hour portion has to be incremented by 1
                  and the minute portion gets reset to MM - 60, thus "105900" is ok, but "106000" must be normalized to
                  "110000" and "109900" gets normalized to "113900".

                  <SIDEBAR>
                  A time integer is just a regular integer where the digit positions represent HHMMSS.
                  A regular integer value like "117000" is technically an invalid time integer because the minutes portion
                  is out of range, it must be between 00 and 59, so "117000" is really "121000" -- converting an invalid
                  time integer to a valid value is known as normalizing.
                  </SIDEBAR>

                  Anyways, if you really want to optimize on start time and/or stop times, you need to resolve this issue of
                  normalization of time integers -- or, better yet, you can explicitly try to avoid it.

                  One way to avoid normalization issues is to use 4 parameters instead of just 2.
                  Try setting up 4 parameters this way:

                  StartHour (restrict values 0 to 23)
                  StartMin (restrict values 0 to 59)
                  StopHour (restrict values 0 to 23)
                  StopMin (restrict values 0 to 59)

                  Inside OnStartUp, build your rangeStart/rangeStop variables using the 4 parameter values, like this:

                  rangeStart = (StartHour * 10000) + (StartMin * 100);
                  rangeStop = (StopHour * 10000) + (StopMin * 100);

                  After these changes are complete, then you can use Strategy Analyzer's optimizer to correctly vary
                  your 4 start/stop parameters without having to worry about normalization issues.

                  Make sense?

                  Comment


                    #10
                    Agh yes. that does make sense.

                    My workaround in the post above still listed the invalid time integers in the Optimizer with zero trades and spead up the backtest as it did not run the entire code for the invalid integers, but your solution will take them out entirely which is what I wanted to do. Thanks!

                    The 4 paramaters you suggest you cannot just paste that into the code though correct? I would have to write some logic that would only allow the restricted values correct?

                    Something like

                    if(StartHour <0 || StartHour>23)
                    return;


                    correct?




                    Comment


                      #11
                      Code:
                      private int startHour = 10;
                      private int startMin = 40;
                      
                      
                      [Category("Parameters")]
                      [Gui.Design.DisplayName("Start Hour")]
                      public int StartHour
                      {
                          get { return startHour; }
                          set { startHour = Math.Max(0, Math.Min(23, value)); }
                      }
                      
                      [Category("Parameters")]
                      [Gui.Design.DisplayName("Start Minute")]
                      public int StartMin
                      {
                          get { return startMin; }
                          set { startMin = Math.Max(0, Math.Min(59, value)); }
                      }
                      The other 2 parameters for StopHour/StopMIn should be straightforward.

                      Comment


                        #12
                        OH great. I hadn't thought of that. Im a shade tree mechanic kind of programmer!lol!

                        Comment

                        Latest Posts

                        Collapse

                        Topics Statistics Last Post
                        Started by JoMoon2024, Today, 06:56 AM
                        0 responses
                        6 views
                        0 likes
                        Last Post JoMoon2024  
                        Started by Haiasi, 04-25-2024, 06:53 PM
                        2 responses
                        17 views
                        0 likes
                        Last Post Massinisa  
                        Started by Creamers, Today, 05:32 AM
                        0 responses
                        5 views
                        0 likes
                        Last Post Creamers  
                        Started by Segwin, 05-07-2018, 02:15 PM
                        12 responses
                        1,786 views
                        0 likes
                        Last Post Leafcutter  
                        Started by poplagelu, Today, 05:00 AM
                        0 responses
                        3 views
                        0 likes
                        Last Post poplagelu  
                        Working...
                        X