//+------------------------------------------------------------------+
//|                                                        Total.mq4 |
//|                                          Copyright © 2009, Aiman |
//|                                                noen.ss@gmail.com |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2007, Aiman"
#property link      "noen.ss@gmail.com"

#property indicator_chart_window
string symbol[28];
double lots[28];
double pips[28];
double profit[28];
double totalpos[28];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init() { Comment(""); ObjectsDeleteAll(); return(0); }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit() { Comment(""); ObjectsDeleteAll(); return(0); }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
   RefreshRates ();
   PairTrades ();

   return(0);
  }
//+------------------------------------------------------------------+
void PairTrades ()
{
   int i, j;
   double totallots=0;
   for(i=0;i<ArraySize(symbol);i++)
   {
      symbol[i]="";
      totalpos[i]=0;      
      lots[i]=0;
      pips[i]=0;
      profit[i]=0;
   }
	for(i=0;i<OrdersTotal();i++)
	{
      OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
      for(j=0;j<ArraySize(symbol);j++)
      {	  
         if(symbol[j]==OrderSymbol() || symbol[j]=="")
         {
            symbol[j]=OrderSymbol();
            totalpos[j]=totalpos[j]+1;
            lots[j]=lots[j]+OrderLots();
            profit[j]=profit[j]+OrderProfit()+OrderSwap()+OrderCommission();

            double point=MarketInfo(OrderSymbol(),MODE_POINT);
            if(OrderType()==OP_BUY)
               pips[j]=pips[j]+(MarketInfo(OrderSymbol(),MODE_BID)-OrderOpenPrice())/point;
            if(OrderType()==OP_SELL)
               pips[j]=pips[j]+(OrderOpenPrice()-MarketInfo(OrderSymbol(),MODE_ASK))/point;   
               
            totallots=totallots+OrderLots();
            break;
	     }
	  }
   }
   
   string strTemp;
   double dblTemp;
   
   if(symbol[0]!="")
   {
      for(i=0;i<ArraySize(symbol);i++)
      {
         if(symbol[i]=="") break;
         for(j=i;j<ArraySize(symbol);j++)
         {
            if(symbol[j]=="") break;
            if(profit[j]<profit[i])
            {
               strTemp=symbol[i];
               symbol[i]=symbol[j];
               symbol[j]=strTemp;
               
               dblTemp=totalpos[i];
               totalpos[i]=totalpos[j];
               totalpos[j]=dblTemp; 
               
               dblTemp=lots[i];
               lots[i]=lots[j];
               lots[j]=dblTemp;               
 
               dblTemp=pips[i];
               pips[i]=pips[j];
               pips[j]=dblTemp; 

               dblTemp=profit[i];
               profit[i]=profit[j];
               profit[j]=dblTemp;                              
              
            }
         }
      }
   }
   
   int ypos=48;
   string text_total;
   int objtotal;
   color fontcolor;
   
   objtotal=0;
   for (i=0;i<ArraySize(symbol);i++)
   {
      ObjectDelete("Pairtrade-Symbol"+DoubleToStr(objtotal,0));
      ObjectDelete("Pairtrade-lots"+DoubleToStr(objtotal,0));
      ObjectDelete("Pairtrade-pips"+DoubleToStr(objtotal,0));
      ObjectDelete("Pairtrade-profit"+DoubleToStr(objtotal,0));
      objtotal++;
   }
   
   objtotal=0;      
   for (i=0;i<ArraySize(symbol);i++)
   {
      if (symbol[i]=="") break;
      
      fontcolor=Red;
      if (StringFind(symbol[i],"GBPUSD",0)!=-1 || StringFind(symbol[i],"EURGBP",0)!=-1 || StringFind(symbol[i],"GBPJPY",0)!=-1 || StringFind(symbol[i],"USDCHF",0)!=-1 
      || StringFind(symbol[i],"NZDUSD",0)!=-1 || StringFind(symbol[i],"AUDJPY",0)!=-1 || StringFind(symbol[i],"EURJPY",0)!=-1 ) fontcolor=Green;
      drawLabel ("Pairtrade-Symbol"+DoubleToStr(objtotal,0), symbol[i]+ " ("+DoubleToStr(totalpos[i],0)+")", 9, "Courier New", fontcolor, 3, 140, ypos);

      drawLabel ("Pairtrade-lots"+DoubleToStr(objtotal,0), ": "+DoubleToStr(lots[i],2), 9, "Courier New", OliveDrab, 3, 90, ypos);
      
      drawLabel ("Pairtrade-pips"+DoubleToStr(objtotal,0), DoubleToStr(MathAbs(pips[i]),0), 9, "Courier New", profitcolor(pips[i]), 3, 55, ypos);

      drawLabel ("Pairtrade-profit"+DoubleToStr(objtotal,0), DoubleToStr(MathAbs(profit[i]),2), 9, "Courier New", profitcolor(profit[i]), 3, 10, ypos);

      objtotal++;
      ypos=ypos+12;
   }
   text_total=StringConcatenate(TimeToStr(TimeLocal(),TIME_DATE),
      " (", TimeToStr(TimeLocal(),TIME_MINUTES), ")");
   drawLabel ("total-Time", text_total, 9, "Courier New", Olive, 3, 8, ypos+2); 
   drawLabel ("total-Line1", "_____________", 12, "Courier New", Olive, 3, 10, ypos+2);
   
   ypos=10;   
   text_total="Pips Collected Today : "+DoubleToStr(PipsCollected(),0);
   drawLabel ("total-PipsGet", text_total, 9, "Courier New", profitcolor1(PipsCollected()), 3, 10, ypos); 
   
   ypos=ypos+12;
   text_total="Total Spread : "+DoubleToStr(TotalSpread(),0);
   drawLabel ("total-Spread", text_total, 9, "Courier New", Olive, 3, 10, ypos); 
 
   ypos=ypos+12;
   text_total="Total ("+DoubleToStr(OrdersTotal(),0)+")";
   drawLabel ("total-TotalOpenPos", text_total, 9, "Courier New", Olive, 3, 140, ypos);
   
   text_total=": "+DoubleToStr(totallots,2);
   drawLabel ("total-Lots", text_total, 9, "Courier New", Olive, 3, 90, ypos);

   text_total=DoubleToStr(MathAbs(TotalProfit()),2);
   drawLabel ("total-TotalProfit", text_total, 9, "Courier New", profitcolor1(TotalProfit()), 3, 10, ypos);

   text_total=DoubleToStr(MathAbs(TotalPips()),0);
   drawLabel ("total-TotalPips", text_total, 9, "Courier New", profitcolor1(TotalPips()), 3, 55, ypos);
   
   drawLabel ("total-Line2", "____________________", 12, "Courier New", Olive, 3, 10, ypos+13);

}
//+------------------------------------------------------------------+
//Total Spread for Opened Positions
//+------------------------------------------------------------------+
double TotalSpread ()
{
   double Spread=0;
   for(int i=0;i<OrdersTotal();i++)
   {
      OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
      if (OrderType()==OP_BUY || OrderType()==OP_SELL)
          Spread=Spread+MarketInfo(OrderSymbol(), MODE_SPREAD);
   }
   return(Spread);
}
//+------------------------------------------------------------------+
//Total Pips
//+------------------------------------------------------------------+
double TotalPips ()
{
   double totalpips,pips;
   totalpips=0;
   pips=0;
   for(int i=0;i<OrdersTotal();i++) 
   {
      OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
      double point=MarketInfo(OrderSymbol(),MODE_POINT);
      if(OrderType()==OP_BUY)
         pips=(MarketInfo(OrderSymbol(),MODE_BID)-OrderOpenPrice())/point;
      if(OrderType()==OP_SELL)
         pips=(OrderOpenPrice()-MarketInfo(OrderSymbol(),MODE_ASK))/point;   
      totalpips=totalpips+pips;
   }
   return (totalpips);
}
//+------------------------------------------------------------------+
//Total Profit
//+------------------------------------------------------------------+
double TotalProfit ()
{
   double totalprofit=0;
   for(int i=0;i<OrdersTotal();i++) 
   {
      OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
      if(OrderType()==OP_BUY || OrderType()==OP_SELL)
          totalprofit=totalprofit+OrderProfit()+OrderCommission()+OrderSwap();
   }
   return (totalprofit);
}
//+------------------------------------------------------------------+
double PipsCollected ()
{
  double Pips;
  double GetPips=0;
  for(int cnt=0;cnt<OrdersHistoryTotal();cnt++)
  {
    OrderSelect(cnt, SELECT_BY_POS, MODE_HISTORY);
    if (OrderType()==OP_BUY && TimeToStr(OrderCloseTime(), TIME_DATE)==TimeToStr(TimeCurrent(), TIME_DATE) )
      Pips=(OrderClosePrice()-OrderOpenPrice())/MarketInfo(OrderSymbol(),MODE_POINT);
    if (OrderType()==OP_SELL && TimeToStr(OrderCloseTime(), TIME_DATE)==TimeToStr(TimeCurrent(), TIME_DATE) )      
      Pips=(OrderOpenPrice()-OrderClosePrice())/MarketInfo(OrderSymbol(),MODE_POINT);
    GetPips=GetPips+Pips;
  }
  return (GetPips);
}
//+------------------------------------------------------------------+
void drawLabel (string Ln, string Lt, int th, string ts, color Lc, int cr, int xp, int yp)
{
   ObjectCreate(Ln, OBJ_LABEL, 0, 0, 0);
   ObjectSetText(Ln, Lt, th, ts, Lc);
   ObjectSet(Ln, OBJPROP_CORNER, cr);
   ObjectSet(Ln, OBJPROP_XDISTANCE, xp+10);
   ObjectSet(Ln, OBJPROP_YDISTANCE, yp);  
}
//+------------------------------------------------------------------+ 
color profitcolor (double total)
{
   if (total>0) return (ForestGreen);
   if (total<0) return (Peru);
   return (Olive);
}
//+------------------------------------------------------------------+ 
color profitcolor1 (double total)
{
   if (total>0) return (LawnGreen);
   if (total<0) return (Red);
   return (Olive);
}