//+------------------------------------------------------------------+
//|                                                  OrderBalans.mq4 |
//|                               Copyright © 2009, Vladimir Hlystov |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2009, Vladimir Hlystov"
#property link      "cmillion@narod.ru"
//--------------------------------------------------------------------
#property indicator_chart_window
extern color   WhiteColor          = Silver; //цвет ордеров
extern color   BlueColor           = Blue;   //цвет заголовка
extern color   LineBuyColor        = Red;    //цвет линий ордеров Buy
extern color   LineSellColor       = Blue;   //цвет линий ордеров Sell
extern int     corner              = 1;
extern int     STYLE               = 1;      //тип линий ордеров
extern bool    DelLineOrders       = false;
//--------------------------------------------------------------------
double Order[25][10];
//--------------------------------------------------------------------
int init()
{
   if (ObjectFind("NAME")!=0) StartDraw();
   return;
}   
//--------------------------------------------------------------------
int start()                                  
{
   string DayWeek = DayWeek(TimeDayOfWeek(CurTime()));
   string tekTime = TimeStr(CurTime(),1);
   ObjectSetText("time",DayWeek+" "+tekTime,8,"Arial",WhiteColor);
   double Maney = AccountFreeMargin();
   double MLot = Maney/MarketInfo(Symbol(),MODE_MARGINREQUIRED);
   CheckOrders();
   if (Order[0][0] > 0) LineOrders();
   double marga=0;
   for (int i=1; i<=Order[0][0]; i++) 
   {
      marga=marga+(Order[i][8]*Order[i][5])*MarketInfo(Symbol(),MODE_TICKVALUE);
   }
   ObjectSetText("balans","  БАЛАНС = "+DoubleToStr(AccountBalance( ),2)+
   " | своб "+DoubleToStr(Maney,2)+" | "+DoubleToStr(MLot,2),8,"Arial",WhiteColor);
   if (marga <0) 
   ObjectSetText("maney",StringConcatenate("  доход = ",DoubleToStr(marga,2)),8,"Arial",Red);
   if (marga >0) 
   ObjectSetText("maney",StringConcatenate("  доход = ",DoubleToStr(marga,2)),8,"Arial",Green);
   if (Order[0][0]==0) 
   ObjectSetText("maney","                ",8,"Arial",WhiteColor);
   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 LineOrders()
{
   string   NameLine;
   for (int n=1; n<=Order[0][0]; n++)
   {
      if (Order[n][6]== 1) NameLine="order Bay  "+DoubleToStr(Order[n][4],0);
      if (Order[n][6]==-1) NameLine="order Sell "+DoubleToStr(Order[n][4],0);
      ObjectDelete(NameLine);
      ObjectDelete(NameLine+" _");
      if (Order[n][6]== 1) {ObjectCreate(NameLine, OBJ_TREND, 0, Order[n][9],Order[n][1] ,Time[0],Bid);
      ObjectSet(NameLine,OBJPROP_COLOR,LineSellColor); 
      ObjectCreate(NameLine+" _",OBJ_ARROW,0,Time[0],Bid,0,0,0,0);}     
      if (Order[n][6]==-1) {ObjectCreate(NameLine, OBJ_TREND, 0, Order[n][9],Order[n][1] ,Time[0],Ask);
      ObjectSet(NameLine,OBJPROP_COLOR,LineBuyColor);
      ObjectCreate(NameLine+" _",OBJ_ARROW,0,Time[0],Ask,0,0,0,0);}
      ObjectSet(NameLine,OBJPROP_STYLE, STYLE);
      ObjectSet(NameLine,OBJPROP_RAY,   false);
      ObjectSet(NameLine+" _",OBJPROP_ARROWCODE,3);
      ObjectSet(NameLine+" _",OBJPROP_COLOR,WhiteColor);   
   }
}
//--------------------------------------------------------------------
int CheckOrders()
{
   int N_O=0,n=0,X_order,Ticket,tip;
   double Y_order;
   string N_order,txt;
   ArrayInitialize(Order,0);
   if (ObjectFind("NAME")==0)
   {  X_order = ObjectGet("NAME", OBJPROP_XDISTANCE);
      Y_order = ObjectGet("NAME", OBJPROP_YDISTANCE);
   }
   else {X_order = 10;Y_order=60+10*N_O;StartDraw();}
   for (int i=1; i<=25; i++)
   {  N_order="Order "+i;
      ObjectDelete(N_order);
   }
   for ( i=0; i<OrdersTotal(); i++)
   {  if((OrderSelect(i, SELECT_BY_POS)==true) && (OrderSymbol()==Symbol()))
      {  tip=OrderType();
         if (tip<2)                                                        // Bay или Sell
         {  
            N_O++;                                                         // Считаем общее количество ордеров
            Order[N_O][1] = NormalizeDouble(OrderOpenPrice() ,Digits);     // Курс открытия ордеров
            Order[N_O][2] = NormalizeDouble(OrderStopLoss() , Digits);     // SL 
            Order[N_O][3] = NormalizeDouble(OrderTakeProfit(),Digits);     // ТР 
            Order[N_O][4] = OrderTicket();                                 // Номер ордера
            Order[N_O][5] = OrderLots();                                   // Количество лотов
            if (tip==0) {Order[N_O][6] = 1;txt="Buy " ;}                   // Bay
            else        {Order[N_O][6] =-1;txt="Sell ";}                   // Sell
            Order[N_O][9] = OrderOpenTime();                               // время ордера
            if (Order[N_O][6]==1) 
            Order[N_O][8] = NormalizeDouble((Bid-Order[N_O][1])/Point,0);  // Прибыль в п Bay
            else                  
            Order[N_O][8] = NormalizeDouble((Order[N_O][1]-Ask)/Point,0);  // Прибыль в п Sell
            N_order="Order "+N_O;
            ObjectDelete(N_order);
            ObjectCreate(N_order, OBJ_LABEL, 0, 0, 0);
            ObjectSet   (N_order, OBJPROP_CORNER, corner );
            ObjectSet   (N_order, OBJPROP_XDISTANCE, X_order);
            ObjectSet   (N_order, OBJPROP_YDISTANCE, Y_order+10*N_O);
            ObjectSetText(N_order,StringConcatenate(
               "  " ,DoubleToStr(Order[N_O][8],0),                         //прибыль в п.
               "  " ,            txt,                                      //тип ордера
               "  " ,            Order[N_O][4],                            //номер ордера
               "   ",DoubleToStr(Order[N_O][1],Digits),                    //цена
               "  " ,DoubleToStr(Order[N_O][5] ,2),                        //лот
               "  " ,DoubleToStr((Bid-Order[N_O][2])/Point,0),             //SL
               "  " ,DoubleToStr((Bid-Order[N_O][3])/Point,0),             //TP
               "  " ,TimeStr(TimeCurrent()-Order[N_O][9],2)),8,"Arial",WhiteColor);
         }
      }
   }
   Order[0][0] = N_O;
   if (Order[0][0]!=0)  
   {
      if (Digits<3) ObjectSetText("NAME",
      "      п    тип      ОРДЕР     ц откр   лот  SL  ТР  время",8,"Arial",BlueColor);
      if (Digits==3)ObjectSetText("NAME",
      "  п    тип      ОРДЕР     ц откр   лот    SL    ТР   время",8,"Arial",BlueColor);
      if (Digits==4)ObjectSetText("NAME",
      "      п    тип      ОРДЕР     ц откр   лот  SL  ТР  время",8,"Arial",BlueColor);
      if (Digits>4) ObjectSetText("NAME",
      "  п    тип      ОРДЕР     ц откр     лот    SL    ТР  время",8,"Arial",BlueColor);
   }
   else ObjectSetText("NAME"," ",8,"Arial",BlueColor);
   ObjectSet("NAME", OBJPROP_CORNER, corner );
return;
}
//--------------------------------------------------------------------
string TimeStr(int taim,int tip)
{
   string sTaim;
   if (tip==1) 
   {
      //int YY=TimeYear(taim); // Year         
      int MN=TimeMonth(taim);  // Month                  
      int DD=TimeDay(taim);    // Day         
      if (DD<10) sTaim = StringConcatenate("0",DoubleToStr(DD,0));
      else sTaim = DoubleToStr(DD,0);
      sTaim = StringConcatenate(sTaim,"/");
      if (MN<10) sTaim = StringConcatenate(sTaim,"0"+DoubleToStr(MN,0));
      else sTaim = StringConcatenate(sTaim,DoubleToStr(MN,0));
      sTaim = StringConcatenate(sTaim," ");
   }
   int HH=TimeHour(taim);     // Hour                  
   int MM=TimeMinute(taim);   // Minute   
   if (HH<10) sTaim = StringConcatenate(sTaim,"0",DoubleToStr(HH,0));
   else sTaim = StringConcatenate(sTaim,DoubleToStr(HH,0));
   if (MM<10) sTaim = StringConcatenate(sTaim,":0",DoubleToStr(MM,0));
   else sTaim = StringConcatenate(sTaim,":",DoubleToStr(MM,0));
   return(sTaim);
}
//--------------------------------------------------------------------
int StartDraw()
{
   ObjectCreate("time",   OBJ_LABEL        , 0, 0, 0);
      ObjectSet("time",   OBJPROP_CORNER   , corner);
      ObjectSet("time",   OBJPROP_XDISTANCE, 10);
      ObjectSet("time",   OBJPROP_YDISTANCE, 13);
   ObjectCreate("NAME",   OBJ_LABEL        , 0, 0, 0);
      ObjectSet("NAME",   OBJPROP_CORNER   , corner );      
      ObjectSet("NAME",   OBJPROP_XDISTANCE, 10);      
      ObjectSet("NAME",   OBJPROP_YDISTANCE, 60);
   ObjectCreate("balans", OBJ_LABEL        , 0, 0, 0);
      ObjectSet("balans", OBJPROP_CORNER   , corner);      
      ObjectSet("balans", OBJPROP_XDISTANCE, 10 ); 
      ObjectSet("balans", OBJPROP_YDISTANCE, 25);
   ObjectCreate("maney",  OBJ_LABEL        , 0, 0, 0);
      ObjectSet("maney",  OBJPROP_CORNER   , corner);
      ObjectSet("maney",  OBJPROP_XDISTANCE, 10 );
      ObjectSet("maney",  OBJPROP_YDISTANCE, 37);
return;
}   
//--------------------------------------------------------------------
int deinit()
{
   ObjectDelete    ("time"  );
   ObjectDelete    ("NAME"  );
   ObjectDelete    ("balans");
   ObjectDelete    ("maney" );
   ObjectsDeleteAll(0,OBJ_TREND);
   ObjectsDeleteAll(0,OBJ_ARROW);
   if (DelLineOrders)
   for (int i=1; i<=25; i++) 
   ObjectDelete("Order "+i);
   return;
}   
//--------------------------------------------------------------------

