//+------------------------------------------------------------------+
//|                                                      Pearson.mq4 |
//|                                                           mladen |
//|                                               mladenfx@gmail.com |
//+------------------------------------------------------------------+
#property copyright "mladen"
#property link      "mladenfx@gmail.com"

#property indicator_separate_window
#property indicator_buffers  8
#property indicator_minimum -1
#property indicator_maximum  1
#property indicator_level1   0
#property indicator_levelcolor DarkSlateGray

//
//
//
//
//

extern string _       = "parameters";
extern int    Length  = 20;
extern int    Price   = PRICE_CLOSE;
extern string Symbols = "EURJPY;EURUSD;EURCHF;GBPJPY;GBPUSD;USDJPY;USDCHF";
extern color  color1  = Red;
extern color  color2  = DarkOrange;
extern color  color3  = Orange;
extern color  color4  = Gold;
extern color  color5  = Lime;
extern color  color6  = Green;
extern color  color7  = DodgerBlue;
extern color  color8  = Blue;
extern string UniqueID = "Pearson 1";

//
//
//
//
//

double buffer1[];
double buffer2[];
double buffer3[];
double buffer4[];
double buffer5[];
double buffer6[];
double buffer7[];
double buffer8[];
string aPairs[];
double pBuffer[][9];

int    totalSymbols;
bool   gettingBars = false;
string IndicatorFileName;
string addition = "";
string shortName;

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
//
//
//
//
//

int init()
{
   SetIndexBuffer(0,buffer1); SetIndexStyle(0,DRAW_LINE,EMPTY,EMPTY,color1);
   if (_=="gettingBars")
   {
      gettingBars = true;
      return(0);
   }

   //
   //
   //
   //
   //

      Symbols = StringTrimLeft(StringTrimRight(Symbols));
      if (StringSubstr(Symbols,StringLen(Symbols),1) != ";")
                       Symbols = StringConcatenate(Symbols,";");
     
      //
      //
      //
      //
      //

         int  s      = 0;
         int  i      = StringFind(Symbols,";",s);
         string current;
            while (i > 0)
            {
               current = StringSubstr(Symbols,s,i-s);
               if (iClose(current+addition,0,0) > 0) {
                     ArrayResize(aPairs,ArraySize(aPairs)+1);
                                 aPairs[ArraySize(aPairs)-1] = current; }
                                 s = i + 1;
                                     i = StringFind(Symbols,";",s);
            }
      totalSymbols      = MathMin(ArraySize(aPairs),8);
      IndicatorFileName = WindowExpertName();

   //
   //
   //
   //
   //
      
   SetIndexBuffer(1,buffer2); SetIndexStyle(1,DRAW_LINE,EMPTY,EMPTY,color2);
   SetIndexBuffer(2,buffer3); SetIndexStyle(2,DRAW_LINE,EMPTY,EMPTY,color3);
   SetIndexBuffer(3,buffer4); SetIndexStyle(3,DRAW_LINE,EMPTY,EMPTY,color4);
   SetIndexBuffer(4,buffer5); SetIndexStyle(4,DRAW_LINE,EMPTY,EMPTY,color5);
   SetIndexBuffer(5,buffer6); SetIndexStyle(5,DRAW_LINE,EMPTY,EMPTY,color6);
   SetIndexBuffer(6,buffer7); SetIndexStyle(6,DRAW_LINE,EMPTY,EMPTY,color7);
   SetIndexBuffer(7,buffer8); SetIndexStyle(7,DRAW_LINE,EMPTY,EMPTY,color8);
      shortName = UniqueID;
      IndicatorShortName(shortName);
   return(0);
}
int deinit()
{
   for (int i=0;i<8;i++) ObjectDelete(shortName+i);
   return(0);
}

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
//
//
//
//
//

int start()
{
   int counted_bars=IndicatorCounted();
   int i,r,limit;

   if(counted_bars<0) return(-1);
   if(counted_bars>0) counted_bars--;
         limit = Bars-counted_bars;

   //
   //
   //
   //
   //

   if (gettingBars) { buffer1[0] = limit; return(0); }
   if (Symbols==Symbol()) return(0);
   if (ArrayRange(pBuffer,0) != Bars) ArrayResize(pBuffer,Bars);

      //
      //
      //
      //
      //
         
      int window = WindowFind(shortName);
      for (i=0; i<totalSymbols;i++)
      {
         limit = MathMax(limit,MathMin(Bars-1,iCustom(aPairs[i]+addition,0,IndicatorFileName,"gettingBars",0,0)));
         if (ObjectFind(shortName+i) == -1)
            {
               ObjectCreate(shortName+i,OBJ_LABEL,window,0,0);
                  ObjectSet(shortName+i,OBJPROP_XDISTANCE,10);
                  ObjectSet(shortName+i,OBJPROP_YDISTANCE,10+i*14);
                  ObjectSet(shortName+i,OBJPROP_CORNER,1);
                  
                  color theColor;
                  switch(i)
                  {
                     case 0: theColor = color1; break;
                     case 1: theColor = color2; break;
                     case 2: theColor = color3; break;
                     case 3: theColor = color4; break;
                     case 4: theColor = color5; break;
                     case 5: theColor = color6; break;
                     case 6: theColor = color7; break;
                     case 7: theColor = color8; break;
                  }
                  ObjectSetText(shortName+i,aPairs[i],10,"Courier new bold",theColor);
            }
      }            

   //
   //
   //
   //
   //
            
   for(i=limit, r=Bars-limit-1; i>=0; i--,r++)
   {
         pBuffer[r][0] = iMA(NULL,0,1,0,MODE_SMA,Price,i);
         
         //
         //
         //
         //
         //
         
         for (int p=0; p<totalSymbols;p++)
         {  
            string forSymbol = aPairs[p]+addition;
            if (forSymbol == Symbol()) continue;
            
            double sx  = 0;
            double sy  = 0;
            double sxy = 0;
            double sx2 = 0;
            double sy2 = 0;
            
            //
            //
            //
            //
            //
            
            pBuffer[r][p+1] = iMA(forSymbol,0,1,0,MODE_SMA,Price,i);
            for (int k=0; k<Length; k++)
            {
               double pricea = pBuffer[r-k][0];
               double priceb = pBuffer[r-k][p+1];
                  sx += pricea; sx2 += pricea*pricea;
                  sy += priceb; sy2 += priceb*priceb;
                                sxy += pricea*priceb;
            }
    
            double result  = 0;
            double divisor = MathSqrt(((sx2-(sx*sx)/Length)*(sy2-(sy*sy)/Length)));
               if (divisor != 0)
                        result = (sxy - (sx*sy)/Length)/divisor;
  
            //
            //
            //
            //
            //
  
            switch(p)
            {
               case 0: buffer1[i] = result; break;
               case 1: buffer2[i] = result; break;
               case 2: buffer3[i] = result; break;
               case 3: buffer4[i] = result; break;
               case 4: buffer5[i] = result; break;
               case 5: buffer6[i] = result; break;
               case 6: buffer7[i] = result; break;
               case 7: buffer8[i] = result; break;
            }
         }                           
   }
   return(0);
}