Plotting with Optimizer not working
-
Hi,
I am having trouble with getting plotting to work:
I have tried:
print('run begin') optimized_runs = cerebro.run(maxcpus=1) print('runs completed: ' + str(len(optimized_runs))) optimized_runs[0][0].plot()
with error
return getattr(self.lines, name) AttributeError: 'Lines_LineSeries_LineIterator_DataAccessor_Strateg' object has no attribute 'plot'
also:
print('run begin') optimized_runs = cerebro.run(maxcpus=1) print('runs completed: ' + str(len(optimized_runs))) #optimized_runs[0][0].plot() for optimized_run in optimized_runs: optimized_run.plot()
with error:
AttributeError: 'list' object has no attribute 'plot'
and
print('run begin') optimized_runs = cerebro.run(maxcpus=1) print('runs completed: ' + str(len(optimized_runs))) for optimized_run in optimized_runs: for strategy in optimized_run: strategy.plot()
with error
AttributeError: 'Lines_LineSeries_LineIterator_DataAccessor_Strateg' object has no attribute 'plot'
Any help would be greatly appreciated!
Thank you
-
Not sure plotting was designed to work with
optstrategy
: https://community.backtrader.com/topic/409/optstrategy-and-plotting -
I've just taken a look at backtrader yesterday, I believe you need to take the strategy again into a new cerbro instance and plot that.
For example:
...
results = cerebro.optstrategy(SomeStrategy, ...)
results_ordered = [] for run in results: for strategy in run: print('--> Params', strategy.params) results_ordered.append(strategy) results_ordered = sorted(results_ordered, key=lambda x: x.broker.get_value(), reverse=True) for strategy in results_ordered: print('--> Strategy {} {}', strategy.broker.get_value(), strategy.params.__dict__) cerebro = bt.Cerebro() cerebro.addstrategy(SomeStrategy, **results_ordered[0].params.__dict__) cerebro.adddata(data) cerebro.run() cerebro.plot()
-
So this is wrong, not sure why the first strategy result in run is a new instance of MyStrategy, with the defaults, but it is.
results_ordered = [] for run in results: results_ordered.append(run[1]) results_ordered = sorted(results_ordered, key=lambda x: x.broker.get_value(), reverse=True) for result in results_ordered[:10]: print('--> Result', result.broker.get_value(), result.params.__dict__) print('--> Winning strategy %s %s' % (results_ordered[0].broker.get_value(), results_ordered[0].params.__dict__))
-
@Kevin-Parker Thanks for the tip - please let me know if you work it out
-
@dracount I did, see the code example.