//+------------------------------------------------------------------+
//|                                             Dashboard_TMA_v1.mq4 |
//|                                                                  |
//|             Based on "Dashboard_RSI_v3.mq4"                      |
//|             Copyright © 2010, gspe (modified by onedognight)     |
//|                                                                  |
//| further modified by doublePip to show TMA Slopes instead of RSI  |
//+------------------------------------------------------------------+

#property indicator_chart_window

extern string	xPairs = "EURAUD,EURCAD,EURJPY,EURUSD,GBPAUD,GBPJPY,GBPNZD,GBPUSD";
extern string	xPeriods = "M1,M5,M15";
extern string	FontName = "Arial Bold";
extern int		FontSize = 10;
extern color   FontColor = White;
extern color	IconColorUp = Lime;
extern color	IconColorDn = Red;
extern color	IconColorNoSignal = DimGray;
extern int     X_Distance = 10;
extern int     Y_Distance = 20;

int		NoOfPairs;
string	Pair[];    			  // The xPairs string will be split and stored in this array

int		NoOfPeriods;
string	strTF[];   			  // TimeFrame array ("M1","M5" etc)
int		TF[];	           	  // TimeFrame array (in minutes)

int   IconUp, IconDn, IconNoSignal, ShiftBar;

string objPrefix ;	// all objects drawn by this indicator will be prefixed with this
string objId ;

//+-----------------------------------------------------------+
//| initialization function                                   |
//+-----------------------------------------------------------+
int init()
{
	objPrefix = WindowExpertName();
	
	objId = StringConcatenate(objPrefix, "Label");
	ObjectDelete(objId);
   ObjectCreate(objId, OBJ_LABEL, 0, 0, 0);
   ObjectSetText(objId,"TMA Slopes",FontSize+2, FontName, FontColor);
   ObjectSet(objId, OBJPROP_CORNER, 0);
   ObjectSet(objId, OBJPROP_XDISTANCE, X_Distance+20);
   ObjectSet(objId, OBJPROP_YDISTANCE, Y_Distance);
	
	NoOfPairs = StringFindCount(xPairs,",")+1;     // Count the pairs
	ArrayResize(Pair, NoOfPairs);                  // Resize their Array
	string Suffix = StringSubstr(Symbol(),6,4);    // See if this dealer uses a pair-suffix eg EURUSDm
	StringToArray(xPairs, Pair, Suffix);           // Populate the Pair array

	NoOfPeriods = StringFindCount(xPeriods, ",")+1;// Count the TimeFrames
	ArrayResize(strTF, NoOfPeriods);               // Resize their string array
	ArrayResize(TF, NoOfPeriods);                  // Resize their in_seconds array 
	StringToArray(xPeriods, strTF, "");            // Populate the timeframe array
	
	for(int i=0; i<NoOfPeriods; i++)
	{
		TF[i] = StrToTF(strTF[i]);                  // Populate the in_minutes array
	}

   // Display Pairs	
	for(i=0; i<NoOfPairs; i++)
	{
		objId = StringConcatenate(objPrefix, Pair[i]);
		ObjectDelete(objId);
		ObjectCreate(objId,OBJ_LABEL,0,0,0,0,0);
		ObjectSet(objId,OBJPROP_CORNER,0);
		ObjectSet(objId,OBJPROP_XDISTANCE,X_Distance);
		ObjectSet(objId,OBJPROP_YDISTANCE,Y_Distance+25+(i+1)*2.0*FontSize);
		ObjectSetText(objId,Pair[i],FontSize-2,FontName,FontColor);
	}

	//Display TimeFrame Headings
	for(int j=0; j<NoOfPeriods; j++)
	{
		objId = StringConcatenate(objPrefix, strTF[j]);
		ObjectDelete(objId);
		ObjectCreate(objId,OBJ_LABEL,0,0,0,0,0);
		ObjectSet(objId,OBJPROP_CORNER,0);
		ObjectSet(objId,OBJPROP_XDISTANCE,X_Distance+60+(j*FontSize*2.8));
		ObjectSet(objId,OBJPROP_YDISTANCE,Y_Distance+25);
		ObjectSetText(objId,strTF[j],FontSize-2,FontName,FontColor);      
	}

	return(0);
}

//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
{
	Comment(""); 
	
	for (int i = ObjectsTotal(); i >= 0; i--)
	{
		string objname = ObjectName(i);
		if (StringFind(objname, objPrefix, 0) > -1) ObjectDelete(objname);
	}
	return(0);
}

//+------------------------------------------------------------------+
//| start function                                            |
//+------------------------------------------------------------------+
int start()
{
	for(int i=0; i<NoOfPairs; i++)
	{
	   string ThisPair = Pair[i];
	   
		for(int j=0; j<NoOfPeriods; j++)
		{
		   int ThisTF   = TF[j];
		   
		   int BarStartTime = iTime(ThisPair,ThisTF,0);
		   if( TimeCurrent()  >  BarStartTime + (30/100 * ThisTF * 60) )
		   {
   		   ShiftBar=0;  // Start TMA from bar zero (ie the current bar)
   		   IconUp=233; IconDn=234; IconNoSignal=108; // Solid icons
		   }
		   else
		   {
		      ShiftBar=1;  // Start TMA from bar1 (ie the previous bar)
		      IconUp=233; IconDn=234; IconNoSignal=108; // Outline icons
		   }

         double TMA_c = iCustom(ThisPair, ThisTF, "FastTMALine", 0, ShiftBar);
         double TMA_p = iCustom(ThisPair, ThisTF, "FastTMALine", 0, ShiftBar+1);

			if(TMA_c ==  TMA_p)                          
			{
			   int TrendIcon = IconNoSignal;
			   color TrendColor = IconColorNoSignal;
			}
			else
			{
               if(TMA_c > TMA_p)                         // ....up
			      {
   					TrendIcon = IconUp;
	     				TrendColor = IconColorUp;
			      }
			      else
			      {
			      if(TMA_c < TMA_p)                         // ....down
			      {
   					TrendIcon = IconDn;
   					TrendColor = IconColorDn;
			      }
            }
         }

			objId = StringConcatenate(objPrefix, ThisPair, strTF[j]);
			ObjectDelete(objId);
			ObjectCreate(objId,OBJ_LABEL,0,0,0,0,0);
 			ObjectSet(objId,OBJPROP_CORNER,0);
			ObjectSet(objId,OBJPROP_XDISTANCE,X_Distance+60+(j*FontSize*2.8));
			ObjectSet(objId,OBJPROP_YDISTANCE,Y_Distance+25+(i+1)*2.0*FontSize);
			ObjectSetText(objId,CharToStr(TrendIcon),FontSize,"Wingdings",TrendColor);

		}//Next j
	}//Next i
  return(0);
}//End start()

//+------------------------------------------------------------------+


//+------------------------------------------------------------------+
// StringFindCount                                                   |
//+------------------------------------------------------------------+
int StringFindCount(string Str, string Str2)
// Returns the number of occurrences of Str2 in Str
// Usage:   int x = StringFindCount("ABCDEFGHIJKABACABB","AB")   returns x = 3
{
   int c=0, Str2Len=StringLen(Str2);
   for (int i=0; i<StringLen(Str); i++)
   {
    if( StringSubstr(Str,i,Str2Len) == Str2 )  c++;
   }
  return(c);
}

//+------------------------------------------------------------------+
// Populate array from csv string                                                 |
//+------------------------------------------------------------------+
void StringToArray(string Str, string &a[], string p_suffix)
{
	int  i=0,                    // array pointer
	     z1=0,                   // string pointer
	     z2=0,                   // position of next comma
	     LenStr=StringLen(Str);
	
	while( z2 < LenStr )
	{
		z2 = StringFind(Str,",",z1);
		if( z2 == -1 ) z2 = LenStr;
		a[i] = StringConcatenate(StringSubstr(Str,z1,z2-z1),p_suffix);
		z1 = z2+1;
		i++;
	}
}

//+------------------------------------------------------------------+
// StrToTF(string Str)                                               |
//+------------------------------------------------------------------+
// Converts a timeframe string to its MT4-numeric value
// Usage:   int x=StrToTF("M15")   returns x=15
int StrToTF(string Str)
{
  if (Str == "M1")   return(PERIOD_M1);
  if (Str == "M5")   return(PERIOD_M5);
  if (Str == "M15")  return(PERIOD_M15);
  if (Str == "M30")  return(PERIOD_M30);
  if (Str == "H1")   return(PERIOD_H1);
  if (Str == "H4")   return(PERIOD_H4);
  if (Str == "D1")   return(PERIOD_D1);
  if (Str == "W1")   return(PERIOD_W1);
  if (Str == "MN")   return(PERIOD_MN1);
  return(0);
}

