pybacktest library hello world error: builtins.AttributeError: 'Series' object has no attribute 'ix'
pybacktest library hello world error: builtins.AttributeError: 'Series' object has no attribute 'ix'
运行以下 pybacktest 代码:
import matplotlib
import matplotlib.pyplot as plt
import pybacktest
import pandas as pd
short_ma = 50
long_ma = 200
ohlc = pybacktest.load_from_yahoo('AAPL', start=2000)
ohlc.tail()
ms = ohlc.C.rolling(short_ma).mean()
ml = ohlc.C.rolling(long_ma).mean()
buy = cover = (ms > ml) & (ms.shift() < ml.shift()) # ma cross up
sell = short = (ms < ml) & (ms.shift() > ml.shift()) # ma cross down
bt = pybacktest.Backtest(locals(), 'ma_cross')
print(bt.summary())
bt.plot_equity()
我得到标题中的错误,有关详细信息,请参阅此屏幕截图:
有人知道解决这个问题的方法吗?
根据这个线程:
AttributeError: 'DataFrame' object has no attribute 'ix'
我使用了 WingWare 的调用堆栈来备份调用的内容 __getattribute__
我替换了行:
eq = self.equity.ix[subset].cumsum()
(backtest.py 的第 188 行)与:
eq = self.equity.loc[subset].cumsum()
也感谢评论者@GustiAdli 的确认。
该代码也适用于 Jupyter 笔记本,正确的代码应该是:
import matplotlib
import matplotlib.pyplot as plt
import pybacktest
import pandas as pd
short_ma = 50
long_ma = 200
ohlc = pybacktest.load_from_yahoo('AAPL', start=2000)
ohlc.tail()
ms = ohlc.C.rolling(short_ma).mean()
ml = ohlc.C.rolling(long_ma).mean()
buy = cover = (ms > ml) & (ms.shift() < ml.shift()) # ma cross up
sell = short = (ms < ml) & (ms.shift() > ml.shift()) # ma cross down
bt = pybacktest.Backtest(locals(), 'ma_cross')
print(bt.summary())
fig,ax = bt.plot_equity()
plt.show()
正确的输出应该是:
我正在为此更改创建一个新存储库(以及需要完成的其他修复):
https://github.com/enjoysmath/pybacktest
因为原来的似乎是独立的(最近的问题消息来自去年)。
运行以下 pybacktest 代码:
import matplotlib
import matplotlib.pyplot as plt
import pybacktest
import pandas as pd
short_ma = 50
long_ma = 200
ohlc = pybacktest.load_from_yahoo('AAPL', start=2000)
ohlc.tail()
ms = ohlc.C.rolling(short_ma).mean()
ml = ohlc.C.rolling(long_ma).mean()
buy = cover = (ms > ml) & (ms.shift() < ml.shift()) # ma cross up
sell = short = (ms < ml) & (ms.shift() > ml.shift()) # ma cross down
bt = pybacktest.Backtest(locals(), 'ma_cross')
print(bt.summary())
bt.plot_equity()
我得到标题中的错误,有关详细信息,请参阅此屏幕截图:
有人知道解决这个问题的方法吗?
根据这个线程:
AttributeError: 'DataFrame' object has no attribute 'ix'
我使用了 WingWare 的调用堆栈来备份调用的内容 __getattribute__
我替换了行:
eq = self.equity.ix[subset].cumsum()
(backtest.py 的第 188 行)与:
eq = self.equity.loc[subset].cumsum()
也感谢评论者@GustiAdli 的确认。
该代码也适用于 Jupyter 笔记本,正确的代码应该是:
import matplotlib
import matplotlib.pyplot as plt
import pybacktest
import pandas as pd
short_ma = 50
long_ma = 200
ohlc = pybacktest.load_from_yahoo('AAPL', start=2000)
ohlc.tail()
ms = ohlc.C.rolling(short_ma).mean()
ml = ohlc.C.rolling(long_ma).mean()
buy = cover = (ms > ml) & (ms.shift() < ml.shift()) # ma cross up
sell = short = (ms < ml) & (ms.shift() > ml.shift()) # ma cross down
bt = pybacktest.Backtest(locals(), 'ma_cross')
print(bt.summary())
fig,ax = bt.plot_equity()
plt.show()
正确的输出应该是:
我正在为此更改创建一个新存储库(以及需要完成的其他修复):
https://github.com/enjoysmath/pybacktest
因为原来的似乎是独立的(最近的问题消息来自去年)。