The continuing evolution of technical analysis software has simplified the creation of computer-automated trading systems. Some systems just generate the signals for the trader to follow, while others place the trades into the market on behalf of the trader. However, being able to program your favorite trading platform is just the beginning. You must have a framework for testing your trading theories to be sure that profitable backtests are not merely because of luck, but are the results of robust modeling of a market’s behavior.
This series of articles will present a simplified approach to developing a trading system for the retail forex market. The system development tool we’ll use will be MetaTrader 4 (MT4), although the ideas and process presented apply to a wide range of software platforms. The methodology will cover general concepts targeted to the beginning system trader. When we take shortcuts for expediency, we’ll refer the reader to additional resources for more in-depth information.
There are five distinct phases in trading system development:
Phase 1: Developing the market model and the basic automated system — the basic automated system implements this model but does not incorporate stop losses or profit targets. The basic system is for the sole purpose of collecting data for statistical analysis used in the later development phases.
Phase 2: Risk management — the initial stop loss (ISL). Using the data gathered in Phase 1 and based on the statistical analysis of that data, we add an ISL to the trading strategy. We use optimization to find a stop loss parameter that suits our needs. We will use walk-forward analysis to test this version of the system.
Phase 3: Profit management — the profit target (PT). As in Phase 2, we will use the statistical analysis of our data to incorporate a profit target into the system. Again, we will use optimization to find an appropriate profit target and then use walk-forward analysis to test this version of the system.
Phase 4: Money management — the trade size algorithm (TSA). This phase does not depend on the data collected in Phase 1. Instead, we will incorporate the popular fixed-fraction trade size method to determine how many lots are allocated to each trade. Popular trade literature is replete with advice to restrict per-trade risk within a range from 1% to 3% of account equity. We will run our optimization using those percentages, and then once again use walk-forward analysis to test this version of the system.
Taken together, Phases 2 through 4 comprise trade management, but there is one more critical step:
Phase 5: Monte Carlo analysis — many traders stop after Phase 4. However, our testing is not complete at that juncture and the system is not ready for deployment (assuming it is profitable). Despite our walk-forward analysis, we cannot be sure that our results are not because of luck. In other words, our model may not describe market behavior accurately; favorable results may have benefited from a market environment whose price action just happened to coincide with our logic. Monte Carlo analysis will help determine whether our model was successful because of luck (randomness) or its ability to identify and exploit a real market pattern.
This article will cover Phase 1; subsequent articles will cover Phases 2 through 5.
Forming the model
This tutorial covers the processes of automated trading system development and not any specific trading strategy. Because of that, our model logic will be based on a random-entry system to keep the focus on the development process. To gather sufficient data for analysis, the system will day-trade the EUR/USD currency pair, which is both extremely liquid and familiar to most traders.
To create a random entry, the system will simulate a coin flip. Our market model is simply “buy on heads, sell on tails.” Based on the results of the coin flip, the system will buy (enter long) or sell (enter short) at 8 a.m. (Eastern) at the start of the New York trading session. The system will exit the trade at 5 p.m. (Eastern) at the end of the New York trading session. We will test the system on hourly bars (referred to as “H1” in MT4 parlance).
We coded the random logic using MT4’s programming language MQL4. This is done by creating what is termed an Expert Advisor (EA). We called this particular one Random Entry DTS (for day trading system). Our EA used the function MathRand() to generate pseudo-random numbers and a short algorithm was created to simulate a coin flip. At 8 a.m., Monday through Friday, the system “flips” the virtual coin. If the result is a positive number (virtual heads), then the system will initiate a long trade in the euro currency pair. If the result is zero or a negative number (virtual tails), then the system will initiate a short trade.
A fair coin theoretically will produce a 50/50 head/tails split as the number of flips approaches infinity. With a finite number of flips, however, the split will drift from the theoretical, but a sufficiently large number of flips will produce splits that are near to that. With a sufficiently large number of trades, therefore, the split between winners and losers also should approach a 50/50 split.
Our next step is to lay down the framework for the test that will collect data for further analysis. The starting hypothetical account balance is $10,000. Trade size is standardized at one $10,000 lot (one mini-lot), with a pip value of $1.
The EA has been programmed to gather the following data for each trade: Bars since entry, highest trade high, bars to highest trade high, lowest trade low, bars to lowest trade low, maximum adverse excursion (MAE) and maximum favorable excursion (MFE). The system exports this data to comma-separated value (CSV) text files. CSV files can be imported into any spreadsheet program for analysis. For the analysis presented here, Microsoft Excel was used, but the open-source (and free) OpenOffice Calc (www.openoffice.org) spreadsheet program works just the same.
MAE and MFE play an important role in our analysis. MAE is the greatest distance the market travels against the trade. MFE is the greatest distance the market travels in favor of the trade. For example, suppose the system entered the euro long at 1.5000. Between the time of entry and the automatic end-of-day exit, the market moved down to 1.4950 and up to 1.5075. The greatest distance the market moved against the long position, or MAE, is 1.5000 – 1.4950 = 0.0050, or 50 pips. The greatest distance the market moved in favor of the trade, or MFE, is 1.5075 – 1.5000 = 0.0075, or 75 pips. These two measurements can give us a basis for setting the ISL and the PT.
MT4 has a backtesting module called the Strategy Tester. We load our EA into the Strategy Tester and run the test. In addition to the data file exported by the EA, the Strategy Tester produces a backtesting report that contains all the usual test metrics (net profit, win/loss percentage, maximum drawdown, etc.) and a system equity graph. The report data will be used to compare each version of the system and will allow us to track our development progress.
We will assume that our EA already has been tested fully to ensure that its logic is correct. The system is entering and exiting trades as expected, and our data are exporting properly. Using the Strategy Tester, the EA was tested on four years (Jan. 1, 2008, to Dec. 31, 2011) of historical EUR/USD data. At the end of the test run our CSV file is complete, the spreadsheet program is launched and the data are imported.
Now we can perform our statistical analysis. The measurements we’re looking for are rather simple: The mean, the median and the standard deviation of the MAE and the MFE.
The system produced 1,038 trades, of which 505 (48.65%) were long trades and 533 (51.35%) were short. As expected, this result approaches the theoretical 50/50 heads/tails split of an unlimited number of coin flips.
Winners and losers were nearly an even split, as well: 513 winners (49.42%) and 525 losers (51.58%). Consecutive wins and consecutive losses both averaged two in a row. The average winning trade amount was $64.38 and the average losing trade amount was $64.51 — again, nearly equal. Due to this small difference, the system generated a net loss of $838.60, representing an 8.386% loss of equity (see “Strategy tester report,” below). If we can reduce the amount of the average loss (with a stop loss) and increase the amount of the average win (with a profit target), the system can be made profitable. MAE and MFE should help us to do that.
Using Excel’s sorting ability, the winning trades are separated from the losing trades. Excel can calculate the mean, median and standard deviation of MAE and MFE using its built-in functions, AVERAGE, MEDIAN and STDEV. Click here to view more detailed statistics.
The results of the calculations are shown in “Statistical analysis” (below). (The calculations for all trades combined also are shown for completeness.) The mean MAE is 0.00291, or 29.1 pips. For trades that exit ultimately profitably, the market averages about 29 pips against the position prior to the exit. Contrast that with the mean MAE for losing trades: 95.6 pips. Clearly, there is no need to tolerate a move of 96 pips against the trade. The MAE data indicates that the trade is unlikely to exit profitably after such an adverse excursion. On the other hand, it would be unwise to set the ISL blindly at 29 pips because too many good trades will be stopped out prematurely. The optimal stop loss likely lies somewhere in between.
Turning to the MFE data, with a mean MFE of 97.1 pips, the market averages 97 pips of open profit prior to the exit. Note the difference between the average MFE of 97 pips and the average win of approximately 64 pips (one pip = $1 when trading a single mini-lot). The typical winning trade is, apparently, giving back at least 30 pips of profit before the trade is exited at the close of the session. If we can capture those additional pips, then combined with the stop loss, we should be able to increase the ratio of average win to average loss for a net profitable system.
Here, we have laid the statistical foundation for improving our core system. In the next article, we will add the ISL to the system and use our MAE findings to optimize it. The next step will be to do the same on the profit side: Add the PT to the system and use MFE to optimize that. Each step is vital groundwork for our ultimate goal of developing a complete trading system.