#property indicator_separate_window
#property indicator_buffers 3
#property strict

input int MA_TF = 1440;//Таймфрейм для МА
extern int Razm_Trenda = 40; //Угол наклона в пунктах для МА
input int iBar = 2; //Кол-во свечей для анализа
input int MAperiod = 10;//МА период
input int MAmethod = 0;// Метод усреднения МА / 0-SMA, 1-EMA, 2-SMMA

#property indicator_color1 clrNavy
#property indicator_width1 4
#property indicator_color2 clrRed
#property indicator_width2 4
#property indicator_color3 clrGold
#property indicator_width3 4

double Razm_Tendenzii;

double UpTrend[];
double DownTrend[];
double Flat[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit(void)
{
   if(Digits==3 || Digits==5)
   {
      Razm_Trenda*=10;
   }

   IndicatorShortName(" MA Trend ");
   IndicatorDigits(Digits);
   IndicatorBuffers(3);

   SetIndexBuffer(0,UpTrend);
   SetIndexStyle(0,DRAW_HISTOGRAM,STYLE_SOLID);

   SetIndexBuffer(1,DownTrend);
   SetIndexStyle(1,DRAW_HISTOGRAM,STYLE_SOLID);

   SetIndexBuffer(2,Flat);
   SetIndexStyle(2,DRAW_HISTOGRAM,STYLE_SOLID); 
   
   IndicatorSetDouble(INDICATOR_MAXIMUM,1);
   IndicatorSetDouble(INDICATOR_MINIMUM,0);

   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[])
{  
   if(Period() < 1440) Alert("Нужно уменьшить угол наклона");
   
   Razm_Tendenzii = Razm_Trenda*Point;
   
   int Counted_bars=IndicatorCounted(); // Количество просчитанных баров 
   int limit = Bars-Counted_bars;        // Индекс первого непосчитанного
   
   for(int i=1;i<=limit;i++)             // Цикл по непосчитанным барам
   {
     if(MathAbs(iMA(Symbol(),MA_TF,MAperiod,0,MAmethod,0,i) - iMA(Symbol(),MA_TF,MAperiod,0,MAmethod,0,i+iBar)) < Razm_Tendenzii) 
     {
       Flat[i] = 1;
     }
     else  
     {
       if(iMA(Symbol(),MA_TF,MAperiod,0,MAmethod,0,i) - iMA(Symbol(),MA_TF,MAperiod,0,MAmethod,0,i+iBar) > Razm_Tendenzii)
       UpTrend[i] = 1;
       if(iMA(Symbol(),MA_TF,MAperiod,0,MAmethod,0,i+iBar) - iMA(Symbol(),MA_TF,MAperiod,0,MAmethod,0,i) > Razm_Tendenzii)
       DownTrend[i] = 1;
     }
      
   } 
   
   return(rates_total);
}
//+------------------------------------------------------------------+



