//+------------------------------------------------------------------+
//|                                               MTFcci 2 pairs.mq4 |
//|                      Copyright © 2011, MetaQuotes Software Corp. |
//|                                        http://www.metaquotes.net |
//| Modified by user666, 18-03-2012, www.esbmonetary.com
//+------------------------------------------------------------------+
#property copyright "Copyright © 2011, MetaQuotes Software Corp., 2012 ESB Monetary SA"
#property link      "http://www.metaquotes.net    www.esbmonetary.com"

#property indicator_separate_window
#property indicator_level1 20
#property indicator_level2 80
#property indicator_level3 50
#property indicator_level4 100
#property indicator_level5 0
#property indicator_buffers 3

extern string  note_MTF = "TF=0, M1,M5,M15,M30,H1,H4,D1,W1,MN1";
extern string IndicatorTimeFrame="";
extern int CCIPeriod = 350; // this parameter is the best in accordance with my experience, better than 100
//extern int D = 1;
//extern int S = 1;


extern string  note_4 = "Base (or Chart) Symbol";
extern string  note_2 =" if XXXXXX - will be obtaned";
extern string  note_3 =" from Chart";
extern string  FirstSymbol = "XXXXXX"; // if XXXXXX or "" - Symbol will be read from Chart
extern string  note_1 = "Hedged Symbol (must be entered)";
extern string  SecondSymbol = "USDCHF";
extern bool    AlertOn = true;
extern bool    EmailOn = false;
extern int     AlertAtorAbove = 80;
extern int     AlertAtorBelow = 50;
extern int     FontSize = 7;
extern color   FirstColor = DodgerBlue;
extern color   SecondColor = Red;
extern color   DiffColor = Yellow;
extern color   DeltaColor = White;
extern int     FirstX_Coordinate = 0;
extern int     FirstY_Coordinate = 15;
extern int     SecondX_Coordinate = 340;
extern int     SecondY_Coordinate = 15;
extern int     DiffX_Coordinate = 110;
extern int     DiffY_Coordinate = 15;
extern int     DektaPipShift_X   = 925;
extern bool    InvertFirstSymbol = false;
extern bool    InvertSecondSymbol = false;
extern bool    CorrelationLabelOn = true;
extern bool    Show_DeltaPip = true;
extern int     CorrelationTimeFrame = 1440;
extern string  Note = "Zero CorrelationPeriods counts all bars"; 
extern int     CorrelationPeriods   = 50;
extern bool Show_in_PV = false;
extern double Lots_1=1;
extern double Lots_2=1;
extern bool ShowTradeDirection=true;


double p1,p2;

double buffer1[], buffer2[],buffer3[];
string m="";
string InfoSpace="          ";
string TradeDirectionFirstSymbol;
string TradeDirectionSecondSymbol;
string IndName = "";
int    win;
string Text1,Text2,Text3,T="NotValid";
datetime alertoncea=0, alertoncee=0,alertonceb=0, alertoncec=0,once=0;
bool     emailonce = true,alertonce = true; 
double a_above=1, e_above=1,DeltaPip1, DeltaPip2, DeltaPip3, DeltaPip4;
string s[31],first,second;
int TFPeriodValue=0;// Time Frame Period Value

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
      
            if( FirstSymbol=="audcad" )FirstSymbol="AUDCAD" ;
            if( FirstSymbol=="audchf" )FirstSymbol="AUDCHF" ;
            if( FirstSymbol=="audjpy" )FirstSymbol="AUDJPY" ;
            if( FirstSymbol=="audnzd" )FirstSymbol="AUDNZD" ;
            if( FirstSymbol=="audusd" )FirstSymbol="AUDUSD" ;
            if( FirstSymbol=="cadjpy" )FirstSymbol="CADJPY" ;           
            if( FirstSymbol=="chfjpy" )FirstSymbol="CHFJPY" ;
            if( FirstSymbol=="euraud" )FirstSymbol="EURAUD" ;
            if( FirstSymbol=="eurcad" )FirstSymbol="EURCAD" ;
            if( FirstSymbol=="eurchf" )FirstSymbol="EURCHF" ;
            if( FirstSymbol=="eurgbp" )FirstSymbol="EURGBP" ;
            if( FirstSymbol=="eurjpy" )FirstSymbol="EURJPY" ;
            if( FirstSymbol=="eurnzd" )FirstSymbol="EURNZD" ;
            if( FirstSymbol=="eurusd" )FirstSymbol="EURUSD" ;
            if( FirstSymbol=="gbpaud" )FirstSymbol="GBPAUD" ;
            if( FirstSymbol=="gbpcad" )FirstSymbol="GBPCAD" ;
            if( FirstSymbol=="gbpchf" )FirstSymbol="GBPCHF" ;
            if( FirstSymbol=="gbpjpy" )FirstSymbol="GBPJPY" ;
            if( FirstSymbol=="gbpnzd" )FirstSymbol="GBPNZD" ;
            if( FirstSymbol=="gbpusd" )FirstSymbol="GBPUSD" ;
            if( FirstSymbol=="nzdchf" )FirstSymbol="NZDCHF" ;
            if( FirstSymbol=="nzdjpy" )FirstSymbol="NZDJPY" ;
            if( FirstSymbol=="nzdusd" )FirstSymbol="NZDUSD" ;
            if( FirstSymbol=="usdcad" )FirstSymbol="USDCAD" ;
            if( FirstSymbol=="usdchf" )FirstSymbol="USDCHF" ;
            if( FirstSymbol=="usdjpy" )FirstSymbol="USDJPY" ;
            if( FirstSymbol=="eurdkk" )FirstSymbol="EURDKK" ;
            if( FirstSymbol=="usdczk" )FirstSymbol="USDCZK" ;
            if( FirstSymbol=="usddkk" )FirstSymbol="USDDKK" ;
            if( FirstSymbol=="usdmxn" )FirstSymbol="USDMXN" ;
            if( FirstSymbol=="usdpln" )FirstSymbol="USDPLN" ;
            
            if( SecondSymbol=="audcad" )SecondSymbol="AUDCAD" ;
            if( SecondSymbol=="audchf" )SecondSymbol="AUDCHF" ;
            if( SecondSymbol=="audjpy" )SecondSymbol="AUDJPY" ;
            if( SecondSymbol=="audnzd" )SecondSymbol="AUDNZD" ;
            if( SecondSymbol=="audusd" )SecondSymbol="AUDUSD" ;
            if( SecondSymbol=="cadjpy" )SecondSymbol="CADJPY" ;           
            if( SecondSymbol=="chfjpy" )SecondSymbol="CHFJPY" ;
            if( SecondSymbol=="euraud" )SecondSymbol="EURAUD" ;
            if( SecondSymbol=="eurcad" )SecondSymbol="EURCAD" ;
            if( SecondSymbol=="eurchf" )SecondSymbol="EURCHF" ;
            if( SecondSymbol=="eurgbp" )SecondSymbol="EURGBP" ;
            if( SecondSymbol=="eurjpy" )SecondSymbol="EURJPY" ;
            if( SecondSymbol=="eurnzd" )SecondSymbol="EURNZD" ;
            if( SecondSymbol=="eurusd" )SecondSymbol="EURUSD" ;
            if( SecondSymbol=="gbpaud" )SecondSymbol="GBPAUD" ;
            if( SecondSymbol=="gbpcad" )SecondSymbol="GBPCAD" ;
            if( SecondSymbol=="gbpchf" )SecondSymbol="GBPCHF" ;
            if( SecondSymbol=="gbpjpy" )SecondSymbol="GBPJPY" ;
            if( SecondSymbol=="gbpnzd" )SecondSymbol="GBPNZD" ;
            if( SecondSymbol=="gbpusd" )SecondSymbol="GBPUSD" ;
            if( SecondSymbol=="nzdchf" )SecondSymbol="NZDCHF" ;
            if( SecondSymbol=="nzdjpy" )SecondSymbol="NZDJPY" ;
            if( SecondSymbol=="nzdusd" )SecondSymbol="NZDUSD" ;
            if( SecondSymbol=="usdcad" )SecondSymbol="USDCAD" ;
            if( SecondSymbol=="usdchf" )SecondSymbol="USDCHF" ;
            if( SecondSymbol=="usdjpy" )SecondSymbol="USDJPY" ;
            if( SecondSymbol=="eurdkk" )SecondSymbol="EURDKK" ;
            if( SecondSymbol=="usdczk" )SecondSymbol="USDCZK" ;
            if( SecondSymbol=="usddkk" )SecondSymbol="USDDKK" ;
            if( SecondSymbol=="usdmxn" )SecondSymbol="USDMXN" ;
            if( SecondSymbol=="usdpln" )SecondSymbol="USDPLN" ;
            
            
      
      m = StringSubstr(Symbol(),6,StringLen(Symbol())-6);


      SecondSymbol = SecondSymbol+m;
      
      if (FirstSymbol == "" || FirstSymbol == "XXXXXX")
         FirstSymbol = Symbol();
      else
         FirstSymbol = FirstSymbol+m;
       
      //Table of Variables for Symbols
      s[0]="AUDCAD"+m;  s[1]="AUDCHF"+m;  s[2]="AUDJPY"+m;  s[3]="AUDNZD"+m;  s[4]="AUDUSD"+m;
      s[5]="CADJPY"+m;  s[6]="CHFJPY"+m;  s[7]="EURAUD"+m;  s[8]="EURCAD"+m;  s[9]="EURCHF"+m;
      s[10]="EURGBP"+m; s[11]="EURJPY"+m; s[12]="EURNZD"+m; s[13]="EURUSD"+m; s[14]="GBPAUD"+m; 
      s[15]="GBPCAD"+m; s[16]="GBPCHF"+m; s[17]="GBPJPY"+m; s[18]="GBPNZD"+m; s[19]="GBPUSD"+m;
      s[20]="NZDCHF"+m; s[21]="NZDJPY"+m; s[22]="NZDUSD"+m; s[23]="USDCAD"+m; s[24]="USDCHF"+m;
      s[25]="USDJPY"+m; s[26]="EURDKK"+m; s[27]="USDCZK"+m; s[28]="USDDKK"+m; s[29]="USDMXN"+m;
      s[30]="USDPLN"+m;  
     
      for ( int nn=0;nn<31;nn++ )
         {
            if ( s[nn] == FirstSymbol )
               first = DoubleToStr(nn,0);
            if ( s[nn] == SecondSymbol )
               second = DoubleToStr(nn,0);   
         }
      
      //GlobalVariableSet("AA"+first+" "+second,0);
      //GlobalVariableSet("EA"+first+" "+second,0);
      a_above = GlobalVariableGet("AA"+first+" "+second);
      e_above = GlobalVariableGet("AA"+first+" "+second);
      
      
      
      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);   
               }
               
// MTF Code begin
               
       if ( IndicatorTimeFrame=="M1" ) TFPeriodValue=1;        
       if ( IndicatorTimeFrame=="M5" ) TFPeriodValue=5; 
       if ( IndicatorTimeFrame=="M15" ) TFPeriodValue=15; 
       if ( IndicatorTimeFrame=="M30" ) TFPeriodValue=30; 
       if ( IndicatorTimeFrame=="H1" ) TFPeriodValue=60; 
       if ( IndicatorTimeFrame=="H4" ) TFPeriodValue=240; 
       if ( IndicatorTimeFrame=="D1" ) TFPeriodValue=1440; 
       if ( IndicatorTimeFrame=="W1" ) TFPeriodValue=10080; 
       if ( IndicatorTimeFrame=="MN" ) TFPeriodValue=43200; 
       if ( IndicatorTimeFrame=="" || IndicatorTimeFrame=="0") TFPeriodValue=0; 
       
// MTF Code end               
               
       if ( CorrelationTimeFrame == 1 ) T = "M1";
       if ( CorrelationTimeFrame == 5 ) T = "M5";
       if ( CorrelationTimeFrame == 15 ) T = "M15";
       if ( CorrelationTimeFrame == 30 ) T = "M30";
       if ( CorrelationTimeFrame == 60 ) T = "H1";
       if ( CorrelationTimeFrame == 240 ) T = "H4";
       if ( CorrelationTimeFrame == 1440 ) T = "D1";
       if ( CorrelationTimeFrame == 10080 ) T = "W1";
       if ( CorrelationTimeFrame == 43200 ) T = "MN";        
       IndName = "MTFcci 1.00  ("+TFPeriodValue+","+CCIPeriod+","+")   "+FirstSymbol+" | "+SecondSymbol;
       IndicatorShortName(IndName);
   /*       
for(int iii=1; iii>=10; iii++)      
{ 
       win = WindowFind("Stochastic 1.7("+K+","+D+","+S+")"+FirstSymbol+"|"+SecondSymbol); 
//win = WindowFind("Stochastic 1.7");       
if (win!=-1 && win!=0) break;       
}   
  */                  
//win=WindowFind(IndName);


//---- 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(2,buffer3);
      SetIndexStyle(2,DRAW_LINE,STYLE_DOT,NULL,DiffColor);
      SetIndexLabel(2,"Difference");    

IndicatorDigits(2);

//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   Comment("");
   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(WindowFind(IndName)+name);   
               }
              
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
   win = WindowFind(IndName);
   if (win == -1) win = 0;
   double p1 , p2;
   string corcomment;
   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-iCCI(FirstSymbol,TFPeriodValue,CCIPeriod,PRICE_TYPICAL,i);
            else
               buffer1[i] = iCCI(FirstSymbol,TFPeriodValue,CCIPeriod,PRICE_TYPICAL,i);   
            if ( InvertSecondSymbol )
               buffer2[i] = 100-iCCI(SecondSymbol,TFPeriodValue,CCIPeriod,PRICE_TYPICAL,i);
            else
               buffer2[i] = iCCI(SecondSymbol,TFPeriodValue,CCIPeriod,PRICE_TYPICAL,i);
               
            buffer3[i] = MathAbs(buffer1[i]-buffer2[i]);
            if((buffer3[i+1]< 20) && (buffer3[i] > 20))
                {DeltaPip1 = iClose(FirstSymbol,0,i+1);
                 DeltaPip3 = iClose(SecondSymbol,0,i+1);}
                                                
                                              
                                                
         } 
         
      if ( buffer3[1] < AlertAtorAbove )
         {
            emailonce = true;
            alertonce = true;
         } 
           
      if ( AlertOn && alertoncea != Time[0] )
         {
            if ( buffer3[1] >= AlertAtorAbove && alertonce)
               {
                  Alert(FirstSymbol+"% - "+SecondSymbol+"% is > "+AlertAtorAbove+"  "+TimeToStr(Time[1],TIME_DATE|TIME_MINUTES));
                  alertoncea = Time[0];
                  alertonce = false;
                  GlobalVariableSet("AA"+first+" "+second,1);
               }
               
         }
      
             
      if ( EmailOn && alertoncee != Time[0] )
         {
            if ( buffer3[1] >= AlertAtorAbove && emailonce )
               {
                  SendMail("Stochastic Pair",FirstSymbol+"% - "+SecondSymbol+"% is > "+AlertAtorAbove+"  "+TimeToStr(Time[1],TIME_DATE|TIME_MINUTES));
                  alertoncee = Time[0];
                  emailonce = false;
                  GlobalVariableSet("EA"+first+" "+second,1);
               }
               
         }

      a_above = GlobalVariableGet("AA"+first+" "+second);
      if ( AlertOn && alertonceb != Time[0] && a_above == 1 )
         {
            if ( buffer3[1] <= AlertAtorBelow )
               {
                  Alert(FirstSymbol+"% - "+SecondSymbol+"% is < "+AlertAtorBelow+"  "+TimeToStr(Time[1],TIME_DATE|TIME_MINUTES));
                  alertonceb = Time[0];
                  GlobalVariableSet("AA"+first+" "+second,0);
               }
               
         }
      
      e_above = GlobalVariableGet("EA"+first+" "+second);       
      if ( EmailOn && alertoncec != Time[0] && e_above == 1 )
         {
            if ( buffer3[1] <= AlertAtorAbove )
               {
                  SendMail("Stochastic Pair",FirstSymbol+"% - "+SecondSymbol+"% is < "+AlertAtorBelow+"  "+TimeToStr(Time[1],TIME_DATE|TIME_MINUTES));
                  alertoncec = Time[0];
                  GlobalVariableSet("EA"+first+" "+second,0);
               }
               }
// Show trade direction with Chart               
    if (ShowTradeDirection==true)
    {
/*    
 if ( buffer3[1] >= AlertAtorAbove)
               {               
if(buffer1[0] >buffer2[0])
{
TradeDirectionFirstSymbol="-= BUY =-";
TradeDirectionFirstSymbol="-= SELL =-";
}
else
{
TradeDirectionFirstSymbol="";
TradeDirectionFirstSymbol="";
}
if(buffer1[0] < buffer2[0])
{
TradeDirectionFirstSymbol="-= SELL =-";
TradeDirectionFirstSymbol="-= BUY =-";
}
else
{
TradeDirectionFirstSymbol="";
TradeDirectionFirstSymbol="";
}
 
}  
*/
             
if(buffer1[1] <  buffer2[1])
{
TradeDirectionFirstSymbol="[ BUY ]";
TradeDirectionSecondSymbol="[ SELL ]";
}

if(buffer1[1] >  buffer2[1])
{
TradeDirectionFirstSymbol="[ SELL ]";
TradeDirectionSecondSymbol="[ BUY ]";
}

  
    }           

               
//         }             
    if ( CorrelationLabelOn )
      {
      if ( once != iTime(Symbol(),CorrelationTimeFrame,0) )
         {               
            corcomment = correlation2pair();
            once = iTime(Symbol(),CorrelationTimeFrame,0);
            ObjectDelete(win+"Diff4"+FirstSymbol+SecondSymbol);  
            ObjectCreate(win+"Diff4"+FirstSymbol+SecondSymbol,OBJ_LABEL,win,0,0);
            ObjectSet(win+"Diff4"+FirstSymbol+SecondSymbol,OBJPROP_XDISTANCE, FirstX_Coordinate);
            ObjectSet(win+"Diff4"+FirstSymbol+SecondSymbol,OBJPROP_YDISTANCE, FirstY_Coordinate+20); 
//ObjectSet("Diff4"+FirstSymbol+SecondSymbol,OBJPROP_YDISTANCE, FirstX_Coordinate+400); 

            ObjectSetText(win+"Diff4"+FirstSymbol+SecondSymbol,corcomment, FontSize,"Arial",DiffColor);

         }
      }    
       
       if (Show_in_PV   ) {p1=MarketInfo(FirstSymbol,MODE_TICKVALUE)*Lots_1;p2=MarketInfo(SecondSymbol,MODE_TICKVALUE)*Lots_2;} else {p1=1;p2=1;}
       
       
       
      DeltaPip2= (MathAbs(DeltaPip1-iClose(FirstSymbol,PERIOD_M1,0))*(1/MarketInfo(FirstSymbol,MODE_POINT)))*p1;
      DeltaPip4= (MathAbs(DeltaPip3-iClose(SecondSymbol,PERIOD_M1,0))*(1/MarketInfo(SecondSymbol,MODE_POINT)))*p2;
      


     if (Show_DeltaPip)
     {
     
//if(ShowColorInfo==true)
//{
     
      string DeltaString =            FirstSymbol + ": " + DoubleToStr(DeltaPip2,3);
      string DeltaString1 =           SecondSymbol+ ": " + DoubleToStr(DeltaPip4,3);
      string DeltaString2 =           "Combined"  + ": " + DoubleToStr(DeltaPip2+DeltaPip4,3);
      
      
            ObjectDelete(win+"DiffDeltaPip"+FirstSymbol+SecondSymbol);  
            ObjectCreate(win+"DiffDeltaPip"+FirstSymbol+SecondSymbol,OBJ_LABEL,win,0,0);
            ObjectSet(win+"DiffDeltaPip"+FirstSymbol+SecondSymbol,OBJPROP_XDISTANCE, FirstX_Coordinate+DektaPipShift_X);
            ObjectSet(win+"DDiffeltaPip"+FirstSymbol+SecondSymbol,OBJPROP_YDISTANCE, FirstY_Coordinate+60); 
            ObjectSetText(win+"DiffDeltaPip"+FirstSymbol+SecondSymbol,DeltaString, FontSize-6,"Arial",DeltaColor);
            
            ObjectDelete(win+"DiffDeltaPip1"+FirstSymbol+SecondSymbol);  
            ObjectCreate(win+"DiffDeltaPip1"+FirstSymbol+SecondSymbol,OBJ_LABEL,win,0,0);
            ObjectSet(win+"DiffDeltaPip1"+FirstSymbol+SecondSymbol,OBJPROP_XDISTANCE, FirstX_Coordinate+DektaPipShift_X);
            ObjectSet(win+"DiffDeltaPip1"+FirstSymbol+SecondSymbol,OBJPROP_YDISTANCE, FirstY_Coordinate+75); 
            ObjectSetText(win+"DiffDeltaPip1"+FirstSymbol+SecondSymbol,DeltaString1, FontSize-6,"Arial",DeltaColor);
            
            
            ObjectDelete(win+"DiffDeltaPip2"+FirstSymbol+SecondSymbol);  
            ObjectCreate(win+"DiffDeltaPip2"+FirstSymbol+SecondSymbol,OBJ_LABEL,win,0,0);
            ObjectSet(win+"DiffDeltaPip2"+FirstSymbol+SecondSymbol,OBJPROP_XDISTANCE, FirstX_Coordinate+DektaPipShift_X);
            ObjectSet(win+"DiffDeltaPip2"+FirstSymbol+SecondSymbol,OBJPROP_YDISTANCE, FirstY_Coordinate+90); 
            ObjectSetText(win+"DiffDeltaPip2"+FirstSymbol+SecondSymbol,DeltaString2, FontSize-6,"Arial",DeltaColor);


      }
      
      Text1 = StringSubstr(FirstSymbol,0,6)+" "+TradeDirectionFirstSymbol;
      Text2 = StringSubstr(SecondSymbol,0,6)+" "+TradeDirectionSecondSymbol;

      ObjectDelete(win+"Diff1"+FirstSymbol+SecondSymbol);
      ObjectDelete(win+"Diff2"+FirstSymbol+SecondSymbol);

      ObjectCreate(win+"Diff1"+FirstSymbol+SecondSymbol,OBJ_LABEL,win,0,0);
      ObjectSet(win+"Diff1"+FirstSymbol+SecondSymbol,OBJPROP_XDISTANCE, FirstX_Coordinate);
      ObjectSet(win+"Diff1"+FirstSymbol+SecondSymbol,OBJPROP_YDISTANCE, FirstY_Coordinate); 
      ObjectSetText(win+"Diff1"+FirstSymbol+SecondSymbol,Text1, FontSize,"Arial",FirstColor);
      
      ObjectCreate(win+"Diff2"+FirstSymbol+SecondSymbol,OBJ_LABEL,win,0,0);
      ObjectSet(win+"Diff2"+FirstSymbol+SecondSymbol,OBJPROP_XDISTANCE, SecondX_Coordinate);
      ObjectSet(win+"Diff2"+FirstSymbol+SecondSymbol,OBJPROP_YDISTANCE, SecondY_Coordinate); 
      ObjectSetText(win+"Diff2"+FirstSymbol+SecondSymbol,Text2, FontSize,"Arial",SecondColor);      
      
      Text3 = "Diff "+DoubleToStr(buffer3[0], 0)+" Coincides with ";
                
      ObjectDelete(win+"Diff3"+FirstSymbol+SecondSymbol);      
      ObjectCreate(win+"Diff3"+FirstSymbol+SecondSymbol,OBJ_LABEL,win,0,0);
      ObjectSet(win+"Diff3"+FirstSymbol+SecondSymbol,OBJPROP_XDISTANCE, DiffX_Coordinate);
      ObjectSet(win+"Diff3"+FirstSymbol+SecondSymbol,OBJPROP_YDISTANCE, DiffY_Coordinate); 
      ObjectSetText(win+"Diff3"+FirstSymbol+SecondSymbol,Text3, FontSize,"Arial",DiffColor);
      
 
//}      
      
//----

//----
   return(0);
  }
//+------------------------------------------------------------------+

string   correlation2pair()
{
	string   Symbol1     = FirstSymbol, Symbol2 = SecondSymbol, cor; 
	int      TimeFrame   = CorrelationTimeFrame;
	datetime closeTime1	= iTime(Symbol1,TimeFrame,0),
				closeTime2	= iTime(Symbol2,TimeFrame,0),
				closeTime	= MathMin(closeTime1,closeTime2);
	int		shift1		= iBarShift(Symbol1,TimeFrame,closeTime),
				shift2		= iBarShift(Symbol2,TimeFrame,closeTime);
	double	close1[],
				close2[];

	ArrayCopySeries(close1,MODE_CLOSE,Symbol1,TimeFrame);
	ArrayCopySeries(close2,MODE_CLOSE,Symbol2,TimeFrame);

	int bars = MathMin(ArraySize(close1)-shift1,ArraySize(close2)-shift2);
	if ( CorrelationPeriods > 0 )
	     bars = CorrelationPeriods;
   double C = Correlation(close1,close2,shift1,shift2,bars);
   
   
	cor = "Correlation "+T+" @ "+bars+" Periods = "+DoubleToStr(100*C,1)+"%"+" "+InfoSpace;
	return(cor);
}

//+------------------------------------------------------------------+
//| Correlation Coefficient R														|
//+------------------------------------------------------------------+
double Correlation(double x[], double y[], int x_shift = 0, int y_shift = 0, int count = -1)
{
	Print(x_shift,"  ",y_shift,"  ",count);
	int n = MathMin(ArraySize(x)-x_shift,ArraySize(y)-y_shift);
	if(n>count && count>0)
		n=count;
	if(n<2)
		return(-2);

	double	sum_sq_x,
				sum_sq_y,
				sum_coproduct,
				mean_x = x[x_shift],
				mean_y = y[y_shift];

	for(int i = 0; i < n; i++)
	{
		double	sweep = i / (i+1.0),
					delta_x = x[i+x_shift] - mean_x,
					delta_y = y[i+y_shift] - mean_y;

		sum_sq_x += delta_x*delta_x * sweep;
		sum_sq_y += delta_y*delta_y * sweep;
    	sum_coproduct += delta_x*delta_y * sweep;
    	mean_x += delta_x / (i+1.0);
    	mean_y += delta_y / (i+1.0);
	}

	double	pop_sd_x = MathSqrt(sum_sq_x/n),
				pop_sd_y = MathSqrt(sum_sq_y/n),
				cov_x_y = sum_coproduct / n;

	if(pop_sd_x*pop_sd_y != 0.0)
		return(cov_x_y / (pop_sd_x*pop_sd_y));

	return(-3);
}