Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Short selling, how does it work (practically) ?

Hi guys, I'm new to quantopian.

I'm a confused about how short selling works in the stock market, and in Quantopian.
I'll try to make my question as clear as possible, I (think I) understand the theory. You borrow stock from some one (usually your broker), and then sell them to some one else, then sometimes in the future you need to buy them back and return to the broker.

What I don't understand is how it works in practice and in Qauntopian.
Let's say I have 10,000K in my portfolio in cache and I allocate 100% of the portfolio to short selling SPY :

order_target_percent(spy, -1)  

I would assume I used my 10K to short SPY, but then when I check how much cash I have :
cash = context.portfolio.cash I see 20K.

Then I've tried to use those 20K to buy something:
order_target_percent(sid(5061), 0.5)

And It bought this stock for 5K although 0.5*20K is 10K.

I understand the theory behind that, I have 20K because I've borrowed something (for free) and sold to some one for 10K.
What I don't understand is :
1. I probably payed for borrowing stocks, where is it indicated how much ?
2. It seems that I can't use this 20K to buy things, Only my original 10K. So why I see it in my cash ?

I've tried to short for 20K and it also works :
order_target_percent(security, -2) 3. So what is my limit ?
4. I guess I'm paying to someone money to sell short for more money then I have, how much ? In what variable it indicated ?

I'll be extremely helpful If someone could explain it to me.

5 responses

There's no limit to how much leverage you can use on your long or short positions. That's up to your broker. The backtester doesn't know that so you have to simulate it yourself.

And It bought this stock for 5K although 0.5*20K is 10K.

You bought the stock for 5k because the target percent is based on your equity. Equity = total assets (20k) - debt (10k)

  1. I probably payed for borrowing stocks, where is it indicated how much ?

You don't pay for borrowing stocks. The broker pays you the cash for selling it.

  1. It seems that I can't use this 20K to buy things, Only my original 10K. So why I see it in my cash ?

A broker will usually require you to keep a certain amount of cash in your account as collateral for short selling. The backtester doesn't care so you're free to use the cash if you want. You'll have to simulate the requirements yourself.

Thanks for your answer.

I have a followup question about leverage (not specifically for short positions) :
Let's assume I use quantopian to trade using interactive brokers, and I've on my account 10,000$.
1. Can I buy stocks for 20,000$ ?30,000$ ? what is the common limit ?
2. When I buy stocks for 20,000$ and have only 10,000$ It means the broker loaned me the money to buy, this means I need to pay interest, does the backtester calculates it some how ? if no, how should I calculate it ?

I'll take a stab at your initial questions...

1. I probably payed for borrowing stocks, where is it indicated how much ?
Quantopian assumes money is free and you don't pay any interest. There is a property 'context.account.accrued_interest' which is populated in real IB trading but for backtests it is 0.

2. It seems that I can't use this 20K to buy things, Only my original 10K. So why I see it in my cash ?
The 'target_percent' method places an order for the requested percentage of portfolio_value and NOT a percentage of available cash (portfolio_value = positions_value + cash) In your example, the portfolio_value was $10,000 (positions_value = -$10,000 + cash = +$20,000). So, 'order_target_percent(sid(5061), 0.5)' would order 50% of the portfolio value or $5000.

3. So what is my limit ?
Quantopian assumes you are Warren Buffet. They will keep lending you money. It just magically shows up in your account. You will see this as an increase in context.account.leverage. An algorithm must manage trading to keep this to a reasonable number. A value of 1 to 2 is reasonable. The Quantopian contest requires this to be below 3. How much is 'borrowed' will be the difference between the current cash (context.portfolio.cash) and cash you started with (context.portfolio.starting_cash).

4. I guess I'm paying to someone money to sell short for more money then I have, how much ? In what variable it indicated ?
As in question 1, Quantopian assumes no borrowing costs during backtests.

The accounting for short positions is very straightforward if one remembers two accounting rules.

A buy always increases shares and reduces cash.
A sell always reduces shares and increases cash.

When taking a long position, one is first placing a buy and then placing a sell.
When taking a short position, one is first placing a sell and then placing a buy (just the reverse).

#An example of taking a long position of XYZ  
#assuming XYZ sells for $10 and $0 commission and a starting balance of $10,000

order_target_percent(xyz, 1)  
#Buy reduces cash by $10,000 and increases positions_value by $10,000.  
#So cash = $0 and positions_value = $10,000

#now we sell..

order_target_percent(xyz, -1)  
#Sell increases cash by $10,000 and reduces positions_value by $10,000.  
#So cash = $10,000 and positions_value = $0

Pretty strightforward and obvious.

#Now what if we took a short position of XYZ.  
#assuming XYZ sells for $10 and $0 commission and a starting balance of $10,000

#All that means is we sell BEFORE we buy it. So...

order_target_percent(xyz,- 1)  
#Sell increases cash by $10,000 and reduces positions_value by $10,000.  
#So cash = $20,000 and positions_value = $-10,000

#now we buy...

order_target_percent(xyz, 1)  
#Buy reduces cash by $10,000 and increases positions_value by $10,000.  
#So cash = $10,000 and positions_value = $0

Notice that at all times the portfolio_value = $10,000. The amount you needed to 'borrow' will be the difference between the current cash (context.portfolio.cash) and cash you started with (context.portfolio.starting_cash). In the short sale case this would be $20,000-$10,000 = $10,000.

Maybe take a look at this post https://www.quantopian.com/posts/a-few-questions-on-short-selling-and-sell-orders. There are others too. The mechanics of short selling is a question that comes up a lot. You can keep it straight by just remembering the two accounting rules above.

Good luck.

Dan Whitnable

Thanks for the elaborated answer, you really made things very clear.
I think quantopian team should add this type of information to their "getting started tutorials".

Dima,

Glad to help. When live trading with IB you will probably want to monitor context.account.available_funds and context.account.buying_power. Those will show how much settled cash you have and how much IB is willing to let you trade at any given time respectively. See the help docs https://www.quantopian.com/help#api-account.