Pyalgotrade SMA Coding Clarification

320 views Asked by At

I've started learning and testing PyAlgoTrade and having a hard time understanding some of the logic behind the code of some of the technicals like SMA and RSI. I understand the self.info() function prints out the dataframe feed it takes as a variable, however, what is the role of the [-1] after SMA and RSI on the last line of the code posted below ?

from pyalgotrade import strategy
from pyalgotrade.barfeed import yahoofeed
from pyalgotrade.technical import ma
from pyalgotrade.technical import rsi
class MyStrategy(strategy.BacktestingStrategy):
    def __init__(self, feed, instrument):
        strategy.BacktestingStrategy.__init__(self, feed)
        self.__rsi = rsi.RSI(feed[instrument].getCloseDataSeries(), 14)
        self.__sma = ma.SMA(self.__rsi, 15)
        self.__instrument = instrument
    def onBars(self, bars):
        bar = bars[self.__instrument]
        self.info("%s %s %s" %(bar.getClose(), self.__rsi[-1], self.__sma[-1]))
1

There are 1 answers

0
GhostCat On BEST ANSWER

A negative index simply means: you count from the "rear" end of your array.

In other words: [-1] refers to the last element in your array, [-2] to the "almost last", and so on.