//+------------------------------------------------------------------+
//|                                        Rubinovi4_Order_Lines.mq4 |
//|                                  Copyright © 2010,  Silen, v 1.0 |
//|                                             http://fx-vladmih.ru |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2010,  Silen, v 1.0"
#property link      "http://fx-vladmih.ru"

#property indicator_chart_window

extern int    Line_Style             = STYLE_DOT;     //-- тип линии 
extern int    Line_Width             = 1;             //-- толщина линии 
extern color  Line_BUY_Color         = Green;         //-- цвет BUY
extern color  Line_SELL_Color        = Red;           //-- цвет SELL
extern int    Text_HOffset           = 10;            //-- смещение подписи вправо от последнего бара (в барах)
extern int    MagicNumber            = -1;            //-- магик ордеров, -1 - все ордера

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- indicators
//----
   Check_Orders(); 
   
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
   string Line_Name;
//----
   //-- check for closed orders
   for (int i=1; i<=OrdersHistoryTotal(); i++)         //Цикл по всем ордерам,..
   {                                                   //отражённым в терминале
      if(OrderSelect(i-1,SELECT_BY_POS, MODE_HISTORY)==true) //Если есть следующий
      {                                     
         if((OrderMagicNumber() == MagicNumber || MagicNumber == -1) &&
            (OrderSymbol() == Symbol()) &&
            (OrderType() == OP_BUY  || OrderType() == OP_SELL))
         {
            Line_Name = "OrderLine_"+DoubleToStr(OrderTicket(),0);
            if(ObjectFind(Line_Name) >= 0)
            {
               ObjectDelete(Line_Name);
               ObjectDelete("txt_"+Line_Name);
            }
         }
       }
   }  
   //-- check open orders
   for (i=1; i<=OrdersTotal(); i++)         //Цикл по всем ордерам,..
   {                                                   //отражённым в терминале
      if(OrderSelect(i-1,SELECT_BY_POS)==true) //Если есть следующий
      {                                     
         if((OrderMagicNumber() == MagicNumber || MagicNumber == -1) &&
            (OrderSymbol() == Symbol()) &&
            (OrderType() == OP_BUY  || OrderType() == OP_SELL))
         {
            Line_Name = "OrderLine_"+DoubleToStr(OrderTicket(),0);
            if(ObjectFind(Line_Name) >= 0)
            {
               ObjectDelete(Line_Name);
               ObjectDelete("txt_"+Line_Name);
            }
         }
       }
   }  
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {   
//----
   Check_Orders();
     
//----
   return(0);
  }
//+------------------------------------------------------------------+
void Check_Orders()
{
   string Line_Name;

   //-- check for closed orders
   for (int i=0; i<=OrdersHistoryTotal(); i++)         //Цикл по всем ордерам,..
   {                                                   //отражённым в терминале
      if(OrderSelect(i-1,SELECT_BY_POS, MODE_HISTORY)==true) //Если есть следующий
      {                        
         if((OrderMagicNumber() == MagicNumber || MagicNumber == -1) &&
            (OrderSymbol() == Symbol()) &&
            (OrderType() == OP_BUY  || OrderType() == OP_SELL))
         {
            Line_Name = "OrderLine_"+DoubleToStr(OrderTicket(),0);
            if(ObjectFind(Line_Name) >= 0)
            {
               ObjectDelete(Line_Name);
               ObjectDelete("txt_"+Line_Name);
            }
         }
       }
   }  
   
   //-- check for open orders
   for (i=1; i<=OrdersTotal(); i++)         //Цикл по всем ордерам,..
   {                                                   //отражённым в терминале
      if(OrderSelect(i-1,SELECT_BY_POS)==true) //Если есть следующий
      {                                     
         if((OrderMagicNumber() == MagicNumber || MagicNumber == -1) &&
            (OrderSymbol() == Symbol()) &&
            (OrderType() == OP_BUY  || OrderType() == OP_SELL))
         {                             
            Line_Name = "OrderLine_"+DoubleToStr(OrderTicket(),0);
            if(OrderCloseTime() == 0)
            {
               if(ObjectFind(Line_Name) < 0)
               {
                  Draw_Order_Line(Line_Name);
               }
               ObjectSet("txt_"+Line_Name, OBJPROP_TIME1, Get_Text_Time());
            }   
            else
            {
               if(ObjectFind(Line_Name) >= 0)
               {
                  ObjectDelete(Line_Name);
                  ObjectDelete("txt_"+Line_Name);
               }
            }   
         }
       }
   }  
   
   
}

void Draw_Order_Line(string Line_Name)
{
   color    Line_Color;
   string   Line_Text;
   
   string   Txt_Name = "txt_"+Line_Name;
   
   if(ObjectFind(Line_Name) >= 0) return;
   
   if(OrderType() == OP_BUY)
   {
      Line_Color = Line_BUY_Color;
      Line_Text  = "Buy - "+DoubleToStr(OrderLots(), 2); 
   }
   else
   {      
      Line_Color = Line_SELL_Color;
      Line_Text  = "Sell - "+DoubleToStr(OrderLots(), 2); 
   }
   
   ObjectCreate(Line_Name, OBJ_HLINE, 0, 0, OrderOpenPrice());
   ObjectSet(Line_Name, OBJPROP_COLOR, Line_Color);
   ObjectSet(Line_Name, OBJPROP_STYLE, Line_Style); 
   //ObjectSet(Line_Name, OBJPROP_WIDTH, Line_Width);       
   //ObjectSetText(Line_Name, Line_Text,  1, "Terminal", Line_Color); 
   
   ObjectCreate(Txt_Name, OBJ_TEXT, 0, Get_Text_Time(), OrderOpenPrice());
   ObjectSet(Txt_Name, OBJPROP_COLOR, Line_Color);      
   ObjectSetText(Txt_Name, Line_Text, 8, "Terminal");
}

double Get_Text_Time()
{
   int right_bound=WindowFirstVisibleBar()-WindowBarsPerChart();
   if(right_bound<0) right_bound=0;
   
   datetime xTime=iTime(Symbol(), 0, right_bound)+Period()*60*Text_HOffset;

   return(xTime);
}