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

Hey guys, So I'm trying to work on an algorithm that takes the highs and lows for the RSI and uses those values to for a chosen period but I can't seem to figure out a way to get the values of the RSI highs and low. I am very new to Python so any help is greatly appreciated.
Here is the code I have so far.

from numpy import isnan, matrix, array, zeros, empty, sqrt, round, ones, dot, append, mean, cov, transpose, linspace
import talib
import numpy as np
import pandas as pd
import statsmodels.api as statsmodels
import statsmodels.tsa.stattools as statstools

def initialize(context):
context.data_frequency = '1m'
context.RSI_period = 140
context.RSI_field = 'close_price'

context.RSI_high_period = 450  
context.RSI_low_period = 450  
context.RSI_high = 0  
context.RSI_low = 0  
context.stock = symbol('SPY')  

def handle_data(context, data):
price_history = history(
bar_count = context.RSI_period,
frequency = context.data_frequency,
field = context.RSI_field
)

RSI = price_history.apply(talib.RSI, timeperiod=context.RSI_period).iloc[-1]  
RSI_high = price_history.apply(talib.RSI, timeperiod=context.RSI_period).iloc.high()  
RSI_low = price_history.apply(talib.RSI, timeperiod=context.RSI_period).iloc.low()  
record(rsi = RSI, RSIhigh = RSI_high, RSIlow = RSI_low)

I've also drawn a picture of what the indicator would look like when it is functional to give a better idea of what I'm trying to accomplish.
http://imgur.com/wsjNHG2

I can also link a video that goes over this type of algorithm is requested.
Thanks for your time!

3 responses

Hi Tyler,

Do you think you could share your backtest on this thread? It's much easier to read through your code and find the problem! When you add a comment, you can click the "Attach" button in the top right and add the appropriate backtest.

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.

Just added the back test to it! Sorry about that.

Hi Tyler,

The issue here is that the field name for close prices is close_price (as opposed to just close). However, there are also issues with the way that the talib.RSI function is being called. I would suggest you take a look at this example in the help documentation for reference on how to use it. I would also recommend you look through the documentation for talib to see how the different functions work and what the timeperiod attribute is used for.