Build notes

ObjectGetValueByTime in MQL5: MT5 Reads a Trendline by Bar, Not by Timestamp

Batdorj Byambadorj · · 5 min read

I needed one number from MetaTrader 5: the price of a trendline right now. My Expert Advisor compares that number with the bid and ask a few times a second and sends an alert when price crosses the line.

MQL5 has a function for it, ObjectGetValueByTime. I could not make it the engine's only source. An alert has to outlive its chart, so the line is stored in a file as two anchors and the engine does its own arithmetic. But I could use the function as a referee: every trendline a user draws is priced twice, once by my code and once by MT5, and the two numbers are compared.

My first version lost that comparison. Finding out why took two evenings of measuring, and it comes down to one sentence: MT5 reads a trendline by bar, not by timestamp.

Three models, one measurement

A trendline is two anchors. To price it at some moment you interpolate between them, and the only question that matters is: interpolate in what?

  • Clock time. Seconds elapsed between the anchors.
  • Fractional bars. The bar index, plus how far the moment sits inside its bar.
  • Whole bars. The bar index and nothing else.

The test line was on a gold M15 chart: 4,103.89 at 18:55 on 8 October to 4,202.45 at 16:10 the next day. I asked MT5 for its value at three moments and fitted the answers against each model.

Model What it predicts What MT5 returned
Clock time 4,153.17 halfway between the anchors 4,151.05. Refuted
Fractional bars one fixed distance between the anchors a different distance at every reading: 80.39, 79.98, 81.57. Refuted
Whole bars a ratio of 1.08333 between two readings 1.08314. Agrees to 0.017%

So the function turns the time you pass into a bar index and interpolates on that.

The value does not move inside a bar. It is the value at that bar's index. On H1 the level steps once an hour and then holds. That is what the chart draws.

The same two anchors give different prices on different timeframes. A time becomes a different index on M15 than on H1, so there is no single true value. A stored line has to remember the timeframe it was drawn on. Mine keeps it next to the anchors.

Across four hourly bars, MetaTrader 5 gives a trendline one value per bar, a staircase, while clock-time interpolation gives a value that moves every second. The two agree only at each bar's open.

The anchor in the future

iBarShift is the obvious way to turn a time into an index. Ask it about a time that has not happened yet and it answers 0, the current bar.

That is not an edge case, because of how people draw trendlines: the right-hand anchor is usually dragged out ahead of price. Every such anchor collapsed onto the current bar, the line came out shorter than the one on the screen, and on gold the two disagreed by 10.95 dollars.

Past the newest bar there is no index to look up. The count has to continue by arithmetic, one bar per PeriodSeconds.

The future is stepped too

I continued that count as a fraction: seconds past the newest bar's open, divided by the period. The next evening the referee reported a mismatch on a EURUSD line that came and went. Restarting the EA seemed to cure it.

It had not. MT5 counts whole bars out there as well. My fraction ran ahead of it by exactly the distance "now" had travelled into the current bar:

Minutes into the M15 bar Predicted gap, in bars Measured gap, in bars
7.5 0.50 0.52
13.7 0.91 0.91
14.4 0.96 0.96
1.8 0.12 not reported
9.0 0.60 0.60

The fourth row is the reason it looked random. My check forgave anything under half a bar, so the error only spoke up in the second half of each bar, and a restart that happened early in a bar looked like a fix.

The real fix is one MathFloor. After it, six minutes into a bar: mine 1.12176, MT5 1.12176, 0.00 bars apart.

// Where a moment sits on the chart's axis, in bars.
// Newest bar = 0, older bars negative, the future positive.
bool BarPos(const string sym, const ENUM_TIMEFRAMES tf,
            const datetime t, double &pos)
  {
   int      period = PeriodSeconds(tf);
   datetime t0     = iTime(sym, tf, 0);        // newest bar's open
   if(period <= 0 || t0 == 0)
      return(false);                           // history not there yet

   if((long)t >= (long)t0 + period)            // past the newest bar
     {
      pos = MathFloor((double)((long)t - (long)t0) / (double)period);
      return(true);
     }

   int i = iBarShift(sym, tf, t, false);
   if(i < 0)
      return(false);
   datetime bi = iTime(sym, tf, i);
   if(bi == 0 || bi > t)
      return(false);                           // older than what is loaded

   pos = -(double)i;
   return(true);
  }

// price = p1 + (p2 - p1) * (pos(t) - pos(t1)) / (pos(t2) - pos(t1))

The ends of the line are on the clock

One more, found the same evening as the first measurement. An AUDUSD H1 line that ended at 16:00, not a ray, returned 0.69514 when asked about 16:00 and no value when asked about 16:29. Same bar, same index, different answer.

So MT5 uses two coordinate systems inside one function. What the line is worth comes off the bar grid. Whether the line exists at that moment is judged on the clock. Decide both by bar index, as I first did, and the engine keeps reading a line for the rest of the hour after the chart has stopped drawing it.

Why keep a referee

The browser version of this alert engine had nothing to be checked against. It interpolated by the clock, and after a weekend it stood 20.79 dollars away from the line on the chart. That went unnoticed for about three months. The trader's side of that story is in the weekend gap article.

Inside MT5 the second opinion costs one function call per line, so the EA keeps asking for it while it runs. That check is what caught the half-bar error above, a day after I thought the arithmetic was finished.

If you price a trendline yourself in MQL5

  • Turn every time into a bar position first. Interpolate on positions, never on seconds.
  • Inside history, use the whole index. Past the newest bar, count whole periods yourself. iBarShift answers 0 there.
  • Store the timeframe the line was drawn on together with its anchors.
  • For a line that is not a ray, decide where it ends by time, not by index.
  • Compare your number with ObjectGetValueByTime, measure the gap in bars rather than in price, and count "one side has a value, the other has none" as a disagreement.

Measured in October 2026 on one broker's MetaTrader 5. The numbers are from my own test lines.

Batdorj Byambadorj

Batdorj Byambadorj has been trading forex for his own account since 2007, mostly gold. He is the sole developer of fx-alert and writes every article on this site himself.

About the author

← All articles