Back to Community
Ernie Chan (USO/GLD) Oil/Gold Pair Spread Trading at Constant Leverage

*** NOTE: I posted this yesterday, but accidentally deleted it when editing it to provide a small revision to the algo code**

This algo attempts to implement the GLD/USO pair trading algo from Ernie Chan's book.

Additionally, I place the trades under the constraint of always opening positions whereby my resulting portfolio leverage at trade open never exceeds 1.0 (ie: I don't go on margin) and I'm fully invested whenever I open a trade based upon whatever my current portfolio value is.

In accounting terms: long_exposure + abs(short_exposure) = portfolio_value

This was accomplished using a short helper function I baked into the algo called computeHoldingsPct() which does all the normalizing and returns the long and short holdings amount as percentages based on my current portfolio value when placing the orders. I've also setup the algo to record the % exposures in both GLD and USO whenever a trade is placed for later investigation in case I want to investigate some of the drawdowns that seem to have come up once in a while. What I like about implementing a pairs strategy in this manner, by using constant leverage, is that it seems to smooth out my equity curve.

Would love to hear folks' thoughts to see if whether approaching a pairs trading strategy in this manner is interesting, or if perhaps my approach can be further improved.

UPDATE:
This updated algo (seen here. If you cloned the original, you should grab this new one instead) addresses very minor issues I discovered in the original. Additionally, I reduced the backtest dates so as to better view the daily values for performance and recorded values that display leverage and % equity invested in each stock of the pair trade. Leverage was chosen to be 1.0 upon trade entry, but since I'm not rebalancing the portfolio daily (churning the portfolio excessively and accruing excess transaction costs would be detrimental for a strategy like this), leverage oscillates a bit up or down depending on how the strategy performs after trade entry. Then at next trade entry positions are taken once again at 1.0 leverage.

Clone Algorithm
575
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: 54b1cbdadb51056d77506bf6
There was a runtime error.
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.

5 responses

How can one use this algo for stat arb?

I wrote a small script that applies Engle-Granger test on pairs of stocks in similar industry and, if the spread is stationary - computes half-life of mean reversion.

SWN  
RRC  
(-4.0305413154958369, 0.0064930159714686308, array([-3.44344373, -2.86731469, -2.56984569]))
12.4479728826  

P-value seems great, half-life is okay, but I haven't had any luck with it

Is there anything I am doing horribly wrong?

Clone Algorithm
17
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: 5503f264fddc940e3ac59b89
There was a runtime error.

Hi Evgeny,
Yes, my example would be an example of a stat arb strategy. Your approach using the Engle-Granger test is also a great approach. You may also want to re-compute the Engle-Granger in the algorithm prior to every trade being made, and only take trades if the p-value from the test is acceptable using the data available up to that point.

Another point is simply that not all pairs of stocks from similar industries have co-integrated timeseries that are also profitable, even though the p-value from the stationarity test and the half-life calculation seeming reasonable. As well, each pair of stocks may require different lookback windows with which to compute the spread in a way that generates a profitable strategy. It definitely takes quite a while experimenting with various combinations of input parameters, and testing for robustness across small variations in these inputs, in order to discover good pairs trading strategies. As well, some pairs will mean-revert better from more extreme spreads of 2.0 standard devs and exiting at 1.0 standard devs, and other spreads result in better profits when trading less extreme mean reversions of say 1.0 standard devs and exiting at 0.0. Good luck on your research!

Hello Justin, I am not a programmer, thank you for sharing the code. Would it be complicated to program it in a way that it is easy to try it with other symbols? thank you.

Can I pay someone to do it?

Douglas,
let me see if i can come up with a generalized algo to accomplish this and try to post it.

-Justin