//+----------------------------------+
//+------------- Info ---------------+
//+----------------------------------+
#property copyright "Copyright © 2017"
#property indicator_chart_window

extern color   InfoColor = Gold;
extern color   InfoLots = White;
extern int     SizeINFO = 10;
extern int     SizeProfit = 9;

int i;
double Mnoj;
color  ProfitColor;
//------------------------------------+
void start() {

   double profit=0, buy_profit=0, sell_profit=0, buy_orders=0, sell_orders=0;
   for(i=OrdersTotal()-1;i>=0;i--) if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)) {
      if(OrderType()<=1) profit+=OrderProfit()+OrderSwap()+OrderCommission();
      if(OrderType()==0) {buy_profit+=OrderProfit()+OrderSwap()+OrderCommission();buy_orders++;}
      if(OrderType()==1) {sell_profit+=OrderProfit()+OrderSwap()+OrderCommission();sell_orders++;}
      }
//==============================================================================================================================
//---------------------------+      
//    Information display    |
//---------------------------+      
   ObjectCreate("Spread", OBJ_LABEL, 0, 0, 0);
   ObjectSet("Spread", OBJPROP_CORNER, 1);
   ObjectSet("Spread", OBJPROP_XDISTANCE, 5);
   ObjectSet("Spread", OBJPROP_YDISTANCE, 15);
   ObjectSetText("Spread",StringConcatenate("Spread: ",DoubleToStr((Ask-Bid)*Mnoj,1)),SizeINFO,"Arial",InfoColor);
   ObjectCreate("Balance", OBJ_LABEL, 0, 0, 0);
   ObjectSet("Balance", OBJPROP_CORNER, 1);
   ObjectSet("Balance", OBJPROP_XDISTANCE, 5);
   ObjectSet("Balance", OBJPROP_YDISTANCE, 30);
   ObjectSetText("Balance",StringConcatenate("Balance: ",DoubleToStr(AccountBalance(),2)),SizeINFO,"Arial",InfoColor);
   ObjectCreate("Equity", OBJ_LABEL, 0, 0, 0);
   ObjectSet("Equity", OBJPROP_CORNER, 1);
   ObjectSet("Equity", OBJPROP_XDISTANCE, 5);
   ObjectSet("Equity", OBJPROP_YDISTANCE, 45);
   ObjectSetText("Equity",StringConcatenate("Equity: ",DoubleToStr(AccountEquity(),2)),SizeINFO,"Arial",InfoColor);
   ObjectCreate("FreeMargin", OBJ_LABEL, 0, 0, 0);
   ObjectSet("FreeMargin", OBJPROP_CORNER, 1);
   ObjectSet("FreeMargin", OBJPROP_XDISTANCE, 5);
   ObjectSet("FreeMargin", OBJPROP_YDISTANCE, 60);
   ObjectSetText("FreeMargin",StringConcatenate("FreeMargin: ",DoubleToStr(AccountFreeMargin(),2)),SizeINFO,"Arial",InfoColor);
//----------------------------------+
   if (buy_orders>0||sell_orders>0) {
   for(i=0; i<OrdersTotal(); i++) {
   int select=OrderSelect(i, SELECT_BY_POS);
   if(OrderType() == OP_BUY || OrderType() == OP_SELL) 
   if(profit<0) ProfitColor=Red;
   if(profit>0) ProfitColor=LimeGreen;
   if(profit==0)ProfitColor=DarkGray;
   ObjectCreate("Profit", OBJ_LABEL, 0, 0, 0);
   ObjectSet("Profit", OBJPROP_CORNER, 1);
   ObjectSet("Profit", OBJPROP_XDISTANCE, 5);
   ObjectSet("Profit", OBJPROP_YDISTANCE, 105);
   ObjectSetText("Profit",StringConcatenate("Profit: ",DoubleToStr(profit,2)),SizeProfit,"Arial",ProfitColor);
   if(buy_profit<0) ProfitColor=Red;
   if(buy_profit>0) ProfitColor=LimeGreen;
   if(buy_profit==0)ProfitColor=DarkGray;
   ObjectCreate("BUY Profit", OBJ_LABEL, 0, 0, 0);
   ObjectSet("BUY Profit", OBJPROP_CORNER, 1);
   ObjectSet("BUY Profit", OBJPROP_XDISTANCE, 5);
   ObjectSet("BUY Profit", OBJPROP_YDISTANCE, 120);
   ObjectSetText("BUY Profit",StringConcatenate("BUY Profit: ",DoubleToStr(buy_profit,2)),SizeProfit,"Arial",ProfitColor);
   ObjectCreate("BUY Lots", OBJ_LABEL, 0, 0, 0);
   ObjectSet("BUY Lots", OBJPROP_CORNER, 1);
   ObjectSet("BUY Lots", OBJPROP_XDISTANCE, 5);
   ObjectSet("BUY Lots", OBJPROP_YDISTANCE, 75);
   ObjectSetText("BUY Lots",StringConcatenate("BUY Lots: ",DoubleToStr(LotsBuy(),2)),SizeProfit,"Arial",InfoLots);
   if(sell_profit<0) ProfitColor=Red;
   if(sell_profit>0) ProfitColor=LimeGreen;
   if(sell_profit==0)ProfitColor=DarkGray;
   ObjectCreate("SELL Profit", OBJ_LABEL, 0, 0, 0);
   ObjectSet("SELL Profit", OBJPROP_CORNER, 1);
   ObjectSet("SELL Profit", OBJPROP_XDISTANCE, 5);
   ObjectSet("SELL Profit", OBJPROP_YDISTANCE, 135);
   ObjectSetText("SELL Profit",StringConcatenate("SELL Profit: ",DoubleToStr(sell_profit,2)),SizeProfit,"Arial",ProfitColor);
   ObjectCreate("SELL Lots", OBJ_LABEL, 0, 0, 0);
   ObjectSet("SELL Lots", OBJPROP_CORNER, 1);
   ObjectSet("SELL Lots", OBJPROP_XDISTANCE, 5);
   ObjectSet("SELL Lots", OBJPROP_YDISTANCE, 90);
   ObjectSetText("SELL Lots",StringConcatenate("SELL Lots: ",DoubleToStr(LotsSell(),2)),SizeProfit,"Arial",InfoLots);
      }
   }
   if (buy_orders==0 && sell_orders==0) {
   if(LastProfitCL(-1)<0) ProfitColor=Red;
   if(LastProfitCL(-1)>0) ProfitColor=LimeGreen;
   if(LastProfitCL(-1)==0)ProfitColor=DarkGray;
   ObjectCreate("Last Profit", OBJ_LABEL, 0, 0, 0);
   ObjectSet("Last Profit", OBJPROP_CORNER, 1);
   ObjectSet("Last Profit", OBJPROP_XDISTANCE, 5);
   ObjectSet("Last Profit", OBJPROP_YDISTANCE, 75);
   ObjectSetText("Last Profit",StringConcatenate("Last Profit: ",DoubleToStr(LastProfitCL(-1),2)),SizeProfit,"Arial",ProfitColor);
   }
   if (buy_orders==0 && sell_orders==0){
       ObjectDelete("Profit");
       ObjectDelete("BUY Profit");
       ObjectDelete("SELL Profit");
       ObjectDelete("BUY Lots");
       ObjectDelete("SELL Lots");
       }
   if (buy_orders>0||sell_orders>0) {
       ObjectDelete("Last Profit");
       }
   return;
//-----------------+
}//   Start END    |
//-----------------+
//<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
//+---------------------------------------------------------------------+
//|          Returns the total amount of lots of open orders            |
//+---------------------------------------------------------------------+
double LotsBuy() {
   double lotsB=0;
   for(i=0;i<OrdersTotal();i++)
    if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES) && OrderType()==OP_BUY) {
      if (OrderType()!=0) continue;
      lotsB+=OrderLots();
      }
  return(lotsB);
  }
double LotsSell() {
   double lotsS=0;
   for(i=0;i<OrdersTotal();i++)
    if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES) && OrderType()==OP_SELL) {
      if (OrderType()!=1) continue;
      lotsS+=OrderLots();
      }
  return(lotsS);
  }
//+--------------------------------------------------------------------------------------+
//    Returns the total profit in the currency of the deposit series of closed orders    |
//+--------------------------------------------------------------------------------------+
double LastProfitCL(int op=-1){ //"op" position (-1 any position)
  int cnt=0,cntb=0,cnts=0;
  datetime t=0,tb=0,ts=0;
  double lastprof=0.0,pr=0.0,prb=0.0,prs=0.0;

   for(i=OrdersHistoryTotal()-1;i>=0;i--) 
     if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY) && OrderType()<2) {
       if (OrderType()>1) continue;
       if(cnt==0 && OrderCloseTime()!=0) t=OrderCloseTime();
       if(OrderCloseTime()+PeriodSeconds()<t) break;
       if (op<0) pr=OrderProfit()+OrderCommission()+OrderSwap(); lastprof+=pr;cnt++;
       }
   for(i=OrdersHistoryTotal()-1;i>=0;i--) 
     if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY) && OrderType()==OP_BUY) {
       if (OrderType()!=1) continue;
       if(cntb==0 && OrderCloseTime()!=0) tb=OrderCloseTime();
       if(OrderCloseTime()+PeriodSeconds()<tb) break;
       if (OrderType()==op) prb=OrderProfit()+OrderCommission()+OrderSwap(); lastprof+=prb;cntb++;
       }
   for(i=OrdersHistoryTotal()-1;i>=0;i--) 
     if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY) && OrderType()==OP_SELL) {
       if (OrderType()!=1) continue;
       if(cnts==0 && OrderCloseTime()!=0) ts=OrderCloseTime();
       if(OrderCloseTime()+PeriodSeconds()<ts) break;
       if (OrderType()==op) prs=OrderProfit()+OrderCommission()+OrderSwap(); lastprof+=prs;cnts++;
       }
  return(lastprof);
   }
//-----------------------------------------------------------------------------------------------+
void init() {
   Mnoj=1/Point;
   return;
   }
//----------------------------+
void deinit() {
   Comment("");
   ObjectDelete("Spread");
   ObjectDelete("Balance");
   ObjectDelete("Equity");
   ObjectDelete("FreeMargin");
   ObjectDelete("Profit");
   ObjectDelete("BUY Profit");
   ObjectDelete("SELL Profit");
   ObjectDelete("BUY Lots");
   ObjectDelete("SELL Lots");
   ObjectDelete("Last Profit");
   return;
   }   
//<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
