Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Newby at Coding multiple strategies..MA and RSI..

All -

Pardon my 'nube'-ness for coding. I am having trouble implementing a multiple algo strategy. Trying to utilize MA and RSI to implement a long/short strategy. I'm not a great coder...but know what I want it to do. Any help would be immensely appreciated..I know this code is probably pretty sloppy. My apologies. Thanks all.

DP

Goal is to initialize a long position when price, OR 50MA, crosses above 100MA AND RSI > 50, and to sell when price, OR 50MA, crosses below 100MA and RSI<50

Put any initialization logic here. The context object will be passed to

the other methods in your algorithm.

import pandas as pd
import numpy
import pytz
import datetime
import talib
import math

def initialize(context):
# Algorithm will call myfunc every day 15 minutes after the market opens
schedule_function(
myfunc,
date_rules.every_day(),
time_rules.market_open(minutes=1)
)

context.stock = sid(26578)

def myfunc(context,data):
pass

def handle_data(context, data):

stock = context.stock  


#current and historic data from Google  
stock_data = data[stock]  

#moving average price data  
mavg_short_buy = stock_data.mavg(50)  
mavg_long_buy = stock_data.mavg(200)  
mavg_short_sell = stock_data.mavg(1)  
mavg_long_sell = stock_data.mavg(3)  

# current price of the stock  
current_price = stock_data.price  
# how much cash in portfolio  
cash = context.portfolio.cash


stock = context.stock  


#RSI  
#variables  
lookback = 200  
lookback2 = 200  #period to lookback  
charttype = '1d' #day or minute chart  
percent_baseline = .5#RSI retracement amount  
low = 30#RSI low value  
RSI_timeperiod = 3  
divergence_strength = 0  


prices_close = history(lookback, charttype, 'close_price')  

prices = history(lookback2,charttype, 'close_price')[context.stock]


prices_close_day = history(10, '1d', 'close_price')  
prices_close_day = list(prices_close_day.values.flatten())  
prices_close_day = prices_close_day[-2]  

rsi = talib.RSI(prices, timeperiod=RSI_timeperiod)  

#rsi_prices = create_rsi_price_array(rsi,prices_close)  





num_shares = math.floor(context.max_notional / data[context.stock].close_price)  


# This runs through each stock.  
for stock in context.stock:  
   if mavg_short_buy > mavg_long_buy and cash > current_price:  

    # how many shares we can buy  
    number_of_shares = int(cash/current_price)  
    # int() helps us get a integer value, we can't buy half a share  

    # place the buy order  
    order(stock, number_of_shares)  

   elif mavg_short_sell < mavg_long_sell:  
    #how many shares we have  
    number_of_shares = context.portfolio.positions[stock.sid].amount  

    #sell all shares  
    order(stock, -number_of_shares)  



# This runs through each stock again.  
for stock in context.stock:  

   if data[context.stock].price < context.stop_price:  
        order_target(context.stock, 0)  
        context.stop_price = 0  

        currentshares = context.portfolio.positions[context.stock].amount  

        divergence = bullish_divergence(rsi_prices,percent_baseline,low)


   if divergence != None and divergence[1] > 1 and currentshares == 0:  
     troughs = divergence[0]  
     trough_diff = troughs[1] - troughs[0]#used for calculate strength of divergence  

   if trough_diff > divergence_strength:  
        order_target_percent(context.stock, 1.0)  
        print 'buying '+str(num_shares)


   record(price=data[context.stock].price, stop=context.stop_price)  
2 responses

I am not very familiar with techinical analysis or trading methods so I am not sure I've quite captured your goal here. That being said, I do have a background in programming so I have taken your code and cleaned it up a bit. I have done my best to implement what I believe you are looking for but I'm not sure I got it quite right. At least this might help move you in the right direction or help others better understand your end goal.

There are still a lot of unused variables and methods in this code, I would recommend getting down to just what you need.

Bailey -

Thank you for your help! I really appreciate it. I am not that great at programming and got lost when I was trying to code the strategies. I will try tweaking this and re-post when I finish. Thank you again!