Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
How to enter "short" order logic into algo?

Hello All,
What would the syntax look like for entering a "short" order for an equity?

For instance, if algo indicates to short aapl when there is currently NO open positions with aapl?

Also would i have to allocate a portion of trading capital for margin in the algo...to accomodate shorting? How would this look like code-wise? Can you provide an example?

6 responses

Hello Adam,

To short AAPL simply order(sid(24), - PositionSize) To cover (close) the short position use exactly the same code since the position size for a short position is stored as a -ve number.

Margin is the least discussed topic on Quantopian at the moment and management of margin is almost non-existent in shared algos. There are some code examples at https://www.quantopian.com/posts/template-limiting-leverage-by-brandon-ogle-and-dan-sandberg and https://www.quantopian.com/posts/a-jump-start-template.

P.

Hi Adam,

Note also that the proceeds from your short sale end up in your cash bucket.

Grant

Peter,
When setting 'context.max_notional' and 'context.min_notional', if i wanted to have a 1:1 relationship with trading capital to margin, would it make sense to set them to the same amount?

For instance:
context.max_notional=1000000.1
context.min_notional=-1000000

This is for a long/short algo.

Hello Adam,

I have to confess i've never understood it and never used it. I see why it is in the sample algo but in a multiple SID long/short algo I've always felt the long and short positions cancelled each other out in the calculation of any notional. Hopefully someone else can advise.

P.

Hello Adam and Peter,

I don't have time now, but there is a better way to handle this. Here's a function that may be of interest:

def capital_invested(context, data):

    # get a sum total of capital spent or borrowed for all current positions  
    capital = 0.0  # initialize to zero

    # check every stock in current positions (also works with set_universe)  
    for stock in context.portfolio.positions:  
        # get amount of shares in current position for this stock  
        amount = context.portfolio.positions[stock].amount  
        # get the cost basis of the shares (how much we spent on average per share)  
        cost_basis = context.portfolio.positions[stock].cost_basis

        # check if position is a short trade (negative amount)  
        amount = max(amount, -amount) # change amount to a positive number  
        # add dollar amount to the 'spent' total  
        capital += amount * cost_basis  
    # return amount of capital tied up in positions  
    return capital  

The max_notional and min_notional can work, but sometimes the algo can run off the tracks if you end up buying/selling large amounts.

Grant

Hello Grant,

Thanks - I'll have a go with that next time.

I've just remembered why I never started to use context.max_notional. I find the ".1" in

context.max_notional=1000000.1  

extraordinary and I can't believe it's necessary to do things like that in 2013.

P.