Daniel Donnelly
Daniel Donnelly

Reputation: 610

pybacktest library hello world error: builtins.AttributeError: 'Series' object has no attribute 'ix'

Runnign the following pybacktest code:

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()

I get the error in the title, and for more info see this screenshot:

pybacktest hello world error

Does anyone know of a way to fix this?

Upvotes: 0

Views: 1310

Answers (1)

Daniel Donnelly
Daniel Donnelly

Reputation: 610

Accordingly with this thread:

AttributeError: 'DataFrame' object has no attribute 'ix'

I used the Call Stack of WingWare to back up to what was calling __getattribute__

and I replaced the line:

    eq = self.equity.ix[subset].cumsum()

(line 188 of backtest.py) with:

    eq = self.equity.loc[subset].cumsum()

Thanks to commenter @GustiAdli as well for confirming.

That code also was meant for a Jupyter notebook and the proper code should be:

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()

And the proper output should be:

matplotlib output

I am making a new repository for this change (and other fixes that need to be done):

https://github.com/enjoysmath/pybacktest

Since the original seems left alone (most recent issue message was from last year).

Upvotes: 1

Related Questions