//+------------------------------------------------------------------+
//|                                                      OrderBalans |
//|                               Copyright © 2012, Vladimir Hlystov |
//|                                         http://cmillion.narod.ru |
//+------------------------------------------------------------------+
#property copyright "Хлыстов Владимир"
#property link      "cmillion@narod.ru"
#property indicator_chart_window
extern color   WhiteColor  = Aquamarine;//цвет вывода информации
extern int     CORNER      = 1;     //если 4, то не выводить
extern color   NameColor   = Green;
extern color   BuyColor    = Blue;
extern color   SellColor   = Red;
extern bool    DrawLine    = true;
extern bool    DrawOrders  = true;
extern bool    DrawBalance = true;
//--------------------------------------------------------------------
double High_Win,Low_Win,balans,MaxLot,AFreeMargin,LINE [100][5],orders[100][10];
int    UG;
string DayTek,DayWe;
//--------------------------------------------------------------------
int deinit()
{
   ObjectDelete("_TIME_");
   ObjectDelete("_SHAPKA_");
   ObjectDelete("_BALANS_");
   ObjectDelete("_MARGA_");
   del("Bay");
   del("Sell");
   del("ОРДЕР");
}
//--------------------------------------------------------------------
int del(string str)
{
   for(int n=ObjectsTotal()-1; n>=0; n--) 
     {
      string Obj_Name=ObjectName(n);
      if (StringFind(Obj_Name,str,0) != -1)
      {
         ObjectDelete(Obj_Name);
      }
   }
   return;
}
//--------------------------------------------------------------------
int init()
{
   if (CORNER>3) CORNER=3;
   if (DrawBalance)
   {
      ObjectCreate("_BALANS_", OBJ_LABEL, 0, 0, 0);
      ObjectSet("_BALANS_", OBJPROP_CORNER, CORNER);      
      ObjectSet("_BALANS_", OBJPROP_XDISTANCE, 10 ); 
      ObjectCreate("_TIME_", OBJ_LABEL, 0, 0, 0);
      ObjectSet("_TIME_", OBJPROP_CORNER, CORNER);
      ObjectSet("_TIME_", OBJPROP_XDISTANCE, 10);
      ObjectCreate("_MARGA_", OBJ_LABEL, 0, 0, 0);
      ObjectSet("_MARGA_", OBJPROP_CORNER, CORNER);
      ObjectSet("_MARGA_", OBJPROP_XDISTANCE, 10 );
   }
   if (DrawOrders)
   {
      ObjectCreate("_SHAPKA_", OBJ_LABEL, 0, 0, 0);
      ObjectSet("_SHAPKA_", OBJPROP_CORNER, CORNER);      
      ObjectSet("_SHAPKA_", OBJPROP_XDISTANCE, 10);      
   }
   if (CORNER<2)
   {  UG=+10;
      ObjectSet("_TIME_", OBJPROP_YDISTANCE, 13);
      ObjectSet("_BALANS_", OBJPROP_YDISTANCE, 25);
      ObjectSet("_MARGA_", OBJPROP_YDISTANCE, 35);
      ObjectSet("_SHAPKA_", OBJPROP_YDISTANCE, 60);
   }
   else
   {  UG=-10;
      ObjectSet("_TIME_", OBJPROP_YDISTANCE, 5);
      ObjectSet("_BALANS_", OBJPROP_YDISTANCE, 15);
      ObjectSet("_MARGA_", OBJPROP_YDISTANCE, 25);
      ObjectSet("_SHAPKA_", OBJPROP_YDISTANCE, 150);
   }
return;
}   
//------------------------------------------------------------------------------- НАЧАЛО ПРОГРАММЫ 
int start()                                  
{
   Terminal();
   if (DrawBalance) 
   {
      DayTek = TimeToStr(TimeCurrent(),TIME_DATE);
      DayWe = DayWeek(TimeDayOfWeek(TimeCurrent()));
      ObjectSetText("_TIME_",StringConcatenate(DayWe," ",TimeToStr(TimeCurrent(),TIME_SECONDS)),8,"Arial",WhiteColor);
      ObjectSetText("_BALANS_",StringConcatenate("  БАЛАНС = ",DoubleToStr(AccountBalance( ),2)," | своб ",DoubleToStr(AFreeMargin,2)," | ",DoubleToStr(MaxLot,2)),8,"Arial",WhiteColor);
      ObjectSetText("_MARGA_",StringConcatenate("  balans = ",DoubleToStr(balans,2)),8,"Arial",Color(balans <0,Red,Lime));
      if (orders[0][0]==0) ObjectSetText("_MARGA_","",8,"Arial",0);
   }
   AFreeMargin = AccountFreeMargin();
   MaxLot = AFreeMargin/MarketInfo(Symbol(),MODE_MARGINREQUIRED);
   //-------------------------------------------------------- назначение лотов end
   if (orders[0][0] > 0) 
   {
      if (DrawLine)
      {
         string NameLine;
         for (int n=1; n<=orders[0][0]; n++)
         {
            if (orders[n][6]==OP_BUY) 
            {
               NameLine=StringConcatenate("Bay, Lot ",DoubleToStr(orders[n][5],2),"  Ticket ",DoubleToStr(orders[n][4],0));
               ObjectDelete(NameLine);
               ObjectCreate(NameLine,OBJ_TREND,0,orders[n][9],orders[n][1],Time[0],Bid);
               ObjectSet(NameLine, OBJPROP_COLOR,BuyColor); 
            }
            if (orders[n][6]==OP_SELL) 
            {
               NameLine=StringConcatenate("Sell, Lot ",DoubleToStr(orders[n][5],2),"  Ticket ",DoubleToStr(orders[n][4],0));
               ObjectDelete(NameLine);
               ObjectCreate(NameLine,OBJ_TREND,0,orders[n][9],orders[n][1],Time[0],Ask);
               ObjectSet(NameLine, OBJPROP_COLOR,SellColor);
            }
            ObjectSet(NameLine, OBJPROP_STYLE, STYLE_DOT);
            ObjectSet(NameLine, OBJPROP_RAY,   false);
         }
      }
   }
return;
}
//-------------------------------------------------------------------------------
string DayWeek(int day)
   {
      switch(day)
      {  case 1 : return("Понедельник ");      
         case 2 : return("Вторник ");      
         case 3 : return("Среда ");
         case 4 : return("Четверг ");      
         case 5 : return("Пятница ");      
         case 6 : return("Суббота ");
         case 7 : return("Воскресенье ");
         default: return("");}
   }
//-------------------------------------------------------------------------------
void Text(string Name, int X, int Y, string txt, color COLOR)
{
   ObjectDelete(Name);
   ObjectCreate(Name, OBJ_LABEL, 0, 0, 0);
   ObjectSet   (Name, OBJPROP_CORNER, CORNER);
   ObjectSet   (Name, OBJPROP_XDISTANCE, X);
   ObjectSet   (Name, OBJPROP_YDISTANCE, Y);
   ObjectSetText(Name,txt,8,"Arial",COLOR);
}
//-----------------------------------------------------------
int Terminal()
{
   int Kol=0,n=0,X_order,Ticket,tip;
   double Y_order;
   string N_order;
          
   ArrayInitialize(orders,0);
   if (ObjectFind("_SHAPKA_")==0)
   {  X_order = ObjectGet("_SHAPKA_", OBJPROP_XDISTANCE);
      Y_order = ObjectGet("_SHAPKA_", OBJPROP_YDISTANCE);
   }
   else {X_order = 10;Y_order=60+10*Kol;}
   /*for (int i=0; i<=100; i++)
   {
      ObjectDelete(StringConcatenate("ОРДЕР ",i));
   }*/
   del("ОРДЕР");
   balans=0;
   int SumPips;
   for (int i=0; i<OrdersTotal(); i++)
   {  if (OrderSelect(i, SELECT_BY_POS)==true)
      {  
         if (OrderSymbol()==Symbol())
         {  tip=OrderType();
            if (tip == OP_BUY  || tip == OP_SELL)
            {  
               Kol++;
               orders[Kol][1] = OrderOpenPrice();
               orders[Kol][2] = OrderStopLoss();
               orders[Kol][3] = OrderTakeProfit();
               orders[Kol][4] = OrderTicket();
               orders[Kol][5] = OrderLots();
               orders[Kol][6] = tip; 
               orders[Kol][7] = OrderMagicNumber();
               orders[Kol][9] = OrderOpenTime();
               //--------------------------------------------------------------------- Вывод текущих ордеров на экран
               if (DrawOrders)
               {
                  string текст,sl,tp;
                  if (tip == OP_BUY) 
                  {
                     текст = "Buy "; 
                     if (orders[Kol][2]!=0) sl = DoubleToStr((orders[Kol][2]-Bid)/Point,0); else sl = "-";
                     if (orders[Kol][3]!=0) tp = DoubleToStr((orders[Kol][3]-Ask)/Point,0); else tp = "-";  
                  }
                  else               
                  {
                     текст = "Sell ";
                     if (orders[Kol][2]!=0) sl = DoubleToStr((Ask-orders[Kol][2])/Point,0); else sl = "-";
                     if (orders[Kol][3]!=0) tp = DoubleToStr((Bid-orders[Kol][3])/Point,0); else tp = "-";  
                  }
                  
                  if (orders[Kol][6]==OP_BUY) orders[Kol][8] = NormalizeDouble((Bid-orders[Kol][1])/Point,0);// Прибыль в п Bay
                  else                   orders[Kol][8] = NormalizeDouble((orders[Kol][1]-Ask)/Point,0);// Прибыль в п Sell
                  SumPips+=orders[Kol][8];
                  balans=balans+OrderProfit();

                  color с = Color(orders[Kol][8] <0,Red,Lime);
                  Text(StringConcatenate("ОРДЕР 0",Kol), X_order+250,Y_order+UG*Kol, Kol, с);
                  Text(StringConcatenate("ОРДЕР 1",Kol), X_order+225,Y_order+UG*Kol, DoubleToStr(orders[Kol][8],0), с);
                  Text(StringConcatenate("ОРДЕР 2",Kol), X_order+195,Y_order+UG*Kol, текст, с);
                  Text(StringConcatenate("ОРДЕР 3",Kol), X_order+150,Y_order+UG*Kol, DoubleToStr(orders[Kol][4],0), с);
                  Text(StringConcatenate("ОРДЕР 4",Kol), X_order+100,Y_order+UG*Kol, DoubleToStr(orders[Kol][1],Digits), с);
                  Text(StringConcatenate("ОРДЕР 5",Kol), X_order+70, Y_order+UG*Kol, DoubleToStr(orders[Kol][5] ,2), с);
                  Text(StringConcatenate("ОРДЕР 6",Kol), X_order+53, Y_order+UG*Kol, sl, с);
                  Text(StringConcatenate("ОРДЕР 7",Kol), X_order+35, Y_order+UG*Kol, tp, с);
                  Text(StringConcatenate("ОРДЕР 8",Kol), X_order,    Y_order+UG*Kol, TimeToStr  (TimeCurrent()-orders[Kol][9],TIME_MINUTES), с);
               }
            }
         }
      }
   }
   orders[0][0] = Kol;
   if (DrawOrders)
   {
      if (Kol!=0) Text(StringConcatenate("ОРДЕР ",Kol),X_order+225,Y_order+UG*(Kol+1),DoubleToStr(SumPips,0),Color(SumPips<0,Red,Lime));
      //else ObjectDelete("ОРДЕР 0");
      if (orders[0][0]!=0)  
      {
         if (CORNER==1 || CORNER==3)  
         {
            ObjectSetText("_SHAPKA_","п    тип      ОРДЕР     ц откр   лот  SL  ТР  время",8,"Arial",WhiteColor);
         }
         else  
         {
            ObjectSetText("_SHAPKA_","время  ТР SL  лот     ц откр      ОРДЕР   тип    п",8,"Arial",WhiteColor);
         }
      }
      else ObjectSetText("_SHAPKA_"," ",8,"Arial",WhiteColor);
      ObjectSet("_SHAPKA_", OBJPROP_CORNER, CORNER);
   }
return;
}
//-------------------------------------------------------------------------------
color Color(bool P,color a,color b)
{
   if (P) return(a);
   else return(b);
}
//------------------------------------------------------------------

