Author: Copyright 2017, IWORI_FX
Auto_Fibo

This script is designed to automate the placement and updating of Fibonacci Retracement levels on your trading chart. Instead of requiring you to manually click and drag lines based on market movements, this tool dynamically identifies key price points to draw the Fibonacci grid automatically.

Core Logic and Functionality

1. Automatic Pivot Detection The script relies on a technical indicator called "ZigZag" to identify significant structural turns in the market (highs and lows). It scans through the historical price data to find these major turning points based on your preferred depth and deviation settings.

2. Dynamic Fibonacci Placement Once the script identifies the most recent significant market move (the "leg"), it automatically anchors a Fibonacci Retracement tool between the start and end points of that move.

  • If the tool does not exist on the chart yet, the script creates it for you.
  • As the market continues to move and new structural peaks or troughs are formed, the script detects these changes and moves the existing Fibonacci tool to stay aligned with the most recent price action in real-time.

3. Customizable Retracement Levels The script allows you to define exactly which Fibonacci levels you want to see on your chart (e.g., 23.6%, 38.2%, 50.0%, 61.8%, etc.). It creates a custom list based on your input, allowing for both standard and extended Fibonacci ratios.

4. Automated Labeling and Visuals For every level the script draws, it automatically calculates the exact price value and displays it as a text label next to the line. This provides an immediate visual reference of where specific retracement targets are located without requiring manual calculation.

Key User Features

  • Real-Time Updates: The tool is designed to refresh with every price tick, ensuring that the Fibonacci levels are always moving in sync with the latest "ZigZag" data.
  • Visual Customization: You can choose the specific colors for the main Fibonacci line and the individual retracement levels to match your chart's aesthetic.
  • Clean Chart Management: When you remove the script from the chart, it is programmed to automatically clean up and delete the Fibonacci object it created, leaving your workspace tidy.
10 Views
0 Downloads
0 Favorites
Auto_Fibo
//+------------------------------------------------------------------+
//|                                                    Auto Fibo.mq4 |
//|                                         Copyright 2017, IWORI_FX |
//|                           https://www.mql5.com/en/users/iwori_fx |
//+------------------------------------------------------------------+
#property copyright "Copyright 2017, IWORI_FX"
#property link      "https://www.mql5.com/en/users/iwori_fx"
#property description "\nPowered By: Iwori Fx"

#property strict
#property indicator_chart_window

//--- input parameters
input string   inpName           = "Fibo_01";      // Fibo Name
input int      inpDepth          = 12;             // ZigZag Depth
input int      inpDeviation      = 5;              // ZigZag Deviation
input int      inpBackStep       = 3;              // ZigZag BackStep
input int      inpLeg            = 1;              // ZigZag Leg
input color    inpLineColor      = clrRed;         // Line Color
input color    inpLevelsColor    = clrSteelBlue;   // Levels Color
input bool     inpRay            = false;          // Ray
input double   inpLevel1         = 0.0;            // Level 1
input double   inpLevel2         = 23.6;           // Level 2
input double   inpLevel3         = 38.2;           // Level 3
input double   inpLevel4         = 50.0;           // Level 4
input double   inpLevel5         = 61.8;           // Level 5
input double   inpLevel6         = 100.0;          // Level 6
input double   inpLevel7         = 161.8;          // Level 7
input double   inpLevel8         = 261.8;          // Level 8
input double   inpLevel9         = 423.6;          // Level 9


//--- global variables
double   levels[9];     // Levels Array
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//---

// Set Levels Array
   levels[0] = inpLevel1;  levels[1] = inpLevel2;  levels[2] = inpLevel3;  levels[3] = inpLevel4;  levels[4] = inpLevel5;
   levels[5] = inpLevel6;  levels[6] = inpLevel7;  levels[7] = inpLevel8;  levels[8] = inpLevel9;

//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---

// To Delete Fibonacci
   ObjectDelete(0,inpName);

//---
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
  {
//---

   datetime times[2];
   double price[2];

// Get Times and Price ZigZag Values.
   if(!GetZZ(times,price)) return 0;

   if(ObjectFind(0,inpName)<0) FiboDraw(inpName,times,price,inpLineColor,inpLevelsColor);    // Create new Fibonacci
   else FiboMove(inpName,times,price,inpLevelsColor);                                        // Move current Fibonacci
   ChartRedraw();                                                                            // Refresh chart

//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+
//| FiboMove function                                                |
//+------------------------------------------------------------------+
void FiboMove(string name,datetime &time[],double &price[],color clrLevels)
  {
// Move current Fibonacci
   ObjectMove(0,name,0,time[1],price[1]);    // Move first point of the fibo
   ObjectMove(0,name,1,time[0],price[0]);    // Move second point of the fibo
   FiboSetLevels(name,price,clrLevels);      // Set Levels of the fibo
   ChartRedraw();                            // Refresh chart
  }
//+------------------------------------------------------------------+ 
//| FiboDraw function                                                | 
//+------------------------------------------------------------------+ 
bool FiboDraw( const string      name,                   // object name 
              datetime          &time[],                // array time 
              double            &price[],               // array price 
              const color       clrFibo=clrRed,         // object color 
              const color       clrLevels=clrYellow)    // levels color 
  {

   long chart_ID=0;
   int sub_window=0;

   ResetLastError();

// Create Fibonacci Object
   if(!ObjectCreate(chart_ID,name,OBJ_FIBO,sub_window,time[1],price[1],time[0],price[0]))
     {
      Print(__FUNCTION__,
            ": failed to create \"Fibonacci Retracement\"! Error code = ",GetLastError());
      return(false);
     }
//--- set fibonacci object properties
   ObjectSetInteger(chart_ID,name,OBJPROP_COLOR,clrFibo);      // Set Fibo Color
   ObjectSetInteger(chart_ID,name,OBJPROP_STYLE,STYLE_SOLID);  // Set Fibo Line Style
   ObjectSetInteger(chart_ID,name,OBJPROP_WIDTH,1);            // Set Fibo Line Width
   ObjectSetInteger(chart_ID,name,OBJPROP_BACK,false);         // Set Fibo To Front
   ObjectSetInteger(chart_ID,name,OBJPROP_SELECTABLE,false);   // Set Fibo Not Selectable
   ObjectSetInteger(chart_ID,name,OBJPROP_SELECTED,false);     // Set Fibo Not Selected
   ObjectSetInteger(chart_ID,name,OBJPROP_RAY_RIGHT,inpRay);   // Set Fibo Ray
   ObjectSetInteger(chart_ID,name,OBJPROP_HIDDEN,false);       // Set Fibo Hidden in Object List
   ObjectSetInteger(chart_ID,name,OBJPROP_ZORDER,0);

// Set Fibonacci Levels
   FiboSetLevels(name,price,clrLevels);

//--- successful execution 
   return(true);
  }
//+------------------------------------------------------------------+ 
//| FiboSetLevels function                                           | 
//+------------------------------------------------------------------+ 
bool FiboSetLevels( const string      name,                   // object name 
                   double            &price[],               // array price 
                   const color       clrLevels=clrYellow)    // levels color 
  {

   long chart_ID=0;
   int sub_window=0;
   int N=ArraySize(levels);
   string str="";

   ResetLastError();

// Define number of levels
   ObjectSetInteger(chart_ID,name,OBJPROP_LEVELS,N);

// Set Levels Properties
   for(int i=0;i<N;i++)
     {
      ObjectSetDouble(chart_ID,name,OBJPROP_LEVELVALUE,i,levels[i]/100.0);    // Set Level Value
      ObjectSetInteger(chart_ID,name,OBJPROP_LEVELCOLOR,i,clrLevels);         // Set Level Color
      ObjectSetInteger(chart_ID,name,OBJPROP_LEVELSTYLE,i,STYLE_DOT);         // Set Level Line Style
      ObjectSetInteger(chart_ID,name,OBJPROP_LEVELWIDTH,i,1);                 // Set Level Line Width
     }

// Set Levels descriptions
   for(int i=0;i<N;i++)
     {
      str = DoubleToString(levels[i],1) + "% = ";                                            // Set % levels value
      str += DoubleToString(price[0]+(levels[i]/100.0)*(price[1]-price[0]),_Digits) + "  ";  // Set price level value
      ObjectSetString(chart_ID,name,OBJPROP_LEVELTEXT,i,str);                                // Set Description Text
     }
//--- successful execution 
   return(true);
  }
//+------------------------------------------------------------------+
//| GetZZ function                                                   |
//+------------------------------------------------------------------+
bool GetZZ(datetime &time[],double &price[])
  {
   bool ret= false;
   int leg = (int)MathMax(1,inpLeg);
   int cnt = 0;
   int idx = 0;
   double zz=0.0;

   for(int i=0; i<Bars-1; i++)
     {
      zz=iCustom(_Symbol,0,"ZigZag",inpDepth,inpDeviation,inpBackStep,0,i);
      if(zz<=0.0 || zz==EMPTY_VALUE || zz>1000000.0) continue;
      cnt++;
      if(cnt<leg) continue;
      time[idx]=Time[i];
      price[idx]=zz;
      idx++;
      if(idx>1) { ret=true; break; }
     }

   return ret;
  }
//+------------------------------------------------------------------+

Comments