//------------------------------------------------------------------
#property copyright "mladen"
#property link      "mladenfx@gmail.com"
//------------------------------------------------------------------
#property indicator_separate_window
#property indicator_buffers 6
#property indicator_color1 clrDeepSkyBlue
#property indicator_color2 clrSandyBrown
#property indicator_color3 clrSandyBrown
#property indicator_color4 clrDeepSkyBlue
#property indicator_color5 clrSandyBrown
#property indicator_color6 clrSandyBrown
#property indicator_type1  DRAW_LINE
#property indicator_type2  DRAW_LINE
#property indicator_type3  DRAW_LINE
#property indicator_type4  DRAW_LINE
#property indicator_type5  DRAW_LINE
#property indicator_type6  DRAW_LINE
#property indicator_width1 2
#property indicator_width2 2
#property indicator_width3 2
#property indicator_width4 1
#property indicator_width5 1
#property indicator_width6 1
#property strict

//
//
//
//
//

enum enPrices
{
   pr_close,      // Close
   pr_open,       // Open
   pr_high,       // High
   pr_low,        // Low
   pr_median,     // Median
   pr_typical,    // Typical
   pr_weighted,   // Weighted
   pr_average,    // Average (high+low+open+close)/4
   pr_medianb,    // Average median body (open+close)/2
   pr_tbiased,    // Trend biased price
   pr_tbiased2,   // Trend biased (extreme) price
   pr_haclose,    // Heiken ashi close
   pr_haopen ,    // Heiken ashi open
   pr_hahigh,     // Heiken ashi high
   pr_halow,      // Heiken ashi low
   pr_hamedian,   // Heiken ashi median
   pr_hatypical,  // Heiken ashi typical
   pr_haweighted, // Heiken ashi weighted
   pr_haaverage,  // Heiken ashi average
   pr_hamedianb,  // Heiken ashi median body
   pr_hatbiased,  // Heiken ashi trend biased price
   pr_hatbiased2, // Heiken ashi trend biased (extreme) price
   pr_habclose,   // Heiken ashi (better formula) close
   pr_habopen ,   // Heiken ashi (better formula) open
   pr_habhigh,    // Heiken ashi (better formula) high
   pr_hablow,     // Heiken ashi (better formula) low
   pr_habmedian,  // Heiken ashi (better formula) median
   pr_habtypical, // Heiken ashi (better formula) typical
   pr_habweighted,// Heiken ashi (better formula) weighted
   pr_habaverage, // Heiken ashi (better formula) average
   pr_habmedianb, // Heiken ashi (better formula) median body
   pr_habtbiased, // Heiken ashi (better formula) trend biased price
   pr_habtbiased2 // Heiken ashi (better formula) trend biased (extreme) price
};
enum enColorOn
{
   col_onSlope, // Change color on slope change
   col_onZero,  // Change color on zero cross
   col_onCycle  // Change color on trend crossing cycle
};
extern int       EmaPeriodT = 5;           // Trend ema period
extern int       EmaPeriodC = 39;          // Cycle ema period
extern enPrices  EmaPrice   = pr_close;    // Price to use
extern enColorOn ColorOn    = col_onCycle; // Color change on :

//
//
//
//
//

double emat[],ematDa[],ematDb[],emac[],emacDa[],emacDb[],trendt[],trendc[];

//------------------------------------------------------------------
//
//------------------------------------------------------------------
//
//
//
//
//

int init()
{
   IndicatorBuffers(8);
   SetIndexBuffer(0,emac);
   SetIndexBuffer(1,emacDa);
   SetIndexBuffer(2,emacDb);
   SetIndexBuffer(3,emat);
   SetIndexBuffer(4,ematDa);
   SetIndexBuffer(5,ematDb); 
   SetIndexBuffer(6,trendt);
   SetIndexBuffer(7,trendc);
      IndicatorShortName("Reverse ema ("+(string)EmaPeriodT+","+(string)EmaPeriodC+")");
   return(0);
}
int deinit() { return(0); }

//------------------------------------------------------------------
//
//------------------------------------------------------------------
//
//
//
//
//

int start()
{
   int counted_bars=IndicatorCounted();
      if(counted_bars<0) return(-1);
      if(counted_bars>0) counted_bars--;
           int limit=MathMin(Bars-counted_bars,Bars-1);

   //
   //
   //
   //
   //

      
      if (trendt[limit]==-1) CleanPoint(limit,ematDa,ematDb);
      if (trendc[limit]==-1) CleanPoint(limit,emacDa,emacDb);
      for(int i=limit; i>=0; i--)
      {
         double price = getPrice(EmaPrice,Open,Close,High,Low,i,Bars);
         emat[i]   = iRema(price,EmaPeriodT,Bars-i-1,Bars,0);
         emac[i]   = iRema(price,EmaPeriodC,Bars-i-1,Bars,1);
         ematDa[i] = EMPTY_VALUE;
         ematDb[i] = EMPTY_VALUE;
         emacDa[i] = EMPTY_VALUE;
         emacDb[i] = EMPTY_VALUE;
            switch (ColorOn)
            {
               case col_onSlope: 
                     trendt[i] = (i<Bars-1) ? emat[i]>emat[i+1] ? 1 : emat[i]<emat[i+1] ? -1 : trendt[i+1] : 0;
                     trendc[i] = (i<Bars-1) ? emac[i]>emac[i+1] ? 1 : emac[i]<emac[i+1] ? -1 : trendc[i+1] : 0;
                     break;
               case col_onCycle: 
                     trendt[i] = emat[i]>emac[i] ?  1 : emat[i]<emac[i] ? -1 : (i<Bars-1) ? trendt[i+1] : 0;
                     trendc[i] = trendt[i];
                     break;
               case col_onZero: 
                     trendt[i] = emat[i]>0 ?  1 : emat[i]<0 ?  -1 : (i<Bars-1) ? trendt[i+1] : 0;
                     trendc[i] = emac[i]>0 ?  1 : emac[i]<0 ?  -1 : (i<Bars-1) ? trendc[i+1] : 0;
                     break;
            }                     
            if (trendt[i]==-1) PlotPoint(i,ematDa,ematDb,emat);
            if (trendc[i]==-1) PlotPoint(i,emacDa,emacDb,emac);
      }
      return(0);
}


//-------------------------------------------------------------------
//                                                                  
//-------------------------------------------------------------------
//
//
//
//
//

#define _remaInstances     2
#define _remaInstancesSize 9
double workRema[][_remaInstances*_remaInstancesSize];
double iRema(double price, double period, int r, int _bars, int instanceNo=0)
{
   if (ArrayRange(workRema,0)!= _bars) ArrayResize(workRema,_bars); instanceNo*=_remaInstancesSize;

   
   //
   //
   //
   //
   //
   
   double _aa = (2./(1.+period));
   if (r>0 && period>1)
   {
      double _cc = 1. - _aa;
         workRema[r][instanceNo] = _aa*price+_cc*workRema[r-1][instanceNo];
         for (int i=1; i<9; i++)
                workRema[r][i+instanceNo] = MathPow(_cc,MathPow(2,i-1))*workRema[r][i-1+instanceNo]+workRema[r-1][i-1+instanceNo];
         return(workRema[r][  instanceNo]-_aa*workRema[r][8+instanceNo]);
   }          
   else for (int i=0; i<9; i++) workRema[r][i+instanceNo] = price; return(0);
}

//-------------------------------------------------------------------
//                                                                  
//-------------------------------------------------------------------
//
//
//
//
//

void CleanPoint(int i,double& first[],double& second[])
{
   if (i>=Bars-3) return;
   if ((second[i]  != EMPTY_VALUE) && (second[i+1] != EMPTY_VALUE))
        second[i+1] = EMPTY_VALUE;
   else
      if ((first[i] != EMPTY_VALUE) && (first[i+1] != EMPTY_VALUE) && (first[i+2] == EMPTY_VALUE))
          first[i+1] = EMPTY_VALUE;
}

void PlotPoint(int i,double& first[],double& second[],double& from[])
{
   if (i>=Bars-2) return;
   if (first[i+1] == EMPTY_VALUE)
      if (first[i+2] == EMPTY_VALUE) 
            { first[i]  = from[i];  first[i+1]  = from[i+1]; second[i] = EMPTY_VALUE; }
      else  { second[i] =  from[i]; second[i+1] = from[i+1]; first[i]  = EMPTY_VALUE; }
   else     { first[i]  = from[i];                           second[i] = EMPTY_VALUE; }
}


//------------------------------------------------------------------
//
//------------------------------------------------------------------
//
//
//
//
//

#define _prHABF(_prtype) (_prtype>=pr_habclose && _prtype<=pr_habtbiased2)
#define _priceInstances     1
#define _priceInstancesSize 4
double  _priceWorkHa[][_priceInstances*_priceInstancesSize];
double getPrice(int tprice, const double& open[], const double& close[], const double& high[], const double& low[], int i, int bars, int instanceNo=0)
{
  if (tprice>=pr_haclose)
   {
      if (ArrayRange(_priceWorkHa,0)!= Bars) ArrayResize(_priceWorkHa,Bars); instanceNo*=_priceInstancesSize; int r = #ifdef __MQL4__ bars-i-1 #else i #endif;
         
         //
         //
         //
         //
         //
         
         double haOpen  = (r>0) ? (_priceWorkHa[r-1][instanceNo+2] + _priceWorkHa[r-1][instanceNo+3])/2.0 : (open[i]+close[i])/2;;
         double haClose = (open[i]+high[i]+low[i]+close[i]) / 4.0;
         if (_prHABF(tprice))
               if (high[i]!=low[i])
                     haClose = (open[i]+close[i])/2.0+(((close[i]-open[i])/(high[i]-low[i]))*MathAbs((close[i]-open[i])/2.0));
               else  haClose = (open[i]+close[i])/2.0; 
         double haHigh  = fmax(high[i], fmax(haOpen,haClose));
         double haLow   = fmin(low[i] , fmin(haOpen,haClose));

         //
         //
         //
         //
         //
         
         if(haOpen<haClose) { _priceWorkHa[r][instanceNo+0] = haLow;  _priceWorkHa[r][instanceNo+1] = haHigh; } 
         else               { _priceWorkHa[r][instanceNo+0] = haHigh; _priceWorkHa[r][instanceNo+1] = haLow;  } 
                              _priceWorkHa[r][instanceNo+2] = haOpen;
                              _priceWorkHa[r][instanceNo+3] = haClose;
         //
         //
         //
         //
         //
         
         switch (tprice)
         {
            case pr_haclose:
            case pr_habclose:    return(haClose);
            case pr_haopen:   
            case pr_habopen:     return(haOpen);
            case pr_hahigh: 
            case pr_habhigh:     return(haHigh);
            case pr_halow:    
            case pr_hablow:      return(haLow);
            case pr_hamedian:
            case pr_habmedian:   return((haHigh+haLow)/2.0);
            case pr_hamedianb:
            case pr_habmedianb:  return((haOpen+haClose)/2.0);
            case pr_hatypical:
            case pr_habtypical:  return((haHigh+haLow+haClose)/3.0);
            case pr_haweighted:
            case pr_habweighted: return((haHigh+haLow+haClose+haClose)/4.0);
            case pr_haaverage:  
            case pr_habaverage:  return((haHigh+haLow+haClose+haOpen)/4.0);
            case pr_hatbiased:
            case pr_habtbiased:
               if (haClose>haOpen)
                     return((haHigh+haClose)/2.0);
               else  return((haLow+haClose)/2.0);        
            case pr_hatbiased2:
            case pr_habtbiased2:
               if (haClose>haOpen)  return(haHigh);
               if (haClose<haOpen)  return(haLow);
                                    return(haClose);        
         }
   }
   
   //
   //
   //
   //
   //
   
   switch (tprice)
   {
      case pr_close:     return(close[i]);
      case pr_open:      return(open[i]);
      case pr_high:      return(high[i]);
      case pr_low:       return(low[i]);
      case pr_median:    return((high[i]+low[i])/2.0);
      case pr_medianb:   return((open[i]+close[i])/2.0);
      case pr_typical:   return((high[i]+low[i]+close[i])/3.0);
      case pr_weighted:  return((high[i]+low[i]+close[i]+close[i])/4.0);
      case pr_average:   return((high[i]+low[i]+close[i]+open[i])/4.0);
      case pr_tbiased:   
               if (close[i]>open[i])
                     return((high[i]+close[i])/2.0);
               else  return((low[i]+close[i])/2.0);        
      case pr_tbiased2:   
               if (close[i]>open[i]) return(high[i]);
               if (close[i]<open[i]) return(low[i]);
                                     return(close[i]);        
   }
   return(0);
}