//+------------------------------------------------------------------+
//|                                                       mStoch.mq4 |
//|                                                           9jaBoy |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "9jaBoy"
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict
#property indicator_chart_window

extern int location = 3;
extern int fontSize = 9;
extern color colour = Red;
extern int K_period = 14;
extern int D_period = 3;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
   if(location!=0)
     {
      ObjectCreate("mstoch",OBJ_LABEL,0,0,0);
      ObjectSet("mstoch",OBJPROP_CORNER,location);
      ObjectSet("mstoch",OBJPROP_XDISTANCE,5);
      ObjectSet("mstoch",OBJPROP_YDISTANCE,3);
     }
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int deinit() 
  {

   ObjectDelete("mstoch");
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
  {
//---
   double K_line_H1=iStochastic(NULL,PERIOD_H1,K_period,D_period,3,MODE_SMA,0,MODE_MAIN,1); //k - main line
   double D_line_H1=iStochastic(NULL,PERIOD_H1,K_period,D_period,3,MODE_SMA,0,MODE_SIGNAL,1);   // d - signal line

   double K_line_H4= iStochastic(NULL,PERIOD_H4,K_period,D_period,3,MODE_SMA,0,MODE_MAIN,1);
   double D_line_H4= iStochastic(NULL,PERIOD_H4,K_period,D_period,3,MODE_SMA,0,MODE_SIGNAL,1);

   double K_line_M15= iStochastic(NULL,PERIOD_M15,K_period,D_period,3,MODE_SMA,0,MODE_MAIN,1);
   double D_line_M15= iStochastic(NULL,PERIOD_M15,K_period,D_period,3,MODE_SMA,0,MODE_SIGNAL,1);
   double Previous_M15=iStochastic(NULL,PERIOD_M15,K_period,D_period,3,MODE_SMA,0,MODE_MAIN,2);
   double Current_M15=iStochastic(NULL,PERIOD_M15,K_period,D_period,3,MODE_SMA,0,MODE_MAIN,1);

//flags to prevent continuous alerts after first alert
   static bool firstick_stoch_buy=true;
   static bool mstoch_buy=false;
   static bool firstick_stoch_sell=true;
   static bool mstoch_sell=false;

   if(firstick_stoch_buy==true)
     {

      //Buy- entry Condition one when the K% Line is above D% line on the H1 and H4
      if((K_line_H1>D_line_H1) && (K_line_H4>D_line_H4))
        {
         //CONDITION TWO - The M15 Stoch cross from below and...
         if(Current_M15>25.0 && Previous_M15<25)
           {
            //we have a buy singal
            ObjectSetText("mstoch","mstoch-Buy "+Symbol()+" Ask= "+DoubleToString(Ask,Digits)+
                          " Time = "+TimeCurrent(),fontSize,"verdana",colour);
            Alert("mstoch-Buy "+Symbol()+" Ask Price = "+DoubleToString(Ask,Digits)+" Time = "+TimeCurrent());

            firstick_stoch_buy=false;      //pause alerts
            mstoch_buy=true;

           }

        }
     }

//we are on a buy, allow incoming ticks
   if(mstoch_buy==true && Current_M15>50) //M15 stoch is above the 50 level,
     {
      firstick_stoch_buy=true; //hence unpause by signals
      mstoch_buy=false;      // allow new buy signals
     }

   if(firstick_stoch_sell==true)
     {
      //Sell Entry - CONDITION ONE - when the K% Line is below D% line on the H1 and H4
      if((K_line_H1<D_line_H1) && (K_line_H4<D_line_H4))
        {
         //CONDITION TWO - The M15 Stoch cross from above and...
         if(Current_M15<75.0 && Previous_M15>75.0)
           {
            //we have a Sell singal
            ObjectSetText("mstoch","mstoch-Sell "+Symbol()+" Bid= "+DoubleToString(Bid,Digits)+
                          " Time = "+TimeCurrent(),fontSize,"verdana",Red);
            Alert("mstoch-Sell "+Symbol()+" Bid Price = "+DoubleToString(Bid,Digits)+" Time = "+TimeCurrent());
            firstick_stoch_sell=false;      //pause alerts
            mstoch_sell=true;  //we are on a sell signal

           }

        }

     }

//we are on a sell, allow incoming ticks
   if(mstoch_sell==true && Current_M15<50) //M15 stoch is below the 50 level
     {
      firstick_stoch_sell=true; // hence allow more sell signas
      mstoch_sell=false;   // wait for fresh sell signals
     }



//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+
