//+--------------------------------------------------------------------+
//|                                               FibBreakout1.2.mq4   |
//|                                                      version 1.2   |
//|                   Copyright © 2004-07, MetaQuotes Software Corp.   |
//|                                       http://www.metaquotes.net/   |
//| http://www.forexfactory.com/showthread.php?t=30109                 |
//| based on Spud's MTF FIB Breakout System                            |
//| This indicator will display the K% line of the Stochastic only     |
//| You can turn the Alert on or off and it'll alert only once per bar |
//| version 1.1: 2 stochs are combined into 1 indicator                |
//| version 1.2: changed alerts according to new rules                 |
//+--------------------------------------------------------------------+
//+--------------------------------------------------------------------+
//|                                                     Stochastic.mq4 |
//|                     Copyright © 2004-07, MetaQuotes Software Corp. |
//|                                         http://www.metaquotes.net/ |
//+--------------------------------------------------------------------+
#property copyright "Copyright © 2004-07, MetaQuotes Software Corp."
#property link      "http://www.metaquotes.net/"

#property indicator_separate_window
#property indicator_minimum 0
#property indicator_maximum 100
#property indicator_buffers 2
#property indicator_color1 Blue //Stoch 5,3,3
#property indicator_color2 Red  //Stoch 14,3,3
#property indicator_style2 STYLE_DOT  //Stoch 14,3,3
#property indicator_level1 76.4
#property indicator_level2 61.8
#property indicator_level3 50.0
#property indicator_level4 38.2
#property indicator_level5 23.6
//+-- input parameters -----------------------------------------------+
extern string note1 = "First Stochastic";
extern int KPeriod1=5;
extern int DPeriod1=3;
extern int Slowing1=3;
extern string note2 = "Second Stochastic";
extern int KPeriod2=14;
extern int DPeriod2=3;
extern int Slowing2=3;
extern string note5 = "turn on Alert = true; turn off = false";
extern bool AlertOn = true;
//+-- buffers -------------------------------------------------------+
double MainBuffer1[];
double HighesBuffer1[];
double LowesBuffer1[];
double MainBuffer2[];
double HighesBuffer2[];
double LowesBuffer2[];
int draw_begin1=0;
int draw_begin2=0;
int draw_begin3=0;
int draw_begin4=0;
//+-- Show regular timeframe string (HCY) ---------------------------+
string AlertPrefix;
string GetTimeFrameStr() {
   switch(Period())
   {
      case 1 : string TimeFrameStr="M1"; break;
      case 5 : TimeFrameStr="M5"; break;
      case 15 : TimeFrameStr="M15"; break;
      case 30 : TimeFrameStr="M30"; break;
      case 60 : TimeFrameStr="H1"; break;
      case 240 : TimeFrameStr="H4"; break;
      case 1440 : TimeFrameStr="D1"; break;
      case 10080 : TimeFrameStr="W1"; break;
      case 43200 : TimeFrameStr="MN1"; break;
      default : TimeFrameStr="CUR";
   } 
   return (TimeFrameStr);
} //string GetTimeFrameStr()
//+------------------------------------------------------------------+
int deinit() {
return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
   string short_name;
//+-- 6 additional buffers are used for counting.
   IndicatorBuffers(6);
      SetIndexStyle(0,DRAW_LINE);
      SetIndexBuffer(0, MainBuffer1);
      SetIndexStyle(1,DRAW_LINE);
      SetIndexBuffer(1, MainBuffer2);  
      SetIndexBuffer(2, HighesBuffer1);
      SetIndexBuffer(3, LowesBuffer1);
      SetIndexBuffer(4, HighesBuffer2);
      SetIndexBuffer(5, LowesBuffer2);
   
//+-- name for DataWindow and indicator subwindow label
   switch(Period())
   {
      case 1 : string TimeFrameStr2=",M1:"; break;
      case 5 : TimeFrameStr2=",M5:"; break;
      case 15 : TimeFrameStr2=",M15:"; break;
      case 30 : TimeFrameStr2=",M30:"; break;
      case 60 : TimeFrameStr2=",H1:"; break;
      case 240 : TimeFrameStr2=",H4"; break;
      case 1440 : TimeFrameStr2=",D1:"; break;
      case 10080 : TimeFrameStr2=",W1:"; break;
      case 43200 : TimeFrameStr2=",MN1:"; break;
      default : TimeFrameStr2=",Current Timeframe:";
   } 
   short_name=Symbol()+TimeFrameStr2+" Stoch("+KPeriod1+","+DPeriod1+","+Slowing1+")" + " Stoch("+KPeriod2+","+DPeriod2+","+Slowing2+")";
   IndicatorShortName(short_name);
   SetIndexLabel(0,short_name);
   SetIndexLabel(1,"Signal");
//+-- 
   draw_begin1=KPeriod1+Slowing1;
   draw_begin2=draw_begin1+DPeriod1;
   SetIndexDrawBegin(0,draw_begin1);
   SetIndexDrawBegin(1,draw_begin2);
   draw_begin3=KPeriod2+Slowing2;
   draw_begin4=draw_begin3+DPeriod2;
   SetIndexDrawBegin(2,draw_begin3);
   SetIndexDrawBegin(3,draw_begin4);
//+-- 
   // Show regular timeframe string (HCY)
   AlertPrefix=Symbol()+" ("+GetTimeFrameStr()+"):  ";

   return(0);
}
//+------------------------------------------------------------------+
bool NewBar()
{
   static datetime lastbar;
   datetime curbar = Time[0];
   if(lastbar!=curbar)
   {
      lastbar=curbar;
      return (true);
   }
   else
   {
      return(false);
   }
}   
//+------------------------------------------------------------------+
//| Stochastic oscillator                                            |
//+------------------------------------------------------------------+
int start()
  {
   int    i,k;
   int    counted_bars=IndicatorCounted();
   double price;

   if((Bars<=draw_begin2) || (Bars<=draw_begin4)) return(0);
//+-- initial zero
   if(counted_bars<1)
     {
      for(i=1;i<=draw_begin1;i++) MainBuffer1[Bars-i]=0;
      for(i=1;i<=draw_begin3;i++) MainBuffer2[Bars-i]=0;
     }
//+-- minimums counting
   i=Bars-KPeriod1;
   if(counted_bars>KPeriod1) i=Bars-counted_bars-1;
   while(i>=0)
     {
      double min=1000000;
      k=i+KPeriod1-1;
      while(k>=i)
        {
         price=Low[k];
         if(min>price) min=price;
         k--;
        }
      LowesBuffer1[i]=min;
      i--;
     }
   i=Bars-KPeriod2;
   if(counted_bars>KPeriod2) i=Bars-counted_bars-1;
   while(i>=0)
     {
      min=1000000;
      k=i+KPeriod2-1;
      while(k>=i)
        {
         price=Low[k];
         if(min>price) min=price;
         k--;
        }
      LowesBuffer2[i]=min;
      i--;
     }
//+-- maximums counting
   i=Bars-KPeriod1;
   if(counted_bars>KPeriod1) i=Bars-counted_bars-1;
   while(i>=0)
     {
      double max=-1000000;
      k=i+KPeriod1-1;
      while(k>=i)
        {
         price=High[k];
         if(max<price) max=price;
         k--;
        }
      HighesBuffer1[i]=max;
      i--;
     }
   i=Bars-KPeriod2;
   if(counted_bars>KPeriod2) i=Bars-counted_bars-1;
   while(i>=0)
     {
      max=-1000000;
      k=i+KPeriod2-1;
      while(k>=i)
        {
         price=High[k];
         if(max<price) max=price;
         k--;
        }
      HighesBuffer2[i]=max;
      i--;
     }
//+-- %K line
   i=Bars-draw_begin1;
   if(counted_bars>draw_begin1) i=Bars-counted_bars-1;
   while(i>=0)
     {
      double sumlow=0.0;
      double sumhigh=0.0;
      for(k=(i+Slowing1-1);k>=i;k--)
        {
         sumlow+=Close[k]-LowesBuffer1[k];
         sumhigh+=HighesBuffer1[k]-LowesBuffer1[k];
        }
      if(sumhigh==0.0) MainBuffer1[i]=100.0;
      else MainBuffer1[i]=sumlow/sumhigh*100;
      i--;
     }

   i=Bars-draw_begin3;
   if(counted_bars>draw_begin3) i=Bars-counted_bars-1;
   while(i>=0)
     {
      sumlow=0.0;
      sumhigh=0.0;
      for(k=(i+Slowing2-1);k>=i;k--)
        {
         sumlow+=Close[k]-LowesBuffer2[k];
         sumhigh+=HighesBuffer2[k]-LowesBuffer2[k];
        }
      if(sumhigh==0.0) MainBuffer2[i]=100.0;
      else MainBuffer2[i]=sumlow/sumhigh*100;
      i--;
     }
//+-- last counted bar will be recounted
   if(counted_bars>0) counted_bars--;
   int limit=Bars-counted_bars;

//+-- Alerts
   if(AlertOn && NewBar()){
      if((MainBuffer1[0] >= indicator_level1) && (MainBuffer2[0] >= indicator_level1))
         {
         Alert(AlertPrefix+"Both Stochs cross up 76.4%");
         }
      else
      {
        if(MainBuffer1[0] >= indicator_level1)
         {
         Alert(AlertPrefix+"Only Stoch("+KPeriod1+","+DPeriod1+","+Slowing1+") crosses up 76.4%");
         }
        if(MainBuffer2[0] >= indicator_level1)
         {
         Alert(AlertPrefix+"Only Stoch("+KPeriod2+","+DPeriod2+","+Slowing2+") crosses up 76.4%");
         }
      }
      if((MainBuffer1[0] <= indicator_level5) && (MainBuffer2[0] <= indicator_level5))
         {
         Alert(AlertPrefix+"Both Stochs cross down 23.6%");
         }     
      else
      {
        if(MainBuffer1[0] <= indicator_level5)
         {
         Alert(AlertPrefix+"Only Stoch("+KPeriod1+","+DPeriod1+","+Slowing1+") crosses down 23.6%");
         }     
        if(MainBuffer2[0] <= indicator_level5)
         {
         Alert(AlertPrefix+"Only Stoch("+KPeriod2+","+DPeriod2+","+Slowing2+") crosses down 23.6%");
         }
      }
    }
//+-- Alert Ends
   return(0);
  }
//+------------------------------------------------------------------+