0_grid_v1_0_mod_horizontal

Download script
Author: Copyright 2014, MetaQuotes Software Corp.
0_grid_v1_0_mod_horizontal

This tool is designed to enhance your trading chart by automatically generating a visual "Price Grid" based on historical price action. Instead of manually drawing horizontal lines, this script calculates a structured framework that helps traders identify key price levels across different timeframes.

How the Logic Works

1. Historical Range Identification The script begins by analyzing historical data over a user-defined number of weeks. It scans through these weeks to identify the highest and lowest price points reached during that period. This ensures that the grid is automatically scaled to be relevant to the current market environment rather than being static or arbitrary.

2. Dynamic Grid Calculation Once the high and low boundaries are established, the script calculates a "Price Step" (the distance between each horizontal line).

  • The size of these steps is fully customizable by the user.
  • The logic includes specific adjustments for assets like Gold (XAUUSD), ensuring that the grid spacing remains accurate regardless of the currency's decimal precision.

3. Automated Line Placement The script then populates the chart with horizontal lines, starting from the lowest historical point up to the highest. It handles all the "heavy lifting" by:

  • Calculating exactly where each line should sit based on your chosen grid size.
  • Drawing these lines automatically across the entire visible chart.

4. Visual Organization (Major vs. Minor Levels) To prevent the chart from becoming cluttered, the script differentiates between types of price levels:

  • Major Levels: Every few intervals (depending on the asset), the script applies a distinct style (like a different width or color). This helps highlight significant "round numbers" or major structural zones.
  • Minor Levels: The remaining lines are drawn as standard grid markers to provide a sense of depth and price progression without overwhelming the visual field.

5. Clean Workspace Management The script is designed to be professional and non-intrusive. It includes an optional on-screen label to show your current grid settings at a glance. Additionally, it features an automatic cleanup routine; as soon as you remove the script from your chart, it instantly deletes all the drawn grid lines, leaving your trading workspace clean and ready for the next analysis.

Price Data Components
Series array that contains the highest prices of each barSeries array that contains the lowest prices of each bar
6 Views
0 Downloads
0 Favorites
0_grid_v1_0_mod_horizontal
//+------------------------------------------------------------------
//|                                                         Grid.mq4 
//|                                                          4xcoder 
//|                                              4xcoder@4xcoder.com 
//| 
//| Original Indicator: http://www.forexfactory.com/showthread.php?t=10128 (Post #1)
//| Modifications File45: https://login.mql5.com/en/users/file45/publications
//+------------------------------------------------------------------
#property copyright "Copyright 2014, MetaQuotes Software Corp."
#property link      "http://www.mql5.com"
#property version   "1.00"
#property strict
#property indicator_chart_window

input int   HGrid_Points = 100; // H-Grid Size in points
input color HLine_1 = clrLightSlateGray;      // H-Grid 1 Color
input color HLine_2 = clrLightSlateGray;       // H-Grid 2 Color
input int   Line_1_Style = 2;     // Line 1 Style
input int   Line_1_Width = 1;     // Line 1 Width
input int   Line_2_Style = 2;     // Line 2 Style
input int   Line_2_Width = 1;     // Line 2 Width
input int   HGrid_Weeks = 10;    // Weeks

input bool Show_Label = true; // Show Label
input color Font_Color = clrDodgerBlue;  // Label Color
input int Font_Size = 11; // Font Size
input bool Font_Bold = false;  // Font Bold
input int Font_Corner = 1;  // Corner
input int Left_Right = 20;  // Left - Right
input int Up_Down = 150;  // Up - Down

string HGrid_text;
string FB;

bool firstTime = true;
datetime lastTime = 0;

int OnInit()
{
   firstTime = true;
   
   switch(Font_Bold)
   {
      case 0 : FB = "Arial"; break;
      case 1 : FB = "Arial Bold"; break;
   }   
   
   return(INIT_SUCCEEDED);
}

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[])
{
   ObjectCreate("HGrid_txt", OBJ_LABEL, 0, 0, 0);
   ObjectSet("HGrid_txt", OBJPROP_CORNER, Font_Corner);
   ObjectSet("HGrid_txt", OBJPROP_XDISTANCE, Left_Right);
   ObjectSet("HGrid_txt", OBJPROP_YDISTANCE, Up_Down); 
   if(Show_Label == 1)
   {
      ObjectSetText("HGrid_txt", "HG : " + string(HGrid_Points), Font_Size, FB, Font_Color);
   }
   else
   {
      ObjectSetText("HGrid_txt", "");
   }      
      
   int    counted_bars=IndicatorCounted();

   if(true /*lastTime == 0 || CurTime() - lastTime > 5*/ ) 
   {
      firstTime = false;
      lastTime = CurTime();
      
      if(HGrid_Weeks > 0 && HGrid_Points > 0 ) 
      {
         double weekH = iHigh( NULL, PERIOD_W1, 0 );
         double weekL = iLow( NULL, PERIOD_W1, 0 );
         
         for(int i = 1; i < HGrid_Weeks; i++ ) 
         {
            weekH = MathMax( weekH, iHigh( NULL, PERIOD_W1, i ) );
            weekL = MathMin( weekL, iLow( NULL, PERIOD_W1, i ) );
         }
      
         double pipRange = HGrid_Points * Point;
         if (Symbol() == "GOLD" || Symbol() == "XAUUSD")
         {
            pipRange = pipRange * 10.0;
         }   

         double topPips = (weekH + pipRange) - MathMod( weekH, pipRange );
         double botPips = weekL - MathMod( weekL, pipRange );
      
         for ( double p = botPips; p <= topPips; p += pipRange ) 
         {
            string gridname = "grid_" + DoubleToStr( p, Digits );
            ObjectCreate( gridname, OBJ_HLINE, 0, 0, p );
            
            double pp = p / Point;
            int pInt = MathRound( pp );
            int mod = 100;
            if ( Symbol() == "GOLD" || Symbol() == "XAUUSD")
               mod = 1000;
            if ( (pInt % mod) == 0 )
            {
               ObjectSet( gridname, OBJPROP_COLOR, HLine_2 );
               ObjectSet( gridname, OBJPROP_STYLE, Line_2_Style );
               ObjectSet( gridname, OBJPROP_WIDTH, Line_2_Width );
            }   
            else
            {
               ObjectSet( gridname, OBJPROP_COLOR, HLine_1 );
               ObjectSet( gridname, OBJPROP_STYLE, Line_1_Style );
               ObjectSet( gridname, OBJPROP_WIDTH, Line_1_Width );
               ObjectSet( gridname, OBJPROP_PRICE1, p );
               ObjectSet( gridname, OBJPROP_BACK, true );
            }   
         }
      }         
   }
   
   return(rates_total);
}

int deinit()
{
   for(int i = ObjectsTotal() - 1; i >= 0; i--) 
   {
      string name = ObjectName( i );
      if ( StringFind( name, "grid_" ) >= 0 ) 
         ObjectDelete( name );
   }
   ObjectDelete("HGrid_txt");
   
   return(0);
}

Comments