//+------------------------------------------------------------------+
//|                                         Schaff Trend Cycle 1.mq4 |
//|                                                           mladen |
//+------------------------------------------------------------------+
#property copyright "mladen"
#property link      "mladenfx@gmail.com"

#property indicator_separate_window
#property indicator_buffers 1
#property indicator_color1  Orange
#property indicator_width1  2

//
//
//
//
//

extern int   STCPeriod          = 10;
extern int   CDPeriod           = 50;
extern int   FastMAPeriod       = 46;
extern int   SlowMAPeriod       = 100;
extern bool  alertsOn           = False;
extern bool  alertsOnCurrentBar = true;
extern bool  alertsMessage      = true;
extern bool  alertsSound        = false;
extern bool  alertsEmail        = false;

//
//
//
//
//

double stcBuffer[];
double macdBuffer[];
double cdBuffer[];
double fastKBuffer[];
double fastDBuffer[];
double fastKKBuffer[];
double slope[];


//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
//
//
//
//
//

int init()
{
   IndicatorBuffers(7);
      SetIndexBuffer(0,stcBuffer);
      SetIndexBuffer(1,macdBuffer);
      SetIndexBuffer(2,cdBuffer);
      SetIndexBuffer(3,fastKBuffer);
      SetIndexBuffer(4,fastDBuffer);
      SetIndexBuffer(5,fastKKBuffer);
      SetIndexBuffer(6,slope);
   IndicatorShortName("Schaff TC1 ("+STCPeriod+","+FastMAPeriod+","+SlowMAPeriod+","+CDPeriod+")");
   return(0);
}

int deinit()
{
   return(0);
}

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
//
//
//
//
//

int start()
{
   int counted_bars = IndicatorCounted();
      if(counted_bars < 0) return(-1);
      if(counted_bars>0) counted_bars--;
         int limit = MathMax(Bars-counted_bars,Bars-1);

   //
   //
   //
   //
   //
   
   for(int i = limit; i >= 0; i--)
   {
      macdBuffer[i] = iSsm(iMA(NULL,0,1,0,MODE_SMA,PRICE_CLOSE,i),FastMAPeriod,i,0)-
                      iSsm(iMA(NULL,0,1,0,MODE_SMA,PRICE_CLOSE,i),SlowMAPeriod,i,1);  
      cdBuffer[i]   = iSsm(macdBuffer[i],CDPeriod,i,2);

      //
      //
      //
      //
      //
      
      double lowCd  = minValue(cdBuffer,i);
      double highCd = maxValue(cdBuffer,i)-lowCd;
         if (highCd > 0)
               fastKBuffer[i] = 100*((cdBuffer[i]-lowCd)/highCd);
         else  fastKBuffer[i] = fastKBuffer[i+1];
               fastDBuffer[i] = fastDBuffer[i+1]+0.5*(fastKBuffer[i]-fastDBuffer[i+1]);
               
      //
      //
      //
      //
      //
                     
      double lowStoch  = minValue(fastDBuffer,i);
      double highStoch = maxValue(fastDBuffer,i)-lowStoch;
         if (highStoch > 0)
               fastKKBuffer[i] = 100*((fastDBuffer[i]-lowStoch)/highStoch);
         else  fastKKBuffer[i] = fastKKBuffer[i+1];
               stcBuffer[i]    = stcBuffer[i+1]+0.5*(fastKKBuffer[i]-stcBuffer[i+1]);
               slope[i]        = slope[i+1];
                  if (stcBuffer[i]>stcBuffer[i+1]) slope[i] =  1;
                  if (stcBuffer[i]<stcBuffer[i+1]) slope[i] = -1;
   }   
   manageAlerts();
   return(0);
}


//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
//
//
//
//
//

double minValue(double& array[],int shift)
{
   double minValue = array[shift];
            for (int i=1; i<STCPeriod; i++) minValue = MathMin(minValue,array[shift+i]);
   return(minValue);
}
double maxValue(double& array[],int shift)
{
   double maxValue = array[shift];
            for (int i=1; i<STCPeriod; i++) maxValue = MathMax(maxValue,array[shift+i]);
   return(maxValue);
}

//+-------------------------------------------------------------------
//|                                                                  
//+-------------------------------------------------------------------
//
//
//
//
//

void manageAlerts()
{
   if ( alertsOn)
   {
      if (alertsOnCurrentBar)
           int whichBar = 0;
      else     whichBar = 1; 
      if (slope[whichBar] != slope[whichBar+1])
      {
         if (slope[whichBar] ==  1) doAlert(whichBar,"up");
         if (slope[whichBar] == -1) doAlert(whichBar,"down");
      }
   }
}

//
//
//
//
//

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)+" Super smoother Schaff trend cycle 1 slope changed to "+doWhat;
          if (alertsMessage) Alert(message);
          if (alertsEmail)   SendMail(StringConcatenate(Symbol()," Super smoother Schaff trend cycle 1 "),message);
          if (alertsSound)   PlaySound("alert2.wav");
   }
}

//------------------------------------------------------------------
//
//------------------------------------------------------------------
//
//
//
//
//

double workSsm[][6];
#define _price  0
#define _ssm    1

double workSsmCoeffs[][4];
#define _period 0
#define _c1     1
#define _c2     2
#define _c3     3
#define Pi 3.14159265358979323846264338327950288

//
//
//
//
//

double iSsm(double price, double period, int i, int instanceNo=0)
{
   if (ArrayRange(workSsm,0) !=Bars)                 ArrayResize(workSsm,Bars);
   if (ArrayRange(workSsmCoeffs,0) < (instanceNo+1)) ArrayResize(workSsmCoeffs,instanceNo+1);
   if (workSsmCoeffs[instanceNo][_period] != period)
   {
      workSsmCoeffs[instanceNo][_period] = period;
      double a1 = MathExp(-1.414*Pi/period);
      double b1 = 2.0*a1*MathCos(1.414*Pi/period);
         workSsmCoeffs[instanceNo][_c2] = b1;
         workSsmCoeffs[instanceNo][_c3] = -a1*a1;
         workSsmCoeffs[instanceNo][_c1] = 1.0 - workSsmCoeffs[instanceNo][_c2] - workSsmCoeffs[instanceNo][_c3];
   }

   //
   //
   //
   //
   //

      i = Bars-i-1;
      int s = instanceNo*2;   
          workSsm[i][s+_price] = price;
          workSsm[i][s+_ssm]   = workSsmCoeffs[instanceNo][_c1]*(workSsm[i][s+_price]+workSsm[i-1][s+_price])/2.0 + 
                                 workSsmCoeffs[instanceNo][_c2]*workSsm[i-1][s+_ssm]                              + 
                                 workSsmCoeffs[instanceNo][_c3]*workSsm[i-2][s+_ssm];
   return(workSsm[i][s+_ssm]);
}