//+------------------------------------------------------------------+
//|                                           Trix Trend Cycle 1.mq4 |
//|                                                            Knoxy |
//+------------------------------------------------------------------+
#property copyright "mladen"
#property link      "www.forex-tsd.com"

#property indicator_separate_window
#property indicator_buffers 3
#property indicator_color1  LimeGreen
#property indicator_color2  Red
#property indicator_color3  Red
#property indicator_width1  2
#property indicator_width2  2
#property indicator_width3  2
//
//
//
//
//

extern ENUM_TIMEFRAMES TimeFrame       = PERIOD_CURRENT;
extern int             TrixPeriod      = 4;
extern bool            alertsOn        = true;
extern bool            alertsOnCurrent = false;
extern bool            alertsMessage   = true;
extern bool            alertsSound     = false;
extern bool            alertsNotify    = true;
extern bool            alertsEmail     = true;
extern string          soundFile       = "alert2.wav"; 

//
//
//
//
//

double stcBuffer[];
double Upper[];
double Lowera[];
double Lowerb[];
double trix_buffer3[];
double cdBuffer[];
double fastKBuffer[];
double fastDBuffer[];
double fastKKBuffer[];
double trix_buffer1[];
double trix_buffer2[];
double slope[];

string indicatorFileName;
bool   returnBars;

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
//
//
//
//
//

int init()
{
   IndicatorBuffers(11);
   SetIndexBuffer(0,stcBuffer);
   SetIndexBuffer(1,Lowera);
   SetIndexBuffer(2,Lowerb);
   SetIndexBuffer(3,trix_buffer3);
   SetIndexBuffer(4,cdBuffer);
   SetIndexBuffer(5,fastKBuffer);
   SetIndexBuffer(6,fastDBuffer);
   SetIndexBuffer(7,fastKKBuffer);
   SetIndexBuffer(8,trix_buffer1);
   SetIndexBuffer(9,trix_buffer2);
   SetIndexBuffer(10,slope);
   
         //
         //
         //
         //
         //

         indicatorFileName = WindowExpertName();
         returnBars        = (TimeFrame==-99);
         TimeFrame         = MathMax(TimeFrame,_Period);
         
         //
         //
         //
         //
         //
         
   IndicatorShortName(timeFrameToString(TimeFrame)+" Trix TC1 ("+TrixPeriod+") v2");
return(0);
}
int deinit() {  return(0); }

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
//
//
//
//
//

int start()
{
   double alphaCD      = 2.0/(1.0 + 3.0);
   int    counted_bars = IndicatorCounted();
   int    limit,i;

   if(counted_bars < 0) return(-1);
   if(counted_bars>0) counted_bars--;
         limit = MathMin(Bars-counted_bars,Bars-1);
         if (returnBars) { stcBuffer[0] = limit+1; return(0); }

   //
   //
   //
   //
   //
   
   if (TimeFrame==Period())
   {
     if (slope[limit]==-1) CleanPoint(limit,Lowera,Lowerb);
     for(i=0; i<limit; i++) trix_buffer1[i]=iMA(NULL,0,TrixPeriod,0,MODE_SMMA,PRICE_CLOSE,i);
     for(i=0; i<limit; i++) trix_buffer2[i]=iMAOnArray(trix_buffer1,0,TrixPeriod,0,MODE_SMMA,i);
     for(i=0; i<limit; i++) trix_buffer3[i]=iMAOnArray(trix_buffer2,0,TrixPeriod,0,MODE_EMA,i);
     for(i=limit; i>=0;i--)
     {
        cdBuffer[i]   = cdBuffer[i+1]+alphaCD*(trix_buffer3[i]-trix_buffer3[i+1]);

        //
        //
        //
        //
        //
      
        double lowCd  = minValue(cdBuffer,TrixPeriod,i);
        double highCd = maxValue(cdBuffer,TrixPeriod,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,TrixPeriod,i);
        double highStoch = maxValue(fastDBuffer,TrixPeriod,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];               
         Lowera[i] = EMPTY_VALUE;
         Lowerb[i] = EMPTY_VALUE;
            if (stcBuffer[i]>stcBuffer[i+1]) slope[i] =  1;
            if (stcBuffer[i]<stcBuffer[i+1]) slope[i] = -1;
            if (slope[i]==-1) PlotPoint(i,Lowera,Lowerb,stcBuffer);
   }
   manageAlerts();
   return(0);
   }
      
   //
   //
   //
   //
   //
      
   limit = MathMax(limit,MathMin(Bars-1,iCustom(NULL,TimeFrame,indicatorFileName,-99,0,0)*TimeFrame/Period()));
   if (slope[limit]==-1) CleanPoint(limit,Lowera,Lowerb);
   for (i=limit;i>=0; i--)
   {
      int y = iBarShift(NULL,TimeFrame,Time[i]);
         stcBuffer[i] = iCustom(NULL,TimeFrame,indicatorFileName,PERIOD_CURRENT,TrixPeriod,alertsOn,alertsOnCurrent,alertsMessage,alertsSound,alertsNotify,alertsEmail,soundFile,0,y);
         slope[i]     = iCustom(NULL,TimeFrame,indicatorFileName,PERIOD_CURRENT,TrixPeriod,alertsOn,alertsOnCurrent,alertsMessage,alertsSound,alertsNotify,alertsEmail,soundFile,10,y);
         Lowera[i]    = EMPTY_VALUE;
         Lowerb[i]    = EMPTY_VALUE;
        
   }
   for (i=limit;i>=0; i--) if (slope[i]==-1) PlotPoint(i,Lowera,Lowerb,stcBuffer);
   return(0);
}

//-------------------------------------------------------------------
//
//-------------------------------------------------------------------
//
//
//
//
//

void CleanPoint(int i,double& first[],double& second[])
{
   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 (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;
      }
}

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
//
//
//
//
//

double minValue(double& array[],int tperiod,int shift) { return(array[ArrayMinimum(array,tperiod,shift)]); }
double maxValue(double& array[],int tperiod,int shift) { return(array[ArrayMaximum(array,tperiod,shift)]); }

//+-------------------------------------------------------------------
//|                                                                  
//+-------------------------------------------------------------------
//
//
//
//
//

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 manageAlerts()
{
   if (alertsOn)
   {
      if (alertsOnCurrent)
           int whichBar = 0;
      else     whichBar = 1;
      if (slope[whichBar] != slope[whichBar+1])
      {
         if (slope[whichBar] == 1) doAlert(whichBar,"sloping up");
         if (slope[whichBar] ==-1) doAlert(whichBar,"sloping 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 =  StringConcatenate(Symbol()," at ",TimeToStr(TimeLocal(),TIME_SECONDS)," - ",timeFrameToString(_Period)+" Trix Trend Cycle ",doWhat);
          if (alertsMessage) Alert(message);
          if (alertsNotify)  SendNotification(message);
          if (alertsEmail)   SendMail(StringConcatenate(Symbol()," Trix Trend Cycle "),message);
          if (alertsSound)   PlaySound("alert2.wav");
   }
}