我目前有一个:
- 父类,称为bt.strategy(这是Backtrader 库中的默认类)
- 子类,称为Strategy(继承自bt.strategy)
- 子类的子类,称为OpeningVolHigh(继承自Strategy)
我无法让OpenVolHigh从Strategy 继承属性。我希望OpeningVolHigh从Strategy继承dataClose、dataOpen和dataHigh属性,但这不起作用。有趣的是,在OpenVolHigh中调用Strategy中的函数self.log()没有问题。
这是我的代码:
子类 -策略:
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))
子类的子级 - 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.**
返回的错误:
对象没有属性“dataClose”
我该如何解决?谢谢!!