Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
How to create Algo to buy when RSI moves below 30 and sell when it reaches above 70?

Hello! I'm an absolute beginner here, and I'm having trouble using quantopian. I completed all of the tutorials, but one thing I don't understand is how to implement a pipeline so that my algo buy and sells when certain conditions are met. I also don't understand how the whole MaximizeAlpha function works. Any help of examples is appreciated.

1 response

Jacob,
Try something like this:

import talib  
# -----------------------------------------------------------------  
STOCK = symbol('SPY'); RSI_PERIOD = 14; LB = 30; UB = 70; LEV = 1.0  
# -----------------------------------------------------------------  
def initialize(context):  
    schedule_function(trade, date_rules.every_day(), time_rules.market_open(minutes = 65))   

def trade(context, data):  
    if get_open_orders(): return 

    prices = data.history(STOCK, 'price', RSI_PERIOD + 2, '1d')  
    rsi = talib.RSI(prices, RSI_PERIOD)  
    rsi_curr = rsi[-1]  
    rsi_yest = rsi[-2]

    record(rsi_curr = rsi_curr, rsi_yest = rsi_yest, LB = LB, UB = UB) 

    if data.can_trade(STOCK):  
        if (rsi_curr > LB and rsi_yest < LB):  
            order_target_percent(STOCK, LEV)  
        elif (rsi_curr > UB and rsi_yest < UB):  
            order_target_percent(STOCK, 0)  
        else:  
            return  

    record(leverage = context.account.leverage)