Strategy Optimizer Result Print Order
Results from the Strategy optimizer are printed in the random order, probably when one finishes. I am using the code from the doucmentation.
strats = cerebro.optstrategy(TestStrategy, maperiod=range(10, 30))
2019-01-16, (MA period 12) Ending Value 40111.92 2019-01-16, (MA period 17) Ending Value 47022.09 2019-01-16, (MA period 14) Ending Value 43011.94 2019-01-16, (MA period 10) Ending Value 41291.69 2019-01-16, (MA period 16) Ending Value 44299.02 2019-01-16, (MA period 13) Ending Value 41680.17 2019-01-16, (MA period 15) Ending Value 43969.19 2019-01-16, (MA period 11) Ending Value 41603.53 2019-01-16, (MA period 18) Ending Value 43675.26 2019-01-16, (MA period 19) Ending Value 42673.76 2019-01-16, (MA period 20) Ending Value 38727.26 2019-01-16, (MA period 21) Ending Value 47905.56 2019-01-16, (MA period 23) Ending Value 64044.14 2019-01-16, (MA period 22) Ending Value 59993.77
Results come in random order with other data as well. I am ordering the print results myself, but probably there is something I am missing.
I would appreciate if you help. Thank you for your time.
Hah sorry I wasnt clear. I would like to see them in order, so that I can see how the strategy performance progresses as the parameter changes. In the documentation examples they are printed in order, therefore I concluded something have to be off in my end. How can I print them in order?
The order is random because the because the optimization is done with a pool of processes and you cannot guarantee order execution. If you could, parallelism wouldn't be guaranteed in all general
multiprocessingcases (you may of course argue that your strategies are all independent, but backtrader simply uses the
Poolfunctionality from the
The ordering effect is achieved by limiting the number of cores to use with the parameter
maxcpus=1, but you lose parallelism.
On the other hand, you have a case with a single parameter, but with multiple parameters it may not be obvious what the ordering key would have to be. Furthermore, some people could argue that results would have to ordered according to the returns produced or the
SharpeRatioor something else.
The results you receive include the strategies and these have a copy of the parameters
paramswith the actual values of the execution. You can use the
strategy.params.maperiodkey to order the results.