-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
-
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)
Beta Was this translation helpful? Give feedback.
All reactions
Replies: 1 comment 1 reply
-
I don't have any problems, and the code is functional. What output do you see?
image
Beta Was this translation helpful? Give feedback.
All reactions
-
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
Beta Was this translation helpful? Give feedback.