SMA Cross Code Error
-
I am beating my head against the wall trying to debug this script but haven't made any progress.
import datetime
import backtrader as btclass SmaCross(bt.SignalStrategy):
def init(self):
sma = bt.ind.SMA(period=50)
price = self.data
crossover = bt.ind.CrossOver(price,sma)
self.signal_add(bt.SIGNAL_LONG,crossover)
cerebro = bt.Cerebro()
cerebro.addstrategy(SmaCross)data = data = bt.feeds.YahooFinanceData(
dataname = "BTC-USD",
fromdate = datetime.datetime(2015,1,1), # Do not pass values before this date
todate = datetime.datetime(2020,1,1), # Do not pass values after this date
reverse=False)cerebro.adddata(data)
cerebro.run()
cerebro.plot()Here is the error code I am getting
runfile('/Users//Desktop/practice/trader.py', wdir='/Users//Desktop/practice')
Traceback (most recent call last):File "/Users//Desktop/practice/trader.py", line 11, in <module>
cerebro = bt.Cerebro()File "/Users//opt/anaconda3/lib/python3.9/site-packages/backtrader/metabase.py", line 88, in call
_obj, args, kwargs = cls.doinit(_obj, *args, **kwargs)File "/Users//opt/anaconda3/lib/python3.9/site-packages/backtrader/metabase.py", line 78, in doinit
_obj.init(*args, **kwargs)File "/Users//opt/anaconda3/lib/python3.9/site-packages/backtrader/cerebro.py", line 315, in init
self._broker = BackBroker()File "/Users//opt/anaconda3/lib/python3.9/site-packages/backtrader/metabase.py", line 86, in call
_obj, args, kwargs = cls.donew(*args, **kwargs)File "/Users//opt/anaconda3/lib/python3.9/site-packages/backtrader/metabase.py", line 283, in donew
params = cls.params()File "/Users//opt/anaconda3/lib/python3.9/site-packages/backtrader/metabase.py", line 193, in new
obj = super(AutoInfoClass, cls).new(cls, *args, **kwargs)TypeError: super(type, obj): obj must be an instance or subtype of type
-
Just a few things I would change in your code. Try them out and reply if you get the same error still.
- I would include an input data source for your technical indicators, for instance:
sma = bt.ind.SMA(self.data, period=50)
- You define your price as self.data, but self.data is an object, not a value. You need to specify whether you want a open, high, low or close price. If you want, for example, the close price of each bar, you should define it as (This will give you the most recent close price):
price = self.data.close[0]
- Also, please show how your input data looks from YahooFinance. I might have some advise there for you in your
bt.feeds
method