Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
RSI multiple securities

Hi,

I am working on calculating RSI for multiple securities, but seem to be having trouble checking if the trade dates are in alignment. I think this is causing NaN values to pass to the RSI function, causing an error.

My code is below, could someone provide guidance on how to check if data is within a security?

Code below:

import talib
import numpy as np
import math
import scipy.stats as ss

Setup our variables

def initialize(context):
context.etfuniverse = {
'spy' : sid(8554), 'total_stock' : sid(22739), 'small_cap' : sid(21519), 'nasdaq': sid(19920), 'total_bond': sid(18387), 'europe': sid(27100), 'emerging': sid(27102), 'reit': sid(26669), 'gold': sid(26807)}

context.LOW_RSI = 30  
context.HIGH_RSI = 70  

# Create a variable to track the date change
context.date = None

def handle_data(context, data):
todays_date = get_datetime().date()

# Do nothing unless the date has changed  
if todays_date == context.date:  
    return  
# Set the new date  
context.date = todays_date

cash = context.portfolio.cash  

# if sid in data:
# Load historical data for the stocks
prices = history(15, '1d', 'price')

# Use pandas dataframe.apply to get the last RSI value  
# for for each stock in our basket

if sid(8554) and sid(22739) and sid(21519) and sid(19920) and sid(18387) and sid(27100) and sid(27102) and sid(26669) and  sid(26807) in data:  

    rsi_data = prices.apply(talib.RSI, timeperiod=14).iloc[-1]  

    rsi1=rsi_data[context.etfuniverse['spy']]  
return  
log.info(rsi1)  
3 responses

Hi Adam,

Since talib.RSI calculates rolling RSI values, you will either need to increase the window of historical data that you are providing, or decrease the timeperiod parameter! You can also use .dropna() on prices to remove NaNs and then look at len(prices[sid(XXX)]) to ensure that you have enough data points.

Does this help?

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.

@Jamie

Thank you for your help, still having some trouble, could you help provide some feedback on the below code?

code additions:
prices = prices.dropna()

if len(prices[sid(27102)]) > 15:  
    rsi_data = prices2.apply(talib.RSI, timeperiod=14).iloc[-1]  
rsi1 = rsi_data[context.etfuniverse['spy’]]  
    log.info(rsi1)  
return

The algo executes, but the if function is never entered, as the log command is never executed, could you help me better understand why this may be occurring? I chose sid 27102 because the etf start date is the latest out of the basket of the efts in the algo.

Thanks!

Hi Adam,

Since you're only getting the history for the last 15 days, the length will never be greater than 15! Using the same code, try getting the history for the last 20 days.