How to pass custom parameters from strategy code into optimization fitness code in NinjaTrader

Hello guys! We are back on track with some hot stuff!) So simple example straight into the business. Imagine you need to optimize your strategy and you want to use profit factor as the optimization fitness parameter but some results return very good profit factor but unfortunately too few trades to take these results seriously statistically and you want to exclude them completely.

Well, you might say that we can hard code a constant of 30 trades into the optimization fitness code and you will be absolutely right however imagine another situation. You might be running an optimization on a 10 year period and on a 1 month period and you might want to have different settings for the minimum trades parameter so the best solution would be to be able to pass this parameter to the optimization fitness code from the strategy code.

Easy done!

Download files

In the example provided we are searching for the best profit factor but if the minimum trades the strategy made is less than the minimum trades number we passed from the strategy as a setting we simply return zero as the performance value. 

You need to put the file into C:\Users\User\Documents\NinjaTrader 8\bin\Custom\OptimizationFitnesses and recompile NinjaTrader assembly using the button in the editor after which the option “PF with TradeLimits” will come up as one of the available optimization fitness algorithms in the strategy analyzer optimization section.

Another cool trick, although a bit more advanced is to actually optimize this value which is also possible because the parameter will come up in the usual set of fields in the strategy analyzer and will be available for optimization.

Finally, since we use the parameter name to search for the minimum trades parameter inside the strategy you do need their names to match! I am sure you will figure this out. Take care and don’t forget to subscribe!