//+------------------------------------------------------------------+
//|                                           FXCorrelator.mq4       |
//|                                           Copyright © 2011,      |
//|                                                                  |
//+------------------------------------------------------------------+
//
// Original version, including licensing terms can be found at:
// FXcorrelator.com 
// Do not resell or Copy
//
//
#property copyright "FXCorrelator.com"
#property link      ""

#property indicator_separate_window
#property indicator_buffers 8
#property indicator_color1 Blue
#property indicator_color2 Turquoise
#property indicator_color3 Orange
#property indicator_color4 Gray
#property indicator_color5 Lime
#property indicator_color6 Turquoise
#property indicator_color7 Brown
#property indicator_color8 Yellow

#property indicator_level1 0.0

// Indicator Configuration
extern int PerAvr=1,Delta=1;
extern bool ShowEUR=true;
extern bool ShowGBP=false;
extern bool ShowAUD=false;
extern bool ShowCHF=false;
extern bool ShowJPY=false;
extern bool ShowNZD=false;
extern bool ShowCAD=false;
extern bool ShowUSD=false;
extern string SymbolSuffix="";

// index buffers for drawing
double Idx[],Idx1[],Idx2[],Idx3[],Idx4[],Idx5[],Idx6[],Idx7[];

// currency rates for calculation
double EUR,GBP,AUD,CHF,JPY,NZD,CAD,USD,A1,A2,A3,A4,A5,A6,A7;

// Currency names and colors
string Currencies[] = {"EUR","GBP","AUD","CHF","JPY","NZD","CAD","USD"};
int Colors[] = {indicator_color1, indicator_color2, indicator_color3, indicator_color4,
                indicator_color5, indicator_color6, indicator_color7, indicator_color8};

// house keeping
string ShortName;
bool ObjectsCreated = false;

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
{
   // set up indicator buffers
   for ( int i = 0; i < 8; i++ )
   {
      SetIndexStyle(i,DRAW_LINE,STYLE_SOLID,2);
      SetIndexStyle(4,DRAW_LINE,STYLE_SOLID,2);
      SetIndexStyle(7,DRAW_LINE,STYLE_SOLID,2);
      SetIndexLabel(i,Currencies[i]);
      
         SetIndexLabel(0,NULL);
   }

   // silly MQL needs this part to be done manually
   SetIndexBuffer(0,Idx);
   SetIndexBuffer(1,Idx1);
   SetIndexBuffer(2,Idx2);
   SetIndexBuffer(3,Idx3);
   SetIndexBuffer(4,Idx4);
   SetIndexBuffer(5,Idx5);
   SetIndexBuffer(6,Idx6);
   SetIndexBuffer(7,Idx7);
   
   
   return(0);
}

//+------------------------------------------------------------------+
//| Custor indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
{
   // delete all objects we created
  ObjectsDeleteAll(WindowFind(ShortName));
  
  return(0);
}

//+------------------------------------------------------------------+
//| Create a single descriptive object at the given coordinates      |
//+------------------------------------------------------------------+
void CreateObject(string currency, int window, int x, int y, int col)
{
   int ret = ObjectCreate("obj_"+currency,OBJ_LABEL,window,0,0);
   ObjectSetText("obj_"+currency,currency,8);
   ObjectSet("obj_"+currency,OBJPROP_COLOR,col);
   ObjectSet("obj_"+currency,OBJPROP_XDISTANCE,x);
   ObjectSet("obj_"+currency,OBJPROP_YDISTANCE,y);
}

//+-------------------------------------------------------------------+
//| Create all text labels (colored names of the displayed currencies)|
//+-------------------------------------------------------------------+
void CreateObjects()
{
   // start and increment of X coordinates
   int xStart = 4;
   int xIncrement = 25;
   
   // start and increment of Y coordinates
   int yStart = 16;
   int yIncrement = 0;
   
   int window = WindowFind(ShortName);

   // create all labels
   for(int i = 0; i < 8; i++ )
   {
      CreateObject(Currencies[i], window, xStart, yStart, Colors[i]);
      xStart += xIncrement;
      yStart += yIncrement;
   }
}

//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
{  

   // only create objects here, window handle is not valid in init()  
   if ( !ObjectsCreated )
   {
      CreateObjects();
      ObjectsCreated = true;
   }
  
   int counted_bars=IndicatorCounted();
   if(counted_bars<0) return(-1);
   if(counted_bars>0) counted_bars--;
   int limit=Bars-counted_bars;
   for(int i=limit; i>=0; i--)    
   {
      A1=(iMA("EURJPY"+SymbolSuffix,0,PerAvr,0,MODE_CLOSE,PRICE_CLOSE,i))/100; 
      A2=(iMA("EURGBP"+SymbolSuffix,0,PerAvr,0,MODE_CLOSE,PRICE_CLOSE,i)); 
      A3=(iMA("EURCHF"+SymbolSuffix,0,PerAvr,0,MODE_CLOSE,PRICE_CLOSE,i)); 
      A4=(iMA("EURUSD"+SymbolSuffix,0,PerAvr,0,MODE_CLOSE,PRICE_CLOSE,i));
      A5=(iMA("EURAUD"+SymbolSuffix,0,PerAvr,0,MODE_CLOSE,PRICE_CLOSE,i)); 
      A6=(iMA("EURNZD"+SymbolSuffix,0,PerAvr,0,MODE_CLOSE,PRICE_CLOSE,i));
      A7=(iMA("EURCAD"+SymbolSuffix,0,PerAvr,0,MODE_CLOSE,PRICE_CLOSE,i));
            
      EUR=(A1+A2+A3+A4+A5+A6+A7)/7;
                      
      if ( ShowEUR )
         Idx[i] = EUR;
         
      if ( ShowGBP )
         Idx1[i] = GBP;
      
      if ( ShowAUD )
         Idx2[i] = AUD;
         
      if ( ShowCHF )
         Idx3[i] = CHF;
      
      if ( ShowJPY )
         Idx4[i] = JPY;
      
      if ( ShowNZD )
         Idx5[i] = NZD;
      
      if ( ShowCAD )
         Idx6[i] = CAD;
      
      if ( ShowUSD )
         Idx7[i] = USD;
   }

   return(0);
}
//+------------------------------------------------------------------+ 