Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
order_target_percent works unexpected

Hey,

so if I do something like:

    if date == dateb1:  
        order_target_percent(context.jj,1)  
    elif date == dateb2:  
        order_target_percent(context.jj,-1)  

with one stock (capital at the beginning: 1000) it works like expected: At the first if statement I buy 10 shares, which is appr. 100% of my portfolio. At the elif statement I sell those 10 shares (made appr. 10% retrun) an go short for -10 shares because the its appr. 100% of my portfolio.

but if I do something like:

    if date == dateb1:  
        order_target_percent(context.jj,1)  
    elif date == dateb2:  
        order_target_percent(context.jj,0)  
        order_target_percent(context.jj,-1)  

The if statement works the same way but after the elif statement I got -20 shares which is 200% of my portfolio.

I do not see what is happening here.

Thanks!

1 response

The short answer is that the 'order_target_percent' method doesn't take into account any open orders. Additionally, the order methods don't fill in-line with the code but rather are filled after all the code is executed each minute.

Here's what's happening...

order_target_percent(context.jj,1)  

places order to buy 10 shares

order is filled after all scheduled and handle_data methods so now have 10 shares

order_target_percent(context.jj,0)  

places order to sell 10 shares (equal to 10 share held)
order isn't filled yet so still have 10 shares with outstanding order to sell those 10 shares

order_target_percent(context.jj,-1)  

places order to sell 20 shares (equal to 10 shares held plus 10 shares needed to go short)
just looks at current holdings and doesn't take into account any outstanding orders
orders are filled after all scheduled and handle_data methods so now have net -20 shares (10 - 10 - 20 = -20)