Forex Trading System Algorithms

A trading system algorithm is a series of steps that shows how the system handles entries, exits at a loss (stop loss) and exits at a profit. Ultimately, these need to be coded into a computer system to automate your trading, but implementation is independent of the actual algorithm.

In this posting, I am going to discuss some price smoothing algorithms.

Price Smoothing – Why Do it?

The trader generally has to transform a price data series into trading signals, but price data itself is very noisy. It is similar to trying to tune into a radio station through a lot of static. It is hard to tell what is important, and what is just random noise.

Noise is the non-tradeable component of price data. If you try to trade it, you will significantly reduce your profits. Clearly, the problem at hand is to isolate the noise from the signal. This smooths the price series so that the underlying direction is highlighted.

This problem is well defined in signal processing and some quite advanced and effective techniques are available, but often traders use very crude approaches. I will start in this posting by discussing the traditional approaches and how they work.

Crude Approaches

I would like to describe two crude noise filters: the breakout and the moving average (and its variants).

The breakout is an entry or exit signal that is triggered when the current price exceeds (e.g.) a 20 day high, or falls below a 20 day low. The parameters that can be tweaked are the number of periods and the amount by which the price must exceed or be below the high or low.

The way that this works to filter noise is through a volatility filter. In effect, the system attempts to remove price volatility attributable to noise and assumes that a price that exceeds a certain level represents a true signal rather than noise.

This is how a breakout can be described in an algorithm:

If price + trigger amount > high of n periods then buy
If price – trigger amount < low of n periods then sell

The problem is that this approach is rather well known, and false breakouts are therefore quite common. This means that the noise from traders entering the market now distorts the signal.

Another approach is a moving average. This is simply the average of the last (say) 20 periods. The result will be a smoother line than the original price series, but lagged by about 1/2 the period selected. A longer number of periods produces a smoother line, but with more lags to price action, while a shorter number of periods produces a less smooth line which reflects more noise, but is more responsive to changes.

A moving average removes noise by reducing the impact of a particular noisy value by averaging it out. Because this is an average, it is still subject to distortion by extreme values, so it doesn’t work well if you have very noisy data, unless you choose a very long moving average period, which causes lags.

The algorithm for a moving average (n period, where n is an integer, e.g. 20) is as follows:

Sum last n periods, then divide by n
Move forward 1 period, then recalculate

The moving average needs to be combined with some other rules for a complete trading system. For example, one popular approach is to look at when 20 period and 50 period moving averages cross over:

If 20 day moving average crosses over 50 day moving average then buy
If 20 day moving average crosses below 50 day moving average then sell

There are some variants to this such as exponential moving averages and median filters. The exponential moving average has similar properties to the usual type but is calculated differently, so I won’t go into detail here. A median filter is more interesting. This has less lag. The algorithm is:

Sort last n periods of price data from highest to lowest
Take the middle point
Use this as the value

Median filters can be used in a similar way to ordinary moving averages. They remove noise by excluding extreme values and looking at the value in the middle.

All these algorithms can be easily implemented in Excel.

Next time, I’ll continue this in more detail and begin to discuss some of the more advanced techniques also.