CCI_ColorCCI_Sig_Alert





//+------------------------------------------------------------------+
//|                                                    Color RSI.mq4 |
//|                                                           mladen |
//| CCI_Color_CCI_with Alerts.mq4                                    |
//+------------------------------------------------------------------+
//mod2008fxtsd   ki
#property copyright "mladen"
#property link      ""

#property indicator_separate_window
#property indicator_buffers   3

#property indicator_color1 Gold
#property indicator_color2 Lime
#property indicator_color3 Red
#property indicator_width1 2
#property indicator_width2 2
#property indicator_width3 2

#property indicator_level1  100
#property indicator_level2  -100
#property indicator_levelcolor DarkSlateGray


//---- input parameters
//
//
//
//

extern int       CCIPeriod     =   14;
extern int       PriceType     =   0;
extern string    timeFrame     =  "Current time frame";
extern int       overBoughtLevel    =   100;
extern int       overSoldLevel      =  -100;


extern bool      alertsOn      = true;
extern bool      alertsMessage = true;
extern bool      alertsSound   = false;
extern bool      alertsEmail   = false;
extern string    alertsSoundfile ="alert2.wav";

extern bool      showArrows       = false;
extern color       ArrowUpColor   = Lime;
extern color       ArrowDnColor   = Red;
extern int       ArrowDistance  = 7;
extern int       ArrowSize      = 1;

extern string     note_Price = "0C 1O 2H 3L 4Md 5Tp 6WghC: Md(HL/2)4,Tp(HLC/3)5,Wgh(HLCC/4)6";
extern string     TimeFrames = "M1;5,15,30,60H1;240H4;1440D1;10080W1;43200MN|0-CurrentTF";



//---- buffers
//
//
//
//
//

double   CCIBuffer[];
double   Upper[];
double   Lower[];

//
//
//
//
//

int      TimeFrame;
datetime TimeArray[];
int      maxArrows;


//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+

int init()
{
      SetIndexBuffer(0,CCIBuffer);
      SetIndexBuffer(1,Upper);
      SetIndexBuffer(2,Lower);
      SetIndexLabel(0,"CCI");
      SetIndexLabel(1,NULL);
      SetIndexLabel(2,NULL);
         
         //
         //
         //
         //
         //
         
         
      TimeFrame         = stringToTimeFrame(timeFrame);
      string shortName  = "CCI  ["+TimeFrameToString(TimeFrame)+"] "+CCIPeriod+" , Price "+PriceTypeToString(PriceType);
            if (overBoughtLevel < overSoldLevel) overBoughtLevel = overSoldLevel;
             shortName  = shortName+" ("+overBoughtLevel+","+overSoldLevel+")";
            IndicatorShortName(shortName);
   return(0);
}

//
//
// 
//

int deinit()
{
   DeleteArrows();
   return(0);
}

//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+

int start()
{
   int    counted_bars=IndicatorCounted();
   int    limit;
   int    i,y;
   
   
   
   
   if(counted_bars<0) return(-1);
   limit=Bars-counted_bars;
         ArrayCopySeries(TimeArray ,MODE_TIME ,NULL,TimeFrame);
            
      
   //
   //
   //
   //
   //
   
   for(i=0,y=0; i<limit; i++)
      {
            if(Time[i]<TimeArray[y]) y++;
               CCIBuffer[i] = iCCI(NULL,TimeFrame,CCIPeriod,PriceType,y);
      }

   //
   //
   //
   //
   //
   
   for(i=limit; i>=0; i--)
      {
            if (CCIBuffer[i] > overBoughtLevel) { Upper[i] = CCIBuffer[i]; Upper[i+1] = CCIBuffer[i+1]; }
            else                                { Upper[i] = EMPTY_VALUE;
                                              if (Upper[i+2] == EMPTY_VALUE)
                                                  Upper[i+1]  = EMPTY_VALUE; }
            if (CCIBuffer[i] < overSoldLevel)   { Lower[i] = CCIBuffer[i]; Lower[i+1] = CCIBuffer[i+1]; }
            else                                { Lower[i] = EMPTY_VALUE;
                                              if (Lower[i+2] == EMPTY_VALUE)
                                                  Lower[i+1]  = EMPTY_VALUE; }
      }
   
   //
   //
   //
   //
   //
   
   if (showArrows)
      for (i=0; i<WindowBarsPerChart() ;i++)
         {
            if (CCIBuffer[i]>overBoughtLevel && CCIBuffer[i+1]<overBoughtLevel) DrawArrow(i,"up");
            if (CCIBuffer[i]<overSoldLevel   && CCIBuffer[i+1]>overSoldLevel)   DrawArrow(i,"down");
         }
   if (alertsOn)
      {
         if (CCIBuffer[0]>overBoughtLevel && CCIBuffer[1]<overBoughtLevel) doAlert(overBoughtLevel+" level crossed up");
         if (CCIBuffer[0]<overSoldLevel   && CCIBuffer[1]>overSoldLevel)   doAlert(overSoldLevel  +" level crossed down");
      }


   //
   //
   //
   //
   //
 
   return(0);
}

//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
//
//
//
//
//

void DrawArrow(int i,string type)
{
   maxArrows++;
      ObjectCreate("CCISignal"+maxArrows,OBJ_ARROW,0,Time[i],0);
      if (type=="up")
         {
            ObjectSet("CCISignal"+maxArrows,OBJPROP_PRICE1,High[i]+((3+ArrowDistance)*Point));
            ObjectSet("CCISignal"+maxArrows,OBJPROP_ARROWCODE,241);
            ObjectSet("CCISignal"+maxArrows,OBJPROP_COLOR,ArrowUpColor);
            ObjectSet("CCISignal"+maxArrows,OBJPROP_WIDTH,ArrowSize);

         }
      else
         {
            ObjectSet("CCISignal"+maxArrows,OBJPROP_PRICE1,Low[i]-(ArrowDistance*Point));
            ObjectSet("CCISignal"+maxArrows,OBJPROP_ARROWCODE,242);
            ObjectSet("CCISignal"+maxArrows,OBJPROP_COLOR,ArrowDnColor);
            ObjectSet("CCISignal"+maxArrows,OBJPROP_WIDTH,ArrowSize);

         }
}
void DeleteArrows()
{
   while(maxArrows>0) { ObjectDelete("CCISignal"+maxArrows); maxArrows--; }
}


//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+

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 ",doWhat);
             if (alertsMessage) Alert(message);
             if (alertsSound)   PlaySound(alertsSoundfile);
             if (alertsEmail)   SendMail(StringConcatenate(Symbol()," CCI crossing"),message);
      }        
}

//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
//
//
//
//
//

string PriceTypeToString(int pt)
{
   string answer;
   switch(pt)
   {
      case 0:  answer = "Close"    ; break; 
      case 1:  answer = "Open"     ; break;
      case 2:  answer = "High"     ; break;
      case 3:  answer = "Low"      ; break;
      case 4:  answer = "Median"   ; break;
      case 5:  answer = "Typical"  ; break;
      case 6:  answer = "Wighted"  ; break;
      default: answer = "Invalid price field requested";
                                    Alert(answer);
   }
   return(answer);
}
int stringToTimeFrame(string tfs)
{
   int tf=0;
       tfs = StringUpperCase(tfs);
         if (tfs=="M1" || tfs=="1")     tf=PERIOD_M1;
         if (tfs=="M5" || tfs=="5")     tf=PERIOD_M5;
         if (tfs=="M15"|| tfs=="15")    tf=PERIOD_M15;
         if (tfs=="M30"|| tfs=="30")    tf=PERIOD_M30;
         if (tfs=="H1" || tfs=="60")    tf=PERIOD_H1;
         if (tfs=="H4" || tfs=="240")   tf=PERIOD_H4;
         if (tfs=="D1" || tfs=="1440")  tf=PERIOD_D1;
         if (tfs=="W1" || tfs=="10080") tf=PERIOD_W1;
         if (tfs=="MN" || tfs=="43200") tf=PERIOD_MN1;
         if (tf<Period()) tf=Period();
  return(tf);
}
string TimeFrameToString(int tf)
{
   string tfs="Current time frame";
   switch(tf) {
      case PERIOD_M1:  tfs="M1"  ; break;
      case PERIOD_M5:  tfs="M5"  ; break;
      case PERIOD_M15: tfs="M15" ; break;
      case PERIOD_M30: tfs="M30" ; break;
      case PERIOD_H1:  tfs="H1"  ; break;
      case PERIOD_H4:  tfs="H4"  ; break;
      case PERIOD_D1:  tfs="D1"  ; break;
      case PERIOD_W1:  tfs="W1"  ; break;
      case PERIOD_MN1: tfs="MN1";
   }
   return(tfs);
}

//
//
//
//
//

string StringUpperCase(string str)
{
   string   s = str;
   int      lenght = StringLen(str) - 1;
   int      char;
   
   while(lenght >= 0)
      {
         char = StringGetChar(s, lenght);
         
         //
         //
         //
         //
         //
         
         if((char > 96 && char < 123) || (char > 223 && char < 256))
                  s = StringSetChar(s, lenght, char - 32);
          else 
              if(char > -33 && char < 0)
                  s = StringSetChar(s, lenght, char + 224);
                  
         //
         //
         //
         //
         //
                                 
         lenght--;
   }
   
   //
   //
   //
   //
   //
   
   return(s);
}



Sample





Analysis



Market Information Used:

Series array that contains open time of each bar
Series array that contains the highest prices of each bar
Series array that contains the lowest prices of each bar


Indicator Curves created:



Indicators Used:

Commodity channel index


Custom Indicators Used:

Order Management characteristics:

Other Features:

It issuies visual alerts to the screen
It plays sound alerts
It sends emails