ta.obv in MQL5: iOBV, tick volume and the starting value
Short answer
Use iOBV(symbol, timeframe, VOLUME_TICK or VOLUME_REAL), which applies the same rule. The volume source and the starting point differ, so absolute OBV values are not comparable across platforms; compare slopes.
ta.obv is On-Balance Volume: a running total that adds the bar's volume when the close rose, subtracts it when the close fell and leaves it unchanged when the close was flat. It is a built-in variable, not a function, and always reads the chart's close and volume. MetaTrader 5's iOBV(symbol, timeframe, applied_volume) applies the same rule. What differs is the volume (tick counts versus traded volume) and the starting point, which makes absolute OBV values incomparable across platforms even when the code is right.
What ta.obv computes
Written out, ta.obv = ta.cum(math.sign(ta.change(close)) * volume). On each bar the sign of the close change (+1, -1 or 0) multiplies the bar's volume, and the products are summed from the first bar of the chart. The first bar has no previous close and contributes nothing. Because it is cumulative, the value on any bar depends on every bar since the start of the loaded history: change how much history is loaded and the whole line shifts up or down by a constant. The shape (slope, highs and lows, divergences against price) is what carries information, and that is what you compare between platforms. On the realtime bar the last step changes sign whenever the close crosses the previous close, and its size grows with every tick of volume.
The MQL5 equivalent
//@version=6
indicator("OBV ref")
plot(ta.obv, "OBV")The MQL5 version offers the volume type as an input, because the right choice depends on the symbol. It starts the total at 0 on the first bar, as the converter does.
//+------------------------------------------------------------------+
//| On-Balance Volume matching ta.obv |
//+------------------------------------------------------------------+
#property indicator_separate_window
#property indicator_buffers 1
#property indicator_plots 1
#property indicator_label1 "OBV"
#property indicator_type1 DRAW_LINE
#property indicator_color1 clrDodgerBlue
input ENUM_APPLIED_VOLUME InpVolume = VOLUME_TICK; // Volume (real if the broker supplies it)
double ObvBuf[];
int OnInit()
{
SetIndexBuffer(0, ObvBuf, 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[])
{
int start = prev_calculated > 1 ? prev_calculated - 1 : 0;
for(int i = start; i < rates_total; i++)
{
if(i == 0) { ObvBuf[0] = 0.0; continue; } // Pine starts the running total at 0
const double v = (double)(InpVolume == VOLUME_REAL ? volume[i] : tick_volume[i]);
const double d = close[i] - close[i - 1];
ObvBuf[i] = ObvBuf[i - 1] + (d > 0.0 ? v : d < 0.0 ? -v : 0.0);
}
return rates_total;
}The built-in handle gives the same steps:
// iOBV(symbol, timeframe, applied_volume): the same running total.
// Its first value is the first bar's volume, not 0.
int obvHandle = iOBV(_Symbol, _Period, VOLUME_TICK); // or VOLUME_REALWhere the two differ
| Aspect | Pine ta.obv | MQL5 iOBV |
|---|---|---|
| Step rule | +volume if close rose, -volume if it fell, 0 if flat | Identical |
| Volume | The data feed's volume | VOLUME_TICK (tick count) or VOLUME_REAL (often all zeros on forex) |
| Start | First bar of TradingView's loaded history | First bar of the MT5 chart, total starts at that bar's volume |
| Absolute level | Depends on the loaded history | Depends on the loaded history |
| Form | Variable, no arguments | Handle with one volume argument |
Two charts that start on different dates add up different bars, so their OBV lines are offset by a constant even with identical data. Compare the change in OBV over a window, or the direction of the line, not its value. The volume row is the real source of disagreement: on a forex chart MetaTrader's tick volume counts price updates, not traded size, and an OBV built from it can diverge from TradingView's on news bars where a few large trades moved the price.
What the Stratlate converter emits
The converter replaces ta.obv with its runtime helper pine_obv(close, volume, bar). The helper keeps the running total in its own internal array (there is no call site to attach state to, because ta.obv is a variable), starts it at 0 on the first bar and adds the signed volume per bar, so the forming bar is recomputed from the previous total rather than compounded. The generated OnCalculate passes real volume when the newest bar has a non-zero volume[] and tick_volume[] otherwise. The mapping is reported as exact; the test script above converts CLEAN. Compile and backtest the result, and judge it by its slope against TradingView, not its level.
Pitfalls
- Comparing OBV values between TradingView and MetaTrader. They differ by a constant that depends on where each chart's history starts; that is not a conversion error.
- Hard-coding a level, such as
obv > 1000000. The condition depends on how much history is loaded and changes when the broker adds bars. - Choosing
VOLUME_REALon a forex symbol. The volume array is zero, every step is 0 and the OBV is a flat line. - Adding the volume when the close is equal to the previous close. Flat bars contribute nothing in both platforms; counting them biases the line upward.
- Recomputing the total from bar 0 on every tick. It works, but the cost grows with the chart; carry the previous bar's total in a buffer.
FAQ
Is iOBV the same as ta.obv?
The rule is the same: add volume on an up close, subtract it on a down close. The values differ by a constant because each chart starts summing on a different bar and MetaTrader starts with the first bar's volume, and they differ in shape where the volume series differ.
Should I use tick volume or real volume for OBV in MetaTrader 5?
Real volume when the symbol has it (exchange-traded futures and stocks at many brokers), tick volume otherwise. If the Volume column in Market Watch is empty for the symbol, only tick volume is available.
Why is my converted OBV shifted up or down compared with TradingView?
OBV is a cumulative sum from the first loaded bar. TradingView and MetaTrader almost never load the same first bar, so the totals carry different histories. The slope and the turning points are what should match.
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