Indicators - Indexing at __init__()
-
Hello All,
Can someone help shed some light on a little issue I have? I think I am missing a platform fundamental / conceptBelow is a simple example code that demonstrates my issue.
Basically I am trying to create a data feed that is calculated at init and compares the current value of a data feed with the previous value before deciding what to assign to another data.
I seem to be able to get away with it when looking at
data.close
but not with a data feed created during initself.data.hlc3
class TestInd(bt.Indicator): lines = ('sig',) def __init__(self): self.addminperiod(55) self.data.hlc3 = (self.data.high + self.data.low + self.data.close) / 3 # This works self.data.vd = bt.If(self.data.close[0] >= self.data.close[-1], self.data.volume, -self.data.volume) # This does not self.data.sd = bt.If(self.data.hlc3[0] >= self.data.hlc3[-1], self.data.volume, -self.data.volume) self.lines.sig = bt.indicators.EMA(self.data.vd, period=34) - bt.indicators.EMA(self.data.vd, period=55)
If someone could jog my memory and remind me what I am doing wrong, that would be great.
-
@thatblokedave said in Indicators - Indexing at __init__():
self.data.vd = bt.If(self.data.close[0] >= self.data.close[-1], self.data.volume, -self.data.volume)
Wrong indexing. During
__init__
you use(0)
.See Docs - Platform Concepts -
Stage1
andStage 2
-
@backtrader Thanks! I knew I was forgetting something. Will give it a go.