How to get an array containing the current and past values of an indicator.
-
Hello,
Lets suppose I declare an indicator :
self.cci = bt.talib.CCI(self.datas[0].high, self.datas[0].low, self.datas[0].close)
Now, when I try to access the indicator value on the next function I can do:
self.cci[0], which will get the me current value, or
self.cci[1], which I believe will get me the previous value.
But how can I get an array containing the n previous values? I tried something like self.cci[:n], but I keep getting an error.
-
@Rodrigo-Pinto You're mistaken in thinking that
self.cci[1]
is a past value; in fact, it's a future value. You'd wantself.cci[-1]
instead.I would first check in
next()
that there are at leastn
values inself.cci
;len()
can do this. Then I would tryself.cci[(-n):]
. I don't know if this will work but I think it would. -
@Rodrigo-Pinto said in How to get an array containing the current and past values of an indicator.:
But how can I get an array containing the n previous values? I tried something like self.cci[:n], but I keep getting an error.
Not supporting slicing is a design decision, because it doesn't make sense with the indexing chosen for this library.
As pointed out by @Curtis-Miller,
[1]
is not the previous value but the future next value. In contexts in which the data has been preloaded (to optimize speed), it won't break, but you will be cheating yourself.See the following:
- Docs - Platform Concepts (where you may want to focus on the section: Indexing: 0 and -1
Use
.get(ago=x, size=y)
(where the default values arex=0, y=1
)This was already also discussed in these threads (good time to put them together) where some discussion about why slicing doesn't make sense is present:
-
An explanation has been added to: