calc_on_every_tick in MQL5: bar-close vs tick evaluation
Short answer
A default Pine strategy becomes a new-bar gate in OnTick: evaluate on the first tick of a bar using the closed bar at shift 1, then send the order. calc_on_every_tick = true is the same code without the gate, reading the forming bar at shift 0.
By default a Pine strategy is calculated once per bar, on the bar's close, and its market orders fill at the open of the next bar. calc_on_every_tick = true recalculates on every realtime tick instead, and process_orders_on_close = true fills orders at the close of the bar that generated them. An MQL5 EA has only OnTick, called on every price change; the default Pine model is a new-bar gate (evaluate on the first tick of a bar, using the closed bar at shift 1, and send the order then), and calc_on_every_tick is the same code without the gate, reading the forming bar at shift 0.
What calc_on_every_tick and process_orders_on_close compute
On historical bars TradingView has no ticks, so a backtest always evaluates once per bar on the close, whatever calc_on_every_tick says; the flag only changes behaviour on realtime bars, where the script reruns on every tick and can place orders mid-bar from a close that is really the latest price. That is why a strategy with the flag on can show different trades live than in its own backtest. Order timing is separate. With process_orders_on_close = false, an order generated on bar N fills at bar N+1's open. With true, it fills at bar N's close, which on historical bars is a price the script has just seen and could not have traded at in reality. Stops and limits set by strategy.exit are checked inside later bars either way, using the emulator's assumed path through open, high, low and close.
The MQL5 equivalent
//@version=6
strategy("Tick vs close demo", overlay = true,
calc_on_every_tick = false, // run once per bar, on its close
process_orders_on_close = false, // fill at the NEXT bar's open
default_qty_type = strategy.fixed, default_qty_value = 1)
fast = ta.ema(close, 9)
slow = ta.ema(close, 21)
if ta.crossover(fast, slow)
strategy.entry("L", strategy.long)
if ta.crossunder(fast, slow)
strategy.close("L")One input switches the EA between the two evaluation models. With InpEveryTick = false it is Pine's default: IsNewBar passes once per bar, the crossover is computed from shifts 1 and 2 (both closed), and Buy fills at roughly the new bar's open. With true it evaluates on every tick from shifts 0 and 1.
#include <Trade/Trade.mqh>
// false: Pine's default. Decide once per bar on the CLOSED bar (shift 1);
// the order fills at the open of the bar that has just started.
// true: calc_on_every_tick = true. Re-evaluate on every tick using the
// FORMING bar (shift 0); signals can appear and vanish within a bar.
input bool InpEveryTick = false;
input double InpLots = 0.10;
input long InpMagic = 60024;
CTrade trade;
int hFast = INVALID_HANDLE, hSlow = INVALID_HANDLE;
int OnInit()
{
hFast = iMA(_Symbol, _Period, 9, 0, MODE_EMA, PRICE_CLOSE);
hSlow = iMA(_Symbol, _Period, 21, 0, MODE_EMA, PRICE_CLOSE);
if(hFast == INVALID_HANDLE || hSlow == INVALID_HANDLE)
return INIT_FAILED;
trade.SetExpertMagicNumber(InpMagic);
trade.SetTypeFillingBySymbol(_Symbol);
return INIT_SUCCEEDED;
}
bool IsNewBar()
{
static datetime last = 0;
const datetime t = iTime(_Symbol, _Period, 0);
if(t == last)
return false;
last = t;
return true;
}
ulong LongTicket()
{
for(int i = PositionsTotal() - 1; i >= 0; i--)
{
const ulong t = PositionGetTicket(i);
if(t != 0 && PositionGetString(POSITION_SYMBOL) == _Symbol
&& PositionGetInteger(POSITION_MAGIC) == InpMagic)
return t;
}
return 0;
}
void OnTick()
{
const bool newBar = IsNewBar(); // must run on every tick
if(!InpEveryTick && !newBar)
return;
// shift 1 = last closed bar, shift 0 = bar still forming
const int shift = InpEveryTick ? 0 : 1;
double f[2], s[2]; // [0] = shift+1, [1] = shift
if(CopyBuffer(hFast, 0, shift, 2, f) != 2 || CopyBuffer(hSlow, 0, shift, 2, s) != 2)
return;
const bool crossUp = f[1] > s[1] && f[0] <= s[0];
const bool crossDown = f[1] < s[1] && f[0] >= s[0];
const ulong ticket = LongTicket();
if(crossUp && ticket == 0)
trade.Buy(InpLots, _Symbol, 0.0, 0.0, 0.0, "L");
if(crossDown && ticket != 0)
trade.PositionClose(ticket);
}IsNewBar() is called before the early return so its stored time stays current in both modes. There is no direct MQL5 equivalent of process_orders_on_close = true: when the EA learns that a bar has closed, the next bar has already begun, so the first tick of the new bar is the earliest fill. On liquid symbols that price is close to the previous close; across weekend gaps it is not.
Where the two differ
| Topic | Pine | MQL5 |
|---|---|---|
| Calls per bar | one (history); one per tick (realtime with the flag) | one per tick, always; you gate it |
| Backtest with tick evaluation | not possible; history is bar-close | "Every tick" models do evaluate per tick in history |
| Default fill | next bar's open | first tick of the next bar, at ask/bid |
process_orders_on_close | signal bar's close | not reproducible; nearest is the next bar's first tick |
| Closed-bar data | close on the bar being processed | iClose(_Symbol, _Period, 1), CopyBuffer(..., 1, ...) |
| Tester model | n/a | "Open prices only" is enough for a bar-close EA's signals; stops need "1 minute OHLC" or real ticks |
What the Stratlate converter emits
Every converted strategy uses the bar-close model. The emitted OnTick starts with a gate on iTime(NULL, 0, 0), appends the bar at shift 1 to its oldest-first price arrays, evaluates the script once and sends market orders, so they fill at the open of the bar that has just started, the same two prices TradingView uses by default; barstate.isconfirmed is true and barstate.isrealtime is false inside it. calc_on_every_tick and process_orders_on_close are not read and produce no code and no report note, so a script that relies on either is converted to the default timing. If the Pine strategy was built around fills at the signal bar's close, expect each converted trade one bar's open later; compile and backtest to see the size of the difference.
Pitfalls
- Evaluating every tick on shift 0 while the Pine script uses the default. Symptom: many more trades in MT5, entries inside bars on crosses that disappeared by the close.
- Gating on a new bar but reading shift 0. Symptom: indicators computed from a bar with one tick;
close > openis never true on that tick. - Comparing a Pine backtest with
calc_on_every_tick = trueagainst an MT5 every-tick test. Symptom: different trades, because TradingView's history was still evaluated per bar. - Expecting
process_orders_on_closefills in MT5. Symptom: every entry one gap worse than Pine on daily charts. - Calling
IsNewBar()only inside a condition. Symptom: the stored bar time goes stale and the next "new bar" fires late or twice.
FAQ
How do I make an MQL5 EA run only once per bar?
Keep the last seen iTime(_Symbol, _Period, 0) in a static variable and return from OnTick while it is unchanged. When it changes, a new bar has opened and the bar at shift 1 has just closed.
Does calc_on_every_tick change TradingView backtest results?
Not on historical bars, which are evaluated once at their close regardless. It changes realtime behaviour, so live results can differ from the backtest of the same script.
Can I fill at the signal bar's close in MetaTrader 5?
Not exactly. The close is only known once the next bar's first tick arrives, and that tick is the earliest price an order can get. The difference is usually a fraction of the spread, except across gaps.
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