Navigation

    Backtrader Community

    • Register
    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    For code/output blocks: Use ``` (aka backtick or grave accent) in a single line before and after the block. See: http://commonmark.org/help/

    SMA Cross Code Error

    General Code/Help
    2
    2
    25
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • Michael Reilly
      Michael Reilly last edited by

      I am beating my head against the wall trying to debug this script but haven't made any progress.

      import datetime
      import backtrader as bt

      class 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

      1 Reply Last reply Reply Quote 0
      • Pierre Cilliers 0
        Pierre Cilliers 0 last edited by

        Hi @Michael-Reilly

        Just a few things I would change in your code. Try them out and reply if you get the same error still.

        1. I would include an input data source for your technical indicators, for instance:
        sma = bt.ind.SMA(self.data, period=50)
        
        1. 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]
        
        1. Also, please show how your input data looks from YahooFinance. I might have some advise there for you in your bt.feeds method
        1 Reply Last reply Reply Quote 0
        • 1 / 1
        • First post
          Last post
        Copyright © 2016, 2017, 2018, 2019, 2020, 2021 NodeBB Forums | Contributors