//------------------------------------------------------------------
#property copyright "www.forex-tsd.com"
#property link      "www.forex-tsd.com"
//------------------------------------------------------------------
#property indicator_separate_window
#property indicator_buffers 4
#property indicator_color1  clrDeepSkyBlue
#property indicator_color2  clrSandyBrown
#property indicator_color3  clrSandyBrown
#property indicator_color4  clrSilver
#property indicator_width1  2
#property indicator_width2  2
#property indicator_width3  2

//
//
//
//
//

enum enPrices
{
   pr_close,      // Close
   pr_open,       // Open
   pr_high,       // High
   pr_low,        // Low
   pr_median,     // Median
   pr_typical,    // Typical
   pr_weighted,   // Weighted
   pr_average,    // Average (high+low+open+close)/4
   pr_medianb,    // Average median body (open+close)/2
   pr_tbiased,    // Trend biased price
   pr_haclose,    // Heiken ashi close
   pr_haopen ,    // Heiken ashi open
   pr_hahigh,     // Heiken ashi high
   pr_halow,      // Heiken ashi low
   pr_hamedian,   // Heiken ashi median
   pr_hatypical,  // Heiken ashi typical
   pr_haweighted, // Heiken ashi weighted
   pr_haaverage,  // Heiken ashi average
   pr_hamedianb,  // Heiken ashi median body
   pr_hatbiased   // Heiken ashi trend biased price
};

extern ENUM_TIMEFRAMES TimeFrame        = PERIOD_CURRENT;
extern int             Length           = 32;
extern enPrices        Price            = pr_close; // Price to use 
extern bool            AlertsOn         = false;
extern bool            AlertsOnCurrent  = true;
extern bool            AlertsMessage    = true;
extern bool            AlertsSound      = false;
extern bool            AlertsEmail      = false;
extern bool            AlertsNotify     = false;
extern bool            ShowArrows       = false;
extern string          arrowsIdentifier = "StochCG Arrows1";
extern double          arrowsUpperGap   = 0.5;
extern double          arrowsLowerGap   = 0.5;
extern color           arrowsUpColor    = clrLimeGreen;
extern color           arrowsDnColor    = clrRed;
extern int             arrowsUpCode     = 241;
extern int             arrowsDnCode     = 242;
extern bool            Interpolate      = true;

double cg[];
double storaw[];
double stocg[];
double stocgda[];
double stocgdb[];
double stosig[];
double trend[];
string indicatorFileName;
bool   returnBars;


//------------------------------------------------------------------
//
//------------------------------------------------------------------
//
//
//
//
//

int init()
{
   IndicatorBuffers(7);
      SetIndexBuffer(0,stocg);
      SetIndexBuffer(1,stocgda);
      SetIndexBuffer(2,stocgdb);
      SetIndexBuffer(3,stosig);
      SetIndexBuffer(4,cg);
      SetIndexBuffer(5,storaw);
      SetIndexBuffer(6,trend);

      //
      //
      //
      //
      //
      
         indicatorFileName = WindowExpertName();
         returnBars        = TimeFrame == -99;
         TimeFrame         = MathMax(TimeFrame,_Period);
         
      //
      //
      //
      //
      //
               
   IndicatorShortName(timeFrameToString(TimeFrame)+" Stochastic CG ("+Length+")");
   return(0); 
}
int deinit() 
{  
   deleteArrows(); 
return(0); 
}

//------------------------------------------------------------------
//
//------------------------------------------------------------------
//
//
//
//
//

int start()
{
   int n,k,counted_bars=IndicatorCounted();
      if(counted_bars<0) return(-1);
      if(counted_bars>0) counted_bars--;
         int limit = MathMin(Bars-counted_bars,Bars-1);
         if (returnBars) { stocg[0] = MathMin(limit+1,Bars-1); return(0); }

   //
   //
   //
   //
   //

   if (TimeFrame == Period())
   {
      if (trend[limit]==-1) CleanPoint(limit,stocgda,stocgdb);
      for(int i=limit; i>=0; i--)
      {
         double num = 0;
         double den = 0;
            for (k=0; k<Length; k++)
            {
               double price = getPrice(Price,Open,Close,High,Low,i+k);
                      num += price*(Length-k);
                      den += k-Length;
            }
            if (den!=0)
                  cg[i] = -num/den;
            else  cg[i] = 0;
         
            //
            //
            //
            //
            //
         
            double hh = cg[ArrayMaximum(cg,Length,i)];          
            double ll = cg[ArrayMinimum(cg,Length,i)];
            if (hh!=ll)
                  storaw[i] = (cg[i]-ll)/(hh-ll);
            else  storaw[i] = 0;
            double smtcg = (4.0*storaw[i]+3.0*storaw[i+1]+2.0*storaw[i+2]+storaw[i+3])/10.0;
         
            //
            //
            //
            //
            //
            
            stocg[i]   = 2.0*(smtcg-0.5);
            stosig[i]  = 0.96*(stocg[i+1] + 0.02);
            stocgda[i] = EMPTY_VALUE;
            stocgdb[i] = EMPTY_VALUE;
            trend[i]   = trend[i+1];
               if (stocg[i]>stosig[i]) trend[i] =  1;
               if (stocg[i]<stosig[i]) trend[i] = -1;
               if (trend[i]==-1) PlotPoint(i,stocgda,stocgdb,stocg);
               
               //
               //
               //
               //
               //
     
               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);
                   }
                }
      }
      manageAlerts();
      return(0);
   }
   
   //
   //
   //
   //
   //
   
   limit = MathMax(limit,MathMin(Bars-1,iCustom(NULL,TimeFrame,indicatorFileName,-99,0,0)*TimeFrame/Period()));
   if (trend[limit]==-1) CleanPoint(limit,stocgda,stocgdb);
   for (i=limit; i>=0; i--)
   {
      int y = iBarShift(NULL,TimeFrame,Time[i]);
         stocg[i]   = iCustom(NULL,TimeFrame,indicatorFileName,PERIOD_CURRENT,Length,Price,AlertsOn,AlertsOnCurrent,AlertsMessage,AlertsSound,AlertsEmail,AlertsNotify,ShowArrows,arrowsIdentifier,arrowsUpperGap,arrowsLowerGap,arrowsUpColor,arrowsDnColor,arrowsUpCode,arrowsDnCode,0,y);
         stosig[i]  = iCustom(NULL,TimeFrame,indicatorFileName,PERIOD_CURRENT,Length,Price,AlertsOn,AlertsOnCurrent,AlertsMessage,AlertsSound,AlertsEmail,AlertsNotify,ShowArrows,arrowsIdentifier,arrowsUpperGap,arrowsLowerGap,arrowsUpColor,arrowsDnColor,arrowsUpCode,arrowsDnCode,3,y);
         trend[i]   = iCustom(NULL,TimeFrame,indicatorFileName,PERIOD_CURRENT,Length,Price,AlertsOn,AlertsOnCurrent,AlertsMessage,AlertsSound,AlertsEmail,AlertsNotify,ShowArrows,arrowsIdentifier,arrowsUpperGap,arrowsLowerGap,arrowsUpColor,arrowsDnColor,arrowsUpCode,arrowsDnCode,6,y);
         stocgda[i] = EMPTY_VALUE;
         stocgdb[i] = EMPTY_VALUE;
         
         //
         //
         //
         //
         //
         
         if (!Interpolate || y==iBarShift(NULL,TimeFrame,Time[i-1])) continue;

         //
         //
         //
         //
         //

            datetime time = iTime(NULL,TimeFrame,y);
               for(n = 1; i+n < Bars && Time[i+n] >= time; n++) continue;	
               for(k = 1; k < n; k++)
               {
                  stocg[i+k]  = stocg[i]  + (stocg[i+n]  - stocg[i] )*k/n;
                  stosig[i+k] = stosig[i] + (stosig[i+n] - stosig[i])*k/n;
               }                  
   }
   for (i=limit;i>=0;i--) if (trend[i]==-1) PlotPoint(i,stocgda,stocgdb,stocg);
return(0);       
}

//------------------------------------------------------------------
//
//------------------------------------------------------------------
//
//
//
//
//
//

double workHa[][4];
double getPrice(int price, const double& open[], const double& close[], const double& high[], const double& low[], int i, int instanceNo=0)
{
  if (price>=pr_haclose && price<=pr_hatbiased)
   {
      if (ArrayRange(workHa,0)!= Bars) ArrayResize(workHa,Bars);
         int r = Bars-i-1;
         
         //
         //
         //
         //
         //
         
         double haOpen;
         if (r>0)
                haOpen  = (workHa[r-1][instanceNo+2] + workHa[r-1][instanceNo+3])/2.0;
         else   haOpen  = (open[i]+close[i])/2;
         double haClose = (open[i] + high[i] + low[i] + close[i]) / 4.0;
         double haHigh  = MathMax(high[i], MathMax(haOpen,haClose));
         double haLow   = MathMin(low[i] , MathMin(haOpen,haClose));

         if(haOpen  <haClose) { workHa[r][instanceNo+0] = haLow;  workHa[r][instanceNo+1] = haHigh; } 
         else                 { workHa[r][instanceNo+0] = haHigh; workHa[r][instanceNo+1] = haLow;  } 
                                workHa[r][instanceNo+2] = haOpen;
                                workHa[r][instanceNo+3] = haClose;
         //
         //
         //
         //
         //
         
         switch (price)
         {
            case pr_haclose:     return(haClose);
            case pr_haopen:      return(haOpen);
            case pr_hahigh:      return(haHigh);
            case pr_halow:       return(haLow);
            case pr_hamedian:    return((haHigh+haLow)/2.0);
            case pr_hamedianb:   return((haOpen+haClose)/2.0);
            case pr_hatypical:   return((haHigh+haLow+haClose)/3.0);
            case pr_haweighted:  return((haHigh+haLow+haClose+haClose)/4.0);
            case pr_haaverage:   return((haHigh+haLow+haClose+haOpen)/4.0);
            case pr_hatbiased:
               if (haClose>haOpen)
                     return((haHigh+haClose)/2.0);
               else  return((haLow+haClose)/2.0);        
         }
   }
   
   //
   //
   //
   //
   //
   
   switch (price)
   {
      case pr_close:     return(close[i]);
      case pr_open:      return(open[i]);
      case pr_high:      return(high[i]);
      case pr_low:       return(low[i]);
      case pr_median:    return((high[i]+low[i])/2.0);
      case pr_medianb:   return((open[i]+close[i])/2.0);
      case pr_typical:   return((high[i]+low[i]+close[i])/3.0);
      case pr_weighted:  return((high[i]+low[i]+close[i]+close[i])/4.0);
      case pr_average:   return((high[i]+low[i]+close[i]+open[i])/4.0);
      case pr_tbiased:   
               if (close[i]>open[i])
                     return((high[i]+close[i])/2.0);
               else  return((low[i]+close[i])/2.0);        
   }
   return(0);
}

//-------------------------------------------------------------------
//
//-------------------------------------------------------------------
//
//
//
//
//

void manageAlerts()
{
   if (AlertsOn)
   {
      if (AlertsOnCurrent)
           int whichBar = 0;
      else     whichBar = 1;

      if (trend[whichBar]!= trend[whichBar+1])
      {
         static datetime time1 = 0;
         static string   mess1 = "";
            if (trend[whichBar] ==  1) doAlert(time1,mess1," stochastic CG trend changed to up");
            if (trend[whichBar] == -1) doAlert(time1,mess1," stochastic CG trend changed to down");
      }
   }
}

//
//
//
//
//

void doAlert(datetime& previousTime, string& previousAlert, string doWhat)
{
   string message;
   
   if (previousAlert != doWhat || previousTime != Time[0]) {
       previousAlert  = doWhat;
       previousTime   = Time[0];

       //
       //
       //
       //
       //

       message =  StringConcatenate(Symbol()," ",timeFrameToString(_Period)," at ",TimeToStr(TimeLocal(),TIME_SECONDS)+doWhat);
          if (AlertsMessage) Alert(message);
          if (AlertsEmail)   SendMail(Symbol()+" stochastic CG",message);
          if (AlertsSound)   PlaySound("alert2.wav");
          if (AlertsNotify)  SendNotification(message);
   }
}

//-------------------------------------------------------------------
//                                                                  
//-------------------------------------------------------------------
//
//
//
//
//

void CleanPoint(int i,double& first[],double& second[])
{
   if (i>=Bars-3) return;
   if ((second[i]  != EMPTY_VALUE) && (second[i+1] != EMPTY_VALUE))
        second[i+1] = EMPTY_VALUE;
   else
      if ((first[i] != EMPTY_VALUE) && (first[i+1] != EMPTY_VALUE) && (first[i+2] == EMPTY_VALUE))
          first[i+1] = EMPTY_VALUE;
}

void PlotPoint(int i,double& first[],double& second[],double& from[])
{
   if (i>=Bars-2) return;
   if (first[i+1] == EMPTY_VALUE)
      if (first[i+2] == EMPTY_VALUE) 
            { first[i]  = from[i];  first[i+1]  = from[i+1]; second[i] = EMPTY_VALUE; }
      else  { second[i] =  from[i]; second[i+1] = from[i+1]; first[i]  = EMPTY_VALUE; }
   else     { first[i]  = from[i];                           second[i] = EMPTY_VALUE; }
}

//-------------------------------------------------------------------
//
//-------------------------------------------------------------------
//
//
//
//
//

string sTfTable[] = {"M1","M5","M15","M30","H1","H4","D1","W1","MN"};
int    iTfTable[] = {1,5,15,30,60,240,1440,10080,43200};

string timeFrameToString(int tf)
{
   for (int i=ArraySize(iTfTable)-1; i>=0; i--) 
         if (tf==iTfTable[i]) return(sTfTable[i]);
                              return("");
}


//
//
//
//
//

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);
}



