//+------------------------------------------------------------------+
//|                                               FibGrid_Lines.mq4 |
//|                        Copyright 2015, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2015, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict
#property indicator_chart_window
#property indicator_buffers 21
#property indicator_color1 Silver
#property indicator_color2 Silver
#property indicator_color3 Silver
#property indicator_color4 Silver
#property indicator_color5 Silver
#property indicator_color6 Silver
#property indicator_color7 Silver
#property indicator_color8 Silver
#property indicator_color9 Silver
#property indicator_color10 Silver
#property indicator_color11 Silver
#property indicator_color12 Silver
#property indicator_color13 Silver
#property indicator_color14 Silver
#property indicator_color15 Silver
#property indicator_color16 Silver
#property indicator_color17 Silver
#property indicator_color18 Silver
#property indicator_color19 Silver
#property indicator_color20 Silver
#property indicator_width1 1
#property indicator_width2 1
#property indicator_width3 1
#property indicator_width4 1
#property indicator_width5 1
#property indicator_width6 1
#property indicator_width7 1
#property indicator_width8 1
#property indicator_width9 1
#property indicator_width10 1
#property indicator_width11 1
#property indicator_width12 1
#property indicator_width13 1
#property indicator_width14 1
#property indicator_width15 1
#property indicator_width16 1
#property indicator_width17 1
#property indicator_width18 1
#property indicator_width19 1
#property indicator_width20 1


#define NSL  10 
#define NMA  7

double   fml[NMA];
int      _bsl[NSL]    = {10, 16, 26, 42, 68, 111, 179, 290, 470, 760};
double   bsl[NSL];
int      ma_period[7] = {21,34,55,75,100,144,233};
double   tmpArray[NMA], lowestMA, highestMA;
double   PipPoints, point;
int      offset;

double S1U[], S1L[], S2U[], S2L[],S3U[], S3L[], S4U[], S4L[],S5U[], S5L[], S6U[], S6L[], S7U[], S7L[],S8U[], S8L[],S9U[], S9L[],S10U[], S10L[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
   IndicatorBuffers(21);
   SetIndexBuffer(19,S1U); SetIndexStyle(0,DRAW_LINE); SetIndexEmptyValue(0,0); 
   SetIndexBuffer(18,S1L); SetIndexStyle(1,DRAW_LINE); SetIndexEmptyValue(1,0); 
   SetIndexBuffer(17,S2U); SetIndexStyle(2,DRAW_LINE); SetIndexEmptyValue(2,0); 
   SetIndexBuffer(16,S2L); SetIndexStyle(3,DRAW_LINE); SetIndexEmptyValue(3,0); 
   SetIndexBuffer(15,S3U); SetIndexStyle(4,DRAW_LINE); SetIndexEmptyValue(4,0); 
   SetIndexBuffer(14,S3L); SetIndexStyle(5,DRAW_LINE); SetIndexEmptyValue(5,0); 
   SetIndexBuffer(13,S4U); SetIndexStyle(6,DRAW_LINE); SetIndexEmptyValue(6,0); 
   SetIndexBuffer(12,S4L); SetIndexStyle(7,DRAW_LINE); SetIndexEmptyValue(7,0); 
   SetIndexBuffer(11,S5U); SetIndexStyle(8,DRAW_LINE); SetIndexEmptyValue(8,0); 
   SetIndexBuffer(10,S5L); SetIndexStyle(9,DRAW_LINE); SetIndexEmptyValue(9,0); 
   SetIndexBuffer(9,S6U); SetIndexStyle(10,DRAW_LINE); SetIndexEmptyValue(10,0); 
   SetIndexBuffer(8,S6L); SetIndexStyle(11,DRAW_LINE); SetIndexEmptyValue(11,0); 
   SetIndexBuffer(7,S7U); SetIndexStyle(12,DRAW_LINE); SetIndexEmptyValue(12,0); 
   SetIndexBuffer(6,S7L); SetIndexStyle(13,DRAW_LINE); SetIndexEmptyValue(13,0); 
   SetIndexBuffer(5,S8U); SetIndexStyle(14,DRAW_LINE); SetIndexEmptyValue(14,0); 
   SetIndexBuffer(4,S8L); SetIndexStyle(15,DRAW_LINE); SetIndexEmptyValue(15,0); 
   SetIndexBuffer(3,S9U); SetIndexStyle(16,DRAW_LINE); SetIndexEmptyValue(16,0); 
   SetIndexBuffer(2,S9L); SetIndexStyle(17,DRAW_LINE); SetIndexEmptyValue(17,0); 
   SetIndexBuffer(1,S10U); SetIndexStyle(18,DRAW_LINE); SetIndexEmptyValue(18,0); 
   SetIndexBuffer(0,S10L); SetIndexStyle(19,DRAW_LINE); SetIndexEmptyValue(19,0); 

   
      point   = 0.01;
      if(Digits > 3) point = 0.0001;
   
      if(Digits==3 || Digits==5)
      {
      PipPoints=Point*10;
      }
   else
      {
      PipPoints=Point;
      }
      
      
      for(int i=0; i<NSL; i++)  bsl[i] = _bsl[i]*point;
       
   
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| 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[]) 
  {
//---
   
   
   for (int q = Bars-1; q>=0; q--){

      for(int i=0; i<NMA; i++)  fml[i] = iMA(NULL, 0, ma_period[i], 0, 0, PRICE_CLOSE, q);  // populating the fml array...
      
      for(int i=0; i<NMA; i++) tmpArray[i] = fml[i];                                                               // finding the highest and lowest MAs from the (7) fml's/MA's
         ArraySort(tmpArray);                                                                                            
         lowestMA  = tmpArray[0];
         highestMA = tmpArray[6];


         S1U[q] = lowestMA  + bsl[0]; 
         S1L[q] = highestMA - bsl[0]; 
         S2U[q] = lowestMA  + bsl[1];
         S2L[q] = highestMA - bsl[1];
         S3U[q] = lowestMA  + bsl[2];
         S3L[q] = highestMA - bsl[2];
         S4U[q] = lowestMA  + bsl[3];
         S4L[q] = highestMA - bsl[3];
         S5U[q] = lowestMA  + bsl[4];
         S5L[q] = highestMA - bsl[4];
         S6U[q] = lowestMA  + bsl[5];
         S6L[q] = highestMA - bsl[5];
         S7U[q] = lowestMA  + bsl[6];
         S7L[q] = highestMA - bsl[6];
         S8U[q] = lowestMA  + bsl[7];
         S8L[q] = highestMA - bsl[7];
         S9U[q] = lowestMA  + bsl[8];
         S9L[q] = highestMA - bsl[8];
         S10U[q] = lowestMA  + bsl[9];
         S10L[q] = highestMA - bsl[9];
         
        
         
         if (S10U[q]<=S10L[q]) {S1U[q]=0; S1L[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q]=0; S4U[q]=0; S4L[q]=0; S5U[q]=0; S5L[q]=0;S6U[q]=0; S6L[q]=0;}
         else if (S5U[q]<=S5L[q]) {S5U[q]=0;S5L[q]=0;S4U[q]=0;S4L[q]=0;S3U[q]=0;S3L[q]=0;S2U[q]=0;S2L[q]=0;S1U[q]=0;S1L[q]=0;}//;S1L[q]=S6L[q];S2U[q]=S7U[q];S2L[q]=S7L[q];S3U[q]=S8U[q];S3L[q]=S8L[q];S4U[q]=S9U[q];S4L[q]=S9L[q];S5U[q]=S10U[q];S5L[q]=S10L[q];S6U[q]=0;S6L[q]=0;S7U[q]=0;S7L[q]=0;S8U[q]=0;S8L[q]=0;S9U[q]=0; S9L[q]=0; S10U[q]=0; S10L[q]=0;}// {S1U[q]=0; S1L[q]=0; S2U[q]=0; S2L[q]=0; S3U[q]=0; S3L[q]=0; S4U[q]=0; S4L[q]=0; S5U[q]=0; S5L[q]=0;}
         else if (S4U[q]<=S4L[q]) {S4U[q]=0;S4L[q]=0;S3U[q]=0;S3L[q]=0;S2U[q]=0;S2L[q]=0;S1U[q]=0;S1L[q]=0;}//;S1L[q]=S5L[q];S2U[q]=S6U[q];S2L[q]=S6L[q];S3U[q]=S7U[q];S3L[q]=S7L[q];S4U[q]=S8U[q];S4L[q]=S8L[q];S5U[q]=S9U[q];S5L[q]=S9L[q];S6U[q]=S10U[q];S6L[q]=S10L[q];S7U[q]=0;S7L[q]=0;S8U[q]=0;S8L[q]=0;S9U[q]=0; S9L[q]=0; S10U[q]=0; S10L[q]=0;}// {S1U[q]=0; S1L[q]=0; S2U[q]=0; S2L[q]=0; S3U[q]=0; S3L[q]=0; S4U[q]=0; S4L[q]=0;}
         else if (S3U[q]<=S3L[q]) {S3U[q]=0;S3L[q]=0;S2U[q]=0;S2L[q]=0;S1U[q]=0;S1L[q]=0;}//;S1L[q]=S4L[q];S2U[q]=S5U[q];S2L[q]=S5L[q];S3U[q]=S6U[q];S3L[q]=S5L[q];S4U[q]=S7U[q];S4L[q]=S7L[q];S5U[q]=S8U[q];S5L[q]=S8L[q];S6U[q]=S9U[q];S6L[q]=S9L[q];S7U[q]=S10U[q];S7L[q]=S10L[q];S8U[q]=0;S8L[q]=0;S9U[q]=0; S9L[q]=0; S10U[q]=0; S10L[q]=0;}// {S1U[q]=0; S1L[q]=0; S2U[q]=0; S2L[q]=0; S3U[q]=0; S3L[q]=0;}
         else if (S2U[q]<=S2L[q]) {S2U[q]=0;S2L[q]=0;S1U[q]=0;S1L[q]=0;}//;S1L[q]=S3L[q];S2U[q]=S4U[q];S2L[q]=S4L[q];S3U[q]=S5U[q];S3L[q]=S4L[q];S4U[q]=S6U[q];S4L[q]=S6L[q];S5U[q]=S7U[q];S5L[q]=S7L[q];S6U[q]=S8U[q];S6L[q]=S8L[q];S7U[q]=S9U[q];S7L[q]=S9L[q];S8U[q]=S10U[q];S8L[q]=S10L[q];S9U[q]=0; S9L[q]=0; S10U[q]=0; S10L[q]=0;}
         else if (S1U[q]<=S1L[q]) {S1U[q]=0;S1L[q]=0;}//;S1L[q]=S2L[q];S2U[q]=S3U[q];S2L[q]=S3L[q];S3U[q]=S4U[q];S3L[q]=S4L[q];S4U[q]=S5U[q];S4L[q]=S5L[q];S5U[q]=S6U[q];S5L[q]=S6L[q];S6U[q]=S7U[q];S6L[q]=S7L[q];S7U[q]=S8U[q];S7L[q]=S8L[q];S8U[q]=S9U[q];S8L[q]=S9L[q];S9U[q]=S10U[q];S9L[q]=S10L[q];S10U[q]=0;S10L[q]=0;}
         else{}
         
        
         
         


   }
   
   
//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+

