Back to Community
Equity Long-Short

This is the result of a couple of hours playing around trying to make an equity long-short algorithm. The returns do not survive costs/slippage, and it seems very sensitive to rebalancing frequency, but perhaps it's helpful to someone.

Clone Algorithm
747
Loading...
Backtest from to with initial capital
Total Returns
--
Alpha
--
Beta
--
Sharpe
--
Sortino
--
Max Drawdown
--
Benchmark Returns
--
Volatility
--
Returns 1 Month 3 Month 6 Month 12 Month
Alpha 1 Month 3 Month 6 Month 12 Month
Beta 1 Month 3 Month 6 Month 12 Month
Sharpe 1 Month 3 Month 6 Month 12 Month
Sortino 1 Month 3 Month 6 Month 12 Month
Volatility 1 Month 3 Month 6 Month 12 Month
Max Drawdown 1 Month 3 Month 6 Month 12 Month
# Backtest ID: 55d7c4510971570da2a80584
We have migrated this algorithm to work with a new version of the Quantopian API. The code is different than the original version, but the investment rationale of the algorithm has not changed. We've put everything you need to know here on one page.
There was a runtime error.
17 responses

Simon,

Perhaps you could provide some context:

  1. Is a long-short strategy a standard approach? Skimming over your code, it appears that you end up with a lists of long stocks and short stocks, but for it to be considered a "long-short" strategy by someone skilled in the art, would they be picked in a certain way?
  2. What was your recipe for picking the stocks, and why might it work?

And why the comments "this is so stupid" and "stupid" in the code?

Grant

  1. Yes, it's the original "hedge fund" approach. The magic is in how you pick them, and here I've not put much effort into that. People spend their lives working on that.
  2. Just best and worst of the "acquirer's multiple". It might work if the "value" effect is real, but even if it is, probably needs more refinement to isolate vs all the noise.

re: stupid, I forgot I left that in - every time I get a code exception because I placed an order for a stock that wasn't yet in data[] I bonk my head against the wall.

Simon.

Hey Grant, hey Simon.

The upcoming lecture and webinar will cover factor modeling for long-short equity. Long-short equity is quite common in my understanding, and factor modeling is a very common way to pick the longs and shorts -- come up with a fundamental factor model, much like Simon did here, and then use that to predict future returns and rank stocks. You can sign up for the webinar here, it will also eventually become available on the lectures page. This also all interfaces nicely with our upcoming FFC feature set, which will allow factor modeling.

Long-short equity does tend to be a very high minimum capacity strategy, but also a very high max capacity strategy. You will be destroyed by trading fees with a small amount of capital, but can successfully trade factor model long-short strategies up to tens of millions of dollars or potentially more.

I just finished attending the European Financial Association annual meeting, and am currently experiencing a little bit of factor model overload :)

Disclaimer

The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by Quantopian. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. No information contained herein should be regarded as a suggestion to engage in or refrain from any investment-related course of action as none of Quantopian nor any of its affiliates is undertaking to provide investment advice, act as an adviser to any plan or entity subject to the Employee Retirement Income Security Act of 1974, as amended, individual retirement account or individual retirement annuity, or give advice in a fiduciary capacity with respect to the materials presented herein. If you are an individual retirement or other investor, contact your financial advisor or other fiduciary unrelated to Quantopian about whether any given investment idea, strategy, product or service described herein may be appropriate for your circumstances. All investments involve risk, including loss of principal. Quantopian makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances.

Also nice algorithm, Simon. We definitely want to see more factor modeling and long-short principle driven strategies in the community as we set up the fund, so this comes at a great time.

So what is the time scale for your typical long-short strategy? How often would it trade?

And is it basically "Hmm? I think these M stocks will go up, and these N stocks will go down. I'll put $10M long on the M, and $10M short on the N. And maybe something in an ETF, to achieve perfect market neutrality."

Long-short equity does tend to be a very high minimum capacity strategy

So how is a contest at $100K capital gonna work? It sounds like it needs to be $25M and 50-100 stocks or so, no?

Passes the “anti-fragile” test. The algo returns +20.2% against SPY @ -53.1%: peak-trough for the sub-prime crisis (Oct 7, 2007 to Mar 2, 2009). Nice. Since hedge funds aren't the best model to emulate, a risk rotation model that limits the universe to SPY and IEF might be worth considering.

I'm not really sure what you are trying to accomplish, this algo was designed to create a beta-neutral long-short portfolio of stocks based on some fundamental criteria - SPY nor IEF have EBIT nor EV... trading those two alone would require some completely different algo, I would think?

@Simon Appreciate your input. Yeah, I think you’re right. My objective was simply to see if your algorithm could translate into a simple SPY IEF market neutral strategy. Bad idea, I guess.

(Whoops, what happened to my post?)

Cool. Thank you for sharing, Simon. I can only wonder why I've never noticed pandas' smoothened var and covar functions before. I pinned it on the wiki.

Did you have time to look into a sector-neutral version of the strategy? Issues with metrics like P/S and financials come to mind.

You mean where you allocate to the top and bottom decile of each sector, beta-neutralized vs each sector ETF? That would probably work well.

EDIT: It does work well.

Responding to your points, Grant:

Time scale is another part of the art I would say. I think you would need to do a robust parameter optimization looking at returns vs. trading costs as you varied the rebalancing frequency. David Edwards showed some of these approaches in this webinar as I recall. More frequent rebalancing will eat more transaction costs, but may be more responsive to market changes. As a rule of thumb I think weekly to monthly sounds about right.

Your intuition here is largely right. What it is doing is rather than betting on stocks in M to go up, or stocks in N to go down, you are betting on the predictive quality of your ranking. An equal dollar-volume long-short portfolio will make money based on r_M - r_N, where r_M is the returns of M and r_N is the returns of N. You make money when your ranking successfully predicts for increased returns, regardless of whether r_M and r_N are positive or negative. The spearman correlation lecture has an example of determining the predictive capacity of some ranking scheme.

The profitability of a strategy is determined by excess returns - trading costs. If you had a particularly good ranking scheme or incurred few enough trading costs due to infrequent rebalancing, then you might be able to run a long-short on $100,000. However, I suspect yes by and large most long-short strategies will not be profitable on $100,000. That said, you can submit your algorithm for consideration for the manager's program even if it's not appropriate for the contest. We understand that the contest is far from the be-all and end-all of which algorithms are good, and we are actively looking for algorithms outside those that do well in the contest. We also run contest submissions over larger capital allocations to check for these cases. You can you can always let us know about a strategy you'd like us to consider at [email protected], and we'll definitely take a look.

I ran the algo above with the default slippage and commission models, by commenting out these lines:

    # set_slippage(slippage.FixedSlippage(spread=0.00))  
    # set_commission(commission.PerShare(cost=0, min_trade_cost=None))  

and $1M in capital.

Kinda went bonkers. Is this expected?

Clone Algorithm
5
Loading...
Backtest from to with initial capital
Total Returns
--
Alpha
--
Beta
--
Sharpe
--
Sortino
--
Max Drawdown
--
Benchmark Returns
--
Volatility
--
Returns 1 Month 3 Month 6 Month 12 Month
Alpha 1 Month 3 Month 6 Month 12 Month
Beta 1 Month 3 Month 6 Month 12 Month
Sharpe 1 Month 3 Month 6 Month 12 Month
Sortino 1 Month 3 Month 6 Month 12 Month
Volatility 1 Month 3 Month 6 Month 12 Month
Max Drawdown 1 Month 3 Month 6 Month 12 Month
# Backtest ID: 56c8c8119792550de16e089f
There was a runtime error.

It seems like there are some issues backtesting over 2008 due to security delistings and therefore strange effects on leverage. It's something I will look into addressing. This falls into the 'execution' category, so the ordering logic could probably be a little smarter.

Here's an example of a long-short algorithm that survives transactions costs. It uses the following rules:

1) Trades only on Wednesdays
2) Risk based position sizing, with risk resizing each week
3) Universe is top 500 stocks by volume (includes ETFs and other weird stuff)
4) Sorts by 6 months' momentum (price / SMA over 126 days)
5) Top 100 are long pool, bottom 100 are short pool
6) Buy/short from top/bottom of list then hold each position until they leave the relevant pool
7) Buy/short as many positions as cash available, given the position sizing (long up to 100% of account value, short to -100%)

I've taken lots of ideas from Andreas Clenow's recent book, "Stocks on the Move", but reduced the weighting rules down to just the 6 month's momentum. I really liked his positioning sizing techniques and tricks to reduce turnover.

I am new to Q/Python, so please give me feedback on the code as well as the strategy. I was determined to use Pipeline, and NO for-loops.

Clone Algorithm
146
Loading...
Backtest from to with initial capital
Total Returns
--
Alpha
--
Beta
--
Sharpe
--
Sortino
--
Max Drawdown
--
Benchmark Returns
--
Volatility
--
Returns 1 Month 3 Month 6 Month 12 Month
Alpha 1 Month 3 Month 6 Month 12 Month
Beta 1 Month 3 Month 6 Month 12 Month
Sharpe 1 Month 3 Month 6 Month 12 Month
Sortino 1 Month 3 Month 6 Month 12 Month
Volatility 1 Month 3 Month 6 Month 12 Month
Max Drawdown 1 Month 3 Month 6 Month 12 Month
# Backtest ID: 56d4bd86ce5af00f1cec7e53
There was a runtime error.

Here's the same strategy, but "equitised". It holds the cash rebate from the shorts in SPY, thus layering the normal equity return onto the long-short return. I think you would normally want to use stock index futures, rather than SPY, as this would keep more cash handy. It's super volatile, but I guess the point is to show long-short plays nicely with equities. I suspect I would need to use half the long-short to SPY, to maximise the combined Sharpe.

Clone Algorithm
146
Loading...
Backtest from to with initial capital
Total Returns
--
Alpha
--
Beta
--
Sharpe
--
Sortino
--
Max Drawdown
--
Benchmark Returns
--
Volatility
--
Returns 1 Month 3 Month 6 Month 12 Month
Alpha 1 Month 3 Month 6 Month 12 Month
Beta 1 Month 3 Month 6 Month 12 Month
Sharpe 1 Month 3 Month 6 Month 12 Month
Sortino 1 Month 3 Month 6 Month 12 Month
Volatility 1 Month 3 Month 6 Month 12 Month
Max Drawdown 1 Month 3 Month 6 Month 12 Month
# Backtest ID: 56d465e552d39b0dfb41154c
There was a runtime error.

Here's the vanilla long-short strategy again, this time with an absolute momentum filter. Long pool is only stocks with momentum > 0, and vice versa for shorts. It doesn't make much difference to the stats. The only divergence I can see from the original is a reduction in longs in late 2008, because most stocks will have been in decline. There was plenty to short however, so the system would have lost its market neutrality for that period. I don't think the complication and added risk is worth it.

Clone Algorithm
146
Loading...
Backtest from to with initial capital
Total Returns
--
Alpha
--
Beta
--
Sharpe
--
Sortino
--
Max Drawdown
--
Benchmark Returns
--
Volatility
--
Returns 1 Month 3 Month 6 Month 12 Month
Alpha 1 Month 3 Month 6 Month 12 Month
Beta 1 Month 3 Month 6 Month 12 Month
Sharpe 1 Month 3 Month 6 Month 12 Month
Sortino 1 Month 3 Month 6 Month 12 Month
Volatility 1 Month 3 Month 6 Month 12 Month
Max Drawdown 1 Month 3 Month 6 Month 12 Month
# Backtest ID: 56d54f53ce47e50de46f3e7b
There was a runtime error.

Hi Burrito Dan,

Thanks for the "Rebalance Threshold"part of this.

For me as a "python newbie" (and very unskilled with it) this is exactly what I wanted to add to my own code but couldn't figure out how to implement successfully. Many thanks indeed.

Cheers, best wishes,
Tony