1

I currently have a:

  • parent class, called bt.strategy (which is a default class in the Backtrader library)
  • child class, called Strategy (which inherits from bt.strategy)
  • child of a child class, called OpeningVolHigh (which inherits from Strategy)

I am having trouble getting OpeningVolHigh to inherit properties from Strategy. I want OpeningVolHigh to inherit the dataClose, dataOpen, and dataHigh properties from Strategy, but this does not work. Interestingly enough, I have no problem calling self.log(), a function from Strategy, in OpeningVolHigh.

This is my code:

Child class - strategy:

class Strategy(bt.Strategy):

    params = {
        'riskfreerate': 0.035,
        'cheat_on_open': False,
        'printlog': True,
    }

    plotinfo = dict(
        plotymargin=0.15,
        plothlines=[0],
        plotyticks=[1.0, 0, -1.0])

    def __init__(self):
        # Used to keep track of pending orders and buy price/commission
        self.order = None
        self.buyprice = None
        self.buycomm = None
        self.order_rejected = False

        self.dataClose = self.datas[0].close
        self.dataOpen = self.datas[0].open
        self.dataHigh = self.datas[0].high
        self.dataLow = self.datas[0].low
        self.dataVolume = self.datas[0].volume
        self.dataOpenInterest = self.datas[0].openinterest
        self.dataDatetime = self.datas[0].datetime
        self.startcash = self.broker.getvalue()

        bt.Strategy.__init__(self) **Inherit bt.strategy methods/properties**


    def log(self, txt, date=None):
        date = date or self.data.datetime.date(0)
        print('{}, {}'.format(date.isoformat(), txt))

Child of a child class - OpeningVolHigh:

class OpeningVolHigh(Strategy):

    def __init__(self):
        #super().__init__() **I tried this too, but it didn't work**
        self.log(self.params.riskfreerate) **This works**
        Strategy.__init__(self)
    self.log(self.dataClose) **This returns an error. dataClose is in the *strategy* class. See below error.**

The error returned:

object has no attribute 'dataClose'

How do I fix this? Thanks!!

01jayss
  • 1,400
  • 6
  • 19
  • 28
  • 1
    What is `base.Strategy`? It’s not defined in your code or mentioned in the question. – Mark Oct 04 '20 at 19:01
  • Sorry, base.strategy is the "strategy" class. Updated Q to reflect this. – 01jayss Oct 04 '20 at 19:26
  • 1
    Did you try `super().__init__` _without_ `Strategy.__init__(self)`? also you haven't indented the `__init__` method of `OpeningVolHigh` which is a syntax error, you should edit the question to fix it. – Dan Oct 04 '20 at 19:31
  • (1) Yes, I have tried `super().__init__` without `Strategy.__init__(self)`. (2) Thanks I have updated Q with correct syntax – 01jayss Oct 04 '20 at 20:24
  • `self.dataClose = self.datas[0].close` Where is `self.datas` defined? – John Gordon Oct 04 '20 at 20:28
  • It's defined in the `Backtrader` python library (i.e. in the parent class, `bt_strategy`). If I put `self.dataClose = self.datas[0].close` in the `__init__` of `OpeningVolHigh`, I can access `self.dataClose` from `OpeningVolHigh`. But if it's defined in `init` of `BaseStrategy`, I cannot access it from `OpeningVolHigh`, despite `OpeningVolHigh` being a child of `BaseStrategy`. (Note - I'd much prefer implementing it this way for code reusability). – 01jayss Oct 04 '20 at 20:31
  • Update: I solved this by closing and reopening the Python shell. – 01jayss Oct 04 '20 at 21:28

0 Answers0