example not working
gigikentlight opened this issue · 7 comments
Hi, your example:
does not seem to work (in a windows 11 + Anaconda distri), running:
dd, cagr, sharpe = backtest([benchmark],
BuyAndHold,
start=start,
end=end,
plot=True)
gives:
AttributeError Traceback (most recent call last)
Cell In[3], line 13
10 start = 1004
11 end = prices.shape[0] - 1
---> 13 dd, cagr, sharpe = backtest([benchmark],
14 BuyAndHold,
15 start=start,
16 end=end,
17 plot=True)
Cell In[2], line 68, in backtest(datas, strategy, start, end, plot, **kwargs)
66 results = cerebro.run(stdstats=False)
67 if plot:
---> 68 cerebro.plot(iplot=False, start=start, end=end)
69 return (results[0].analyzers.drawdown.get_analysis()['max']['drawdown'],
70 results[0].analyzers.returns.get_analysis()['rnorm100'],
71 results[0].analyzers.sharperatio.get_analysis()['sharperatio'])
File ~\anaconda3\lib\site-packages\backtrader\cerebro.py:989, in Cerebro.plot(self, plotter, numfigs, iplot, start, end, width, height, dpi, tight, use, **kwargs)
987 for stratlist in self.runstrats:
988 for si, strat in enumerate(stratlist):
--> 989 rfig = plotter.plot(strat, figid=si * 100,
990 numfigs=numfigs, iplot=iplot,
991 start=start, end=end, use=use)
992 # pfillers=pfillers2)
994 figs.append(rfig)
File ~\anaconda3\lib\site-packages\backtrader\plot\plot.py:262, in Plot_OldSync.plot(self, strategy, figid, numfigs, iplot, start, end, **kwargs)
257 # Applying fig.autofmt_xdate if the data axis is the last one
258 # breaks the presentation of the date labels. why?
259 # Applying the manual rotation with setp cures the problem
260 # but the labels from all axis but the last have to be hidden
261 for ax in laxis:
--> 262 self.mpyplot.setp(ax.get_xticklabels(), visible=False)
264 self.mpyplot.setp(lastax.get_xticklabels(), visible=True,
265 rotation=self.pinf.sch.tickrotation)
267 # Things must be tight along the x axis (to fill both ends)
File ~\anaconda3\lib\site-packages\matplotlib\axes_base.py:74, in _axis_method_wrapper.set_name..wrapper(self, *args, **kwargs)
73 def wrapper(self, *args, **kwargs):
---> 74 return get_method(self)(*args, **kwargs)
File ~\anaconda3\lib\site-packages\matplotlib\axis.py:1451, in Axis.get_ticklabels(self, minor, which)
1449 if minor:
1450 return self.get_minorticklabels()
-> 1451 return self.get_majorticklabels()
File ~\anaconda3\lib\site-packages\matplotlib\axis.py:1408, in Axis.get_majorticklabels(self)
1406 def get_majorticklabels(self):
1407 """Return this Axis' major tick labels, as a list of ~.text.Text
."""
-> 1408 self._update_ticks()
1409 ticks = self.get_major_ticks()
1410 labels1 = [tick.label1 for tick in ticks if tick.label1.get_visible()]
File ~\anaconda3\lib\site-packages\matplotlib\axis.py:1257, in Axis._update_ticks(self)
1252 def _update_ticks(self):
1253 """
1254 Update ticks (position and labels) using the current data interval of
1255 the axes. Return the list of ticks that will be drawn.
1256 """
-> 1257 major_locs = self.get_majorticklocs()
1258 major_labels = self.major.formatter.format_ticks(major_locs)
1259 major_ticks = self.get_major_ticks(len(major_locs))
File ~\anaconda3\lib\site-packages\matplotlib\axis.py:1479, in Axis.get_majorticklocs(self)
1477 def get_majorticklocs(self):
1478 """Return this Axis' major tick locations in data coordinates."""
-> 1479 return self.major.locator()
File ~\anaconda3\lib\site-packages\matplotlib\dates.py:1378, in AutoDateLocator.call(self)
1375 def call(self):
1376 # docstring inherited
1377 dmin, dmax = self.viewlim_to_dt()
-> 1378 locator = self.get_locator(dmin, dmax)
1379 return locator()
File ~\anaconda3\lib\site-packages\backtrader\plot\locator.py:226, in AutoDateLocator.get_locator(self, dmin, dmax)
222 locator = MicrosecondLocator(interval, tz=self.tz)
224 locator.set_axis(self.axis)
--> 226 locator.set_view_interval(*self.axis.get_view_interval())
227 locator.set_data_interval(*self.axis.get_data_interval())
228 return locator
AttributeError: 'RRuleLocator' object has no attribute 'set_view_interval'
Error in callback <function _draw_all_if_interactive at 0x000001918CA504C0> (for post_execute):
AttributeError Traceback (most recent call last)
File ~\anaconda3\lib\site-packages\matplotlib\pyplot.py:120, in _draw_all_if_interactive()
118 def _draw_all_if_interactive():
119 if matplotlib.is_interactive():
--> 120 draw_all()
File ~\anaconda3\lib\site-packages\matplotlib_pylab_helpers.py:132, in Gcf.draw_all(cls, force)
130 for manager in cls.get_all_fig_managers():
131 if force or manager.canvas.figure.stale:
--> 132 manager.canvas.draw_idle()
File ~\anaconda3\lib\site-packages\matplotlib\backend_bases.py:2078, in FigureCanvasBase.draw_idle(self, *args, **kwargs)
2076 if not self._is_idle_drawing:
2077 with self._idle_draw_cntx():
-> 2078 self.draw(*args, **kwargs)
File ~\anaconda3\lib\site-packages\matplotlib\backends\backend_agg.py:400, in FigureCanvasAgg.draw(self)
396 # Acquire a lock on the shared font cache.
397 with RendererAgg.lock,
398 (self.toolbar._wait_cursor_for_draw_cm() if self.toolbar
399 else nullcontext()):
--> 400 self.figure.draw(self.renderer)
401 # A GUI class may be need to update a window using this draw, so
402 # don't forget to call the superclass.
403 super().draw()
File ~\anaconda3\lib\site-packages\matplotlib\artist.py:95, in _finalize_rasterization..draw_wrapper(artist, renderer, *args, **kwargs)
93 @wraps(draw)
94 def draw_wrapper(artist, renderer, *args, **kwargs):
---> 95 result = draw(artist, renderer, *args, **kwargs)
96 if renderer._rasterizing:
97 renderer.stop_rasterizing()
File ~\anaconda3\lib\site-packages\matplotlib\artist.py:72, in allow_rasterization..draw_wrapper(artist, renderer)
69 if artist.get_agg_filter() is not None:
70 renderer.start_filter()
---> 72 return draw(artist, renderer)
73 finally:
74 if artist.get_agg_filter() is not None:
File ~\anaconda3\lib\site-packages\matplotlib\figure.py:3125, in Figure.draw(self, renderer)
3122 # ValueError can occur when resizing a window.
3124 self.patch.draw(renderer)
-> 3125 mimage._draw_list_compositing_images(
3126 renderer, self, artists, self.suppressComposite)
3128 for sfig in self.subfigs:
3129 sfig.draw(renderer)
File ~\anaconda3\lib\site-packages\matplotlib\image.py:131, in _draw_list_compositing_images(renderer, parent, artists, suppress_composite)
129 if not_composite or not has_images:
130 for a in artists:
--> 131 a.draw(renderer)
132 else:
133 # Composite any adjacent images together
134 image_group = []
File ~\anaconda3\lib\site-packages\matplotlib\artist.py:72, in allow_rasterization..draw_wrapper(artist, renderer)
69 if artist.get_agg_filter() is not None:
70 renderer.start_filter()
---> 72 return draw(artist, renderer)
73 finally:
74 if artist.get_agg_filter() is not None:
File ~\anaconda3\lib\site-packages\matplotlib\axes_base.py:3066, in _AxesBase.draw(self, renderer)
3063 if artists_rasterized:
3064 _draw_rasterized(self.figure, artists_rasterized, renderer)
-> 3066 mimage._draw_list_compositing_images(
3067 renderer, self, artists, self.figure.suppressComposite)
3069 renderer.close_group('axes')
3070 self.stale = False
File ~\anaconda3\lib\site-packages\matplotlib\image.py:131, in _draw_list_compositing_images(renderer, parent, artists, suppress_composite)
129 if not_composite or not has_images:
130 for a in artists:
--> 131 a.draw(renderer)
132 else:
133 # Composite any adjacent images together
134 image_group = []
File ~\anaconda3\lib\site-packages\matplotlib\artist.py:72, in allow_rasterization..draw_wrapper(artist, renderer)
69 if artist.get_agg_filter() is not None:
70 renderer.start_filter()
---> 72 return draw(artist, renderer)
73 finally:
74 if artist.get_agg_filter() is not None:
File ~\anaconda3\lib\site-packages\matplotlib\axis.py:1371, in Axis.draw(self, renderer, *args, **kwargs)
1368 return
1369 renderer.open_group(name, gid=self.get_gid())
-> 1371 ticks_to_draw = self._update_ticks()
1372 tlb1, tlb2 = self._get_ticklabel_bboxes(ticks_to_draw, renderer)
1374 for tick in ticks_to_draw:
File ~\anaconda3\lib\site-packages\matplotlib\axis.py:1257, in Axis._update_ticks(self)
1252 def _update_ticks(self):
1253 """
1254 Update ticks (position and labels) using the current data interval of
1255 the axes. Return the list of ticks that will be drawn.
1256 """
-> 1257 major_locs = self.get_majorticklocs()
1258 major_labels = self.major.formatter.format_ticks(major_locs)
1259 major_ticks = self.get_major_ticks(len(major_locs))
File ~\anaconda3\lib\site-packages\matplotlib\axis.py:1479, in Axis.get_majorticklocs(self)
1477 def get_majorticklocs(self):
1478 """Return this Axis' major tick locations in data coordinates."""
-> 1479 return self.major.locator()
File ~\anaconda3\lib\site-packages\matplotlib\dates.py:1378, in AutoDateLocator.call(self)
1375 def call(self):
1376 # docstring inherited
1377 dmin, dmax = self.viewlim_to_dt()
-> 1378 locator = self.get_locator(dmin, dmax)
1379 return locator()
File ~\anaconda3\lib\site-packages\backtrader\plot\locator.py:226, in AutoDateLocator.get_locator(self, dmin, dmax)
222 locator = MicrosecondLocator(interval, tz=self.tz)
224 locator.set_axis(self.axis)
--> 226 locator.set_view_interval(*self.axis.get_view_interval())
227 locator.set_data_interval(*self.axis.get_data_interval())
228 return locator
AttributeError: 'RRuleLocator' object has no attribute 'set_view_interval'
AttributeError Traceback (most recent call last)
File ~\anaconda3\lib\site-packages\IPython\core\formatters.py:340, in BaseFormatter.call(self, obj)
338 pass
339 else:
--> 340 return printer(obj)
341 # Finally look for special method names
342 method = get_real_method(obj, self.print_method)
File ~\anaconda3\lib\site-packages\IPython\core\pylabtools.py:152, in print_figure(fig, fmt, bbox_inches, base64, **kwargs)
149 from matplotlib.backend_bases import FigureCanvasBase
150 FigureCanvasBase(fig)
--> 152 fig.canvas.print_figure(bytes_io, **kw)
153 data = bytes_io.getvalue()
154 if fmt == 'svg':
File ~\anaconda3\lib\site-packages\matplotlib\backend_bases.py:2338, in FigureCanvasBase.print_figure(self, filename, dpi, facecolor, edgecolor, orientation, format, bbox_inches, pad_inches, bbox_extra_artists, backend, **kwargs)
2332 renderer = _get_renderer(
2333 self.figure,
2334 functools.partial(
2335 print_method, orientation=orientation)
2336 )
2337 with getattr(renderer, "_draw_disabled", nullcontext)():
-> 2338 self.figure.draw(renderer)
2340 if bbox_inches:
2341 if bbox_inches == "tight":
File ~\anaconda3\lib\site-packages\matplotlib\artist.py:95, in _finalize_rasterization..draw_wrapper(artist, renderer, *args, **kwargs)
93 @wraps(draw)
94 def draw_wrapper(artist, renderer, *args, **kwargs):
---> 95 result = draw(artist, renderer, *args, **kwargs)
96 if renderer._rasterizing:
97 renderer.stop_rasterizing()
File ~\anaconda3\lib\site-packages\matplotlib\artist.py:72, in allow_rasterization..draw_wrapper(artist, renderer)
69 if artist.get_agg_filter() is not None:
70 renderer.start_filter()
---> 72 return draw(artist, renderer)
73 finally:
74 if artist.get_agg_filter() is not None:
File ~\anaconda3\lib\site-packages\matplotlib\figure.py:3125, in Figure.draw(self, renderer)
3122 # ValueError can occur when resizing a window.
3124 self.patch.draw(renderer)
-> 3125 mimage._draw_list_compositing_images(
3126 renderer, self, artists, self.suppressComposite)
3128 for sfig in self.subfigs:
3129 sfig.draw(renderer)
File ~\anaconda3\lib\site-packages\matplotlib\image.py:131, in _draw_list_compositing_images(renderer, parent, artists, suppress_composite)
129 if not_composite or not has_images:
130 for a in artists:
--> 131 a.draw(renderer)
132 else:
133 # Composite any adjacent images together
134 image_group = []
File ~\anaconda3\lib\site-packages\matplotlib\artist.py:72, in allow_rasterization..draw_wrapper(artist, renderer)
69 if artist.get_agg_filter() is not None:
70 renderer.start_filter()
---> 72 return draw(artist, renderer)
73 finally:
74 if artist.get_agg_filter() is not None:
File ~\anaconda3\lib\site-packages\matplotlib\axes_base.py:3066, in _AxesBase.draw(self, renderer)
3063 if artists_rasterized:
3064 _draw_rasterized(self.figure, artists_rasterized, renderer)
-> 3066 mimage._draw_list_compositing_images(
3067 renderer, self, artists, self.figure.suppressComposite)
3069 renderer.close_group('axes')
3070 self.stale = False
File ~\anaconda3\lib\site-packages\matplotlib\image.py:131, in _draw_list_compositing_images(renderer, parent, artists, suppress_composite)
129 if not_composite or not has_images:
130 for a in artists:
--> 131 a.draw(renderer)
132 else:
133 # Composite any adjacent images together
134 image_group = []
File ~\anaconda3\lib\site-packages\matplotlib\artist.py:72, in allow_rasterization..draw_wrapper(artist, renderer)
69 if artist.get_agg_filter() is not None:
70 renderer.start_filter()
---> 72 return draw(artist, renderer)
73 finally:
74 if artist.get_agg_filter() is not None:
File ~\anaconda3\lib\site-packages\matplotlib\axis.py:1371, in Axis.draw(self, renderer, *args, **kwargs)
1368 return
1369 renderer.open_group(name, gid=self.get_gid())
-> 1371 ticks_to_draw = self._update_ticks()
1372 tlb1, tlb2 = self._get_ticklabel_bboxes(ticks_to_draw, renderer)
1374 for tick in ticks_to_draw:
File ~\anaconda3\lib\site-packages\matplotlib\axis.py:1257, in Axis._update_ticks(self)
1252 def _update_ticks(self):
1253 """
1254 Update ticks (position and labels) using the current data interval of
1255 the axes. Return the list of ticks that will be drawn.
1256 """
-> 1257 major_locs = self.get_majorticklocs()
1258 major_labels = self.major.formatter.format_ticks(major_locs)
1259 major_ticks = self.get_major_ticks(len(major_locs))
File ~\anaconda3\lib\site-packages\matplotlib\axis.py:1479, in Axis.get_majorticklocs(self)
1477 def get_majorticklocs(self):
1478 """Return this Axis' major tick locations in data coordinates."""
-> 1479 return self.major.locator()
File ~\anaconda3\lib\site-packages\matplotlib\dates.py:1378, in AutoDateLocator.call(self)
1375 def call(self):
1376 # docstring inherited
1377 dmin, dmax = self.viewlim_to_dt()
-> 1378 locator = self.get_locator(dmin, dmax)
1379 return locator()
File ~\anaconda3\lib\site-packages\backtrader\plot\locator.py:226, in AutoDateLocator.get_locator(self, dmin, dmax)
222 locator = MicrosecondLocator(interval, tz=self.tz)
224 locator.set_axis(self.axis)
--> 226 locator.set_view_interval(*self.axis.get_view_interval())
227 locator.set_data_interval(*self.axis.get_data_interval())
228 return locator
AttributeError: 'RRuleLocator' object has no attribute 'set_view_interval'
Anaconda is (one of) the most popular python package for windows, so not sure ... what do you recommend as an env. for your stuff? thanks
Hi @gigikentlight, backtrader is an abandoned project, it doesn't work with the latest versions of python, matplotlib, numpy and other libraries.
exactly, but it's being used by riskfolio, or is there a replacement to it?
It's not used by Riskfolio-Lib (it is not in requirements.txt), it's just an old example.
roger - so, every other example you have under:
https://nbviewer.org/github/dcajasn/Riskfolio-Lib
are working with up to date libraries ... or is there a post I can read about "risks" & other packages in similar position to backtrader that are being referenced by riskfolio?
thanks
If you have questions, the cost is $25 USD per question that doesn't require write code. See fees section in Readme.md
can you convince me this is better than matlab ... then we can talk about projects.