Back to Community
Help with creating a 30-minute chart strategy for the RSI

Hi guys,

I'm trying to figure out how to code a very simple strategy but I'm having trouble re-sampling Quantopian's 1m data into 30m data because this strategy uses the RSI on a 30m chart to trade. I don't know if I coded the RSI correctly neither. I'm not a programmer so it's been a bit difficult to figure out what to do.

If RSI(11) cross> 50, buy SPY with 100% equity
If RSI(11) cross< 50, short SPY with 100% equity

Thanks ahead!

import numpy as np  
import math  
import talib

def initialize(context):  
    context.stock = symbol('SPY')  
    set_benchmark(symbol('SPY'))

    # For every minute available (max is 6 hours and 30 minutes)  
    total_minutes = 6*60 + 30  
    for i in range(1, total_minutes):  
    # Every 30 minutes run schedule  
      if i % 30 == 0:  
      # This will start at 6:31AM and will run every 30 minutes  
        schedule_function(  
        myfunc,  
          date_rules.every_day(),  
          time_rules.market_open(minutes=i),  
          True  
        )              

def myfunc  
    pass

def handle_data(context, data):  
    SPY = context.stock  
    prices = history(390, '1m', 'close')  
    rsi = prices.apply(talib.RSI, timeperiod=11).iloc[-1]  
    #Buy XIV when RSI crosses above 50  
    if rsi[SPY] > 50:  
        order_target_percent(SPY, 1.00)  
    #Short SPY when RSI crosses below 50  
    elif rsi[SPY] < 50:  
        order_target_percent(SPY, -1.00)  
    record(leverage = context.account.leverage)

1 response

trying to do the same thing as you, trade off of different timeframes. I am not a coder either lol. Does this code work for you? there has to be an easier way in the api but i have not found it in my searches.