For code/output blocks: Use ``` (aka backtick or grave accent) in a single line before and after the block. See: http://commonmark.org/help/
Recursive indicator
-
Hi there,
I am new to bt and need some help. I want self.move to be a trend indicator, and want its value always be the last non-zero value after triggering.
def __init__(self): # keep track of close price in the series self.data_close = self.datas[0].close # keep track of pending orders/buy price/buy commission self.order = None self.price = None self.comm = None self.move = bt.LineNum(0.0) # add Bollinger Bands indicator and track the buy/sell signals self.b_band = bt.ind.BollingerBands(self.data_close, period=self.p.period, devfactor=self.p.devfactor) self.upbot = bt.ind.CrossOver(self.data_close, self.b_band.lines.bot) self.downtop = bt.ind.CrossOver(self.data_close, self.b_band.lines.top) self.move = bt.If(self.upbot>0, 1, bt.If(self.upbot<0, -2, bt.If(self.downtop<0, -1, self.move(-1))))
Currently, it returns the following result to me
uptop: -1.0, downtop: 0.0, move: -2.0 uptop: 1.0, downtop: 0.0, move: 1.0 uptop: 0.0, downtop: 0.0, move: 0.0 uptop: 0.0, downtop: 0.0, move: 0.0
What I wanted is
uptop: -1.0, downtop: 0.0, move: -2.0 uptop: 1.0, downtop: 0.0, move: 1.0 uptop: 0.0, downtop: 0.0, move: 1.0 uptop: 0.0, downtop: 0.0, move: 1.0
Please help! Thank you in advance!