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)