//+------------------------------------------------------------------+
//|                                   AccountEquityAnalyzer v1.00    |
//|                                   Copyright © 2007, Project1972  |
//|        Property of  http://www.forex-tsd.com ELITE section ONLY  |
//| You are NOT ALLOWED to distribute this tool in any public forum  |
//+------------------------------------------------------------------+
//|                      This indicator is based on:                 |
//|                                                  i-BalEq_v1.mq4  |
//|                                          Ким Игорь В. aka KimIV  |
//|                                             http://www.kimiv.ru  |
//+------------------------------------------------------------------+
//|                                  Account Equity Analyzer v2.00   |
//|              Fixed Run Hight CPU + Redesign + Fixed Wrong Draw   |
//|                                    by Cmkm-2011[thhudu@yahoo.com]|
//+------------------------------------------------------------------+
#property copyright "Cmkm-2011[thhudu@yahoo.com]"
//
#property indicator_separate_window
#property indicator_buffers 3

#property indicator_color1 Yellow
#property indicator_width1 2
#property indicator_color2 DodgerBlue
#property indicator_width2 1
#property indicator_color3 LightBlue
#property indicator_width3 2
// +------------------------------------------------------------------+

extern double StartingDeposit =100;
extern int    CountedOrders = 30;

//
int ImageTime=0;
double dBuf0[], dBuf1[], dBuf2[];
double InitialDeposit=0,PercentDD,MaxPercentDD,PercentDD2;
double MaxDrawdown,Balance,Equity,DW,StartingTime;
double Drawdown,WorstDrawdown,GDrawdownTime;
string MaxDD,MaxBL,MaxEQ,GMaxDrawdown,DrawDownAnalizer,DrawdownTime,GMaxPercentDD;
string TextDrawDownTime,PercentDDT;
//------- 
int    oob[];      // 
int    oty[];      // 
double olo[];      // 
string osy[];      // 
double oop[];      // 
int    ocb[];      // 
double ocp[];      // 
double osw[];      // 
double opr[];      //
//+------------------------------------------------------------------+
void init()
  {
   IndicatorDigits(2);
//----
   SetIndexBuffer(0, dBuf0);
   SetIndexLabel (0, "Balance");
   SetIndexStyle (0, DRAW_LINE);
//----
   SetIndexBuffer(1, dBuf1);
   SetIndexLabel (1, "Equity");
   SetIndexStyle (1, DRAW_LINE);
//----
   SetIndexBuffer(2, dBuf2);
   SetIndexLabel (2, "Deposit");
   SetIndexStyle (2, DRAW_LINE);
    
   IndicatorShortName("Account Equity Analyzer V2 | ");
   
   GMaxDrawdown=AccountNumber()+"_MaxDrawdown";
   if (!GlobalVariableCheck(GMaxDrawdown)) GlobalVariableSet(GMaxDrawdown,0);
   GMaxPercentDD=AccountNumber()+"_GMaxPercentDD";
   if (!GlobalVariableCheck(GMaxPercentDD)) GlobalVariableSet(GMaxPercentDD,0);
   ImageTime=TimeCurrent()+60;
  }
//+------
void deinit()
{
 ObjectDelete("B_object");
 ObjectDelete("E_object");
 ObjectDelete("Deposit");
 ObjectDelete("PL_object");
 ObjectDelete("note1");
 ObjectDelete("DDPercent2");
 return(0);
}
//+----------------------Sum Account Profit--------------------------+

  double Prof( )
  {
  double p=0;
  int    i, k=OrdersHistoryTotal();
  for    (i=OrdersHistoryTotal()-CountedOrders; i<k; i++) { //i=0 : counting orders from begin of history
  if     (OrderSelect(i, SELECT_BY_POS, MODE_HISTORY)) {
  if     (0<OrderCloseTime()&&OrderComment()!="Deposit") {  // not calculated order which report deposit
         p+=OrderProfit()+OrderCommission()+OrderSwap();
     } }   }
  return (p);
  }
         
//+------------------------------------------------------------------+
void start()
  {
   double b, e, p,t;
   int    i, j, k;
//----
   ReadDeals();
 	 if(oob[0] < 0) 
 	     return;
 	 k = ArraySize(oob);
//----
   for(i = Bars; i >= oob[0]; i--)
     {
       dBuf0[i] = EMPTY_VALUE;
       dBuf1[i] = EMPTY_VALUE;
     }
   for(i = oob[0]; i >= 0; i--)
     {   
       b =  AccountBalance();
       e = 0;
       for(j = 0; j < k; j++)
         {
           if(i <= oob[j] && i >= ocb[j])
             {
               p = MarketInfo(osy[j], MODE_POINT);
               t = MarketInfo(osy[j], MODE_TICKVALUE);
               if(t == 0) 
                   t = 10;
               if(p == 0) 
                   if(StringFind(osy[j], "JPY") < 0) 
                       p = 0.0001; 
                   else 
                       p=0.01;
               if(oty[j] == OP_BUY) 
                   e += (iClose(osy[j], 0, i) - oop[j]) / p*olo[j]*t;
               else 
                   e += (oop[j] - iClose(osy[j], 0, i)) / p*olo[j]*t;
             } 
           else  
               if(i <= ocb[j]) 
                    b+= osw[j] +opr[j];
         }
         
    int d;         
        if (OrderComment()=="Deposit")
         {d=OrderProfit();}else{d=0;}
         
       dBuf2[i] = StartingDeposit;
       dBuf0[i] = b-Prof();
       dBuf1[i] = b-Prof() + e ;
     }
int OrderOpen;     
for (i = 0; i <= OrdersHistoryTotal(); i++) 
{ 
 OrderSelect(i,SELECT_BY_POS,MODE_HISTORY); 
 OrderOpen=OrderOpenTime();
 if(StartingTime==0 && OrderOpen>StartingTime)
   { StartingTime=OrderOpen; } 
} 
//---------------------  
WorstDrawdown=GlobalVariableGet(GMaxDrawdown);
GDrawdownTime=GlobalVariableGet(DrawdownTime);
MaxPercentDD=GlobalVariableGet(GMaxPercentDD);
Drawdown=AccountEquity()-AccountBalance();
if (Drawdown<WorstDrawdown)
 {
  WorstDrawdown=Drawdown;
  GDrawdownTime=CurTime();
  GlobalVariableSet(GMaxDrawdown,WorstDrawdown);
  GlobalVariableSet(DrawdownTime,GDrawdownTime);
 }
PercentDD=(MathAbs(WorstDrawdown)/AccountBalance())*100;
if (PercentDD>MaxPercentDD)
 {
  MaxPercentDD=PercentDD;
  GlobalVariableSet(GMaxPercentDD,MaxPercentDD);
 }
if (WorstDrawdown<MaxDrawdown)
  {  MaxDrawdown=WorstDrawdown; } 
PercentDD2=(MathAbs(MaxDrawdown)/AccountBalance())*100;
if (PercentDD2>MaxPercentDD)MaxPercentDD=PercentDD2;

MaxBL="Balance = $ "+DoubleToStr(AccountBalance(),1);
MaxEQ="Equity     = $ "+DoubleToStr(AccountEquity(),1);
MaxDD="Max DrawDown = $ "+DoubleToStr(MaxDrawdown,1);
PercentDDT="( "+DoubleToStr(MaxPercentDD,2)+"% )";

setLabel("B_object",MaxBL,Yellow,0,5,15,false,12,"Arial");
setLabel("E_object",MaxEQ,DodgerBlue,0,5,33,false,12,"Arial");
setLabel("Deposit","Deposit  = $ "+DoubleToStr(StartingDeposit,0),LightBlue,0,5,51,false,12,"Arial");
setLabel("PL_object",MaxDD,Red,0,5,69,false,12,"Arial");
setLabel("DDPercent2",PercentDDT,Red,0,200,69,false,12,"Arial");
setLabel("note1","NOTE: Graph calculation is not reliable for the last bar",Lime,0,5,87,false,9,"Arial");
}
//+------------------------------------------------------------------+
//|    ReadDeals                                                     |
//+------------------------------------------------------------------+
void ReadDeals()
  {
   ArrayResize(oob, 0);
   ArrayResize(oty, 0);
   ArrayResize(olo, 0);
   ArrayResize(osy, 0);
   ArrayResize(oop, 0);
   ArrayResize(ocb, 0);
   ArrayResize(ocp, 0);
   ArrayResize(osw, 0);
   ArrayResize(opr, 0);
   int h = 0, i, k;
   int ord = OrdersHistoryTotal();
   int indexOrder ;
   
  //Order
//----
  //CountedOrders = OrdersHistoryTotal() - indexOrder;
   
   int start = OrdersHistoryTotal() - CountedOrders; 
   for(i = start; i < OrdersHistoryTotal(); i++)
     {
       if(OrderSelect(i, SELECT_BY_POS, MODE_HISTORY))
         {
               if(OrderType() == OP_BUY || OrderType() == OP_SELL)
                 {
                   k = ArraySize(oob);
                   ArrayResize(oob, k + 1);
                   ArrayResize(oty, k + 1);
                   ArrayResize(olo, k + 1);
                   ArrayResize(osy, k + 1);
                   ArrayResize(oop, k + 1);
                   ArrayResize(ocb, k + 1);
                   ArrayResize(ocp, k + 1);
                   ArrayResize(osw, k + 1);
                   ArrayResize(opr, k + 1);
                   oob[k] = iBarShift(NULL, 0, OrderOpenTime()); 
                   oty[k] = OrderType();       // тип
                   olo[k] = OrderLots();       // лот
                   osy[k] = OrderSymbol();     // инструмент
                   oop[k] = OrderOpenPrice();  // цена открытия
                   ocb[k] = iBarShift(NULL, 0, OrderCloseTime());
                   ocp[k]=OrderClosePrice();   // цена закрытия
                   osw[k]=OrderSwap();         // своп
                   opr[k]=OrderProfit();       // прибыль
                 }
         }
     }
   h = OrdersTotal();
//----
   for(i = 0; i < h; i++)
     {
       if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
         {
               if(OrderType() == OP_BUY || OrderType() == OP_SELL)
                 {
                   k = ArraySize(oob);
                   ArrayResize(oob, k + 1);
                   ArrayResize(oty, k + 1);
                   ArrayResize(olo, k + 1);
                   ArrayResize(osy, k + 1);
                   ArrayResize(oop, k + 1);
                   ArrayResize(ocb, k + 1);
                   ArrayResize(ocp, k + 1);
                   ArrayResize(osw, k + 1);
                   ArrayResize(opr, k + 1);
                   oob[k] = iBarShift(NULL, 0, OrderOpenTime()); 
                   oty[k] = OrderType();       // тип
                   olo[k] = OrderLots();       // лот
                   osy[k] = OrderSymbol();     // инструмент
                   oop[k] = OrderOpenPrice();  // цена открытия
                   ocb[k] = 0;                 // номер бара закрытия
                   ocp[k] = 0;                 // цена закрытия
                   osw[k] = OrderSwap();       // своп
                   opr[k] = OrderProfit();     // прибыль
                 }
         }
     }
  }
//+------------------------------------------------------------------+
void setLabel(string name, string text, color col, int corner,
            int x, int y, bool back = false, int fontsize = 12, 
            string fontname = "MS Sans Serif") {
   if (ObjectFind(name)==-1){
      // aaa
      ObjectCreate(name, OBJ_LABEL, WindowOnDropped(),1, 0, 0);
      ObjectSetText(name, text, fontsize, fontname, col);
      ObjectSet(name, OBJPROP_CORNER, corner);
      ObjectSet(name, OBJPROP_BACK,back);
      ObjectSet(name, OBJPROP_XDISTANCE, x);
      ObjectSet(name, OBJPROP_YDISTANCE, y);          
   }else{
      ObjectSetText(name, text, fontsize, fontname, col);
      ObjectSet(name, OBJPROP_CORNER, corner);
      ObjectSet(name, OBJPROP_BACK,back);
      ObjectSet(name, OBJPROP_XDISTANCE, x);
      ObjectSet(name, OBJPROP_YDISTANCE, y); 
   }  
}