
//+------------------------------------------------------------------+
//|                            FREEMAN291187                         |
//+------------------------------------------------------------------+

#property indicator_separate_window
#property indicator_buffers 3
#property indicator_color1  OrangeRed
#property indicator_color2  LimeGreen
#property indicator_color3  LimeGreen
#property indicator_minimum  -20
#property indicator_maximum 120
#property indicator_level1 95
#property indicator_level2 5
#property indicator_levelstyle STYLE_DOT
#property indicator_levelcolor MediumBlue

//+------------------------------------------------------------------+
//|                            FREEMAN291187                         |
//+------------------------------------------------------------------+

extern string TimeFrame    = "Current time frame";
extern int    STCPeriod    = 10;
extern int    FastMAPeriod = 23;
extern int    SlowMAPeriod = 50;
extern int    CDPeriod     = 25;
extern double SM           = 2;
extern int    LevelForUp   = 5;
extern int    LevelForDown = 95;
extern bool   AlertsOn        = true;
extern bool   AlertsOnCurrent = false;
extern bool   AlertsMessage   = true;
extern bool   AlertsPushNotif = true;
extern bool   AlertsSound     = true;
extern bool   AlertsEmail     = true;

double stcBuffer[];
double stcBufferUA[];
double stcBufferUB[];
double macdBuffer[];
double cdBuffer[];
double fastKBuffer[];
double fastDBuffer[];
double fastKKBuffer[];
double trend[];
double cross[];

string indicatorFileName;
bool   returnBars;
int    timeFrame;


//+------------------------------------------------------------------+
//|                            FREEMAN291187                         |
//+------------------------------------------------------------------+

int init()
{
   IndicatorBuffers(8);
      SetIndexBuffer(0,stcBuffer);
      SetIndexBuffer(1,stcBufferUA);
      SetIndexBuffer(2,stcBufferUB);
      SetIndexBuffer(3,macdBuffer);
      SetIndexBuffer(4,fastKBuffer);
      SetIndexBuffer(5,fastDBuffer);
      SetIndexBuffer(6,fastKKBuffer);
      SetIndexBuffer(7,cdBuffer);
      
         indicatorFileName = WindowExpertName();
         returnBars        = TimeFrame == "returnBars";     if (returnBars)     return(0);
         timeFrame         = stringToTimeFrame(TimeFrame);
         
   IndicatorShortName("Scalping ("+STCPeriod+","+FastMAPeriod+","+SlowMAPeriod+")");
   return(0);
}

int deinit()
{
   return(0);
}


//+------------------------------------------------------------------+
//|                            FREEMAN291187                         |
//+------------------------------------------------------------------+

int start()
{
   double alphaCD = SM / (1.0 + CDPeriod);
   int    i,r,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) { stcBuffer[0] = limit+1; return(0); }
         if (ArraySize(trend)!=Bars) ArrayResize(trend,Bars);
         if (ArraySize(cross)!=Bars) ArrayResize(cross,Bars);
         if (timeFrame!=Period())
         {
            limit = MathMax(limit,MathMin(Bars-1,iCustom(NULL,timeFrame,indicatorFileName,"returnBars",0,0)*timeFrame/Period()));
            if (trend[Bars-limit-1]==-1) CleanPoint(limit,stcBufferUA,stcBufferUB);
            for(i = limit,r=Bars-i-1; i >= 0; i--,r++)
            {
               int y = iBarShift(NULL,timeFrame,Time[i]);               
                  stcBuffer[i]   = iCustom(NULL,timeFrame,indicatorFileName,"calculateValue",STCPeriod,FastMAPeriod,SlowMAPeriod,CDPeriod,SM,LevelForUp,LevelForDown,AlertsOn,AlertsOnCurrent,AlertsMessage,AlertsPushNotif,AlertsSound,AlertsEmail,0,y);
                  stcBufferUA[i] = EMPTY_VALUE;
                  stcBufferUB[i] = EMPTY_VALUE;
                  trend[r]       = trend[r-1];      
                     if (stcBuffer[i] > stcBuffer[i+1]) trend[r] = -1;
                     if (stcBuffer[i] < stcBuffer[i+1]) trend[r] =1;
                     if (trend[r] == 1) PlotPoint(i,stcBufferUA,stcBufferUB,stcBuffer);
            }
            return(0);                  
         }
         


//+------------------------------------------------------------------+
//|                            FREEMAN291187                         |
//+------------------------------------------------------------------+

   if (trend[Bars-limit-1]==1) CleanPoint(limit,stcBufferUA,stcBufferUB);
   for(i = limit,r=Bars-i-1; i >= 0; i--,r++)
   {
      macdBuffer[i] = iMA(NULL,0,FastMAPeriod,0,MODE_EMA,PRICE_CLOSE,i)-
                      iMA(NULL,0,SlowMAPeriod,0,MODE_EMA,PRICE_CLOSE,i);  
      cdBuffer[i]   = cdBuffer[i+1]+alphaCD*(macdBuffer[i]-cdBuffer[i+1]);
   

//+------------------------------------------------------------------+
//|                            FREEMAN291187                         |
//+------------------------------------------------------------------+
      
         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]);
               

//+------------------------------------------------------------------+
//|                            FREEMAN291187                         |
//+------------------------------------------------------------------+
                           
         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]);

//+------------------------------------------------------------------+
//|                            FREEMAN291187                         |
//+------------------------------------------------------------------+

         trend[r]=trend[r-1];      
         cross[r]=cross[r-1];      
               if (stcBuffer[i] >= LevelForUp)   cross[r] =  -1;
               if (stcBuffer[i] <= LevelForDown) cross[r] = 1;
         stcBufferUA[i] = EMPTY_VALUE;
         stcBufferUB[i] = EMPTY_VALUE;
            if (stcBuffer[i] > stcBuffer[i+1]) trend[r] = 1;
            if (stcBuffer[i] < stcBuffer[i+1]) trend[r] =-1;
            if (trend[r] == 1) PlotPoint(i,stcBufferUA,stcBufferUB,stcBuffer);
   }   
   manageAlerts();
   return(0);
}



//+------------------------------------------------------------------+
//|                            FREEMAN291187                         |
//+------------------------------------------------------------------+

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);
}


//+------------------------------------------------------------------+
//|                            FREEMAN291187                         |
//+------------------------------------------------------------------+

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;
}


//+------------------------------------------------------------------+
//|                            FREEMAN291187                         |
//+------------------------------------------------------------------+

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;
      }
}


//+------------------------------------------------------------------+
//|                            FREEMAN291187                         |
//+------------------------------------------------------------------+

string sTfTable[] = {"M1","M5","M15","M30","H1","H4","D1","W1","MN"};
int    iTfTable[] = {1,5,15,30,60,240,1440,10080,43200};


//+------------------------------------------------------------------+
//|                            FREEMAN291187                         |
//+------------------------------------------------------------------+

int stringToTimeFrame(string tfs)
{
   tfs = stringUpperCase(tfs);
   for (int i=ArraySize(iTfTable)-1; i>=0; i--)
         if (tfs==sTfTable[i] || tfs==""+iTfTable[i]) return(MathMax(iTfTable[i],Period()));
                                                      return(Period());
}


//+------------------------------------------------------------------+
//|                            FREEMAN291187                         |
//+------------------------------------------------------------------+

string stringUpperCase(string str)
{
   string   s = str;

   for (int length=StringLen(str)-1; length>=0; length--)
   {
      int tchar = StringGetChar(s, length);
         if((tchar > 96 && tchar < 123) || (tchar > 223 && tchar < 256))
                     s = StringSetChar(s, length, tchar - 32);
         else if(tchar > -33 && tchar < 0)
                     s = StringSetChar(s, length, tchar + 224);
   }
   return(s);
}


//+------------------------------------------------------------------+
//|                            FREEMAN291187                         |
//+------------------------------------------------------------------+

void manageAlerts()
{
   if (AlertsOn)
   {
      int whichBar = 1; if (AlertsOnCurrent) whichBar = 0; whichBar = Bars-whichBar-1;
      if (cross[whichBar] != cross[whichBar-1])
      {
         if (cross[whichBar] ==  1) doAlert(0,"SELL now");
         if (cross[whichBar] == -1) doAlert(0," BUY now");
      }         
   }
}

//
//
//
//
//

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 = "Scalping - "+ StringConcatenate(Symbol()+" "+Period()+"M"+" - ",doWhat);
             if (AlertsMessage)      Alert(message," - Price ",Ask);
             if (AlertsEmail)        SendMail(StringConcatenate(Symbol()+" "+Period()+"M"+" - Price ",Ask," "),message);
             if (AlertsPushNotif)    SendNotification(StringConcatenate(message," at ",Ask+" - "+Period()+"M"));
             if (AlertsSound)        PlaySound("alert2.wav");
 }
}

//+------------------------------------------------------------------+
//|                            FREEMAN291187                         |
//+------------------------------------------------------------------+