//+------------------------------------------------------------------+
//|                                   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]|
//+------------------------------------------------------------------+

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~|
//|                                   Account Equity Analyzer v2.3   |
//|                      I have update   main code from i-BalEq2.mq4 | 
//+-------------------------------------------------------------------------+
//|                                                           i-BalEq2.mq4  |
//|  01.10.2006  Draw balance and equity on Chart.                          |
//|                                                 Ким Игорь В. aka KimIV  |
//|                                                    http://www.kimiv.ru  |
//|  23.06.2007  Added TickValue,  spread and swap..                        |
//|                           Fix update orders faster      =Xupypr=        |
//+-------------------------------------------------------------------------+
//|  3-5-2012              + Draw much accurate + remove % risk      |
//|                                    by Cmkm-2012[thhudu@yahoo.com]|
//+------------------------------------------------------------------+
#property copyright "Cmkm-2012[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 Deposit =100;

bool   CurSymbolOnly = false;
//
double dBuf0[], dBuf1[], dBuf2[];
double Balance,Equity,DW,StartingTime;
string MaxBL,MaxEQ;
//------- 
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.3 | ");
   
  }
//+------
void deinit()
{
 ObjectDelete("B_object");
 ObjectDelete("E_object");
 ObjectDelete("Deposit");
 return(0);
}

//+----------------------Sum Account Profit--------------------------+
 
  double Prof(string sy="", int op=-1, int mn=-1, datetime dt=0)
  {
  double p=0;
  int    i, k=OrdersHistoryTotal();
  if     (sy=="0") sy=Symbol();
  for    (i=0; i<k; i++) {
  if     (OrderSelect(i, SELECT_BY_POS, MODE_HISTORY)) {
  if     ((OrderSymbol()==sy || sy=="") && (op<0 || OrderType()==op)) {
  if     (OrderType()==OP_BUY || OrderType()==OP_SELL) {
  if     (mn<0 || OrderMagicNumber()==mn) {
  if     (dt<OrderCloseTime()) {
         p+=OrderProfit()+OrderCommission()+OrderSwap();
  } } } } } }
  return (p);
  }    
//+------------------------------------------------------------------+
void start()
  {
 double b,e,p,s,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;
   }
   
b=AccountBalance();

   for (i=oob[0];i>=0;i--)
    {
e=0; 
  for (j=0;j<k;j++)
  {
   if (i==ocb[j] && ocp[j]!=0) 
       b+=osw[j]+opr[j];
   else if (i<=oob[j] && i>=ocb[j])
   {
    p=MarketInfo(osy[j],MODE_POINT);
    s=p*MarketInfo(osy[j],MODE_SPREAD);
    t=NormalizeDouble(TickValue(osy[j],i),2);
    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)-s)/p*olo[j]*t;
    if (osw[j]!=0) e+=(oob[j]-i)*(osw[j]/(oob[j]-ocb[j]));
   }
  }
//----
      dBuf2[i] = Deposit;
      if (Prof()<=0){   
              dBuf0[i] = b+MathAbs(Prof())  ;
              dBuf1[i] = b+MathAbs(Prof())+e;  }  
             else { 
                dBuf0[i] = b-MathAbs(Prof());
                dBuf1[i] = b-MathAbs(Prof())+e; } 
     }
//----
MaxBL="Balance = $ "+DoubleToStr(AccountBalance(),1);
MaxEQ="Equity     = $ "+DoubleToStr(AccountEquity(),1);

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(Deposit,1),LightBlue,0,5,51,false,12,"Arial");

}


//+-------TickValue----------------------------------------------+
double TickValue(string symbol, int bar)
{
 double value=MarketInfo(symbol,MODE_TICKVALUE);
 if (StringLen(symbol)!=6) return(value);
 double size=MarketInfo(symbol,MODE_LOTSIZE)*MarketInfo(symbol,MODE_TICKSIZE);
 if (StringSubstr(symbol,3,3)=="USD") return(value);
 else if (StringSubstr(symbol,0,3)=="USD") value=size/iClose(symbol,0,bar);
 else
 {
  string BQ=StringSubstr(symbol,0,3)+"USD";
  if (iClose(BQ,0,0)==0) BQ="USD"+StringSubstr(symbol,0,3);
  if (iClose(BQ,0,0)==0) return(value);
  if (StringSubstr(BQ,0,3)=="USD") value=size/iClose(BQ,0,bar)/iClose(symbol,0,bar);
  else value=size*iClose(BQ,0,bar)/iClose(symbol,0,bar);
 } 
 return(value);
}
//+----------------------------------------------------------------+
//+--------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=OrdersHistoryTotal(),i,k;

 for (i=0;i<h;i++)
 {
  if (OrderSelect(i,SELECT_BY_POS,MODE_HISTORY))
  {
   if (!CurSymbolOnly || OrderSymbol()==Symbol())
   {
    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 (!CurSymbolOnly || OrderSymbol()==Symbol())
   {
    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();                     // прибыль
    }
   }
  }
 }
}
//+---------setLabel--------------------------------------------+
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); 
   }  
}