Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
A Few Questions on Short Selling and Sell Orders

Hi everybody, I am very curious to know how to sell short and to cover the short position as there currently seems to be no reference on how to do so. I also would like to know how Quantopian's backtester uses leverage and how to customize leverage settings. On a different note, I would like to know how to prevent the algorithm to place endless sell orders for 0 shares for stocks that aren't even in your portfolio. Here is an example:

if current_price <= buy_long_band and cash > number_of_shares and notional < context.max_notional:  
            order(stock, +number_of_shares)  
            log.debug("Buying long" + str(stock))  
        elif current_price >= average_band:  
            order_target(stock, 0)  
            log.debug("Selling long" + str(stock))  

Whenever I run this, the backtester places endless sell orders on stocks I didn't previously buy. Help is greatly appreciated. Thanks!

Owen

12 responses

You answered your own question; to sell short, you place sell orders for stock you haven't bought. Your second order method is trying to sell short 0 shares for all those stocks.

If you want to only be closing positions using that branch, you need to check yourself if you already have positions. And it's made more tricky by the fact that you might have placed orders to buy but which haven't filled by the time you want to sell. So you gotta account for all those possibilities.

it would be interesting if there's a sample for short sale strategy and how quantopian audit its cash wheather its... net positive or net negative since you will sell at a high price and buy at lows.... puzzled.....

John could you please elaborate, what exactly is your question?

my question Simon.. is when you sell short.... just borrowing securities... and selling it at a high price then later on..buy to cover at low price.. how will quantopian compute your cash balance for that is it net positive or net negative..? since your... taking... a negative position....;)

You gain the cash. Quantopian doesn't have a margin calculator.

John, imagine you sell short 10 'stock' at 20$ each, the following happens:

  • context.portfolio.cash is increased by the amount of the sell (20$ x 10 = 200$). If cash was 1000 now is 1200
  • context.portfolio.positions['stock'].amount is negative (-10) to indicate a debit that has to be paid back eventually
  • context.portfolio.positions_value is negative and calculated at each handle_event by the current value of 'stock'. If after some time the value of 'stock is 18$, then positions_value is -180$ (- 10 x 18$)
  • context.portfolio.portfolio_value is: context.portfolio.cash + context.portfolio.positions_value that is 1200-180=1020

I'm trying to understand this as well. Is the actual short transaction a naked put option? If so, how do you know when it expires. If it is not an option, then what is it and how would you compute time value and commission and such?

It is selling shares you don't have.

Long you buy shares then sell them.
Short you sell shares then buy them.

I see. Is there a specific time that you need to return (or buy back) the sold shares?

Not specifically, but technically they could be called in at any time, forcing you to cover. There's also a loan fee you must pay, usually nominal but sometimes quite high.

Samson lets say you use TD Ameritrade as your broker. Then when you go to short Apple, you are actually borrowing shares of Apple that TD Ameritrade owns and selling them. You have to have a margin account (not cash account) to do so. Once you cover your short (hopefully at a lower price) you buy the shares back and "return them" to TD Ameritrade.

I see that makes a lot more sense now. Thanks for the info guys!