//+------------------------------------------------------------------+ 
//|   TRO_DASHBOARD_HL_TREND                                         | 
//|                                                                  | 
//|   Copyright © 2008, Avery T. Horton, Jr. aka TheRumpledOne       |
//|                                                                  |
//|   PO BOX 43575, TUCSON, AZ 85733                                 |
//|                                                                  |
//|   GIFTS AND DONATIONS ACCEPTED                                   | 
//|   All my indicators should be considered donationware. That is   |
//|   you are free to use them for your personal use, and are        |
//|   under no obligation to pay for them. However, if you do find   |
//|   this or any of my other indicators help you with your trading  |
//|   then any Gift or Donation as a show of appreciation is         |
//|   gratefully accepted.                                           |
//|                                                                  |
//|   Gifts or Donations also keep me motivated in producing more    |
//|   great free indicators. :-)                                     |
//|                                                                  |
//|   PayPal - THERUMPLEDONE@GMAIL.COM                               |  
//+------------------------------------------------------------------+ 
 
#property indicator_separate_window
#property indicator_minimum 0
#property indicator_maximum 1

extern bool   Sound.Alert    = false ;

extern int myBars      = 10 ;
extern int myThreshold = 4 ;
extern string myFont  = "Verdana" ; // "Arial Bold"
//extern string Symbols      = "USDJPY;EURUSD;GBPJPY;GBPUSD;USDCHF;USDCAD;EURJPY;GBPCHF;AUDJPY;AUDUSD;EURGBP;NZDUSD" ;
extern string Symbols      = "AUDJPY;AUDUSD;EURGBP;EURJPY;EURUSD;GBPCHF;GBPJPY;GBPUSD;NZDUSD;USDCAD;USDCHF;USDJPY" ;

//extern string Symbols      = "EURUSD;USDJPY";
extern string TimeFrames   = "M1;M5;M15;M30;H1;H4;D1;W1;MN";

//extern bool   UseMSLineDrawFont = false;

 
 color  StrongUp     = LimeGreen;
 color  WeakUp       = Green;
 color  NoMove       = DimGray;
 color  WeakDown     = FireBrick;
 color  StrongDown   = Red;
 color  DoesNotExist = Black;

string shortName;
int    cpairsLenH;
int    cpairsLenV;
int    shortLength;
int    window;  

color theColor ;

string symbol, tChartPeriod, ShortName , thevalue, aChartPeriod;  

int    digits, period, win ; 
int     Stats[11], StatsR[11], StatsG[11],scount;

string  ind_bufferOp[] ;

double point, High0, Low0, Open0,  Close0, Open1,High1, Low1, Close1, TheCount   ;  
double  High2, Low2, Open2,  Close2, Open3,High3, Low3, Close3    ;   
  
bool red0, red1, green0, green1 ;
    

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

int    ctimesLen;
string cpairsh[];
string cpairsv[];
int    aTimes[];
string addition  = "";
string FontToUse = "Terminal";
 


//+------------------------------------------------------------------+
int init()
{

   if(Symbols == "") { Symbols = Symbol() ; }

   period       =  Period() ; 
   tChartPeriod =  TimeFrameToString(period) ;
   symbol       =  Symbol() ;
   digits       =  Digits ;
   point        =  Point ;
   ShortName    =  "HLTrend" ;
   win          =  0 ;
   
   ArrayResize(ind_bufferOp,myBars) ;



//   if (UseMSLineDrawFont) FontToUse = "MS LIneDraw";

      setUpStrings(cpairsh,cpairsLenH,Symbols);

 

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

                                      
            
         int s = 0;
         int i = StringFind(TimeFrames,";",s);
         int time;
            while (i > 0)
            {
               string current = StringSubstr(TimeFrames,s,i-s);
               time    = stringToTimeFrame(current);
               if (time > 0) {
                     ArrayResize(aTimes,ArraySize(aTimes)+1);
                                 aTimes[ArraySize(aTimes)-1] = time; }
                                 s = i + 1;
                                     i = StringFind(TimeFrames,";",s);
            }
      ctimesLen = ArraySize(aTimes);
      
 
   
   if (IsMini())   addition="m";
   if (IsDoubleDotted()) addition="..";
   else 
      if (IsDotted()) addition=".";
      shortName   = ShortName ;
      shortLength = StringLen(shortName);
      IndicatorShortName(shortName);
      
   
      
   return(0);
}


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

int deinit()
{
   clearObjects();
   return(0);
}

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

int start()
{
   string name;
   int    i,k,t;

         window = WindowFind(shortName);  
   
  

      for (t = 0; t < cpairsLenH; t++ )
      {
         name = shortName+"t"+t;
         if (ObjectFind(name) == -1)
             ObjectCreate(name,OBJ_LABEL,window,0,0,0,0);
                ObjectSet(name,OBJPROP_XDISTANCE,20);
                ObjectSet(name,OBJPROP_YDISTANCE,20+t*15);
                ObjectSetText(name,cpairsh[t],10,myFont);
      }                   
      for (i = 0; i < ctimesLen; i++)
      {
         name = shortName+"h"+i;
         if (ObjectFind(name) == -1)
             ObjectCreate(name,OBJ_LABEL,window,0,0,0,0);
                ObjectSet(name,OBJPROP_XDISTANCE,i*66+110);
                ObjectSet(name,OBJPROP_YDISTANCE,1);
                ObjectSetText(name,TimeFrameToString(aTimes[i]),10,myFont);
      }
      
     

   for (i = 0; i < cpairsLenH; i++)
   for (t = 0; t < ctimesLen;  t++)
   {
      string symbol = cpairsh[i]+addition;
      double price  = iClose(symbol,aTimes[t],0);
      bool   exist  = true;
         
         if (price == 0) exist = false;
         if (!exist) continue;      
         double close = iClose(symbol,aTimes[t],1);
         double high  = iHigh(symbol,aTimes[t],1);
         double low   = iLow(symbol,aTimes[t],1);
         
         DoWork( symbol ,  aTimes[t] ) ;
            name = shortName+t+k+i;
            if (ObjectFind(name) == -1)
                  ObjectCreate(name,OBJ_LABEL,window,0,0,0,0);
                  ObjectSet(name,OBJPROP_XDISTANCE,t*66+120);
                  ObjectSet(name,OBJPROP_YDISTANCE,k*15+20+i*15); 
                  ObjectSetText(name,ind_bufferOp[0],10,"Wingdings",theColor);
        if( Sound.Alert ) { DoAlert( symbol, aTimes[t], thevalue ) ; }
   }
   return(0);
}


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

bool   IsMini()         { return(StringFind(Symbol(),"m")  > -1); }
bool   IsDoubleDotted() { return(StringFind(Symbol(),"..") > -1); }
bool   IsDotted()       { return(StringFind(Symbol(),".")  > -1); }
string MakeUniqueName(string first, string rest)
{
   string result = first+(MathRand()%1001)+rest;

   while (WindowFind(result)> 0)
          result = first+(MathRand()%1001)+rest;
   return(result);
}

 

//+------------------------------------------------------------------+
void clearObjects()
{
   for (int i = ObjectsTotal(); i>=0; i--)
   {
         string name = ObjectName(i);
         if (StringSubstr(name,0,shortLength) == shortName) ObjectDelete(name);
   }         
}

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

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="MN";
   }
   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);
}

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

void setUpStrings(string& array[],int& length, string source)
{
   source = StringUpperCase(StringTrimLeft(StringTrimRight(source)));
   if (StringSubstr(source,StringLen(source)-1,1) != ";")
                    source = StringConcatenate(source,";");

   //
   //
   //
   //
   //
   
   int  s = 0;
   int  i = StringFind(source,";",s);
   string current;
      while (i > 0)
      {
         current = StringSubstr(source,s,i-s);
         ArrayResize(array,ArraySize(array)+1);
                     array[ArraySize(array)-1] = current;
                     s = i + 1;
                     i = StringFind(source,";",s);
      }
   length = ArraySize(array);
}
 

//+------------------------------------------------------------------+
void DoWork( string wSymbol, int wPeriod )
{
   for(int i=myBars; i >= 0; i--) 
   {  
      ind_bufferOp[i] = 0 ;             
   } 
   
     

      Open0   = iOpen(wSymbol,wPeriod,0) ;
      Close0  = iClose(wSymbol,wPeriod,0) ;
      
      Open1   = iOpen(wSymbol,wPeriod,1) ;
      Close1  = iClose(wSymbol,wPeriod,1) ;

      High0 = iHigh(wSymbol,wPeriod,0) ;
      Low0  = iLow(wSymbol,wPeriod,0) ;
      
      High1 = iHigh(wSymbol,wPeriod,1) ;
      Low1  = iLow(wSymbol,wPeriod,1) ;
      
while(true)
{         
  
      if(High0 >  High1 && Low0 >= Low1 ) { ind_bufferOp[0] = ArrowN  ; theColor = Lime    ; break ; }  
      if(High0 == High1 && Low0 >  Low1 ) { ind_bufferOp[0] = ArrowNE ; theColor = Lime    ; break ; }        
      if(High0 <= High1 && Low0 <  Low1 ) { ind_bufferOp[0] = ArrowS  ; theColor = Red     ; break ; }   
      if(High0 <  High1 && Low0 == Low1 ) { ind_bufferOp[0] = ArrowSE ; theColor = Red     ; break ; }             
                                            ind_bufferOp[0] = ArrowE  ; theColor = DimGray ; break ;
         
}     
 




}

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

void DoAlert( string wSymbol, int wPeriod, double wValue )
{
   aChartPeriod =  TimeFrameToString(wPeriod) ;
   if( wValue >= myThreshold ) { Alert(wSymbol,"  ", aChartPeriod, " Bar Count Threshold ", DoubleToStr(wValue,0) ) ; }     

}