{"id":38,"date":"2015-11-26T15:54:59","date_gmt":"2015-11-26T14:54:59","guid":{"rendered":"http:\/\/www.nickstricks.net\/wp\/?p=38"},"modified":"2015-12-18T14:40:46","modified_gmt":"2015-12-18T13:40:46","slug":"following-in-the-footsteps-of-following-the-trend","status":"publish","type":"post","link":"https:\/\/nickstricks.net\/wp\/2015\/11\/26\/following-in-the-footsteps-of-following-the-trend\/","title":{"rendered":"Following in the footsteps of Following the Trend"},"content":{"rendered":"<p>I recently read the book &#8220;<a href=\"http:\/\/www.followingthetrend.com\/the-book\/buy-the-book\/\">Following the Trend<\/a>&#8221; by\u00a0<a href=\"http:\/\/www.followingthetrend.com\/about\/\">Andreas Clenow<\/a>. \u00a0It gives explicit details about how to construct a trend following trading strategy on futures\u00a0using simple rules, and explores in some depth the performance over a wide variety of asset classes. \u00a0As a means to learn more about algorithmic trading I thought it would be an interesting exercise to try to reproduce some of the results.<\/p>\n<p>In this post I&#8217;ll just give a summary of what I&#8217;ve tried to do and give some examples of the end results. I&#8217;ll dig a bit deeper into the mechanics and some points of interest at a later date.<\/p>\n<p>The R code is on GitHub <a href=\"https:\/\/github.com\/NickAltmann\/trend\">here<\/a>.<\/p>\n<p>While the book covers a range of concerns you would need to consider when running a trend following trading strategy, such as sizing and asset mix, initially I&#8217;m just going to concentrate on coding up the trend following algorithm itself.<\/p>\n<h3>Time-series Data<\/h3>\n<p>Before thinking of running a back-test you obviously need the time-series data. The Quandl site has a large set of future time-series and it is very easy indeed to load them into R. However, by their very nature future time-series have an expiry date, and to get a full series you need to stitch these together. When it comes to actually executing a strategy you would normally aim to enter into the trade by buying\/shorting the most liquid future in the series at that point. As you continue to hold the position you would then want to roll on the the next future in the sequence as that becomes more liquid. At the point of rolling there may be a basis spread, and you need to account for this when constructing the full series.<\/p>\n<p>There are series available where this has been done for you, but I wanted to be faithful to the description of the algorithm in the book so I implemented it myself. There are numerous candidate rules for deciding when to roll, but the one used in my code and the book is to switch when the open interest (the total open positions) in the later future exceeds the open interest of the current future.<\/p>\n<h3>The Trend Algorithm<\/h3>\n<p>The algorithm makes use of a trend-filter that determines whether the overall direction of the asset price is either up or down. This is done by simply taking two moving averages of the price, one on a short span of say 50 days, and the other on a long span of say 100 days. If the shorter average is above the longer one then the price is in an upward trend, and vice-versa.<\/p>\n<p>It also makes use of the Average True Range (ATR), which acts as a very simple measure of the series volatility. The true range is defined as the difference between the high and low for the day, taking into account the previous day&#8217;s close. The ATR is then the average over a given span of say 50 days.<\/p>\n<p>The algorithm can then be summarised as:<\/p>\n<ul>\n<li>Long trades can only be entered if the trend filter indicates the overall trend is up, and inversely for short trades.<\/li>\n<li>If the criteria above is met, we enter a long trade if the closing price is the highest in the last 50 days; and conversely the lowest close for short trades.<\/li>\n<li>A long position is closed out if the closing price falls 3 ATR units from the highest point reached since entering the trade, with the equivalent applying to short positions.<\/li>\n<\/ul>\n<h3>Running the Code<\/h3>\n<p>Once the code is loaded a simulation can be run as in the example below.<\/p>\n<pre class=\"brush: r; title: ; notranslate\" title=\"\">\r\n# Load CME euro-dollar between 1995 and 2005\r\ned_series = create_future_time_series(1995, 2005, 'CME', 'ED')\r\n\r\n# Run a back-test on this data using the default parameters\r\ned_trades = run_simulation(ed_series$full_series, simulation_parameters())\r\n\r\n# Print out trades and total pnl\r\ned_trades\r\nsum(ed_trades$pnl)\r\n\r\n# Plot the results\r\nplot_trades(ed_series$full, ed_trades, title = &quot;CME\/ED&quot;)\r\n<\/pre>\n<h3>A Sample Result<\/h3>\n<p>Running the euro-dollar example given above gives the output below.<\/p>\n<pre>&gt; ed_trades\r\n   position    date_in settle_in   date_out settle_out    pnl\r\n1         1 1995-05-24    89.333 1995-06-07     89.433  0.020\r\n2         1 1995-11-30    89.583 1996-02-20     89.323 -0.200\r\n3        -1 1996-03-15    89.023 1996-06-27     88.893  0.110\r\n4        -1 1996-07-05    88.653 1996-07-16     88.893 -0.230\r\n5         1 1996-10-01    89.223 1997-01-02     89.333  0.070\r\n6        -1 1997-02-27    89.283 1997-05-09     89.178  0.110\r\n7         1 1997-07-02    89.428 1997-10-10     89.593  0.190\r\n8         1 1997-10-27    89.728 1997-11-14     89.593 -0.240\r\n9         1 1998-01-06    89.763 1998-02-19     89.778  0.020\r\n10        1 1998-08-27    89.810 1998-09-15     90.088  0.222\r\n11        1 1998-09-23    90.273 1998-11-02     90.473  0.225\r\n12       -1 1999-02-12    90.033 1999-03-09     90.123 -0.070\r\n13       -1 1999-06-11    89.803 1999-09-08     89.875 -0.057\r\n14       -1 1999-10-05    89.680 1999-10-29     89.755 -0.050\r\n15       -1 1999-12-20    89.592 2000-02-24     89.519  0.085\r\n16       -1 2000-04-27    89.364 2000-06-02     89.354  0.000\r\n17        1 2000-07-27    89.654 2001-01-12     90.939  1.319\r\n18        1 2001-01-30    91.229 2001-04-16     91.554  0.370\r\n19        1 2001-04-20    91.974 2001-06-28     92.279  0.255\r\n20        1 2001-07-18    92.504 2001-09-04     92.609  0.165\r\n21        1 2001-09-07    92.919 2001-11-16     94.069  1.142\r\n22        1 2001-12-10    94.316 2002-01-24     94.434  0.038\r\n23        1 2002-04-05    94.474 2002-10-11     96.086  1.495\r\n24        1 2002-10-29    96.311 2003-03-13     96.548  0.255\r\n25        1 2003-06-10    96.668 2003-06-25     96.573 -0.140\r\n26        1 2003-11-14    96.658 2003-11-28     96.588 -0.120\r\n27        1 2003-12-05    96.693 2004-01-29     96.708  0.030\r\n28        1 2004-02-11    96.838 2004-04-02     96.788 -0.060\r\n29       -1 2004-06-04    96.253 2004-06-30     96.303 -0.050\r\n30       -1 2004-11-19    96.350 2004-12-08     96.425 -0.115\r\n31       -1 2004-12-16    96.315 2005-04-06     95.910  0.380\r\n32       -1 2005-08-01    95.845 2005-08-31     95.900 -0.057\r\n33       -1 2005-09-12    95.790 2005-12-30     95.225  0.585\r\n\r\n&gt; sum(ed_trades$pnl)\r\n[1] 5.697\r\n<\/pre>\n<p>So we can see we made just over 30 trades during the period and made a profit of 5.7 units. A quick count would show 20 winning trades and 12 losing ones.<\/p>\n<p>However there are almost too many provisos to list here. In particular the algorithm as it stands assumes that a position can be entered or closed using the opening price the day after the signal. This doesn&#8217;t take into account the bid-ask spread or the slippage on the price. Nor are transaction fees factored in. However it does give a basic illustration of the effectiveness of trend detection using a very simple formula.<\/p>\n<p>The plot looks as below. A green triangle is a trade entry, a red triangle an exit. A triangle pointing up is a buy\/cover, pointing down is short\/close. Although the presentation could do with some work you should be able to distinguish some of the key characteristics of this type of algorithmic strategy, namely that you can end up making quite a lot of small loses in a relatively flat market, but you can make relatively bigger gains when strong trends do emerge.<\/p>\n<p><a href=\"http:\/\/www.nickstricks.net\/wp\/wp-content\/uploads\/2015\/11\/ed_trades.jpeg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-47\" src=\"http:\/\/www.nickstricks.net\/wp\/wp-content\/uploads\/2015\/11\/ed_trades-300x263.jpeg\" alt=\"ed_trades\" width=\"300\" height=\"263\" srcset=\"https:\/\/nickstricks.net\/wp\/wp-content\/uploads\/2015\/11\/ed_trades-300x263.jpeg 300w, https:\/\/nickstricks.net\/wp\/wp-content\/uploads\/2015\/11\/ed_trades.jpeg 800w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I recently read the book &#8220;Following the Trend&#8221; by\u00a0Andreas Clenow. \u00a0It gives explicit details about how to construct a trend following trading strategy on futures\u00a0using simple rules, and explores in some depth the performance over a wide variety of asset &hellip; <a href=\"https:\/\/nickstricks.net\/wp\/2015\/11\/26\/following-in-the-footsteps-of-following-the-trend\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","enabled":false},"version":2}},"categories":[4,3],"tags":[],"class_list":["post-38","post","type-post","status-publish","format-standard","hentry","category-finance","category-r"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p77vO4-C","_links":{"self":[{"href":"https:\/\/nickstricks.net\/wp\/wp-json\/wp\/v2\/posts\/38","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/nickstricks.net\/wp\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/nickstricks.net\/wp\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/nickstricks.net\/wp\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/nickstricks.net\/wp\/wp-json\/wp\/v2\/comments?post=38"}],"version-history":[{"count":7,"href":"https:\/\/nickstricks.net\/wp\/wp-json\/wp\/v2\/posts\/38\/revisions"}],"predecessor-version":[{"id":50,"href":"https:\/\/nickstricks.net\/wp\/wp-json\/wp\/v2\/posts\/38\/revisions\/50"}],"wp:attachment":[{"href":"https:\/\/nickstricks.net\/wp\/wp-json\/wp\/v2\/media?parent=38"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nickstricks.net\/wp\/wp-json\/wp\/v2\/categories?post=38"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nickstricks.net\/wp\/wp-json\/wp\/v2\/tags?post=38"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}