line.new() in MQL5: OBJ_TREND, rays and time coordinates

Short answer

line.new() becomes OBJ_TREND, created with two time and price anchors; extend maps to OBJPROP_RAY_RIGHT and OBJPROP_RAY_LEFT. The one real translation step is the x axis: Pine lines are usually placed by bar index, while MetaTrader objects only understand time.

line.new() draws a segment between two bar/price points and can extend it into a ray. MQL5's OBJ_TREND is the same object: two anchor points given as datetime and double, a colour, a style, a width, and two booleans, OBJPROP_RAY_RIGHT and OBJPROP_RAY_LEFT, that stand in for Pine's extend. The one real translation step is the x axis: Pine lines are usually placed by bar index, MetaTrader objects only understand time.

What line.new() computes

line.new(x1, y1, x2, y2, xloc, extend, color, style, width) creates a line from (x1, y1) to (x2, y2). With xloc.bar_index (default) the x values are bar indexes and may point past the last bar; with xloc.bar_time they are UNIX times in milliseconds. extend is extend.none, extend.right, extend.left or extend.both; style is line.style_solid, style_dotted, style_dashed, or one of the arrow styles (style_arrow_left, style_arrow_right, style_arrow_both). The call returns a line id for line.set_xy1, line.set_xy2, line.set_color, line.delete and line.get_price(x), which evaluates the line's equation at a bar. The script keeps at most max_lines_count lines (default 50, maximum 500), oldest deleted first, and lines created on the realtime bar are rolled back and recreated each tick.

The MQL5 equivalent

The Pine version draws the highest high of the last 20 bars as a level extended to the right, plus a dashed segment placed with time coordinates.

//@version=6
indicator("Line demo", overlay=true)
len = input.int(20)
hh = ta.highest(high, len)
var line lvl = na
if barstate.islast
    line.delete(lvl)
    lvl := line.new(bar_index - len, hh, bar_index, hh, extend=extend.right, color=color.orange, width=2)
    line.new(time[len], hh, time, hh, xloc=xloc.bar_time, color=color.gray, style=line.style_dashed)

In MQL5 both become OBJ_TREND objects with fixed names, updated in place when the last bar changes. bar_index - len is time[last - InpLength]; a time coordinate from xloc.bar_time is used as it is once divided by 1000. extend.right is OBJPROP_RAY_RIGHT = true; set both ray properties explicitly rather than trusting the terminal default.

#property indicator_chart_window
#property indicator_buffers 0
#property indicator_plots   0

input int InpLength = 20;   // Lookback

// line.new(x1, y1, x2, y2, extend=extend.right, color, width): an OBJ_TREND with a right ray
void LineNew(const string nm, const datetime t1, const double p1, const datetime t2, const double p2,
             const color clr, const int width, const ENUM_LINE_STYLE style, const bool rayRight)
{
   if(ObjectFind(0, nm) < 0)
      ObjectCreate(0, nm, OBJ_TREND, 0, t1, p1, t2, p2);
   else
     {                                               // line.set_xy1 / set_xy2
      ObjectSetInteger(0, nm, OBJPROP_TIME,  0, t1);
      ObjectSetDouble (0, nm, OBJPROP_PRICE, 0, p1);
      ObjectSetInteger(0, nm, OBJPROP_TIME,  1, t2);
      ObjectSetDouble (0, nm, OBJPROP_PRICE, 1, p2);
     }
   ObjectSetInteger(0, nm, OBJPROP_COLOR, clr);
   ObjectSetInteger(0, nm, OBJPROP_WIDTH, width);
   ObjectSetInteger(0, nm, OBJPROP_STYLE, style);
   ObjectSetInteger(0, nm, OBJPROP_RAY_RIGHT, rayRight);   // extend.right
   ObjectSetInteger(0, nm, OBJPROP_RAY_LEFT, false);
   ObjectSetInteger(0, nm, OBJPROP_SELECTABLE, false);
}

int OnInit() { return INIT_SUCCEEDED; }

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 <= InpLength)
      return 0;
   // `if barstate.islast`: only the newest bar draws, and the same two objects are reused
   int last = rates_total - 1;
   double hh = high[last];
   for(int k = last - InpLength + 1; k < last; k++)
      hh = MathMax(hh, high[k]);
   // xloc.bar_index: bar_index - len  ->  time[last - InpLength]
   LineNew("lvl_hh", time[last - InpLength], hh, time[last], hh, clrOrange, 2, STYLE_SOLID, true);
   // xloc.bar_time: the x values are already times, so they map directly
   LineNew("lvl_hh_dash", time[last - InpLength], hh, time[last], hh, clrGray, 1, STYLE_DASH, false);
   return rates_total;
}

void OnDeinit(const int reason) { ObjectsDeleteAll(0, "lvl_"); }

The remaining arguments map directly: color is OBJPROP_COLOR, width is OBJPROP_WIDTH (1 to 5; a width above 1 forces a solid style), and style is OBJPROP_STYLE with STYLE_SOLID, STYLE_DOT and STYLE_DASH. The arrow styles have no OBJ_TREND equivalent; OBJ_ARROWED_LINE draws a segment with an arrowhead at its second point and covers line.style_arrow_right. A line whose x2 lies in the future is fine on both platforms: use time[last] + n * PeriodSeconds() for a point n bars ahead.

Where the two differ

TopicPine ScriptMQL5
x coordinateBar index or ms time, chosen by xlocAlways a datetime in seconds
Extensionextend.* constantTwo booleans, OBJPROP_RAY_RIGHT and OBJPROP_RAY_LEFT
Identity and limitHandle; max_lines_count with oldest-first deletionUnique name; unlimited; you delete
Realtime barRolled back per tickPersistent; update the same named object
Price at a barline.get_price(x)ObjectGetValueByTime(0, name, t)
Arrow stylesThree arrow stylesOBJ_ARROWED_LINE for one direction only
Width and styleIndependentDotted and dashed only at width 1
Gaps in timeBars are evenly spaced; weekends do not existA time between two bars snaps to the next bar; a ray through a weekend is drawn through it

What the Stratlate converter emits

line.new(x1, y1, x2, y2) becomes pine_line_new(x1, y1, x2, y2), a runtime helper that creates an OBJ_TREND named pine_line_N and returns an integer id used by pine_line_set_xy1, pine_line_set_xy2, pine_line_set_color and pine_line_delete. Bar-index x values are converted to times by the helper; an xloc.bar_time argument is emitted as time[i] * 1000.0 and recognised by its magnitude. When the call also passes extend, color, style or width, the creation is wrapped in pine_line_style(...), which sets OBJPROP_COLOR, OBJPROP_STYLE (line.style_dashed arrives as STYLE_DASH), OBJPROP_WIDTH and OBJPROP_RAY_RIGHT / OBJPROP_RAY_LEFT from the extend constant (1 right, 2 left, 3 both). The runtime caps live lines at 50, the max_lines_count default, deleting the oldest. The example above converts CLEAN; compile it and view the chart, since the ray and the time snapping are easiest to judge visually.

Pitfalls

  • Passing a bar index to ObjectCreate. The value is read as seconds since 1970, and the line is drawn in January 1970, far off the left of the chart.
  • Relying on the default ray settings. Create the line and set OBJPROP_RAY_RIGHT and OBJPROP_RAY_LEFT yourself; a level that should stop at the last bar otherwise runs across the screen, or the reverse.
  • Reading time[last - InpLength] when rates_total is smaller than the lookback. Guard it; the array index goes negative on a fresh symbol with little history.
  • Recreating the line with a new name on each tick. Hundreds of identical lines stack up; keep one name and move its anchors.
  • Setting STYLE_DASH with width 2. MetaTrader draws it solid; dashed and dotted styles exist only at width 1.
  • Leaving the objects behind in OnDeinit. They stay on the chart after the indicator is removed and get saved in the template.

FAQ

How do I place a line endpoint in the future?

Add whole bar periods to the last bar's time: time[rates_total - 1] + n * PeriodSeconds(). The chart extends its time axis to the right as far as needed, the same way Pine accepts bar_index + n.

What is the MQL5 equivalent of line.get_price?

ObjectGetValueByTime(0, name, t) returns the price of the (extended) line at time t. It works for an OBJ_TREND whether or not the rays are on, as long as t lies on a bar.

Do converted lines slow the chart down?

Not at the Pine limits. A few hundred chart objects are fine; the slowdown appears when a script recreates them on every tick instead of updating them, which is why naming by bar time and reusing objects matters more than the object count.

Convert the whole script

Paste a Pine Script indicator or strategy and get MQL5 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 converter

Related