//+------------------------------------------------------------------+
//|                                                     Sledopat.mq4 |
//|                                              Copyright 2018, AM2 |
//|                                      http://www.forexsystems.biz |
//+------------------------------------------------------------------+
#property copyright "Copyright 2018, AM2"
#property link      "http://www.forexsystems.biz"
#property version   "1.00"
#property strict
#property indicator_chart_window

#property indicator_buffers 2

input double BBDev=2;
input int BBPeriod=20;
input int MAPeriod=2;
input int RSIPeriod=9;
input int RSILevel=30;
input int PointSize=2;
input int UPCode=233;
input int DNCode=234;
input color DNColor=Red;
input color UPColor=Blue;

double up[];
double dn[];
datetime t=0;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
   SetIndexBuffer(0,up);
   SetIndexStyle(0,DRAW_ARROW,0,PointSize,Blue);
   SetIndexArrow(0,UPCode);

   SetIndexBuffer(1,dn);
   SetIndexStyle(1,DRAW_ARROW,0,PointSize,Red);
   SetIndexArrow(1,DNCode);
//---
   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 i=0;i<1111;i++)
     {
      double ma1=iMA(NULL,0,MAPeriod,0,0,0,i);
      double ma2=iMA(NULL,0,MAPeriod,0,0,0,i+1);

      double rsi1=iRSI(NULL,0,RSIPeriod,0,i);
      double rsi2=iRSI(NULL,0,RSIPeriod,0,i+1);

      double hi=iBands(NULL,0,BBPeriod,BBDev,0,0,1,i);
      double lo=iBands(NULL,0,BBPeriod,BBDev,0,0,2,i);

      if(ma1>lo && ma2<lo && rsi1>RSILevel && rsi2<RSILevel)
        {
         up[i]=low[i];
        }
      if(ma1<hi && ma2>hi && rsi1<100-RSILevel && rsi2>100-RSILevel)
        {
         dn[i]=high[i];
        }

     }

   if(t!=time[0])
     {
      if(up[1]<1000) Alert(_Symbol+" BUY!!!");
      if(dn[1]<1000) Alert(_Symbol+" SELL!!!");
      t=time[0];
     }
//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+
