//+------------------------------------------------------------------+
//|                                  rsx + ma + tma asymmetric bands |
//|                                                           mladen |
//+------------------------------------------------------------------+
#property copyright "mladen"
#property link      "mladenfx@gmail.com"

#property indicator_separate_window
#property indicator_buffers 7
#property indicator_color1  Lime
#property indicator_color2  Maroon
#property indicator_color3  Maroon
#property indicator_color4  CLR_NONE
#property indicator_color5  CLR_NONE
#property indicator_color6  White
#property indicator_color7  White
#property indicator_style5  STYLE_DOT
#property indicator_width1  4
#property indicator_width2  4
#property indicator_width3  4

//
//
//
//
//

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_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
};
enum enApplyTo
{
   ap_rsx,  // Apply to RSX
   ap_mar   // Apply to signal line (ma of RSX)
};
extern ENUM_TIMEFRAMES TimeFrame               = PERIOD_CURRENT;
extern int             RsxPeriod               = 3;
extern enPrices        RsxPrice                = pr_haclose;
extern int             SignalPeriod            = 2;
extern ENUM_MA_METHOD  SignalMethod            = MODE_SMA;
extern int             TmaHalfLength           = 6;
extern enApplyTo       TmaApplyTo              = ap_rsx; 
extern double          BandsDeviations         = 1.2;
extern bool            drawDivergences         = true;
extern bool            drawIndicatorTrendLines = false;
extern bool            drawPriceTrendLines     = true;
extern color           colorBullishDivergence  = White;
extern color           colorBearishDivergence  = Red;
extern string          UniqueID                = "bomp1";
extern bool            Interpolate             = true;


//
//
//
//
//

double rsx[];
double rsxDa[];
double rsxDb[];
double signal[];
double tmBuffer[];
double upBuffer[];
double dnBuffer[];
double trend[];
double wrkBuffer[][15];

string indicatorFileName;
bool   returnBars;
bool   calculateValue;
string shortName;

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
//
//
//
//
//

int init()
{
   IndicatorBuffers(8);
   SetIndexBuffer(0,rsx);      SetIndexDrawBegin(0,TmaHalfLength+1); SetIndexLabel(0,"RSX");
   SetIndexBuffer(1,rsxDa);    SetIndexDrawBegin(1,TmaHalfLength+1); SetIndexLabel(1,"RSX");
   SetIndexBuffer(2,rsxDb);    SetIndexDrawBegin(2,TmaHalfLength+1); SetIndexLabel(2,"RSX");
   SetIndexBuffer(3,signal);   SetIndexDrawBegin(3,TmaHalfLength+1); SetIndexLabel(3,"RSX signal");
   SetIndexBuffer(4,tmBuffer); SetIndexDrawBegin(4,TmaHalfLength+1);
   SetIndexBuffer(5,upBuffer); SetIndexDrawBegin(5,TmaHalfLength+1);
   SetIndexBuffer(6,dnBuffer); SetIndexDrawBegin(6,TmaHalfLength+1);
   SetIndexBuffer(7,trend);

      TmaHalfLength = MathMax(TmaHalfLength,1);
   
      //
      //
      //
      //
      //
      
         indicatorFileName = WindowExpertName();
         returnBars        = (TimeFrame==-99); if (returnBars) return(0);
         calculateValue    = (TimeFrame==-98);
         if (calculateValue)
         {
            int s = StringFind(UniqueID,":",0);
               shortName = UniqueID;
               UniqueID  = StringSubstr(UniqueID,0,s);
               TimeFrame = 0;
               return(0);
         }            
         TimeFrame = MathMax(TimeFrame,_Period);
      
      //
      //
      //
      //
      //
  

   shortName = UniqueID+": "+timeFrameToString(TimeFrame)+"Rsx ("+RsxPeriod+")";
   IndicatorShortName(shortName);
   return(0);
}

//
//
//
//
//

int deinit()
{
   int length = StringLen(UniqueID);
   for(int i=ObjectsTotal()-1; i>=0; i--)
   {
      string name = ObjectName(i);  if(StringSubstr(name,0,length) == UniqueID)  ObjectDelete(name);   
   }
   return(0);
}




//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
//
//
//
//
//

#define wuBuffer 13
#define wdBuffer 14

int start()
{
   double FullLength = 2.0*TmaHalfLength+1.0;
   int counted_bars=IndicatorCounted();
   int i,j,k,n,r,limit;

   if(counted_bars<0) return(-1);
   if(counted_bars>0) counted_bars--;
         limit = MathMin(Bars-1,Bars-counted_bars+TmaHalfLength);
            if (returnBars) { rsx[0] = limit+1; return(0); }

   //
   //
   //
   //
   //

   if (calculateValue || TimeFrame == Period())
   {
      if (ArrayRange(wrkBuffer,0) != Bars) ArrayResize(wrkBuffer,Bars);
      double Kg = (3.0)/(2.0+RsxPeriod);
      double Hg = 1.0-Kg;
   
      for(i=limit, r=Bars-i-1; i>=0; i--, r++)
      {
         wrkBuffer[r][12] = getPrice(RsxPrice,Open,Close,High,Low,i);

         if (i==(Bars-1)) { for (int c=0; c<12; c++) wrkBuffer[r][c] = 0; continue; }  

         //
         //
         //
         //
         //
      
         double roc = wrkBuffer[r][12]-wrkBuffer[r-1][12];
         double roa = MathAbs(roc);
         for (k=0; k<3; k++)
         {
            int kk = k*2;
               wrkBuffer[r][kk+0] = Kg*roc                + Hg*wrkBuffer[r-1][kk+0];
               wrkBuffer[r][kk+1] = Kg*wrkBuffer[r][kk+0] + Hg*wrkBuffer[r-1][kk+1]; roc = 1.5*wrkBuffer[r][kk+0] - 0.5 * wrkBuffer[r][kk+1];
               wrkBuffer[r][kk+6] = Kg*roa                + Hg*wrkBuffer[r-1][kk+6];
               wrkBuffer[r][kk+7] = Kg*wrkBuffer[r][kk+6] + Hg*wrkBuffer[r-1][kk+7]; roa = 1.5*wrkBuffer[r][kk+6] - 0.5 * wrkBuffer[r][kk+7];
         }
         if (roa != 0)
              rsx[i] = MathMax(MathMin((roc/roa+1.0)*50.0,100.00),0.00); 
         else rsx[i] = 50.0;
         if (drawDivergences)
         {
            CatchBullishDivergence(rsx,i);
            CatchBearishDivergence(rsx,i);
         }           
         
      }
      for (i=limit; i>=0; i--) signal[i] = iMAOnArray(rsx,0,SignalPeriod,0,SignalMethod,i);
   
      //
      //
      //
      //
      //
      
      if (trend[limit] == -1) ClearPoint(limit,rsxDa,rsxDb);
      for(i=limit, r=Bars-i-1; i>=0; i--, r++)
      {
         rsxDa[i] = EMPTY_VALUE;
         rsxDb[i] = EMPTY_VALUE;
         trend[i] = trend[i+1];
            if (rsx[i]>signal[i]) trend[i] =  1;
            if (rsx[i]<signal[i]) trend[i] = -1;
            if (trend[i] == -1) PlotPoint(i,rsxDa,rsxDb,rsx);
            if (TmaApplyTo==0)
               double price = rsx[i];
            else      price = signal[i];

         //
         //
         //
         //
         //
      
         double sum  = (TmaHalfLength+1)*price;
         double sumw = (TmaHalfLength+1);
         for(j=1, k=TmaHalfLength; j<=TmaHalfLength; j++, k--)
         {
            if (TmaApplyTo==0)               
                  sum  += k*rsx[i+j];
            else  sum  += k*signal[i+j];
            sumw += k;

            if (j<=i)
            {
               if (TmaApplyTo==0)               
                     sum  += k*rsx[i-j];
               else  sum  += k*signal[i-j];
               sumw += k;
            }
         }
         tmBuffer[i] = sum/sumw;
   
      //
      //
      //
      //
      //
            
         double diff = price-tmBuffer[i];
            if (i> (Bars-TmaHalfLength-2)) continue;
            if (i==(Bars-TmaHalfLength-2))
            {
               upBuffer[i] = tmBuffer[i];
               dnBuffer[i] = tmBuffer[i];
               if (diff>=0)
                  {
                     wrkBuffer[r][wuBuffer] = MathPow(diff,2);
                     wrkBuffer[r][wdBuffer] = 0;
                  }
               else
                  {               
                     wrkBuffer[r][wdBuffer] = MathPow(diff,2);
                     wrkBuffer[r][wuBuffer] = 0;
                  }                  
               continue;
            }
      
         //
         //
         //
         //
         //
         
         wrkBuffer[r][wuBuffer] = wrkBuffer[r-1][wuBuffer];
         wrkBuffer[r][wdBuffer] = wrkBuffer[r-1][wdBuffer];
            if(diff>0)
            {
               wrkBuffer[r][wuBuffer] = (wrkBuffer[r-1][wuBuffer]*(FullLength-1)+MathPow(diff,2))/FullLength;
               wrkBuffer[r][wdBuffer] =  wrkBuffer[r-1][wdBuffer]*(FullLength-1)/FullLength;
            }
            if(diff<0)
            {
               wrkBuffer[r][wdBuffer] = (wrkBuffer[r-1][wdBuffer]*(FullLength-1)+MathPow(diff,2))/FullLength;
               wrkBuffer[r][wuBuffer] =  wrkBuffer[r-1][wuBuffer]*(FullLength-1)/FullLength;
            }
         upBuffer[i] = tmBuffer[i] + BandsDeviations*MathSqrt(wrkBuffer[r][wuBuffer]);
         dnBuffer[i] = tmBuffer[i] - BandsDeviations*MathSqrt(wrkBuffer[r][wdBuffer]);
      }
      return(0);
   }      
      
   //
   //
   //
   //
   //

   limit = MathMax(limit,MathMin(Bars-1,iCustom(NULL,TimeFrame,indicatorFileName,-99,0,0)*TimeFrame/Period()));
   if (trend[limit] == -1) ClearPoint(limit,rsxDa,rsxDb);
   for(i=limit; i>=0; i--)
   {
      int y = iBarShift(NULL,TimeFrame,Time[i]);
         rsxDa[i]    = EMPTY_VALUE;
         rsxDb[i]    = EMPTY_VALUE;
         rsx[i]      = iCustom(NULL,TimeFrame,indicatorFileName,-98,RsxPeriod,RsxPrice,SignalPeriod,SignalMethod,TmaHalfLength,TmaApplyTo,BandsDeviations,drawDivergences,drawIndicatorTrendLines,drawPriceTrendLines,colorBullishDivergence,colorBearishDivergence,shortName,Interpolate,0,y);
         signal[i]   = iCustom(NULL,TimeFrame,indicatorFileName,-98,RsxPeriod,RsxPrice,SignalPeriod,SignalMethod,TmaHalfLength,TmaApplyTo,BandsDeviations,drawDivergences,drawIndicatorTrendLines,drawPriceTrendLines,colorBullishDivergence,colorBearishDivergence,shortName,Interpolate,3,y);
         tmBuffer[i] = iCustom(NULL,TimeFrame,indicatorFileName,-98,RsxPeriod,RsxPrice,SignalPeriod,SignalMethod,TmaHalfLength,TmaApplyTo,BandsDeviations,drawDivergences,drawIndicatorTrendLines,drawPriceTrendLines,colorBullishDivergence,colorBearishDivergence,shortName,Interpolate,4,y);
         upBuffer[i] = iCustom(NULL,TimeFrame,indicatorFileName,-98,RsxPeriod,RsxPrice,SignalPeriod,SignalMethod,TmaHalfLength,TmaApplyTo,BandsDeviations,drawDivergences,drawIndicatorTrendLines,drawPriceTrendLines,colorBullishDivergence,colorBearishDivergence,shortName,Interpolate,5,y);
         dnBuffer[i] = iCustom(NULL,TimeFrame,indicatorFileName,-98,RsxPeriod,RsxPrice,SignalPeriod,SignalMethod,TmaHalfLength,TmaApplyTo,BandsDeviations,drawDivergences,drawIndicatorTrendLines,drawPriceTrendLines,colorBullishDivergence,colorBearishDivergence,shortName,Interpolate,6,y);
         trend[i]    = iCustom(NULL,TimeFrame,indicatorFileName,-98,RsxPeriod,RsxPrice,SignalPeriod,SignalMethod,TmaHalfLength,TmaApplyTo,BandsDeviations,drawDivergences,drawIndicatorTrendLines,drawPriceTrendLines,colorBullishDivergence,colorBearishDivergence,shortName,Interpolate,7,y);
            
         //
         //
         //
         //
         //
      
         if (!Interpolate || y==iBarShift(NULL,TimeFrame,Time[i-1])) continue;

         //
         //
         //
         //
         //

         datetime time = iTime(NULL,TimeFrame,y);
            for(n = 1; i+n < Bars && Time[i+n] >= time; n++) continue;	
            for(k = 1; k < n; k++)
            {
               rsx[i+k]      = rsx[i]      + (rsx[i+n]      - rsx[i]     )*k/n;
               signal[i+k]   = signal[i]   + (signal[i+n]   - signal[i]  )*k/n;
               tmBuffer[i+k] = tmBuffer[i] + (tmBuffer[i+n] - tmBuffer[i])*k/n;
               upBuffer[i+k] = upBuffer[i] + (upBuffer[i+n] - upBuffer[i])*k/n;
               dnBuffer[i+k] = dnBuffer[i] + (dnBuffer[i+n] - dnBuffer[i])*k/n;
            }               
   }
   for(i=limit; i>=0; i--) if (trend[i] == -1) PlotPoint(i,rsxDa,rsxDb,rsx);
      
   //
   //
   //
   //
   //
   
   return(0);
}

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
//
//
//
//
//

void ClearPoint(int i,double& first[],double& second[])
{
   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 (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;
      }
}

//+-------------------------------------------------------------------
//|                                                                  
//+-------------------------------------------------------------------
//
//
//
//
//

string sTfTable[] = {"M1","M5","M15","M30","H1","H4","D1","W1","MN"};
int    iTfTable[] = {1,5,15,30,60,240,1440,10080,43200};

string timeFrameToString(int tf)
{
   for (int i=ArraySize(iTfTable)-1; i>=0; i--) 
         if (tf==iTfTable[i]) return(sTfTable[i]);
                              return("");
}

//+------------------------------------------------------------------
//|                                                                  
//+------------------------------------------------------------------
//
//
//
//
//

void CatchBullishDivergence(double& values[], int i)
{
   i++;
            ObjectDelete(UniqueID+"l"+DoubleToStr(Time[i],0));
            ObjectDelete(UniqueID+"l"+"os" + DoubleToStr(Time[i],0));            
   if (!IsIndicatorLow(values,i)) return;  

   //
   //
   //
   //
   //

   int currentLow = i;
   int lastLow    = GetIndicatorLastLow(values,i+1);
      if (values[currentLow] > values[lastLow] && Low[currentLow] < Low[lastLow])
      {
         if(drawPriceTrendLines)     DrawPriceTrendLine("l",Time[currentLow],Time[lastLow],Low[currentLow],Low[lastLow],colorBullishDivergence,STYLE_SOLID);
         if(drawIndicatorTrendLines) DrawIndicatorTrendLine("l",Time[currentLow],Time[lastLow],values[currentLow],values[lastLow],colorBullishDivergence,STYLE_SOLID);
      }
      if (values[currentLow] < values[lastLow] && Low[currentLow] > Low[lastLow])
      {
         if(drawPriceTrendLines)     DrawPriceTrendLine("l",Time[currentLow],Time[lastLow],Low[currentLow],Low[lastLow], colorBullishDivergence, STYLE_DOT);
         if(drawIndicatorTrendLines) DrawIndicatorTrendLine("l",Time[currentLow],Time[lastLow],values[currentLow],values[lastLow], colorBullishDivergence, STYLE_DOT);
      }
}

//
//
//
//
//

void CatchBearishDivergence(double& values[], int i)
{
   i++; 
            ObjectDelete(UniqueID+"h"+DoubleToStr(Time[i],0));
            ObjectDelete(UniqueID+"h"+"os" + DoubleToStr(Time[i],0));            
   if (IsIndicatorPeak(values,i) == false) return;

   //
   //
   //
   //
   //
      
   int currentPeak = i;
   int lastPeak = GetIndicatorLastPeak(values,i+1);
      if (values[currentPeak] < values[lastPeak] && High[currentPeak]>High[lastPeak])
      {
         if (drawPriceTrendLines)     DrawPriceTrendLine("h",Time[currentPeak],Time[lastPeak],High[currentPeak],High[lastPeak],colorBearishDivergence,STYLE_SOLID);
         if (drawIndicatorTrendLines) DrawIndicatorTrendLine("h",Time[currentPeak],Time[lastPeak],values[currentPeak],values[lastPeak],colorBearishDivergence,STYLE_SOLID);
      }
      if(values[currentPeak] > values[lastPeak] && High[currentPeak] < High[lastPeak])
      {
         if (drawPriceTrendLines)     DrawPriceTrendLine("h",Time[currentPeak],Time[lastPeak],High[currentPeak],High[lastPeak], colorBearishDivergence, STYLE_DOT);
         if (drawIndicatorTrendLines) DrawIndicatorTrendLine("h",Time[currentPeak],Time[lastPeak],values[currentPeak],values[lastPeak], colorBearishDivergence, STYLE_DOT);
      }
}

//
//
//
//
//

bool IsIndicatorPeak(double& values[], int i) { return(values[i] >= values[i+1] && values[i] > values[i+2] && values[i] > values[i-1]); }
bool IsIndicatorLow( double& values[], int i) { return(values[i] <= values[i+1] && values[i] < values[i+2] && values[i] < values[i-1]); }

int GetIndicatorLastPeak(double& values[], int shift)
{
   for(int i = shift+5; i<Bars; i++)
         if (values[i] >= values[i+1] && values[i] > values[i+2] && values[i] >= values[i-1] && values[i] > values[i-2]) return(i);
   return(-1);
}
int GetIndicatorLastLow(double& values[], int shift)
{
   for(int i = shift+5; i<Bars; i++)
         if (values[i] <= values[i+1] && values[i] < values[i+2] && values[i] <= values[i-1] && values[i] < values[i-2]) return(i);
   return(-1);
}

//+------------------------------------------------------------------
//|                                                                  
//+------------------------------------------------------------------
//
//
//
//
//

void DrawPriceTrendLine(string first,datetime t1, datetime t2, double p1, double p2, color lineColor, double style)
{
   string   label = UniqueID+first+"os"+DoubleToStr(t1,0);
   if (Interpolate) t2 += Period()*60-1;
    
   ObjectDelete(label);
      ObjectCreate(label, OBJ_TREND, 0, t1+Period()*60-1, p1, t2, p2, 0, 0);
         ObjectSet(label, OBJPROP_RAY, false);
         ObjectSet(label, OBJPROP_COLOR, lineColor);
         ObjectSet(label, OBJPROP_STYLE, style);
}
void DrawIndicatorTrendLine(string first,datetime t1, datetime t2, double p1, double p2, color lineColor, double style)
{
   int indicatorWindow = WindowFind(shortName);
   if (indicatorWindow < 0) return;
   if (Interpolate) t2 += Period()*60-1;
   
   string label = UniqueID+first+DoubleToStr(t1,0);
   ObjectDelete(label);
      ObjectCreate(label, OBJ_TREND, indicatorWindow, t1+Period()*60-1, p1, t2, p2, 0, 0);
         ObjectSet(label, OBJPROP_RAY, false);
         ObjectSet(label, OBJPROP_COLOR, lineColor);
         ObjectSet(label, OBJPROP_STYLE, style);
}

//------------------------------------------------------------------
//
//------------------------------------------------------------------
//
//
//
//
//
//

double workHa[][4];
double getPrice(int price, const double& open[], const double& close[], const double& high[], const double& low[], int i, int instanceNo=0)
{
  if (price>=pr_haclose)
   {
      if (ArrayRange(workHa,0)!= Bars) ArrayResize(workHa,Bars);
         int r = Bars-i-1;
         
         //
         //
         //
         //
         //
         
         double haOpen;
         if (r>0)
                haOpen  = (workHa[r-1][instanceNo+2] + workHa[r-1][instanceNo+3])/2.0;
         else   haOpen  = (open[i]+close[i])/2;
         double haClose = (open[i] + high[i] + low[i] + close[i]) / 4.0;
         double haHigh  = MathMax(high[i], MathMax(haOpen,haClose));
         double haLow   = MathMin(low[i] , MathMin(haOpen,haClose));

         if(haOpen  <haClose) { workHa[r][instanceNo+0] = haLow;  workHa[r][instanceNo+1] = haHigh; } 
         else                 { workHa[r][instanceNo+0] = haHigh; workHa[r][instanceNo+1] = haLow;  } 
                                workHa[r][instanceNo+2] = haOpen;
                                workHa[r][instanceNo+3] = haClose;
         //
         //
         //
         //
         //
         
         switch (price)
         {
            case pr_haclose:     return(haClose);
            case pr_haopen:      return(haOpen);
            case pr_hahigh:      return(haHigh);
            case pr_halow:       return(haLow);
            case pr_hamedian:    return((haHigh+haLow)/2.0);
            case pr_hamedianb:   return((haOpen+haClose)/2.0);
            case pr_hatypical:   return((haHigh+haLow+haClose)/3.0);
            case pr_haweighted:  return((haHigh+haLow+haClose+haClose)/4.0);
            case pr_haaverage:   return((haHigh+haLow+haClose+haOpen)/4.0);
            case pr_hatbiased:
               if (haClose>haOpen)
                     return((haHigh+haClose)/2.0);
               else  return((haLow+haClose)/2.0);        
         }
   }
   
   //
   //
   //
   //
   //
 
   switch (price)
   {
      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);        
   }
   return(0);
}