Mark Thomas– Trade On Track

In this article we will complete the pseudo-code logic behind our fractal-based trailing stop. Previously (see this article: http://winnersedgetrading.com/trade-of-the-day/defining-our-fractal-based-trailing-stop-ea), we explored how a fractal point itself could be defined in pseudo-code and we also defined some variables that would remember where our last fractal points were. In addition, we created some pseudo-code logic to tell us where the trailing stop would go.

Here’s where we’re up to so far:

if longPositionEntered then:

stopLoss = lastFractalDown – priceGap

else if shortPositionEntered then:

stopLoss = lastFractalUp + priceGap

This image shows an example of a short position entered (at the green line). The last known fractal up is about 6 candles back from the entry point, so our stop loss goes just above there (where the red line is).

An automated trading program generally works by looking at the situation (price, indicators etc.) for every new tick that comes along. So, our program has a loop which wakes up whenever there’s new data available (a new tick), then it goes and checks for changes in the situation and acts accordingly. Our main program loop might look something like this in pseudo-code:

for each new tick:

checkFractals

checkPriceBreak

checkFractalPullback

calcNewStopLevel

if hasBeenPriceBreak then:

if canMoveStopLoss then:

moveStopLoss

else if hasBeenFractalPullback then:

if canMoveStopLoss then:

moveStopLoss

Let me explain what we’re doing here.

“for each new tick” is self explanatory. We’re going to execute the processes “checkFractals”, “checkPriceBreak” and so on whenever there is a new piece of data available (IE. a new tick).

“checkFractals” will be a separate process or function where we look at the candles or bars that we’ve just drawn. If we find that a new fractal has been formed (either bearish or bullish), then we’ll update our variables to remember where our last fractal point(s) were. Here is how our “checkFractals” function might look:

checkFractals:

#

# check for a new high fractal

#

if (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) then

fractalHigh = priceBar[-3].high

if factalHigh is different to lastFractalUp then:

previousFractalUp = lastFractalUp

lastFractalUp = fractalHigh

#

# if not a new high fractal, then check for a new low fractal

#

else if (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) then
fractalLow = priceBar[-3].low
if fractalLow is different to lastFractalDown then:
previousFractalDown = lastFractalDown
lastFractalDown = fractalLow

So, if a new bearish fractal has been found, we’re updating our ‘lastFractalUp’ and ‘previousFractalUp’ variables with the high prices of the last 2 up fractals that we know about. Similarly, if we’ve just identified a new bullish fractal, we’re updating our ‘lastFractalDown’ and ‘previousFractalDown’ variables with the low prices of the last 2 down fractals.

The next function that we come across in our main loop is “checkPriceBreak”. What this function will do is check to see if price has broken through the last down (bullish) fractal if we’re in a short trade. Or, if we’re in a long trade, the function will check to see if price has broken through the last up (bearish) fractal. Here’s how the function might look:

checkPriceBreak:

hasBeenPriceBreak = No

if longPositionEntered then:

if currentPrice > (lastFractalUp + priceGap) then:

hasBeenPriceBreak = Yes

else if shortPositionEntered then:

if currentPrice < (lastFractalDown - priceGap) then:
hasBeenPriceBreak = Yes

The sole purpose of this routine is to set a variable called ‘hasBeenPriceBreak’. It will be set to “Yes” if price has broken through the last fractal, or “No” if it hasn’t.

Continuing on in our main program loop, the next function to be defined is called “checkFractalPullback”. If you remember in our original design of this trailing stop system, we said that if we’re in a short trade and we have a bearish fractal followed by a higher bearish fractal, then we’d move the stop loss to just above the last bearish fractal, even if price hadn’t broken down through the last bullish fractal. That is what “checkFractalPullback’ checks for. Here’s how it will look:

checkFractalPullback:

hasBeenFractalPullback = No

if longPositionEntered then:

if lastFractalDown is lower than previousFractalDown then:

hasBeenFractalPullback = Yes

else if shortPositionEntered then:

if lastFractalUp is higher than previousFractalUp then:

hasBeenFractalPullback = Yes

In a nutshell, this function will set the variable called “hasBeenFractalPullback” to “Yes” if there has been a fractal pullback, or “No” if not.

The next function in our main loop is “calcNewStopLevel”. This function will just calculate where our new stop loss level could be (not necessarily “will” be, but “could” be).

calcNewStopLevel:

canMoveStopLoss = “No”

if longPositionEntered then:

newStopLevel = lastFractalDown – priceGap

if newStopLevel > stopLoss then:

canMoveStopLoss = “Yes”

else if shortPositionEntered then:

newStopLevel = lastFractalUp + priceGap

if newStopLevel < stopLoss then:
canMoveStopLoss = "Yes"
You'll notice that some of this function is the same as the logic we used to set the stop loss when first entering the trade. The rest of the function checks to see if the new stop loss would be higher than the previous stop loss (in a long position) or if the new stop loss is lower than the previous stop loss (in a short position). We only want to move the stop if it moves in the direction of our trade. That is why it's called a trailing stop, we never want to move a stop loss backwards, away from our trade direction.
The final function in the main loop is "moveStopLoss". All this function would do is move the actual stop loss on our chart to the value stored in the "newStopLevel" variable.
That's basically all there is to the logic for a trailing stop system based on fractals. The code itself may seem quite complex, but you can see that there's not a great deal of code there to make it all happen. When we build the EA for Metatrader (coming up in a future article), the code may expand a little bit, but really, the above code encapsulates what we're trying to achieve. If you've been trying to nut out the rules for a trailing stop system yourself, you might like to try pseudo-code as I have above, to break it all down and define exactly what conditions must be met before you move the stop, then define "how" you will move the stop.

#### Latest posts by admin (see all)

- Fading the Momentum in Forex Trading - September 14, 2017
- Trend Line Drawing With Fractals - September 8, 2017
- Bollinger Bands Strategy, a basic overview - September 7, 2017

Winner’s Edge Trading, as seen on: