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
Emiliano Fraticelli
So I took a screenshot of this old Quantopian lesson where we see the usage of data.history() and an output
Maybe this can help
Derek Melchin
Hi Emiliano,
Our History method returns a DataFrame, indexed by Symbol and time. So to resolve the issue above, change line 2 to
hist = self.History([sec.Symbol], timedelta(days=self.ZBF_PERIOD + 1),Resolution.Daily).loc[sec.Symbol].close[:-1]
See the attached backtest for reference. In addition, consider reviewing our Intro to Financial Python tutorial to get more familiar with our API.
Best,
Derek Melchin
The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.
Emiliano Fraticelli
Thank you Derek Melchin
It seems to work now. I have another question, if I might ask.
I don't understand very well the 'price' parameter in the original Quantopian code.
I mean, this 'price' isn't passed from the external and can't become something like open, high, low or close. It just remains price. So, from what I can understand, looking at both this and this,
this instruction, present in the original Quantopian code,
hist = data.history(sec, 'price', ZBF_PERIOD + 1, '1d')[:-1]
should return a dataframe like this one? In this case passing as sec IBM and as ZBF_PERIOD 1 , right?
https://ibb.co/SKJz1QLAfter, again in the original Quantopian code, we have:
zbf_momentum = hist[-1] - hist[0]
So it basically makes the difference of every close, high, low, open and volume column, right?
So, zbf_momentum becomes something like this?
https://ibb.co/6ZQRYwQThanks
Emiliano Fraticelli
The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.
To unlock posting to the community forums please complete at least 30% of Boot Camp.
You can continue your Boot Camp training progress from the terminal. We hope to see you in the community soon!