//+------------------------------------------------------------------+
//|                                                  iMFIL_Trade.mq4 |
//|                                                                * |
//|                                                                * |
//+------------------------------------------------------------------+
#property copyright "http://dmffx.com"
#property link      "http://dmffx.com"

#property indicator_chart_window
#property indicator_buffers 2
#property indicator_color1 DeepSkyBlue
#property indicator_color2 Red

   /*
      Индикатор рисует на графике цены стрелки в местах пересечения 
      линии индикатора MFI с уровнями.
   */

//---- input parameters
extern int MFIPeriod    =  14;   // Период MFI
extern int MFILevel     =  80;   // Уровень MFI для стрелок вниз (пересечение сверху вниз), нижний рассчитывается как 100-MFILevel (пересечение снизу вверх);

//---- buffers
double buy[];
double sell[];
double pt;
int drb;
double ULevel,LLevel;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
   
   ULevel=MFILevel;
   LLevel=100.0-MFILevel;
   pt=Point*PTA(Period());
   
//---- indicators
   SetIndexStyle(0,DRAW_ARROW);
   SetIndexArrow(0,233);
   SetIndexBuffer(0,buy);
   SetIndexEmptyValue(0,0.0);
   SetIndexStyle(1,DRAW_ARROW);
   SetIndexArrow(1,234);
   SetIndexBuffer(1,sell);
   SetIndexEmptyValue(1,0.0);
   
   SetIndexLabel(0,"");
   SetIndexLabel(1,"");
   
   drb=MFIPeriod+1;
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
         int limit=MathMin(Bars-IndicatorCounted(),Bars-drb);

            for(int i=limit-1;i>=0;i--){
               double mfi0=iMFI(NULL,0,MFIPeriod,i);
               double mfi1=iMFI(NULL,0,MFIPeriod,i+1);
               buy[i]=0;
               sell[i]=0;
                  if(mfi0>LLevel){
                     if(mfi1<=LLevel){
                        buy[i]=Low[i]-pt;
                     }
                  }
                  if(mfi0<ULevel){
                     if(mfi1>=ULevel){
                        sell[i]=High[i]+pt;
                     }
                  }                  
            }
   return(0);
  }
//+------------------------------------------------------------------+

int PTA(int aPeriod){
      switch(aPeriod){
         case PERIOD_M1:
            return(4);
         case PERIOD_M5:
            return(6);
         case PERIOD_M15:
            return(8);
         case PERIOD_M30:
            return(13);             
         case PERIOD_H1:
            return(17);  
         case PERIOD_H4:
            return(40);  
         case PERIOD_D1:
            return(70);                   
         case PERIOD_W1:
            return(150); 
         case PERIOD_MN1:
            return(300);          
      }
   return(5);
}

