-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
-
I took the example:
from backtesting import Backtest, Strategy from backtesting.lib import crossover from backtesting.test import SMA, GOOG class SmaCross(Strategy): n1 = 10 n2 = 20 def init(self): close = self.data.Close self.sma1 = self.I(SMA, close, self.n1) self.sma2 = self.I(SMA, close, self.n2) def next(self): if crossover(self.sma1, self.sma2): self.buy() elif crossover(self.sma2, self.sma1): self.sell() bt = Backtest(GOOG, SmaCross, cash=10000, commission=.002, exclusive_orders=True) output = bt.run() bt.plot()
and tried to run it using Python 3.12 on a Win 10 machine and it errors out with this message:
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
Cell In[3], [line 28](vscode-notebook-cell:?execution_count=3&line=28)
[23](vscode-notebook-cell:?execution_count=3&line=23) bt = Backtest(GOOG, SmaCross,
[24](vscode-notebook-cell:?execution_count=3&line=24) cash=10000, commission=.002,
[25](vscode-notebook-cell:?execution_count=3&line=25) exclusive_orders=True)
[27](vscode-notebook-cell:?execution_count=3&line=27) output = bt.run()
---> [28](vscode-notebook-cell:?execution_count=3&line=28) bt.plot()
File ~\AppData\Roaming\Python\Python312\site-packages\backtesting\backtesting.py:1592, in Backtest.plot(self, results, filename, plot_width, plot_equity, plot_return, plot_pl, plot_volume, plot_drawdown, smooth_equity, relative_equity, superimpose, resample, reverse_indicators, show_legend, open_browser)
[1589](https://file+.vscode-resource.vscode-cdn.net/c%3A/My/Work/Python/~/AppData/Roaming/Python/Python312/site-packages/backtesting/backtesting.py:1589) raise RuntimeError('First issue `backtest.run()` to obtain results.')
[1590](https://file+.vscode-resource.vscode-cdn.net/c%3A/My/Work/Python/~/AppData/Roaming/Python/Python312/site-packages/backtesting/backtesting.py:1590) results = self._results
-> [1592](https://file+.vscode-resource.vscode-cdn.net/c%3A/My/Work/Python/~/AppData/Roaming/Python/Python312/site-packages/backtesting/backtesting.py:1592) return plot(
[1593](https://file+.vscode-resource.vscode-cdn.net/c%3A/My/Work/Python/~/AppData/Roaming/Python/Python312/site-packages/backtesting/backtesting.py:1593) results=results,
[1594](https://file+.vscode-resource.vscode-cdn.net/c%3A/My/Work/Python/~/AppData/Roaming/Python/Python312/site-packages/backtesting/backtesting.py:1594) df=self._data,
[1595](https://file+.vscode-resource.vscode-cdn.net/c%3A/My/Work/Python/~/AppData/Roaming/Python/Python312/site-packages/backtesting/backtesting.py:1595) indicators=results._strategy._indicators,
[1596](https://file+.vscode-resource.vscode-cdn.net/c%3A/My/Work/Python/~/AppData/Roaming/Python/Python312/site-packages/backtesting/backtesting.py:1596) filename=filename,
[1597](https://file+.vscode-resource.vscode-cdn.net/c%3A/My/Work/Python/~/AppData/Roaming/Python/Python312/site-packages/backtesting/backtesting.py:1597) plot_width=plot_width,
[1598](https://file+.vscode-resource.vscode-cdn.net/c%3A/My/Work/Python/~/AppData/Roaming/Python/Python312/site-packages/backtesting/backtesting.py:1598) plot_equity=plot_equity,
[1599](https://file+.vscode-resource.vscode-cdn.net/c%3A/My/Work/Python/~/AppData/Roaming/Python/Python312/site-packages/backtesting/backtesting.py:1599) plot_return=plot_return,
[1600](https://file+.vscode-resource.vscode-cdn.net/c%3A/My/Work/Python/~/AppData/Roaming/Python/Python312/site-packages/backtesting/backtesting.py:1600) plot_pl=plot_pl,
[1601](https://file+.vscode-resource.vscode-cdn.net/c%3A/My/Work/Python/~/AppData/Roaming/Python/Python312/site-packages/backtesting/backtesting.py:1601) plot_volume=plot_volume,
[1602](https://file+.vscode-resource.vscode-cdn.net/c%3A/My/Work/Python/~/AppData/Roaming/Python/Python312/site-packages/backtesting/backtesting.py:1602) plot_drawdown=plot_drawdown,
[1603](https://file+.vscode-resource.vscode-cdn.net/c%3A/My/Work/Python/~/AppData/Roaming/Python/Python312/site-packages/backtesting/backtesting.py:1603) smooth_equity=smooth_equity,
[1604](https://file+.vscode-resource.vscode-cdn.net/c%3A/My/Work/Python/~/AppData/Roaming/Python/Python312/site-packages/backtesting/backtesting.py:1604) relative_equity=relative_equity,
[1605](https://file+.vscode-resource.vscode-cdn.net/c%3A/My/Work/Python/~/AppData/Roaming/Python/Python312/site-packages/backtesting/backtesting.py:1605) superimpose=superimpose,
[1606](https://file+.vscode-resource.vscode-cdn.net/c%3A/My/Work/Python/~/AppData/Roaming/Python/Python312/site-packages/backtesting/backtesting.py:1606) resample=resample,
[1607](https://file+.vscode-resource.vscode-cdn.net/c%3A/My/Work/Python/~/AppData/Roaming/Python/Python312/site-packages/backtesting/backtesting.py:1607) reverse_indicators=reverse_indicators,
[1608](https://file+.vscode-resource.vscode-cdn.net/c%3A/My/Work/Python/~/AppData/Roaming/Python/Python312/site-packages/backtesting/backtesting.py:1608) show_legend=show_legend,
[1609](https://file+.vscode-resource.vscode-cdn.net/c%3A/My/Work/Python/~/AppData/Roaming/Python/Python312/site-packages/backtesting/backtesting.py:1609) open_browser=open_browser)
File ~\AppData\Roaming\Python\Python312\site-packages\backtesting\_plotting.py:250, in plot(results, df, indicators, filename, plot_width, plot_equity, plot_return, plot_pl, plot_volume, plot_drawdown, smooth_equity, relative_equity, superimpose, resample, reverse_indicators, show_legend, open_browser)
[245](https://file+.vscode-resource.vscode-cdn.net/c%3A/My/Work/Python/~/AppData/Roaming/Python/Python312/site-packages/backtesting/_plotting.py:245) trades_cmap = factor_cmap('returns_positive', colors_darker, ['0', '1'])
[247](https://file+.vscode-resource.vscode-cdn.net/c%3A/My/Work/Python/~/AppData/Roaming/Python/Python312/site-packages/backtesting/_plotting.py:247) if is_datetime_index:
[248](https://file+.vscode-resource.vscode-cdn.net/c%3A/My/Work/Python/~/AppData/Roaming/Python/Python312/site-packages/backtesting/_plotting.py:248) fig_ohlc.xaxis.formatter = CustomJSTickFormatter(
[249](https://file+.vscode-resource.vscode-cdn.net/c%3A/My/Work/Python/~/AppData/Roaming/Python/Python312/site-packages/backtesting/_plotting.py:249) args=dict(axis=fig_ohlc.xaxis[0],
--> [250](https://file+.vscode-resource.vscode-cdn.net/c%3A/My/Work/Python/~/AppData/Roaming/Python/Python312/site-packages/backtesting/_plotting.py:250) formatter=DatetimeTickFormatter(days=['%d %b', '%a %d'],
[251](https://file+.vscode-resource.vscode-cdn.net/c%3A/My/Work/Python/~/AppData/Roaming/Python/Python312/site-packages/backtesting/_plotting.py:251) months=['%m/%Y', "%b'%y"]),
[252](https://file+.vscode-resource.vscode-cdn.net/c%3A/My/Work/Python/~/AppData/Roaming/Python/Python312/site-packages/backtesting/_plotting.py:252) source=source),
[253](https://file+.vscode-resource.vscode-cdn.net/c%3A/My/Work/Python/~/AppData/Roaming/Python/Python312/site-packages/backtesting/_plotting.py:253) code='''
[254](https://file+.vscode-resource.vscode-cdn.net/c%3A/My/Work/Python/~/AppData/Roaming/Python/Python312/site-packages/backtesting/_plotting.py:254) this.labels = this.labels || formatter.doFormat(ticks
[255](https://file+.vscode-resource.vscode-cdn.net/c%3A/My/Work/Python/~/AppData/Roaming/Python/Python312/site-packages/backtesting/_plotting.py:255) .map(i => source.data.datetime[i])
[256](https://file+.vscode-resource.vscode-cdn.net/c%3A/My/Work/Python/~/AppData/Roaming/Python/Python312/site-packages/backtesting/_plotting.py:256) .filter(t => t !== undefined));
[257](https://file+.vscode-resource.vscode-cdn.net/c%3A/My/Work/Python/~/AppData/Roaming/Python/Python312/site-packages/backtesting/_plotting.py:257) return this.labels[index] || "";
[258](https://file+.vscode-resource.vscode-cdn.net/c%3A/My/Work/Python/~/AppData/Roaming/Python/Python312/site-packages/backtesting/_plotting.py:258) ''')
[260](https://file+.vscode-resource.vscode-cdn.net/c%3A/My/Work/Python/~/AppData/Roaming/Python/Python312/site-packages/backtesting/_plotting.py:260) NBSP = '\N{NBSP}' * 4
[261](https://file+.vscode-resource.vscode-cdn.net/c%3A/My/Work/Python/~/AppData/Roaming/Python/Python312/site-packages/backtesting/_plotting.py:261) ohlc_extreme_values = df[['High', 'Low']].copy(deep=False)
File d:\anaconda3\Lib\site-packages\bokeh\models\formatters.py:593, in DatetimeTickFormatter.__init__(self, *args, **kwargs)
[592](file:///D:/anaconda3/Lib/site-packages/bokeh/models/formatters.py:592) def __init__(self, *args, **kwargs) -> None:
--> [593](file:///D:/anaconda3/Lib/site-packages/bokeh/models/formatters.py:593) super().__init__(*args, **kwargs)
File d:\anaconda3\Lib\site-packages\bokeh\models\formatters.py:93, in TickFormatter.__init__(self, *args, **kwargs)
[92](file:///D:/anaconda3/Lib/site-packages/bokeh/models/formatters.py:92) def __init__(self, *args, **kwargs) -> None:
---> [93](file:///D:/anaconda3/Lib/site-packages/bokeh/models/formatters.py:93) super().__init__(*args, **kwargs)
File d:\anaconda3\Lib\site-packages\bokeh\model\model.py:119, in Model.__init__(self, *args, **kwargs)
[116](file:///D:/anaconda3/Lib/site-packages/bokeh/model/model.py:116) if "id" in kwargs:
[117](file:///D:/anaconda3/Lib/site-packages/bokeh/model/model.py:117) raise ValueError("initializing 'id' is not allowed")
--> [119](file:///D:/anaconda3/Lib/site-packages/bokeh/model/model.py:119) super().__init__(**kwargs)
[120](file:///D:/anaconda3/Lib/site-packages/bokeh/model/model.py:120) default_theme.apply_to_model(self)
File d:\anaconda3\Lib\site-packages\bokeh\core\has_props.py:304, in HasProps.__init__(self, **properties)
[302](file:///D:/anaconda3/Lib/site-packages/bokeh/core/has_props.py:302) if value is Undefined or value is Intrinsic:
[303](file:///D:/anaconda3/Lib/site-packages/bokeh/core/has_props.py:303) continue
--> [304](file:///D:/anaconda3/Lib/site-packages/bokeh/core/has_props.py:304) setattr(self, name, value)
[306](file:///D:/anaconda3/Lib/site-packages/bokeh/core/has_props.py:306) initialized = set(properties.keys())
[307](file:///D:/anaconda3/Lib/site-packages/bokeh/core/has_props.py:307) for name in self.properties(_with_props=True): # avoid set[] for deterministic behavior
File d:\anaconda3\Lib\site-packages\bokeh\core\has_props.py:336, in HasProps.__setattr__(self, name, value)
[334](file:///D:/anaconda3/Lib/site-packages/bokeh/core/has_props.py:334) properties = self.properties(_with_props=True)
[335](file:///D:/anaconda3/Lib/site-packages/bokeh/core/has_props.py:335) if name in properties:
--> [336](file:///D:/anaconda3/Lib/site-packages/bokeh/core/has_props.py:336) return super().__setattr__(name, value)
[338](file:///D:/anaconda3/Lib/site-packages/bokeh/core/has_props.py:338) descriptor = getattr(self.__class__, name, None)
[339](file:///D:/anaconda3/Lib/site-packages/bokeh/core/has_props.py:339) if isinstance(descriptor, property): # Python property
File d:\anaconda3\Lib\site-packages\bokeh\core\property\descriptors.py:330, in PropertyDescriptor.__set__(self, obj, value, setter)
[327](file:///D:/anaconda3/Lib/site-packages/bokeh/core/property/descriptors.py:327) class_name = obj.__class__.__name__
[328](file:///D:/anaconda3/Lib/site-packages/bokeh/core/property/descriptors.py:328) raise RuntimeError(f"{class_name}.{self.name} is a readonly property")
--> [330](file:///D:/anaconda3/Lib/site-packages/bokeh/core/property/descriptors.py:330) value = self.property.prepare_value(obj, self.name, value)
[331](file:///D:/anaconda3/Lib/site-packages/bokeh/core/property/descriptors.py:331) old = self._get(obj)
[332](file:///D:/anaconda3/Lib/site-packages/bokeh/core/property/descriptors.py:332) self._set(obj, old, value, setter=setter)
File d:\anaconda3\Lib\site-packages\bokeh\core\property\bases.py:363, in Property.prepare_value(self, owner, name, value, hint)
[361](file:///D:/anaconda3/Lib/site-packages/bokeh/core/property/bases.py:361) else:
[362](file:///D:/anaconda3/Lib/site-packages/bokeh/core/property/bases.py:362) obj_repr = owner if isinstance(owner, HasProps) else owner.__name__
--> [363](file:///D:/anaconda3/Lib/site-packages/bokeh/core/property/bases.py:363) raise ValueError(f"failed to validate {obj_repr}.{name}: {error}")
[365](file:///D:/anaconda3/Lib/site-packages/bokeh/core/property/bases.py:365) if isinstance(owner, HasProps):
[366](file:///D:/anaconda3/Lib/site-packages/bokeh/core/property/bases.py:366) obj = owner
ValueError: failed to validate DatetimeTickFormatter(id='p1138', ...).days: expected a value of type str, got ['%d %b', '%a %d'] of type list
Beta Was this translation helpful? Give feedback.
All reactions
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment