//+------------------------------------------------------------------+
//|                                                   GG-RSI-CCI.mq4 |
//|                                         Copyright © 2009, GGekko |
//|                                         http://www.fx-ggekko.com |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2009, GGekko"
#property link      "http://www.fx-ggekko.com"

//---- indicator settings
#property  indicator_separate_window
#property  indicator_buffers 3
#property  indicator_color1  Lime
#property  indicator_color2  Red
#property  indicator_color3  OrangeRed
#property  indicator_width1  2
#property  indicator_width2  2
#property  indicator_width3  2
#property indicator_minimum  0
#property indicator_maximum  1.5 

//---- indicator parameters
extern string   __Copyright__          = "www.fx-ggekko.com";
extern int      Avg_Period1            = 8;
extern int      Avg_Period2            = 14;
extern int      Ind_Period             = 20;
extern bool            alertsOn        = true;
extern bool            alertsOnCurrent = false;
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 = "rsicci Arrows";
extern double arrowsUpperGap   = 1.0;
extern double arrowsLowerGap   = 1.0;

extern color  arrowsUpColor    = LimeGreen;
extern color  arrowsDnColor    = Red;
extern color  arrowsNuColor    = Gray;
extern int    arrowsUpCode     = 241;
extern int    arrowsDnCode     = 242;
extern int    arrowsNuCode     = 108;

//---- indicator buffers
double     BufferUp[];
double     BufferFlat[];
double     BufferDown[];

double     ind1[];
double     ind2[];

double     ind3[];
double     ind4[];
double     ind5[];
double     ind6[];
double     slope[];


//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- drawing settings
   IndicatorBuffers(10);
   SetIndexStyle(0,DRAW_HISTOGRAM);
   SetIndexStyle(1,DRAW_HISTOGRAM);
   SetIndexStyle(2,DRAW_HISTOGRAM);
   
   
//---- indicator buffers mapping
   SetIndexBuffer(0,BufferUp);
   SetIndexBuffer(1,BufferFlat);
   SetIndexBuffer(2,BufferDown);
   SetIndexBuffer(3,ind1);
   SetIndexBuffer(4,ind2);
   SetIndexBuffer(5,ind3);
   SetIndexBuffer(6,ind4);
   SetIndexBuffer(7,ind5);
   SetIndexBuffer(8,ind6);
   SetIndexBuffer(9,slope);
//---- name for DataWindow and indicator subwindow label
   IndicatorShortName("GG-RSI-CCI ("+Avg_Period1+","+Avg_Period2+","+Ind_Period+") * www.fx-ggekko.com * ");
   
   
//---- initialization done
   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=limit; i >= 0; i--) ind1[i]=iRSI(NULL,0,Ind_Period,PRICE_CLOSE,i);
   for (i=limit; i >= 0; i--) ind2[i]=iCCI(NULL,0,Ind_Period,PRICE_CLOSE,i);
   for (i=limit; i >= 0; i--) ind3[i]=iMAOnArray(ind1,0,Avg_Period1,0,MODE_SMMA,i);
   for (i=limit; i >= 0; i--) ind4[i]=iMAOnArray(ind1,0,Avg_Period2,0,MODE_SMMA,i);
   for (i=limit; i >= 0; i--) ind5[i]=iMAOnArray(ind2,0,Avg_Period1,0,MODE_SMMA,i);
   for (i=limit; i >= 0; i--) ind6[i]=iMAOnArray(ind2,0,Avg_Period2,0,MODE_SMMA,i);
   for (i=limit; i >= 0; i--)
   {
      BufferFlat[i] = EMPTY_VALUE;
      BufferDown[i] = EMPTY_VALUE;  
      BufferUp[i]   = EMPTY_VALUE;
      slope[i]      = 0;
      
      if(ind3[i]>ind4[i] && ind5[i]>ind6[i])                                          slope[i] = 1;
      if(ind3[i]<ind4[i] && ind5[i]<ind6[i])                                          slope[i] =-1;
      if((ind3[i]<ind4[i] && ind5[i]>ind6[i])|| (ind3[i]>ind4[i] && ind5[i]<ind6[i])) slope[i] = 0;
      if (slope[i] == 1) BufferUp[i]  = 1; 
      if (slope[i] ==-1) BufferDown[i]= 1;
      if (slope[i] == 0) BufferFlat[i]= 1;
      
      if (ShowArrows)
      {
        deleteArrow(Time[i]);
        if (slope[i]!= slope[i+1])
        {
          if (slope[i] == 1)                   drawArrow(i,arrowsUpColor,arrowsUpCode,false);
          if (slope[i] ==-1)                   drawArrow(i,arrowsDnColor,arrowsDnCode, true);
          if (slope[i] == 0 && slope[i+1]== 1) drawArrow(i,arrowsNuColor,arrowsNuCode,false);
          if (slope[i] == 0 && slope[i+1]==-1) drawArrow(i,arrowsNuColor,arrowsNuCode, true);
        }
      }
    }
      
    //
    //
    //
    //
    //
    
    if (alertsOn)
    {
      if (alertsOnCurrent)
           int whichBar = 0;
      else     whichBar = 1; 
      
      //
      //
      //
      //
      //
      
      if (BufferUp[whichBar+1]   == EMPTY_VALUE && BufferUp[whichBar]   != EMPTY_VALUE) doAlert(whichBar,"buy");
      if (BufferDown[whichBar+1] == EMPTY_VALUE && BufferDown[whichBar] != EMPTY_VALUE) doAlert(whichBar,"sell");
      if (BufferFlat[whichBar+1] == EMPTY_VALUE && BufferFlat[whichBar] != EMPTY_VALUE) doAlert(whichBar,"flat");
    }         
   return(0);
  }

//
//
//
//
//

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 =  Symbol()+" at "+TimeToStr(TimeLocal(),TIME_SECONDS)+" GG-RSI-CCI changed trend to "+doWhat;
             if (alertsMessage) Alert(message);
             if (alertsNotify)  SendNotification(message);
             if (alertsEmail)   SendMail(StringConcatenate(Symbol(), Period(), " GG-RSI-CCI "),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);
         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);
}