Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Reorganize history into 30-minute increments

I'm porting a script from Pine on TradingView to Quantopian.
Their "history" is in 30-minute increments instead of the tiny one-minute views. I'm trying to replicate the functionality of doing their RSI(close, input(11)) function, but I haven't made any progress. I'm not familiar with Quantopian, but I am familiar with Python in general, so explanations about the types of anything related to data.history would be great.
This is what I have so far:

def handle_data(context, data):  
    hist = data.history(symbol('XIV'), "price", 390, '1m')  
    hist = hist.groupby(lambda d: d.date()).resample('30T')  
   rsi = talib.RSI(hist.values, timeperiod=11)[-1]  
    print 'RSI: %s' % (rsi,)  
5 responses

Just bumping this, I hope it's okay to do that.

I am interested to find out, too. BUMP

Hi Daniel, thanks for posting! If you're looking for some details about data.history, you can look at the documentation here and the relevant Getting Started Tutorial lesson here. TA-Lib documentation is here.

As for your code, .resample defaults to taking the mean of each group of bars. If you want the close of each 30-minute group, you should add how='last' as a keyword argument in order to take the close of the last minute bar. The documentation for .resample is here.

If you're having some issues with the code, could you elaborate on what they are?

Disclaimer

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 Quantopian. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. No information contained herein should be regarded as a suggestion to engage in or refrain from any investment-related course of action as none of Quantopian nor any of its affiliates is undertaking to provide investment advice, act as an adviser to any plan or entity subject to the Employee Retirement Income Security Act of 1974, as amended, individual retirement account or individual retirement annuity, or give advice in a fiduciary capacity with respect to the materials presented herein. If you are an individual retirement or other investor, contact your financial advisor or other fiduciary unrelated to Quantopian about whether any given investment idea, strategy, product or service described herein may be appropriate for your circumstances. All investments involve risk, including loss of principal. Quantopian 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.

Well, when I try and take the RSI of the resampled data of XIV from pretty much any time period, it almost always gives me NaN as the value. I'm not sure if I'm using RSI correctly, because it's very different than the Pine script on TradingView. This is my code:

hist = data.history(context.stock, "price", 390, '1m').resample('30m', how='last')  
rsi = talib.RSI(hist.values, timeperiod=11)[-1]  # Almost always NaN. This is an array that is mostly NaN and a few actual values. Which values do I take?  

Anyways, sorry for the late reply, I appreciate your help a lot.

Bump.