//+------------------------------------------------------------------+
//|                                                 CCI Ma cross.mq4 |
//+------------------------------------------------------------------+
#property copyright "www,forex-station.com"
#property link      "www,forex-station.com"

#property indicator_separate_window
#property indicator_buffers    2
#property indicator_color1     clrWhite
#property indicator_color2     clrAqua
#property indicator_width1     2
#property indicator_width2     2

//
//
//
//
//

extern int                 CciPeriod        = 21;
extern ENUM_APPLIED_PRICE  CciPrice         = PRICE_TYPICAL;
extern double              T3Period         = 21;
extern double              T3Hot            = 0.618; 
extern bool                T3Original       = false; 

extern bool                alertsOn         = true;
extern bool                alertsOnCurrent  = true;
extern bool                alertsMessage    = false;
extern bool                alertsSound      = true;
extern bool                alertsEmail      = false;
extern bool                alertsNotify     = false;
extern string              soundfile        = "Cable 5 ready.wav";// "alert2.wav"

extern bool                verticalLinesVisible = true;// ********************
extern string              verticalLinesID      = "CCiLines";
extern color               verticalLinesUpColor = clrDeepSkyBlue;
extern color               verticalLinesDnColor = clrPaleVioletRed;
extern ENUM_LINE_STYLE     verticalLinesStyle   = STYLE_DASHDOT;
extern int                 verticalLinesWidth   = 0;

extern bool                ShowArrows       = true;
extern string              arrowsIdentifier = "CCi_T3_Ma cross Arrows";
extern double              arrowsUpperGap   = 0.5;
extern double              arrowsLowerGap   = 0.5;
extern color               arrowsUpColor    = clrYellow;//*********************
extern color               arrowsDnColor    = clrYellow;
extern color               arrowsUpCode     = 233;//241
extern color               arrowsDnCode     = 234;//242

extern color               LevZeroCol       = clrGold ; 
extern color               LevMidCol        = clrDarkKhaki;  
extern color               LevOutCol        = clrLimeGreen ;

//
//
//
//
//

double cci[];
double ccima[];
double prices[];
double trend[];
double cross[];
string shortName_ADPSN;

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
//
//
//
//
//

int init()
{
   IndicatorBuffers(5);
      SetIndexBuffer(0,cci);
      SetIndexBuffer(1,ccima); 
      SetIndexBuffer(2,prices);
      SetIndexBuffer(3,trend);
      SetIndexBuffer(4,cross);
      shortName_ADPSN = "ADPSN" ;
   IndicatorShortName(shortName_ADPSN);
return(0);
}

int deinit()
{
   deleteArrows();
   deleteLines();
   ObjectDelete("Ln1_ADPSN");  
   ObjectDelete("Ln2_ADPSN"); 
   ObjectDelete("Ln3_ADPSN"); 
   ObjectDelete("Ln4_ADPSN"); 
   ObjectDelete("Ln5_ADPSN");  
return(0);
}


//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
//
//
//
//

int start()
{
   int counted_bars=IndicatorCounted();
   int i,k,limit;

   if(counted_bars<0) return(-1);
   if(counted_bars>0) counted_bars--;
         limit = MathMin(Bars-counted_bars,Bars-1);

   //
   //
   //
   //
   //
   
   double indlev1 ,indlev2 ,indlev3 ,indlev4 ,indlev5 ;
    switch(Period()) 
  { case 1:  case 5: case 15: case 30:  case 60: case 240: case 1440: case 10080: case 43200:
 
     indlev1 =   200 ;
     indlev2 =   100 ;
     indlev3 =     0 ;
     indlev4 =  -100 ;
     indlev5 =  -200 ;

     break;
  }   
      drawLine_ADPSN(indlev1,  "Ln1_ADPSN"  ,STYLE_DOT     ,  LevOutCol   , 0 );   
      drawLine_ADPSN(indlev2,  "Ln2_ADPSN"  ,STYLE_DOT     ,  LevMidCol   , 0 ); 
      drawLine_ADPSN(indlev3,  "Ln3_ADPSN"  ,STYLE_SOLID   ,  LevZeroCol  , 2 );   //+MagicNumber 
      drawLine_ADPSN(indlev4,  "Ln4_ADPSN"  ,STYLE_DOT     ,  LevMidCol   , 0 );  
      drawLine_ADPSN(indlev5,  "Ln5_ADPSN"  ,STYLE_DOT     ,  LevOutCol   , 0 );      
   for(i=limit; i>=0; i--)
   {
      prices[i]  = iMA(NULL,0,1,0,MODE_SMA,CciPrice,i);
      double avg = 0; for(k=0; k<CciPeriod; k++) avg +=         prices[i+k];      avg /= CciPeriod;
      double dev = 0; for(k=0; k<CciPeriod; k++) dev += MathAbs(prices[i+k]-avg); dev /= CciPeriod;
         if (dev!=0)
               cci[i] = (prices[i]-avg)/(0.015*dev);
         else  cci[i] = 0;
   }
   for(i=limit; i>=0; i--)
   {  
         ccima[i] = iT3(cci[i],T3Period,T3Hot,T3Original,i);
         trend[i] = trend[i+1];
         if (cci[i]>ccima[i]) trend[i] = 1;
         if (cci[i]<ccima[i]) trend[i] =-1;
         if (cci[i]>0)        cross[i] = 1;
         if (cci[i]<0)        cross[i] =-1;
         
         //
         //
         //
         //
         //
         
         if (verticalLinesVisible)
          {
             deleteLine(Time[i]);
             if (trend[i]!=trend[i+1])
             {
                if (trend[i] == 1) drawLine(i,verticalLinesUpColor);
                if (trend[i] ==-1) drawLine(i,verticalLinesDnColor);
             }
           }      
         manageArrow(i);       
   }
 
   if (alertsOn)
   {
      if (alertsOnCurrent)
           int whichBar = 0;
      else     whichBar = 1;

      if (trend[whichBar] != trend[whichBar+1])
      if (trend[whichBar] == 1)
            doAlert("crossing up");
      else  doAlert("crossing down");       
   }
return(0);
}
  
//------------------------------------------------------------------
//
//------------------------------------------------------------------
//
//
//
//
//

void doAlert(string doWhat)
{
   static string   previousAlert="nothing";
   static datetime previousTime;
   string message;
   
      if (previousAlert != doWhat || previousTime != Time[0]) {
          previousAlert  = doWhat;
          previousTime   = Time[0];

          //
          //
          //
          //
          //

          message =  StringConcatenate(Symbol()," at ",TimeToStr(TimeLocal(),TIME_SECONDS)," Cci T3Ma ",doWhat);
             if (alertsMessage) Alert(message);
             if (alertsEmail)   SendMail(StringConcatenate(Symbol()," Cci T3Ma Cross "),message);
             if (alertsNotify)  SendNotification(StringConcatenate(Symbol(), Period() ," Cci T3Ma " +" "+message));
             if (alertsSound)   PlaySound(soundfile);
      }
}

//+---------------------------------------------------------------------------------------------------------------+
//|                                                                                                               |
//+---------------------------------------------------------------------------------------------------------------+
//
//
//
//
//

void manageArrow(int i)
{
   if (ShowArrows)
   {
      deleteArrow(Time[i]);
      if (cross[i]!= cross[i+1])
      {
         if (cross[i] == 1) drawArrow(i,arrowsUpColor,arrowsUpCode,false);
         if (cross[i] ==-1) drawArrow(i,arrowsDnColor,arrowsDnCode, true);
      }
   }
}               

//
//
//
//
//

//
//
//
//
//

void drawArrow(int i,color theColor,int theCode,bool up)
{
   string name = arrowsIdentifier+":"+Time[i];
   double gap  = iATR(NULL,0,20,i);   
   
      //
      //
      //
      //
      //
      
      ObjectCreate(name,OBJ_ARROW,0,Time[i],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);
}

//
//
//
//
//

void deleteArrows()
{
   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);
   }
}

//
//
//
//
//

void deleteArrow(datetime time)
{
   string lookFor = arrowsIdentifier+":"+time; ObjectDelete(lookFor);
}

//------------------------------------------------------------------
//
//------------------------------------------------------------------
//
//
//
//
//

double workT3[][6];
double workT3Coeffs[][6];
#define _period 0
#define _c1     1
#define _c2     2
#define _c3     3
#define _c4     4
#define _alpha  5

//
//
//
//
//

double iT3(double price, double period, double hot, bool original, int i, int instanceNo=0)
{
   if (ArrayRange(workT3,0) != Bars)                ArrayResize(workT3,Bars);
   if (ArrayRange(workT3Coeffs,0) < (instanceNo+1)) ArrayResize(workT3Coeffs,instanceNo+1);

   if (workT3Coeffs[instanceNo][_period] != period)
   {
     workT3Coeffs[instanceNo][_period] = period;
        double a = hot;
            workT3Coeffs[instanceNo][_c1] = -a*a*a;
            workT3Coeffs[instanceNo][_c2] = 3*a*a+3*a*a*a;
            workT3Coeffs[instanceNo][_c3] = -6*a*a-3*a-3*a*a*a;
            workT3Coeffs[instanceNo][_c4] = 1+3*a+a*a*a+3*a*a;
            if (original)
                 workT3Coeffs[instanceNo][_alpha] = 2.0/(1.0 + period);
            else workT3Coeffs[instanceNo][_alpha] = 2.0/(2.0 + (period-1.0)/2.0);
   }
   
   //
   //
   //
   //
   //
   
   int buffer = instanceNo*6;
   int r = Bars-i-1;
   if (r == 0)
      {
         workT3[r][0+buffer] = price;
         workT3[r][1+buffer] = price;
         workT3[r][2+buffer] = price;
         workT3[r][3+buffer] = price;
         workT3[r][4+buffer] = price;
         workT3[r][5+buffer] = price;
      }
   else
      {
         workT3[r][0+buffer] = workT3[r-1][0+buffer]+workT3Coeffs[instanceNo][_alpha]*(price              -workT3[r-1][0+buffer]);
         workT3[r][1+buffer] = workT3[r-1][1+buffer]+workT3Coeffs[instanceNo][_alpha]*(workT3[r][0+buffer]-workT3[r-1][1+buffer]);
         workT3[r][2+buffer] = workT3[r-1][2+buffer]+workT3Coeffs[instanceNo][_alpha]*(workT3[r][1+buffer]-workT3[r-1][2+buffer]);
         workT3[r][3+buffer] = workT3[r-1][3+buffer]+workT3Coeffs[instanceNo][_alpha]*(workT3[r][2+buffer]-workT3[r-1][3+buffer]);
         workT3[r][4+buffer] = workT3[r-1][4+buffer]+workT3Coeffs[instanceNo][_alpha]*(workT3[r][3+buffer]-workT3[r-1][4+buffer]);
         workT3[r][5+buffer] = workT3[r-1][5+buffer]+workT3Coeffs[instanceNo][_alpha]*(workT3[r][4+buffer]-workT3[r-1][5+buffer]);
      }

   //
   //
   //
   //
   //
   
   return(workT3Coeffs[instanceNo][_c1]*workT3[r][5+buffer] + 
          workT3Coeffs[instanceNo][_c2]*workT3[r][4+buffer] + 
          workT3Coeffs[instanceNo][_c3]*workT3[r][3+buffer] + 
          workT3Coeffs[instanceNo][_c4]*workT3[r][2+buffer]);
}

//
//
//
//
//

void drawLine(int i,color theColor)
{
      string name = verticalLinesID+":"+Time[i];
   
      //
      //
      //
      //
      //
      
      ObjectCreate(name,OBJ_VLINE,0,Time[i],0);
         ObjectSet(name,OBJPROP_COLOR,theColor);
         ObjectSet(name,OBJPROP_STYLE,verticalLinesStyle);
         ObjectSet(name,OBJPROP_WIDTH,verticalLinesWidth);
         ObjectSet(name,OBJPROP_BACK,true);
}

//
//
//
//
//

void deleteLines()
{
   string lookFor       = verticalLinesID+":";
   int    lookForLength = StringLen(lookFor);
   for (int i=ObjectsTotal()-1; i>=0; i--)
   {
      string objectName = ObjectName(i);
         if (StringSubstr(objectName,0,lookForLength) == lookFor) ObjectDelete(objectName);
   }
}

//
//
//
//
//

void deleteLine(datetime time)
{
   string lookFor = verticalLinesID+":"+time; ObjectDelete(lookFor);
}

//================HORIZONTAL COLOR LINES==========================================================================
  void drawLine_ADPSN(double lvl_ADPSN,string name_ADPSN, double Style_ADPSN, color Col_ADPSN, double Size_ADPSN )
{ int indicatorWindow_ADPSN = WindowFind(shortName_ADPSN);
        if(indicatorWindow_ADPSN < 0)
        return;
       
   ObjectDelete(name_ADPSN);
   ObjectCreate(name_ADPSN,    OBJ_HLINE,indicatorWindow_ADPSN, Time[0], lvl_ADPSN,Time[0],lvl_ADPSN);
   ObjectSet   (name_ADPSN   , OBJPROP_STYLE, Style_ADPSN);
   ObjectSet   (name_ADPSN   , OBJPROP_COLOR, Col_ADPSN);       
   ObjectSet   (name_ADPSN   , OBJPROP_WIDTH, Size_ADPSN);
   ObjectSet   (name_ADPSN   , OBJPROP_BACK, true);
  }   
//=====================================================================================================+
