//------------------------------------------------------------------

   #property copyright "mladen"
   #property link      "www.forex-tsd.com"
   
//------------------------------------------------------------------
#property indicator_separate_window
#property indicator_buffers 8
#property indicator_color1  DeepSkyBlue
#property indicator_color2  DeepSkyBlue
#property indicator_color3  PaleVioletRed
#property indicator_color4  PaleVioletRed
#property indicator_color5  LimeGreen
#property indicator_color6  Orange
#property indicator_color7  Orange
#property indicator_color8  Peru
#property indicator_width1  2
#property indicator_width3  2
#property indicator_width5  2
#property indicator_width6  2
#property indicator_width7  2
#property indicator_style8  STYLE_DOT

//
//
//
//
//

extern ENUM_TIMEFRAMES TimeFrame        = PERIOD_CURRENT;
extern int             ROCPeriod1       = 14;
extern int             ROCPeriod2       = 10;
extern int             Smooth           = 11;
extern int             Signal           = 5;
extern ENUM_MA_METHOD  SignalMethod     = MODE_SMA;
extern bool            alertsOn         = false;
extern bool            alertsOnCurrent  = true;
extern bool            alertsMessage    = true;
extern bool            alertsSound      = false;
extern bool            alertsNotify     = false;
extern bool            alertsEmail      = false;
extern string          soundFile        = "alert2.wav";
extern bool            ShowArrows       = false;
extern string          arrowsIdentifier = "CC Arrows";
extern double          arrowsUpperGap   = 1.0;
extern double          arrowsLowerGap   = 1.0;
extern color           arrowsUpColor    = LimeGreen;
extern color           arrowsDnColor    = Red;
extern color           arrowsUpCode     = 241;
extern color           arrowsDnCode     = 242;
extern bool            arrowsOnFirst    = false;
extern bool            Interpolate      = true;

extern bool            ShowHistogram    = true;

//
//
//
//
//

double histUu[];
double histUd[];
double histDd[];
double histDu[];
double osc[];
double oscda[];
double oscdb[];
double signal[];
double slope[];
double trend[];
string indicatorFileName;
bool   returnBars;

//------------------------------------------------------------------
//
//------------------------------------------------------------------
//
//
//
//

int init()
{
   int style = DRAW_NONE; if (ShowHistogram) style = DRAW_HISTOGRAM;
   IndicatorBuffers(10);
      SetIndexBuffer(0,histUu); SetIndexStyle(0,style); SetIndexLabel(0,"Coppock curve");
      SetIndexBuffer(1,histUd); SetIndexStyle(1,style); SetIndexLabel(1,"Coppock curve");
      SetIndexBuffer(2,histDd); SetIndexStyle(2,style); SetIndexLabel(2,"Coppock curve");
      SetIndexBuffer(3,histDu); SetIndexStyle(3,style); SetIndexLabel(3,"Coppock curve");
      SetIndexBuffer(4,osc);                            SetIndexLabel(4,"Coppock curve");
      SetIndexBuffer(5,oscda);                          SetIndexLabel(5,"Coppock curve");
      SetIndexBuffer(6,oscdb);                          SetIndexLabel(6,"Coppock curve"); 
      SetIndexBuffer(7,signal);                         SetIndexLabel(7,"Coppock curve signal");
      SetIndexBuffer(8,slope);
      SetIndexBuffer(9,trend);
      indicatorFileName = WindowExpertName();
      returnBars        = TimeFrame==-99;
      TimeFrame         = MathMax(TimeFrame,_Period); 

   IndicatorShortName(timeFrameToString(TimeFrame)+" Coppock curve "+" ("+ROCPeriod1+","+ROCPeriod2+","+Smooth+","+Signal+")");
return(0);
}
int deinit()
{
   string lookFor       = arrowsIdentifier+":";
   int    lookForLength = StringLen(lookFor);
   for (int i=ObjectsTotal()-1; i>=0; i--)
   {
      string objectName = ObjectName(i);
         if (StringSubstr(objectName,0,lookForLength) == lookFor) ObjectDelete(objectName);
   }
   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 (returnBars)  { histUu[0] = limit+1; return(0); } 
      
      //
      //
      //
      //
      //
      if (TimeFrame == Period())
      {  
        if (slope[limit] == -1) CleanPoint(limit,oscda,oscdb);
        for (int i=limit;i>=0;i--)
        {
           osc[i]    = iLwma(iROC(ROCPeriod1,i)+iROC(ROCPeriod2,i),Smooth,i,0);
           oscda[i]  = EMPTY_VALUE;
           oscdb[i]  = EMPTY_VALUE;
           histUu[i] = EMPTY_VALUE;
           histUd[i] = EMPTY_VALUE;
           histDd[i] = EMPTY_VALUE;
           histDu[i] = EMPTY_VALUE;
           slope[i] = slope[i+1];
           
              if (osc[i] > osc[i+1]) slope[i] =  1;
              if (osc[i] < osc[i+1]) slope[i] = -1;
              if (osc[i] > 0 && slope[i] ==  1) histUu[i] = osc[i];
              if (osc[i] > 0 && slope[i] == -1) histUd[i] = osc[i];
              if (osc[i] < 0 && slope[i] ==  1) histDu[i] = osc[i];
              if (osc[i] < 0 && slope[i] == -1) histDd[i] = osc[i];
              if (slope[i] == -1) PlotPoint(i,oscda,oscdb,osc);      
      }
      for (i=limit; i>=0; i--) 
      {
         signal[i] = iMAOnArray(osc,0,Signal,0,SignalMethod,i);
         trend[i]  = trend[i+1];
         if (osc[i]>signal[i]) trend[i] = 1;
         if (osc[i]<signal[i]) trend[i] =-1;
         
         //
		   //
		   //
		   //
		   //
		
		   if (ShowArrows)
                {
                  ObjectDelete(arrowsIdentifier+":"+Time[i]);
                  if (trend[i]!=trend[i+1])
                  {
                    if (trend[i] == 1)  drawArrow(i,arrowsUpColor,arrowsUpCode,false);
                    if (trend[i] ==-1)  drawArrow(i,arrowsDnColor,arrowsDnCode, true);
                  }
               }
      } 
      
      //
      //
      //
      //
      //
      
      if (alertsOn)
      {
         if (alertsOnCurrent)
              int whichBar = 0;
         else     whichBar = 1;
         if (trend[whichBar] != trend[whichBar+1])
         {
            if (trend[whichBar] == 1)   doAlert(whichBar,"up");
            if (trend[whichBar] ==-1)   doAlert(whichBar,"down");
         }         
      }
   return(0);
   }
      
   //
   //
   //
   //
   //
 
    limit = MathMax(limit,MathMin(Bars-1,iCustom(NULL,TimeFrame,indicatorFileName,-99,0,0)*TimeFrame/Period()));
    if (slope[limit] == -1) CleanPoint(limit,oscda,oscdb);
    for(i=limit; i >= 0; i--)  
    {
       int y = iBarShift(NULL,TimeFrame,Time[i]);
          histUu[i] = iCustom(NULL,TimeFrame,indicatorFileName,PERIOD_CURRENT,ROCPeriod1,ROCPeriod2,Smooth,Signal,SignalMethod,alertsOn,alertsOnCurrent,alertsMessage,alertsSound,alertsNotify,alertsEmail,soundFile,ShowArrows,arrowsIdentifier,arrowsUpperGap,arrowsLowerGap,arrowsUpColor,arrowsDnColor,arrowsUpCode,arrowsDnCode,arrowsOnFirst,0,y);
          histUd[i] = iCustom(NULL,TimeFrame,indicatorFileName,PERIOD_CURRENT,ROCPeriod1,ROCPeriod2,Smooth,Signal,SignalMethod,alertsOn,alertsOnCurrent,alertsMessage,alertsSound,alertsNotify,alertsEmail,soundFile,ShowArrows,arrowsIdentifier,arrowsUpperGap,arrowsLowerGap,arrowsUpColor,arrowsDnColor,arrowsUpCode,arrowsDnCode,arrowsOnFirst,1,y);
          histDd[i] = iCustom(NULL,TimeFrame,indicatorFileName,PERIOD_CURRENT,ROCPeriod1,ROCPeriod2,Smooth,Signal,SignalMethod,alertsOn,alertsOnCurrent,alertsMessage,alertsSound,alertsNotify,alertsEmail,soundFile,ShowArrows,arrowsIdentifier,arrowsUpperGap,arrowsLowerGap,arrowsUpColor,arrowsDnColor,arrowsUpCode,arrowsDnCode,arrowsOnFirst,2,y);
          histDu[i] = iCustom(NULL,TimeFrame,indicatorFileName,PERIOD_CURRENT,ROCPeriod1,ROCPeriod2,Smooth,Signal,SignalMethod,alertsOn,alertsOnCurrent,alertsMessage,alertsSound,alertsNotify,alertsEmail,soundFile,ShowArrows,arrowsIdentifier,arrowsUpperGap,arrowsLowerGap,arrowsUpColor,arrowsDnColor,arrowsUpCode,arrowsDnCode,arrowsOnFirst,3,y);
          osc[i]    = iCustom(NULL,TimeFrame,indicatorFileName,PERIOD_CURRENT,ROCPeriod1,ROCPeriod2,Smooth,Signal,SignalMethod,alertsOn,alertsOnCurrent,alertsMessage,alertsSound,alertsNotify,alertsEmail,soundFile,ShowArrows,arrowsIdentifier,arrowsUpperGap,arrowsLowerGap,arrowsUpColor,arrowsDnColor,arrowsUpCode,arrowsDnCode,arrowsOnFirst,4,y);
          signal[i] = iCustom(NULL,TimeFrame,indicatorFileName,PERIOD_CURRENT,ROCPeriod1,ROCPeriod2,Smooth,Signal,SignalMethod,alertsOn,alertsOnCurrent,alertsMessage,alertsSound,alertsNotify,alertsEmail,soundFile,ShowArrows,arrowsIdentifier,arrowsUpperGap,arrowsLowerGap,arrowsUpColor,arrowsDnColor,arrowsUpCode,arrowsDnCode,arrowsOnFirst,7,y);
          slope[i]  = iCustom(NULL,TimeFrame,indicatorFileName,PERIOD_CURRENT,ROCPeriod1,ROCPeriod2,Smooth,Signal,SignalMethod,alertsOn,alertsOnCurrent,alertsMessage,alertsSound,alertsNotify,alertsEmail,soundFile,ShowArrows,arrowsIdentifier,arrowsUpperGap,arrowsLowerGap,arrowsUpColor,arrowsDnColor,arrowsUpCode,arrowsDnCode,arrowsOnFirst,8,y);
          oscda[i]  = EMPTY_VALUE;
          oscdb[i]  = EMPTY_VALUE;
         
          //
          //
          //
          //
          //
      
          if (!Interpolate || y==iBarShift(NULL,TimeFrame,Time[i-1])) continue;

          //
          //
          //
          //
          //

          datetime time = iTime(NULL,TimeFrame,y);
             for(int n = 1; i+n < Bars && Time[i+n] >= time; n++) continue;
             for(int x = 1; x < n; x++) 
             {
                osc[i+x]    = osc[i]     + (osc[i+n]    - osc[i])    * x/n;
                signal[i+x] = signal[i]  + (signal[i+n] - signal[i]) * x/n;
                if (histUu[i]!= EMPTY_VALUE) histUu[i+x] = osc[i+x];
                if (histUd[i]!= EMPTY_VALUE) histUd[i+x] = osc[i+x];
                if (histDd[i]!= EMPTY_VALUE) histDd[i+x] = osc[i+x];
                if (histDu[i]!= EMPTY_VALUE) histDu[i+x] = osc[i+x];
             }               
   }
   for(i=limit; i >= 0; i--) if (slope[i] == -1) PlotPoint(i,oscda,oscdb,osc); 
   return(0); 
}
     

//------------------------------------------------------------------
//
//------------------------------------------------------------------
//
//
//
//
//

double workLwma[][1];
double iLwma(double price, double period, int r, int instanceNo=0)
{
   if (ArraySize(workLwma)!= Bars) ArrayResize(workLwma,Bars); r = Bars-r-1;
   
   //
   //
   //
   //
   //
   
   workLwma[r][instanceNo] = 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 iROC(int period, int shift)
{
   double ROC;

   if (Close[shift + period] != 0)
          ROC = (Close[shift]-Close[shift + period]) / Close[shift + period];
   else   ROC = 0.00;
   return(ROC*100.00);
}

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
//
//
//
//
//

void CleanPoint(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;
      }
}

//+-------------------------------------------------------------------
//|                                                                  
//+-------------------------------------------------------------------
//
//
//
//
//

void doAlert(int forBar, string doWhat)
{
   static string   previousAlert="nothing";
   static datetime previousTime;
   string message;
   
      if (previousAlert != doWhat || previousTime != Time[forBar]) {
          previousAlert  = doWhat;
          previousTime   = Time[forBar];

          //
          //
          //
          //
          //

           message =  StringConcatenate(Symbol()," ",timeFrameToString(_Period)," at ",TimeToStr(TimeLocal(),TIME_SECONDS)," Coppock Curve ",doWhat);
             if (alertsMessage) Alert(message);
             if (alertsNotify)  SendNotification(message);
             if (alertsEmail)   SendMail(StringConcatenate(Symbol(), Period(), " Coppock Curve "),message);
             if (alertsSound)   PlaySound("alert2.wav");
      }
}

//
//
//
//
//

void drawArrow(int i,color theColor,int theCode,bool up)
{
   string name = arrowsIdentifier+":"+Time[i];
   double gap  = iATR(NULL,0,20,i);  
   int    add  = 0; if (!arrowsOnFirst) add = _Period*60-1;  
   
      //
      //
      //
      //
      //
      
      ObjectCreate(name,OBJ_ARROW,0,Time[i]+add,0);
         ObjectSet(name,OBJPROP_ARROWCODE,theCode);
         ObjectSet(name,OBJPROP_COLOR,theColor);
         if (up)
               ObjectSet(name,OBJPROP_PRICE1,High[i] + arrowsUpperGap * gap);
         else  ObjectSet(name,OBJPROP_PRICE1,Low[i]  - arrowsLowerGap * gap);
}


//+-------------------------------------------------------------------
//|                                                                  
//+-------------------------------------------------------------------
//
//
//
//
//

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("");
}