Child class not inheriting from parent class

103 views Asked by At

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!!

0

There are 0 answers