Defining Our Fractal-Based Trailing Stop EA

Mark Thomas — Trade On Track

In this article we’re moving from ‘designing’ to ‘defining’. We’re going to start specifying exactly what our automated trailing stop will do and how it will do it. If you’re not interested in automation, then please don’t be put off … the breaking down of this trailing stop strategy into little pieces should still be interesting and perhaps useful to you.

I’m going to start breaking down our strategy into pseudo-code in this article. Pseudo-code is a logical, English-language like way of expressing objects, processes and systems without actually going as far as computer language code. It’s a good first-step for creating a specification for a new computer program – one which is not locked in to any particular programming language or computing platform.

First of all, let’s define what a fractal is, in our pseudo code. In the last article we saw that a fractal can be defined by a minimum of 5 bars or candles. A high must be surrounded by two lower highs, or a low must be surrounded by two higher lows to be classed as a fractal. These are price turning points and they often become support (bullish fractal) and resistance (bearish fractal) areas. In other words, once fractals form, price finds it a little more difficult to break through these points. Fractals can also be defined with more than 5 bars. The more bars surrounding a high or a low, the stronger the fractal becomes. For the purposes of our trailing stop system – fractals will just be 5 bars.

In our pseudo code, we’ll call a price bar ‘priceBar’ (strangely enough). Actually, we’ll go a bit further and call the current price bar or candle (the one that is currently drawing): priceBar[0]. And, we’ll call the last price bar (the one just before the current price bar: priceBar[-1]. The bar before that will be priceBar[-2] and so on.

Here’s a graphical representation of our bars/candles:

Fractal EA

Furthermore, let’s define the high of the current price bar as: priceBar[0].high

Keep in mind that the current bar (priceBar[0]) is always still drawing, so we can’t use it in our definition of a fractal because we can only look at closed candles. Once priceBar[0] closes, it becomes priceBar[-1].

So, for a bearish fractal (a fractal pointing Up), we need to ensure that priceBar[-3].high is higher (greater) than the ones surrounding it. So we can describe a bearish fractal as:

(priceBar[-3].high > priceBar[-2].high) and (priceBar[-3].high > priceBar[-1].high)
and (priceBar[-3].high > priceBar[-4].high) and (priceBar[-3].high > priceBar[-5].high)

Similarly, a bullish fractal (a fractal pointing down) can be defined as:

(priceBar[-3].low < priceBar[-2].low) and (priceBar[-3].low < priceBar[-1].low)
and (priceBar[-3].low < priceBar[-4].low) and (priceBar[-3].low < priceBar[-5].low)

Fairly straightforward so far?

For our trailing stop system, we are going to need to keep track of the last two bearish fractals and the last two bullish fractals. To be specific, we need to keep track of the high price of the last bearish fractals and the low price of the last bullish fractals. We can keep track of these prices by using ‘variables’. Variables are just a way of storing a value in a program so that we can look at it later. ‘priceBar’ in our definitions above is a kind of variable too, although it’s a list or array of variables.

Let’s define four variables:
lastFractalUp – which will be the high price of the last bearish fractal we have found on our chart
previousFractalUp – the high price of the bearish fractal before ‘lastFractalUp’
lastFractalDown – which will be the low price of the last bullish fractal we have found on our chart.
previousFractalDown – the low price of the bullish fractal before ‘lastFractalDown’

Now let’s consider we’re trading an up-trend. We’ve just entered a trade and the first thing we want to do is place our stop loss. To do this, we’ll need to find the low price of the last bullish fractal, which should be stored in our lastFractalDown variable. Here’s a couple of examples:

Fractal EA

In this example, we’re buying at the green line. There might be a number of triggers that get you into this trade, but for simplicity, let’s just say it was a break of the last fractal pointing up. Our trailing stop rules say that our stop goes just below the last bullish fractal (the red line on the chart). It’s interesting to note that this bullish fractal can occur AFTER the bearish fractal (as shown on the above chart), or BEFORE the last bearish fractal (as in the chart below):

Fractal EA

This holds true all the way through the trade, as we’re moving the trailing stop. For a long position, our stop will be just below the last bullish fractal. This doesn’t mean we move the stop as soon as we get another bullish fractal, it just means that is where we will move it when we are ready to.

In our last article we said that we’d leave a gap of a few pips between the fractal points and the actual stop loss level. We’ll store this gap in another variable which you could change each time you use this script. The variable assignment might be something like:

priceGap = 0.0005

indicating that the gap between the fractal price and the stop loss is 5 pips (on non-Yen type currencies).

So, our pseudo code at the start of a long position might be something like:

if longPositionEntered then:
stopLoss = lastFractalDown – priceGap

And, the pseudo code at the start of a short position could be:

if shortPositionEntered then:
stopLoss = lastFractalUp + priceGap

You can’t be in a long position and a short position at the same time on the same chart (well, you shouldn’t be anyway), so we can simplify the above statements to:

if longPositionEntered then:
stopLoss = lastFractalDown – priceGap
else if shortPositionEntered then:
stopLoss = lastFractalUp + priceGap

That’s probably a good place to finish up for today, there’s a bit to take in there but hopefully you can see how things are coming together. The video below shows another example of our trailing stop strategy, this time in an uptrend.

The following two tabs change content below.
Winners Edge Trading was founded in 2009 and is working to create the most current and useful Forex information and training available on the internet.

Winner’s Edge Trading, as seen on:

Winner's Edge Trading in the news