//+------------------------------------------------------------------+
//|                                                HistoryOrders.mq4 |
//|               Ilnur17021992. Special for http://tradelikeapro.ru |
//|                                          http://tradelikeapro.ru |
//+------------------------------------------------------------------+
#property copyright "Ilnur17021992. Special for http://tradelikeapro.ru"
#property link      "http://tradelikeapro.ru"
#property version   "1.01"
#property strict
#property indicator_chart_window

extern int        MagicNumber                = 0;
extern datetime   HistoryOrdersFromDateTime  = 0;
extern int        FontSize                   = 10;
extern color      SellColor                  = clrRed;
extern color      BuyColor                   = clrBlue;
extern color      ProfitColor                = clrWhite;
extern bool       DeleteHistoryOrders        = false;

int TotalPos=-1;

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
   
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+

void start()
{
   for(int i=OrdersHistoryTotal()-1; i>=0; i--)
   {
      if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY))
      {
         if(OrderMagicNumber()==MagicNumber && OrderSymbol()==Symbol())
         {
            if(HistoryOrdersFromDateTime<OrderCloseTime())
            {
               if(OrdersHistoryTotal()!=TotalPos)
                  HistoryOrders();
            }   
         }
      }
   }
}
void HistoryOrders()
{
   int CountOrders=0;
   double b=OrderOpenPrice(), d=OrderClosePrice(), lots=OrderLots(), Profit=0;
   datetime a=OrderOpenTime(), c=OrderCloseTime(), close_time;
   string Ticket=(string)OrderTicket(), ProfitTicket, type="Sell", symbol=OrderSymbol(), comment=OrderComment(), Background; 
   color col=SellColor;
   if(OrderType()==0) {col=BuyColor; type="Buy";}
         
   if(DeleteHistoryOrders==false)
   {
      ObjectCreate("#"+Ticket+" "+type+" "+DoubleToString(lots,2)+" "+symbol+" at "+DoubleToString(b,Digits)+"\n"+comment,OBJ_ARROW,0,a,b);
      ObjectSet("#"+Ticket+" "+type+" "+DoubleToString(lots,2)+" "+symbol+" at "+DoubleToString(b,Digits)+"\n"+comment,OBJPROP_COLOR,col);
      ObjectSet("#"+Ticket+" "+type+" "+DoubleToString(lots,2)+" "+symbol+" at "+DoubleToString(b,Digits)+"\n"+comment,OBJPROP_ARROWCODE,1);
      
      ObjectCreate("#"+Ticket+" "+DoubleToString(b,Digits)+" -> "+DoubleToString(d,Digits),OBJ_TREND,0,a,b,c,d);
      ObjectSet("#"+Ticket+" "+DoubleToString(b,Digits)+" -> "+DoubleToString(d,Digits),OBJPROP_COLOR,col);
      ObjectSet("#"+Ticket+" "+DoubleToString(b,Digits)+" -> "+DoubleToString(d,Digits),OBJPROP_WIDTH,1);
      ObjectSet("#"+Ticket+" "+DoubleToString(b,Digits)+" -> "+DoubleToString(d,Digits),OBJPROP_STYLE,STYLE_DOT);
      ObjectSet("#"+Ticket+" "+DoubleToString(b,Digits)+" -> "+DoubleToString(d,Digits),OBJPROP_RAY,0);
      ObjectSet("#"+Ticket+" "+DoubleToString(b,Digits)+" -> "+DoubleToString(d,Digits),OBJPROP_BACK,true);

      ObjectCreate("#"+Ticket+" "+type+" "+DoubleToString(lots,2)+" "+symbol+" at "+DoubleToString(b,Digits)+" close at "+DoubleToString(d,Digits),OBJ_ARROW,0,c,d);
      ObjectSet("#"+Ticket+" "+type+" "+DoubleToString(lots,2)+" "+symbol+" at "+DoubleToString(b,Digits)+" close at "+DoubleToString(d,Digits),OBJPROP_COLOR,col);
      ObjectSet("#"+Ticket+" "+type+" "+DoubleToString(lots,2)+" "+symbol+" at "+DoubleToString(b,Digits)+" close at "+DoubleToString(d,Digits),OBJPROP_ARROWCODE,3);
      
      for(int i=OrdersHistoryTotal()-1; i>=0; i--) 
      {
         if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY))
         {
            if(OrderMagicNumber()==MagicNumber && OrderSymbol()==Symbol())
            {
               close_time=OrderCloseTime();
               if(c<=close_time+60 && c>=close_time-60)
               {
                  Profit+=OrderProfit()+OrderCommission()+OrderSwap();
                  ProfitTicket=(string)OrderTicket();
                  CountOrders++;
               }
            }
         }      
      }
      
      if(CountOrders>1)
      {  
         ObjectDelete("#"+Ticket+" Background");
         ObjectDelete("#"+Ticket+" Profit: "+DoubleToString(Profit,2));
         Ticket=ProfitTicket;      
      }
      
      for(int i=2; i<StringLen(DoubleToString(Profit,2)); i++) 
         StringAdd(Background,"g");
      
      ObjectCreate("#"+Ticket+" Background",OBJ_TEXT,0,c,d);
      ObjectSet("#"+Ticket+" Background",OBJPROP_ANCHOR,ANCHOR_LOWER);
      ObjectSetText("#"+Ticket+" Background",Background,FontSize,"Webdings",col);
      ObjectSet("#"+Ticket+" Background",OBJPROP_PRICE1,d);
      ObjectSet("#"+Ticket+" Background",OBJPROP_TIME1,c+Period());
      
      ObjectCreate("#"+Ticket+" Profit: "+DoubleToString(Profit,2),OBJ_TEXT,0,c,d);
      ObjectSet("#"+Ticket+" Profit: "+DoubleToString(Profit,2),OBJPROP_ANCHOR,ANCHOR_LOWER);
      ObjectSetText("#"+Ticket+" Profit: "+DoubleToString(Profit,2),DoubleToString(Profit,2),FontSize,"Arial",ProfitColor);
      ObjectSet("#"+Ticket+" Profit: "+DoubleToString(Profit,2),OBJPROP_PRICE1,d);
      ObjectSet("#"+Ticket+" Profit: "+DoubleToString(Profit,2),OBJPROP_TIME1,c+Period());
   } else ObjectsDeleteAll(0,"#"+Ticket+" ");   
}

void OnDeinit(const int reason)
{  
   for(int i=0; i<OrdersHistoryTotal(); i++)
   {
      DeleteHistoryOrders=true;
      if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY))
      {
         if(OrderMagicNumber()==MagicNumber && OrderSymbol()==Symbol())
            HistoryOrders();
      }
   }
}
