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

I am fairly new to both python and Quantopian, but decided to try an algorithm centered on the RSI. Any suggestions on improving the algorithm, or corrections to any errors are much appreciated.

# Put any initialization logic here.  The context object will be passed to  
# the other methods in your algorithm.  
import talib  
import numpy as np  
import math

def initialize(context):  
    context.security = symbol('SWKS')  
    context.LOW_RSI = 45  
    context.HIGH_RSI = 70

# Will be called on every trade event for the securities you specify.  
def handle_data(context, data):  
    price = data[context.security].price  
    prices = history(15, '1d', 'price')  
    # Use pandas dataframe.apply to get the last RSI value  
    rsi_data = prices.apply(talib.RSI, timeperiod=14,).iloc[-1]  
    sec_rsi = rsi_data[context.security]  
    cash = context.portfolio.cash  
    #RSI over 70 indicates overbought, time to sell  
    if sec_rsi<context.LOW_RSI and cash>price:  
        shares_bought = int(cash/price)  
        order(context.security, +shares_bought)  
        log.info('Buying %s shares of %s' %(shares_bought, context.security.symbol))  
    #RSI under 30 indicates oversold, time to buy  
    elif sec_rsi>context.HIGH_RSI:  
        order_target(context.security, 0)  
        log.info('Selling %s' %(context.security.symbol))  

    record(secRSI=sec_rsi, secPRICE=data[context.security].close_price)  
    log.info(cash)  
2 responses

Looks good. One thing you could do is change your order() call in the if statement to order_target_percent(context.security, 1) to purchase as many shares as you can afford. Also, you shouldn't need to use apply() — just use sec_rsi = talib.RSI(prices, 14)[-1].

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.

Thank you for the suggestions Gus!