Monday, 3 October 2011

Expert Advisor development process

PT1 - The donkey work, formulating the manual strategy.


   The expectation of what a forex robot, or Expert Advisor, will do largely depends on the experience of the individual using it. If you feel that an EA is appropriate to trade your strategy, then you will have a better chance of success. If you have not traded the strategy manually, then you are at best guessing as to the probability of its potential success or failure.

   It is useful to follow the thought process behind a strategy, test it out manually and then see whether or not it would be advantageous to create an automatic system.

   I wrote the following article in order to describe the full development process with the following major steps:

1. Identifying the opportunity
2. Calculating entry and exit points
3. Money management

   The pictures I display were created using indicators that I have developed for my own purposes. Though I am a commercial developer, I explain exactly how the same results can be obtained for free.

Identifying the opportunity

   For the example strategy, I have chosen a simple mean reversion system. In one shape or another, mean reversion forms the basis of the majority of systems seeking to profit from currency moves (and also those systems that profit from prices that stagnate!). So let me first explain in simple terms what mean reversion is:

   There is a tendency for a price series to follow a general trend. Within that general trend, there will be minor trends. The tendency for the price to follow the general trend, whilst exhibiting minor variations, is called mean reversion. More technical/official explanations can be found here and here. Note that the first link (a more mathematical treatise) provides a clue as to how we will formulate our strategy ("regression to the mean").

   If prices regress to the mean price, then, if we measure the distance a price is from its recent mean, we can calculate the potential profit we could achieve if we took a trade right now, and, following our trade entry, the price nicely reverted to its mean price. In order to identify the opportunity, we would need only to look at a currency pair, and see how far, right now, the price is distant from its mean.

   We can calculate the mean (over our chosen time period), and then apply some simple arithmetic. If the price is above the mean this would indicate a possible future tendency for the price to fall. If the price is below the mean, this would indicate a possible future tendency for the price to increase. The distance between the price and the mean shows us the potential profit.

   Although I can't claim to have seen and/or used all the commercially available trading platforms, I would make a (small) bet that all of them have the capability to display moving averages. A simple moving average is suitable for our purposes. Since we are going to be testing this strategy manually, I have chosen to use a daily timeframe, set up the trade, and then come back the next day to see if I need to make any adjustments.

   In the following diagram, I show a recent daily EURUSD chart with a 20 period (one trading month) simple moving average. This is entirely suitable for our purposes and we could stop here and trade the signals so generated as they have been traded since the 1970s (despite it statistically NOT being a causal phenomenon!).

EURUSD1 with SMA(20)
   I think there is a better way; remembering the clue we had before in the mean reversion definition, it is also possible to use a different mechanism for this type of trading. I refer to linear regression. Linear regression is a way of finding the "straightest line between 2 points". Choose the mid of a bar at the beginning of a period, the mid of a bar at the end of a period, draw a line between them and you have roughly what we need (again, for a more in depth perspective, see here and here. Unlike the simple moving average, this can filter out some of the minor moves that occur in the chosen period (i.e. the simple moving average will be irregular, the regression line will be straight - linear). We can see how far off the mean the price moves, allowing us to weigh up exactly which currencies are currently suitable for us to trade. If the regression line slopes upwards (trend for the period is up), and the price is currently below the mean, this provides the setup for a long trade. If the regression line slopes downwards (trend for the period is down), and the price is above the mean, this would indicate the possibility of taking a short position.

   If we add a fixed amount to each side of the regression line and draw two more lines, we can more clearly see what and where the minor trend is, and predict more clearly where it could go next. If we see the price bouncing along a fixed "regression channel", we might conclude that, if the price is just below the upper line, a downwards reversal is imminent. The next picture shows the Metatrader4 built in Linear regression channel indicator. The picture after that shows an adapted channel indicator, which moves out the upper and lower lines so that they just touch the current maximum highs and lows of all the bars in the channel. This allows us to see more clearly which bars defined the limits of the actual channel, rather than the usual regression plus average daily range that MT4 gives us.


EURUSD1 with Linear Regression channel, take profit and stop loss
EURUSD1 with adjusted Linear Regression channel, take profit and stop loss
 Calculating entry and exit points

   An obvious entry point is entry at market when we identify the opportunity. We have a choice of take profit points. We could exit once the price crosses the regression line (reversion to the mean will have completed), exit half the position in anticpation of a further move, or add to our position on a definite cross of the centre line. For illustration purposes, I choose the easier (and nearer point of the regression line). Drawing a horizontal line and noting the price where where the regression line is on the last bar gives us our take profit (TP) point. For stop loss (SL), if the price moves outside the near channel boundary, then we have a breakout of the channel and our directional bet is jeopardised. Other possible SL points are at the high/low of the previous bar, or high/low of a bar at some point in the period where we think we won't get stopped out unnecessarily.We draw another horizontal line and note the price at that point.


Money management

"I spent almost all of my money on women and alcohol, the rest I just wasted - George Best"

   We have all the information we need to make the trade, except how much do we bet? I created a simple blotter (trade list) in Excel, and created a formula for calculating the volume as follows:

=MAX(MIN_LOT,(RiskPC*Balance)/(ABS(OpenPrice-StopLoss)/TickSize*TickValue))

   I use a percentage of my balance, and divide this by the maximum adverse price deviation I will allow, and then divide again by the pip cost. We now have the volume, stop loss, take profit, and can now take the trade.

   I also use this blotter to document my thinking behind each trade, and the result, so I don't make the same stupid mistake twice!

Below are some actual trades. I display the ADX(2) in each one to confirm my understanding of the trend strength.


CHFJPY with LRC (19) to eliminate outlier candle
GBPJPY with LRC(20) and Opportunity Indicator (See part2)
USDCHF with LRC(19) eliminating outlier candle


PT2 - Automating the process.

In PT2, I will be describing how to identify the trading opportunities for every available currency pair, and outlining the EA design.

No comments:

Post a Comment

Note: only a member of this blog may post a comment.