How to Use Forex Fractals Indicator with MQL4 Language

The Forex fractals indicator is a very useful tool for traders. This indicator can help spot support and resistance areas and can also be used in some breakout strategies. In this article you will see how it works and how you can use the MQL4 language in MetaTrader 4 (MT4) to get the fractals indicator values.

What Is Fractals Indicator?

Fractals is an indicator invented by Bill Williams, the same analyst who developed the Alligator indicator. Despite being very simple, fractals are very popular in both stock and Forex trading. Even though Fractals is a repainting indicator (it doesn't put the arrow on a candle until the next two candles finish forming), it is still a great indicator for some purposes.

How Does It Look Like?

In MetaTrader 4, and also other software, fractals are represented as arrows pointing upward or downward depending on their position.

How Is It Calculated?

To calculate fractals, you need a group of five candles. If the middle candle has the highest price, this is a fractal with an arrow pointing upward and the value equal to the highest price. If the middle candle has the lowest price, this is a fractal with an arrow pointing downward and value equal to the lowest price.

You can see here two examples of fractals. Remember that they assume the value of the highest or lowest price, depending on the direction of the fractal:

Fractal Up Example

Fractal Down Example

Fractals can be next to each other in some cases, actually quite often, as you can see in the following image:

metatrader-fractalsindicator-3

As you can see, spotting a fractal is quite easy and now you will learn how you can use this indicator.

How to Use Fractals?

Fractals can be used in various situations. The main use cases for the fractals indicator are:

  • Find support and resistance areas — fractals can highlight levels where price can reverse or slow down.
  • Trailing stop — you can move the stop level to fractals as these prices have more probability to hold.
  • Break-out strategies — orders can be triggered when the price breaks out a fractal level.
  • Trend continuation strategies — you can re-enter a trend-following trade when a fractal gives a continuation signal.

Fractals alone may not be the best indicator, however, it can provide confirmation to other indicators to reinforce a strategy or setup.

How to Retrieve Fractals Value in MQL4 Language

MetaTrader 4 and MQL4 language provide a native function to detect fractals. It is called iFractals(). MetaQuotes provides a complete description of this function on its website.

In detail, the function accepts the following parameters:

double  iFractals(
   string       symbol,           // symbol
   int          timeframe,        // timeframe
   int          mode,             // line index
   int          shift             // shift
   );

where:

  • symbol is the instrument. Using Symbol() will use the current one.
  • timeframe is the timeframe for the indicator to use. Period() will mean the current chart's timeframe.
  • mode is to define whether you want to retrieve the upper arrow values (MODE_UPPER) or lower arrow values (MODE_LOWER).
  • shift is the time shift expressed in number of candles.

The fractal indicator can be tricky to use as is. However, you can use the following simple function to detect fractals easily.

The following code can retrieve three values:

  1. The value of the most recent upper fractal.
  2. The value of the most recent lower fractal.
  3. Whether the most recent fractal was an upper or lower one.
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+

bool FractalsUp=false;
bool FractalsDown=false;
double FractalsUpPrice=0;
double FractalsDownPrice=0;
int FractalsLimit=15;

void FindFractals(){
   //Initialization of the variables
   FractalsUp=false;
   FractalsDown=false;
   FractalsUpPrice=0;
   FractalsDownPrice=0;
   
   //For loop to scan the last FractalsLimit candles starting from the oldest and finishing with the most recent
   for(int i=FractalsLimit; i>=0; i--){
      //If there is a fractal on the candle the value will be greater than zero and equal to the highest or lowest price
      double fu=iFractals(NULL,0,MODE_UPPER,i);
      double fl=iFractals(NULL,0,MODE_LOWER,i);
      //If there is an upper fractal I store the value and set true the FractalsUp variable
      if(fu>0){
         FractalsUp=true;
         FractalsDown=false;
         FractalsUpPrice=fu;
      }
      //If there is an lower fractal I store the value and set true the FractalsDown variable
      if(fl>0){
         FractalsUp=false;
         FractalsDown=true;
         FractalsDownPrice=fl;
      } 
      //if the candle has both upper and lower fractal the values are stored but we do not consider it as last fractal
      if(fu>0 && fl>0){
         FractalsUp=false;
         FractalsDown=false;
         FractalsUpPrice=fu;        
         FractalsDownPrice=fl;
      }
  }
}

void OnStart()
  {
//---
   FindFractals();
   
   //We print the values
   Print(FractalsUpPrice," ",FractalsDownPrice);
   Print(FractalsUp," ",FractalsDown);
   
  }
//+------------------------------------------------------------------+

This function can be quite useful as it allows you to gather information that you can use for your trailing stop, entry or exit point, and more.

Conclusion

Fractals are very popular in financial trading. Bill Williams did a very good job inventing such an indicator, and despite being so simple, it can the very useful. In this guide, you learned more about the indicator and how to use it with MetaTrader 4 and MQL4 language.


If you want to get news of the most recent updates to our guides or anything else related to Forex trading, you can subscribe to our monthly newsletter.

© 2005–2021

EarnForex.com

Design — Mart Studio

Forex trading bears intrinsic risks of loss. You must understand that Forex trading, while potentially profitable, can make you lose your money. Never trade with the money that you cannot afford to lose! Trading with leverage can wipe your account even faster.

CFDs are leveraged products and as such loses may be more than the initial invested capital. Trading in CFDs carry a high level of risk thus may not be appropriate for all investors.