//+------------------------------------------------------------------+
//|                                                     Terminal.mq4 |
//|                                    Copyright 2017, TPSproTrading |
//|                                    https://www.tpsprotrading.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2017, TPSproTrading"
#property link      "https://www.tpsprotrading.com"
#property version   "1.00"
#property strict
#property indicator_chart_window
//+------------------------------------------------------------------+
//| Custom indicator Parameters                                      |
//+------------------------------------------------------------------+
#define SYMBOLS_MAX 1024
#define ORDER          0
#define LOTS           1
#define PROFIT         2
#define PIPS           3
#define PERCENT        4
#define BUY            0
#define SELL           1

extern   ENUM_BASE_CORNER corner =  CORNER_RIGHT_LOWER;  // Graph corner for attachment
extern   string font_face        =  "Calibri";           // Font
extern   int    font_size        =  9;                   // Fontsize
extern   color  ecText           =  clrSlateGray;        // Text color
extern   color  ecProfit         =  clrLime;             // Profit color
extern   color  ecLoss           =  clrDarkOrange;       // Loss color
extern   int    smY              =  20;                  // Offset text vertically
extern   int    stY              =  14;                  // Step displacement text vertically
extern   int    smX1             =  10;                  // X coordinate of the first column
extern   int    smX2             =  90;                  // X coordinate of the second column
extern   int    smX3             =  130;                 // X coordinate of the third column
extern   int    smX4             =  190;                 // X coordinate of the fourth column
extern   int    smX5             =  230;                 // X coordinate of the fifth column
extern   int    smX6             =  280;                 // X coordinate of the sixth column

         int    k                =  0;                   // Number of orders in the market
         double n                =  1.0;                 // Coefficient to refine the type of items (old / new)
         int    s                =  0;                   // Row Count
         string symb[SYMBOLS_MAX];
         double summ[SYMBOLS_MAX][5];
         double clos[4];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
   EventSetTimer(1);
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
   EventKillTimer();
   DeleteObjects(1000);

   Comment("");
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
  {

   return(rates_total);
  }
//+------------------------------------------------------------------+
//| Timer function                                                   |
//+------------------------------------------------------------------+
void OnTimer()
  {
   if(MarketInfo(Symbol(), MODE_LOTSTEP) == 0 || AccountBalance() == 0.0) return;

   ArrayInitialize(summ,0.0);
   ArrayInitialize(clos,0.0);

   int c=Analyze();

   int c_x_1 = smX1;
   int c_x_2 = smX2;
   int c_x_3 = smX3;
   int c_x_4 = smX4;
   int c_x_5 = smX5;
   int c_x_6 = smX6;
   int c_y_1 = smY;
   int xy    = 1;

   if(corner==1 || corner==3) 
     {
      c_x_6 = smX1;
      c_x_5 = smX1 + (smX6 - smX5);
      c_x_4 = smX1 + (smX6 - smX4);
      c_x_3 = smX1 + (smX6 - smX3);
      c_x_2 = smX1 + (smX6 - smX2);
      c_x_1 = smX6;
     }
   if(corner==2 || corner==3) 
     {
      c_y_1 = smY+font_size+stY*c;
      xy=-1;
     }

   SetLabel("Terminal_1","Pair",ecText,c_x_1,c_y_1,1);
   SetLabel("Terminal_2","Order",ecText,c_x_2,c_y_1,2);
   SetLabel("Terminal_3","Lot",ecText,c_x_3,c_y_1,2);
   SetLabel("Terminal_4","Profit $",ecText,c_x_4,c_y_1,2);
   SetLabel("Terminal_5","Pips",ecText,c_x_5,c_y_1,2);
   SetLabel("Terminal_6","Perc %",ecText,c_x_6,c_y_1,2);

   double sumlot = 0.0,
   sumprf = 0.0,
   sumpip = 0.0,
   sumprc = 0.0;

   if(s!=c+2) DeleteObjects(s);

   for(int i=0; i<c; i++)
     {
      color clr=ColorOnSign(summ[i][PROFIT]);
      SetLabel("Terminal01"+IntegerToString(i,0,' '),symb[i],ecText,c_x_1,c_y_1+xy*stY*(i+1),1);
      SetLabel("Terminal02"+IntegerToString(i,0,' '),summ[i][ORDER]? "sell" : "buy",ecText,c_x_2,c_y_1+xy*stY*(i+1),2);
      SetLabel("Terminal03"+IntegerToString(i,0,' '),DoubleToStr(summ[i][LOTS],2),ecText,c_x_3,c_y_1+xy*stY*(i+1),2);
      SetLabel("Terminal04"+IntegerToString(i,0,' '),DoubleToStr(summ[i][PROFIT],2),clr,c_x_4,c_y_1+xy*stY*(i+1),2);
      SetLabel("Terminal05"+IntegerToString(i,0,' '),DoubleToStr(summ[i][PIPS],1),clr,c_x_5,c_y_1+xy*stY*(i+1),2);
      SetLabel("Terminal06"+IntegerToString(i,0,' '),DoubleToStr(summ[i][PERCENT],1)+" %",clr,c_x_6,c_y_1+xy*stY*(i+1),2);

      sumlot += summ[i][LOTS];
      sumprf += summ[i][PROFIT];
      sumpip += summ[i][PIPS];
      sumprc += summ[i][PERCENT];
     }

   color clr=ColorOnSign(sumprf);

   SetLabel("Terminal01"+IntegerToString(c,0,' '),"Total:",ecText,c_x_1,c_y_1+xy*stY*(c+1),1);
   SetLabel("Terminal02"+IntegerToString(c,0,' ')," ",ecText,c_x_2,c_y_1+xy*stY*(c+1),2);
   SetLabel("Terminal03"+IntegerToString(c,0,' '),DoubleToStr(sumlot,2),clr,c_x_3,c_y_1+xy*stY*(c+1),2);
   SetLabel("Terminal04"+IntegerToString(c,0,' '),DoubleToStr(sumprf,2),clr,c_x_4,c_y_1+xy*stY*(c+1),2);
   SetLabel("Terminal05"+IntegerToString(c,0,' '),DoubleToStr(sumpip,1),clr,c_x_5,c_y_1+xy*stY*(c+1),2);
   SetLabel("Terminal06"+IntegerToString(c,0,' '),DoubleToStr(sumprc,1)+" %",clr,c_x_6,c_y_1+xy*stY*(c+1),2);

   clr=ColorOnSign(clos[1]);

   SetLabel("Terminal01"+IntegerToString(c+1,0,' '),"Total today:",ecText,c_x_1,c_y_1+xy*stY*(c+2),1);
   SetLabel("Terminal02"+IntegerToString(c+1,0,' ')," ",ecText,c_x_2,c_y_1+xy*stY*(c+2),2);
   SetLabel("Terminal03"+IntegerToString(c+1,0,' '),DoubleToStr(clos[0],2),clr,c_x_3,c_y_1+xy*stY*(c+2),2);
   SetLabel("Terminal04"+IntegerToString(c+1,0,' '),DoubleToStr(clos[1],2),clr,c_x_4,c_y_1+xy*stY*(c+2),2);
   SetLabel("Terminal05"+IntegerToString(c+1,0,' '),DoubleToStr(clos[2],1),clr,c_x_5,c_y_1+xy*stY*(c+2),2);
   SetLabel("Terminal06"+IntegerToString(c+1,0,' '),DoubleToStr(clos[3],1)+" %",clr,c_x_6,c_y_1+xy*stY*(c+2),2);

   s=c+2;
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int Analyze()
  {
   double digit;
   int    i,type,total=OrdersTotal(),l=0;

   for(i=0; i<total; i++) 
     {
      if(!OrderSelect(i,SELECT_BY_POS)) continue;
      type=OrderType();

      if(type!=OP_BUY && type!=OP_SELL) continue;
      symb[l]=StringSubstr(OrderSymbol(),0,6);

      summ[l][ORDER]= type;
      summ[l][LOTS] = OrderLots();

      summ[l][PROFIT]=OrderProfit()+OrderCommission()+OrderSwap();

      if(MarketInfo(OrderSymbol(),MODE_TICKVALUE)!=0) 
        {
         if(MarketInfo(OrderSymbol(),MODE_DIGITS)==3 || MarketInfo(OrderSymbol(),MODE_DIGITS)==5)
            digit = 10.0;
         else  digit =  1.0;

         summ[l][PIPS]=(OrderProfit()/OrderLots()/MarketInfo(OrderSymbol(),MODE_TICKVALUE))/digit;
        }

      summ[l][PERCENT]=(OrderProfit()+OrderSwap()+OrderCommission())/AccountBalance()*100;

      l++;
     }

   datetime d0=StrToTime(TimeToStr(TimeCurrent(),TIME_DATE));
   while(TimeDayOfWeek(d0)<1 || TimeDayOfWeek(d0)>5) d0-=24*60*60;

   for(i=OrdersHistoryTotal()-1; i>=0; i--) 
     {
      if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)) 
        {
         if(OrderType()==OP_BUY || OrderType()==OP_SELL) 
           {
            if(d0<OrderCloseTime()) 
              {
               clos[0] += OrderLots();
               clos[1] += OrderProfit() + OrderCommission() + OrderSwap();

               if(MarketInfo(OrderSymbol(),MODE_TICKVALUE)!=0) 
                 {
                  if(MarketInfo(OrderSymbol(),MODE_DIGITS)==3 || MarketInfo(OrderSymbol(),MODE_DIGITS)==5)
                     digit = 10.0;
                  else  digit =  1.0;

                  clos[2]+=(OrderProfit()/OrderLots()/MarketInfo(OrderSymbol(),MODE_TICKVALUE))/digit;
                 }
               clos[3]+=(OrderProfit()+OrderSwap()+OrderCommission())/AccountBalance()*100;
              }
           }
        }
     }

   return(l);
  }
//+----------------------------------------------------------------------------+
//| Returns the color by the sign of a number                                  |
//+----------------------------------------------------------------------------+
color ColorOnSign(double nu)
  {
   color lcColor=ecText;

   if(nu > 0)    lcColor = ecProfit;
   if(nu < 0)    lcColor = ecLoss;

   return(lcColor);
  }
//+----------------------------------------------------------------------------+
//| Deleting objects                                                           |
//+----------------------------------------------------------------------------+
void DeleteObjects(int v)
  {
   string st="Terminal0";
   int    i,j;

   for(i=0; i<v; i++) 
     {
      for(j=1; j<7; j++) ObjectDelete(st+IntegerToString(j,0,' ')+IntegerToString(i,0,' '));
     }

   if(v==1000) 
     {
      ObjectDelete("Terminal_1");
      ObjectDelete("Terminal_2");
      ObjectDelete("Terminal_3");
      ObjectDelete("Terminal_4");
      ObjectDelete("Terminal_5");
      ObjectDelete("Terminal_6");
     }
  }
//+----------------------------------------------------------------------------+
//|  Author: Kim Igor V. aka KimIV, http://www.kimiv.ru                        |
//+----------------------------------------------------------------------------+
//| Version: 12/10/2007                                                        |
//| Description: Setting a text label                                          |
//+----------------------------------------------------------------------------+
//| Parameters:                                                                |
//| nm - name of the object                                                    |
//| tx - text                                                                  |
//| cl - the color of the label                                                |
//| xd is the X coordinate in pixels                                           |
//| yd - the Y coordinate in pixels                                            |
//| cr - the number of the binding angle (0 - upper left)                      |
//| fs is the font size (8 is the default)                                     |
//+----------------------------------------------------------------------------+
void SetLabel(string nm,string tx,color cl,int xd,int yd,int mode)
  {
   if(ObjectFind(nm)<0)
      ObjectCreate(nm,OBJ_LABEL,0,0,0);

   ObjectSetText(nm,tx,font_size);
   ObjectSet(nm,OBJPROP_COLOR,cl);
   ObjectSet(nm,OBJPROP_XDISTANCE,xd);
   ObjectSet(nm,OBJPROP_YDISTANCE,yd);
   ObjectSet(nm,OBJPROP_CORNER,corner);
   ObjectSetInteger(0,nm,OBJPROP_FONTSIZE,font_size);
   ObjectSetString(0,nm,OBJPROP_FONT,font_face);

   if(corner==1 || corner==3) 
     {
      if(mode==1 && corner==1)
         ObjectSetInteger(0,nm,OBJPROP_ANCHOR,ANCHOR_LEFT_UPPER);
      if(mode==1 && corner==3)
         ObjectSetInteger(0,nm,OBJPROP_ANCHOR,ANCHOR_LEFT_LOWER);
     }

   if(corner==0 || corner==2) 
     {
      if(mode==2 && corner==0)
         ObjectSetInteger(0,nm,OBJPROP_ANCHOR,ANCHOR_RIGHT_UPPER);
      if(mode==2 && corner==2)
         ObjectSetInteger(0,nm,OBJPROP_ANCHOR,ANCHOR_RIGHT_LOWER);
     }
  }
//+----------------------------------------------------------------------------+
