//+------------------------------------------------------------------+
//|                                          Average_Entry_Price.mq4 |
//|                                       Copyright © 2008, Denkhaus |
//|                                    Modified by Kris, 10-Jan-2011 |
//+------------------------------------------------------------------+

#property indicator_chart_window
#property indicator_color1 C'000,150,250'   // DodgerBlue
#property indicator_color2 C'200,200,000'   // Yellow
#property indicator_color3 C'070,000,000'   // Red
#property indicator_color4 C'000,070,000'   // Green
#property indicator_color5 C'200,200,200'   // Silver
//-----
void init()
{   
    if(ObjectFind("ClosePrice") != 0)
    {
      ObjectCreate("ClosePrice", OBJ_HLINE, 0, Time[0], 0.0);
      ObjectSet("ClosePrice", OBJPROP_STYLE, STYLE_SOLID);
      ObjectSet("ClosePrice", OBJPROP_COLOR, indicator_color1);  
      ObjectSet("ClosePrice", OBJPROP_WIDTH, 1); 
    }
}
//-------
void deinit()
{
   ObjectDelete("AvgOpenPrice");    
   ObjectDelete("ClosePrice");
   ObjectDelete("PipRectangle");
   ObjectDelete("InfoString");
   return(0);
}
//-------
void start()
{
   int nOrders = 0;
   double dOpenPrice = 0.0;
   double dProfit = 0.0;
   double dLots = 0.0;
   
   for (int i = 0; i <= OrdersTotal(); i++) 
   { 
      if(OrderSelect(i,SELECT_BY_POS))
      { 
         if(OrderSymbol() == Symbol())
         {
            if(OrderType() == OP_BUY || OrderType() == OP_SELL)
            { 
               dOpenPrice += OrderOpenPrice()*OrderLots();
               //dOpenPrice += OrderOpenPrice();
               dProfit += OrderProfit() + OrderSwap();
               dLots += OrderLots();
               nOrders++;
            }
         } 
      }
   } 
   
   if(nOrders > 0)
   {
      double dAvgOpenPrice = dOpenPrice/dLots;
      //double dAvgOpenPrice = dOpenPrice/nOrders;
      
      if(ObjectFind("AvgOpenPrice")!= 0)
      {
         ObjectCreate("AvgOpenPrice", OBJ_HLINE, 0, Time[0], 0.0);
         ObjectSet("AvgOpenPrice", OBJPROP_STYLE, STYLE_SOLID);
         ObjectSet("AvgOpenPrice", OBJPROP_COLOR, indicator_color2);  
         ObjectSet("AvgOpenPrice", OBJPROP_WIDTH, 1); 
      }
      
      if(ObjectFind("PipRectangle")!= 0)
      {
         ObjectCreate("PipRectangle", OBJ_RECTANGLE, 0, Time[0], dAvgOpenPrice, Time[0], Close[0]);      
      }
      
      datetime dtInfoText     = Time[0] + Period() * 1200;  // distance from left
      double   dPriceInfoText = ((Close[0] + dAvgOpenPrice)/2) + 2 * Point;
      
      if(ObjectFind("InfoString")!= 0)
      {         
         ObjectCreate("InfoString", OBJ_TEXT, 0,dtInfoText, dPriceInfoText);         
      }
      
      ObjectSetText("InfoString", DoubleToStr(dProfit,2), 8, "Verdana Bold", indicator_color5);
      ObjectSet("InfoString", OBJPROP_PRICE1, dPriceInfoText);
      ObjectSet("InfoString", OBJPROP_TIME1, dtInfoText);
      
      ObjectSet("AvgOpenPrice", OBJPROP_PRICE1, dAvgOpenPrice);  
      ObjectSet("PipRectangle", OBJPROP_TIME1,  Time[0]);         
      ObjectSet("PipRectangle", OBJPROP_PRICE1, dAvgOpenPrice);  
      ObjectSet("PipRectangle", OBJPROP_TIME2,  Time[0] + Period() * 2000);         
      ObjectSet("PipRectangle", OBJPROP_PRICE2, Close[0]);  
      
      if(dProfit < 0 )
      {
         ObjectSet("PipRectangle", OBJPROP_COLOR, indicator_color3);
      }
      else
      {
         ObjectSet("PipRectangle", OBJPROP_COLOR, indicator_color4);
      }
   }
   else
   {
      ObjectDelete("AvgOpenPrice");  
      ObjectDelete("PipRectangle");
      ObjectDelete("InfoString");  
   }
   
   ObjectSet("ClosePrice", OBJPROP_PRICE1, Close[0]);         
}

