//+------------------------------------------------------------------+
//|                                   TRO_Dashboard_SupRes.mq4       |
//|                                        Copyright © 2008, "TRO"   |
//|                                                                  |
//+------------------------------------------------------------------+


#property copyright "Copyright © 2008, Avery T. Horton, Jr. aka TheRumpledOne"
#property link      "therumpledone@gmail.com"

#property indicator_separate_window  

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

extern bool my.alert.sound = false ;

extern string _            = "PAIRS";
extern string pairs        = "GBPUSD;EURUSD;USDCHF;USDJPY;USDCAD;GBPJPY;EURJPY;GBPCHF;AUDUSD;";

//extern string pairs        = "USDJPY;";


extern string timeFrames   = "H1;H4;D1;W1;MN1";
extern color  ColorUp      = Blue;
extern color  ColorNeutral = Yellow ;
extern color  ColorDown    = Red ;
extern color  ColorLoLg    = Aqua;
extern color  ColorHiSh    = Orange ;

extern color  ValueColor   = DodgerBlue;
extern int    ValueSize    = 10;
extern string ValueFont    = "Verdana";
 
extern string myHeading    = "TRO FX DASHBOARD S/R";
extern string myName       = "Support/Restance";
extern string Ind_Name_1   = "Support/Restance";


extern int       max_bars  = 100; 
extern int       iPeriods  = 5 ; 




int      window;  
int      totalPairs;
int      totalTimeFrames;
int      totalLabels;
int      aTimes[];
string   aPairs[];
string   sTimes[];
color    ColorLabels  = White;
color    MA_color;
color    srsi_color;
color    cad_color;
string   MA_arrow;
string   srsi_arrow;
string   cad_arrow;
string   labelNames;
string   shortName;
int      corner;
int       INC_UP ;
int       INC_UC ;
int       INC_DN;
int       MAMode;
int       PRMode;
int       RSIMode;
string    strMAType;
string    strRSIType;
bool   ShowCount    = true;
 


string Pair0    = ""      ;
string Pair1    = ""      ;
string Pair2    = ""      ;
string Pair3    = ""      ;
string Pair4    = ""      ;
string Pair5    = ""      ;
string Pair6    = ""      ;
string Pair7    = ""      ;
string Pair8    = ""      ;
string Pair9    = ""      ;
string Pair10   = ""      ;
string Pair11   = ""      ;
string Pair12   = ""      ;
string Pair13   = ""      ;
string Pair14   = ""      ;
string Pair15   = ""      ;
string Pair16   = ""      ;
string Pair17   = ""      ;
string Pair18   = ""      ;
string Pair19   = ""      ;
string Pair20   = ""      ;

double ShTrigger = 0, LgTrigger = 0;

double cdayClose;
double cdayOpen;
color  ColorPrice ; 
double point  ;
int    xPoint ;

int    h,l ;

string vTrigger ;

string ArrowN  = "ñ"; // NORTH
string ArrowNE = "ö";  // NORTHEAST
string ArrowE  = "ð";  // EAST
string ArrowSE = "ø";  // SOUTHEAST
string ArrowS  = "ò";  // SOUTH



//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+

int init()
{

   corner     = 0;
   shortName  = MakeUniqueName("FXDB ","");
   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);
         int time;
         string current;
        
         string temp;
         
            while (i > 0)
            {
               current = StringSubstr(pairs,s,i-s);
               if (isMini) current = StringConcatenate(current,"m");
              time    = stringToTimeFrame(current);
               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;
      
      
Pair0    = aPairs[0];
Pair1    = aPairs[1];
Pair2    = aPairs[2];
Pair3    = aPairs[3];
Pair4    = aPairs[4];
Pair5    = aPairs[5];
Pair6    = aPairs[6];
Pair7    = aPairs[7];
Pair8    = aPairs[8];
Pair9    = aPairs[9];
Pair10   = aPairs[10];
Pair11   = aPairs[11];
Pair12   = aPairs[12];
Pair13   = aPairs[13];
Pair14   = aPairs[14];
Pair15   = aPairs[15];
Pair16   = aPairs[16];
Pair17   = aPairs[17];
Pair18   = aPairs[18];
Pair19   = aPairs[19];
Pair20   = aPairs[20];   
      
   return(0);
   
}
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
{
   while (totalLabels>0) { ObjectDelete(StringConcatenate(labelNames,totalLabels)); totalLabels--;}
     
   return(0);
}


//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+

int start()
{

ObjectsDeleteAll( window, 21);
ObjectsDeleteAll( window, 22);
ObjectsDeleteAll( window, 23);
int m,n;
int i,k;
   window      = WindowFind(shortName);
   totalLabels = 0;
    for (i=0,m=30; i < totalPairs;      i++, m+=25){   // 25
   INC_UP = 0;
   INC_UC = 0;
   INC_DN = 0; 
   for (k=0,n=170; k < totalTimeFrames; k++, n+=65){   // 46 
   showPair(aPairs[i],aTimes[k],sTimes[k],m,n);}
   if (ShowCount)  DoShowCount(m,n); }             
   if (Pair1 == "") Pair1 = Symbol();
    
     
     
   return(0);
}


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


void DoShowCount( int xdistance, int ydistance )
{

int x_Pair, y_Pair , y_Inc ;

x_Pair = 10;
y_Pair = 40;
y_Inc  = 25;


double PERp,PERC,PER;
int yInc = -125;

  PER=INC_UP; 
  PERC=PER*100;
  PERp=PERC/7;
  string PER_Str =DoubleToStr(PERp,Point);
   
color xColor = Yellow ;
 
//if( PERp > 50) { xColor = LimeGreen ; }
//else if( PERp < 50 ) { xColor = Crimson ; }


 if(ValueSize>12) ValueSize=12;
 
 setObject(next(),myHeading + " © 2008 ",90,5, Gray ,ValueFont,7,0);

//  if(show.SCORE){  
//  setObject(next(),PER_Str,525,xdistance+10, xColor ,"Arial bold",10,0);}
  
  
  setObject(next(),Pair0,x_Pair,40, ValueColor ,ValueFont,ValueSize);
  setObject(next(),Pair1,x_Pair,65, ValueColor ,ValueFont,ValueSize);
  setObject(next(),Pair2,x_Pair,90, ValueColor ,ValueFont,ValueSize);
  setObject(next(),Pair3,x_Pair,115, ValueColor ,ValueFont,ValueSize);
  setObject(next(),Pair4,x_Pair,140, ValueColor ,ValueFont,ValueSize);
  setObject(next(),Pair5,x_Pair,165, ValueColor ,ValueFont,ValueSize);
  setObject(next(),Pair6,x_Pair,190, ValueColor ,ValueFont,ValueSize);
  setObject(next(),Pair7,x_Pair,215, ValueColor ,ValueFont,ValueSize);
  setObject(next(),Pair8,x_Pair,240, ValueColor ,ValueFont,ValueSize);
  setObject(next(),Pair9,x_Pair,265, ValueColor ,ValueFont,ValueSize);
  setObject(next(),Pair10,x_Pair,290, ValueColor ,ValueFont,ValueSize);
  setObject(next(),Pair11,x_Pair,315, ValueColor ,ValueFont,ValueSize);
  setObject(next(),Pair12,x_Pair,340, ValueColor ,ValueFont,ValueSize);
  setObject(next(),Pair13,x_Pair,365, ValueColor ,ValueFont,ValueSize);
  setObject(next(),Pair14,x_Pair,390, ValueColor ,ValueFont,ValueSize);
  setObject(next(),Pair15,x_Pair,415, ValueColor ,ValueFont,ValueSize);
  setObject(next(),Pair16,x_Pair,440, ValueColor ,ValueFont,ValueSize);
  setObject(next(),Pair17,x_Pair,465, ValueColor ,ValueFont,ValueSize);
  setObject(next(),Pair18,x_Pair,490, ValueColor ,ValueFont,ValueSize);
  setObject(next(),Pair19,x_Pair,515, ValueColor ,ValueFont,ValueSize);

string cTitle = myName + "(" + iPeriods+ ")"  ;

int sbeg = 200 ;
int sinc = 65 ;

   setObject(next(),cTitle,340,3, ValueColor ,"Verdana Bold",ValueSize);
   setObject(next(),"PAIR",x_Pair,18, ValueColor ,ValueFont,ValueSize);
   setObject(next(),sTimes[0],sbeg,18, ValueColor ,ValueFont,ValueSize);
   setObject(next(),sTimes[1],sbeg+sinc,18, ValueColor ,ValueFont,ValueSize);
   setObject(next(),sTimes[2],sbeg+sinc*2,18, ValueColor ,ValueFont,ValueSize);
   setObject(next(),sTimes[3],sbeg+sinc*3,18, ValueColor ,ValueFont,ValueSize);
   setObject(next(),sTimes[4],sbeg+sinc*4,18, ValueColor ,ValueFont,ValueSize);
   setObject(next(),sTimes[5],sbeg+sinc*5,18, ValueColor ,ValueFont,ValueSize);
   setObject(next(),sTimes[6],sbeg+sinc*6,18, ValueColor ,ValueFont,ValueSize);

//   setObject(next(),"%",525,18, ValueColor ,ValueFont,ValueSize);
     

x_Pair = x_Pair + 70 ;

setObject(next(),"Price",x_Pair,18, ValueColor ,ValueFont,ValueSize);
   
if( Pair0 != "" ) { DoPrice( Pair0, x_Pair  , y_Pair ) ; }
if( Pair1 != "" ) { y_Pair = y_Pair + y_Inc ; DoPrice( Pair1, x_Pair  , y_Pair ) ; }
if( Pair2 != "" ) { y_Pair = y_Pair + y_Inc ; DoPrice( Pair2, x_Pair  , y_Pair ) ; }
if( Pair3 != "" ) { y_Pair = y_Pair + y_Inc ; DoPrice( Pair3, x_Pair  , y_Pair ) ; }
if( Pair4 != "" ) { y_Pair = y_Pair + y_Inc ; DoPrice( Pair4, x_Pair  , y_Pair ) ; }
if( Pair5 != "" ) { y_Pair = y_Pair + y_Inc ; DoPrice( Pair5, x_Pair  , y_Pair ) ; }
if( Pair6 != "" ) { y_Pair = y_Pair + y_Inc ; DoPrice( Pair6, x_Pair  , y_Pair ) ; }
if( Pair7 != "" ) { y_Pair = y_Pair + y_Inc ; DoPrice( Pair7, x_Pair  , y_Pair ) ; }
if( Pair8 != "" ) { y_Pair = y_Pair + y_Inc ; DoPrice( Pair8, x_Pair  , y_Pair ) ; }
if( Pair9 != "" ) { y_Pair = y_Pair + y_Inc ; DoPrice( Pair9, x_Pair  , y_Pair ) ; }     
if( Pair10 != "" ) { y_Pair = y_Pair + y_Inc ; DoPrice( Pair10, x_Pair  , y_Pair ) ; }
if( Pair11 != "" ) { y_Pair = y_Pair + y_Inc ; DoPrice( Pair11, x_Pair  , y_Pair ) ; }
if( Pair12 != "" ) { y_Pair = y_Pair + y_Inc ; DoPrice( Pair12, x_Pair  , y_Pair ) ; }
if( Pair13 != "" ) { y_Pair = y_Pair + y_Inc ; DoPrice( Pair13, x_Pair  , y_Pair ) ; }
if( Pair14 != "" ) { y_Pair = y_Pair + y_Inc ; DoPrice( Pair14, x_Pair  , y_Pair ) ; }
if( Pair15 != "" ) { y_Pair = y_Pair + y_Inc ; DoPrice( Pair15, x_Pair  , y_Pair ) ; }
if( Pair16 != "" ) { y_Pair = y_Pair + y_Inc ; DoPrice( Pair16, x_Pair  , y_Pair ) ; }
if( Pair17 != "" ) { y_Pair = y_Pair + y_Inc ; DoPrice( Pair17, x_Pair  , y_Pair ) ; }
if( Pair18 != "" ) { y_Pair = y_Pair + y_Inc ; DoPrice( Pair18, x_Pair  , y_Pair ) ; }
if( Pair19 != "" ) { y_Pair = y_Pair + y_Inc ; DoPrice( Pair19, x_Pair  , y_Pair ) ; }         
}

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void DoPrice( string dpSymbol, int dp_xAxis, int dp_yAxis )
{
   cdayClose      = iClose(dpSymbol,PERIOD_D1,0);
   cdayOpen       = iOpen(dpSymbol,PERIOD_D1,0);
   
   if(cdayClose > cdayOpen) { ColorPrice = LimeGreen ; } else {
   if(cdayClose < cdayOpen) { ColorPrice = Red ; } else {ColorPrice = Yellow ; } }

   point = MarketInfo(dpSymbol,MODE_POINT);  
   
   if ( point == 0.01 )  {xPoint = 2 ; } else { xPoint = 4 ;  } 

   setObject(next(),DoubleToStr(cdayClose,xPoint),dp_xAxis, dp_yAxis, ColorPrice ,ValueFont,ValueSize);   
}


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

void showPair(string pair, int timeFrame,string label, int xdistance, int ydistance)
{
  
   DoWork(pair,timeFrame,0) ;    

   double open         = iOpen(pair,timeFrame,0);
   double high         = iHigh(pair,timeFrame,0);  
   double low          = iLow(pair,timeFrame,0);
   double close        = iClose(pair,timeFrame,0);
         
   string tChartPeriod =  TimeFrameToString(timeFrame) ;
   
   vTrigger     =  "Wait" ;
  
   int digits          = MarketInfo(pair,MODE_DIGITS);     
      
   if (close == LgTrigger) { if( my.alert.sound) { Alert(tChartPeriod,pair,", SupRes Long ", DoubleToStr(LgTrigger,digits)); }
                           INC_UP = INC_UP +1 ; MA_color = ColorUp; MA_arrow = "Ù"; 
                           vTrigger = DoubleToStr(LgTrigger, digits) ; } else {

   if (close == ShTrigger) { if( my.alert.sound) { Alert(tChartPeriod,pair,", SupRes Short ", DoubleToStr(ShTrigger,digits)); }
                           INC_DN = INC_DN +1 ; MA_color = ColorDown; MA_arrow = "Ú"; 
                           vTrigger = DoubleToStr(ShTrigger, digits) ; } else {
                           
                           
   if (low == LgTrigger)  { INC_UP = INC_UP +1 ; MA_color = ColorLoLg;  MA_arrow = ArrowNE;
                           vTrigger = DoubleToStr(LgTrigger, digits) ; } else {

   if (high == ShTrigger) { INC_DN = INC_DN +1 ; MA_color = ColorHiSh; MA_arrow = ArrowSE; 
                           vTrigger = DoubleToStr(ShTrigger, digits) ;}  else {
                        
                                                   
                           INC_UC = INC_UC +1 ; MA_color = ColorNeutral; MA_arrow = "Ø"; }} } }




   setLabel(" "  ,xdistance+10,ydistance+30," ",MA_color,vTrigger); //*******
       
  }

//+------------------------------------------------------------------+
//| Custom functions and procedures                                  |
//+------------------------------------------------------------------+

string next() { totalLabels++; return(totalLabels); }  


void setLabel(string text,int x,int y,string sarv, color theColor, string arrow, bool isADX=true)
{
    
  setObject(next(),arrow  ,y,x,theColor,"Verdana",9,0);
      
 }              
             

void setObject(string name,string text,int x,int y,color theColor, string font = "Verdana",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;
         if (tfs=="MN1" || 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);    
}


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

void DoWork( string myPair, int myPeriod , int p )  
{
		
int h, l ;
		
    h = Highest(myPair,myPeriod,MODE_HIGH,iPeriods,0);
    l = Lowest(myPair,myPeriod,MODE_LOW,iPeriods,0); 

    ShTrigger = iHigh(myPair,myPeriod,h) ;  
    LgTrigger = iLow(myPair,myPeriod,l) ;

}