barstate in MQL5: isconfirmed, islast, isrealtime, isnew
Short answer
MQL5 has no barstate, but each flag is an expression in OnCalculate: isconfirmed is i < rates_total-1, islast is i == rates_total-1, isrealtime is prev_calculated > 0, isfirst is i == 0, and isnew is a change in the bar's open time.
The barstate namespace tells a Pine script where the bar it is running on sits: first bar, last bar, closed or still forming, historical or live, first tick or a later one. MQL5 has no such variables, but every one of them is an expression over rates_total, prev_calculated and the loop index in an indicator, or a comparison of bar open times in an Expert Advisor.
What barstate computes
barstate.isfirst: true on the oldest bar (bar_index == 0).barstate.islast: true on the newest bar, whether or not the market is open.barstate.ishistory: true on bars that were already closed when the script loaded.barstate.isrealtime: true on bars that arrived while the script was running; on a live chart that is the forming bar and every bar that opens afterwards.barstate.isconfirmed: true when the script is running on the bar's final update. On history every bar is confirmed; on the realtime bar only the last tick is.barstate.isnew: true on the first update of a bar: every historical bar, and the first tick of each realtime bar.barstate.islastconfirmedhistory: true on the last closed historical bar.
A script that only wants closed-bar signals wraps them in if barstate.isconfirmed; one that counts ticks uses isnew to reset a varip counter.
The MQL5 equivalent
An indicator sees history and live updates through the same OnCalculate. prev_calculated == 0 is the history pass; later calls come one per tick. Inside the bar loop, i < rates_total - 1 is a closed bar, and the forming bar is i == rates_total - 1 on a call with prev_calculated > 0. isnew needs memory: the open time of the bar seen on the previous call.
//@version=6
indicator("barstate demo")
varip int ticks = 0
if barstate.isnew
ticks := 0
ticks += 1
plot(barstate.isconfirmed and close > open ? 1 : 0, "confirmed up bar", style=plot.style_histogram)
plot(barstate.isrealtime ? ticks : 0, "ticks on forming bar")#property indicator_separate_window
#property indicator_buffers 2
#property indicator_plots 2
#property indicator_type1 DRAW_HISTOGRAM
#property indicator_color1 clrDodgerBlue
#property indicator_label1 "confirmed up bar"
#property indicator_type2 DRAW_LINE
#property indicator_color2 clrOrangeRed
#property indicator_label2 "ticks on forming bar"
double UpBuf[];
double TicksBuf[];
int OnInit()
{
SetIndexBuffer(0, UpBuf, INDICATOR_DATA);
SetIndexBuffer(1, TicksBuf, INDICATOR_DATA);
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[])
{
static datetime formingBarTime = 0; // for barstate.isnew
static int ticks = 0; // a varip-style counter
int lastBar = rates_total - 1;
bool isRealtimeCall = prev_calculated > 0; // history pass is over
bool isNewBar = time[lastBar] != formingBarTime; // first call on this bar
if(isNewBar) { formingBarTime = time[lastBar]; ticks = 0; }
ticks++;
int start = prev_calculated > 0 ? prev_calculated - 1 : 0;
for(int i = start; i < rates_total; i++)
{
bool isLast = (i == lastBar); // barstate.islast
bool isConfirmed = !isLast || !isRealtimeCall; // barstate.isconfirmed on a live chart: i < lastBar
bool isRealtime = isLast && isRealtimeCall; // barstate.isrealtime
// Pine: plot(barstate.isconfirmed and close > open ? 1 : 0)
UpBuf[i] = (isConfirmed && close[i] > open[i]) ? 1.0 : 0.0;
// Pine: varip int ticks ... plot(barstate.isrealtime ? ticks : 0)
TicksBuf[i] = isRealtime ? ticks : 0.0;
}
return rates_total;
}In an Expert Advisor OnTick runs once per tick with no bar loop, so the equivalent of barstate.isnew is the standard new-bar test: datetime t = iTime(_Symbol, _Period, 0); if(t != lastBarTime) { lastBarTime = t; /* new bar */ }. A strategy that acts on confirmed bars reads shift 1 inside that block, which is the bar that has just closed.
Where the two differ
| Pine | MQL5 indicator (oldest-first loop over i) | MQL5 EA (OnTick) |
|---|---|---|
barstate.isfirst | i == 0 | n/a; the EA has no history pass |
barstate.islast | i == rates_total - 1 | always the current bar (shift 0) |
barstate.ishistory | prev_calculated == 0, or i < rates_total - 1 on later calls | n/a; in the Strategy Tester every tick is "live" |
barstate.isrealtime | prev_calculated > 0 && i == rates_total - 1 | every call |
barstate.isconfirmed | i < rates_total - 1 (the forming bar is never confirmed until the next bar opens) | work on shift 1 when a new bar is detected |
barstate.isnew | time[rates_total - 1] differs from the value stored on the previous call | iTime(_Symbol, _Period, 0) changed since last tick |
| Last tick of a bar | not observable: MT5 does not call the indicator "on close", it calls it on the next bar's first tick | same |
The last row is the real semantic gap. Pine's isconfirmed is true during the bar's final tick; MetaTrader has no final-tick event, so a confirmed-bar signal appears one tick later, when the next bar has opened.
What the Stratlate converter emits
With __pineBar as the loop index and __pineLastBar = rates_total - 1, the engine emits barstate.isfirst as (__pineBar == 0), islast as (__pineBar == __pineLastBar), isconfirmed and ishistory as (__pineBar < __pineLastBar), isrealtime as (__pineBar == __pineLastBar) and isnew as the constant true; in an Expert Advisor, which evaluates each bar once on close, isconfirmed becomes true and isrealtime false. These are exact on the history pass. On the forming bar isnew stays true on every tick, so a varip tick counter reset with if barstate.isnew reads 1 on every tick in the converted indicator; the report does not flag this, so check such counters by hand.
Pitfalls
- Testing
prev_calculated == 0for "history" and nothing else: after a history reload or a terminal reconnect the next call also hasprev_calculated == 0, so static state tied to "first run" must be reset there too. - Expecting an indicator call at bar close: it never comes. Signals gated on
isconfirmedfire on the first tick of the next bar, which also changes when alerts go out. - Using
isrealtimeas "the bar is open": in the Strategy Tester an indicator is recalculated per tick from the start, so the distinction between history and realtime is defined by the tester's progress, not by the clock. - Counting ticks in a static variable without a new-bar reset: the count keeps growing across bars. Symptom: the "ticks" plot never returns to 1.
- Putting the new-bar test inside the bar loop in an EA translation:
OnTickhas no loop; the test belongs at the top ofOnTickand runs once per tick.
FAQ
What is barstate.isconfirmed in MQL5?
Inside an indicator's bar loop it is i less than rates_total - 1: every bar except the forming one. MetaTrader never calls the indicator on a bar's final tick, so the forming bar becomes "confirmed" only when the next bar opens and the loop revisits it from prev_calculated - 1.
How do I detect a new bar in MQL5?
Remember the open time of the newest bar in a static datetime and compare it on each call: in an indicator time[rates_total - 1], in an EA iTime(_Symbol, _Period, 0). When it changes, a new bar has opened and the previous bar is closed.
Does the Strategy Tester have barstate.isrealtime?
Not as Pine means it. The tester feeds ticks in order and every tick is "live" from the EA's point of view; an indicator inside the tester sees prev_calculated grow as bars are added. Code that relies on a history/realtime distinction should rely on bar close detection instead.
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