//+------------------------------------------------------------------+
//|                                         .mq4 |
//|                         |
//+------------------------------------------------------------------+

/*
  +------------------------------------------------------------------+
  
  +------------------------------------------------------------------+
*/   

#property indicator_minimum 0.0
#property indicator_maximum 3.0

#property indicator_separate_window
#property indicator_buffers 2
#property indicator_color1 Blue
#property indicator_width1 3
#property indicator_color2 Red
#property indicator_width2 3
//----
IndicatorShortName ("MA RSI");
IndicatorDigits (0);
double CrossUp[];
double CrossDown[];
//int Mode = 1;
extern int TimeFrame = 0;
extern int Fast_MA_Period = 14;
extern int Fast_MA_Mode = 1;
extern int Fast_Applied_Price = 0;
extern int Slow_MA_Period = 20;
extern int Slow_MA_Mode = 0;
extern int Slow_Applied_Price = 0;
//extern int Slowest_MA_Period = 13;
//extern int Slowest_MA_Mode = 1;
//extern int Slowest_Applied_Price = 0;
extern  string RSI_Parameters = " ----RSI Variables ---  ";
extern int RSI_TimeFrame = 0;
extern int RSI_Period = 17;
extern int RSI_Sell_Below = 49;
extern int RSI_Buy_Above = 51;
extern int RSI_LowerTimeFrame = 5;
extern int MaValueGap = 2;


//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- indicators



   SetIndexStyle(0, DRAW_HISTOGRAM, EMPTY, 4);

 //  SetIndexStyle(0, DRAW_ARROW, EMPTY, 1);  
//   SetIndexArrow(0, 117);
//   SetIndexArrow(0, 233);
   SetIndexBuffer(0, CrossUp);
   SetIndexStyle(1, DRAW_HISTOGRAM, EMPTY, 4);
//   SetIndexStyle(1, DRAW_HISTOGRAM, STYLE_SOLID);
   SetIndexArrow(1, 117);
 //  SetIndexArrow(1, 234);
   SetIndexBuffer(1, CrossDown);
//----
   return(0);
  }
//DRAW_HISTOGRAM
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//---- 

//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start() {
   int limit, i, counter;
   double fasterEMAnow, slowerEMAnow, fasterEMAprevious, slowerEMAprevious;
      double RSICurrent,RSICurrentLowerTF,RSIPreviousLowerTF;   

         double Range, AvgRange;
   int counted_bars=IndicatorCounted();
//---- check for possible errors
   if(counted_bars<0) return(-1);
//---- last counted bar will be recounted
   if(counted_bars>0) counted_bars--;

   limit=Bars-counted_bars;
   
   for(i = 0; i <= limit; i++) {
   
      counter=i;
      for (counter=i ;counter<=i+9;counter++)
      
       
      fasterEMAnow = iMA(NULL, TimeFrame, Fast_MA_Period, 0, Fast_MA_Mode, Fast_Applied_Price, i);
//      fasterEMAprevious = iMA(NULL, TimeFrame, Fast_MA_Period, 0, Fast_MA_Mode, Fast_Applied_Price, i+1);
//      fasterEMAafter = iMA(NULL, TimeFrame, Fast_MA_Period, 0, Fast_MA_Mode, Fast_Applied_Price, i-1);

      slowerEMAnow = iMA(NULL, TimeFrame, Slow_MA_Period, 0, Slow_MA_Mode, Slow_Applied_Price, i);
//      slowerEMAprevious = iMA(NULL, TimeFrame, Slow_MA_Period, 0, Slow_MA_Mode, Slow_Applied_Price, i+1);
//      slowerEMAafter = iMA(NULL, TimeFrame, Slow_MA_Period, 0, Slow_MA_Mode, Slow_Applied_Price, i-1);

//      slowestEMAnow = iMA(NULL, TimeFrame, Slowest_MA_Period, 0, Slowest_MA_Mode, Slowest_Applied_Price, i);
//      slowestEMAprevious = iMA(NULL, TimeFrame, Slowest_MA_Period, 0, Slowest_MA_Mode, Slowest_Applied_Price, i+1);
      RSICurrent =  iRSI( NULL,RSI_TimeFrame,RSI_Period, PRICE_CLOSE, i + 0) ;
//      RSIPrevious =  iRSI( NULL,RSI_TimeFrame,RSI_Period, PRICE_CLOSE, i+1) ;      
      RSICurrentLowerTF =  iRSI( NULL,RSI_LowerTimeFrame,RSI_Period, PRICE_CLOSE, i + 0) ;
      RSIPreviousLowerTF =  iRSI( NULL,RSI_LowerTimeFrame,RSI_Period, PRICE_CLOSE, i+1) ;      
      if ((fasterEMAnow > slowerEMAnow)
    //  && slowerEMAnow > slowerEMAprevious && slowerEMAnow > slowestEMAnow && slowestEMAnow > slowestEMAprevious 
      && RSICurrent > RSI_Buy_Above && RSICurrentLowerTF > RSIPreviousLowerTF
      && MathAbs(fasterEMAnow - slowerEMAnow)/Point > MaValueGap) {
         CrossUp[i] = 1;
      }
//(fasterEMAnow < fasterEMAprevious)&&  (fasterEMAnow > fasterEMAprevious)&& RSICurrent > RSIPrevious && RSICurrent < RSIPrevious && 
    else if ((fasterEMAnow < slowerEMAnow) 
   //   && slowerEMAnow < slowerEMAprevious && slowerEMAnow < slowestEMAnow && slowestEMAnow < slowestEMAprevious 
     && RSICurrent < RSI_Sell_Below && RSICurrentLowerTF < RSIPreviousLowerTF
      && MathAbs(fasterEMAnow - slowerEMAnow)/Point > MaValueGap) {
         CrossDown[i] = 1;
      }
   }
   return(0);
}

