//+-------------------------------------------------------------------------------------------+
//|                                                                             MACDMULTY.mq4 |
//|                                                             Copyright © 2008, FOREXflash. |
//|                                                                 http://www.metaquotes.net |
//+-------------------------------------------------------------------------------------------+
#property copyright "Copyright © 2008, FOREXflash Software Corp."
#property link      "http://www.metaquotes.net"

//+-------------------------------------------------------------------------------------------+

#property  indicator_separate_window
#property indicator_buffers     2
#property indicator_color1      Green
#property indicator_color2      Red
#property indicator_width1      2
#property indicator_width2      2
#property indicator_level1      0.0
#property indicator_levelcolor  Black
//---- input parameters
extern int    FastEMA   = 12;
extern int    SlowEMA   = 26;
extern int    SignalSMA = 9;
extern int    NumPairs = 12;
extern int    TF       = 0;
extern string Pairs1   = "GBPUSD";
extern string Pairs2   = "GBPJPY";
extern string Pairs3   = "GBPCHF";
extern string Pairs4   = "EURUSD";
extern string Pairs5   = "EURJPY";
extern string Pairs6   = "EURCHF";
extern string Pairs7   = "AUDUSD";
extern string Pairs8   = "AUDJPY";
extern string Pairs9   = "CHFJPY";
extern string Pairs10  = "NZDUSD";
extern string Pairs11  = "NZDJPY";
extern string Pairs12  = "USDJPY";
extern string Pairs13  = "USDCHF";
extern string Pairs14  = "EURGBP";
extern int    Mode     = 0;
extern int    Limit    = 1440;


double buf0[], buf1[], buf2[], buf3[], buf4[];
double buftmp[];
string Pairs[];
int bar[][2];
double mPoint[];
bool Recalculate=true;

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init() {
   IndicatorBuffers(2);
   SetIndexStyle(0,DRAW_LINE,0,2);
   SetIndexStyle(1,DRAW_LINE,0,1);
   SetIndexBuffer(0,buf0);
   SetIndexBuffer(1,buf1);
   ArrayResize(Pairs,NumPairs);
   ArrayResize(bar,NumPairs);
   ArrayResize(mPoint,NumPairs);
   ArrayInitialize(mPoint,1);
   if (NumPairs > 0) Pairs[0] = Pairs1;
   if (NumPairs > 1) Pairs[1] = Pairs2;
   if (NumPairs > 2) Pairs[2] = Pairs3;
   if (NumPairs > 3) Pairs[3] = Pairs4;
   if (NumPairs > 4) Pairs[4] = Pairs5;
   if (NumPairs > 5) Pairs[5] = Pairs6;
   if (NumPairs > 6) Pairs[6] = Pairs7;
   if (NumPairs > 7) Pairs[7] = Pairs8;
   if (NumPairs > 8) Pairs[8] = Pairs9;
   if (NumPairs > 9) Pairs[9] = Pairs10;
   if (NumPairs > 10) Pairs[10] = Pairs11;
   if (NumPairs > 11) Pairs[11] = Pairs12;
   if (NumPairs > 12) Pairs[12] = Pairs13;
   if (NumPairs > 13) Pairs[13] = Pairs14;
   GetLastError();
   return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit() {
   return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start() {
   int limit,err;
   CalculatePoint();
   int counted_bars = IndicatorCounted();
   if(counted_bars>0) counted_bars--;
   err=GetLastError();
   if (err==4066) {
      counted_bars=0;
      Recalculate=true;
   }
   else {
      if (Recalculate) {
         counted_bars=0;
         Recalculate=false;
      }
   }
   limit = Bars - counted_bars-1;
   int i, j;
   double kk0, kk1;
   if (limit>Limit && Limit>0) limit=Limit;
   for(i = limit; i > -1; i--) {
      kk0 = 0.0;
      kk1 = 0.0;
      int timebar=iTime(NULL,TF,i);
      
      for(j = 0; j < NumPairs; j++) {
         int shift=iBarShift(Pairs[j],TF,timebar);
         if (iTime(NULL,TF,i)<iTime(Pairs[j],TF,shift)) shift++;
         kk0 = kk0 + iMACD(Pairs[j], TF, FastEMA, SlowEMA, SignalSMA, 0, MODE_MAIN, shift)*mPoint[j];
         kk1 = kk1 + iMACD(Pairs[j], TF, FastEMA, SlowEMA, SignalSMA, 0, MODE_SIGNAL, shift)*mPoint[j];
      }
      buf0[i] = kk0;
      buf1[i] = kk1;
   }
   return(0);
}

void CalculatePoint(){
   double _Point, _Tick;
   for (int i=0;i<NumPairs;i++) {

      if (MarketInfo(Pairs[i], MODE_DIGITS) == 2) {
         mPoint[i]=0.01;
      } else {
         mPoint[i]=1.00;
      }

      _Point=1.0;
      _Tick=1.0;
      if (Mode>0) _Point=MarketInfo(Pairs[i],MODE_POINT);
      if (Mode>1) _Tick=MarketInfo(Pairs[i],MODE_TICKVALUE);
      if (_Point>0 && _Tick>0) mPoint[i]=_Tick/_Point;
   }
}