//+------------------------------------------------------------------+
//|                               iHistory Merged with iExposure.mq4 |
//|                      Copyright © 2007, MetaQuotes Software Corp. |
//|                                        http://www.metaquotes.net |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2007, MetaQuotes Software Corp."
#property link      "http://www.metaquotes.net"
#include <WinUser32.mqh>
#include <stdlib.mqh>

#property indicator_separate_window
#property indicator_buffers 1
//#property indicator_minimum 0.0
//#property indicator_maximum 0.1


#define SYMBOLS_MAX 1024
#define DEALS          0
#define BUY_LOTS       1
#define BUY_ORDERS     2
#define SELL_LOTS      3
#define SELL_ORDERS    4
#define TOTAL_LOTS     5
#define GPROFIT        6
#define GLOSS          7
#define PROFIT         8
#define NET_PIPS       9 

extern string  se="History date format - YYYY/MM/DD";
extern string  startdate="2013.08.18";
extern string  enddate  ="2013.12.31";
extern color   HeaderColor=Yellow;
extern color   SymbolColor=Lime;
extern color   TextColor=White;
extern int     TextSize=10;
extern int     RowSpacing=14;
extern bool    IncludeOpenTrades=false;
extern bool    IncludeClosedTrades=true;

string Name="History";
string Symbols[SYMBOLS_MAX];
int    SymbolsTotal=0;
double SymbolsSummaries[SYMBOLS_MAX][15];
int    Lines=-1;
string Cols[11]={"Symbol", "Deals", "Buy lots", "Buy deals", "Sell lots", "Sell deals", "Total lots", "Profit", "Loss", "Net P/L", "Net Pips"};
int    Shifts[10]={ 10, 80, 130, 190, 260, 320, 390, 460, 530, 600, 670};

double MapBuffer[];
double Tprofit,Tloss,Tpl,Tpips,Tdeals,OPTprofit,OPTloss,OPTpl,OPTpips,OPTdeals;
datetime sdate,edate;
double multiplier;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
void init()
  {
	IndicatorShortName(Name);
   SetIndexBuffer(0,MapBuffer);
   SetIndexStyle(0,DRAW_NONE);
   IndicatorDigits(0);
	SetIndexEmptyValue(0,0.0);
	sdate=StrToTime(startdate);
	edate=StrToTime(enddate);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void deinit()
  {
   int windex=WindowFind(Name);
   if(windex>0) ObjectsDeleteAll(windex);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
void start()
  {
   string name;
   int    i,col,line,windex=WindowFind(Name);
//----
   if(windex<0) return;
//---- header line
   if(Lines<0)
     {
      for(col=0; col<11; col++)
        {
         name="Head_"+col;
         if(ObjectCreate(name,OBJ_LABEL,windex,0,0))
           {
            ObjectSet(name,OBJPROP_XDISTANCE,Shifts[col]);
            ObjectSet(name,OBJPROP_YDISTANCE,RowSpacing);
            ObjectSetText(name,Cols[col],9,"Arial",HeaderColor);
           }
        }
      Lines=0;
     }
//----
   ArrayInitialize(SymbolsSummaries,0.0);
int total,total1;
if(IncludeClosedTrades) {total=Analyze();}
if(IncludeOpenTrades) {total1=AnalyzeOpenTrades();}
SortTables(Symbols);
   if(total>0||total1>0)
     {
      line=0;
      for(i=0; i<SymbolsTotal; i++)
        {
         if(SymbolsSummaries[i][DEALS]<=0) continue;
         line++;
         //---- add line
         if(line>Lines)
           {
            int y_dist=RowSpacing*(line+1)+1;
            for(col=0; col<11; col++)
              {
               name="Line_"+line+"_"+col;
               if(ObjectCreate(name,OBJ_LABEL,windex,0,0))
                 {
                  ObjectSet(name,OBJPROP_XDISTANCE,Shifts[col]);
                  ObjectSet(name,OBJPROP_YDISTANCE,y_dist);
                 }
              }
            Lines++;
           }

         //---- set line

         name="Line_"+line+"_0";
         ObjectSetText(name,Symbols[i],9,"Arial",SymbolColor);
         name="Line_"+line+"_1";
         ObjectSetText(name,DoubleToStr(SymbolsSummaries[i][DEALS],0),TextSize,"Arial",TextColor);
         name="Line_"+line+"_2";
         ObjectSetText(name,DoubleToStr(SymbolsSummaries[i][BUY_LOTS],2),TextSize,"Arial",TextColor);
         name="Line_"+line+"_3";
         ObjectSetText(name,DoubleToStr(SymbolsSummaries[i][BUY_ORDERS],0),TextSize,"Arial",TextColor);
         name="Line_"+line+"_4";
         ObjectSetText(name,DoubleToStr(SymbolsSummaries[i][SELL_LOTS],2),TextSize,"Arial",TextColor);
         name="Line_"+line+"_5";
         ObjectSetText(name,DoubleToStr(SymbolsSummaries[i][SELL_ORDERS],0),TextSize,"Arial",TextColor);
         name="Line_"+line+"_6";
         ObjectSetText(name,DoubleToStr(SymbolsSummaries[i][BUY_LOTS]+SymbolsSummaries[i][SELL_LOTS],2),TextSize,"Arial",TextColor);
         name="Line_"+line+"_7";
         ObjectSetText(name,DoubleToStr(SymbolsSummaries[i][GPROFIT],2),TextSize,"Arial",TextColor);
         name="Line_"+line+"_8";
         ObjectSetText(name,DoubleToStr(SymbolsSummaries[i][GLOSS],2),TextSize,"Arial",TextColor);
         name="Line_"+line+"_9";
         ObjectSetText(name,DoubleToStr(SymbolsSummaries[i][PROFIT],2),TextSize,"Arial",TextColor);
         name="Line_"+line+"_10";
         ObjectSetText(name,DoubleToStr(SymbolsSummaries[i][NET_PIPS],0),TextSize,"Arial",TextColor);         
       }
     }
DrawTotals();     
WindowRedraw();  
//---- to avoid minimum==maximum
   MapBuffer[Bars-1]=-1;
//----

  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int Analyze()
  {
   double profit,bprofit,sprofit;
   int    i,index,type,total=OrdersHistoryTotal();
//----

   for(i=0; i<total; i++)
     {
      if(!OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)) continue;
      type=OrderType();
      if(type!=OP_BUY && type!=OP_SELL) continue;
      index=SymbolsIndex(OrderSymbol());
      if(index<0 || index>=SYMBOLS_MAX) continue;
      if (OrderOpenTime()<sdate) continue;
      if (OrderOpenTime()>edate) continue;
      
      //----
      //SymbolsSummaries[index][DEALS]++;
double PIPS;
GetMultiplier();
if(OrderType() == OP_BUY) {PIPS=((OrderClosePrice()-OrderOpenPrice())/MarketInfo(OrderSymbol(),MODE_POINT)*multiplier);}
if(OrderType() == OP_SELL) {PIPS=((OrderOpenPrice()-OrderClosePrice())/MarketInfo(OrderSymbol(),MODE_POINT)*multiplier);}
SymbolsSummaries[index][NET_PIPS]+= PIPS;

//if (OrderOpenTime()<sdate)sdate=OrderOpenTime();
//if (OrderOpenTime()>edate)edate=OrderOpenTime();

      profit=OrderProfit()+OrderCommission()+OrderSwap();
      if(profit>=0)SymbolsSummaries[index][GPROFIT]+=profit;
      else SymbolsSummaries[index][GLOSS]+=profit;
      SymbolsSummaries[index][PROFIT]=SymbolsSummaries[index][GPROFIT]+SymbolsSummaries[index][GLOSS];
      if(type==OP_BUY)
        {
         SymbolsSummaries[index][BUY_LOTS]+=OrderLots();
         SymbolsSummaries[index][BUY_ORDERS]++;
        }
      if(type==OP_SELL)
        {
         SymbolsSummaries[index][SELL_LOTS]+=OrderLots();
         SymbolsSummaries[index][SELL_ORDERS]++;
        }
        
        SymbolsSummaries[index][DEALS]=SymbolsSummaries[index][SELL_ORDERS]+SymbolsSummaries[index][BUY_ORDERS];
     }
//----
   total=0;
   Tpl=0;Tpips=0;Tdeals=0;Tprofit=0;Tloss=0;
   for(i=0; i<SymbolsTotal; i++)
     {
      if(SymbolsSummaries[i][DEALS]>0) total++;
      Tpl+=SymbolsSummaries[i][PROFIT];
      Tpips+=SymbolsSummaries[i][NET_PIPS];
      Tdeals+=SymbolsSummaries[i][DEALS];
      Tprofit+=SymbolsSummaries[i][GPROFIT];
      Tloss+=SymbolsSummaries[i][GLOSS];
     }
//----
   return(total);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int AnalyzeOpenTrades()
  {
   double profit,bprofit,sprofit;
   int    i,index,type,total=OrdersTotal();

   for(i=0; i<total; i++)
     {
      if(!OrderSelect(i,SELECT_BY_POS,MODE_TRADES)) continue;
      type=OrderType();
      if(type!=OP_BUY && type!=OP_SELL) continue;
      index=SymbolsIndex(OrderSymbol());
      if(index<0 || index>=SYMBOLS_MAX) continue;
      if (OrderOpenTime()<sdate) continue;
      if (OrderOpenTime()>edate) continue;
      //----
      //SymbolsSummaries[index][DEALS]++;
double PIPS;
GetMultiplier();
if(OrderType() == OP_BUY) {PIPS=((OrderClosePrice()-OrderOpenPrice())/MarketInfo(OrderSymbol(),MODE_POINT)*multiplier);}
if(OrderType() == OP_SELL) {PIPS=((OrderOpenPrice()-OrderClosePrice())/MarketInfo(OrderSymbol(),MODE_POINT)*multiplier);}
SymbolsSummaries[index][NET_PIPS]+= PIPS;

//if (OrderOpenTime()<sdate)sdate=OrderOpenTime();
//if (OrderOpenTime()>edate)edate=OrderOpenTime();

      profit=OrderProfit()+OrderCommission()+OrderSwap();
      if(profit>=0)SymbolsSummaries[index][GPROFIT]+=profit;
      else SymbolsSummaries[index][GLOSS]+=profit;
      SymbolsSummaries[index][PROFIT]=SymbolsSummaries[index][GPROFIT]+SymbolsSummaries[index][GLOSS];
      if(type==OP_BUY)
        {
         SymbolsSummaries[index][BUY_LOTS]+=OrderLots();
         SymbolsSummaries[index][BUY_ORDERS]++;
        }
      if(type==OP_SELL)
        {
         SymbolsSummaries[index][SELL_LOTS]+=OrderLots();
         SymbolsSummaries[index][SELL_ORDERS]++;
        }
        
        SymbolsSummaries[index][DEALS]=SymbolsSummaries[index][SELL_ORDERS]+SymbolsSummaries[index][BUY_ORDERS];
     }
//----
   total=0;
   Tpl=0;Tpips=0;Tdeals=0;Tprofit=0;Tloss=0;
   for(i=0; i<SymbolsTotal; i++)
     {
      if(SymbolsSummaries[i][DEALS]>0) total++;
      Tpl+=SymbolsSummaries[i][PROFIT];
      Tpips+=SymbolsSummaries[i][NET_PIPS];
      Tdeals+=SymbolsSummaries[i][DEALS];
      Tprofit+=SymbolsSummaries[i][GPROFIT];
      Tloss+=SymbolsSummaries[i][GLOSS];
     }
//----
   return(total);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int SymbolsIndex(string SymbolName)
  {
   bool found=false;
//----
   for(int i=0; i<SymbolsTotal; i++)
     {
      if(SymbolName==Symbols[i])
        {
         found=true;
         break;
        }
     }
//----
   if(found) return(i);
   if(SymbolsTotal>=SYMBOLS_MAX) return(-1);
//----
   i=SymbolsTotal;
   SymbolsTotal++;
   Symbols[i]=SymbolName;
   SymbolsSummaries[i][DEALS]=0;
   SymbolsSummaries[i][BUY_LOTS]=0;
   SymbolsSummaries[i][BUY_ORDERS]=0;
   SymbolsSummaries[i][SELL_LOTS]=0;
   SymbolsSummaries[i][SELL_ORDERS]=0;
   SymbolsSummaries[i][TOTAL_LOTS]=0;
   SymbolsSummaries[i][GPROFIT]=0;
   SymbolsSummaries[i][GLOSS]=0;
   SymbolsSummaries[i][PROFIT]=0;
   SymbolsSummaries[i][NET_PIPS]=0;
//----
   return(i);
  }
//+------------------------------------------------------------------+
void SortTables(string &a[])
{
string tmp;
double tmp0,tmp1,tmp2,tmp3,tmp4,tmp5,tmp6,tmp7,tmp8,tmp9;
  for (int i = 0; i < SymbolsTotal; i++) 
  {
    for (int j = i + 1; j < SymbolsTotal; j++) 
    {
      if( a[i]>a[j] ) 
      {
        tmp = a[i];
        tmp0=SymbolsSummaries[i][0];
        tmp1=SymbolsSummaries[i][1];
        tmp2=SymbolsSummaries[i][2];
        tmp3=SymbolsSummaries[i][3];
        tmp4=SymbolsSummaries[i][4];
        tmp5=SymbolsSummaries[i][5];
        tmp6=SymbolsSummaries[i][6];
        tmp7=SymbolsSummaries[i][7];
        tmp8=SymbolsSummaries[i][8];
        tmp9=SymbolsSummaries[i][9];
        a[i] = a[j];
        SymbolsSummaries[i][0]=SymbolsSummaries[j][0];
        SymbolsSummaries[i][1]=SymbolsSummaries[j][1];
        SymbolsSummaries[i][2]=SymbolsSummaries[j][2];
        SymbolsSummaries[i][3]=SymbolsSummaries[j][3];
        SymbolsSummaries[i][4]=SymbolsSummaries[j][4];
        SymbolsSummaries[i][5]=SymbolsSummaries[j][5];
        SymbolsSummaries[i][6]=SymbolsSummaries[j][6];
        SymbolsSummaries[i][7]=SymbolsSummaries[j][7];
        SymbolsSummaries[i][8]=SymbolsSummaries[j][8];
        SymbolsSummaries[i][9]=SymbolsSummaries[j][9];
        a[j] = tmp;
        SymbolsSummaries[j][0]=tmp0;
        SymbolsSummaries[j][1]=tmp1;
        SymbolsSummaries[j][2]=tmp2;
        SymbolsSummaries[j][3]=tmp3;
        SymbolsSummaries[j][4]=tmp4;
        SymbolsSummaries[j][5]=tmp5;
        SymbolsSummaries[j][6]=tmp6;
        SymbolsSummaries[j][7]=tmp7;
        SymbolsSummaries[j][8]=tmp8;
        SymbolsSummaries[j][9]=tmp9;

      }
    }
  }
}
//+------------------------------------------------------------------+
void DrawTotals()
{
int windex=WindowFind(Name);
string name;
//ObjectDelete("Totals-OPENdeals");ObjectDelete("Totals-OPENprofit");ObjectDelete("Totals-OPENloss");ObjectDelete("Totals-OPENnetpl");ObjectDelete("Totals-OPENpips");
ObjectDelete("Totals-TXT");ObjectDelete("Totals-Deals");ObjectDelete("Totals-Profit");ObjectDelete("Totals-Loss");ObjectDelete("Totals-PL");
ObjectDelete("Totals-pips");ObjectDelete("From-Date-text");ObjectDelete("From-Date");ObjectDelete("To-Date-text");ObjectDelete("To-Date");
         int y=RowSpacing*(Lines+3)+3;
         name="Totals-TXT";
         if(ObjectCreate(name,OBJ_LABEL,windex,0,0))
           {
            ObjectSet(name,OBJPROP_XDISTANCE,Shifts[0]);
            ObjectSet(name,OBJPROP_YDISTANCE,y);
            ObjectSetText(name,"TOTALS:",9,"Arial",HeaderColor);
           }
         name="Totals-Deals";
         if(ObjectCreate(name,OBJ_LABEL,windex,0,0))
           {
            ObjectSet(name,OBJPROP_XDISTANCE,Shifts[1]);
            ObjectSet(name,OBJPROP_YDISTANCE,y);
            ObjectSetText(name,DoubleToStr(Tdeals,0),9,"Arial",HeaderColor);
           }//---- remove lines
         name="Totals-Profit";
         if(ObjectCreate(name,OBJ_LABEL,windex,0,0))
           {
            ObjectSet(name,OBJPROP_XDISTANCE,Shifts[7]);
            ObjectSet(name,OBJPROP_YDISTANCE,y);
            ObjectSetText(name,DoubleToStr(Tprofit,2),9,"Arial",HeaderColor);
           }//---- remove lines 
         name="Totals-Loss";
         if(ObjectCreate(name,OBJ_LABEL,windex,0,0))
           {
            ObjectSet(name,OBJPROP_XDISTANCE,Shifts[8]);
            ObjectSet(name,OBJPROP_YDISTANCE,y);
            ObjectSetText(name,DoubleToStr(Tloss,2),9,"Arial",HeaderColor);
           }//---- remove lines              
         name="Totals-PL";
         if(ObjectCreate(name,OBJ_LABEL,windex,0,0))
           {
            ObjectSet(name,OBJPROP_XDISTANCE,Shifts[9]);
            ObjectSet(name,OBJPROP_YDISTANCE,y);
            ObjectSetText(name,DoubleToStr(Tpl,2),9,"Arial",HeaderColor);
           }//---- remove lines
         name="Totals-pips";
         if(ObjectCreate(name,OBJ_LABEL,windex,0,0))
           {
            ObjectSet(name,OBJPROP_XDISTANCE,Shifts[10]);
            ObjectSet(name,OBJPROP_YDISTANCE,y);
            ObjectSetText(name,DoubleToStr(Tpips,0),9,"Arial",HeaderColor);
           }//---- remove lines
         int x=10;
         name="From-Date-text";
         if(ObjectCreate(name,OBJ_LABEL,windex,0,0))
           {
            ObjectSet(name,OBJPROP_XDISTANCE,x);
            ObjectSet(name,OBJPROP_YDISTANCE,y+RowSpacing);
            ObjectSetText(name,"This Report is From:",9,"Arial",HeaderColor);
           }//---- remove lines
         name="From-Date";
         if(ObjectCreate(name,OBJ_LABEL,windex,0,0))
           {
            ObjectSet(name,OBJPROP_XDISTANCE,x+120);
            ObjectSet(name,OBJPROP_YDISTANCE,y+RowSpacing);
            ObjectSetText(name,TimeToStr(sdate,TIME_DATE),9,"Arial",HeaderColor);
           }//---- remove lines 
         name="To-Date-text";
         if(ObjectCreate(name,OBJ_LABEL,windex,0,0))
           {
            ObjectSet(name,OBJPROP_XDISTANCE,x+190);
            ObjectSet(name,OBJPROP_YDISTANCE,y+RowSpacing);
            ObjectSetText(name,"Until:",9,"Arial",HeaderColor);
           }//---- remove lines 
         name="To-Date";
         if(ObjectCreate(name,OBJ_LABEL,windex,0,0))
           {
            ObjectSet(name,OBJPROP_XDISTANCE,x+230);
            ObjectSet(name,OBJPROP_YDISTANCE,y+RowSpacing);
            ObjectSetText(name,TimeToStr(edate,TIME_DATE),9,"Arial",HeaderColor);
           }//---- remove lines 
    }
void GetMultiplier()
{
   int digits=MarketInfo(OrderSymbol(),MODE_DIGITS);
   if(digits == 2 || digits == 4)   multiplier = 1;
   if(digits == 3 || digits == 5)   multiplier = 0.1;
   if(digits == 6)                  multiplier = 0.01;  
   if(digits == 7)                  multiplier = 0.001;  

}