MQL4 chart drawing

MQL4 chart objects with explicit ownership

Every MQL4 chart object is identified by a name within a chart. Create it with ObjectCreate, check the result, set properties with the matching ObjectSetInteger, ObjectSetDouble, or ObjectSetString function, and delete only objects owned by your program.

Open the Coding Agent, then choose MQL4 from the language menu.

CreatePriceLine.mq4.mq4
#property strict

void OnStart()
  {
   string name = "Pineify_Example_Line";
   RefreshRates();

   if(ObjectFind(0, name) >= 0)
      ObjectDelete(0, name);

   if(!ObjectCreate(0, name, OBJ_HLINE, 0, 0, Bid))
     {
      Print("ObjectCreate failed. Error ", GetLastError());
      return;
     }

   ObjectSetInteger(0, name, OBJPROP_COLOR, clrDodgerBlue);
   ObjectSetInteger(0, name, OBJPROP_STYLE, STYLE_DASH);
   ObjectSetInteger(0, name, OBJPROP_WIDTH, 1);
   ObjectSetString(0, name, OBJPROP_TEXT, "Example price line");
   ChartRedraw(0);
  }
  • Names identify chart objects
  • Creation can fail
  • Use typed property setters
  • Delete only owned names

Object type determines anchor coordinates

A horizontal line needs a price anchor, while a trend line needs time and price coordinates for multiple points. Text, labels, arrows, rectangles, and Fibonacci tools each have their own coordinate and property requirements.

ObjectCreate returns false when the request fails. Check GetLastError immediately instead of assuming a later property call created or repaired the object.

ObjectCoordinatesCommon use
OBJ_HLINEOne priceLevel
OBJ_VLINEOne timeEvent marker
OBJ_TRENDTwo time-price pointsTrend segment or ray
OBJ_LABELScreen corner and pixel offsetsFixed chart UI

Names are the ownership boundary

Use a stable prefix plus symbol, timeframe, instance identifier, or ticket when several program instances share a chart. A generic name such as Line1 can collide with a manual object or another EA.

Cleanup should target the same prefix or explicit name set that the program created. ObjectDelete and bulk deletion mutate the chart, so broad filters need the same care as order filters.

  • Prefix every owned object name
  • Check ObjectFind before update or replacement
  • Use the setter matching the property data type
  • Redraw after a batch instead of after every property

Indicator arrows and chart arrows are different systems

A custom indicator can draw arrow values through an indicator buffer and SetIndexArrow. A chart object such as OBJ_ARROW is an independently named object with coordinates and properties.

Choose a buffer for one value per bar and chart objects for individually managed annotations. Mixing both without a cleanup plan can leave stale marks after recalculation.

Review the complete example

The script owns one prefixed name, removes an earlier copy, verifies creation, applies typed properties, and redraws the current chart. It does not delete unrelated objects.

The example mutates the current chart but not the trading account. Keep object names scoped so cleanup cannot remove manual analysis or another program output.

CreatePriceLine.mq4.mq4
#property strict

void OnStart()
  {
   string name = "Pineify_Example_Line";
   RefreshRates();

   if(ObjectFind(0, name) >= 0)
      ObjectDelete(0, name);

   if(!ObjectCreate(0, name, OBJ_HLINE, 0, 0, Bid))
     {
      Print("ObjectCreate failed. Error ", GetLastError());
      return;
     }

   ObjectSetInteger(0, name, OBJPROP_COLOR, clrDodgerBlue);
   ObjectSetInteger(0, name, OBJPROP_STYLE, STYLE_DASH);
   ObjectSetInteger(0, name, OBJPROP_WIDTH, 1);
   ObjectSetString(0, name, OBJPROP_TEXT, "Example price line");
   ChartRedraw(0);
  }
Where Pineify fits

Move from the rule to editable MQL4 source

Pineify can generate or revise one self-contained .mq4 file with MQL4-specific reference context. Its independent static checker returns MetaEditor diagnostics so you can repair source issues before testing in your own MT4 environment.

Open MQL4 Coding Agent

Static diagnostics do not prove runtime behavior, backtest results, broker compatibility, or live-trading safety.

Pineify MQL4 Coding Agent with MQL4 selected and an editable MetaTrader 4 code artifact

A practical workflow

  1. 1

    Choose the object type

    List its required time, price, or screen coordinates.

  2. 2

    Create an owned name

    Use a prefix that cannot collide with manual or other program objects.

  3. 3

    Set typed properties

    Check creation, then set integer, double, and string properties with matching APIs.

  4. 4

    Update and clean up

    Move existing objects when possible and delete only the names this instance owns.

Frequently asked questions

How do I create a chart object in MQL4?

Call ObjectCreate with the chart ID, unique name, object type, subwindow, and required coordinates, then check its boolean result.

Why does ObjectCreate fail?

Common causes include a duplicate name, invalid coordinates, an invalid subwindow, or unsupported property assumptions. Read GetLastError after failure.

How do I update an existing object?

Find it by name, then use ObjectMove or the typed ObjectSet functions instead of creating another object with the same name.

Should an EA delete all chart objects on exit?

No. It should delete only the objects it owns, usually identified by a controlled name prefix.

What is the difference between SetIndexArrow and OBJ_ARROW?

SetIndexArrow configures an indicator-buffer drawing style. OBJ_ARROW creates a separately named chart object.

Last verified against the linked official references on . Compile and test the final source in the MetaTrader 4 build, broker, symbol, and account environment you plan to use.

Continue with one reviewable MQL4 file

Define the requirement, inspect the generated source, repair diagnostics, and test the behavior in your own MT4 environment.

Open Coding Agent