Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

TypeError: other must be a MultiIndex or a list of tuples #1188

lfferraz started this conversation in General
Discussion options

I have this problem when I try to run the below code, I am using the Apple M1 and a python version 3.9.13, because compatiblity whith pandas and numpy. Anybody now how to solve this problem?

from backtesting import Backtest, Strategy
from backtesting.lib import crossover

from backtesting.test import SMA, GOOG

import datetime
import yfinance as yf

ticker = "GOOG"

start = datetime.datetime(2021,1,1)
end = datetime.datetime(2023,12,31)

ticker_ohlc = yf.download(ticker,start,end)

class SmaCross(Strategy):
n1=10
n2=70

def init(self):
 price = self.data.Close
 self.ma1 = self.I(SMA, price, self.n1)
 self.ma2 = self.I(SMA, price, self.n2)
def next(self):
 if crossover(self.ma1, self.ma2):
 self.buy()
 elif crossover(self.ma2, self.ma1):
 self.sell()

bt = Backtest(ticker_ohlc, SmaCross, cash=10000, commission=.002, exclusive_orders=True)
stats = bt.run()

print(stats)
print(stats['_trades'].tail(20))
print(stats['_equity_curve'].tail(20))

#bt.plot()

result_optimize, heatmap = bt.optimize(n1=range(5, 70, 5),
n2=range(10, 100, 5),
maximize='Equity Final [$]',
constraint=lambda param: param.n1 < param.n2,
return_heatmap=True)

#print(result_optimize)
#print(result_optimize['_strategy'])
print(heatmap)

hm = heatmap.groupby(['n1', 'n2']).mean().unstack()

print(hm)

You must be logged in to vote

Replies: 1 comment 1 reply

Comment options

I don't have any problems, and the code is functional. What output do you see?
image

You must be logged in to vote
1 reply
Comment options

Hi @lfferraz, I was having the same issue. Downgrading yfinance to v0.2.44 worked for me. Maybe this isn't the best solution, but it's a quick one. Hope it helps

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet

AltStyle によって変換されたページ (->オリジナル) /