Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Data / Programming question for TA-lib output

Hello everyone! I am relatively new to Python and Quantopian, so this may be a basic question.

I have been playing around with a sample algorithm for TA-lib using RSI. My goal is to create a moving average for RSI. This was fairly straightforward to do in the notebook environment:

import talib
import matplotlib.pyplot as pyplot
data = get_pricing(
'FANG',
fields='close_price',
start_date="2016-01-01",
end_date="2018-06-15",
frequency='daily'
) data.plot(use_index=False)
pyplot.title('FANG Close Price')
FANG_RSI = talib.RSI(data)
FANG_RSIMovingAVG = talib.SMA(FANG_RSI, timeperiod=10)
pyplot.plot(FANG_RSI)
pyplot.plot(FANG_RSIMovingAVG)
pyplot.title('FANG')

The problem comes when carrying this over to an algorithm. I have tried several approaches, but none have worked so far. I suspect I need to use data.history somehow, but I was unsure if this is only for price data.

Any help would be much appreciated.

Andrew

2 responses

This may help.

# RSI Indicator with moving average.  
import talib  
# ----------------------------------------  
stock, period, ma = symbol('FANG'), 14, 10  
# ----------------------------------------  
def initialize(context):  
    schedule_function(trade, date_rules.every_day(), time_rules.market_open(minutes = 65))

def trade(context, data):  
    bars = period + ma + 1

    prices = data.history(stock, 'price', bars, '1d')  
    rsi = talib.RSI(prices, period)  
    rsi_mavg = talib.SMA(rsi[-ma:], ma)  

    record(rsi = rsi[-1], rsi_mavg = rsi_mavg[-1])  

Thank you, that works!