How to show OrderProfit() as Text on every OrderCloseTime()?

 

Hello there. I am a beginner. I tried to display OrderProfit() on the closed time on the candle.


Here is the code

// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
// timginter @ ForexFactory
//
// [spread], [ATR], ([number of opened]) [average]
//
// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

#property indicator_chart_window
#property strict

extern bool showCancelledPending = false;
extern bool hiddenFromObjectList = true;
extern bool synchHistory = false;

extern string Ignore_List = "";
double p = Point;
int      objtick = 0;
// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

/**
 * Init
 */
void init()
{
   //
}

/**
 * Deinit
 */
void deinit()
{
   if (synchHistory) {
      deleteAllObjects();
   }
}

/**
 * Main function
 */
void start()
{
   // go through closed orders
   int orderTotalClosed = OrdersHistoryTotal();
   for (int i = orderTotalClosed - 1; i >= 0; i--) {
      // check if order is valid
      if (!isValidClosedOrder(i)) {
         continue;
      }
      
      // build object names (limited to 63 characters)
      string nameClose = StringSubstr(getCloseSuffix(), 0, 63);

      
      // skip if order has open, line, close, tp (or no tp set) and sl (or no sl set)
      if (ObjectFind(nameClose) >= 0
      ) {
         continue;
      }
      
      // remove all arrows for current order
      deleteOrderObjects();
      
      // recreate close
      createClose(nameClose);
      
      
   }
}

// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

/**
 * Checks if a closed order is valid
 */
bool isValidClosedOrder(int position)
{
   if (!OrderSelect(position, SELECT_BY_POS, MODE_HISTORY)
      || OrderSymbol() != Symbol()
      || (OrderProfit() == 0.0 && !showCancelledPending)
      || isInIgnoreList(OrderTicket())
   ) {
      return false;
   }
   
   return true;
}

/**
 * Checks if Order is in ignore list
 */
bool isInIgnoreList(int orderTicket)
{
   if (StringFind(Ignore_List, "_" + IntegerToString(orderTicket) + "_") == -1) {
      return false;
   }
   
   return true;
}

// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

/**
 * Parses OrderType() constants to strings
 */
string parseOrderType()
{
   switch(OrderType())
   {
      case OP_BUY:
         return "buy";
      case OP_SELL:
         return "sell";
      case OP_BUYLIMIT:
         return "buy limit";
      case OP_SELLLIMIT:
         return "sell limit";
      case OP_BUYSTOP:
         return "buy stop";
      case OP_SELLSTOP:
         return "sell stop";
   }
   
   return "";
}

string getCloseSuffix()
{
   switch(OrderType())
   {
      case OP_BUY:
         return DoubleToString(OrderProfit(), Digits);
      case OP_SELL:
         return DoubleToString(OrderProfit(), Digits);
      case OP_BUYLIMIT:
         return "deleted";
      case OP_SELLLIMIT:
         return "deleted";
      case OP_BUYSTOP:
         return "deleted";
      case OP_SELLSTOP:
         return "deleted";
   }
   
   return "";
}

double getOrderClosePrice()
{
   switch(OrderType())
   {
      case OP_BUY:
         return OrderClosePrice();
      case OP_SELL:
         return OrderClosePrice();
      case OP_BUYLIMIT:
         return OrderOpenPrice();
      case OP_SELLLIMIT:
         return OrderOpenPrice();
      case OP_BUYSTOP:
         return OrderOpenPrice();
      case OP_SELLSTOP:
         return OrderOpenPrice();
   }
   
   return 0;
}

bool createClose(string name)
{
   ObjectCreate(name, OBJ_ARROW, 0, 0, 0);
   ObjectSet(name, OBJPROP_TIME1, OrderCloseTime());
   ObjectSet(name, OBJPROP_PRICE1, getOrderClosePrice());
   ObjectSetInteger(0, name, OBJPROP_ARROWCODE, 3);
   ObjectSet(name, OBJPROP_COLOR, Goldenrod);
   ObjectCreate("myx" + DoubleToStr(objtick, 0), OBJ_TEXT, 0, Time[0], High[0] + (5*p));
   string G_TEXT = getCloseSuffix();
   ObjectSetText("myx" + DoubleToStr(objtick, 0), "$" +G_TEXT, 12, "Arial", White);
   
   return true;
}

bool deleteOrderObjects()
{
   bool success = true;
   
   string orderTicket = IntegerToString(OrderTicket());

   int objects = ObjectsTotal();
   for (int i = objects - 1; i >= 0; i--) {
      string name = ObjectName(i);
      
      // skip if doesn't start with # or doesn't have currently selected order ticket at second character
      if (StringFind(name, "#") != 0
         || StringFind(name, orderTicket) != 1) {
         continue;
      }
      
      if (!ObjectDelete(name)) {
         Print("Failed to delete /" + name + "/ error #" + IntegerToString(GetLastError()));
         success = false;
      }
   }
   
   return success;
}

bool deleteAllObjects()
{
   bool success = true;

   // delete all arrows and lines
   int objects = ObjectsTotal();
   for (int i = objects - 1; i >= 0; i--) {
      string name = ObjectName(i);
      
      // skip if not an arrow or trendline
      if (ObjectGetInteger(0, name, OBJPROP_TYPE) != OBJ_TREND
         && ObjectGetInteger(0, name, OBJPROP_TYPE) != OBJ_ARROW
      ) {
         continue;
      }
      
      // skip if doesn't start with #
      if (StringFind(name, "#") != 0) {
         continue;
      }
      
      // skip if trendline and doesn't have " -> "
      if (ObjectGetInteger(0, name, OBJPROP_TYPE) == OBJ_TREND
         && StringFind(name, " -> ") == -1
      ) {
         continue;
      }
      
      // skip if arrow is not type 1, 3 or 4, and doesn't have " [order type] (...) at "
      if (ObjectGetInteger(0, name, OBJPROP_TYPE) == OBJ_ARROW
         && ObjectGetInteger(0, name, OBJPROP_ARROWCODE) != 1
         && ObjectGetInteger(0, name, OBJPROP_ARROWCODE) != 3
         && ObjectGetInteger(0, name, OBJPROP_ARROWCODE) != 4
      ) {
         continue;
      }
      
      if (!ObjectDelete(name)) {
         Print("Failed to delete /" + name + "/ error #" + IntegerToString(GetLastError()));
         success = false;
      }
   }
   
   return success;
}


My goal is to produce something like this



Thank you.