Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Trading Idea - single stock method

Very overwhelmed so far with the coding necessary to make this happen, so I thought I'd throw this out for any comments or pointers. I'd like to build this simple algor as described:

Using the RSI as a set up:

Once the signal line has crossed over the top or bottom limits (70,30)

A trade is setup to be executed (Buy or Sell) when the signal line later crosses back over one or the other limits AND the stock price has crossed over or under a moving average as per the direction of the trade.

I would like to also include a following stop that can be set to a % or dollar amount.

2 responses

Hello James,

Here's some code and a backtest:

import talib 

def initialize(context):  
    context.stock = sid(8554) # SPY  
    context.prior_bar_day = None

def handle_data(context, data):  
    if get_datetime().day != context.prior_bar_day:  
        closes = history(16,'1d','price')[0:-1]  
        RSI = talib.RSI(closes[context.stock],timeperiod=14)[-1]  
        # print RSI  
        record(RSI=RSI)  
        if RSI > 70:  
            order_target_percent(context.stock,1)  
        elif RSI < 30:  
            order_target_percent(context.stock,0)  
    context.prior_bar_day = get_datetime().day  

I think it's correct, but you might want to check the RSI calculation against your expectation, since I'm not so familiar with the Quantopian TA-LIB implementation.

Grant

Thanks very much Grant! I'm looking over the code, and I can't determine whether or not trades are being executed at some point after the RSI has gone above the limits and then crossed back over or trades are taken after the signal line has simply crossed. I ran a test, but I can't really figure out the exact conditions that the trades were made under. Furthermore, I'm going to need to coordinate the conditions returned by the RSI code with a MA requirement - correct? I don't think I see that in this bit of code.