Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Fading the Gap Algo Bug?

Cannot figure out why my portfolio doubles in size with two short sales in a row.

Log output:

2013-06-07PRINTPrev Hi: 217.93  
2013-06-07PRINTOpen: 219.03  
2013-06-07PRINTBuy Short on 2013-06-07 13:31:00+00:00  
2013-06-07PRINTPortfolio value: 10214.7381812  
2013-06-07PRINTPosition shares: -46.6362515691  
2013-06-07PRINT----  
2013-06-07PRINTPortfolio value: 20314.4238187  
2013-06-07PRINTPosition shares: -46  
2013-06-07PRINTSell- cover short on 2013-06-07 13:36:00+00:00  
2013-06-07PRINTTarget: 217.93  
2013-06-07PRINTCurrent: 217.401  
2013-06-07PRINTPortfolio value: 20314.4238187  
2013-06-07PRINTPosition shares: 46  

My algo should only have one position open at a time. Looks like for some reason a position is not closing out and begins building and borrowing to do so.

What is the best way to ensure no cash is borrowed, and positions are closed out before another position is opened?

Thanks

2 responses

@Chuck, by definition a short position must borrow (stocks, not cash). As a result your "cash" is still there but is tied up as collateral.

I plugged in a quick test of how much capital you are using. It looks like you are borrowing $90k on your starting $10k. Not good.

I would suggest you examine your algorithm logic for how you check for existing positions. The easiest way I know of is from the portfolio object:

qty = context.portfolio.positions[context.stocks[0]].amount  
if qty > 0: print "long"  
if qty < 0: print "short"  
if qty == 0: print "no position"  

I'm wondering about your $10k starting cash. If you are planning to trade in a real account you will need $25k minimum to get a margin account. Otherwise you won't be able to take a short position at all.

Thanks for the look Dennis.

I will change the initial investment to $25k as that does make more sense.

I will also add the logic you suggested.