//+------------------------------------------------------------------+
//|                                          Diff ZScore 2 pairs.mq4 |
//|                                         Ronaldo Araújo de Farias |
//|                                                                  |
//+------------------------------------------------------------------+
#property copyright "Ronaldo Araújo de Farias"
#property link      ""

#property indicator_separate_window
#property indicator_buffers 3

#property indicator_color1 clrRed
#property indicator_style1 STYLE_DOT

#property indicator_color2 clrDodgerBlue
#property indicator_style2 STYLE_DOT

#property indicator_color3 clrMagenta
#property indicator_width3 2

#property indicator_level1  2.5
#property indicator_level2 -2.5
#property indicator_level3  3.1
#property indicator_level4 -3.1
#property indicator_level5  3.5
#property indicator_level6 -3.5
#property indicator_level7  4.1
#property indicator_level8 -4.1
#property indicator_level9  4.5
#property indicator_level10 -4.5


//--- input parameters
extern int       Period_=200;
extern string    Second_Pair="GBPUSD";
extern bool      Invert=false;
extern double    Alert_Above=2;
extern double    Alert_Below=-2;


//--- buffers
double ExtMapBuffer1[];
double ExtMapBuffer2[];
double ExtMapBuffer3[];


bool Alerted=false;




//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- indicators
   SetIndexStyle(0,DRAW_LINE);
   SetIndexBuffer(0,ExtMapBuffer1); SetIndexLabel(0,"Pair1");
   
   SetIndexStyle(1,DRAW_LINE);
   SetIndexBuffer(1,ExtMapBuffer2); SetIndexLabel(1,"Pair2");
   
   SetIndexStyle(2,DRAW_LINE);
   SetIndexBuffer(2,ExtMapBuffer3); SetIndexLabel(2,"Diff");
//----

   //SetLevelStyle(STYLE_DOT,0,Green);
   //SetLevelValue(0,2);   
   //SetLevelValue(1,-2);
   //SetLevelValue(2,0);
   
   IndicatorShortName(WindowExpertName()+" "+Symbol()+"-"+Second_Pair+" ");
   //IndicatorShortName("ZScore "+Symbol()+"-"+Second_Pair+" ");

   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start(){
   int  i,j,counted_bars=IndicatorCounted();
   double s,desv_1,desv_2,MA_1,MA_2,c2;
   

   for(i=0;i<Bars-Period_;i++){
      MA_1 = iMA(Symbol(),0,Period_,0,MODE_SMA,PRICE_CLOSE,i);
      MA_2 = iMA(Second_Pair,0,Period_,0,MODE_SMA,PRICE_CLOSE,i); 
      if (Invert) MA_2=1/MA_2;
      
      //---Calculates the sample standard deviation Pair 1 -------
      s=0;
      for(j=0;j<Period_;j++){
         s+=MathPow(MA_1-Close[j+i],2);      
      }
      desv_1 = MathSqrt(s/(Period_-1));  
      //----------------
      
      
      //---Calculates the sample standard deviation Pair 2 -------
      s=0;
      for(j=0;j<Period_;j++){
         //Close Pair 2
         c2=iClose(Second_Pair,0,j+i);
         if (Invert) c2=1/c2;
         
         s+=MathPow(MA_2-c2,2);      
      }
      desv_2 = MathSqrt(s/(Period_-1)); 
      //----------------
      
      
      c2=iClose(Second_Pair,0,i);
      if (Invert) c2=1/c2;
      
      ExtMapBuffer1[i]=(Close[i]-MA_1)/desv_1;     //Pair1
      ExtMapBuffer2[i]=(c2-MA_2)/desv_2;           //Pair2
      ExtMapBuffer3[i]=ExtMapBuffer1[i]-ExtMapBuffer2[i];  //Diff
      
   }
   
  
  if(Alerted==false){
      if(ExtMapBuffer3[0]>Alert_Above) {
         Alert("Diff ZScore > "+Alert_Above);
         //To Disarm
         Alerted=true;
      }
      if(ExtMapBuffer3[0]<Alert_Below) {
         Alert("Diff ZScore < "+Alert_Below);
         //To Disarm
         Alerted=true;
      }
   }
   else{
      //To Arm
      if((ExtMapBuffer3[0]<=Alert_Above) &&  (ExtMapBuffer3[0]>=Alert_Below)) Alerted=false;
   }
   
   
   return(0);
}
//+------------------------------------------------------------------+