//+------------------------------------------------------------------+
//|                                                         test.mq4 |
//|                                                         NickBixy |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "NickBixy"
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict
#property indicator_chart_window

#define OBJ_NAME "PProfitIndicatorObj"
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
   ObjectDelete(OBJ_NAME);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
  {
//---
   double predictedProfit=0.0;

   for(int i=OrdersTotal()-1; i>=0; i--)
     {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
        {
         if(OrderSymbol()==_Symbol)
           {
            if((OrderType()==OP_SELL || OrderType()==OP_SELLLIMIT || OrderType()==OP_SELLSTOP) && OrderTakeProfit()>0)// sell type order and TakeProfit is active
              {
               double tick_val=MarketInfo(_Symbol,MODE_TICKVALUE);//GetTickValue
               double tp_dist=(OrderOpenPrice()-OrderTakeProfit())/_Point;//CalcDistance
               double tp_val=(tp_dist*tick_val)*OrderLots();//CalcValue

               predictedProfit+=tp_val;
              }

            if((OrderType()==OP_BUY || OrderType()==OP_BUYLIMIT || OrderType()==OP_BUYSTOP) && OrderTakeProfit()>0)//buy type order and TakeProfit is active
              {
               double tick_val=MarketInfo(_Symbol,MODE_TICKVALUE);//GetTickValue
               double tp_dist=(OrderTakeProfit()-OrderOpenPrice())/_Point;//CalcDistance
               double tp_val=(tp_dist*tick_val)*OrderLots();//CalcValue
               
               predictedProfit+=tp_val;
              }
           }
        }
     }

   DrawPredictedProfit(predictedProfit);

//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+ 
//| End of the code                                                  | 
//+------------------------------------------------------------------+ 
void DrawPredictedProfit(double value)
  {
   string s="Predicted Profits - $"+(DoubleToString(value,0));

   if(ObjectFind(OBJ_NAME)<0)
     {
      ObjectCreate(OBJ_NAME,OBJ_LABEL,0,0,0);
      ObjectSet(OBJ_NAME,OBJPROP_CORNER,CORNER_RIGHT_UPPER);
      ObjectSet(OBJ_NAME,OBJPROP_YDISTANCE,260);
      ObjectSet(OBJ_NAME,OBJPROP_XDISTANCE,20);
      ObjectSet(OBJ_NAME,OBJPROP_SELECTABLE,false);
      ObjectSetText(OBJ_NAME,s,12,"Arial",clrYellow);
     }

   ObjectSetText(OBJ_NAME,s);

   WindowRedraw();
  }
//+------------------------------------------------------------------+
