Hi Everyone! I'm trying to understand and convert a function that once was used in Quantopian on QuantConnect. The original code is not mine, so I don't understand well it, also my conversion in QC's code doesn't work.

This is the original function:

def zero_bound_filter(sec, threshold): hist = data.history(sec, 'price', ZBF_PERIOD + 1, '1d')[:-1] if sec.symbol == 'XRT': debug("zbf: %s, period: %s, start_hist: %s, end_hist: %s, hist:\n%s" % (sec.symbol, ZBF_PERIOD, hist[0], hist[-1], hist)) zbf_momentum = hist[-1] - hist[0] if numpy.isnan(zbf_momentum): zbf_momentum = 0.0 debug("%s: ZBF momentum: %s, threshold: %s" % (sec.symbol, zbf_momentum, threshold)) return zbf_momentum < threshold

And here's my conversion:

def zero_bound_filter_original(self,sec, threshold): hist = self.History([sec.Symbol], timedelta(days=self.ZBF_PERIOD + 1),Resolution.Daily)[:-1] if sec.Symbol == 'XRT': self.Debug("zbf: %s, period: %s, start_hist: %s, end_hist: %s, hist:\n%s" % (sec.Symbol, self.ZBF_PERIOD, hist[0], hist[-1], hist)) #self.Log(hist[0]) zbf_momentum = hist[-1] - hist[0] if numpy.isnan(zbf_momentum): zbf_momentum = 0.0 self.Debug("%s: ZBF momentum: %s, threshold: %s" % (sec.Symbol, zbf_momentum, threshold)) return zbf_momentum < threshold

hist correctly gives a dataframe, when doing hist[0] or hist[-1] I get the error: 

Runtime Error: Trying to retrieve an element from a collection using a key that does not exist in that collection throws a KeyError exception. To prevent the exception, ensure that the key exist in the collection and/or that collection is not empty. at OnData in main.py:line 232 at zero_bound_filter_original in main.py:line 220 :: self.Log(hist[0]) KeyError : 0 (Open Stacktrace)

ZBF_PERIOD is an int, in this case = 10. 

threshold is another int, in this case = 1

My understanding is that this function checks if momementum is higher or lower than a threshold, but I get lost soon after in understanding well what happens