//------------------------------------------------------------------
#property copyright "www.forex-station.com"
#property link      "www.forex-station.com"
//------------------------------------------------------------------
#property indicator_separate_window
#property indicator_buffers 10
#property indicator_color1  clrSilver
#property indicator_color2  clrSilver
#property indicator_color3  clrSilver
#property indicator_color4  clrKhaki
#property indicator_style2  STYLE_DOT
//#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
};
enum enMaTypes
{
   ma_sma,    // Simple moving average
   ma_ema,    // Exponential moving average
   ma_smma,   // Smoothed MA
   ma_lwma,   // Linear weighted MA
   ma_tema    // Triple exponential moving average - TEMA
};
enum enColorOn
{
   cc_onSlope,   // Change color on slope change
   cc_onMiddle,  // Change color on middle line cross
   cc_onLevels   // Change color on outer levels cross
};
enum enLevelType
{
   lvl_floa,  // Floating levels
   lvl_quan   // Quantile levels
};


extern int         PreSmoothPeriod   = 20;             // Pcci pre smooth average period
extern enMaTypes   PreSmoothMaType   = ma_sma;         // Pcci pre smooth average type
extern enPrices    PcciPrice         = pr_close;       // Pcci Price
extern int         PcciSmoothLength  = 10;             // Pcci smooth length
extern enMaTypes   PcciSmoothMaType  = ma_sma;         // Pcci smooth average type
extern enLevelType LevelsType        = lvl_floa;       // Levels type
extern int         LevelsPeriod      = 25;             // Levels period
extern double      LevelsUp          = 80;             // Upper level %
extern double      LevelsDown        = 20;             // Lower level %
extern enColorOn   ColorOn           = cc_onLevels;    // Color change on :
extern color       ColorUp           = clrLimeGreen;   // Color for up
extern color       ColorDown         = clrRed;         // Color for down
extern int         LineWidth         = 3;              // Main line width
extern int         ShadowWidth       = 0;              // Shadow width (<=0 main line width+3)


double buffer1[],buffer1da[],buffer1db[],buffer1ua[],buffer1ub[],levup[],levmi[],levdn[],trend[],shadowa[],shadowb[],prices[];


//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
//
//

int init()
{
   int shadowWidth = (ShadowWidth<=0) ? LineWidth+3 : ShadowWidth;
   IndicatorBuffers(12);
   SetIndexBuffer(0,levup);
   SetIndexBuffer(1,levmi);
   SetIndexBuffer(2,levdn);
   SetIndexBuffer(3,buffer1);   SetIndexStyle(3,EMPTY,EMPTY,LineWidth);
   SetIndexBuffer(4,shadowa);   SetIndexStyle(4,EMPTY,EMPTY,shadowWidth);
   SetIndexBuffer(5,shadowb);   SetIndexStyle(5,EMPTY,EMPTY,shadowWidth);
   SetIndexBuffer(6,buffer1ua); SetIndexStyle(6,EMPTY,EMPTY,LineWidth,ColorUp);
   SetIndexBuffer(7,buffer1ub); SetIndexStyle(7,EMPTY,EMPTY,LineWidth,ColorUp);
   SetIndexBuffer(8,buffer1da); SetIndexStyle(8,EMPTY,EMPTY,LineWidth,ColorDown);
   SetIndexBuffer(9,buffer1db); SetIndexStyle(9,EMPTY,EMPTY,LineWidth,ColorDown);
   SetIndexBuffer(10,prices);
   SetIndexBuffer(11,trend);
 
   IndicatorShortName("pcci smoothed fls");
return(0);
}
int deinit()
{
return(0);
}

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
//
//

int start()
{
    int i,counted_bars=IndicatorCounted();
      if(counted_bars<0) return(-1);
      if(counted_bars>0) counted_bars--;
           int limit=fmin(Bars-counted_bars,Bars-1);

   //
   //
   //
   //
   //
   
   int    colorOn = (LevelsPeriod>0) ? ColorOn : cc_onSlope;
   if (trend[limit]==-1) { CleanPoint(limit,buffer1da,buffer1db); CleanPoint(limit,shadowa,shadowb); }
   if (trend[limit]== 1) { CleanPoint(limit,buffer1ua,buffer1ub); CleanPoint(limit,shadowa,shadowb); }
   for (i=limit;i>=0;i--)
   {  
      if (i<Bars-1) 
      {
        prices[i]   = iCustomMa(PreSmoothMaType,getPrice(PcciPrice,Open,Close,High,Low,i),PreSmoothPeriod,i,0);
        double response =
          0.1177628838235*prices[i]
         +0.1170077388431*prices[i+1]
         +0.1147601209029*prices[i+2]
         +0.1110729054065*prices[i+3]
         +0.1060324421434*prices[i+4]
         +0.0997560988008*prices[i+5]
         +0.0923890376402*prices[i+6]
         +0.0841000389219*prices[i+7]
         +0.0750766919597*prices[i+8]
         +0.0655202662708*prices[i+9]
         +0.0556401583046*prices[i+10]
         +0.0456482572592*prices[i+11]
         +0.0357530528012*prices[i+12]
         +0.02615415804227*prices[i+13]
         +0.01703731282680*prices[i+14]
         +0.00856960311288*prices[i+15]
         +0.000895555708556*prices[i+16]
         -0.00586627422066*prices[i+17]
         -0.01162552971203*prices[i+18]
         -0.01632152444323*prices[i+19]
         -0.01992414580732*prices[i+20]
         -0.02243335117589*prices[i+21]
         -0.02387813894599*prices[i+22]
         -0.02431430200598*prices[i+23]
         -0.02382176331725*prices[i+24]
         -0.02250140527159*prices[i+25]
         -0.02047075493180*prices[i+26]
         -0.01786000052706*prices[i+27]
         -0.01480733499641*prices[i+28]
         -0.01145463232852*prices[i+29]
         -0.00794304696010*prices[i+30]
         -0.00440829083275*prices[i+31]
         -0.000977238472653*prices[i+32]
         +0.002235823933755*prices[i+33]
         +0.00513191787383*prices[i+34]
         +0.00762972457736*prices[i+35]
         +0.00966747623918*prices[i+36]
         +0.01120310303187*prices[i+37]
         +0.01221499285956*prices[i+38]
         +0.01270127547603*prices[i+39]
         +0.01267881242200*prices[i+40]
         +0.01218165493397*prices[i+41]
         +0.01125820035359*prices[i+42]
         +0.00996954486984*prices[i+43]
         +0.00838605323109*prices[i+44]
         +0.00658436935337*prices[i+45]
         +0.00464439195323*prices[i+46]
         +0.002645759417416*prices[i+47]
         +0.000666068159555*prices[i+48]
         -0.001222776729259*prices[i+49]
         -0.002956488520058*prices[i+50]
         -0.00448011021214*prices[i+51]
         -0.00574988855361*prices[i+52]
         -0.00673332989225*prices[i+53]
         -0.00741117882839*prices[i+54]
         -0.00777644547070*prices[i+55]
         -0.00783388153265*prices[i+56]
         -0.00759966748743*prices[i+57]
         -0.00709931269272*prices[i+58]
         -0.00636769053766*prices[i+59]
         -0.00544559940274*prices[i+60]
         -0.00437852874428*prices[i+61]
         -0.00321542503890*prices[i+62]
         -0.002005580069262*prices[i+63]
         -0.000798178859997*prices[i+64]
         +0.000361723322003*prices[i+65]
         +0.001433101593600*prices[i+66]
         +0.002380073229874*prices[i+67]
         +0.00317453700490*prices[i+68]
         +0.00379492177651*prices[i+69]
         +0.00422884523807*prices[i+70]
         +0.00447038314289*prices[i+71]
         +0.00452100306476*prices[i+72]
         +0.00439082398691*prices[i+73]
         +0.00409492037175*prices[i+74]
         +0.00365528338580*prices[i+75]
         +0.003096238149856*prices[i+76]
         +0.002446112685573*prices[i+77]
         +0.001736376831290*prices[i+78]
         +0.000996103380422*prices[i+79]
         +0.0002557237615143*prices[i+80]
         -0.000458282529397*prices[i+81]
         -0.001119581482823*prices[i+82]
         -0.001704893115208*prices[i+83]
         -0.002199032152313*prices[i+84]
         -0.002586916898054*prices[i+85]
         -0.002861741841535*prices[i+86]
         -0.003018651749353*prices[i+87]
         -0.003060101508137*prices[i+88]
         -0.002997112675297*prices[i+89]
         -0.002834938217095*prices[i+90]
         -0.002589089321820*prices[i+91]
         -0.002272072078644*prices[i+92]
         -0.001902835889698*prices[i+93]
         -0.001502872027811*prices[i+94]
         -0.001077466851623*prices[i+95]
         -0.000655481642188*prices[i+96]
         -0.0002511660753314*prices[i+97]
         +0.0001137882502083*prices[i+98]
         +0.000426098842482*prices[i+99]
         +0.000714775119642*prices[i+100]
         +0.000916639580735*prices[i+101]
         +0.001063116482197*prices[i+102]
         +0.001143113996535*prices[i+103]
         +0.001159315803654*prices[i+104]
         +0.00922554212244*prices[i+105]; 
         
         //
         //
         //
         //
         //
         
         buffer1[i]  = iCustomMa(PcciSmoothMaType,prices[i]-response,PcciSmoothLength,i,1);
         buffer1da[i] = EMPTY_VALUE;
         buffer1db[i] = EMPTY_VALUE;
         buffer1ua[i] = EMPTY_VALUE;
         buffer1ub[i] = EMPTY_VALUE;
         shadowa[i]   = EMPTY_VALUE;
         shadowb[i]   = EMPTY_VALUE;
         double hi = buffer1[i]; double lo = buffer1[i];
            if (LevelsType==lvl_floa)
               {
                  if (LevelsPeriod>0)
                  {
                     hi = buffer1[ArrayMaximum(buffer1,LevelsPeriod,i)];
                     lo = buffer1[ArrayMinimum(buffer1,LevelsPeriod,i)];
                     hi = lo+(hi-lo)*LevelsUp  /100.0;
                     lo = lo+(hi-lo)*LevelsDown/100.0;
                  }                     
                  levup[i] = hi;
                  levdn[i] = lo;
                  levmi[i] = (levup[i]+levdn[i])/2.0;
               }
            else
               {
                  levup[i] = iQuantile(buffer1[i],LevelsPeriod, LevelsUp  ,i,Bars);
                  levdn[i] = iQuantile(buffer1[i],LevelsPeriod, LevelsDown,i,Bars);
                  levmi[i] = iQuantile(buffer1[i],LevelsPeriod,(LevelsDown+LevelsUp)/2,i,Bars);
               }                                 

         switch(colorOn)
         {
            case cc_onLevels:         trend[i] = (buffer1[i]>levup[i])     ? 1 : (buffer1[i]<levdn[i])     ? -1 : 0; break;
            case cc_onMiddle:         trend[i] = (buffer1[i]>levmi[i])     ? 1 : (buffer1[i]<levmi[i])     ? -1 : 0; break;
            default :  if (i<Bars-1)  trend[i] = (buffer1[i]>buffer1[i+1]) ? 1 : (buffer1[i]<buffer1[i+1]) ? -1 : trend[i+1];
         }                  
         if (trend[i] == -1) { PlotPoint(i,buffer1da,buffer1db,buffer1); PlotPoint(i,shadowa,shadowb,buffer1); }
         if (trend[i] ==  1) { PlotPoint(i,buffer1ua,buffer1ub,buffer1); PlotPoint(i,shadowa,shadowb,buffer1); }
      }
   }
return(0);
}  

//------------------------------------------------------------------
//                                                                  
//------------------------------------------------------------------
//
//
//
//
//

#define _maInstances 2
#define _maWorkBufferx1 1*_maInstances
#define _maWorkBufferx2 2*_maInstances
#define _maWorkBufferx3 3*_maInstances

double iCustomMa(int mode, double price, double length, int r, int instanceNo=0)
{
   int bars = Bars; r = bars-r-1;
   switch (mode)
   {
      case ma_sma   : return(iSma(price,(int)length,r,bars,instanceNo));
      case ma_ema   : return(iEma(price,length,r,bars,instanceNo));
      case ma_smma  : return(iSmma(price,(int)length,r,bars,instanceNo));
      case ma_lwma  : return(iLwma(price,(int)length,r,bars,instanceNo));
      case ma_tema  : return(iTema(price,(int)length,r,bars,instanceNo));
      default       : return(price);
   }
}

//
//
//
//
//

double workSma[][_maWorkBufferx2];
double iSma(double price, int period, int r, int _bars, int instanceNo=0)
{
   if (ArrayRange(workSma,0)!= _bars) ArrayResize(workSma,_bars); instanceNo *= 2; int k;

   workSma[r][instanceNo+0] = price;
   workSma[r][instanceNo+1] = price; for(k=1; k<period && (r-k)>=0; k++) workSma[r][instanceNo+1] += workSma[r-k][instanceNo+0];  
   workSma[r][instanceNo+1] /= 1.0*k;
   return(workSma[r][instanceNo+1]);
}

//
//
//
//
//

double workEma[][_maWorkBufferx1];
double iEma(double price, double period, int r, int _bars, int instanceNo=0)
{
   if (ArrayRange(workEma,0)!= _bars) ArrayResize(workEma,_bars);

   workEma[r][instanceNo] = price;
   if (r>0 && period>1)
          workEma[r][instanceNo] = workEma[r-1][instanceNo]+(2.0/(1.0+period))*(price-workEma[r-1][instanceNo]);
   return(workEma[r][instanceNo]);
}

//
//
//
//
//

double workSmma[][_maWorkBufferx1];
double iSmma(double price, double period, int r, int _bars, int instanceNo=0)
{
   if (ArrayRange(workSmma,0)!= _bars) ArrayResize(workSmma,_bars);

   workSmma[r][instanceNo] = price;
   if (r>1 && period>1)
          workSmma[r][instanceNo] = workSmma[r-1][instanceNo]+(price-workSmma[r-1][instanceNo])/period;
   return(workSmma[r][instanceNo]);
}

//
//
//
//
//

double workLwma[][_maWorkBufferx1];
double iLwma(double price, double period, int r, int _bars, int instanceNo=0)
{
   if (ArrayRange(workLwma,0)!= _bars) ArrayResize(workLwma,_bars);
   
   workLwma[r][instanceNo] = price; if (period<=1) return(price);
      double sumw = period;
      double sum  = period*price;

      for(int k=1; k<period && (r-k)>=0; k++)
      {
         double weight = period-k;
                sumw  += weight;
                sum   += weight*workLwma[r-k][instanceNo];  
      }             
      return(sum/sumw);
}

double workTema[][_maWorkBufferx3];
#define _tema1 0
#define _tema2 1
#define _tema3 2

double iTema(double price, double period, int r, int bars, int instanceNo=0)
{
   if (period<=1) return(price);
   if (ArrayRange(workTema,0)!= bars) ArrayResize(workTema,bars); instanceNo*=3;

   //
   //
   //
   //
   //
      
   workTema[r][_tema1+instanceNo] = price;
   workTema[r][_tema2+instanceNo] = price;
   workTema[r][_tema3+instanceNo] = price;
   double alpha = 2.0 / (1.0+period);
   if (r>0)
   {
          workTema[r][_tema1+instanceNo] = workTema[r-1][_tema1+instanceNo]+alpha*(price                         -workTema[r-1][_tema1+instanceNo]);
          workTema[r][_tema2+instanceNo] = workTema[r-1][_tema2+instanceNo]+alpha*(workTema[r][_tema1+instanceNo]-workTema[r-1][_tema2+instanceNo]);
          workTema[r][_tema3+instanceNo] = workTema[r-1][_tema3+instanceNo]+alpha*(workTema[r][_tema2+instanceNo]-workTema[r-1][_tema3+instanceNo]); }
   return(workTema[r][_tema3+instanceNo]+3.0*(workTema[r][_tema1+instanceNo]-workTema[r][_tema2+instanceNo]));
}

//-------------------------------------------------------------------
//                                                                  
//-------------------------------------------------------------------
//
//
//
//
//

#define _quantileInstances 1
double _sortQuant[];
double _workQuant[][_quantileInstances];

double iQuantile(double value, int period, double qp, int i, int bars, int instanceNo=0)
{
   if (period<1) return(value);
   if (ArrayRange(_workQuant,0)!=bars) ArrayResize(_workQuant,bars); 
   if (ArraySize(_sortQuant)!=period)  ArrayResize(_sortQuant,period); 
            i=bars-i-1; _workQuant[i][instanceNo]=value;
            int k=0; for (; k<period && (i-k)>=0; k++) _sortQuant[k] = _workQuant[i-k][instanceNo];
                     for (; k<period            ; k++) _sortQuant[k] = 0;
                     ArraySort(_sortQuant);

   //
   //
   //
   //
   //
   
   double index = (period-1.0)*qp/100.00;
   int    ind   = (int)index;
   double delta = index - ind;
   if (ind == NormalizeDouble(index,5))
         return(            _sortQuant[ind]);
   else  return((1.0-delta)*_sortQuant[ind]+delta*_sortQuant[ind+1]);
}   

//------------------------------------------------------------------
//
//------------------------------------------------------------------
//
//
//
//
//
//

#define priceInstances 1
double workHa[][priceInstances*4];
double getPrice(int tprice, const double& open[], const double& close[], const double& high[], const double& low[], int i, int instanceNo=0)
{
  if (tprice>=pr_haclose)
   {
      if (ArrayRange(workHa,0)!= Bars) ArrayResize(workHa,Bars); instanceNo*=4;
         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 (tprice)
         {
            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);        
            case pr_hatbiased2:
               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);
}   

//-------------------------------------------------------------------
//                                                                  
//-------------------------------------------------------------------
//
//
//
//
//

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; }
}
   
             
        