FractalsTrendLines

Download script
Author: Copyright 2014, Svds75

This script is designed to automate the identification of market trends by drawing dynamic trendlines based on "Fractals"—which are specific price points that represent local highs and lows in a market.

How the Logic Works

The tool functions by identifying significant turning points in the price action and connecting them to create two distinct trendlines: a Bearish Trendline (representing downward pressure) and a Bullish Trendline (representing upward pressure).

Instead of drawing lines randomly, the script follows a specific logical sequence:

  1. Fractal Identification: The script looks for "Fractals" in the price history. These are peaks and valleys where the price reached a high or low point relative to the candles immediately surrounding them.
  2. Dynamic Line Drawing:
    • It connects the most recent significant high to the previous significant high to form the Bearish Trendline.
    • It connects the most recent significant low to the previous significant low to form the Bullish Trendline.
  3. Automatic Updates: The lines are not static. The script constantly monitors the price. When a new fractal is formed that breaks the existing trend, the line "locks" in place until a new extreme point is reached, at which point it updates to reflect the latest market structure.

Market Interpretation

The script provides a visual framework for traders to interpret the current market state based on how the price interacts with these two lines:

  • Bullish Trend: When the price closes consistently above both the Bullish and Bearish trendlines, indicating strong upward momentum.
  • Bearish Trend: When the price closes consistently below both trendlines, indicating strong downward momentum.
  • Passing Cross: Occurs when the two trendlines intersect each other, signaling a potential shift in market sentiment.
  • Flight (Breakout): When the price closes between the two lines but the "Passing Cross" occurred recently on the left side of the chart.
  • Triangle of Indeterminacy: When the price closes between the two lines and the "Passing Cross" occurred recently on the right side, suggesting a period of consolidation or uncertainty where the market direction is unclear.
Price Data Components
Series array that contains open time of each bar
Indicators Used
Fractals
8 Views
0 Downloads
0 Favorites
FractalsTrendLines
//+------------------------------------------------------------------+
//|                                           FractalsTrendLines.mq5 |
//|                                           Copyright 2014, Svds75 |
//|                                             http://reksystem.ru/ |
//+------------------------------------------------------------------+
#property copyright "Copyright 2014, Svds75"
#property link      "https://login.mql5.com/ru/users/svds75/"
#property version   "1.00"
#property description "Èíäèêàòîð ÷åðòèò áû÷üþ è ìåäâåæüþ ëèíèè òðåíäà, íà îñíîâàíèè èíä. iFractals. Îò áëèæàéøåãî ýêñòðåìóìà äî ïîñëåäíåãî íå ïðîáèòîãî ôðàêòàëà. Ïðè ïðîáèòèè äàííîãî ôðàêòàëà ëèíèÿ çàêðåïëÿåòñÿ äî ïîÿâëåíèÿ íîâîãî ýêñòðåìóìà.\n"
#property description "Òðàêòîâàòü ìîæíî ñëåäóþùèì îáðàçîì:"
#property description "1) Ïåðåñå÷åíèå ëèíèé \"Ïðîõîäíîé Êðåñò\""
#property description "2) Çàêðûòèå âûøå îáîèõ ëèíèé: \"Òðåíä áû÷èé\""
#property description "3) Çàêðûòèå íèæå îáîèõ ëèíèé: \"Òðåíä ìåäâåæèé\""
#property description "4) Çàêðûòèå ìåæäó ëèíèé, è \"Ï.Ê.\" ñëåâà: \"Ôëåò\""
#property description "5) Çàêðûòèå ìåæäó ëèíèé, è \"Ï.Ê.\" ñïðàâà: \"Òðåóãîëüíèê íåîïðåäåëåííîñòè\""
#property indicator_chart_window
#property indicator_plots 0
//+------------------------------------------------------------------+
#define PROGRAM_NAME MQLInfoString(MQL_PROGRAM_NAME)
//+------------------------------------------------------------------+
sinput color inpClrBull=clrGreen;   // Öâåò áû÷üåé ëèíèè òðåíäà
sinput color inpClrBear=clrRed;     // Öâåò ìåäâåæüåé ëèíèè òðåíäà
//+------------------------------------------------------------------+
#include <ChartObjects\ChartObjectsLines.mqh>
//+------------------------------------------------------------------+
int handle;                // Áóäåò õðàíèòü õýíäë èíäèêàòîðà iFractals
CChartObjectTrend upLine;  // Îáúåêò âåðõíåé ëèíèè òðåíäà (ìåäâåæåé)
CChartObjectTrend dnLine;  // Îáúåêò íèæíåé ëèíèè òðåíäà (áû÷åé)
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- create handle indicator iFractals
   handle=iFractals(_Symbol,_Period);
   if(handle==INVALID_HANDLE) handle=IndicatorCreate(_Symbol,_Period,IND_FRACTALS);
   if(handle==INVALID_HANDLE)
     {
      PrintFormat("Error create Fractals indicator handle %s/%s, code %d",_Symbol,EnumToString(_Period),GetLastError());
      return(INIT_FAILED);
     }
//--- create indicator shortname
   IndicatorSetString(INDICATOR_SHORTNAME,StringFormat(PROGRAM_NAME+"(%s/%s)",_Symbol,EnumToString(_Period)));
//--- init ok
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int OnCalculate (const int rates_total,      // ðàçìåð âõîäíûõ òàéìñåðèé
                 const int prev_calculated,  // îáðàáîòàíî áàðîâ íà ïðåäûäóùåì âûçîâå
                 const datetime& time[],     // Time
                 const double& open[],       // Open
                 const double& high[],       // High
                 const double& low[],        // Low
                 const double& close[],      // Close
                 const long& tick_volume[],  // Tick Volume
                 const long& volume[],       // Real Volume
                 const int& spread[]         // Spread
                 )
  {
//---
   ArraySetAsSeries(time,true);
   if(!createTrendLine(0,time)) return(0);
   if(!createTrendLine(1,time)) return(0);
//---
   return(rates_total);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//--- delete handle indicator iFractals
   IndicatorRelease(handle);
//---
  }
//+------------------------------------------------------------------+
//| Custom indicator create TrendLine function                       |
//+------------------------------------------------------------------+
int createTrendLine(const int t,const datetime &TM[])
  {
//---
   int count=500;
//---
   if(t==0)
     {
      double a[];
      ArraySetAsSeries(a,true);
      if(CopyBuffer(handle,0,0,count,a)<0)
        {
         PrintFormat("Íå óäàëîñü ñêîïèðîâàòü äàííûå èç èíäèêàòîðà iFractals, êîä îøèáêè %d",GetLastError());
         return(false);
        }
      int b1=0,b2=0;
      double p1=0,p2=0;
      datetime t1=0,t2=0;
      //---
      for(int b=3;b<count;b++) if(a[b]!=EMPTY_VALUE)
        {
         if(p2==0)
           {
            p2=a[b];
            t2=TM[b];
            b2=b;
           }
         else if(p1==0)
           {
            p1=a[b];
            t1=TM[b];
            b1=b;
           }
         else if(a[b]<p1 && p1>p2) break;
         else
           {
            p2=p1;
            t2=t1;
            b2=b1;
            p1=a[b];
            t1=TM[b];
            b1=b;
           }
        }
      //---
      for(int b=b2-1;b>2;b--) if(a[b]!=EMPTY_VALUE) if(p2>a[b])
        {
         p2=a[b];
         t2=TM[b];
         b2=b;
        }
      else break;
      //---
      if(upLine.Name()!=PROGRAM_NAME+"_Bear_Trend")
        {
         upLine.Create(0,PROGRAM_NAME+"_Bear_Trend",0,t1,p1,t2,p2);
         upLine.RayRight(true);
         upLine.Style(STYLE_DOT);
         upLine.Color(inpClrBear);
        }
      else
        {
         upLine.Time(0,t1);
         upLine.Price(0,p1);
         upLine.Time(1,t2);
         upLine.Price(1,p2);
        }
      //---
      return(true);
     }
//---
   if(t==1)
     {
      double a[];
      ArraySetAsSeries(a,true);
      if(CopyBuffer(handle,1,0,count,a)<0)
        {
         PrintFormat("Íå óäàëîñü ñêîïèðîâàòü äàííûå èç èíäèêàòîðà iFractals, êîä îøèáêè %d",GetLastError());
         return(false);
        }
      int b1=0,b2=0;
      double p1=0,p2=0;
      datetime t1=0,t2=0;
      //---
      for(int b=3;b<count;b++) if(a[b]!=EMPTY_VALUE)
        {
         if(p2==0)
           {
            p2=a[b];
            t2=TM[b];
            b2=b;
           }
         else if(p1==0)
           {
            p1=a[b];
            t1=TM[b];
            b1=b;
           }
         else if(a[b]>p1 && p1<p2) break;
         else
           {
            p2=p1;
            t2=t1;
            b2=b1;
            p1=a[b];
            t1=TM[b];
            b1=b;
           }
        }
      //---
      for(int b=b2-1;b>2;b--) if(a[b]!=EMPTY_VALUE) if(p2<a[b])
        {
         p2=a[b];
         t2=TM[b];
         b2=b;
        }
      else break;
      //---
      if(dnLine.Name()!=PROGRAM_NAME+"_Bull_Trend")
        {
         dnLine.Create(0,PROGRAM_NAME+"_Bull_Trend",0,t1,p1,t2,p2);
         dnLine.RayRight(true);
         dnLine.Style(STYLE_DOT);
         dnLine.Color(inpClrBull);
        }
      else
        {
         dnLine.Time(0,t1);
         dnLine.Price(0,p1);
         dnLine.Time(1,t2);
         dnLine.Price(1,p2);
        }
      //---
      return(true);
     }
//---
   return(false);
  }
//+------------------------------------------------------------------+

Comments