//+------------------------------------------------------------------+
//|                                                 CCI Ma cross.mq4 |
//+------------------------------------------------------------------+
#property copyright ""
#property link      ""

#property indicator_separate_window
#property indicator_buffers    2
#property indicator_color1     Black
#property indicator_color2     Crimson
#property indicator_width1     2
#property indicator_style2     STYLE_DOT
#property indicator_levelcolor MediumOrchid

//
//
//
//
//

extern string  iCCIPeriod_Desc    =  "Период CCI";
extern int     iCCIPeriod         =  14;
extern string  iCCIPrice_Desc     =  "Цена CCI: 0-Close, 1-Open, 2-High, 3-Low, 4-Median, 5-Typical, 6-Weighted";
extern int     iCCIPrice          =  0;
extern int    MaPeriod         = 27;
extern int    MaType           = MODE_SMA;
extern double OverSold         = -100;
extern double OverBought       = 100;

extern string note             = "turn on Alert = true; turn off = false";
extern bool   alertsOn         = true;
extern bool   alertsOnCurrent  = true;
extern bool   alertsMessage    = true;
extern bool   alertsSound      = true;
extern bool   alertsEmail      = false;
extern bool   alertsNotify     = false;
extern string soundfile        = "alert2.wav";

extern bool   ShowArrows       = true;
extern string arrowsIdentifier = "cci ma cross Arrows";
extern double arrowsUpperGap   = 1.0;
extern double arrowsLowerGap   = 1.0;
extern color  arrowsUpColor    = LimeGreen;
extern color  arrowsDnColor    = Red;
extern color  arrowsUpCode     = 233;
extern color  arrowsDnCode     = 234;


//
//
//
//
//

double cci[];
double ccima[];
double prices[];
double trend[];

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
//
//
//
//
//

int init()
{
   IndicatorBuffers(4);
      SetIndexBuffer(0,cci);
      SetIndexStyle (0, DRAW_HISTOGRAM, STYLE_SOLID);
      SetIndexBuffer(1,ccima); 
      SetIndexBuffer(2,prices);
      SetIndexBuffer(3,trend);
      SetLevelValue(0,OverBought);
      SetLevelValue(1,OverSold);
      
   IndicatorShortName("CCI Ma Cross ("+iCCIPeriod +","+MaPeriod+")");
return(0);
}

int deinit()
{
   deleteArrows();
   
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);

   //
   //
   //
   //
   //

   for(i=limit; i>=0; i--)
   {
      prices[i]  = iCCI(NULL, 0, iCCIPeriod, iCCIPrice, i);//iRSI можно поменять на все значения iCCI будет RSI
      double avg = 0; for(k=0; k<iCCIPeriod ; k++) avg +=         prices[i+k];      avg /= iCCIPeriod ;
      double dev = 0; for(k=0; k<iCCIPeriod ; k++) dev += MathAbs(prices[i+k]-avg); dev /= iCCIPeriod ;
         if (dev!=0)
               cci[i] = (prices[i]-avg)/(0.015*dev);
         else  cci[i] = 0;          
   }
   
   for (i=limit; i>=0; i--)
   {
      ccima[i] = iMAOnArray(cci,0,MaPeriod,0,MaType,i);
      trend[i] = trend[i+1];
         if (cci[i]> ccima[i]&& OverSold) trend[i] = 1;  
         if (cci[i]< ccima[i]&& OverBought) trend[i] =-1;
         manageArrow(i); 
   }
   
   //
   //
   //
   //
   //
      
   if (alertsOn)
   {
      if (alertsOnCurrent)
           int whichBar = 0;
      else     whichBar = 1;

      if (trend[whichBar] != trend[whichBar+1])
      if (trend[whichBar] == 1)
            doAlert("Buy");
      else  doAlert("Sell");       
   }
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 Ma Cross ",doWhat);
             if (alertsMessage) Alert(message);
             if (alertsEmail)   SendMail(StringConcatenate(Symbol()," CCI Ma Cross "),message);
             if (alertsNotify)  SendNotification(StringConcatenate(Symbol(), Period() ," CCI Ma Cross " +" "+message));
             if (alertsSound)   PlaySound(soundfile);
      }
}

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
//
//
//
//
//

void manageArrow(int i)
{
   if (ShowArrows)
   {
      deleteArrow(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);
      }
   }
}               

//
//
//
//
//

//
//
//
//
//

void drawArrow(int i,color theColor,int theCode,bool up)
{
   string name = arrowsIdentifier+":"+Time[i];
   double gap  = iATR(NULL,0,17,i);   // было 20
   
      //
      //
      //
      //
      //
      
      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);
}

