//+------------------------------------------------------------------+
//|                                  Multipair_Average_Indicator.mq4 |
//|                                                         Zen_Leow |
//|                                                                  |
//+------------------------------------------------------------------+
#property copyright "Zen_Leow"
#property link      ""

#property indicator_separate_window

#property indicator_buffers 1
#property indicator_color1 Blue

#define MAX_PAIRS 3
//---- indicator parameters
extern string Title1 = "Pair names MUST be exactly same as broker display";
extern string Pair1 = "EURUSD";
extern bool Inverse_Pair1 = false;
extern string Pair2 = "GBPUSD";
extern bool Inverse_Pair2 = false;
extern string Pair3 = "USDJPY";
extern bool Inverse_Pair3 = true;
//---- indicator buffers
double Line_Buffer[];

string Pair_Name[MAX_PAIRS];
bool Pair_Inverse[MAX_PAIRS];
double Pair_Price[MAX_PAIRS];

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
{
//---- indicators
   IndicatorDigits(MarketInfo(Symbol(),MODE_DIGITS));
   SetIndexStyle(0,DRAW_LINE);
   SetIndexBuffer(0,Line_Buffer);
//----

   Pair_Name[0] = Pair1;
   Pair_Name[1] = Pair2;
   Pair_Name[2] = Pair3;
   
   Pair_Inverse[0] = Inverse_Pair1;
   Pair_Inverse[1] = Inverse_Pair2;
   Pair_Inverse[2] = Inverse_Pair3;

   return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
{
//----
   
//----
   return(0);
}

void RetreivePairPrice(int index, int shift)
{
   Pair_Price[index] = iClose(Pair_Name[index], 0, shift);
   if (Pair_Inverse[index])
   {
      Pair_Price[index] = Pair_Price[index] / 100;
   }
}

double CalculateAverage()
{
   double averagePrice = 0;
   double totalPrice = Pair_Price[0];
   for (int i=1; i<MAX_PAIRS; i++)
   {
      totalPrice = totalPrice + Pair_Price[i];
   }
   averagePrice = totalPrice / MAX_PAIRS;
   return (averagePrice);
}


//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
{
   int i;                           // Bar index       
   int Counted_bars;                // Number of counted bars
   //--------------------------------------------------------------------   
   Counted_bars=IndicatorCounted(); // Number of counted bars   
   i=Bars-Counted_bars-1;           // Index of the first uncounted   
   if (i == 0)
   {
      i = 2;                        // always recount the last 2 bars again, don't ask me why.
   }
   while(i>=0)                      // Loop for uncounted bars     
   {
      for (int k=0; k<MAX_PAIRS; k++)
      {
         RetreivePairPrice(k, i);
      }
      Line_Buffer[i] = CalculateAverage();
      i--;   
   }
   return(0);
}
//+------------------------------------------------------------------+