//+------------------------------------------------------------------+
//| MODIFIED_VERSION_Correlation02                PairDifference.mq4 |
//| MODIFIED BY AVERY T. HORTON, JR. AKA THERUMPLEDONE@GMAIL.COM     |
//|                                                                  |
//+------------------------------------------------------------------+
#property copyright "Created by RoBiK"
#property link      ""

#property indicator_separate_window
#property indicator_buffers 8
#property indicator_color1 Blue
#property indicator_color2 Red
#property indicator_color3 Yellow
#property indicator_color4 Crimson
#property indicator_color5 White
#property indicator_color6 Khaki
#property indicator_color7 Gray
#property indicator_color8 Olive
//---- input parameters


  extern string note1 = "Display the price in what corner?";
  extern string note2 = "Upper left=0; Upper right=1";
  extern string note3 = "Lower left=2; Lower right=3";
  extern int    WhatCorner=0;

extern int       NeutralPeriod = 48;
extern int       MaxPeriod = 100;
extern int       MAPeriod = 12;
extern int       MAMethod = MODE_EMA;
extern int       MAPrice = PRICE_CLOSE;
extern string    Pair1 = "EURUSD";
extern bool      Inverted1 = false;
extern string    Pair2="GBPUSD";
extern bool      Inverted2 = false;
extern string    Pair3="USDJPY";
extern bool      Inverted3 = false;
extern string    Pair4 = "USDCAD";
extern bool      Inverted4 = false;
extern string    Pair5 = "USDCHF";
extern bool      Inverted5 = false;
extern string    Pair6 = "AUDUSD";
extern bool      Inverted6 = false;
extern string    Pair7;
extern bool      Inverted7 = false;
extern string    Pair8;
extern bool      Inverted8 = false;
//---- buffers
double b1[];
double b2[];
double b3[];
double b4[];
double b5[];
double b6[];
double b7[];
double b8[];



int      window;  
int      corner;
int      totalLabels;

color  ColorLabels  = Gray;

string   labelNames;
string   shortName;

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- indicators

   corner     = 0;
   shortName  = MakeUniqueName("Corr ","");
   labelNames = shortName;
   IndicatorShortName(shortName); 

   if (StringLen(Pair1) > 0)
   {
     SetIndexStyle(0,DRAW_LINE);
     SetIndexBuffer(0,b1);
     SetIndexDrawBegin(0, Bars - MaxPeriod);
     SetIndexLabel(0, Pair1);
   }
   else SetIndexStyle(0,DRAW_NONE);
   if (StringLen(Pair2) > 0)
   {
     SetIndexStyle(1,DRAW_LINE);
     SetIndexBuffer(1,b2);
     SetIndexDrawBegin(1, Bars - MaxPeriod);
     SetIndexLabel(1, Pair2);
   }
   else SetIndexStyle(1,DRAW_NONE);
   if (StringLen(Pair3) > 0)
   {
     SetIndexStyle(2,DRAW_LINE);
     SetIndexBuffer(2,b3);
     SetIndexDrawBegin(2, Bars - MaxPeriod);
     SetIndexLabel(2, Pair3);
   }
   else SetIndexStyle(2,DRAW_NONE);
   if (StringLen(Pair4) > 0)
   {
     SetIndexStyle(3,DRAW_LINE);
     SetIndexBuffer(3,b4);
     SetIndexDrawBegin(3, Bars - MaxPeriod);
     SetIndexLabel(3, Pair4);
   }
   else SetIndexStyle(3,DRAW_NONE);
   if (StringLen(Pair5) > 0)
   {
     SetIndexStyle(4,DRAW_LINE);
     SetIndexBuffer(4,b5);
     SetIndexDrawBegin(4, Bars - MaxPeriod);
     SetIndexLabel(4, Pair5);
   }
   else SetIndexStyle(4,DRAW_NONE);
   if (StringLen(Pair6) > 0)
   {
     SetIndexStyle(5,DRAW_LINE);
     SetIndexBuffer(5,b6);
     SetIndexDrawBegin(5, Bars - MaxPeriod);
     SetIndexLabel(5, Pair6);
   }
   else SetIndexStyle(5,DRAW_NONE);
   if (StringLen(Pair7) > 0)
   {
     SetIndexStyle(6,DRAW_LINE);
     SetIndexBuffer(6,b7);
     SetIndexDrawBegin(6, Bars - MaxPeriod);
     SetIndexLabel(6, Pair7);
   }
   else SetIndexStyle(6,DRAW_NONE);
   if (StringLen(Pair8) > 0)
   {
     SetIndexStyle(7,DRAW_LINE);
     SetIndexBuffer(7,b8);
     SetIndexDrawBegin(7, Bars - MaxPeriod);
     SetIndexLabel(7, Pair8);
   }
   else SetIndexStyle(7,DRAW_NONE);
//----


   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   ObjectsDeleteAll(window ); 
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
   window      = WindowFind(shortName);  
   ObjectsDeleteAll(window );    

/*  
   if (Bars < MaxPeriod) return(0);
//----
   for (int i = 0; i < MaxPeriod; i++)
*/  
   
   int limit;
   int counted_bars=IndicatorCounted();
//---- check for possible errors
   if(counted_bars<0) return(-1);
//---- last counted bar will be recounted
   if(counted_bars>0) counted_bars--;
   limit=Bars-counted_bars;
   
   if (limit > MaxPeriod) {limit= MaxPeriod ;}  // TRO  
   
//---- macd counted in the 1-st additional buffer
   for(int i=0; i<limit; i++)   
   {
     double c = Coeficient(NULL, false, i);
     if (StringLen(Pair1) > 0) b1[i] = Coeficient(Pair1, Inverted1, i) - c;
     if (StringLen(Pair2) > 0) b2[i] = Coeficient(Pair2, Inverted2, i) - c;
     if (StringLen(Pair3) > 0) b3[i] = Coeficient(Pair3, Inverted3, i) - c;
     if (StringLen(Pair4) > 0) b4[i] = Coeficient(Pair4, Inverted4, i) - c;
     if (StringLen(Pair5) > 0) b5[i] = Coeficient(Pair5, Inverted5, i) - c;
     if (StringLen(Pair6) > 0) b6[i] = Coeficient(Pair6, Inverted6, i) - c;
     if (StringLen(Pair7) > 0) b7[i] = Coeficient(Pair7, Inverted7, i) - c;
     if (StringLen(Pair8) > 0) b8[i] = Coeficient(Pair8, Inverted8, i) - c;
   }


   DoShowLegend() ;

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

double Coeficient(string pair, bool inverted, int shift)
{
   double c1, c2;
   c1 = iMA(pair, 0, MAPeriod, shift, MAMethod, MAPrice, 0);
   c2 = iMA(pair, 0, MAPeriod, NeutralPeriod, MAMethod, MAPrice, 0);
   if (c1 == c2) return(0);
   if (inverted)
   {
     c1 = 1 / c1;
     c2 = 1 / c2;
   }
   return (100 * (c1 - c2)/ c2);
}



//+------------------------------------------------------------------+



void DoShowLegend()
{
   corner = 1;
   setObject(Pair1, Pair1 ,30,20,indicator_color1); //setObject(next(),"l",11,21,indicator_color1 ,"Wingdings");
   setObject(Pair2, Pair2 ,30,33,indicator_color2); //setObject(next(),"l",11,34,indicator_color2 ,"Wingdings");
   setObject(Pair3, Pair3 ,30,46,indicator_color3); //setObject(next(),"l",11,47,indicator_color3 ,"Wingdings");
   setObject(Pair4, Pair4 ,30,59,indicator_color4); //setObject(next(),"l",11,60,indicator_color4 ,"Wingdings");
   setObject(Pair5, Pair5 ,30,72,indicator_color5); //setObject(next(),"l",11,73,indicator_color5 ,"Wingdings");
   setObject(Pair6, Pair6 ,30,85,indicator_color6); //setObject(next(),"l",11,86,indicator_color6 ,"Wingdings");
   setObject(Pair7, Pair7 ,30,98,indicator_color7); //setObject(next(),"l",11,99,indicator_color7 ,"Wingdings");   
   setObject(Pair8, Pair8 ,30,101,indicator_color8); //setObject(next(),"l",11,99,indicator_color7 ,"Wingdings");   
   
}



//+------------------------------------------------------------------+

string next() { totalLabels++; return(totalLabels); }  
//+------------------------------------------------------------------+
void setObject(string name,string text,int x,int y,color theColor, string font = "Arial Bold",int size=10,int angle=0)
{
   string labelName = StringConcatenate(labelNames,name);

      
      if (ObjectFind(labelName) == -1)
          {
             ObjectCreate(labelName,OBJ_LABEL,window,0,0);
             ObjectSet(labelName,OBJPROP_CORNER,corner);
             if (angle != 0)
                  ObjectSet(labelName,OBJPROP_ANGLE,angle);
          }               
       ObjectSet(labelName,OBJPROP_XDISTANCE,x);
       ObjectSet(labelName,OBJPROP_YDISTANCE,y);
       ObjectSetText(labelName,text,size,font,theColor); 
     
     }

//+------------------------------------------------------------------+

string MakeUniqueName(string first, string rest)
{
   string result = first+(MathRand()%1001)+rest;

   while (WindowFind(result)> 0)
          result = first+(MathRand()%1001)+rest;
   return(result);
}

//+------------------------------------------------------------------+