Help for consolidated position indicator

 

Is anyone able to help and advise why this consolidated position indicator (from http://www.informedtrades.com/blogs/3d_fx/2492-consolidated-position-indicator-mt4.html) when a position is partially closed, does not include the remaining open position in the calculation? and if so, suggest what needs to be changed?

I had asked/posted on the developer's site, but have not heard back in months...

 Thankyou in advance. 

 Full open position

Partially closed position 

 

//+------------------------------------------------------------------+
//|                                                       ConPos.mq4 |
//+------------------------------------------------------------------+

#property copyright "3dmarketanalysis"
#property link      "http://www.3dmarketanalysis.com"

#property indicator_chart_window

extern string Account_Symbol = "£";
extern string Order_Comment = "";
extern int    Order_Magic_Number = 0;
extern color  Buy_Line_Color = Blue;
extern color  Sell_Line_Color = Purple;
extern color  Long_Text_Color = Blue;
extern color  Short_Text_Color = Blue;
extern int    Text_Corner_0_to_3 = 0;
extern int    Text_X_Distance = 5;
extern int    Long_Text_Y_Distance = 14;
extern int    Short_Text_Y_Distance = 30;

int init()
{
  ObjectCreate("Average_Buy_Report",OBJ_LABEL,0,0,0);
  ObjectCreate("Average_Sell_Report",OBJ_LABEL,0,0,0);
  return(0);
}

int deinit()
{
   ObjectDelete("Average_Sell_Line");
   ObjectDelete("Average_Buy_Line");
   ObjectDelete("Average_Sell_Report");
   ObjectDelete("Average_Buy_Report");
   return(0);
}

int start()
  {
   int    counted_bars=IndicatorCounted();
   double Total_Sell_Price=0;
   double Cons_Sell_Price=0;
   double Total_Sell_Size=0;
   double Total_Buy_Price=0;
   double Cons_Buy_Price=0;
   double Total_Buy_Size=0;
   double Long_Profit=0;
   double Short_Profit=0;
   double Long_Locked=0;
   double Short_Locked=0;
   int i;

   ObjectDelete("Average_Buy_Line");
   ObjectDelete("Average_Sell_Line");
   for(i=0;i<OrdersTotal();i++)
   {
      OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
      if(OrderSymbol()==Symbol() && OrderComment()==Order_Comment && OrderMagicNumber()==Order_Magic_Number)
      {
         if(OrderType()==OP_BUY)
         {
            Total_Buy_Price += OrderOpenPrice()*OrderLots();
            Total_Buy_Size += OrderLots();
            Long_Profit += OrderProfit() + OrderSwap();
            if(Bid != OrderOpenPrice())
            {
               Long_Locked += ((OrderStopLoss()-OrderOpenPrice())/(Bid-OrderOpenPrice()))*OrderProfit();
            }
         }
         if(OrderType()==OP_SELL)
         {
            Total_Sell_Price += OrderOpenPrice()*OrderLots();
            Total_Sell_Size += OrderLots();
            Short_Profit += OrderProfit() + OrderSwap();
            if(Ask != OrderOpenPrice())
            {
               Short_Locked += ((OrderOpenPrice()-OrderStopLoss())/(OrderOpenPrice()-Ask))*OrderProfit();
            }
         }
      }   
   }
   if(Total_Buy_Price>0)
   {
      Total_Buy_Price /= Total_Buy_Size;
      ObjectCreate("Average_Buy_Line",OBJ_HLINE,0,0,Total_Buy_Price);
      ObjectSet("Average_Buy_Line",OBJPROP_COLOR,Buy_Line_Color);
   }
   if(Total_Sell_Price>0)
   {
      Total_Sell_Price /= Total_Sell_Size;
      ObjectCreate("Average_Sell_Line",OBJ_HLINE,0,0,Total_Sell_Price);
      ObjectSet("Average_Sell_Line",OBJPROP_COLOR,Sell_Line_Color);
   }
 
   ObjectSet("Average_Buy_Report",OBJPROP_CORNER,Text_Corner_0_to_3);
   ObjectSet("Average_Buy_Report",OBJPROP_XDISTANCE,Text_X_Distance);
   ObjectSet("Average_Buy_Report",OBJPROP_YDISTANCE,Long_Text_Y_Distance);
   ObjectSet("Average_Buy_Report",OBJPROP_COLOR,Long_Text_Color);
   ObjectSet("Average_Buy_Report",OBJPROP_WIDTH,3);
   ObjectSet("Average_Buy_Report",OBJPROP_BACK,false);
   ObjectSet("Average_Buy_Report",OBJPROP_FONTSIZE,12);
   ObjectSetText("Average_Buy_Report",StringConcatenate("Long: ",Total_Buy_Price,", ",
      Total_Buy_Size,", ",Account_Symbol,Long_Profit,", ",Account_Symbol,DoubleToStr(Long_Locked,2)),10,"Times New Roman");
   ObjectSet("Average_Sell_Report",OBJPROP_CORNER,Text_Corner_0_to_3);
   ObjectSet("Average_Sell_Report",OBJPROP_XDISTANCE,Text_X_Distance);
   ObjectSet("Average_Sell_Report",OBJPROP_YDISTANCE,Short_Text_Y_Distance);
   ObjectSet("Average_Sell_Report",OBJPROP_COLOR,Short_Text_Color);
   ObjectSet("Average_Sell_Report",OBJPROP_WIDTH,3);
   ObjectSet("Average_Sell_Report",OBJPROP_BACK,false);
   ObjectSet("Average_Sell_Report",OBJPROP_FONTSIZE,12);
   ObjectSetText("Average_Sell_Report",StringConcatenate("Short: ",Total_Sell_Price,", ",
      Total_Sell_Size,", ",Account_Symbol,Short_Profit,", ",Account_Symbol,DoubleToStr(Short_Locked,2)),10,"Times New Roman");

   return(0);
  }
 

Two ideas;

1. A small fix (it likely will not fix the issue), but the OrdersTotal() for loop needs to be the other way around. The issue is that if one order is closed OrdersTotal() is one less hence use this;

for(i=OrdersTotal()-1;i>=0;i--){  // Never count up while closing orders etc. (when using OrdersTotal) (With thanks WHRoeder https://forum.mql4.com/26603#220124) | -1: array is 0 based

2. The indicator uses MODE_TRADES. (ref https://docs.mql4.com/trading/orderselect) which does not include closed orders. I wonder if there is a bug in MT4 that excludes partially closed positions from MODE_TRADES list.

It may be possible to change/extend the indicator to use MODE_HISTORY for partially closed orders. 

3. Aha, I just found this; https://forum.mql4.com/44280

Hope this gets you started. You may still need a programmer to fix it (mql5 website > hire some freelancer > usually quite cheap)

 
Btw, the indicator does not work for me (MT4 v745, 200 positions open)
 

Thanks Roel13 for the advice, I will look into what you've suggested and get back if it does anything or not.

The indicator works for me on MT4 v765... Not sure if it's the amount of trades it may not be able to handle. Each position requires a stop-loss in order to calculate the amount at risk, and also when applied to the chart is just for that currency symbol (unless you've specified an order comment or magic number).   

Reason: