Back to Community
Robin hood live trading

I am wondering if someone would be able to help me put this algorithm into the Robinhood framework, it is using unsettled funds to make purchase orders causing my portfolio to have a negative value. I am still just learning how to build algorithms but since I want to go live with this algorithm I want to make sure its done the proper way.

Clone Algorithm
20
Loading...
Backtest from to with initial capital
Total Returns
--
Alpha
--
Beta
--
Sharpe
--
Sortino
--
Max Drawdown
--
Benchmark Returns
--
Volatility
--
Returns 1 Month 3 Month 6 Month 12 Month
Alpha 1 Month 3 Month 6 Month 12 Month
Beta 1 Month 3 Month 6 Month 12 Month
Sharpe 1 Month 3 Month 6 Month 12 Month
Sortino 1 Month 3 Month 6 Month 12 Month
Volatility 1 Month 3 Month 6 Month 12 Month
Max Drawdown 1 Month 3 Month 6 Month 12 Month
from quantopian.pipeline.factors import SimpleMovingAverage
import talib
from quantopian.pipeline.data.builtin import USEquityPricing

def initialize(context):
    context.stocks=[sid(41759),sid(24),sid(8461),sid(8089),sid(700)] 
    set_long_only()
    
    context.LOW_RSI = 30
    context.HIGH_RSI = 70
    
def handle_data(context,data):    
    rsis = {}
    
    
   
       
    for stock in context.stocks:
        history = data.history(context.stocks,'price',30,'1d')
        rsi = talib.RSI(history[stock], timeperiod=14)[-1]
        rsis[stock] = rsi
        sma_30 = history[stock].mean()
        sma_10 = history[stock][-10].mean()
        
        if  rsis[stock] < context.LOW_RSI and sma_10 < sma_30:
           order_target_percent(stock, 0.5)
        elif rsis[stock] > context.HIGH_RSI and sma_10 > sma_30:
           order_target_percent(stock, 0.0)
        
    record(bac_rsi=rsis[sid(700)],
           dnkn_rsi=rsis[sid(41759)],
           aapl_rsi=rsis[sid(24)],
           chk_rsi=rsis[sid(8461)],
           wba_rsi=rsis[sid(8089)])  
               
     
   
There was a runtime error.
6 responses

I'm no expert, but I'll take a stab. It's being executed every minute and trying to order every time the conditions are met. I changed the schedule to run only at open. Second, I put in a check to make sure that it isn't trying to sell shares you don't own.

Your money management is the biggest issue. Currently you can only hold two positions at a time since is set to buy 50%. I changed it a little so buys are made based on how much cash is available. Returns are diminished, though you're not using negative cash. Should give you something to play around with.

Clone Algorithm
15
Loading...
Backtest from to with initial capital
Total Returns
--
Alpha
--
Beta
--
Sharpe
--
Sortino
--
Max Drawdown
--
Benchmark Returns
--
Volatility
--
Returns 1 Month 3 Month 6 Month 12 Month
Alpha 1 Month 3 Month 6 Month 12 Month
Beta 1 Month 3 Month 6 Month 12 Month
Sharpe 1 Month 3 Month 6 Month 12 Month
Sortino 1 Month 3 Month 6 Month 12 Month
Volatility 1 Month 3 Month 6 Month 12 Month
Max Drawdown 1 Month 3 Month 6 Month 12 Month
from quantopian.pipeline.factors import SimpleMovingAverage
import talib
from quantopian.pipeline.data.builtin import USEquityPricing

def initialize(context):
    context.stocks=[sid(41759),sid(24),sid(8461),sid(8089),sid(700)] 
    set_long_only()
    
    context.LOW_RSI = 30
    context.HIGH_RSI = 70
    schedule_function(my_rebalance, date_rules.every_day(), time_rules.market_open(minutes=1))
    
def my_rebalance(context, data):
    trade = context.portfolio.cash / 4 
    
    rsis = {}
    
    for stock in context.stocks:
        history = data.history(context.stocks,'price',30,'1d')
        rsi = talib.RSI(history[stock], timeperiod=14)[-1]
        rsis[stock] = rsi
        sma_30 = history[stock].mean()
        sma_10 = history[stock][-10].mean()
        
        if  rsis[stock] < context.LOW_RSI and sma_10 < sma_30:
            shares = trade / data.current(stock, 'price')
            order(stock, shares)
            log.info("Bought: %s"%(stock))
        elif rsis[stock] > context.HIGH_RSI and sma_10 > sma_30 and context.portfolio.positions[stock].amount > 0:
            order_target_percent(stock, 0.0)
            log.info("Sold: %s"%(stock))
            
    log.info(context.portfolio.cash)
    
    record(bac_rsi=rsis[sid(700)],
           dnkn_rsi=rsis[sid(41759)],
           aapl_rsi=rsis[sid(24)],
           chk_rsi=rsis[sid(8461)],
           wba_rsi=rsis[sid(8089)])  
               
     
   
There was a runtime error.

Please correct me if I'm wrong, but if Dave's Algo didn't solve the problem you may need to look at line 14 on his
14: trade = context.portfolio.cash / 4
context.portfolio.cash is a sum of both settled and unsettled funds while context.account.settled_cash ignores unsettled funds
14: trade = context.account.settled_cash / 4

william,

This logic for entering long position

if rsis[stock] < context.LOW_RSI and sma_10 < sma_30:  

is pretty dangerous in bear market.
I would recommend to backtest algo from 2007 before turning it live.

Nice catch Ethan. I use Robinhood Instant so unsettled funds aren't an issue.

Dave, thank you so much for the help. being so new to this there are things in your code I would have never even considered or thought of so thank you again.
and Ethan thank you also I wasn't sure about the difference between context.portfolio.cash and context.account.settled_cash or I should say I wasn't positive if context.portfolio.cash used unsettled funds or just cash available. so thank you for making that clear

Vladimir, Thank you for the warning. Ideally I would only like to sell my long positions after a 10% profit is made but like i said before being so new to coding and building algorithms i am not sure how to put that into the elif part of the algo if you could help that would be much appreciated.