//+------------------------------------------------------------------+
//|                                     Moving averages cross SR.mq4 |
//| coded after the idae of bulliz                                   |
//| so the credits go to him                                         |
//+------------------------------------------------------------------+

#property indicator_chart_window
#property indicator_buffers 4
#property indicator_color1 DeepSkyBlue
#property indicator_color2 Red
#property indicator_color3 DeepSkyBlue
#property indicator_color4 Red

//
//
//
//
//

extern int  FastMaPeriod      = 20;
extern int  FastMaMethod      = MODE_EMA;
extern int  FastMaPrice       = PRICE_CLOSE;
extern int  SlowMaPeriod      = 50;
extern int  SlowMaMethod      = MODE_EMA;
extern int  SlowMaPrice       = PRICE_CLOSE;
extern bool ShowAverages      = true;
extern bool ShowTillChange    = true;
extern int  BarForSRHighsLows = 0;

//
//
//
//
//

double fastMa[];
double slowMa[];
double resistance[];
double support[];
double trend[];

//+------------------------------------------------------------------+
//|                                                                 
//+------------------------------------------------------------------+
//
//
//
//
//

int init()
{
   IndicatorBuffers(5);
   SetIndexBuffer(0,fastMa); if (ShowAverages) SetIndexStyle(0,DRAW_LINE); else SetIndexStyle(0,DRAW_NONE);
   SetIndexBuffer(1,slowMa); if (ShowAverages) SetIndexStyle(1,DRAW_LINE); else SetIndexStyle(1,DRAW_NONE);
   SetIndexBuffer(2,resistance);
   SetIndexBuffer(3,support);
   SetIndexBuffer(4,trend);
   
      SetIndexStyle(2,DRAW_ARROW); SetIndexArrow(2,159);
      SetIndexStyle(3,DRAW_ARROW); SetIndexArrow(3,159);

   return(0);
}
int deinit() { return(0); }

//+------------------------------------------------------------------+
//|                                                                 
//+------------------------------------------------------------------+
//
//
//
//
//

int start()
{
   int counted_bars=IndicatorCounted();
   int i,limit;

   if (counted_bars<0) return(-1);
   if (counted_bars>0) counted_bars--;
          limit = MathMin(Bars-counted_bars,Bars-1);

   //
   //
   //
   //
   //
   
   for (i=limit; i>=0; i--)
   {
      fastMa[i]     = iMA(NULL,0,FastMaPeriod,0,FastMaMethod,FastMaPrice,i);
      slowMa[i]     = iMA(NULL,0,SlowMaPeriod,0,SlowMaMethod,SlowMaPrice,i);
      resistance[i] = resistance[i+1];
      support[i]    = support[i+1];
      trend[i]      = trend[i+1];
   
         //
         //
         //
         //
         //
         
         if (fastMa[i]>slowMa[i]) trend[i] =  1;
         if (fastMa[i]<slowMa[i]) trend[i] = -1;
         
         //
         //
         //
         //
         //
         
         if (trend[i]!=trend[i+1])
         {
            if (trend[i] == 1)
               {
                  resistance[i] = High[i+BarForSRHighsLows];
                  if (!ShowTillChange)
                     support[i] = EMPTY_VALUE;
               }                     
            else
               {
                  support[i] = Low[i+BarForSRHighsLows];
                  if (!ShowTillChange)
                     resistance[i] = EMPTY_VALUE;
               }                     
         }
   }
   return(0);
}