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

Hi,
I started working with quantopian 1 week ago. I wrote my first 'trading algos' to see what happens if... and so on. But I guess I still do not understand how quantopian is handling the trades during the backtest. Lets assume we have only one stock 'AAPL' and start with 1000$ over one year.

def initialize(context):  
    schedule_function(bb_trade,date_rules.every_day(),time_rules.market_close(minutes=1))

    context.aapl = sid(24)


def bb_trade(context, data):  
    cur_price = data.current(context.jj,'price')  


    if some_condition:  
        order_target_percent(context.aapl,0)  
    elif some_other_condition:  
        order_target_percent(context.aapl,1)  

What I don't get is: if I do 'order_target_percent(context.aapl,1)' ,my 1000$ should be invested in AAPL stock (at least in the amount I could buy for 1000$). What happens is, that I do 'order_target_percent(context.aapl,1)' two or more times in a row. But how can I buy Stock again with 100% of my portfolio if I already invested almost everything. Shouldn't the position be closed to do that again? Same Question for -1.
And 'order_target_percent(context.aapl,0)' means that I sell all the AAPL stock I have?

I'm looking forward to be enlightened:)

Best,
Robin

4 responses

If you don't get filled 100% in the first minute bar (i.e. only partially filled), I believe it tries again to fill 100% of capital the next minute. I'd suggest to look at the API help doc and try to limit leverage, which should prevent you from ordering more than your current position + remaining capital.

Thanks a lot!
There is one thing left.

Lets stay with the example of AAPL and 1000$.

I did 'order_target_percent(context.jj,1)' (the alg bought 10 shares) and made 15% return. After that I did directly order_target_percent(context.jj,-1) without leaving the position with order_target_percent(context.jj,0). The alg 'bought' -10 shares. What happens if
I go directly from 1 to -1. I do not see what is happening here.

Thanks in advance!

Robin

Sorry, I'm not sure I'm following. The minus sign means that you're selling, and the '1' means 100% of your capital. Meaning that you first sell the 10 shares that you're long (long-sell), and then go short with whatever capital left. If you just want to liquidate your long position, put a '0' as the target percent.

So what your saying makes sense. But the problem is, if I do a long sell with 1 to -1, my position changes from 10 shares to -10 shares. If I close my position with 0 first and go short right after that I got -20 shares. That’s confusing me.

If I have ten shares with a value approximately over 1000$ and do a long sell with -1 I’d think that I sell those ten got 1000$ and go short for approximately 10 shares. But what is the difference to 0? Why do I get -20 instead Of -10 shares this way? For me it seemed to be the same framework.

Thanks:)