MTF_Fractal

Download script
Author: Copyright � 2014, TrueTL
MTF_Fractal

This script is designed to identify and visualize specific price turning points, known as "fractals," across different timeframes on your trading chart. It helps traders see significant structural highs and lows by automatically plotting markers and extending horizontal lines based on these key price levels.

Core Functionality

Multi-Timeframe Analysis The tool is capable of looking at higher timeframes than the one currently displayed on your screen. This allows you to identify significant support and resistance levels from a broader perspective without having to switch back and forth between different charts.

Fractal Detection The script scans historical price data to locate "fractals." In technical analysis, these are specific peaks or valleys where the high (or low) of a candle is higher (or lower) than the candles surrounding it. The tool identifies two types:

  • Up Fractals: These represent local peaks in price movement.
  • Down Fractals: These represent local troughs or bottoms in price movement.

Dynamic Line Extension One of the primary features of this script is its ability to draw horizontal lines from these fractal points. Rather than just placing a dot on the chart, the tool automatically extends a line to show where that specific price level remains relevant.

Smart Validation Logic To ensure the lines are as accurate as possible, the script includes a "validation" process. Instead of simply drawing a line indefinitely, it looks ahead at subsequent candles to see if the price has broken through that specific level. If the price crosses through the fractal level (moving from above to below or vice versa), the script logic recognizes that the "strength" of that level has been breached and stops the line accordingly.

Visual Customization The tool provides several visual options to keep your chart clean and organized:

  • Color Coding: You can set specific colors for upward and downward fractals (e.g., Red for highs and Blue for lows).
  • Line Styles: You can adjust the thickness, style (solid, dashed, etc.), and whether the lines appear on top of or behind other chart elements.
  • Marker Symbols: It uses specific symbols to clearly mark the exact point where a fractal was detected.

Summary of Behavior

When you load this script, it calculates the necessary price levels based on your chosen timeframe and scans back through your history. It places markers at every significant peak and valley and then draws horizontal "support" or "resistance" lines that persist until the market price actually breaks through them.

Price Data Components
Series array that contains the highest prices of each bar
Indicators Used
Fractals
Miscellaneous
Implements a curve of type %1
8 Views
0 Downloads
0 Favorites
MTF_Fractal
//+------------------------------------------------------------------+
//|                                                  MTF Fractal.mq4 |
//|                                         Copyright © 2014, TrueTL |
//|                                            http://www.truetl.com |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2014, TrueTL"
#property link      "http://www.truetl.com"
#property version "1.40"
#property indicator_chart_window
#property indicator_buffers 2

extern string  Version_140                      = "www.truetl.com";
extern int     Fractal_Timeframe                = 0;
extern int     Maxbar                           = 2000;
extern color   Up_Fractal_Color                 = Red;
extern int     Up_Fractal_Symbol                = 108;
extern color   Down_Fractal_Color               = Blue;
extern int     Down_Fractal_Symbol              = 108;
extern bool    Extend_Line                      = true;
extern bool    Extend_Line_to_Background        = true;
extern bool    Show_Validation_Candle           = true;
extern color   Up_Fractal_Extend_Line_Color     = Red;
extern int     Up_Fractal_Extend_Width          = 0;
extern int     Up_Fractal_Extend_Style          = 2;
extern color   Down_Fractal_Extend_Line_Color   = Blue;
extern int     Down_Fractal_Extend_Width        = 0;
extern int     Down_Fractal_Extend_Style        = 2;

double UpBuffer[], DoBuffer[], refchk, tempref, level;
int barc;

//+------------------------------------------------------------------+
//|                                                             INIT |
//+------------------------------------------------------------------+

int init() {

   SetIndexBuffer(0,UpBuffer);
   SetIndexStyle(0,DRAW_ARROW, DRAW_ARROW, 0, Up_Fractal_Color);
   SetIndexArrow(0,Up_Fractal_Symbol);
   SetIndexBuffer(1,DoBuffer);
   SetIndexStyle(1,DRAW_ARROW, DRAW_ARROW, 0, Down_Fractal_Color);
   SetIndexArrow(1,Down_Fractal_Symbol);
   
   return(0);
}

//+------------------------------------------------------------------+
//|                                                           DEINIT |
//+------------------------------------------------------------------+

int deinit() {
   for (int i = ObjectsTotal(); i >= 0; i--) {
      if (StringSubstr(ObjectName(i),0,12) == "MTF_Fractal_") {
         ObjectDelete(ObjectName(i));
      }
   }
   
   return(0);
}

//+------------------------------------------------------------------+
//|                                                            START |
//+------------------------------------------------------------------+

int start() {
   int i, c, dif;
   tempref =   iHigh(Symbol(), Fractal_Timeframe, 1) + 
               iHigh(Symbol(), Fractal_Timeframe, 51) + 
               iHigh(Symbol(), Fractal_Timeframe, 101);
   
   if (barc != Bars || IndicatorCounted() < 0 || tempref != refchk) {
      barc = Bars;
      refchk = tempref;
   } else
      return(0);
   
   deinit();
   
   if (Fractal_Timeframe <= Period()) Fractal_Timeframe = Period();
   
   dif = Fractal_Timeframe/Period();
   
   if (Maxbar > Bars) Maxbar = Bars-10;
   
   for(i = 0; i < Maxbar; i++) {
      if (iBarShift(NULL,Fractal_Timeframe,Time[i]) < 3) {
         UpBuffer[i] = 0;
         DoBuffer[i] = 0;
         continue;
      }
      UpBuffer[i] = iFractals(NULL,Fractal_Timeframe,1,iBarShift(NULL,Fractal_Timeframe,Time[i]));
      DoBuffer[i] = iFractals(NULL,Fractal_Timeframe,2,iBarShift(NULL,Fractal_Timeframe,Time[i]));
   }
   
   if (Extend_Line) {
      for(i = 0; i < Maxbar; i++) {
         if (UpBuffer[i] > 0) {
            level = UpBuffer[i];
            for (c = i; c > 0; c--) {
               if ((Open[c] < level && Close[c] > level) || (Open[c] > level && Close[c] < level)) 
                  break;
               if (Open[c] <= level && Close[c] <= level && Open[c-1] >= level && Close[c-1] >= level) 
                  break;
               if (Open[c] >= level && Close[c] >= level && Open[c-1] <= level && Close[c-1] <= level) 
                  break;
            }
            DrawLine ("H", i, c, level, Extend_Line_to_Background, Up_Fractal_Extend_Line_Color, Up_Fractal_Extend_Width, Up_Fractal_Extend_Style);
            if (Show_Validation_Candle) UpBuffer[i-2*dif] = level;
            i += dif;         
         }
      }
      
      for(i = 0; i < Maxbar; i++) {
         if (DoBuffer[i] > 0) {
            level = DoBuffer[i];
            for (c = i; c > 0; c--) {
               if ((Open[c] < level && Close[c] > level) || (Open[c] > level && Close[c] < level)) 
                  break;
               if (Open[c] <= level && Close[c] <= level && Open[c-1] >= level && Close[c-1] >= level) 
                  break;
               if (Open[c] >= level && Close[c] >= level && Open[c-1] <= level && Close[c-1] <= level) 
                  break;
            }
            DrawLine ("L", i, c, level, Extend_Line_to_Background, Down_Fractal_Extend_Line_Color, Down_Fractal_Extend_Width, Down_Fractal_Extend_Style);
            if (Show_Validation_Candle) DoBuffer[i-2*dif] = level;
            i += dif;
         }
      }
   }
   
   return(0);
}
//+------------------------------------------------------------------+
//|                                                        DRAW LINE |
//+------------------------------------------------------------------+

void DrawLine (string dir, int i, int c, double lev, bool back, color col, int width, int style) {
   ObjectCreate("MTF_Fractal_"+dir+i,OBJ_TREND,0,0,0,0,0);
   ObjectSet("MTF_Fractal_"+dir+i,OBJPROP_TIME1,iTime(Symbol(),Period(),i));
   ObjectSet("MTF_Fractal_"+dir+i,OBJPROP_PRICE1,lev);
   ObjectSet("MTF_Fractal_"+dir+i,OBJPROP_TIME2,iTime(Symbol(),Period(),c));
   ObjectSet("MTF_Fractal_"+dir+i,OBJPROP_PRICE2,lev);
   ObjectSet("MTF_Fractal_"+dir+i,OBJPROP_RAY,0);
   ObjectSet("MTF_Fractal_"+dir+i,OBJPROP_BACK,back);
   ObjectSet("MTF_Fractal_"+dir+i,OBJPROP_COLOR,col);
   ObjectSet("MTF_Fractal_"+dir+i,OBJPROP_WIDTH,width);
   ObjectSet("MTF_Fractal_"+dir+i,OBJPROP_STYLE,style);
}

Comments