How to Increase Optimization Speed by 10x in NinjaTrader

The other half of this topic should say "... and not get 1 million of errors printed". NinjaTrader manual is actually very helpful and it says you can, all you have to do is use this setting:

IsInstantiatedOnEachOptimizationIteration = InitiateOnEachRun;

Pretty long word for a setting!!!) Anyways, it basically means that NinjaTrader won't initialize your strategy object on every single optimization run. This can really increase the speed and free up quite a few Gb of your RAM!!!

It seems, NinjaTrader goes into a funky multi thread scenario when doing this and you should see all your core threads getting utilized quite neatly.

However, there are some issue to be very carefully aware of and most of them are mentioned in the manual which is great although I think one very important one is missing which did bring me quite a few headaches.

So what happens is your strategy object will get reused. This means that on the next optimization run all your global variables will get reused. Which means that if you had a stop loss global variable set in a previous strategy run to 100USD this value will get carried over to your next optimization run and if you don't reset this variable at either State.Configured which seems to be initialized during the transitions or at the moment of new position entry you will get in huge trouble!

By the way NinjaTrader manual actually mentions the above however what it doesn't say anything about, or maybe I missed it, is the virtual positions!!! So, imagine this, your strategy opened up a position on the last bar of a particular optimization run. If you don't close this position it will get carried away to the next optimization run and you will get loads of strange errors that you never seen before printed in the log.

Yes! So what is the solution, or let's call it a "walking stick".

1) Close all your positions a few historical bars before the end of data.

2) Reset all your orders if you need to at that point as well.

3) Make sure no positions are opened after that so make a little handler for that as well.

I hope this information has saved some sleep for someone!) Peace!)