//+------------------------------------------------------------------+
//|                                                  Zscore Diff.mq4 |
//|                                         Ronaldo Araújo de Farias |
//|                                                                  |
//+------------------------------------------------------------------+
#property copyright "Ronaldo Araújo de Farias"
#property link      ""

//--- input parameters
extern int       Period_=20;
extern string    Second_Pair="GBPUSD";
extern bool      Invert=false;

bool Nivel_Alto_Ativo[5]={false,false,false,false,false};
double Nivel_Alto_Valor[5]={1.7, 2.1, 2.5, 2.9, 3.4};

bool Nivel_Baixo_Ativo[5]={false,false,false,false,false};
double Nivel_Baixo_Valor[5]={-1.7, -2.1, -2.5, -2.9, -3.4};

int i;


//--Signal------------------------------------------------------------------------------
double iDiff(){
   //Alert("Teste");
   double s,desv_1,desv_2,MA_1,MA_2,c2;
   int j;


   MA_1 = iMA(Symbol(),0,Period_,0,MODE_SMA,MODE_CLOSE,0);
   MA_2 = iMA(Second_Pair,0,Period_,0,MODE_SMA,MODE_CLOSE,0); 
   
   
   
   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],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);
      if (Invert) c2=1/c2;
      
      s+=MathPow(MA_2-c2,2);      
   }
   desv_2 = MathSqrt(s/(Period_-1)); 
   

   //----------------
   
   
   c2=iClose(Second_Pair,0,0);
   if (Invert) c2=1/c2;
   
   double Zscore1=(Close[0]-MA_1)/desv_1;     //Pair1
   double Zscore2=(c2-MA_2)/desv_2;           //Pair2
   
   return(Zscore1-Zscore2);  //Diff
}
//-----------------------------------------------------------------------------





//---Orders---------------------------------------------------------------------

double Lots(){
   return(0.01);
}


int Buy_And_Sell(){    //Buy first pair and Sell second pair

   OrderSend(Symbol(),OP_BUY,Lots(),Ask,5,0,0,"xxxx",1313,0,Green);   
   OrderSend(Second_Pair,OP_SELL,Lots(),MarketInfo(Second_Pair,MODE_BID),3,0,0,"xxxx",1313,0,Green);
   
   return(0);
}

int Sell_And_Buy(){   //Sell first pair and Buy second pair
   OrderSend(Symbol(),OP_SELL,Lots(),Bid,5,0,0,"xxxx",1313,0,Green); 
   OrderSend(Second_Pair,OP_BUY,Lots(),MarketInfo(Second_Pair,MODE_ASK),3,0,0,"xxxx",1313,0,Green);   

   
   return(0);
}

int Close_All_Orders(){
    
    // while there are open orders...
    while (OrdersTotal() > 0) {
        // iterate the orders
        for (int i=OrdersTotal()-1;i>=0;i--) {
            // select the order
            OrderSelect(i,SELECT_BY_POS);
            // close or kill depending on order type
            Print("Closing order ", OrderTicket());
            switch(OrderType()) {
                case OP_BUY:  OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol() ,MODE_BID),10); break;
                case OP_SELL: OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol() ,MODE_ASK),10); break;
                default:      OrderDelete(OrderTicket());
            }    
        }
    }
   
   //Disarm
   for(i=0;i<5;i++){
      Nivel_Baixo_Ativo[i]=false;
      Nivel_Alto_Ativo[i]=false;
   }
   return(0);
}

//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start(){
   double Diff = iDiff();
   
  
   for(i=0;i<5;i++){
      if(Nivel_Alto_Ativo[i]==false){
   
         if(Diff>Nivel_Alto_Valor[i]){
            Nivel_Alto_Ativo[i]=true;
            Sell_And_Buy();
         }
      }
      else{
         if(Diff<=0) Close_All_Orders(); 
      }
   
      if(Nivel_Baixo_Ativo[i]==false){
   
         if(Diff<Nivel_Baixo_Valor[i]){
            Nivel_Baixo_Ativo[i]=true;
            Buy_And_Sell();
         }
      }
      else{
         if(Diff>=0) Close_All_Orders(); 
      }
   }
   
   
   return(0);
  }
//+------------------------------------------------------------------+