#property strict

#property indicator_separate_window

#property indicator_buffers 4
#property indicator_type1   DRAW_HISTOGRAM
#property indicator_width1  1
#property indicator_color1  clrLime
#property indicator_type2   DRAW_HISTOGRAM
#property indicator_width2  1
#property indicator_color2  clrTomato
#property indicator_type3   DRAW_NONE
#property indicator_type4   DRAW_NONE


input ENUM_TIMEFRAMES resLT = PERIOD_D1; //Res Longterm
input ENUM_TIMEFRAMES resMT = PERIOD_H4; //Res Midterm

double up[], dn[], MTc[], MTema20[];



//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int OnInit() {
   IndicatorDigits(Digits);
   IndicatorSetDouble(INDICATOR_MINIMUM, -1);
   IndicatorSetDouble(INDICATOR_MAXIMUM,  1);

   SetIndexLabel(0, "bull");
   SetIndexBuffer(0, up);
   SetIndexLabel(1, "bear");
   SetIndexBuffer(1, dn);

   SetIndexLabel(2, "");
   SetIndexBuffer(2, MTc);
   SetIndexLabel(3, "");
   SetIndexBuffer(3, MTema20);

   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[] ) {

   int limit = fmax(rates_total - prev_calculated - 1, 0);

   for (int i = limit; i >= 0 && !_StopFlag; i--) {
      up[i] = 0;
      dn[i] = 0;

      int resLTShift = iBarShift(NULL, resLT, time[i]);
      int resMTShift = iBarShift(NULL, resMT, time[i]);

      double   LTo = iCustom(NULL, resLT, "Heiken Ashi", 2, resLTShift);
      double   LTc = iCustom(NULL, resLT, "Heiken Ashi", 3, resLTShift);
      double   MTo = iCustom(NULL, resMT, "Heiken Ashi", 2, resMTShift);
      MTc[i] = iCustom(NULL, resMT, "Heiken Ashi", 3, resMTShift);

      bool LTlong  = LTc > LTo ? true : false;
      bool LTshort = LTc < LTo ? true : false;
      MTema20[i] = iMAOnArray(MTc, 0, 20, 0, MODE_EMA, i);

      double MTema20delta = i < rates_total - 1 ? change(MTema20, i) : 0;

      bool MTlong  = MTc[i] > MTo && MTc[i] > MTema20[i] && MTema20delta > 0 ? true : false;
      bool MTshort = MTc[i] < MTo && MTc[i] < MTema20[i] && MTema20delta < 0 ? true : false;

      bool bull  = MTlong  == true && LTlong  == true ? true : false;
      bool bear = MTshort == true && LTshort == true ? true : false;

      if (bull  == true) {
         up[i] =  1;
      }
      if (bear == true) {
         dn[i] = -1;
      }
   }

   return(rates_total);
}

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void OnDeinit(const int reason) {

}

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
double change(double &_src[], int i) {
   return _src[i] - _src[i + 1];
}
//+------------------------------------------------------------------+
