//+------------------------------------------------------------------+
//|                                         Melpheos_StochV.mq4      |
//|         Copyright © 2007, A.Meignan                              |
//|                   http://www.forexfactory.com                    |
//+------------------------------------------------------------------+
// Chandra (ACS) modified on 03-Dec-07. Correction of the alert system
// Pop script credit to Coder's guru @ forex-tsd
/*
  +------------------------------------------------------------------+
  | Shows signals when the Boxingislife strategy rules               |
  | of stochastics are respected.                                    |
  |                                                                  |
  | Mainly this strategy relies on waiting for a pull back           |
  | on Slower term move                                              |
  +------------------------------------------------------------------+
*/
#property copyright "Copyright © 2007, Melpheos"
#property link      "http://www.forexfactory.com"
#import "shell32.dll"
   int ShellExecuteA(int hwnd,string lpOperation, string lpFile,string lpParameters,string lpDirectory,int nShowCmd );
#import

void pop ( string msg , string installed_dir)

{
   ShellExecuteA(0,"Open" ,"pop.exe",msg,installed_dir,1);
}


#property indicator_chart_window
#property indicator_buffers 6
#property indicator_color1 Lime
#property indicator_color2 Magenta
#property indicator_width1 3
#property indicator_width2 3
#property indicator_color3 Lime
#property indicator_color4 Magenta
#property indicator_width3 3
#property indicator_width4 3
#property indicator_color5 Lime
#property indicator_color6 Magenta
#property indicator_width5 3
#property indicator_width6 3

//+------------------------------------------------------------------+
//| Stochastics Settings                                             |
//+------------------------------------------------------------------+

extern string Fast_Stoch_Settings = "";
extern int K1 = 4;
extern int D1 = 2;
extern int S1 = 2;
extern int M1 = 2; //0=sma, 1=ema, 2=smma, 3=lwma
extern int Price_Field1 = 1; // 0=Low/High 1=Close/Close

extern string Medium_Stoch_Settings = "";
extern int K2 = 20;
extern int D2 = 2;
extern int S2 = 10;
extern int M2 = 2; 
extern int Price_Field2 = 1;

extern string Slow_Stoch_Settings = "";
extern int K3 = 50;
extern int D3 = 2;
extern int S3 = 10;
extern int M3 = 2; 
extern int Price_Field3 = 1;

//+------------------------------------------------------------------+
//| Global Filtering Settings                                        |
//+------------------------------------------------------------------+

extern string Global_Bollinger_Filtering = "";
extern int bollinger_period = 20;
extern int bollinger_deviation = 2;
extern int spread_multiplicator = 4;
extern string Global_MA_Filtering = "";
extern int MA_Filter_Period = 10;
extern int MA_Price_Field = 1;
extern int MA_Method = 0;
extern double MA_Speed_Filter = 0.0; 

//+------------------------------------------------------------------+
//| Filtering Settings Case 1                                        |
//+------------------------------------------------------------------+

extern string Case1_Filtering_Settings = "";
extern bool Show_Case1 = True;
extern int OSOB_Fast_Stoch1 = 10; // point above/below 0/100 level for o/s o/b level K1 stoch
extern int OSOB_Med_Stoch1 = 20; // point above/below 0/100 level for o/s o/b level K2 stoch
extern int OSOB_Slow_Stoch1 = 10; // point above/below 0/100 level for o/s o/b level K3 stoch
extern double Medium_Stoch_Speed_Filter1 = 0; // Medium stoch speed filter
extern double Slow_Stoch_Speed_Filter1 = 0; // Slow stoch speed filter

//+------------------------------------------------------------------+
//| Filtering Settings Case 2                                        |
//+------------------------------------------------------------------+

extern string Case2_Settings = "";
extern bool Show_Case2 = True;
extern int OSOB_Fast_Stoch2 = 10; 
extern int OSOB_Med_Stoch2 = 40; 
extern int OSOB_Slow_Stoch2 = 20; 
extern double Medium_Stoch_Speed_Filter2 = 4;
extern double Slow_Stoch_Speed_Filter2 = 2;

//+------------------------------------------------------------------+
//| Filtering Settings Case 4                                        |
//+------------------------------------------------------------------+ 

extern string Case4_Settings = "";
extern bool Show_Case4 = False;
extern int OSOB_Fast_Stoch4 = 20; 
extern int Super_OSOB_Filter = 20; // Super O/b O/s filter 

//+------------------------------------------------------------------+
//| Signal Distance                                                  |
//+------------------------------------------------------------------+ 

extern string Signal_Distance_Setting = "";
extern double Signal_Distance = 1.6;

//+------------------------------------------------------------------+
//| Filtering Settings Alerts                                        |
//+------------------------------------------------------------------+ 

extern bool DisplayAlert = true; // use  Alert
extern bool emailAlert = false; // use mail Alert
extern bool PopUp = true; // use Msn style Popup alert

//-----

double up1[];
double down1[];
double up2[];
double down2[];
double up4[];
double down4[];

//-----

datetime lastAlertTime;

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+

int init() {
   SetIndexStyle(0, DRAW_ARROW, EMPTY);
   SetIndexArrow(0, 129);
   SetIndexBuffer(0, up1);
   SetIndexStyle(1, DRAW_ARROW, EMPTY);
   SetIndexArrow(1, 129);
   SetIndexBuffer(1, down1);
   SetIndexStyle(2, DRAW_ARROW, EMPTY);
   SetIndexArrow(2, 130);
   SetIndexBuffer(2, up2);
   SetIndexStyle(3, DRAW_ARROW, EMPTY);
   SetIndexArrow(3, 130);
   SetIndexBuffer(3, down2);
   SetIndexStyle(4, DRAW_ARROW, EMPTY);
   SetIndexArrow(4, 132);
   SetIndexBuffer(4, up4);
   SetIndexStyle(5, DRAW_ARROW, EMPTY);
   SetIndexArrow(5, 132);
   SetIndexBuffer(5, down4);
   return(0); 
}

//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+

int deinit() 
{ 
   return(0);
}

//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+

int start() {
   int limit, i, counter;
   double stoch5now, stoch5previous, stoch5after;
   double stoch20now, stoch20after, stoch20previous;
   double stoch50now, stoch50after, stoch50previous;
   double maafter, manow;
   double bbup, bbdown;
   double Range, AvgRange;

   int counted_bars=IndicatorCounted();
   if(counted_bars<0) return(-1);
   if(counted_bars>0) counted_bars--;
   limit=Bars-counted_bars;
   
   for(i = 0; i <= limit; i++) {
      AvgRange=0;
      for (counter=i ;counter<=i+10;counter++) 
      AvgRange=AvgRange+MathAbs(High[counter]-Low[counter]);
      Range=AvgRange/11;
      
//+------------------------------------------------------------------+
//| Variable Begin                                                   |
//+------------------------------------------------------------------+

      stoch5now       = iStochastic( NULL, 0, K1, D1, S1, M1, Price_Field1, MODE_SIGNAL, i+2);
      stoch5previous  = iStochastic( NULL, 0, K1, D1, S1, M1, Price_Field1, MODE_SIGNAL, i+3);
      stoch5after  = iStochastic( NULL, 0, K1, D1, S1, M1, Price_Field1, MODE_SIGNAL, i+1);
      stoch20previous = iStochastic( NULL, 0, K2, D2, S2, M2, Price_Field2, MODE_SIGNAL, i+3);
      stoch20now = iStochastic( NULL, 0, K2, D2, S2, M2, Price_Field2, MODE_SIGNAL, i+2);
      stoch20after = iStochastic( NULL, 0, K2, D2, S2, M2, Price_Field2, MODE_SIGNAL, i+1);
      stoch50previous = iStochastic( NULL, 0, K3, D3, S3, M3, Price_Field3, MODE_SIGNAL, i+3);
      stoch50now = iStochastic( NULL, 0, K3, D3, S3, M3, Price_Field3, MODE_SIGNAL, i+2);
      stoch50after = iStochastic( NULL, 0, K3, D3, S3, M3, Price_Field3, MODE_SIGNAL, i+1);
      maafter = iMA(NULL, 0, MA_Filter_Period, 0, MA_Method, MA_Price_Field, i+1);
      manow = iMA(NULL, 0, MA_Filter_Period, 0, MA_Method, MA_Price_Field, i+3);
      bbup = iBands(NULL, 0, bollinger_period, bollinger_deviation, 0, PRICE_OPEN, MODE_UPPER, i );
      bbdown = iBands(NULL, 0, bollinger_period, bollinger_deviation, 0, PRICE_OPEN, MODE_LOWER, i );
 
      
//+------------------------------------------------------------------+
//| Variable End                                                     |
//+------------------------------------------------------------------+

//+------------------------------------------------------------------+
//| Clean arrows                                                     |
//+------------------------------------------------------------------+

      up1[i]   = 0;
      down1[i] = 0;
      up2[i]   = 0;
      down2[i] = 0;
      up4[i]   = 0;
      down4[i] = 0;
      
//+------------------------------------------------------------------+
//| Main tests start here                                            |
//+------------------------------------------------------------------+
/* 
  +----------------------------------------------------------------------------+
  | 1st Case up                                                                |
  |                                                                            |
  | V shape exist and is lower than set level                                  |
  | Medium Speed stochastic is heading north with minimum speed,               |
  | slow stoch is higher than oversold level,                                  |
  | slow stoch is not yet in overbought and heading north with minimum speed,  |
  | MA is heading north with minimum speed                                     |
  +----------------------------------------------------------------------------+
*/
     if   (( stoch5now < stoch5previous )
       && (  stoch5now < stoch5after )
       && (  stoch5now < (100 - OSOB_Fast_Stoch1 ))
       && (( stoch20after - stoch20now ) > Medium_Stoch_Speed_Filter1 )
       && (  stoch20after > stoch20now )
       && (  stoch50after > OSOB_Slow_Stoch1 )
       && (  stoch50after > stoch50now )
       && (  stoch50after < (100 - OSOB_Slow_Stoch1 ))
       && (( stoch50after - stoch50now ) > Slow_Stoch_Speed_Filter1 )
       && (( maafter - manow) > MA_Speed_Filter )
       && (  maafter > manow )
       && (( bbup - bbdown) > (( Ask - Bid )* spread_multiplicator ))
       && (  Show_Case1 == True ))

            up1[i] = Low[i] - Range*Signal_Distance;
/* 
  +----------------------------------------------------------------------------+
  | 2nd Case up                                                                |
  |                                                                            |
  | V shape exist and is lower than set level,                                 |
  | Medium Speed stochastic is higher than overbought level,                   |
  | Medium Stoch in not in a strong downward trend                             |
  | slow stoch is higher than overbought level,                                |
  | MA is heading north with minimum speed                                     |
  +----------------------------------------------------------------------------+
*/
     if   (( stoch5now < stoch5previous )
       && (  stoch5now < stoch5after )
       && (  stoch5now < (100 - OSOB_Fast_Stoch2 ))
       && (  stoch20after > (100 - OSOB_Med_Stoch2 ))
       && (( stoch20now - stoch20after) < Medium_Stoch_Speed_Filter2 )
       && (( stoch50previous - stoch50after ) < Slow_Stoch_Speed_Filter2 )
       && (  stoch50after > (100 - OSOB_Slow_Stoch2 ))
       && (( maafter - manow) > MA_Speed_Filter )
       && (  maafter > manow )
       && (( bbup - bbdown) > (( Ask - Bid )* spread_multiplicator ))
       && (  Show_Case2 == True)
       && (  up1[i] ==0))

            up2[i] = Low[i] - Range*Signal_Distance;
/*
  +----------------------------------------------------------------------------+
  | 4th Case up                                                                |
  |                                                                            |
  | Reversal pattern 1                                                         |
  +----------------------------------------------------------------------------+
*/
     if   (( stoch20now < stoch20previous )
       && (  stoch20now < stoch20after )
       && (  stoch50now < stoch50previous)
       && (  stoch50now < stoch20after)
       && (  stoch20now < Super_OSOB_Filter )
       && (  stoch50now < Super_OSOB_Filter )
       && (( manow - maafter) < MA_Speed_Filter )
       && (  maafter > manow)
       && (  Show_Case4 == True))

          up4[i] = Low[i] - Range*Signal_Distance;
/* 
  +----------------------------------------------------------------------------+
  | 4th Case up                                                                |
  |                                                                            |
  | Reversal pattern 2                                                         |
  +----------------------------------------------------------------------------+
*/
     if    (( stoch5now < stoch5previous )
       && (   stoch5now < stoch5after )
       && (   stoch5now < OSOB_Fast_Stoch4 )
       && (   stoch20after < stoch20now )
       && (   stoch50after < stoch20now)
       && (   stoch20now < Super_OSOB_Filter )
       && (   stoch50now < Super_OSOB_Filter )
       && ((  manow - maafter) < MA_Speed_Filter )
       && (   maafter > manow)
       && (   Show_Case4 == True))

          up4[i] = Low[i] - Range*Signal_Distance;
/*
  +----------------------------------------------------------------------------+
  | 1st Case down                                                              |
  |                                                                            |
  | /\ shape exist and is higher than set level,                               |
  | Medium Speed stochastic is heading south with minimum speed,               |
  | slow stoch is higher than overbought level,                                |
  | slow stoch is not yet in oversoldt and heading south with minimum speed,   |
  | MA is heading south with minimum speed                                     |
  +----------------------------------------------------------------------------+
*/
      if  (( stoch5now > stoch5previous )
       && (  stoch5now > stoch5after )
       && (  stoch5now > OSOB_Fast_Stoch1 )
       && (( stoch20now - stoch20after) > Medium_Stoch_Speed_Filter1 )
       && (  stoch20after < stoch20now )
       && (  stoch50after < (100-OSOB_Slow_Stoch1 ))
       && (  stoch50after < stoch50now )
       && (  stoch50after > OSOB_Slow_Stoch1 )
       && (( stoch50now - stoch50after) > Slow_Stoch_Speed_Filter1 )
       && (( manow - maafter) > MA_Speed_Filter )
       && (  maafter < manow )
       && (( bbup - bbdown) > (( Ask - Bid )* spread_multiplicator ))
       && (  Show_Case1 == True))

            down1[i] = High[i] + Range*Signal_Distance;
/*
  +----------------------------------------------------------------------------+
  | 2nd Case down                                                              |
  |                                                                            |
  | /\ shape exist and is higher than set level,                               |
  | Medium Speed stochastic is lower than oversold level,                      |
  | Medium Stoch in not in a strong upward trend                               |
  | slow stoch is lower than oversold level,                                   |
  | MA is heading south with minimum speed                                     |
  +----------------------------------------------------------------------------+
*/
      if  (( stoch5now > stoch5previous )
       && (  stoch5now > stoch5after )
       && (  stoch5now > OSOB_Fast_Stoch2 )
       && (  stoch20after < OSOB_Med_Stoch2 )
       && (( stoch20after - stoch20now) < Medium_Stoch_Speed_Filter2 )
       && (( stoch50after - stoch50previous ) < Slow_Stoch_Speed_Filter2 )
       && (  stoch50after < OSOB_Slow_Stoch2 )
       && (( manow - maafter) > MA_Speed_Filter )
       && (  manow > maafter)
       && (( bbup - bbdown) > (( Ask - Bid )* spread_multiplicator ))
       && (  Show_Case2 == True)
       && (  down1[i] == 0))

            down2[i] = High[i] + Range*Signal_Distance;

/* 
  +----------------------------------------------------------------------------+
  | 4th Case down                                                              |
  |                                                                            |
  | Reversal pattern 1                                                         |
  +----------------------------------------------------------------------------+
*/
     if   ((stoch20now > stoch20previous )
       && (stoch20now > stoch20after )
       && (stoch50now > stoch50previous)
       && (stoch50now > stoch20after)
       && (stoch20now > (100 - Super_OSOB_Filter ))
       && (stoch50now > (100 - Super_OSOB_Filter ))
       && (( manow - maafter) > MA_Speed_Filter )
       && ( maafter < manow)
       && ( Show_Case4 == True))

          down4[i] = High[i] + Range*Signal_Distance;
/* 
  +----------------------------------------------------------------------------+
  | 4th Case down                                                                |
  |                                                                            |
  | Reversal pattern 2                                                         |
  |                                                                            |
  +----------------------------------------------------------------------------+
*/
     if   ((stoch5now > stoch5previous )
       && (stoch5now > stoch5after )
       && (stoch5now > (100 - OSOB_Fast_Stoch4 ))
       && (stoch20after > stoch20now )
       && (stoch50after > stoch20now)
       && (stoch20now > (100 - Super_OSOB_Filter ))
       && (stoch50now < (100 - Super_OSOB_Filter ))
       && (( manow - maafter) > MA_Speed_Filter )
       && ( maafter < manow)
       && (Show_Case4 == True))

          down4[i] = High[i] + Range*Signal_Distance;
   } // end of For loop.

   if (up1[0] > 0) SendAlert ( " Stochastic Uptrend Case 1 : ");
   if (up2[0] > 0) SendAlert ( " Stochastic Uptrend Case 2 : ");
   if (down1[0] > 0) SendAlert ( " Stochastic Downtrend Case 1 : ");
   if (down2[0] > 0) SendAlert ( " Stochastic Downtrend Case 2 : ");
   
   return(0); 
}

//+------------------------------------------------------------------+
//| Diplay alert module                                              |
//+------------------------------------------------------------------+

void SendAlert(string message) 
{
   if(Time[0] > lastAlertTime)
      {
         lastAlertTime = TimeCurrent();
         if (DisplayAlert) Alert(message, Symbol(), " , ", Period(), " minutes chart");
         if (emailAlert) SendMail(message + Symbol(), message + Symbol() + " , " + Period() + " minutes chart");
         if (PopUp) 
            { message = message + Symbol()+ " , " + Period() + " minutes chart";
              pop (message, "c:");
            }  
      }
   return; 
}
//+------------------------------------------------------------------+

