//+------------------------------------------------------------------+
//|                                                bs_MAs_Trend.mq4 |
//|                                   Anatoliy Koschak aka bespaniki |
//|                                        mailto:akoschak@gmail.com |
//+------------------------------------------------------------------+
#property copyright "Anatoliy Koschak aka bespaniki"
#property link      "mailto:akoschak@gmail.com"
#property version   "2.01"
#property strict
#property indicator_chart_window
#property indicator_buffers 17



//--- input parameters
input int      Inp_MA1_Period=5;
input int      Inp_MA2_Period=10;
input int      Inp_MA3_Period=15;
input int      Inp_MA4_Period=20;
input int      Inp_MA5_Period=50;
input ENUM_MA_METHOD Inp_MA_Mode=MODE_EMA; // MODE of MA. Common for all MA
input bool     Inp_Trend_Show_Internal_MAs=true; // Show internal MAs in trend
input bool     Inp_Trend_Show_Hatch=false; // Show hatch in trend
input bool     Inp_Range_Show_Internal_MAs=true; // Show internal MAs in range
input bool     Inp_Range_Show_Hatch=false; // Show hatch in range
//--- indicator buffers

double         Red_Hatch_Buf[];
double         Orange_Hatch_Buf[];
double         Orange2_Hatch_Buf[];
double         Yellow_Hatch_Buf[];
double         Yellow2_Hatch_Buf[];
double         Green_Hatch_Buf[];
double         Green2_Hatch_Buf[];
double         Blue_Hatch_Buf[];
double         Gray_Hatch_Buf[];
double         Gray2_Hatch_Buf[];
double         MA1[];
double         MA2[];
double         MA3[];
double         MA4[];
double         MA5[];
double         Flat_Upper[];
double         Flat_Lower[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
   IndicatorDigits(Digits);
   IndicatorShortName("bs_MAs_Trend v2");

//--- indicator buffers mapping

   SetIndexBuffer(0,Red_Hatch_Buf);
   SetIndexStyle(0,DRAW_HISTOGRAM,STYLE_DOT,1,clrRed);

   SetIndexBuffer(1,Orange_Hatch_Buf);
   SetIndexStyle(1,DRAW_HISTOGRAM,STYLE_DOT,1,clrOrange);

   SetIndexBuffer(2,Orange2_Hatch_Buf);
   SetIndexStyle(2,DRAW_HISTOGRAM,STYLE_DOT,1,clrOrange);

   SetIndexBuffer(3,Yellow_Hatch_Buf);
   SetIndexStyle(3,DRAW_HISTOGRAM,STYLE_DOT,1,clrYellow);

   SetIndexBuffer(4,Yellow2_Hatch_Buf);
   SetIndexStyle(4,DRAW_HISTOGRAM,STYLE_DOT,1,clrYellow);

   SetIndexBuffer(5,Green_Hatch_Buf);
   SetIndexStyle(5,DRAW_HISTOGRAM,STYLE_DOT,1,clrGreen);

   SetIndexBuffer(6,Green2_Hatch_Buf);
   SetIndexStyle(6,DRAW_HISTOGRAM,STYLE_DOT,1,clrGreen);

   SetIndexBuffer(7,Blue_Hatch_Buf);
   SetIndexStyle(7,DRAW_HISTOGRAM,STYLE_DOT,1,clrBlue);

   SetIndexBuffer(8,Gray_Hatch_Buf);
   SetIndexStyle(8,DRAW_HISTOGRAM,STYLE_DOT,1,clrLightGray);

   SetIndexBuffer(9,Gray2_Hatch_Buf);
   SetIndexStyle(9,DRAW_HISTOGRAM,STYLE_DOT,1,clrLightGray);

   SetIndexBuffer(10,MA1);
   SetIndexStyle(10,DRAW_LINE,STYLE_SOLID,1,clrRed);

   SetIndexBuffer(11,MA2);
   SetIndexStyle(11,DRAW_LINE,STYLE_SOLID,1,clrOrange);

   SetIndexBuffer(12,MA3);
   SetIndexStyle(12,DRAW_LINE,STYLE_SOLID,1,clrYellow);

   SetIndexBuffer(13,MA4);
   SetIndexStyle(13,DRAW_LINE,STYLE_SOLID,1,clrGreen);

   SetIndexBuffer(14,MA5);
   SetIndexStyle(14,DRAW_LINE,STYLE_SOLID,1,clrBlue);

   SetIndexBuffer(15,Flat_Upper);
   SetIndexStyle(15,DRAW_LINE,STYLE_SOLID,1,clrLightGray);

   SetIndexBuffer(16,Flat_Lower);
   SetIndexStyle(16,DRAW_LINE,STYLE_SOLID,1,clrLightGray);

//---
   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[])
  {
//---
   int limit=rates_total-prev_calculated;
//--- main cycle
   for(int i=0; i<limit && !IsStopped(); i++)
     {

      MA1[i] = iMA(NULL,0,Inp_MA1_Period,0,Inp_MA_Mode,PRICE_CLOSE,i);
      MA2[i] = iMA(NULL,0,Inp_MA2_Period,0,Inp_MA_Mode,PRICE_CLOSE,i);
      MA3[i] = iMA(NULL,0,Inp_MA3_Period,0,Inp_MA_Mode,PRICE_CLOSE,i);
      MA4[i] = iMA(NULL,0,Inp_MA4_Period,0,Inp_MA_Mode,PRICE_CLOSE,i);
      MA5[i] = iMA(NULL,0,Inp_MA5_Period,0,Inp_MA_Mode,PRICE_CLOSE,i);
      double MAs[5];
      MAs[0] = MA1[i];
      MAs[1] = MA2[i];
      MAs[2] = MA3[i];
      MAs[3] = MA4[i];
      MAs[4] = MA5[i];
      double upperMA = MAs[ArrayMaximum(MAs)];
      double lowerMA = MAs[ArrayMinimum(MAs)];
      if(
         (MA1[i]>MA2[i] && MA2[i]>MA3[i] && MA3[i]>MA4[i] && MA4[i]>MA5[i]) || 
         (MA1[i]<MA2[i] && MA2[i]<MA3[i] && MA3[i]<MA4[i] && MA4[i]<MA5[i])
         )
        {

         Flat_Upper[i]=EMPTY_VALUE;
         Flat_Lower[i]=EMPTY_VALUE;

         if(Inp_Trend_Show_Hatch)
           {
            Red_Hatch_Buf[i]=MA1[i];
            Orange_Hatch_Buf[i]=MA2[i];
            Orange2_Hatch_Buf[i]=Orange_Hatch_Buf[i];
            Yellow_Hatch_Buf[i]=MA3[i];
            Yellow2_Hatch_Buf[i]=Yellow_Hatch_Buf[i];
            Green_Hatch_Buf[i]=MA4[i];
            Green2_Hatch_Buf[i]=Green_Hatch_Buf[i];
            Blue_Hatch_Buf[i]=MA5[i];
            Gray_Hatch_Buf[i] = EMPTY_VALUE;
            Gray2_Hatch_Buf[i]= EMPTY_VALUE;
            Flat_Upper[i] = EMPTY_VALUE;
            Flat_Lower[i] = EMPTY_VALUE;
           }

         if(!Inp_Trend_Show_Internal_MAs)
           {
            MA1[i]=(MA1[i]==upperMA || MA1[i]==lowerMA)? MA1[i]: EMPTY_VALUE;
            MA2[i]=(MA2[i]==upperMA || MA2[i]==lowerMA)? MA2[i]: EMPTY_VALUE;
            MA3[i]=(MA3[i]==upperMA || MA3[i]==lowerMA)? MA3[i]: EMPTY_VALUE;
            MA4[i]=(MA4[i]==upperMA || MA4[i]==lowerMA)? MA4[i]: EMPTY_VALUE;
            MA5[i]=(MA5[i]==upperMA || MA5[i]==lowerMA)? MA5[i]: EMPTY_VALUE;
           }
        }
      else
        {

         Flat_Upper[i]=upperMA;
         Flat_Lower[i]=lowerMA;

         if(Inp_Range_Show_Hatch)
           {
            Gray_Hatch_Buf[i]=upperMA;
            Gray2_Hatch_Buf[i]=lowerMA;

            Red_Hatch_Buf[i]=EMPTY_VALUE;
            Orange_Hatch_Buf[i]=EMPTY_VALUE;
            Orange2_Hatch_Buf[i]=EMPTY_VALUE;
            Yellow_Hatch_Buf[i]=EMPTY_VALUE;
            Yellow2_Hatch_Buf[i]=EMPTY_VALUE;
            Green_Hatch_Buf[i]=EMPTY_VALUE;
            Green2_Hatch_Buf[i]=EMPTY_VALUE;
            Blue_Hatch_Buf[i]=EMPTY_VALUE;
           }

         if(!Inp_Range_Show_Internal_MAs)
           {
            MA1[i]=(MA1[i]==Flat_Upper[i] || MA1[i]==Flat_Lower[i])? MA1[i]: EMPTY_VALUE;
            MA2[i]=(MA2[i]==Flat_Upper[i] || MA2[i]==Flat_Lower[i])? MA2[i]: EMPTY_VALUE;
            MA3[i]=(MA3[i]==Flat_Upper[i] || MA3[i]==Flat_Lower[i])? MA3[i]: EMPTY_VALUE;
            MA4[i]=(MA4[i]==Flat_Upper[i] || MA4[i]==Flat_Lower[i])? MA4[i]: EMPTY_VALUE;
            MA5[i]=(MA5[i]==Flat_Upper[i] || MA5[i]==Flat_Lower[i])? MA5[i]: EMPTY_VALUE;
           }
         else
           {
            Flat_Upper[i]=EMPTY_VALUE;
            Flat_Lower[i]=EMPTY_VALUE;
           }
        }

     }

//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+
