strategy · on price · open source

On-Balance Volume (OBV) trend strategy

On-Balance Volume trend strategy: buy when OBV is higher than 20 bars ago, exit when it is lower. Plain-English rules, copyable code and a costed backtest. Deepwick Script runs a subset of Pine Script.

v1Updated 3 Oct 2026▲ 0 users
Running…
Loading chart…

What the OBV Trend strategy does

On-Balance Volume trend strategy: long when OBV has risen over the lookback window (accumulation phase), exit when OBV turns lower.

  • OBV rising on flat/down prices = stealth accumulation.
  • Combine with a price trend filter for cleaner entries.
  • 20-period is the canonical OBV trend window.

Entry and exit rules

    Language reference: Deepwick Script.

    Default inputs

    InputDefaultMinMax
    OBV Lookback20550
    Stop Loss %20.510
    Take Profit %6020

    How Deepwick fills this strategy

    • Market orders fill at the next bar's open.
    • Stops and limits follow the bar's path: open → nearer extreme → farther extreme → close.
    • Gaps fill at the open.
    • Commission is charged on every fill, and slippage applies to market and stop fills, as set in strategy().

    More on how backtests work

    Source code

    Deepwick Script, a subset of Pine Script · read-only

    //@version=6
    strategy("OBV Trend Strategy", overlay=true, initial_capital=10000, default_qty_type=strategy.percent_of_equity, default_qty_value=10, commission_type=strategy.commission.percent, commission_value=0.05)
    len = input.int(20, "OBV Lookback", minval=5, maxval=50, step=1)
    stopPct = input.float(2, "Stop Loss %", minval=0.5, maxval=10, step=0.1)
    tpPct = input.float(6, "Take Profit %", minval=0, maxval=20, step=0.1)
    colorMid2 = input.color("color.teal", "Color  Mid2")
    colorUp = input.color("color.green", "Color  Up")
    colorDown = input.color("color.red", "Color  Down")
    var float ta.obv = 0.0
    ta.obv := close > close[1] ? nz(ta.obv[1]) + volume : close < close[1] ? nz(ta.obv[1]) - volume : nz(ta.obv[1])
    longSignal = strategy.position_size == 0 and ta.obv > ta.obv[len]
    exitSignal = strategy.position_size > 0 and ta.obv < ta.obv[len]
    if longSignal
        strategy.entry("Long", strategy.long)
    if strategy.position_size > 0
        strategy.exit("Risk", stop=stopPct > 0 ? strategy.position_avg_price * (1 - stopPct / 100) : na, limit=tpPct > 0 ? strategy.position_avg_price * (1 + tpPct / 100) : na)
    if exitSignal
        strategy.close("Long", comment="OBV turned")
    p_OBV = plot(ta.obv, "OBV", color=colorMid2, linewidth=2)
    plotshape(longSignal, "Long", style=shape.triangleup, location=location.belowbar, color=colorUp, text="L")
    plotshape(exitSignal, "Exit", style=shape.triangledown, location=location.abovebar, color=colorDown, text="X")
    alertcondition(longSignal, "OBV long entry", "OBV rose over the lookback")
    alertcondition(exitSignal, "OBV exit", "OBV turned lower")
    

    Run it in Deepwick

    1. Open a chart of any crypto pair or CFD.
    2. Click “Scripts” and add this strategy.
    3. Press Run and read the trades in the Strategy Tester.
    Start free

    FAQ

    Why does the strategy ignore the price trend?

    It is a pure volume-flow test: it asks whether accumulation alone is a useful long filter. Adding a price filter is a good next experiment.

    Can I trade OBV divergences with this?

    Not directly. A divergence needs pivot detection on price and OBV; this script compares OBV with its own past value.

    Comments

    No comments yet.

    Sign in to comment.