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

Hello Folks....newbie question here

RE: Using variable other than prices for first variable of RSI

Function:
rsi = talib.RSI(variable other than prices, timeperiod=14)
RSI=rsi[-1]

I'm having some trouble using the talib rsi function. Rather than using 'prices' as my first variable.....I would like to use another variable. For purposes of illustration lets say I'm using the high of one security less the low of a second security as my RSI variable.

I am using this to grab data:

Grab 2 stocks

context.stock1 = symbol('AMZN')
context.stock2 = symbol('FSLR')
context.stocks = [context.stock1, context.stock2]

For notational convenience

s1 = context.stock1
s2 = context.stock2
prices = data.history([s1, s2], "price", context.long_ma_length, '1d')
short_prices = prices.iloc[-context.short_ma_length:]

My problem is if I use A (below) as my variable...RSI will not compute since its looking for 14 days of data ? If so, then I need to setup an array with each days data manipulation ?
A. Hi_Low_spread = ((short_prices[s1]-x)/y) - ((short_prices[s2])-x/y)

I can't use B (below) price history because prices are an array and I need to do 'daily' data manipulation
B. Hi_Low_spread = ((prices[s1]-x)/y) - ((prices[s2])-x/y)

The above is more concpetual than actual since I'm not showing the data capture of highs and lows.....but I hope I've illustrated my dilemma

2 responses

This may help

# PriceRelative RSI  
import talib  
# -----------------------------------------------------  
sec_A, sec_B, period = symbol('QQQ'), symbol('TLT'), 14  
# -----------------------------------------------------  
def initialize(context):  
    schedule_function(PriceRelativeRSI, date_rules.every_day(), time_rules.market_open(minutes = 15))  

def PriceRelativeRSI(context, data):  
    prices = data.history([sec_A, sec_B], 'price', period + 1, '1d')  
    PriceRelative = prices[sec_A] / prices[sec_B]  
    rsi = talib.RSI(PriceRelative[-period-1:], period)[-1]       

    record(rsi = rsi)  

Vladimir.....I looked thru many many algos and see your gracious help everywhere ! I'll give this a shot.

Many thanks.