Back to Community
Trouble making a program that buys an equal amount of two etfs

I am trying to make a program that spends 1/2 of my cash on VOO and the other half on RGI. However, the RGI is not getting filled. Also it is saying that negative number of shares cannot be filed. What am I doing wrong?

Can you please help me figure this out.

Clone Algorithm
5
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
def initialize(context):
    context.myportfolio = [sid(32836), sid(8554),]
    
    
def handle_data(context, data):
    own = []
    for stock in context.myportfolio:
        if stock not in own:
            order_target_percent(stock, .5)
            own.append(stock)
        else:
            break
 

        
def before_trading_start(context, data):
    pass
There was a runtime error.
7 responses

so the

own = []  
own.append(stock)  

doesn't do anything. the own is reinitialized every time you run the func, stock is never in own because you've just set own = []

that said,

order_target_percent(sid(32836), .5)  

needs another look, i suspect it's corrupt data. try using spy and voo.

ps. i'm going to put up a bug post to see if Q notices.

@toan

There definitely seems to be a data issue.

I ran

order_target_percent(sid(32836), 1)  

and the graph does not look anything like this compared to the benchmark:
https://www.google.com/finance?chdnp=1&chdd=1&chds=1&chdv=1&chvs=maximized&chdeh=0&chfdeh=0&chdet=1465105413209&chddm=541535&chls=IntervalBasedLine&cmpto=NYSEARCA:SPY&cmptdms=0&q=NYSEARCA:RSP&ntsp=0&ei=8btTV6iZD4qmebv7jcgD

This is just an equally waited S&P 500 etf. It should not look like the back test.

Should I email quantopian?

The a similar issue happens to EWRI, hte EW Russell 1000

Clone Algorithm
1
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
def initialize(context):
    context.myportfolio = [sid(32836), sid(8554),]
    
    
def handle_data(context, data):
    order_target_percent(sid(32836), 1)  


        
def before_trading_start(context, data):
    pass
There was a runtime error.

in the bug posting i did, one of the guys says it's not a bug, but i still think it is.

the order_target_percent will place an order and if it's not filled in the same minute, will place another order. if it is not a liquid etf, you will stack up a bunch of orders and they slowly get filled. I think that's a really buggy way to implement that function. the correct way, i think, is check portfolio and unfilled orders, and only place a new one if amount is insufficient to target.

toan tran,

It is not a bug in prices it is a good illustration how badly Quantopian unrealistic default slippage model controlling any algo.
If you disable it, the results will be much better or as expected.

Clone Algorithm
4
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
def initialize(context):
    
    context.asset = sid(32836)
    set_slippage(slippage.FixedSlippage(spread=0))
    set_commission(commission.PerShare(cost=0))
    schedule_function(trade,date_rules.every_day(), time_rules.market_close(minutes=5)) 
    
def trade(context, data):
    order_target_percent(context.asset, 1.0)  


        
There was a runtime error.

Vald, here are the results of not setting slippage and commission. I'm not saying that the models are anywhere accurate, but in this case, the broken function is the order_target_percent.

Clone Algorithm
2
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
def initialize(context):
    
    context.asset = sid(32836)
    #set_slippage(slippage.FixedSlippage(spread=0))
    #set_commission(commission.PerShare(cost=0))
    schedule_function(trade,date_rules.every_day(), time_rules.market_close(minutes=5)) 
    
def trade(context, data):
    order_target_percent(context.asset, 1.0)  


        
There was a runtime error.

toan,

To my mind the order_target_percent function working properly if you use it the right way.

Why does this under perform the benchmark?

The SPY should do the same as the benchmark and the RGI does better than the benchmark so together they should do slightly better than the benchmark.

Am I buying 50% of each improperly?

Clone Algorithm
2
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
def initialize(context):
    
    context.myportfolio = [sid(24744), sid(8554),]
    set_slippage(slippage.FixedSlippage(spread=0))
    set_commission(commission.PerShare(cost=0))
    schedule_function(trade,date_rules.every_day(), time_rules.market_close(minutes=5)) 
    context.own = []

    
def trade(context, data):
    for stock in context.myportfolio:
        if stock not in context.own:
            order_target_percent(stock, .5)
            context.own.append(stock)
        else:
            break


        
There was a runtime error.