tag:blogger.com,1999:blog-35364652.post1591444652166972694..comments2022-05-25T08:54:23.376-04:00Comments on Quantitative Trading: Reader suggested a possible trading strategy with the GLD - GDX spreadErnie Chanhttp://www.blogger.com/profile/02747099358519893177noreply@blogger.comBlogger27125tag:blogger.com,1999:blog-35364652.post-10729123913962346102021-12-31T09:16:54.052-05:002021-12-31T09:16:54.052-05:00Hi Michael,
Yes, your understanding is correct. Wh...Hi Michael,<br />Yes, your understanding is correct. When we short a spread, we short GLD and long GDX. The first column in the df is for GLD, and so we apply -1 to it. (We distinguish long and short positions in the spread by _Long vs _Short, but a negative position in either case denotes a short position.)<br />Hope this helps!<br />ErnieErnie Chanhttps://www.blogger.com/profile/02747099358519893177noreply@blogger.comtag:blogger.com,1999:blog-35364652.post-89655753126848871192021-12-29T16:26:09.102-05:002021-12-29T16:26:09.102-05:00Hi Ernest,
Thank you for generously sharing your t...Hi Ernest,<br />Thank you for generously sharing your time and knowledge in writing such an instructive book.<br /><br />I have a question on Example 3.6 Pairs Trading of GLD and GDX in the 2nd edition of "Quantitative Trading". Specifically, the positions to take in the python code provided<br />df.loc[df.zscore>=2, ('positions_GLD_Short', 'positions_GDX_Short')]=[-1, 1] # Short spread <br />df.loc[df.zscore<=-2, ('positions_GLD_Long', 'positions_GDX_Long')]=[1, -1] # Buy spread<br /><br />Where spread is defined by<br />spread = GLD - hedgeRatio*GDX<br /><br />My understanding is that to short the spread, I need to short GLD and long GDX, and to buy the spread through long GLD and short GDX. Is that correct? <br /><br />If my understanding is not flawed, I would have expected the code to be<br />df.loc[df.zscore>=2, ('positions_GLD_Short', 'positions_GDX_Long')]=[1, 1] # Short spread <br />df.loc[df.zscore<=-2, ('positions_GLD_Long', 'positions_GDX_Short')]=[1, 1] # Buy spread<br /><br />Clearly I am missing something here as the Matlab, Python and R codes are consistent. Could you please advise? Thank you!Michael Lamhttps://www.blogger.com/profile/03357146421284084473noreply@blogger.comtag:blogger.com,1999:blog-35364652.post-85284987761268087432016-08-30T10:34:22.812-04:002016-08-30T10:34:22.812-04:00Hi David,
Thank you for your kind words.
Yes, ther...Hi David,<br />Thank you for your kind words.<br />Yes, there is a chapter on AI in my 3rd book.<br />The book is called "Machine Trader", and it is due out in 2017 Q1.<br />ErnieErnie Chanhttps://www.blogger.com/profile/02747099358519893177noreply@blogger.comtag:blogger.com,1999:blog-35364652.post-80771044191378248632016-08-30T05:41:51.220-04:002016-08-30T05:41:51.220-04:00Hi Ernie,
I've been reading your book recently...Hi Ernie,<br />I've been reading your book recently to learn to become a independent algo trader. It really helps me get to know quant trading quickly. Thanks for your knowledge and experience in the book. <br />I'm looking forward to your 3rd book (you mentioned it in Quora and said that it will have some A.I. discussions), since I am aiming to build my trading strategy based on A.I..<br /><br />Cheers,<br />DavidDavid C.https://www.blogger.com/profile/14006699839433067205noreply@blogger.comtag:blogger.com,1999:blog-35364652.post-83876837707911177462016-07-28T10:38:16.497-04:002016-07-28T10:38:16.497-04:00Hi Lucas,
As I have described numerous GLD-GDX str...Hi Lucas,<br />As I have described numerous GLD-GDX strategies in the past, please specify exactly which book and which example you are referring to.<br />Thanks,<br />ErnieErnie Chanhttps://www.blogger.com/profile/02747099358519893177noreply@blogger.comtag:blogger.com,1999:blog-35364652.post-53116198471529285952016-07-28T10:18:52.774-04:002016-07-28T10:18:52.774-04:00Hi Ernie :
I used your code in your book to backt...Hi Ernie :<br /><br />I used your code in your book to backtest the GLD-GDX pair trading. But I got a different sharpe ratio of trainset, which is 0.7856(your book is 2.3). I think the code that I write based on your book is correct. So I wonder maybe the date we used are not the same. I used the price of GLAD and GDX from 5/23/06 to 11/30/07, 385 daily price, are these data you used in the example in the book?<br /><br /><br />Thank you!<br /><br />LucasAnonymoushttps://www.blogger.com/profile/17588311638627080939noreply@blogger.comtag:blogger.com,1999:blog-35364652.post-25069338334106672182016-01-10T14:10:05.084-05:002016-01-10T14:10:05.084-05:00Hi Ernie,
Thank you very much.
JuanjoHi Ernie,<br /><br />Thank you very much.<br /><br />JuanjoJuanjohttps://www.blogger.com/profile/08962061712884688923noreply@blogger.comtag:blogger.com,1999:blog-35364652.post-36330432331982012712016-01-04T11:03:17.800-05:002016-01-04T11:03:17.800-05:00Hi Juanjo,
The code on my website sometimes get up...Hi Juanjo,<br />The code on my website sometimes get updated (maybe due to bugs discovered, or other improvements.)<br /><br />No, I haven't published any revised edition of my books.<br /><br />ErnieErnie Chanhttps://www.blogger.com/profile/02747099358519893177noreply@blogger.comtag:blogger.com,1999:blog-35364652.post-54386848723092066762016-01-04T10:53:06.785-05:002016-01-04T10:53:06.785-05:00Hi Ernie,
I have been "debugging" my co...Hi Ernie,<br /><br />I have been "debugging" my code and there is a mistake I was stubbornly doing over and over again: I didn't assign positions back after calling to fillMissingData. The code in the book was part in a comment and that got me wrong. I should have read more carefully.<br /><br />I am now debugging the second part (to detect the look-ahead bias). The code in the website is far more "complicated" than the one suggested in the book. Is there any new revision of the book I'm not aware of?<br /><br />Kindest Regards,<br /><br />JJJuanjohttps://www.blogger.com/profile/08962061712884688923noreply@blogger.comtag:blogger.com,1999:blog-35364652.post-71505837177802270572015-12-31T08:42:23.900-05:002015-12-31T08:42:23.900-05:00Hi Amine,
1) Indeed GDX vs GLD sometimes falls out...Hi Amine,<br />1) Indeed GDX vs GLD sometimes falls out of cointegration, and your test period may be one of those regimes. <br />But as you observed, even though a pair may not be strictly cointegrating, it can still mean-revert to some extent. You can apply a short term mean reversion strategy to see if it is profitable.<br /><br />2) AUDCAD is a readily available for trading as one instrument on many FX broker. Just buy low and sell high and vice versa.<br /><br />ErnieErnie Chanhttps://www.blogger.com/profile/02747099358519893177noreply@blogger.comtag:blogger.com,1999:blog-35364652.post-25092487636182343042015-12-31T08:39:37.131-05:002015-12-31T08:39:37.131-05:00Hi Juanjo,
Thank you for your compliments.
Yes, yo...Hi Juanjo,<br />Thank you for your compliments.<br />Yes, you should manually set the first row of the position array to zero, before applying the fillMissingData function.<br />ErnieErnie Chanhttps://www.blogger.com/profile/02747099358519893177noreply@blogger.comtag:blogger.com,1999:blog-35364652.post-87469447542871899472015-12-31T00:37:54.140-05:002015-12-31T00:37:54.140-05:00Thanks for the reply,
I have two questions :
1...Thanks for the reply,<br /><br />I have two questions :<br /><br /> <b> 1/ </b>I read the page p126-133 again and I did the cointegration test on the pair GDX,GLD using data from 01/01/2014 to 29/12/2015. Below you can see my results<br /><br /> Augmented DF test for co-integration variables: GLD2Y,GDX2Y <br />CADF t-statistic # of lags AR(1) estimate <br /> -3.12366596 1 -0.046204 <br /><br /> 1% Crit Value 5% Crit Value 10% Crit Value <br /> -3.880 -3.359 -3.038 <br /><br />hedgeRatio =<br /><br /> 5.6534<br /><br />--------------------------------------------------------------<br /><br />As you can see they are not cointgrated up to 90% chance. By drawing the residus it shows that the time series is not stationnary.<br /><br />However, I tried spread= (dailyret(GDX)-dailyret(GLD)) without any regressions or cointegration test ... the spreadseries look meenreverting and stationnary (around 0).<br /><br />What do you think of this approach?<br /><br /> <b> 2/ </b> you mentionned in page 133 : For example, the Canadian<br />dollar/Australian dollar (CAD/AUD) cross-currency rate is quite<br />stationary. how can I trade the spread? I did not get the point. should I be long CAD/AUD and short AUD/USD for example? could you explain me more pleaseAnonymoushttps://www.blogger.com/profile/13000821606740626804noreply@blogger.comtag:blogger.com,1999:blog-35364652.post-926048092702934282015-12-30T13:44:41.018-05:002015-12-30T13:44:41.018-05:00I'm also with the good comments on the book Mr...I'm also with the good comments on the book Mr Chan. I appreciate your effort in putting together such a good piece of knowledge (excuse my English as it is not my mother tongue).<br /><br />I'm having troubles with program example3_6.m. I have downloaded GLD and GDX series from Yahoo Finance, check whether there are NaNs around but I still get values for the Sharpe Ratio in the test and in the training set NaN. I have checked the code but I don't know where the problem may be.<br /><br />Trying to debug the code I have come across that positions is pass to fillMissingData with row 1 (both columns) equal to NaN, so the result is NaN. Could this be the problem? Why are these values NaN in the first place?<br /><br />Thanks in advance and have a nice Xmas and a my best wishes for the next year!<br /><br />JJJuanjohttps://www.blogger.com/profile/08962061712884688923noreply@blogger.comtag:blogger.com,1999:blog-35364652.post-82080898902543347912015-12-30T08:03:23.997-05:002015-12-30T08:03:23.997-05:00Hi Amine,
Thank you for your kind words.
A cointeg...Hi Amine,<br />Thank you for your kind words.<br />A cointegrated pair of price series means that their price spread will eventually mean revert, but we don't know exactly when. If you look at returns of a fixed lookback, you are forced to look for mean reversion of a fixed timeframe. Please see p.126-133 of my book Quantitative Trading.<br />ErnieErnie Chanhttps://www.blogger.com/profile/02747099358519893177noreply@blogger.comtag:blogger.com,1999:blog-35364652.post-15137210166086765722015-12-30T00:27:32.521-05:002015-12-30T00:27:32.521-05:00First of all I want to thank you for this precious...First of all I want to thank you for this precious book. It is really well written and it is among my bests until now.<br /><br />I have a question regarding the pair strategy spread : Why are you regressing prices on each other and then checking the spread? Isnt better the calculate the daily return and then check the absolute spread, and finally apply the same strategy as you propose (using the right parameters). Or we can make a regression of the returns and not the prices?<br /><br />Amine,Anonymoushttps://www.blogger.com/profile/13000821606740626804noreply@blogger.comtag:blogger.com,1999:blog-35364652.post-85397461196549272982015-05-26T08:44:29.552-04:002015-05-26T08:44:29.552-04:00Hi JPS,
You can easily compute the average and sta...Hi JPS,<br />You can easily compute the average and standard deviation of daily returns in Excel. Taking their ratio and multiplying by sqrt(252) gives you the annualized Sharpe ratio, assuming 0 risk free rate.<br />ErnieErnie Chanhttps://www.blogger.com/profile/02747099358519893177noreply@blogger.comtag:blogger.com,1999:blog-35364652.post-28930076676735546042015-05-26T08:44:29.221-04:002015-05-26T08:44:29.221-04:00Hi JPS,
You can easily compute the average and sta...Hi JPS,<br />You can easily compute the average and standard deviation of daily returns in Excel. Taking their ratio and multiplying by sqrt(252) gives you the annualized Sharpe ratio, assuming 0 risk free rate.<br />ErnieErnie Chanhttps://www.blogger.com/profile/02747099358519893177noreply@blogger.comtag:blogger.com,1999:blog-35364652.post-4549231130604605652015-05-26T04:55:30.495-04:002015-05-26T04:55:30.495-04:00Dear Arnie,
in the book quantitative trading you c...Dear Arnie,<br />in the book quantitative trading you calculate the sharp ration for the spread ( for the training set )as<br /><br /> sqrt(252)*mean(pnl(trainset(2:end))).<br />/std(pnl(trainset(2:end)))<br /><br />I am not using matlab ( as I dont have the software), how can I use the Sharp ratio for the spread which in excel is in the form of ln(x)-0.78765*ln(y) where x and y are the future prices of the two scripts.JPShttps://www.blogger.com/profile/18108911426235042361noreply@blogger.comtag:blogger.com,1999:blog-35364652.post-21504395010337170172014-07-04T11:55:21.554-04:002014-07-04T11:55:21.554-04:00Hi Lu,
Did you use the same periods for training a...Hi Lu,<br />Did you use the same periods for training and testing using Yahoo data?<br /><br />Your Sharpe ratios sound too good to be true.<br /><br />When you load my .xls files into Matlab, make sure your price arrays do not have NaN.<br /><br />ErnieErnie Chanhttps://www.blogger.com/profile/02747099358519893177noreply@blogger.comtag:blogger.com,1999:blog-35364652.post-77679370245266572512014-07-04T11:37:43.748-04:002014-07-04T11:37:43.748-04:00Hi Ernie,
When I took the GDX.xls and GLD.xls file...Hi Ernie,<br />When I took the GDX.xls and GLD.xls files from http://epchan.com/book/ I got same error as anonymous. However, when I took the files straight from Yahoo Finance I got no NaN result. But I got 3.0 and 5.6 for test and training sharpe ratios respectively, results which did not match what you suggested in your matlab code for example 3.6<br /><br />Cheers,<br />ManuelAnonymoushttps://www.blogger.com/profile/10435467874709800340noreply@blogger.comtag:blogger.com,1999:blog-35364652.post-5990764329229164192012-08-21T07:47:35.561-04:002012-08-21T07:47:35.561-04:00Hi Anon,
Mostly likely you have included non-numer...Hi Anon,<br />Mostly likely you have included non-numeric data in your input.<br />ErnieErnie Chanhttps://www.blogger.com/profile/02747099358519893177noreply@blogger.comtag:blogger.com,1999:blog-35364652.post-10406274219559692682012-08-20T22:14:25.276-04:002012-08-20T22:14:25.276-04:00Hi Ernie,
I've been trying to use the backtes...Hi Ernie, <br />I've been trying to use the backtesting code for cointegrating pairs, and have been using it in conjunction with scraping data from yahoo, for some pairs that I backtest on (most) the sharpetrainset comes up as NaN. any idea whats going on?<br /><br />CheersAnonymousnoreply@blogger.comtag:blogger.com,1999:blog-35364652.post-24172893890010605792011-01-20T08:02:02.820-05:002011-01-20T08:02:02.820-05:00Hi Art,
The fillMissingData function is available ...Hi Art,<br />The fillMissingData function is available for download from epchan.com/book. See last paragraph of page 34 of my book for password.<br /><br />It simply copies the previous bar's value (price? position? whatever?) to the current bar *if* the current bar's value is NaN.<br /><br />ErnieErnie Chanhttps://www.blogger.com/profile/02747099358519893177noreply@blogger.comtag:blogger.com,1999:blog-35364652.post-83155653029428126382011-01-19T19:55:47.464-05:002011-01-19T19:55:47.464-05:00Sir,
I am newbie to algo trading.Currently I try ...Sir,<br /><br />I am newbie to algo trading.Currently I try to follow your book,now I have some questions.<br /><br />According to example 3.6<br /><br />% ensure existing positions are carried forward<br />unless there is an exit signal positions=<br />fillMissingData(positions);<br /><br />I am not really understand how to carry forward those positions and also I can't find the fillMissingData function in spatial-econometrics.com<br /><br />Thank you very much for your suggestion.<br />ArtUnknownhttps://www.blogger.com/profile/11468120073474066075noreply@blogger.comtag:blogger.com,1999:blog-35364652.post-69157241287165867512006-11-27T08:27:00.000-05:002006-11-27T08:27:00.000-05:00I should add that the standard deviation of GOLD-G...I should add that the standard deviation of GOLD-GDM is $207, not $125 based on the short history of GLD-GDX. So following Mr. Hansen's suggestion of 2xstddev, the entry point would be $414.Ernie Chanhttps://www.blogger.com/profile/02747099358519893177noreply@blogger.com