//+------------------------------------------------------------------+
//|                                   AccountEquityAnalyzer v1.00    |
//|                                   Copyright © 2007, Project1972  |
//|        Property of  http://www.forex-tsd.com ELITE section ONLY  |
//|                                                  Modify by myst  |
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
//|                      This indicator is based on:                 |
//|                                                  i-BalEq_v1.mq4  |
//|                                          Ким Игорь В. aka KimIV  |
//|                                             http://www.kimiv.ru  |
//+------------------------------------------------------------------+
#property copyright "Project1972"
#property link    "http://www.forex-tsd.com"
//
#property indicator_separate_window
#property indicator_buffers 3
#property indicator_color1 Red
#property indicator_width1 2
#property indicator_color2 Lime
#property indicator_width2 1
#property indicator_color3 LightBlue
#property indicator_width3 2
// +------------------------------------------------------------------+
// |  Input parameters                                                |
// +------------------------------------------------------------------+
  
extern double StartingDeposit           =50000;
extern int    MinutesScreenshootInterval=120;
extern bool ScreenShotFlg = false;
//        
// 
int ImageTime=0;
double dBuf0[], dBuf1[], dBuf2[];
double InitialDeposit=0,PercentDD,MaxPercentDD,PercentDD2;
double MaxDrawdown,Balance,Equity,DW,StartingTime,MaxEquity,MaxBalance,Adr,Afm;
double LowEquity = 999999; 
double LowBalance = 999999;
double LowAdr = 999999;
double LowAfm = 999999; 
double Drawdown,WorstDrawdown,GDrawdownTime;
string MaxDD,MaxBL,MaxEQ,MaxEQAll,LowEQAll,MaxBLAll,LowBLAll,GMaxDrawdown,DrawDownAnalizer,DrawdownTime,GMaxPercentDD,AdrAll,LowAdrAll,AfmAll,LowAfmAll;
string TextDrawDownTime,PercentDDT,ROIText,growtext;
//------- 
int    oob[];      // 
int    oty[];      // 
double olo[];      // 
string osy[];      // 
double oop[];      // 
int    ocb[];      // 
double ocp[];      // 
double osw[];      // 
double opr[];      // 
//+------------------------------------------------------------------+
//|  Custom indicator initialization function                        |
//+------------------------------------------------------------------+
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");

  }

void deinit()
{
 ObjectDelete("B_object");
 ObjectDelete("E_object");
 ObjectDelete("PL_object");
 ObjectDelete("ME_object");
 ObjectDelete("LE_object");
 ObjectDelete("Adr_object");
 ObjectDelete("LowAdr_object");
 ObjectDelete("Afm_object");
 ObjectDelete("LowAfm_object");
 ObjectDelete("Time_object");
 ObjectDelete("Time_DD");
 ObjectDelete("note");
 ObjectDelete("DDPercent");
 ObjectDelete("DDPercent2");
 ObjectDelete("ROI");
 ObjectDelete("ROI2");
 ObjectDelete("ROI3");
 ObjectDelete("ROI4");
 ObjectDelete("ROI5");
 ObjectDelete("ROI6");
 return(0);
}
//+------------------------------------------------------------------+
//|  Custom indicator iteration function                             |
//+------------------------------------------------------------------+
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 = StartingDeposit; 
       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];
         }
       dBuf2[i] = StartingDeposit;
       dBuf0[i] = b;
       dBuf1[i] = b + 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; 
 } 
} 

}
//+------------------------------------------------------------------+
//|    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(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();     // прибыль
                 }
         }
     }
  }
//+------------------------------------------------------------------+

