//+------------------------------------------------------------------+ 
//|  _TRO_SAK_MultiPair                                              | 
//|                                                                  | 
//|   Copyright © 2007, Avery T. Horton, Jr. aka TheRumpledOne       |
//|                                                                  |
//|   PO BOX 43575, TUCSON, AZ 85733                                 |
//|                                                                  |
//|   GIFT AND DONATIONS ACCEPTED                                    | 
//|                                                                  |
//|   therumpldone@gmail.com                                         |  
//+------------------------------------------------------------------+ 

//+------------------------------------------------------------------+
//|                                      Easy iCustom and Alerts.mq4 |
//|                                                       Codersguru |
//|                                         http://www.forex-tsd.com |
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
//|                                                  Tickwatcher.mq4 |
//|                                                           mladen |
//+------------------------------------------------------------------+

#property indicator_separate_window
#property indicator_buffers 0
#property indicator_minimum 0
#property indicator_maximum 1


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

extern string _            = "pairs and timeframes";
extern string pairs        = "GBPUSD;EURUSD;USDCHF;USDJPY";
extern string timeFrames   = "M1;M5;M15;M30;H1;H4;D1;W1;MN";
extern string __           = "levels";

extern bool   myAlert      = false;
extern bool   ShowLegend   = true;
extern bool   ShowCount    = true;
extern color  ColorPrice   = PaleTurquoise;
extern color  ColorPrice00 = Yellow;
extern color  ColorLabels  = Gray;
//--------iCustom------------------
//
 
extern string Indicator_Name_1="$CDL";
//extern int TimeFrame_1=0;

extern bool ShowValue1     = true;
extern int Line_1=0;
extern double Parameter1_1 = -100;
extern double Parameter2_1 = -100;
extern double Parameter3_1 = -100;
extern double Parameter4_1 = -100;
extern double Parameter5_1 = -100;

extern double Ind1_Level4    = 0.5;
extern double Ind1_Level3    = 0.5;
extern double Ind1_Level2    = -0.50;
extern double Ind1_Level1    = -0.70;
extern string ___          = "colors";
extern color  Ind1_ColorUp      = Lime;
extern color  Ind1_ColorNeutral = Yellow;
extern color  Ind1_ColorDown    = OrangeRed;


extern string ____         = "other";

//--------------------------------
 
extern string Indicator_Name_2="$MID";
extern int TimeFrame_2=0;
extern int Line_2=0;
extern double Parameter1_2 = -100;
extern double Parameter2_2 = -100;
extern double Parameter3_2 = -100;
extern double Parameter4_2 = -100;
extern double Parameter5_2 = -100;
 
extern double Ind2_Level4    = 0.00;
extern double Ind2_Level3    = 0.00;
extern double Ind2_Level2    = -0.50;
extern double Ind2_Level1    = -0.50;
 
extern color  Ind2_ColorUp      = Lime;
extern color  Ind2_ColorNeutral = Yellow;
extern color  Ind2_ColorDown    = OrangeRed;

 
//--------------------------------
 
extern string Indicator_Name_3="$BIAS";
extern int TimeFrame_3=0;
extern int Line_3=0;
extern double Parameter1_3 = -100;
extern double Parameter2_3 = -100;
extern double Parameter3_3 = -100;
extern double Parameter4_3 = -100;
extern double Parameter5_3 = -100;

extern double Ind3_Level4    = 1.0;
extern double Ind3_Level3    = 0.00;
extern double Ind3_Level2    = -1.00;
extern double Ind3_Level1    = -2.00;
 
extern color  Ind3_ColorUp      = Aqua;
extern color  Ind3_ColorNeutral = Gray;
extern color  Ind3_ColorDown    = Magenta;
//---------------------------------
//
//
//
//
//

int      window;  
int      totalPairs;
int      totalTimeFrames;
int      totalLabels;
int      aTimes[];
string   aPairs[];
string   sTimes[];

//
//
//
//
//


int    Count_Up;
int    Count_Eq;
int    Count_Dn;

color    Ind3_Color;
color    Ind2_Color;
color    Ind1_Color;
string   Ind3_arrow;
string   Ind2_arrow;
string   Ind1_arrow;
string   labelNames;
string   shortName;
int      corner;


//+-------------iCustom--------------------------------------------+


string Pair_1="", Pair_2="", Pair_3=""  ;    

int TimeFrame_1=0;
                      
//+------------------------------------------------------------------+------------------------------------------------------------------+

int init()
{
   corner     = 0;
   shortName  = MakeUniqueName("TW ","");
   labelNames = shortName;
      IndicatorShortName(shortName);

   //
   //
   //
   //
   //
   
      pairs = StringUpperCase(StringTrimLeft(StringTrimRight(pairs)));
      if (StringSubstr(pairs,StringLen(pairs),1) != ";")
                       pairs = StringConcatenate(pairs,";");
                       

         //
         //
         //
         //
         //                                   

         bool isMini = IsMini();            
         int  s      = 0;
         int  i      = StringFind(pairs,";",s);
         string current;
            while (i > 0)
            {
               current = StringSubstr(pairs,s,i-s);
               if (isMini) current = StringConcatenate(current,"m");
               if (iClose(current,0,0) > 0) {
                     ArrayResize(aPairs,ArraySize(aPairs)+1);
                                 aPairs[ArraySize(aPairs)-1] = current; }
                                 s = i + 1;
                                     i = StringFind(pairs,";",s);
            }

      //
      //
      //
      //
      //

      timeFrames = StringUpperCase(StringTrimLeft(StringTrimRight(timeFrames)));
      if (StringSubstr(timeFrames,StringLen(timeFrames),1) != ";")
                       timeFrames = StringConcatenate(timeFrames,";");

         //
         //
         //
         //
         //                                   
            
         s = 0;
         i = StringFind(timeFrames,";",s);
         int time;
            while (i > 0)
            {
               current = StringSubstr(timeFrames,s,i-s);
               time    = stringToTimeFrame(current);
               if (time > 0) {
                     ArrayResize(sTimes,ArraySize(sTimes)+1);
                     ArrayResize(aTimes,ArraySize(aTimes)+1);
                                 sTimes[ArraySize(sTimes)-1] = TimeFrameToString(time); 
                                 aTimes[ArraySize(aTimes)-1] = time; }
                                 s = i + 1;
                                     i = StringFind(timeFrames,";",s);
            }

      //
      //
      //
      //
      //

      totalTimeFrames = ArraySize(aTimes);
      totalPairs      = ArraySize(aPairs);
      totalLabels     = 0;
   return(0);
}

int deinit()
{
   while (totalLabels>0) { ObjectDelete(StringConcatenate(labelNames,totalLabels)); totalLabels--;}
   return(0);
}


//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+

int start()
{

bool sFirst = true ;

if ( sFirst ) {
sFirst = false ;
//ObjectDelete(tObjName04);

ObjectsDeleteAll( window, 21);
ObjectsDeleteAll( window, 22);
ObjectsDeleteAll( window, 23);
}

   int m,n;
   int i,k;

   //
   //
   //
   //
   //



   window      = WindowFind(shortName);
   totalLabels = 0;
         for (i=0,m=80; i < totalPairs;      i++, m+=140){
            Count_Up = 0;
            Count_Eq = 0;
            Count_Dn = 0;
            for (k=0,n=20; k < totalTimeFrames; k++, n+=13){
               showPair(aPairs[i],aTimes[k],sTimes[k],m,n); }
               if (ShowCount)  DoShowCount(m,n); }
              
               
   if (ShowLegend) DoShowLegend();

      
   return(0);
}


//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
//
//
//
//
//
void DoShowLegend()
{
   corner = 1;
   setObject(next(),"up"       ,30,20,ColorLabels); setObject(next(),"l",11,21,Ind1_ColorUp     ,"Wingdings");
   setObject(next(),"weak up"  ,30,33,ColorLabels); setObject(next(),"¡",10,34,Ind1_ColorUp     ,"Wingdings");
   setObject(next(),"neutral"  ,30,46,ColorLabels); setObject(next(),"l",10,47,Ind1_ColorNeutral,"Wingdings");
   setObject(next(),"weak down",30,59,ColorLabels); setObject(next(),"¡",10,60,Ind1_ColorDown   ,"Wingdings");
   setObject(next(),"down"     ,30,72,ColorLabels); setObject(next(),"l",11,74,Ind1_ColorDown   ,"Wingdings");
   corner = 0;      
}

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
//
void DoShowCount( int xdistance, int ydistance )
{
 
int yInc = 13 ;
   
string Count_Str = "Up="+DoubleToStr(Count_Up,0)+" Eq="+DoubleToStr(Count_Eq,0)+" Dn="+DoubleToStr(Count_Dn,0) ;


color xColor = Yellow ;
 
if( Count_Up > Count_Dn ) { xColor = LimeGreen ; }
else if( Count_Up < Count_Dn ) { xColor = Crimson ; }

 
setObject(next(),Count_Str,xdistance-16,ydistance+yInc, xColor ,"Arial bold",10);
           
}

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
//
//
//
//
//

void showPair(string pair, int timeFrame, string label, int xdistance, int ydistance)
{
   double price = iClose(pair,0,0);
   
   Pair_1       = pair ;
   TimeFrame_1  = timeFrame ;
   double Ind1_Val   = GetIcustomValue(1,0);
   

   Pair_2       = pair ;
   TimeFrame_2  = timeFrame ;
   double Ind2_Val   = GetIcustomValue(2,0);


   Pair_3       = pair ;
   TimeFrame_3  = timeFrame ;
   double Ind3_Val   = GetIcustomValue(3,0);
   
   
   
   //
   //
   //
   //
   //   

      if (ydistance==20) {
            setObject(next(),pair                                           ,xdistance-1 ,20,ColorLabels,"Arial bold",12,270);            


           setObject(next(), Indicator_Name_1+ " " + Indicator_Name_2+ " " + Indicator_Name_3 ,xdistance+32, 1,ColorPrice ,"Arial bold", 8);           

           if( MathMod(price,1.00) == 0 ) 
              {color pColor = ColorPrice00 ;
              if (myAlert) Alert(Symbol(),"  at  ", Close[0],"  -  ", TimeToStr(CurTime(),TIME_SECONDS));
              }
           else pColor = ColorPrice ;
           

           setObject(next(),DoubleToStr(price,MarketInfo(pair,MODE_DIGITS)),xdistance-16, 1,pColor ,"Arial bold",8);
         }            
         
      //
      //
      //
      //
      //

      setInd1ColorArrow(Ind1_Val);
      setInd2ColorArrow(Ind2_Val );
      setInd3ColorArrow(Ind3_Val);
      
if (ShowValue1) setLabel(label,xdistance   ,ydistance,Ind1_Val,Ind1_Color,Ind1_arrow);
else setLabel(label,xdistance   ,ydistance," ",Ind1_Color,Ind1_arrow);


         setLabel(" "  ,xdistance+79,ydistance," ",Ind2_Color,Ind2_arrow,false);
         setLabel(" "  ,xdistance+91,ydistance," ",Ind3_Color,Ind3_arrow,false);
}



//+------------------------------------------------------------------+
//| Custom functions and procedures                                  |
//+------------------------------------------------------------------+
string next() { totalLabels++; return(totalLabels); }  
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
//
//
//
//
//

void setInd1ColorArrow(double first)
{
 
 
if( first > Ind1_Level4 ) { Count_Up = Count_Up +1 ; Ind1_arrow = "l"; Ind1_Color = Ind1_ColorUp; } else {
if( first > Ind1_Level3 ) { Count_Up = Count_Up +1 ; Ind1_arrow = "¡"; Ind1_Color = Ind1_ColorUp; } else {
if( first > Ind1_Level2 ) { Count_Eq = Count_Eq +1 ; Ind1_arrow = "l"; Ind1_Color = Ind1_ColorNeutral; } else {
if( first > Ind1_Level1 ) { Count_Dn = Count_Dn +1 ; Ind1_arrow = "¡"; Ind1_Color = Ind1_ColorDown; } else {
                          { Count_Dn = Count_Dn +1 ; Ind1_arrow = "l"; Ind1_Color = Ind1_ColorDown; } } } } }
}


void setInd2ColorArrow(double first )
{
if( first > Ind2_Level4 ) { Count_Up = Count_Up +1 ; Ind2_arrow = "l"; Ind2_Color = Ind2_ColorUp; } else {
if( first > Ind2_Level3 ) { Count_Up = Count_Up +1 ; Ind2_arrow = "¡"; Ind2_Color = Ind2_ColorUp; } else {
if( first > Ind2_Level2 ) { Count_Eq = Count_Eq +1 ; Ind2_arrow = "l"; Ind2_Color = Ind2_ColorNeutral; } else {
if( first > Ind2_Level1 ) { Count_Dn = Count_Dn +1 ; Ind2_arrow = "¡"; Ind2_Color = Ind2_ColorDown; } else {
                          { Count_Dn = Count_Dn +1 ; Ind2_arrow = "l"; Ind2_Color = Ind2_ColorDown; } } } } }

}


void setInd3ColorArrow(double first)
{

if( first > Ind3_Level4 ) { Count_Up = Count_Up +1 ; Ind3_arrow = "l"; Ind3_Color = Ind3_ColorUp; } else {
if( first > Ind3_Level3 ) { Count_Up = Count_Up +1 ; Ind3_arrow = "¡"; Ind3_Color = Ind3_ColorUp; } else {
if( first > Ind3_Level2 ) { Count_Eq = Count_Eq +1 ; Ind3_arrow = "l"; Ind3_Color = Ind3_ColorNeutral; } else {
if( first > Ind3_Level1 ) { Count_Dn = Count_Dn +1 ; Ind3_arrow = "¡"; Ind3_Color = Ind3_ColorDown; } else {
                          { Count_Dn = Count_Dn +1 ; Ind3_arrow = "l"; Ind3_Color = Ind3_ColorDown; } } } } }


}  


//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
//
//
//
//
//

void setLabel(string text,int x,int y,string value, color theColor, string arrow, bool isADX=true)
{
     int last = 0;
     
     if (isADX) {last=67;}
     if (isADX) setObject(next(),text                   ,x     ,y,ColorLabels);
     if (isADX) setObject(next(),StringSubstr(value,0,5),x+30  ,y,theColor);
                setObject(next(),arrow                  ,x+last,y,theColor,"Wingdings",9);
}

//
//
//
//
//

void setObject(string name,string text,int x,int y,color theColor, string font = "Arial",int size=10,int angle=0)
{
   string labelName = StringConcatenate(labelNames,name);

   //
   //
   //
   //
   //
      
      if (ObjectFind(labelName) == -1)
          {
             ObjectCreate(labelName,OBJ_LABEL,window,0,0);
             ObjectSet(labelName,OBJPROP_CORNER,corner);
             if (angle != 0)
                  ObjectSet(labelName,OBJPROP_ANGLE,angle);
          }               
       ObjectSet(labelName,OBJPROP_XDISTANCE,x);
       ObjectSet(labelName,OBJPROP_YDISTANCE,y);
       ObjectSetText(labelName,text,size,font,theColor);
}

//+------------------------------------------------------------------+
//+                                                                  +
//+------------------------------------------------------------------+
//
//
//
//
//

int stringToTimeFrame(string tfs)
{
   int tf=0;
       tfs = StringTrimLeft(StringTrimRight(StringUpperCase(tfs)));
         if (tfs=="M1" || tfs=="1")     tf=PERIOD_M1;
         if (tfs=="M5" || tfs=="5")     tf=PERIOD_M5;
         if (tfs=="M15"|| tfs=="15")    tf=PERIOD_M15;
         if (tfs=="M30"|| tfs=="30")    tf=PERIOD_M30;
         if (tfs=="H1" || tfs=="60")    tf=PERIOD_H1;
         if (tfs=="H4" || tfs=="240")   tf=PERIOD_H4;
         if (tfs=="D1" || tfs=="1440")  tf=PERIOD_D1;
         if (tfs=="W1" || tfs=="10080") tf=PERIOD_W1;
         if (tfs=="MN" || tfs=="43200") tf=PERIOD_MN1;
  return(tf);
}
string TimeFrameToString(int tf)
{
   string tfs;
   switch(tf) {
      case PERIOD_M1:  tfs="M1"  ; break;
      case PERIOD_M5:  tfs="M5"  ; break;
      case PERIOD_M15: tfs="M15" ; break;
      case PERIOD_M30: tfs="M30" ; break;
      case PERIOD_H1:  tfs="H1"  ; break;
      case PERIOD_H4:  tfs="H4"  ; break;
      case PERIOD_D1:  tfs="D1"  ; break;
      case PERIOD_W1:  tfs="W1"  ; break;
      case PERIOD_MN1: tfs="MN1";
   }
   return(tfs);
}

//
//
//
//
//

string StringUpperCase(string str)
{
   string   s = str;
   int      lenght = StringLen(str) - 1;
   int      char;
   
   while(lenght >= 0)
      {
         char = StringGetChar(s, lenght);
         
         //
         //
         //
         //
         //
         
         if((char > 96 && char < 123) || (char > 223 && char < 256))
                  s = StringSetChar(s, lenght, char - 32);
          else 
              if(char > -33 && char < 0)
                  s = StringSetChar(s, lenght, char + 224);
         lenght--;
   }
   
   //
   //
   //
   //
   //
   
   return(s);
}
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
//
//
//
//
//

string MakeUniqueName(string first, string rest)
{
   string result = first+(MathRand()%1001)+rest;

   while (WindowFind(result)> 0)
          result = first+(MathRand()%1001)+rest;
   return(result);
}
bool IsMini()
{
   if (StringFind(Symbol(),"m") > -1)
         return(true);
   else  return(false);    
}
  

//+-------------iCustom--------------------------------------------+

  
  double GetIcustomValue(int level ,int shift)
  {
      double value =0;
      
      switch(level)
      {
         case 1 :
         {  
         
         
            if(Parameter1_1==-100 )
               value = iCustom(Pair_1,TimeFrame_1,Indicator_Name_1,Parameter1_1,Line_1,shift);
            if(Parameter1_1!=-100 && Parameter2_1==-100 && Parameter3_1==-100 && Parameter4_1==-100 && Parameter5_1==-100)
               value = iCustom(Pair_1,TimeFrame_1,Indicator_Name_1,Parameter1_1,Line_1,shift);
            if(Parameter1_1!=-100 && Parameter2_1!=-100 && Parameter3_1==-100 && Parameter4_1==-100 && Parameter5_1==-100)
               value = iCustom(Pair_1,TimeFrame_1,Indicator_Name_1,Parameter1_1,Parameter2_1,Line_1,shift);
            if(Parameter1_1!=-100 && Parameter2_1!=-100 && Parameter3_1!=-100 && Parameter4_1==-100 && Parameter5_1==-100)
               value = iCustom(Pair_1,TimeFrame_1,Indicator_Name_1,Parameter1_1,Parameter2_1,Parameter3_1,Line_1,shift);
            if(Parameter1_1!=-100 && Parameter2_1!=-100 && Parameter3_1!=-100 && Parameter4_1!=-100 && Parameter5_1==-100)
               value = iCustom(Pair_1,TimeFrame_1,Indicator_Name_1,Parameter1_1,Parameter2_1,Parameter3_1,Parameter4_1,Line_1,shift);
            if(Parameter1_1!=-100 && Parameter2_1!=-100 && Parameter3_1!=-100 && Parameter4_1!=-100 && Parameter5_1!=-100)
               value = iCustom(Pair_1,TimeFrame_1,Indicator_Name_1,Parameter1_1,Parameter2_1,Parameter3_1,Parameter4_1,Parameter5_1,Line_1,shift);
            break;
         }
      
         case 2 :
         { 
            if(Parameter1_2==-100 )
               value = iCustom(Pair_2,TimeFrame_2,Indicator_Name_2,Parameter1_2,Line_2,shift);          
            if(Parameter1_2!=-100 && Parameter2_2==-100 && Parameter3_2==-100 && Parameter4_2==-100 && Parameter5_2==-100)
               value = iCustom(Pair_2,TimeFrame_2,Indicator_Name_2,Parameter1_2,Line_2,shift);
            if(Parameter1_2!=-100 && Parameter2_2!=-100 && Parameter3_2==-100 && Parameter4_2==-100 && Parameter5_2==-100)
               value = iCustom(Pair_2,TimeFrame_2,Indicator_Name_2,Parameter1_2,Parameter2_2,Line_2,shift);
            if(Parameter1_2!=-100 && Parameter2_2!=-100 && Parameter3_2!=-100 && Parameter4_2==-100 && Parameter5_2==-100)
               value = iCustom(Pair_2,TimeFrame_2,Indicator_Name_2,Parameter1_2,Parameter2_2,Parameter3_2,Line_2,shift);
            if(Parameter1_2!=-100 && Parameter2_2!=-100 && Parameter3_2!=-100 && Parameter4_2!=-100 && Parameter5_2==-100)
               value = iCustom(Pair_2,TimeFrame_2,Indicator_Name_2,Parameter1_2,Parameter2_2,Parameter3_2,Parameter4_2,Line_2,shift);
            if(Parameter1_2!=-100 && Parameter2_2!=-100 && Parameter3_2!=-100 && Parameter4_2!=-100 && Parameter5_2!=-100)
               value = iCustom(Pair_2,TimeFrame_2,Indicator_Name_2,Parameter1_2,Parameter2_2,Parameter3_2,Parameter4_2,Parameter5_2,Line_2,shift);
            break;
         }
      
         case 3 :
         {  
            if(Parameter1_3==-100)
               value = iCustom(Pair_3,TimeFrame_3,Indicator_Name_3,Parameter1_3,Line_3,shift);
            if(Parameter1_3!=-100 && Parameter2_3==-100 && Parameter3_3==-100 && Parameter4_3==-100 && Parameter5_3==-100)
               value = iCustom(Pair_3,TimeFrame_3,Indicator_Name_3,Parameter1_3,Line_3,shift);
            if(Parameter1_3!=-100 && Parameter2_3!=-100 && Parameter3_3==-100 && Parameter4_3==-100 && Parameter5_3==-100)
               value = iCustom(Pair_3,TimeFrame_3,Indicator_Name_3,Parameter1_3,Parameter2_3,Line_3,shift);
            if(Parameter1_3!=-100 && Parameter2_3!=-100 && Parameter3_3!=-100 && Parameter4_3==-100 && Parameter5_3==-100)
               value = iCustom(Pair_3,TimeFrame_3,Indicator_Name_3,Parameter1_3,Parameter2_3,Parameter3_3,Line_3,shift);
            if(Parameter1_3!=-100 && Parameter2_3!=-100 && Parameter3_3!=-100 && Parameter4_3!=-100 && Parameter5_3==-100)
               value = iCustom(Pair_3,TimeFrame_3,Indicator_Name_3,Parameter1_3,Parameter2_3,Parameter3_3,Parameter4_3,Line_3,shift);
            if(Parameter1_3!=-100 && Parameter2_3!=-100 && Parameter3_3!=-100 && Parameter4_3!=-100 && Parameter5_3!=-100)
               value = iCustom(Pair_3,TimeFrame_3,Indicator_Name_3,Parameter1_3,Parameter2_3,Parameter3_3,Parameter4_3,Parameter5_3,Line_3,shift);
            break;
         }
      }
       
      return (value);

  }
  