vectorbt
vectorbt copied to clipboard
Unable to plot the SMA with custom stock data
Hi Vectorbt team,
I am trying to plot the SMA for a stock(ETH-BTC) instead of data getting fetched from Yahoo finance as mentioned in the repo.
Existing code in github: symbols = ["BTC-USD", "ETH-USD", "LTC-USD"] price = vbt.YFData.download(symbols, missing_index='drop').get('Close')
windows = np.arange(2, 101) fast_ma, slow_ma = vbt.MA.run_combs(price, window=windows, r=2, short_names=['fast', 'slow']) entries = fast_ma.ma_crossed_above(slow_ma) exits = fast_ma.ma_crossed_below(slow_ma)
pf_kwargs = dict(size=np.inf, fees=0.001, freq='1D') pf = vbt.Portfolio.from_signals(price, entries, exits, **pf_kwargs)
fig = pf.total_return().vbt.heatmap( x_level='fast_window', y_level='slow_window', symmetric=True, trace_kwargs=dict(colorbar=dict(title='Total return', tickformat='%'))) fig.show()
pf[(10, 20, 'BTC-USD')].plot().show()
Customised code: df = pd.read_csv("C:/Users/Shan/Downloads/BTC-USD.csv", index_col="Date", parse_dates=True) price = df.vbt.ohlcv.get_column('close')
windows = np.arange(2, 101) fast_ma, slow_ma = vbt.MA.run_combs(price, window=windows, r=2, short_names=['fast', 'slow']) entries = fast_ma.ma_crossed_above(slow_ma) exits = fast_ma.ma_crossed_below(slow_ma)
pf_kwargs = dict(size=np.inf, fees=0.001, freq='1D') pf = vbt.Portfolio.from_signals(price, entries, exits, **pf_kwargs)
fig = pf.total_return().vbt.heatmap( x_level='fast_window', y_level='slow_window', symmetric=True, trace_kwargs=dict(colorbar=dict(title='Total return', tickformat='%'))) fig.show()
pf[(10, 20, 'BTC-USD')].plot().show()
Issue: when i tried to plot for single data such as ETH-USD or BTC-USD , i am not able to plot the data. facing below error:
Traceback (most recent call last): File "C:\Users\Shan\AppData\Roaming\Python\Python39\site-packages\pandas\core\series.py", line 948, in getitem result = self._get_value(key) File "C:\Users\Shan\Roaming\Python\Python39\site-packages\pandas\core\series.py", line 1051, in _get_value loc = self.index.get_loc(label) File "C:\Users\Shan\AppData\Roaming\Python\Python39\site-packages\pandas\core\indexes\multi.py", line 2927, in get_loc raise KeyError( KeyError: 'Key length (3) exceeds index depth (2)'
Any help on this is really appreciated.
Regards, Shan
Try pf[(10, 20)].plot().show()