//+------------------------------------------------------------------+
//|                           iBalans.mq4                            |
//+------------------------------------------------------------------+
#property  copyright "Yurich"
#property  link      ""
//---- indicator settings
#property  indicator_separate_window
#property  indicator_buffers 3
#property  indicator_color1  Green
#property  indicator_color2  Gold
#property  indicator_width1  2
#property  indicator_width2  2
//---- indicator parameters
extern int Days=0;
extern bool Show_info=true;
//---- indicator buffers
double     iB[];
double     iS[];
double     iT[];
//---- indicator var
int wh;
datetime prevtime;
double prevbalans,midlevel,minlevel,maxlevel;
#define names "iBalans_"
//+------------------------------------------------------------------+
int init()
  {
   SetIndexBuffer(0,iB);
   SetIndexBuffer(1,iS);
   SetIndexBuffer(2,iT);
   SetIndexStyle(0,DRAW_LINE);
   SetIndexStyle(1,DRAW_LINE);
   SetIndexStyle(2,DRAW_NONE);
   IndicatorDigits(2);
   IndicatorShortName(names+"("+(string)Days+")");
   SetIndexLabel(0,"Total balans");
   SetIndexLabel(1,NULL);
   SetIndexLabel(2,NULL);
//----
   prevbalans=0;
   midlevel=0;
   minlevel=0;
   maxlevel=0;
   if(Days<0) Days=0;
   return(0);
  }
//+------------------------------------------------------------------+
int start()
  {
   int i,j=0,pt=0,lt=0,st=0,spt=0,slt=0;
   int htm[][2];
   double cp,mnl,mxl;
   double sb=0.0,gp=0.0,gl=0.0,sgp=0.0,sgl=0.0;
   datetime now=iTime(NULL,0,0);
   if(now!=prevtime)
     {
      prevtime=now;
      prevbalans=0;
     }
   double sbalans=AccountBalance();
   if(prevbalans==sbalans)return(0);
   prevbalans=sbalans;
   datetime stday=iTime(Symbol(),PERIOD_D1,0)-Days*PERIOD_D1*60;
//----
   int oht=OrdersHistoryTotal();
   ArrayResize(htm,oht);
   for(i=0; i<oht; i++)
      if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY))
         if(OrderType()<=OP_SELL && (OrderCloseTime()>stday || Days==0))
           {
            htm[j][0]=OrderCloseTime();
            htm[j][1]=OrderTicket();
            j++;
           }
   ArrayResize(htm,j);
   if(ArraySize(htm)==0)
     {
     Print("Trade history is empty.");
      return(0);
     }
   ArraySort(htm);
   
   mxl=sbalans;
   mnl=sbalans;
   iB[0]=sbalans;
   for(i=1; i<=j; i++)
     {
      OrderSelect(htm[j-i][1],SELECT_BY_TICKET);
      cp=OrderProfit()+OrderCommission()+OrderSwap();
      iB[i]=iB[i-1]-cp;
      if(cp>=0) { pt++; gp+=cp;} else { lt++; gl+=-cp;}
      if(iB[i]>mxl) mxl=iB[i];
      if(iB[i]<mnl) mnl=iB[i];
      if(OrderSymbol()==Symbol())
        {
         iS[i-1]=iB[i-1];
         iS[i]=iB[i];
         sb+=cp;
         if(cp>=0) { spt++; sgp+=cp;} else { slt++; sgl+=-cp;}
         st++;
        }
      else iS[i]=EMPTY_VALUE;
     }
   sbalans=iB[i-1];
   if(sbalans!=midlevel)
     {
      midlevel=sbalans;
      SetLevelValue(0,midlevel);
     }
   if(mxl!=maxlevel)
     {
      maxlevel=MathMax(mxl,midlevel);
      SetLevelValue(1,maxlevel);
      iT[0]=maxlevel+0.1*(maxlevel-minlevel);
     }
   if(mnl!=minlevel)
     {
      minlevel=MathMin(mnl,midlevel);
      SetLevelValue(2,minlevel);
      iT[1]=minlevel-0.1*(maxlevel-minlevel);
     }
//----
   if(!Show_info) return(0);
   wh=WindowFind(names+"("+(string)Days+")");
   show(0,Lime,"Total Balans "+DoubleToStr(AccountBalance(),2));
   show(1,Lime,"Gross Profit "+DoubleToStr(gp,2)
        +"  Gross Loss "+DoubleToStr(gl,2)
        +"  Net Profit "+DoubleToStr(gp-gl,2));
   show(2,Lime,"Total Trades "+(string)j+"     Profit Trades "+(string)pt+"     Loss Trades "+(string)lt);
   show(3,Gold,"Net Profit "+Symbol()+" "+DoubleToStr(sb,2));
   show(4,Gold,"Gross Profit "+DoubleToStr(sgp,2)
        +"  Gross Loss "+DoubleToStr(sgl,2));
   show(5,Gold,Symbol()+" Trades "+(string)st+"     Profit Trades "+(string)spt+"    Loss Trades "+(string)slt);
//----
   return(0);
  }
//+------------------------------------------------------------------+ 
void show(int line,color col,string str)
  {
   string sobj=names+(string)line;
   int cw=ObjectFind(sobj);
   if(cw!=wh)
     {
      if(cw>=0) ObjectDelete(sobj);
      ObjectCreate(sobj,OBJ_LABEL,wh,0,0);
      ObjectSet(sobj,OBJPROP_CORNER,0);
      ObjectSet(sobj,OBJPROP_XDISTANCE,3);
      ObjectSet(sobj,OBJPROP_YDISTANCE,25+line*12);
     }
   ObjectSetText(sobj,str,8,"",col);
  }
//+------------------------------------------------------------------+ 
int deinit()
  {
   for(int i=0; i<=5; i++) ObjectDelete(names+(string)i);
   return(0);
  }
//+------------------------------------------------------------------+ 
