On Balance Volume

Technical Analysis Indicator: obv

Fork me on GitHub

Function Prototype

/* On Balance Volume */
/* Type: indicator */
/* Input arrays: 2    Options: 0    Output arrays: 1 */
/* Inputs: close, volume */
/* Options: none */
/* Outputs: obv */
int ti_obv_start(TI_REAL const *options);
int ti_obv(int size,
      TI_REAL const *const *inputs,
      TI_REAL const *options,
      TI_REAL *const *outputs);

Description

This documentation is still a work in progress. It has omissions, and it probably has errors too. If you see any issues, or have any general feedback, please get in touch.

The On Balance Volume indicator calculates a running total of volume. Volume is added on up-days and subtracted on down days. The calculation is as follows:

$$obv_{t} =\begin{cases} obv_{t-1} + volume_{t} & \mathrm{if} \; close_{t} > close_{t-1} \\obv_{t-1} - volume_{t} & \mathrm{if} \; close_{t} \lt close_{t-1} \\0 & \text{else} \end{cases} $$

See Also

References

Example Usage

Calling From C

/* Example usage of On Balance Volume */
/* Assuming that 'close' and 'volume' are pre-loaded arrays of size 'in_size'. */
TI_REAL *inputs[] = {close, volume};
TI_REAL options[] = {}; /* No options */
TI_REAL *outputs[1]; /* obv */

/* Determine how large the output size is for our options. */
const int out_size = in_size - ti_obv_start(options);

/* Allocate memory for output. */
outputs[0] = malloc(sizeof(TI_REAL) * out_size); assert(outputs[0] != 0); /* obv */

/* Run the actual calculation. */
const int ret = ti_obv(in_size, inputs, options, outputs);
assert(ret == TI_OKAY);

Calling From Lua (with Tulip Chart bindings)

-- Example usage of On Balance Volume
obv = ti.obv(close, volume)

Example Calculation

dateclosevolumeobv
2005-11-0181.595,653,100.000.00
2005-11-0281.066,447,400.00-6,447,400.00
2005-11-0382.877,690,900.001,243,500.00
2005-11-0483.003,831,400.005,074,900.00
2005-11-0783.614,455,100.009,530,000.00
2005-11-0883.153,798,000.005,732,000.00
2005-11-0982.843,936,200.001,795,800.00
2005-11-1083.994,732,000.006,527,800.00
2005-11-1184.554,841,300.0011,369,100.00
2005-11-1484.363,915,300.007,453,800.00
2005-11-1585.536,830,800.0014,284,600.00
2005-11-1686.546,694,100.0020,978,700.00
2005-11-1786.895,293,600.0026,272,300.00
2005-11-1887.777,985,800.0034,258,100.00
2005-11-2187.294,807,900.0029,450,200.00

Chart

 

Other Indicators

Previous indicator: Negative Volume Index

Next indicator: Percentage Price Oscillator

Random indicator: Vector Floor