Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Ernie Chan's EWA/EWC pair trading

Got this work finally. The strategy is a linear mean reversion strategy pair trading EWA and EWC. The trading used minute data. The earning is quite consistent with the description in Ernie's book.

However, the disappointing thing is that I found it quite difficult to long or short a large amount of EWA or EWC at close price. If I want to long or short more than 50 shares of EWA or EWC each day, I simply can't long or short these shares at the close price of that day. I can't only achieve this earning using $1000 of starting cash and base share of only 20 shares. If I try to use more money I found it difficult to get similar performance because I can't long or short EWA and EWC at the price that I want.

Please comment and advise if you have better ideas to improve this algo. Thanks.

8 responses

Hi Huapu,

Can you explain the strategy? I'm wiling to help you, but I don't have access to the book and don't quite understand your code.

Regards,
Ed

Hello Huapu,

The algorithm seems to rely on the 2008-2009 crash to be profitable. Also, my understanding is that $1K capital is not adequate if you are shorting; you'll need at least $25K, due to regulatory requirements.

Grant

Hello Huapu,

EWA and EWC trade 1M - 1.5M shares each day on average according to Yahoo Finance. You could modify commission and slippage in initialize as shown in this example:

    set_commission(commission.PerShare(cost=0.005))  
    set_slippage(slippage.FixedSlippage(spread=0.00)  

The second line allows you to trade in a backtest irrespective of volume.

P.

Let me explain the algorithm first. The algo is based on pair trading EWA and EWC. I assigned a pre-defined weight to EWA and EWC in the algo in context.evec, which means if I order 0.943 share of EWA, I will order -0.822 share of EWC, and vice versa. Then I look at the combined price of the EWA/EWC pair weighted by their weights in the context.evec, which would be a semi-stationary price. I will have -h*context.unit_shares of the EWA/EWC pair, where h is the z-score of the combined EWA/EWC pair price. So this basically a mean-reversion strategy trading on the spread between EWA and EWC.

The problem I have is that this strategy relies on I can buy EWA/EWC at the price I want. However, when I increase the number of shares that I long/short, the price start to slip. For example, here I increase context.unit_shares to 200, which means I will long/short on the order of 200*z-score shares of EWA/EWC everyday. In this Full Backtest attached, you can see in the Transaction Details that the algo bought 125 shares of EWA at $20.38 at 2006-06-06 10:04:00, 150 shares at $20.34 at 2006-06-06 10:07:00, and 109 shares at $20.20 at 2006-06-06 10:08:00. But really what I meant is to buy (125+150+109) shares at the same price at once. However, if I set context.unit_shares to 20, I don't have the problem because I can buy 30 shares of EWA all at once.

This price slippage problem will basically kill most of pair trading strategy that has a small price spread, if you can't buy enough shares at the price you want. Can you give some suggestions on that? Of course the regulation on minimum short of $25K would kill the algo as well. If I have to short at least $25K, I am sure the price that I short the stock would be all over the place.

Forgot to attach the Full Backtest using context.unit_shares of 200. Here it is. The Transaction Details show the slippage problem.

Hi peter,

Can I ask how to you derive the weights for the two stocks?
Which period of data did you use to derive the weights?
Thanks.

I believe the johansen test is the answer for determining the correct weights... see this blog post for an example https://robotwealth.com/exploring-mean-reversion-and-cointegration-part-2/ and general post for pairs trading http://epchan.blogspot.com/2006/11/cointegration-is-not-same-as.html

@Jianwei Wang

You can use slope of linear regression between EWA and EWC to determine the weight of two stocks. Another approach rightly pointed out above is to use Johansen test's Eigen vectors as weights. More details about these methods can be found in course by Dr. Ernie Chan on Mean Reversion.

A pitfall alert is don't consider full data to determine weight as you will introduce look ahead bias in your back testing results.