How to deal with multiple CreateObject() functions

 

Hello, I'm trying to create an indicator to show some basic info but I have problems with text objects. It only draws the last text label like it's overwriting it, I suspect it's because of the indicator buffers but I'm not sure..


//+------------------------------------------------------------------+
//| Account_Stats.mq5                                                 |
//| Copyright 2023, MetaQuotes Ltd.                                   |
//| https://www.mql5.com                                              |
//+------------------------------------------------------------------+
#property copyright "Copyright 2023, MetaQuotes Ltd."
#property link      "https://www.mql5.com"
#property version   "1.00"

#property indicator_chart_window
#property indicator_buffers 2
#property indicator_color1 clrLime
#property indicator_color2 clrRed

// Inputs
input bool showEntryLevels = false;
input color colour = clrSilver;


// DateTime variables
datetime currentTime = TimeCurrent();
string currentDataString = TimeToString(currentTime, TIME_DATE);
string currentMinutesString = TimeToString(currentTime, TIME_MINUTES);
string yesterdayDataString = TimeToString(currentTime, TIME_DATE - 1);

// Double variables
double prevBalance;
double balance, equity;
long limit_orders;
double open_positions;

// String variables
string balanceString, equityString;

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
{
   Print(currentDataString);
   Print(currentMinutesString);

   // Initialize account data
   balance = AccountInfoDouble(ACCOUNT_BALANCE);
   equity = AccountInfoDouble(ACCOUNT_EQUITY);
   limit_orders = AccountInfoInteger(ACCOUNT_LIMIT_ORDERS);
   open_positions = AccountInfoDouble(ACCOUNT_ASSETS);

   // Convert balance and equity to strings
   balanceString = DoubleToString(balance, 2);
   equityString = DoubleToString(equity, 2);

   ObjectCreate(0, "balanceLabel", OBJ_LABEL, 0, 0, 0);
   ObjectSetInteger(0, "balanceLabel", OBJPROP_SELECTABLE, true);
   ObjectSetInteger(0, "balanceLabel", OBJPROP_CORNER, CORNER_RIGHT_UPPER);
   ObjectSetInteger(0, "balanceLabel", OBJPROP_XDISTANCE, 10);
   ObjectSetInteger(0, "balanceLabel", OBJPROP_YDISTANCE, 20);
   ObjectSetInteger(0, "balanceLabel", OBJPROP_FONTSIZE, 10);
   ObjectSetString(0, "balanceLabel", OBJPROP_FONT, "Verdana");
   ObjectSetInteger(0, "balanceLabel", OBJPROP_COLOR, colour);
   ObjectSetString(0, "balanceLabel", OBJPROP_TEXT, "BALANCE: " + balanceString);

   ObjectCreate(1, "equityLabel", OBJ_TEXT, 0, 0, 0);
   ObjectSetInteger(1, "equityLabel", OBJPROP_SELECTABLE, true);
   ObjectSetInteger(1, "equityLabel", OBJPROP_CORNER, CORNER_RIGHT_UPPER);
   ObjectSetInteger(1, "equityLabel", OBJPROP_XDISTANCE, 10);
   ObjectSetInteger(1, "equityLabel", OBJPROP_YDISTANCE, 40);
   ObjectSetInteger(1, "equityLabel", OBJPROP_FONTSIZE, 10);
   ObjectSetString(1, "equityLabel", OBJPROP_FONT, "Verdana");
   ObjectSetInteger(1, "equityLabel", OBJPROP_COLOR, colour);
   ObjectSetString(1, "equityLabel", OBJPROP_TEXT, "EQUITY: " + equityString);
   
   return(INIT_SUCCEEDED);
}

//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total, const int prev_calculated, const int begin, const double &price[])
{
   return(rates_total);
}

void OnDeinit()
{
   ObjectDelete(0, "balanceLabel");
   ObjectDelete(1, "equityLabel");
}

void OnStart()
{
   color BuyColor = clrBlue;
   color SellColor = clrRed;

   // Request trade history
   HistorySelect(0, TimeCurrent());

   // Create objects for trade history
   string name;
   uint total = HistoryDealsTotal();
   ulong ticket = 0;
   double price;
   double profit;
   datetime time;
   string symbol;
   long type;
   long entry;

   for (uint i = 0; i < total; i++)
   {
      // Try to get deal's ticket
      if ((ticket = HistoryDealGetTicket(i)) > 0)
      {
         // Get deal's properties
         price = HistoryDealGetDouble(ticket, DEAL_PRICE);
         time = (datetime)HistoryDealGetInteger(ticket, DEAL_TIME);
         symbol = HistoryDealGetString(ticket, DEAL_SYMBOL);
         type = HistoryDealGetInteger(ticket, DEAL_TYPE);
         entry = HistoryDealGetInteger(ticket, DEAL_ENTRY);
         profit = HistoryDealGetDouble(ticket, DEAL_PROFIT);

         if (profit >= 0)
         {
            Print("Profit: ", profit);
         }
         else
         {
            Print("Loss: ", profit);
         }

         // Show trade labels only for the current symbol
         if (showEntryLevels == true)
         {
            if (price && time && symbol == Symbol())
            {
               // Create price object
               name = "TradeHistory_Deal_" + string(ticket);
               if (entry) ObjectCreate(0, name, OBJ_ARROW_RIGHT_PRICE, 0, time, price, 0, 0);
               else ObjectCreate(0, name, OBJ_ARROW_LEFT_PRICE, 0, time, price, 0, 0);

               // Set object properties
               ObjectSetInteger(0, name, OBJPROP_SELECTABLE, true);
               ObjectSetInteger(0, name, OBJPROP_BACK, 0);
               ObjectSetInteger(0, name, OBJPROP_COLOR, type ? BuyColor : SellColor);
            }
         }
      }
   }

   // Apply changes on chart
   ChartRedraw();
}

Can you help me to fix it, thank you.

 
ObjectSetInteger(0, "balanceLabel", OBJPROP_XDISTANCE, 150);
ObjectCreate(0, "equityLabel", OBJ_LABEL, 0, 0, 0);
ObjectSetInteger(0, "equityLabel", OBJPROP_SELECTABLE, true);
ObjectSetInteger(0, "equityLabel", OBJPROP_CORNER, CORNER_RIGHT_UPPER);
ObjectSetInteger(0, "equityLabel", OBJPROP_XDISTANCE, 150);
ObjectSetInteger(0, "equityLabel", OBJPROP_YDISTANCE, 40);
ObjectSetInteger(0, "equityLabel", OBJPROP_FONTSIZE, 10);
ObjectSetString(0, "equityLabel", OBJPROP_FONT, "Verdana");
ObjectSetInteger(0, "equityLabel", OBJPROP_COLOR, colour);
ObjectSetString(0, "equityLabel", OBJPROP_TEXT, "EQUITY: " + equityString);
//void OnStart()
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[]) 
{
   color BuyColor = clrBlue;
   color SellColor = clrRed;

   // Request trade history
   HistorySelect(0, TimeCurrent());

   // Create objects for trade history
   string name;
   uint total = HistoryDealsTotal();
   ulong ticket = 0;
   double price;
   double profit;
   datetime _time;
   string symbol;
   long type;
   long entry;
:
:
   return(rates_total);
}
//int OnCalculate(const int rates_total, const int prev_calculated, const int begin, const double &price[])
//{
   //return(rates_total);
//}
 
Nagisa Unada #:
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[]



Thank You!!