🚀 AI One-Click Generation of mql4 Strategy Code

How to Draw Graphical Objects on a Chart with Mql4?

mql4 | Published on: 2025/11/26
The following content is generated by EasyQuant .

Problem Description

How can I draw a horizontal line and some text on the chart using code?

Solution

To draw objects on an MT4 chart, you use the ObjectCreate() function combined with various ObjectSet... functions to define properties like color, size, and text content.

Below is a complete MQL4 script. You can copy this code into a new Script file in MetaEditor, compile it, and drag it onto any chart to see the result.

MQL4 Script: Draw Line and Text

//+------------------------------------------------------------------+
//|                                        DrawLineAndText.mq4       |
//|                        Copyright 2023, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2023"
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict
#property show_inputs // Allows input window to appear

//--- Input Parameters
input double   InpPrice       = 0.0;      // Price level (0.0 = current Bid)
input color    InpLineColor   = clrRed;   // Line Color
input int      InpLineWidth   = 2;        // Line Width
input string   InpText        = "Support";// Text Label
input color    InpTextColor   = clrWhite; // Text Color
input int      InpFontSize    = 10;       // Font Size

//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
{
   // 1. Determine the price level
   double priceLevel = InpPrice;
   
   // If user left input as 0.0, use the current Bid price
   if(priceLevel <= 0.0) priceLevel = Bid;

   // 2. Define Object Names
   string lineName = "Custom_HLine";
   string textName = "Custom_Text";
   
   // 3. Clean up previous objects with the same name to avoid errors
   if(ObjectFind(0, lineName) >= 0) ObjectDelete(0, lineName);
   if(ObjectFind(0, textName) >= 0) ObjectDelete(0, textName);

   // --- DRAWING THE HORIZONTAL LINE ---
   // ObjectCreate(ChartID, Name, Type, SubWindow, Time1, Price1)
   if(ObjectCreate(0, lineName, OBJ_HLINE, 0, 0, priceLevel))
   {
      // Set Line Properties
      ObjectSetInteger(0, lineName, OBJPROP_COLOR, InpLineColor);
      ObjectSetInteger(0, lineName, OBJPROP_WIDTH, InpLineWidth);
      ObjectSetInteger(0, lineName, OBJPROP_STYLE, STYLE_SOLID);
      ObjectSetInteger(0, lineName, OBJPROP_SELECTABLE, true); // Allow user to select/move it
   }
   else
   {
      Print("Failed to create line. Error code: ", GetLastError());
   }

   // --- DRAWING THE TEXT ---
   // We use OBJ_TEXT to anchor text to a specific time and price
   // Time[0] places it at the current candle, priceLevel places it at the line
   if(ObjectCreate(0, textName, OBJ_TEXT, 0, Time[0], priceLevel))
   {
      // Set Text Content
      ObjectSetString(0, textName, OBJPROP_TEXT, InpText + " (" + DoubleToString(priceLevel, Digits) + ")");
      
      // Set Visual Properties
      ObjectSetString(0, textName, OBJPROP_FONT, "Arial");
      ObjectSetInteger(0, textName, OBJPROP_FONTSIZE, InpFontSize);
      ObjectSetInteger(0, textName, OBJPROP_COLOR, InpTextColor);
      
      // Anchor the text so it sits slightly above the line (Bottom_Left corner of text is at the price)
      ObjectSetInteger(0, textName, OBJPROP_ANCHOR, ANCHOR_BOTTOM_LEFT);
   }
   else
   {
      Print("Failed to create text. Error code: ", GetLastError());
   }

   // 4. Redraw the chart to make objects appear immediately
   ChartRedraw(0);
}
//+------------------------------------------------------------------+

Code Explanation

  1. ObjectCreate: This is the core function.

    • Horizontal Line (OBJ_HLINE): Requires only a price coordinate. The time coordinate is ignored (set to 0).
    • Text (OBJ_TEXT): Requires both a Time and a Price coordinate. In the example, I used Time[0] (current candle time) and the priceLevel.
  2. ObjectSetInteger / ObjectSetString: These functions modify the properties of the object created.

    • OBJPROP_COLOR: Sets the color.
    • OBJPROP_WIDTH: Sets the thickness of the line.
    • OBJPROP_TEXT: Sets the actual string content for the text object.
    • OBJPROP_ANCHOR: Crucial for text. ANCHOR_BOTTOM_LEFT ensures the text sits on top of the line rather than being centered over it (which would make it hard to read).
  3. ObjectFind & ObjectDelete: Before creating an object, it is best practice to check if an object with that specific name already exists. If it does, we delete it first to ensure we are creating a fresh object with the new settings.

  4. ChartRedraw: Forces the terminal to refresh the chart immediately so the user sees the drawing without waiting for the next incoming tick.

OBJ_TEXT vs. OBJ_LABEL

  • OBJ_TEXT (Used above): Moves with the chart. If you scroll back in history, the text moves with the candles. It is bound to a specific Time and Price.
  • OBJ_LABEL: Stays fixed on the screen (e.g., "Top Left Corner"). It does not move when you scroll the chart. It is bound to X and Y pixel coordinates.

Q&A: MQL4 Object Drawing

Q: How do I make the text stay in the corner of the screen instead of moving with the price?
A: Change the object type from OBJ_TEXT to OBJ_LABEL. Instead of Time and Price, you will need to set OBJPROP_CORNER (to define which corner) and OBJPROP_XDISTANCE / OBJPROP_YDISTANCE (pixels from that corner).

Q: How can I make the line a ray (infinite) or a segment?
A: OBJ_HLINE is always infinite horizontally. If you want a line segment that starts and stops at specific times, use OBJ_TREND. You can set OBJPROP_RAY_RIGHT to false to make it a segment, or true to make it a ray.

Q: Why isn't my object appearing?
A: Check GetLastError() in the Experts tab. Common errors include:

  1. Duplicate names (trying to create an object that already exists).
  2. Invalid coordinates (e.g., Price is 0 or Time is 0 for objects that require them).
  3. The object is created but is off-screen (price is too high/low).