ObjectCreate in Pine Script: line.new, box.new, label.new
Short answer
Use Pine's typed drawing objects: OBJ_TREND is line.new, OBJ_RECTANGLE is box.new, OBJ_TEXT and OBJ_LABEL are label.new, and a screen panel is table.new. Keep the handle in a var and restyle it with line.set_*, box.set_* or label.set_*; use hline() for constant levels.
ObjectCreate(chart_id, name, type, subwindow, time1, price1, …) places a named graphical object on a MetaTrader chart; ObjectSet* styles it, ObjectMove moves its anchor points and ObjectDelete removes it, all by name. Pine Script has typed drawing objects instead, each created by a function that returns a handle: line.new for OBJ_TREND (and OBJ_HLINE/OBJ_VLINE with extend), box.new for OBJ_RECTANGLE, label.new for OBJ_TEXT, OBJ_ARROW* and OBJ_LABEL, and table.new for screen-anchored panels. Styling is passed to the constructor or set later with line.set_*, box.set_*, label.set_* on the handle.
What ObjectCreate computes
It adds an object to the chart's object list and returns true if the request was queued. Objects belong to the chart, not to the program: they survive the indicator unless OnDeinit deletes them, they are visible to every other program on the chart, and a second ObjectCreate with the same name fails, which is why code checks ObjectFind(0, name) < 0 first and moves the existing object otherwise. Anchors are (datetime, price) pairs; OBJ_HLINE uses only the price, OBJ_VLINE only the time, OBJ_LABEL and OBJ_RECTANGLE_LABEL use pixel distances from a corner instead. Properties include colour, style, width, ray extension (OBJPROP_RAY_RIGHT), fill, z-order (OBJPROP_BACK) and text. There is no fixed limit on the number of objects.
Pine drawings belong to the script. A handle is a value you store in a variable (normally var so it persists), the x coordinate is a bar index by default or a time with xloc = xloc.bar_time, and drawings disappear when the script is removed. Each script has a quota per drawing type set in the declaration: max_lines_count, max_boxes_count, max_labels_count, default 50, maximum 500. When the quota is exceeded the oldest drawings of that type are deleted automatically.
The Pine Script equivalent
A range box over the last n bars with a dashed midline, a trend line from the first to the last close and a text label, all updated in place on the latest bar.
//@version=6
indicator("Range box", overlay = true, max_boxes_count = 50, max_lines_count = 50, max_labels_count = 50)
len = input.int(30, "Range lookback, bars")
hi = ta.highest(high, len)
lo = ta.lowest(low, len)
// One handle per object, created on the first bar and moved afterwards (the ObjectFind/ObjectMove pattern).
var box rng = box.new(na, na, na, na, border_color = color.rgb(70, 130, 180), bgcolor = color.new(color.rgb(70, 130, 180), 85))
var line mid = line.new(na, na, na, na, extend = extend.both, style = line.style_dashed) // OBJ_HLINE
var line trend = line.new(na, na, na, na) // OBJ_TREND, no ray
var label txt = label.new(na, na, "", style = label.style_label_lower_left) // OBJ_TEXT
if barstate.islast
first = bar_index - len + 1
box.set_lefttop(rng, first, hi)
box.set_rightbottom(rng, bar_index, lo)
line.set_xy1(mid, first, (hi + lo) / 2)
line.set_xy2(mid, bar_index, (hi + lo) / 2)
line.set_xy1(trend, first, close[len - 1])
line.set_xy2(trend, bar_index, close)
label.set_xy(txt, bar_index, hi)
label.set_text(txt, "Range " + str.tostring(hi - lo, format.mintick))#property indicator_chart_window
#property indicator_plots 0
input int InpLen = 30; // Range lookback, bars
const string PFX = "rng_";
int OnInit()
{
return(INIT_SUCCEEDED);
}
void OnDeinit(const int reason)
{
ObjectsDeleteAll(0, PFX); // chart objects outlive the indicator otherwise
}
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[])
{
if(rates_total < InpLen)
return(0);
int last = rates_total - 1;
int first = rates_total - InpLen;
double hi = high[ArrayMaximum(high, first, InpLen)];
double lo = low[ArrayMinimum(low, first, InpLen)];
// Create once, then move: objects are found by name.
if(ObjectFind(0, PFX + "box") < 0)
{
ObjectCreate(0, PFX + "box", OBJ_RECTANGLE, 0, time[first], hi, time[last], lo);
ObjectSetInteger(0, PFX + "box", OBJPROP_COLOR, clrSteelBlue);
ObjectSetInteger(0, PFX + "box", OBJPROP_FILL, true);
ObjectSetInteger(0, PFX + "box", OBJPROP_BACK, true);
ObjectCreate(0, PFX + "mid", OBJ_HLINE, 0, 0, (hi + lo) / 2);
ObjectSetInteger(0, PFX + "mid", OBJPROP_STYLE, STYLE_DASH);
ObjectCreate(0, PFX + "trend", OBJ_TREND, 0, time[first], close[first], time[last], close[last]);
ObjectSetInteger(0, PFX + "trend", OBJPROP_RAY_RIGHT, false);
ObjectCreate(0, PFX + "txt", OBJ_TEXT, 0, time[last], hi);
}
ObjectMove(0, PFX + "box", 0, time[first], hi);
ObjectMove(0, PFX + "box", 1, time[last], lo);
ObjectSetDouble(0, PFX + "mid", OBJPROP_PRICE, (hi + lo) / 2);
ObjectMove(0, PFX + "trend", 0, time[first], close[first]);
ObjectMove(0, PFX + "trend", 1, time[last], close[last]);
ObjectMove(0, PFX + "txt", 0, time[last], hi);
ObjectSetString(0, PFX + "txt", OBJPROP_TEXT, "Range " + DoubleToString(hi - lo, _Digits));
return(rates_total);
}| MQL5 | Pine |
|---|---|
OBJ_TREND (OBJPROP_RAY_RIGHT) | line.new(x1, y1, x2, y2, extend = extend.right) |
OBJ_HLINE | Constant level: hline(price). Moving level: line.new(…, extend = extend.both) with y1 = y2 |
OBJ_VLINE | line.new(x, low, x, high, extend = extend.both), or bgcolor on that bar |
OBJ_RECTANGLE | box.new(left, top, right, bottom) |
OBJ_TEXT, OBJ_ARROW_UP | label.new(x, y, text, style = …); per-bar markers are better as plotshape |
OBJ_LABEL (pixels from a corner) | table.new(position.top_right, …) + table.cell |
ObjectFind / create-once | var handle created on the first bar |
ObjectMove(name, point, t, p) | line.set_xy1/set_xy2, box.set_lefttop/set_rightbottom, label.set_xy |
ObjectSetString(OBJPROP_TEXT) | label.set_text, box.set_text |
ObjectDelete | line.delete(handle) etc. |
Where the two differ
Names versus handles. MQL5 code builds names like "arrow_" + TimeToString(time[i]) to make one object per event and looks them up later. Pine has no lookup by name; keep handles in variables or in an array<line>, or a map<string, label> if you really need keys.
Quota. An MQL5 indicator that drew an arrow on every signal over 10,000 bars shows all of them. In Pine only the last 500 at most remain. For per-bar marks, use plotshape or plotchar, which are plots and have no such limit.
Coordinates. MetaTrader anchors are times. Pine's default x is bar_index; pass xloc = xloc.bar_time to use times (in milliseconds). Drawing into the future works with bar indices up to 500 bars ahead.
Ownership. No cleanup in OnDeinit is needed and none is possible; on the other hand a Pine script cannot read or modify objects drawn by the user or other scripts.
Line styles. line.style_solid, _dashed, _dotted and arrow ends exist; MetaTrader's STYLE_DASHDOT has no equivalent.
What the Stratlate converter emits
The MQL5 to Pine engine keeps MetaTrader's name-keyed model by emitting a runtime registry: f_objr holds map<string, line>, map<string, box>, map<string, label> and map<string, table> keyed by the object name, with xloc = xloc.bar_time (PinePrelude). ObjectCreate reads the OBJ_* type and the anchors from either argument layout (ExprLowering lowerObjectCreate): OBJ_TREND and the channel/Fibonacci family become a line.new between the two anchors, OBJ_RECTANGLE (and triangle/ellipse) a box.new, OBJ_HLINE a line with extend.both, OBJ_VLINE a vertical line, OBJ_TEXT and OBJ_ARROW* a gray text label, and OBJ_LABEL or a zero-anchored object a one-cell table. ObjectMove, ObjectSetString(OBJPROP_TEXT) and ObjectDelete reach the same object through the registry; colour, width, style, fill and ray properties set with ObjectSetInteger are not carried, and every call in the family is reported as an approximation. In the program above the object calls converted, but the indicator has no per-bar loop and computes its range with ArrayMaximum over absolute indices, so the report flagged MQLW0511 and the computed range was not usable; the hand-written ta.highest version is what that code needs. Review the drawings on a chart after converting.
Pitfalls
- Calling
line.newon every bar withoutvar. A new line per bar hits the quota, and only the last 50 remain, scattered over the chart. Create once and move, or delete the previous one. - Using
xloc.bar_indexwith times. Passingtimeas x withoutxloc = xloc.bar_timeputs the x coordinate millions of bars into the future. Symptom: a runtime error saying the object is too far from the current bar. - Drawing per-bar arrows with labels. Use
plotshape; labels are capped at 500. - Translating
OBJ_HLINEwith a series price ashline().hlineneeds an input or constant; a level that moves needsline.newwithextend.both. - Updating drawings on every historical bar. Wrap the updates in
if barstate.islastwhen only the final state matters; it is much faster.
FAQ
How many lines, boxes and labels can a Pine script draw?
Up to 500 of each type, set with max_lines_count, max_boxes_count and max_labels_count in indicator() or strategy(); the default is about 50. Older drawings of a type are removed automatically when the limit is passed.
What replaces OBJ_LABEL text in a chart corner?
A table: var t = table.new(position.top_right, 1, 3) and table.cell(t, 0, row, text) inside if barstate.islast. Tables stay in place when the chart scrolls, like pixel-anchored labels.
Can Pine read objects the user drew, like ObjectGetDouble on a manual line?
No. A script sees only its own drawings. User input of a price or time is done with input.price and input.time, which can be set by clicking on the chart (confirm = true).
Convert the whole program
Paste an MQL4 or MQL5 indicator or Expert Advisor and get Pine Script v6 with this and every other call mapped, checked against the original on the same bars, and every construct that could not be carried over reported.
Open the converterRelated
- Pine line.new, label.new, box.new as MQL5 objects
- line.new() in MQL5: OBJ_TREND, rays and time coordinates
- box.new() in MQL5: OBJ_RECTANGLE, fill colour and border width
- label.new() in MQL5: OBJ_TEXT, anchors, limits and deletion
- Convert MQL5 to Pine Script (v6) — online MT5 to TradingView converter