//+------------------------------------------------------------------+
//|                                   Stochastic different pairs.mq4 |
//|                      Copyright © 2011, MetaQuotes Software Corp. |
//|                                        http://www.metaquotes.net |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2011, MetaQuotes Software Corp."
#property link      "http://www.metaquotes.net"

#property indicator_separate_window
#property indicator_minimum 0
#property indicator_maximum 100
#property indicator_level1 20
#property indicator_level2 80
#property indicator_level3 50
#property indicator_buffers 4

extern int K = 100;
extern int D = 1;
extern int S = 1;

extern string  FirstSymbol = "";
extern string  SecondSymbol = "XXXXXX";
extern string  ThirdSymbol = "XXXXXX";

extern color   FirstColor = DodgerBlue;
extern color   SecondColor = Red;
extern color   ThirdColor = Lime;
extern color   DiffColor = Yellow;

extern bool    InvertFirstSymbol = false;
extern bool    InvertSecondSymbol = false;
extern bool    InvertThirdSymbol = false;


double buffer1[], buffer2[],buffer3[],buffer4[];
string m="";
int    win;
string Text1,Text2,Text3;
datetime alertoncea=0, alertoncee=0;
bool     emailonce = true,alertonce = true;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
      m = StringSubstr(Symbol(),6,StringLen(Symbol())-6);
      SecondSymbol = SecondSymbol+m;
      ThirdSymbol = ThirdSymbol+m;
      if (FirstSymbol == "" )
         FirstSymbol = Symbol();
      else
         FirstSymbol = FirstSymbol+m;
      
      string name,char4;
      
      
            int obj = ObjectsTotal();
            for ( int n=obj;n > 0 ;n--)
               {
                  name = ObjectName(n);
                  char4 = StringSubstr(name,0,4);
                  if ( char4 == "Diff" )
                    ObjectDelete(name);   
               }
               
       IndicatorShortName("Stochastic ("+K+","+D+","+S+") "+FirstSymbol+"|"+SecondSymbol);
       win = WindowFind("Stochastic ("+K+","+D+","+S+") "+FirstSymbol+"|"+SecondSymbol);        
       
                      
//---- indicators
      SetIndexBuffer(0,buffer1);
      SetIndexStyle(0,DRAW_LINE,STYLE_SOLID,NULL,FirstColor);
      SetIndexLabel(0,FirstSymbol);

      SetIndexBuffer(1,buffer2);
      SetIndexStyle(1,DRAW_LINE,STYLE_SOLID,NULL,SecondColor);
      SetIndexLabel(1,SecondSymbol);    
      
      SetIndexBuffer(3,buffer4);
      SetIndexStyle(3,DRAW_LINE,STYLE_SOLID,NULL,ThirdColor);
      SetIndexLabel(3,ThirdSymbol); 

      SetIndexBuffer(2,buffer3);
      SetIndexStyle(2,DRAW_LINE,STYLE_DOT,NULL,DiffColor);
      SetIndexLabel(2,"Difference");    

IndicatorDigits(2);

//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
   string name,char4;
      
      
            int obj = ObjectsTotal();
            for ( int n=obj;n > 0 ;n--)
               {
                  name = ObjectName(n);
                  char4 = StringSubstr(name,0,4);
                  if ( char4 == "Diff" )
                    ObjectDelete(name);   
               }
              
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
   
   
   int    i,counted_bars=IndicatorCounted();
   if(counted_bars>0) counted_bars--;
      int limit=Bars-counted_bars;
      
      for(i=limit; i>=0; i--)
         {
            if ( InvertFirstSymbol)
               buffer1[i] = 100-iStochastic(FirstSymbol,0,K,D,S,0,0,0,i);
            else
               buffer1[i] = iStochastic(FirstSymbol,0,K,D,S,0,0,0,i);   
            if ( InvertSecondSymbol )
               buffer2[i] = 100-iStochastic(SecondSymbol,0,K,D,S,0,0,0,i);
            else
               buffer2[i] = iStochastic(SecondSymbol,0,K,D,S,0,0,0,i);
               if ( InvertThirdSymbol )
               buffer4[i] = 100-iStochastic(ThirdSymbol,0,K,D,S,0,0,0,i);
            else
               buffer4[i] = iStochastic(ThirdSymbol,0,K,D,S,0,0,0,i);
               
            buffer3[i] = MathAbs((buffer1[i]+buffer2[i]+buffer4[i])/3);
                                                
         } 
         
      
      
      
      
//----

//----
   return(0);
  }
//+------------------------------------------------------------------+