//+------------------------------------------------------------------+
//|                                                coppock curve.mq4 |
//|                                        from Mladen Rakic version |
//+------------------------------------------------------------------+
//------------------------------------------------------------------
#property copyright "www.forex-station.cm"
#property link      "www.forex-station.cm"
//------------------------------------------------------------------
#property indicator_separate_window
#property indicator_buffers 2
#property indicator_minimum 0
#property indicator_maximum 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
};

extern ENUM_TIMEFRAMES TimeFrame      = PERIOD_CURRENT;    // Time frame to use
input enPrices         RocPrice       = pr_close;          // Price to use
input int              RocPeriod1     = 14;                // Roc period 1
input int              RocPeriod2     = 10;                // Roc period 2
input int              Smooth         = 11;                // Lwma Roc smoothing
input int              histoWidth     = 3;                 // Histogram bars width
input color            upHistoColor   = clrLimeGreen;      // Bullish coppock curve color
input color            dnHistoColor   = clrRed;            // Bearish coppock curve color
input bool             Interpolate    = true;              // Interpolate mtf true/false?

double osc[],oscda[],oscdb[],slope[],count[];
string indicatorFileName;
#define _mtfCall(_buff,_ind) iCustom(NULL,TimeFrame,indicatorFileName,PERIOD_CURRENT,RocPrice,RocPeriod1,RocPeriod2,Smooth,_buff,_ind)

//------------------------------------------------------------------
//
//------------------------------------------------------------------
//
//
//
//

int OnInit()
{
      IndicatorBuffers(6);
      SetIndexBuffer(0,oscda,INDICATOR_DATA); SetIndexStyle(0,DRAW_HISTOGRAM,EMPTY,histoWidth,upHistoColor); SetIndexLabel(0,"Coppock curve up");
      SetIndexBuffer(1,oscdb,INDICATOR_DATA); SetIndexStyle(1,DRAW_HISTOGRAM,EMPTY,histoWidth,dnHistoColor); SetIndexLabel(1,"Coppock curve down");
      SetIndexBuffer(2,osc);    
      SetIndexBuffer(3,slope);
      SetIndexBuffer(4,count);
      
      indicatorFileName = WindowExpertName();
      TimeFrame         = fmax(TimeFrame,_Period); 

   IndicatorSetString(INDICATOR_SHORTNAME,timeFrameToString(TimeFrame)+" Coppock curve "+" ("+(string)RocPeriod1+","+(string)RocPeriod2+","+(string)Smooth+")");
return(INIT_SUCCEEDED);
}
void OnDeinit(const int reason) {  }

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
//
//
//
//
//

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 i=rates_total-prev_calculated+1; if (i>=rates_total) i=rates_total-1;  count[0] = i;
      if (TimeFrame != _Period)
      {
         i = (int)fmax(i,fmin(rates_total-1,_mtfCall(4,0)*TimeFrame/_Period));
            for (; i>=0 && !_StopFlag; i--)
            {
                int y = iBarShift(NULL,TimeFrame,time[i]);
                   oscda[i] = _mtfCall(0,y);
                   oscdb[i] = _mtfCall(1,y);                          
            }
   return(rates_total);
   }
     
   //
   //
   //
   //
   //

   for (; i>=0 && !_StopFlag; i--)
   {     
       double price = getPrice(RocPrice,open,close,high,low,i,rates_total);
         osc[i]   = iLwma(iRoc(price,RocPeriod1,i,rates_total)+iRoc(price,RocPeriod2,i,rates_total),Smooth,i,rates_total);
         slope[i] = (i<rates_total-1) ? (osc[i] > osc[i+1]) ? 1 : (osc[i] < osc[i+1]) ? -1 : slope[i+1] : 0;  
         oscda[i] = (slope[i] == 1) ? 1 : EMPTY_VALUE;
         oscdb[i] = (slope[i] ==-1) ? 1 : EMPTY_VALUE;     
    }     
return(rates_total);
}      

//------------------------------------------------------------------
//
//------------------------------------------------------------------
//
//
//
//
//

double workLwma[][1];
double iLwma(double price, double period, int r, int bars, int instanceNo=0)
{
   if (ArraySize(workLwma)!= bars) ArrayResize(workLwma,bars); r = bars-r-1;
   
   //
   //
   //
   //
   //
   
   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 rocPrices[];
double iRoc(double price, int period, int shift, int bars)
{
   if (ArraySize(rocPrices)!=bars) ArrayResize(rocPrices,bars); shift = bars-shift-1;
   double ROC; rocPrices[shift] = price;

   if (rocPrices[fmax(shift - period,0)] != 0)
          ROC = (rocPrices[shift]-rocPrices[fmax(shift - period,0)]) / rocPrices[fmax(shift - period,0)];
   else   ROC = 0.00;
   return(ROC*100.00);
}

//------------------------------------------------------------------
//
//------------------------------------------------------------------
//
//
//
//
//

#define _prHABF(_prtype) (_prtype>=pr_habclose && _prtype<=pr_habtbiased2)
#define _priceInstances     1
#define _priceInstancesSize 4
double workHa[][_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(workHa,0)!= Bars) ArrayResize(workHa,Bars); instanceNo*=_priceInstancesSize; int r = bars-i-1;
         
         //
         //
         //
         //
         //
         
         double haOpen  = (r>0) ? (workHa[r-1][instanceNo+2] + workHa[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) { 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:
            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);
}

//-------------------------------------------------------------------
//                                                                  
//-------------------------------------------------------------------
//
//
//
//
//

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("");
}
