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

How to Stop Backtest Iteration and Continue with Next

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

    #16
    Originally posted by madb123 View Post
    Is this expected because i am terminating offspring from each generation?
    I don't think so. Once I got things worked out with this, I didn't have anymore crashes.

    is there a way to terminate/ take out useless iterations from a genetic optimisation run?
    One of the easiest things you could do is set a flag in the Configure or DataLoaded state and then at the start of OnBarUpdate do if (flag) { return; }. The iteration will be done very fast even without having to terminate, as ChelseaB mentions above. Saves you a bit of uncertainty about whether terminating might be causing some unforeseen problems.

    Performance Optimization

    I have recently done a lot of work to optimize the performance of my backtest for both regular optimization and genetic.

    Here are my takeaways:
    1. DO use IsInstantiateOnEachOptimization=false. Make sure your strategy properly resets everything in Configure because the strategy object will get reused for multiple runs. This saves a lot of time.
    2. Use a small value for "Keep Best # Results." Mostly this impacts memory, but might slow things down a lot too. Experiment with setting it to 5 and see if that's significantly faster than setting to to 300.
    3. Avoid Session Iterators. If you don't require them, avoid them. Session iterators are quite slow.
    4. Hard code all data series that your strategy might need and never change that for your strategy. For a long time I didn't understand what they meant by "dynamic" data series. But the NT caching can freak out in very unpredictable ways. Even an if statement related to adding data series can screw things up. If you hard code the data series, even if you add several, the performance is quite good as all the caching works seamlessly.
    5. Avoid custom PerformanceMetrics - I don't know why, but they really slow things down. Note that custom OptimizationFitnesses do NOT. They are fine. Maybe I was doing something wrong, certainly possible.
    6. My experience says that executing in a Chart is slower than executing in Strategy Analyzer. I think there's some extra overhead in a Chart... which makes sense. So don't assume chart performance is reflective of SA performance.
    7. Backtest speed is proportional to number of bars. So be consistent in comparisons with the amount of data and number of data series you are using.
    Here are things that do NOT seem to affect performance for me:
    1. IncludeTradeHistoryInBacktest - Documentation says setting this to false will improve performance. Perhaps this again just affects memory and I have plenty on my machine. Backtest speed seems just as good.
    Troubleshooting:
    • Code a stopwatch in your strategy. Start it in Configure and end it on the last historical bar. Print the time.
    • Comment out all of your code logic and validate that everything runs super fast.
    • Add logic back in one piece at a time until you discover what's crashing or adding a lot of extra time.



    Comment


      #17
      Hello madb123,

      I would recommend calling return at the top of OnBarUpdate() instead of calling SetState().
      Chelsea B.NinjaTrader Customer Service

      Comment


        #18
        Originally posted by BarzTrading View Post

        One of the easiest things you could do is set a flag in the Configure or DataLoaded state and then at the start of OnBarUpdate do if (flag) { return; }. The iteration will be done very fast even without having to terminate, as ChelseaB mentions above. Saves you a bit of uncertainty about whether terminating might be causing some unforeseen problems.
        i somehow missed the importance of that point from Chelsea.
        i did this, and can confirm no more crashes and faster. Works fine with genetic optimisation too.

        one thing to note is, with IsInstantiatedOnEachOptimizationIteration = false; i had to set my boolean back to false before calling retrun.

        if(itterationTerminate){itterationTerminate =false; return;}

        thanks

        Comment


          #19
          one thing to note is, with IsInstantiatedOnEachOptimizationIteration = false; i had to set my boolean back to false before calling retrun.

          if(itterationTerminate){itterationTerminate =false; return;}​
          If I understand correctly what you're saying, it would probably be more standard to set iterationTerminate=false at the start of the Configure state. Then you'll start every iteration with a value of false. And your logic can determine in Configure or DataLoaded if you need to set it to true.

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by helpwanted, Today, 03:06 AM
          1 response
          7 views
          0 likes
          Last Post sarafuenonly123  
          Started by Brevo, Today, 01:45 AM
          0 responses
          7 views
          0 likes
          Last Post Brevo
          by Brevo
           
          Started by aussugardefender, Today, 01:07 AM
          0 responses
          5 views
          0 likes
          Last Post aussugardefender  
          Started by pvincent, 06-23-2022, 12:53 PM
          14 responses
          242 views
          0 likes
          Last Post Nyman
          by Nyman
           
          Started by TraderG23, 12-08-2023, 07:56 AM
          9 responses
          385 views
          1 like
          Last Post Gavini
          by Gavini
           
          Working...
          X