//+------------------------------------------------------------------+
//|                                                   ma crosses.mq4 |
//+------------------------------------------------------------------+

#property indicator_chart_window
#property indicator_buffers 2
#property indicator_color1 Aqua
#property indicator_color2 Magenta
#property indicator_width1 2
#property indicator_width2 2

       
extern string note1              = "MA cross settings";
extern int    MALength            = 100;          //fast MA Priod
extern int    MAShift        = 0;           //fast MA Shift
extern int    MAMethod       = MODE_LWMA;   //fast MA Mode
extern int    MAPrice        = PRICE_CLOSE; //fast MA Price
extern int    MAlookback     = 15;
extern int    Strong_Trend_Value = 30;
extern string note2              ="Arrow_Mode";
extern string note3              ="CCi: 0=CCi Value ,1=First weaker";
extern int    Arrow_Mode         = 0;
extern int    CCiPeriod          = 14;
extern int    plus_CCi_value_reach = 100;
extern int    minus_CCi_value_reach= -100;
extern int    plus_CCi_value_return  = 100;
extern int    minus_CCi_value_return = -100; 
/*extern string arrowsIdentifier = "ac Arrows1";
extern double arrowsUpperGap   = 1.0;
extern double arrowsLowerGap   = 1.0;
extern color  arrowsUpColor    = LimeGreen;
extern color  arrowsDnColor    = Red;
extern int    arrowsUpCode     = 233;
extern int    arrowsDnCode     = 234;*/

extern string note4            = "turn on Alert = true; turn off = false";
extern bool   alertsOn         = false;
extern bool   alertsOnCurrent  = true;
extern bool   alertsMessage    = true;
extern bool   alertsSound      = true;
extern bool   alertsEmail      = false;
extern string soundfile        = "alert2.wav";

//
//

double CrossUp[];
double CrossDn[];
double trend[];
double DnBuffer[];
double UpBuffer[];
double smadiff, smadiffpercent;
int cp;
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int init()
{  IndicatorBuffers(5); 
   SetIndexBuffer(0, CrossUp);  SetIndexStyle(0, DRAW_ARROW,0); SetIndexArrow(0, 233);
   SetIndexBuffer(1, CrossDn);  SetIndexStyle(1, DRAW_ARROW,0); SetIndexArrow(1, 234);
   SetIndexBuffer(2, trend);
   SetIndexBuffer(3, UpBuffer);
   SetIndexBuffer(4, DnBuffer);
   return(0);
}
int deinit() {

                  // deleteArrows();
  return(0); }

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
//
//
//
//

int start() {
    
   int counted_bars=IndicatorCounted();
   int i,limit;

   if(counted_bars<0) return(-1);
   if(counted_bars>0) counted_bars--;
         limit = MathMin(Bars-counted_bars,Bars-1);
         
         
  for(i=Bars-CCiPeriod-1; i>=0; i--)
   {
 

      double currsma = iMA(NULL,0,MALength,MAShift,MAMethod,MAPrice,i+1);
      double prevsma = iMA(NULL,0,MALength,MAShift,MAMethod,MAPrice,i+1+MAlookback);
    
        
         if(currsma >= prevsma) {smadiff = currsma - prevsma; cp =  1;}
         if(currsma <  prevsma) {smadiff = prevsma - currsma; cp = -1;}
 
           int pipMult = 1000000;
      if (StringFind(Symbol(),"JPY",0) != -1) pipMult = 10000;
      if (StringFind(Symbol(),"XAU",0) != -1) pipMult = 1000;
      if (StringFind(Symbol(),"Cash",0) != -1) pipMult = 10;
        
          smadiffpercent = pipMult * (smadiff/MAlookback);
 
      
      bool MAtrend;
      if (smadiffpercent>Strong_Trend_Value) MAtrend = true;

 

      double Pcci  =iCCI(NULL,0,CCiPeriod,PRICE_TYPICAL,i);
      double Pcci1 =iCCI(NULL,0,CCiPeriod,PRICE_TYPICAL,i+1);
      double Pcci2 =iCCI(NULL,0,CCiPeriod,PRICE_TYPICAL,i+2);
      double Pcci3 =iCCI(NULL,0,CCiPeriod,PRICE_TYPICAL,i+3);

      trend[i] = EMPTY_VALUE;
      trend[i+1] = trend[i];
      
      UpBuffer[i] = UpBuffer[i+1];
      DnBuffer[i] = DnBuffer[i+1];
      if (Pcci<minus_CCi_value_reach) UpBuffer[i] = 1;
      if (Pcci>plus_CCi_value_reach)  DnBuffer[i] = 1;

     if (Arrow_Mode==0){ 
         if (Pcci>minus_CCi_value_return){if (UpBuffer[i]==1 && MAtrend == true && cp ==  1) trend[i]=  1; UpBuffer[i] = 0;}
         if (Pcci<plus_CCi_value_return) {if (DnBuffer[i]==1 && MAtrend == true && cp == -1) trend[i]= -1; DnBuffer[i] = 0;}
         }
     if (Arrow_Mode==1){ 
         if (UpBuffer[i]==1 && MAtrend && cp ==  1 && Pcci2 < -100 && Pcci3 > Pcci2 && Pcci2 < Pcci1) trend[i] =  1;
         if (UpBuffer[i]==1 && MAtrend && cp == -1 && Pcci2 >  100 && Pcci3 < Pcci2 && Pcci2 > Pcci1) trend[i] = -1;
         }
         //
         //
         CrossUp[i] = EMPTY_VALUE;
         CrossDn[i] = EMPTY_VALUE;
        // deleteArrow(Time[i]);
         if (trend[i] !=trend[i+1])
         if (trend[i] == 1)
               CrossUp[i] = Low[i] - iATR(NULL,0,20,i)/2.0;
              // drawArrow(i,arrowsUpColor,arrowsUpCode,false);
         if (trend[i] == -1)
               CrossDn[i] = High[i]+ iATR(NULL,0,20,i)/2.0;
             //  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("uptrend");
         else  doAlert("downtrend");       
   }
   
   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];

          //
          //
          //
          //
          //

          if (trend[0] == 1) message =  StringConcatenate(Symbol()," at ",TimeToStr(TimeLocal(),TIME_SECONDS),"CCi up ",doWhat);
          else message =  StringConcatenate(Symbol()," at ",TimeToStr(TimeLocal(),TIME_SECONDS),"CCi down",doWhat);
             if (alertsMessage) Alert(message);
             if (alertsEmail)   SendMail(StringConcatenate(Symbol()," ma cross "),message);
             if (alertsSound)   PlaySound(soundfile);
      }
}

/*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);
         ObjectSet(name,OBJPROP_WIDTH,ArrowSize);
         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);
}*/