iBalans_v2





//+------------------------------------------------------------------+
//|                           iBalans_v2.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+"("+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, cnt=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[cnt][0]=OrderCloseTime();
            htm[cnt][1]=OrderTicket();
            cnt++;
         }
   ArrayResize(htm,cnt);
   ArraySort(htm);
   mxl=sbalans;
   mnl=sbalans;
   iB[0]=sbalans;
   int limit=Bars/2;
   if(cnt<limit) limit=cnt;
   j=2;
   for(i=1; i<=limit; i++)
   {
      OrderSelect(htm[cnt-i][1],SELECT_BY_TICKET);
      cp=OrderProfit()+OrderCommission()+OrderSwap();
      iB[j]=iB[j-2]-cp;
      iB[j-1]=(iB[j-2]+iB[j])/2;
      if(cp>=0) { pt++; gp+=cp;} else { lt++; gl+=-cp;}
      if(iB[j]>mxl) mxl=iB[j];
      if(iB[j]<mnl) mnl=iB[j];
      if(OrderSymbol()==Symbol())
      {
         iS[j-1]=iB[j-1];
         iS[j-2]=iB[j-2];
         iS[j]=iB[j];
         sb+=cp;
         if(cp>=0) { spt++; sgp+=cp;} else { slt++; sgl+=-cp;}
         st++;
      } else { iS[j]=EMPTY_VALUE; iS[j-1]=EMPTY_VALUE;}
      j+=2;
   }
   sbalans=iB[j-2];   
   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+"("+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 "+cnt+"     Profit Trades "+pt+"     Loss Trades "+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 "+st+"     Profit Trades "+spt+"    Loss Trades "+slt);
//----
   return(0);
}
//+------------------------------------------------------------------+ 
void show(int line, color col, string str)
{
   string sobj=names+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+i);
   return(0);
}
//+------------------------------------------------------------------+ 
 



Sample





Analysis



Market Information Used:



Indicator Curves created:


Implements a curve of type DRAW_LINE
Implements a curve of type DRAW_NONE

Indicators Used:



Custom Indicators Used:

Order Management characteristics:
Checks for the total of closed orders

Other Features: