//+------------------------------------------------------------------+
//|                                                         Grid.mq4 |
//|                                                          4xcoder |
//|                                              4xcoder@4xcoder.com |
//+------------------------------------------------------------------+
#property copyright "4xcoder"
#property link      "4xcoder@4xcoder.com"

#property indicator_chart_window
//---- input parameters
extern int       HGridWeeks=2;       // Period over which to calc High/Low of grid
extern int       HGridPips=500;        // Size of grid in pips
extern color     Line_Color=White;     // Grid line color



bool firstTime = true;
datetime lastTime = 0;
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
   int    counted_bars=IndicatorCounted();
//----
   if ( true /*lastTime == 0 || CurTime() - lastTime > 5*/ ) {
      firstTime = false;
      lastTime = CurTime();
      
      if ( HGridWeeks > 0 && HGridPips > 0 ) {
         double weekH = iHigh( NULL, PERIOD_W1, 0 );
         double weekL = iLow( NULL, PERIOD_W1, 0 );
         
         for ( int i = 1; i < HGridWeeks; i++ ) {
            weekH = MathMax( weekH, iHigh( NULL, PERIOD_W1, i ) );
            weekL = MathMin( weekL, iLow( NULL, PERIOD_W1, i ) );
         }
      
         double pipRange = HGridPips * Point;
         if ( Symbol() == "GOLD" )
            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 );
            
                   
            double pp = p / Point;
            int pInt = MathRound( pp );
            int mod = 100;
            if ( Symbol() == "GOLD" )
               mod = 1000;
            if ( (pInt % mod) == 0 )
               ObjectCreate( gridname, OBJ_TREND, 0, Time[0]*2, p, 0, p);
               ObjectSet( gridname, OBJPROP_COLOR, Line_Color );
               ObjectSet( gridname, OBJPROP_STYLE, STYLE_DOT );
               ObjectSet( gridname, OBJPROP_PRICE1, p );   
                            
      }
    }            
   }
   


//----
   return(0);
  }

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- indicators
   firstTime = true;
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//----

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