Skip to content
GetProfitable
Search
Volume

On Balance Volume with Average

The running total that adds a bar's volume on an up close and subtracts it on a down close, drawn against a moving average of itself so the direction of accumulation is read rather than guessed.

indicator
//@version=6
indicator("On Balance Volume with Average", "OBV")

maLen = input.int(20, "Average length", minval = 1, group = "Settings")
maType = input.string("EMA", "Average type", options = ["EMA", "SMA"], group = "Settings")
bull = input.color(#26A69A, "Above average", group = "Style")
bear = input.color(#EF5350, "Below average", group = "Style")
avgCol = input.color(#787B86, "Average", group = "Style")

obvLine = ta.obv
obvEma = ta.ema(obvLine, maLen)
obvSma = ta.sma(obvLine, maLen)
obvAvg = maType == "EMA" ? obvEma : obvSma

above = obvLine > obvAvg

plot(obvLine, "OBV", color = above ? bull : bear, linewidth = 2)
plot(obvAvg, "Average", color = color.new(avgCol, 20))

crossedUp = ta.crossover(obvLine, obvAvg)
crossedDown = ta.crossunder(obvLine, obvAvg)
plotshape(crossedUp, "Above average", location = location.bottom, style = shape.triangleup, color = bull, size = size.tiny)
plotshape(crossedDown, "Below average", location = location.top, style = shape.triangledown, color = bear, size = size.tiny)
alertcondition(crossedUp, "OBV above its average", "On balance volume crossed above its moving average")
alertcondition(crossedDown, "OBV below its average", "On balance volume crossed below its moving average")
This runs in TradingView, not here
Pine Script only executes inside TradingView. Paste the source into the Pine Editor and add it to a chart to see it plotted.

What it will not do

  • OBV treats a close up by one tick exactly like a close up by five percent, so the running total is a crude summary of a bar by design.
  • The absolute level depends on where the chart's history begins and means nothing on its own. Only the slope and the crosses carry information.
  • A divergence between OBV and price is an observation, not a signal. It can persist for a very long time before anything happens.
  • For spot forex TradingView reports tick count instead of traded size, so the total is accumulating ticks rather than money.

Written from this description

Plot on balance volume in its own pane with a configurable moving average of itself, colour the line by whether it sits above or below that average, and alert on the crosses.

Educational only, not financial advice. The maths is simple arithmetic on the numbers you enter; it knows nothing about your broker, fees, slippage or the market. Something wrong with it? Say so in Site Feedback.