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

Optimize with different date ranges or a list of instruments

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

    Optimize with different date ranges or a list of instruments

    Hello.

    I am working with a half dozen different instruments and have a custom strategy and optimizer with has a bunch of parameters that I'm trying to optimize.

    >> Is it possible for me to do one run and vary the date period in a single run? Essentially, I'd like to set Strategy.To and .From just like I set Strategy.OptimizationParameters before calling RunIteration().

    Maybe it's possible but I'm just not doing it in the right place?

    >> Or I'd like to be able to run through a list of instruments for that single date range?

    Right now, I need to change the instrument + date range manually for 6 different instruments * 10 different date ranges with each run taking 20 minutes.

    Thanks in advance for your responses.

    #2
    Hi ntdev,
    To vary the date period in a single run should is possible, but requires coding in NinjaScript.
    You could leave the default "Start Date" and "End Date" as is, but add your own date frame (FROM - TO) and then check in your backtest or/and optimization if you are within that frame.
    To create this date frame is a bit tricky. It is not helpful to create one big int for both of them in the format YYYYMMDD, because there are only 12 months and only up to 31 days. Thus, you would test tons of invalid date combinations, if you used just one "all in" int without logic. Instead, you should create separate ints for
    A. FROM, 1) Year (min (e.g.) 2000 - max 2021), 2) Month (1-12), 3) Date (1-31) and
    B. TO (see above).
    To use 6 separate ints prevent invalid input / date combinations.
    Then, concatenate the three input variables back together in a string so that you get a FROM and a TO variable in the format YYYYMMDD.
    Convert the strings to ints !!!
    Then, during the backtest or optimization, check if you are in an allowed date frame prior to firing off your trades.
    The basic date comparison is shown in the help section.

    protectedoverridevoidOnBarUpdate()
    {
    // Compare the date of the current bar to September 15, 2014
    if(ToDay(Time[0])>20140915)
    {
    // Do something
    }
    }

    Just replace the orange YYYYMMDD with your FROM (int) and TO (int) variable respectively, check both and add a final check that FROM (int) is smaller than TO (int).
    Have fun.
    NT-Roland

    Comment


      #3
      Hey Rolland and thanks for the quick reply.

      OK. I understand this approach, basically you let the strategy test for a huge range then just ignore OnBarUpdate() for the date range that you don't want?

      My strategy uses Tick data so I was hoping to have a optimization that didn't require ignoring the callback for every tick in a year.

      I was hoping for something clean like setting Strategy.To/From during State.Configure so that NT only calls me with the dates that I want but I don't think that works.

      Thank you again, at least I have a solution that saves me clicking.

      Comment


        #4
        Hi ntdev,
        Originally posted by ntdev View Post
        ...
        OK. I understand this approach, basically you let the strategy test for a huge range then just ignore OnBarUpdate() for the date range that you don't want?
        ...
        I was hoping for something clean like setting Strategy.To/From during State.Configure so that NT only calls me with the dates that I want but I don't think that works.
        ...
        To my understanding (happy to be corrected here, always eager to learn), the regular date input can not be iterated through (same as the regular time input).
        If you add the below to your strat and then click optimize, you will not be able to specify a range, only one value at a time.

        [NinjaScriptProperty]
        [PropertyEditor("NinjaTrader.Gui.Tools.ChartAnchorT imeEditor")]
        [Display(Name="MyDateTime")]
        public DateTime MyDateTime { get; set; }

        By contrast, what I suggested would allow you to iterate in an optimization.
        BTW: Even if you create an empty strategy with NinjaScript, the Start Date and End Date are added to the strategy. I did not yet have the need to take a deeper dive why this is the case and if these two fields (which don't even show up in the code) can perhaps be eliminated. If they can, great, replace them with what I had suggested. If not, start from what is generated by default and add the date control functionality (see my earlier post above).
        I did not ask myself why you are trying to do what you are trying to achieve, I just provided a suggestion how it can be done.
        NT-Roland

        Comment


          #5
          If you're curious about the 'why' ...

          My strategy has 15+ parameters where 1 parameter set can work very well for one 2-week period but not for another. I'm trying to find the optimal set that works for multiple periods or I might end up with an "auto-tuning" feature that learns how interpret the current field. I am testing groups of 2-week periods to study the local best result and general best results.

          The number of combinations is in the trillions so I can't test everything with any h/w that I have access too.

          You have been so helpful -- THANK YOU!

          You have even helped me think outside the box of other possible hacks to do whatever I want. For example, I could run the big range then go back and construct my own SystemPerformance objects with the filtered trades from a specific range and I could even stuff whatever I wanted into Optimizer.Results so that I can see it in NT which isn't necessary for me because I wrote another piece of software to analyze, visualize and compare all the results graphically.

          HAVE A GREAT REST OF THE WEEKEND!

          Thanks again.

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by mjairg, 07-20-2023, 11:57 PM
          3 responses
          213 views
          1 like
          Last Post PaulMohn  
          Started by TheWhiteDragon, 01-21-2019, 12:44 PM
          4 responses
          544 views
          0 likes
          Last Post PaulMohn  
          Started by GLFX005, Today, 03:23 AM
          0 responses
          3 views
          0 likes
          Last Post GLFX005
          by GLFX005
           
          Started by XXtrader, Yesterday, 11:30 PM
          2 responses
          12 views
          0 likes
          Last Post XXtrader  
          Started by Waxavi, Today, 02:10 AM
          0 responses
          7 views
          0 likes
          Last Post Waxavi
          by Waxavi
           
          Working...
          X