Over the last couple of weeks I have been studying AmiBroker’s Formula Language (AFL) like there is no tomorrow. Luckily the "Introduction to AmiBroker" by Howard Bandy (free download) is a great read and AFL has a huge and generous user base, gathered in the AmiBroker User’s List on Yahoo. Last but not least there is Tomasz Janeczko’s extensive UsersGuide with an overwhelming amount of examples.
So this post is all about backtesting. Actually it probably will be the first in a series for presenting some of the backtesting possibilities AmiBroker has to offer. And while we are discovering the AmiBroker Backtester, chances are we might come across some pretty neat portfolio strategies along the way. Stay tuned!
Harvesting Momentum
As stated in previous posts, the "momentum anomaly" is known for centuries. The gist of the momentum anomaly is that assets often continue their price momentum, defined as the change in price over a given lookback period. Momentum works well across asset classes as well as within them. So harvesting momentum really is all about follow the money.
On Seeking Alpha contributor Marc Cohn has published various strategies involving momentum based trading. One of them, "Return Like A Stock, Risk Like A Bond: 15.5% CAGR With 17% Drawdown" utilizes so called "Paired Switching" (see here for SSRN paper) between two exchange traded funds: SPY and TLT. Marc's comprehensive system actually is a TAA strategy in its simplest appearance.
In a nutshell: the strategy is long only and intermittently all capital is re-balanced to the best performing EFT out of just SPY (stocks) and TLT (bonds). Marc applied daily periodicity for portfolio re-balancing, so let's start there too. Later on, periodicity will be expanded to the weekly, monthly and even quarterly time frames. In the end just pick the periodicity that suits your personal trading style or 401(k) limitations best.
Benchmark
The "classical" 40% stocks / 60% bonds buy and hold portfolio is the benchmark to compare the backtest results to. Initial capital is set at $100,000.
Buy & Hold SPY (40%) - TLT (60%) |
Buy & Hold Profit Table |
SPY(40%)-TLT(60%) B&H 2004 - 2013 Total Profit 89 % CAR 6.59 % MDD (trades) -55.19 % MDD (system) -18.46 % Calmar (0%) 0.36 Sharpe (0%) 2.51 (!) Trades 2 Winners 100 %
Daily Periodicity
For a replication of the mentioned paired switching between SPY and TLT the best performing asset is selected based on 85-day momentum, yielding the below results for the full 10 year period 2004 - 2013:
Equity SPY - TLT (85d) |
Profit Table SPY - TLT (85d) |
AmiBroker allows for optimization of a trading system: the process of finding optimal values for one or a set of parameters (giving the highest profit or other metric from the system) for a given symbol (or a portfolio of symbols). Optimizing for x-days sorted on Net Profit (add $100,000 as initial capital to get ending capital) renders the following overview:
SPY-TLT (85d) 2004 - 2013 Total Profit 293 CAR 14.69 % MDD (trades) -17.05 % MDD (system) -22.66 % Calmar (0%) 0.65 Sharpe (0%) 0.90 Trades 71 Winners 57.75 %
3D mountain graphic
Optimizing effectively is data snooping, hence the risk of curve fitting is luring. So consideration, prudence and caution are of the essence here. An optimal value may be a just a lucky pick. To check robustness of found values, the optimizer results can be graphically demonstrated in a 3D mountain graphic. To display a 3D optimization chart, AmiBroker needs two optimizable variables. In this case an average length variable for smoothing price data is appropriate.
SPY-TLT Daily Periodicity |
Best look for a wide mountain plateau instead of single peaks for higher probability on reliable future results or backtest found values on Out-of-Sample periods. Robust settings are regions in the 3D graph that show gradual rather than abrupt changes in the surface plot. Radical changes (or spikes) in the 3D optimization charts show clearly over-optimization areas.
What would happen if SPY was replaced by MDY? The broad and wide plateau is indicative for consistency.
MDY-TLT Daily Periodicity |
MDY-TLT, top down |
Equity MDY - TLT (65d/15d) |
Profit Table MDY - TLT (65d/15d) |
To conclude the exploration of the Daily Periodicity let's take a look at 23 years of historical results starting from 1991. Because there isn't enough historical price data available for the ETF's used until now, a switch to Index Funds is necessary. For stocks: FDVLX and for bonds: VUSTX. Acknowledgment goes to Seeking Alpha contributor "varan" for pointing at this pair.
MDY-TLT (65d/15d) 2004 - 2013 Total Profit 553 CAR 20.64 % MDD (trades) -17.05 % MDD (system) -19.22 % Calmar (0%) 1.07 Sharpe (0%) 1.06 Trades 23 Winners 78.26 %
Equity FDVLX - VUSTX (65d/15d) 1991 - 2013 |
Profit Table FDVLX - VUSTX (65d/15d) 1991 - 2013 |
FDVLX-VUSTX (65d/15d) 1991 - 2013 Total Profit 3,286 % CAR 16.54 % MDD (trades) -14.90 % MDD (system) -17.39 % Calmar (0%) 1.01 Sharpe (0%) 0.95 Trades 64 Winners 79.69 %
23 year comparison between Stocks100%, Stocks40%-Bonds60% and Stocks-Bonds-Switching (1991-2013, log scale) |
System monitoring
Day-to-day monitoring of the strategy at hand works like a breeze through AmiBroker's Explore feature.
Code & Comments
Both the source code for thinkscript as well as for AmiBroker Formula Language are available below. Please
share any thoughts and ideas in the comment section below. Especially
suggestion for lowering maxDD are very much appreciated.
To be continued in Part II and III
In the next posts momentum will be explored at the Weekly, Monthly and Quarterly Periodicity. To lift a little corner of the veil:
Portfolio: CAR: 31%, Calmar: 1.78, Win: 72% |
Care to support?
If you value this post or the free source code, please do consider to show your support. Apart from an investment in AmiBroker of nearly $500 (count VAT in!), preparing a post like the above involves a tremendous amount of time and dedication. A donation by itself already is fabulously motivating, so why not show your support by PayPalling me just 10 or 20 bucks? No registration required. Thanks!
thinkscript
AFL-code
Review the afl-code for optimizable settings below. The same optimizable afl-code or a (non-optimizable) version with user selectable parameters is available for direct download from TrendXplorer's Google Drive.