//------------------------------------------------------------------
#property copyright "www.forex-tsd.com" 
#property link      "www.forex-tsd.com" 
//------------------------------------------------------------------
#property indicator_separate_window
#property indicator_buffers 2
#property indicator_color1  PaleVioletRed
#property indicator_color2  DarkGray
#property indicator_width1  2
#property indicator_style2  STYLE_DOT

//
//
//
//
//

extern string Symbols     = "EURUSD,1;USDCHF,-1;GBPUSD,1;EURJPY,1;AUDUSD,1;NZDJPY,1;GBPJPY,1;NZDUSD,1;AUDJPY,1;CHFJPY,1;EURCHF,1;GBPCHF,1;USDJPY,-1;CADJPY,1;EURGBP,1";
extern string Prefix      = "";
extern string Suffix      = "";
extern int    LookBack    = 10;
extern int    Price       = PRICE_CLOSE;
extern int    Smooth      = 5;
extern int    SmoothMode  = MODE_EMA;
extern int    Average     = 10;
extern int    AverageMode = MODE_SMA;
extern bool   UseExactChangedBars = true;

//
//
//
//
//

double ADLine[];
double ADAverage[];
string aSymbol[];
double aMulti[];
int    aSize;
double mainMultiplier=1;
string indicatorFileName;
bool   returnBars;

//------------------------------------------------------------------
//
//------------------------------------------------------------------
//
//
//
//
//

int init()
{
   //
   //
   //
   //
   //
   
   indicatorFileName = WindowExpertName();
   returnBars        = (Symbols=="returnBars"); if (returnBars) return(0);
   
   //
   //
   //
   //
   //
   
   Symbols = stringUpperCase(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);
                        int k = StringFind(current,",",0);
                        
                        string currSym  =             StringSubstr(current,0,k);
                        double currMull = StrToDouble(StringSubstr(current,k+1,0));
                                  ArrayResize(aSymbol,ArraySize(aSymbol)+1);
                                  ArrayResize(aMulti ,ArraySize(aMulti )+1);
                                              aSymbol[ArraySize(aSymbol)-1] = Prefix+currSym+Suffix;
                                              aMulti[ArraySize(aMulti)-1]   = currMull;
                                    s = i + 1;
                                        i = StringFind(Symbols,";",s);
                        if (currSym==Symbol()) mainMultiplier = currMull;                                        
                    }
                    aSize = ArraySize(aSymbol);

   //
   //
   //
   //
   //
   
   SetIndexBuffer(0,ADLine);
   SetIndexBuffer(1,ADAverage);
      Smooth = MathMax(Smooth,1);
   return(0);
}
int deinit() { return(0); }

//------------------------------------------------------------------
//
//------------------------------------------------------------------
//
//
//
//
//

int start()
{
   int counted_bars=IndicatorCounted();
      if(counted_bars < 0) return(-1);
      if(counted_bars>0) counted_bars--;
         int limit = MathMin(Bars-counted_bars,Bars-1);
         if (returnBars) { ADLine[0]=limit; return(0); }
         if (UseExactChangedBars) for(int k = 0; k<aSize; k++) checkLimit(aSymbol[k],limit);


   //
   //
   //
   //
   //
    
   for(int i = limit; i >= 0; i--)
   {
       ADLine[i]=ADLine[i+1];
       for(k = 0; k<aSize; k++)
       {
         int    y    = iBarShift(aSymbol[k],0,Time[i]);
         double imac = iMA(aSymbol[k],0,Smooth,0,SmoothMode,Price,y);
         double imap = iMA(aSymbol[k],0,Smooth,0,SmoothMode,Price,y+1);
            if (imac == 0 || imap==0) 
            {
               ADLine[i]=ADLine[i+1]; break; 
            }
            if (imac > imap) ADLine[i] += aMulti[k]*mainMultiplier;
            if (imac < imap) ADLine[i] -= aMulti[k]*mainMultiplier;
       }
   }       
   for(i = limit; i >= 0; i--) ADAverage[i] = iMAOnArray(ADLine,0,Average,0,AverageMode,i);
   return(0);
}


//------------------------------------------------------------------
//
//------------------------------------------------------------------
//
//
//
//
//

void checkLimit(string symbol, int& limit) 
{
   string checkSymbol = symbol;
      if (checkSymbol!=Symbol() && iClose(checkSymbol,0,0)!=0)
         limit = MathMin(Bars-1,MathMax(limit,iCustom(checkSymbol,0,indicatorFileName,"returnBars",0,0)));
}

//------------------------------------------------------------------
//
//------------------------------------------------------------------
//
//
//
//
//

string stringUpperCase(string str)
{
   string   s = str;

   for (int length=StringLen(str)-1; length>=0; length--)
   {
      int tchar = StringGetChar(s, length);
         if((tchar > 96 && tchar < 123) || (tchar > 223 && tchar < 256))
                     s = StringSetChar(s, length, tchar - 32);
         else if(tchar > -33 && tchar < 0)
                     s = StringSetChar(s, length, tchar + 224);
   }
   return(s);
}