//+------------------------------------------------------------------+
//|                                      Вывод ордеров на график.mq4 |
//|                                              Vladimir Irek Ildar |
//|                                        http://www.metaquotes.net |
//+------------------------------------------------------------------+
#property copyright "Vladimir Irek Ildar"
#property link      "http://www.metaquotes.net"

#property indicator_chart_window
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- indicators
//----
   int RECTANGLE_COUNT = 0;
   double MinPrice, MaxPrice;
   int BarSearch, BarShift;
   int iOrders=OrdersHistoryTotal(), i,  j;


    for(i=0; i<OrdersHistoryTotal(); i++) 
    {
     OrderSelect(i,SELECT_BY_POS,MODE_HISTORY);
     if ((OrderSymbol()==Symbol())&&((OrderType()==OP_BUY)||(OrderType()==OP_SELL)))
     {ObjectCreate("rect_"+i, OBJ_RECTANGLE, 0, OrderOpenTime(), OrderOpenPrice(), OrderCloseTime(), OrderClosePrice());
     ObjectSet("rect_"+i,OBJPROP_COLOR, LightPink);
     if ((OrderType()==OP_BUY)&&(OrderOpenPrice()<OrderClosePrice()))
         ObjectSet("rect_"+i,OBJPROP_COLOR, LightGreen);
     if ((OrderType()==OP_SELL)&&(OrderOpenPrice()>OrderClosePrice()))
         ObjectSet("rect_"+i,OBJPROP_COLOR, LightGreen);
     
     BarSearch=iBarShift(NULL, 0, OrderOpenTime())-iBarShift(NULL, 0, OrderCloseTime());
     BarShift=iBarShift(NULL, 0, OrderCloseTime());
     MaxPrice=MaxMinPrice(1,BarSearch,BarShift);
     MinPrice=MaxMinPrice(2,BarSearch,BarShift);
    //максимальная прибыль 
    if (OrderType()==OP_SELL)
      if (OrderOpenPrice()>OrderClosePrice())
        ObjectCreate("rectmax_"+i, OBJ_RECTANGLE, 0, OrderOpenTime(), OrderClosePrice(), OrderCloseTime(),MinPrice);
      else
        ObjectCreate("rectmax_"+i, OBJ_RECTANGLE, 0, OrderOpenTime(), OrderOpenPrice(), OrderCloseTime(), MinPrice);
    if (OrderType()==OP_BUY)
      if (OrderOpenPrice()<OrderClosePrice())
        ObjectCreate("rectmax_"+i, OBJ_RECTANGLE, 0, OrderOpenTime(), OrderClosePrice(), OrderCloseTime(), MaxPrice);
      else
        ObjectCreate("rectmax_"+i, OBJ_RECTANGLE, 0, OrderOpenTime(), OrderOpenPrice(), OrderCloseTime(), MaxPrice);
    ObjectSet("rectmax_"+i,OBJPROP_COLOR, Gold);
    //максимальный убыток
    if (OrderType()==OP_SELL)
      if (OrderOpenPrice()>OrderClosePrice())
        ObjectCreate("rectmin_"+i, OBJ_RECTANGLE, 0, OrderOpenTime(), OrderOpenPrice(), OrderCloseTime(),MaxPrice);
      else
        ObjectCreate("rectmin_"+i, OBJ_RECTANGLE, 0, OrderOpenTime(), OrderClosePrice(), OrderCloseTime(), MaxPrice);
    if (OrderType()==OP_BUY)
      if (OrderOpenPrice()<OrderClosePrice())
        ObjectCreate("rectmin_"+i, OBJ_RECTANGLE, 0, OrderOpenTime(), OrderOpenPrice(), OrderCloseTime(), MinPrice);
      else
        ObjectCreate("rectmin_"+i, OBJ_RECTANGLE, 0, OrderOpenTime(), OrderClosePrice(), OrderCloseTime(), MinPrice);
    ObjectSet("rectmin_"+i,OBJPROP_COLOR, Maroon);
    
    
    
    }
    }
i=i-1;
Print(MaxPrice);
Print(MinPrice);
Print(OrderClosePrice());
j=iBarShift(NULL, 0, OrderCloseTime());
Print(iTime( NULL, 0, j));
Print(Time[j]);
   return(0);
  }
  
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   ObjectsDeleteAll();
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
   ObjectsDeleteAll();
   init();
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+

//Функция поиска максимума/минимума за заданное количество баров
// type-максимум или минимум, barsearch -количество баров
 
double MaxMinPrice(int type,int barsearch, int barshift)
{
    
    double minmax;
 
    if(type==2)
       {
           minmax=Low[iLowest(NULL,0,MODE_LOW,barsearch,barshift)];
       }
    if(type==1)
       {
           minmax=High[iHighest(NULL,0,MODE_HIGH,barsearch,barshift)];
       }
return(minmax);
}