The error occurs because OBJ_PRICE_LABEL is not a valid object type in MQL5. The correct object type for a price label is OBJ_LABEL, not OBJ_PRICE_LABEL. Here's how to fix it:
✅ Corrected Code Snippet (Fix for OBJ_PRICE_LABEL)
Replace this section:
//--- Draw the Price Label (OBJ_PRICE_LABEL)
if(ObjectCreate(0, labelName, OBJ_PRICE_LABEL, 0, 0, currentPrice))
{
ObjectSetInteger(0, labelName, OBJPROP_COLOR, InpLineColor);
string priceText = DoubleToString(currentPrice, _Digits);
ObjectSetString(0, labelName, OBJPROP_TEXT, priceText);
ObjectSetInteger(0, labelName, OBJPROP_XDISTANCE, 5);
ObjectSetInteger(0, labelName, OBJPROP_YDISTANCE, 0);
}
With this:
//--- Draw the Price Label (OBJ_LABEL)
if(ObjectCreate(0, labelName, OBJ_LABEL, 0, 0, currentPrice))
{
ObjectSetInteger(0, labelName, OBJPROP_COLOR, InpLineColor);
string priceText = DoubleToString(currentPrice, _Digits);
ObjectSetString(0, labelName, OBJPROP_TEXT, priceText);
ObjectSetInteger(0, labelName, OBJPROP_XDISTANCE, 5); // Offset from the Y-axis
ObjectSetInteger(0, labelName, OBJPROP_YDISTANCE, 0); // Align to the price level
}
🔍 Explanation
OBJ_PRICE_LABEL is invalid in MQL5. The correct object type for a price label is OBJ_LABEL.
OBJ_LABEL is a standard object type in MQL5 for creating text labels on the chart. It automatically aligns to the price scale when you pass a double value as the last parameter (e.g., currentPrice).
🧪 Full Fix in Context
Here’s the corrected DrawGrid() function with the fix applied:
void DrawGrid()
{
DeleteGrid();
double currentPrice = InpStartPrice - (InpNumLines * InpSpacing);
int totalLines = (InpNumLines * 2) + 1;
for(int i = 0; i < totalLines; i++)
{
string lineName = PREFIX + "Line_" + IntegerToString(i);
string labelName = PREFIX + "Label_" + IntegerToString(i);
// Draw horizontal line
if(ObjectCreate(0, lineName, OBJ_HLINE, 0, 0, currentPrice))
{
ObjectSetInteger(0, lineName, OBJPROP_COLOR, InpLineColor);
ObjectSetInteger(0, lineName, OBJPROP_WIDTH, InpLineWidth);
ObjectSetInteger(0, lineName, OBJPROP_STYLE, InpLineStyle);
ObjectSetInteger(0, lineName, OBJPROP_BACK, false);
ObjectSetInteger(0, lineName, OBJPROP_SELECTABLE, false);
}
// Draw price label
if(ObjectCreate(0, labelName, OBJ_LABEL, 0, 0, currentPrice))
{
ObjectSetInteger(0, labelName, OBJPROP_COLOR, InpLineColor);
string priceText = DoubleToString(currentPrice, _Digits);
ObjectSetString(0, labelName, OBJPROP_TEXT, priceText);
ObjectSetInteger(0, labelName, OBJPROP_XDISTANCE, 5); // Offset from Y-axis
ObjectSetInteger(0, labelName, OBJPROP_YDISTANCE, 0); // Align to price level
}
currentPrice += InpSpacing;
}
ChartRedraw();
}
📌 Notes
OBJ_LABEL is used for general text labels. If you want the label to stick to the Y-axis (price scale), you must pass a double value (e.g., currentPrice) as the last argument to ObjectCreate().
- If you still want to use
OBJ_PRICE_LABEL, you’d need to define it manually (not recommended). Stick to OBJ_LABEL for simplicity.
✅ Result
After applying the fix, the code should compile without errors. The price labels will now appear correctly on the chart, aligned to the price levels.
Let me know if you need further help integrating trading logic into the OnTick() function!