Indicator · Pine Script v5 · On the chart

Trend Reversal

by Trader-IKU· code credited to oIKUo

Combines an ATR-based Supertrend with an On Balance Volume momentum oscillator.

View on GitHub

GitHub stars1on the repository
Pine versionv5runs as is
Lines of code143
Settings6inputs
LicenceMIT
Last update2 years ago15 Apr 2024

Add It to Your TradingView Chart

  1. 1Copy the scriptPress Copy script above, or the copy button on the code below.
  2. 2Open the Pine EditorOpen a TradingView chart and choose Pine Editor at the bottom of the screen.
  3. 3Paste over the templateCreate a new indicator, select all of the template text and paste. Press Save and give it a name.
  4. 4Add it to the chartPress Add to chart, then adjust the settings with the gear icon next to its name.

Source Code

143 lines of Pine Script v5 from supertrend_and_obv.txt, published under MIT. Keep the author's credit and licence lines when you reuse it.

supertrend_and_obv.txt
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © oIKUo

//@version=5
indicator("TrendReversal", overlay=true)

window = input.int(title="ATR Period", defval=5)
source = input(hl2, title="Source")
multiplier = input.float(title="ATR Multiplier", defval=2.0)

atr = ta.atr(window)
up = source -(multiplier * atr)
upper = source -(multiplier * atr)
upper1 = nz(upper[1], upper)
upper := close[1] > upper1 ? math.max(upper, upper1) : upper

lo = source + (multiplier * atr)
lower = source + (multiplier * atr)
lower1 = nz(lower[1], lower)
lower := close[1] < lower1 ? math.min(lower, lower1) : lower
trend = 1
trend := nz(trend[1], trend)
trend := trend == -1 and close > lower1 ? 1 : trend == 1 and close < upper1 ? -1 : trend
//plot(trend == 1 ? upper : na, title="Up Trend", style=plot.style_linebr, linewidth=1, color=color.green)
//plot(trend == 1 ? na : lower, title="Down Trend", style=plot.style_linebr, linewidth=1, color=color.red)

up_plot = plot(trend == 1 ? upper : na, title="Up Trend", style=plot.style_linebr, linewidth=3, color=color.green)
lo_plot = plot(trend == -1 ? lower : na, title="Down Trend", style=plot.style_linebr, linewidth=3, color=color.red)
plot(up, title="Up Trend", style=plot.style_linebr, linewidth=1, color=color.blue)
plot(lo, title="Down Trend", style=plot.style_linebr, linewidth=1, color=color.yellow)

mPlot = plot(ohlc4, title="", style=plot.style_circles, linewidth=0)
longFillColor = trend == 1 ? color.new(color.green, 30) : color.white
shortFillColor = trend == -1 ? color.new(color.red, 30) : color.white
//fill(mPlot, up_plot, title="UpTrend Highligter", color=longFillColor)
//fill(mPlot, lo_plot, title="DownTrend Highligter", color=shortFillColor)


// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © oIKUo

// A combination of "On Balance Volume (OBV)" and "Volume Oscillator".
// "OBV Momentum" is a trend momentum indicator, that can be used to identify strong trends and also trend changes based on volume.
// High positive values indicate strong volume on the buy side, high negative values indicate strong volume on the sell side.
// An increasing OBV momentum indicates a strengthening buy trend or a weakening sell trend,
// decvreasing OBV Momentum indicates a strenghening sell trend or weakening buy trend.
// OBV Momentum is calculated by comparing a short vs. a long moving average and plotting the difference in volume.
// OBV Momentum metric is absolute volume.

//@version=5
indicator(title="On Balance Volume Momentum", shorttitle="OBV Momentum", format=format.volume, precision=2, timeframe="", timeframe_gaps=true)

ma(source, length, type) =>
    switch type
        "SMA" => ta.sma(source, length)
        "EMA" => ta.ema(source, length)
        "SMMA (RMA)" => ta.rma(source, length)
        "WMA" => ta.wma(source, length)
        "VWMA" => ta.vwma(source, length)
typeMA = input.string(title = "Method", defval = "SMA", options=["SMA", "EMA", "SMMA (RMA)", "WMA", "VWMA"], group="Smoothing")

smoothingLengthShort = input.int(title = "Length Short", defval = 2, minval = 1, maxval = 100, group="Smoothing")
smoothingLengthLong = input.int(title = "Length Long", defval = 14, minval = 1, maxval = 100, group="Smoothing")

smoothingLineShort = ma(ta.obv, smoothingLengthShort, typeMA)
smoothingLineLong = ma(ta.obv, smoothingLengthLong, typeMA)
osc = smoothingLineShort - smoothingLineLong

pcol = color.gray

// bullish
if(osc > 0)
    pcol := color.teal
if(osc > 0 and osc[1] > 0)
    pcol := color.green
// all above zero
if(osc > 0 and osc[1] > 0 and osc[2] > 0)
    pcol := color.teal
    // rising trend
    if(osc > osc[1])
        pcol := color.green
        // strong rising trend
        if(osc[1] > osc[2])
            pcol := color.lime
    // falling trend
    else if(osc < osc[1])
        pcol := color.green
        // strong falling trend
        if(osc < osc[2])
            pcol := color.teal
// bearish
if(osc < 0)
    pcol := color.yellow
if(osc < 0 and osc[1] < 0)
    pcol := color.orange
// all below zero
if(osc < 0 and osc[1] < 0 and osc[2] < 0)
    pcol := color.yellow
    // falling trend
    if(osc < osc[1])
        pcol := color.orange
        // strong falling trend
        if(osc[1] < osc[2])
            pcol := color.red
    // rising trend
    else if(osc > osc[1])
        pcol := color.orange
        // strong rising trend
        if(osc > osc[2])
            pcol := color.yellow

    
// pcol = osc > 0 and lastOsc < osc ? color.lime : osc < 0 and lastOsc < osc ? color.orange : color.red
hline(0, color = #787B86, title="Zero")
plot(osc, color=pcol, style=plot.style_columns)

    
buy_signal = osc > osc[1] and (pcol == color.green or pcol == color.lime)
sell_signal = osc < osc[1] and (pcol == color.orange or pcol == color.red)

buy_alert = buy_signal
sell_alert = sell_signal

for i = 1 to 100
    if(buy_signal[i])
        buy_alert := false
        break
    if(osc[i] < 0)
        break

for i = 1 to 100
    if(sell_signal[i])
        sell_alert := false
        break
    if(osc[i] > 0)
        break
        

plotshape(buy_alert ,style=shape.labeldown,location=location.top,color=color.green,size=size.auto,text="Buy",textcolor=color.white,title='Buy') 
plotshape(sell_alert ,style=shape.labelup,location=location.bottom,color=color.red,size=size.auto,text="Sell",textcolor=color.white,title='Sell') 

alertcondition(buy_signal, title='OBVM Bull', message='Bullish OBV Momentum')
alertcondition(sell_signal, title='OBVM Bear', message='Bearish OBV Momentum')

Overview

The Supertrend bands, with adjustable ATR period, source and multiplier, appear on the price chart with up and down trend highlighting. The OBV momentum compares a short and a long average of On Balance Volume, with a choice of SMA, EMA, RMA, WMA or VWMA smoothing. Supertrend lines flip when price closes through them, so signals follow price.

Details

Published by

Trader-IKU on GitHub

Code credit

oIKUo, as written in the script's header

Type

Indicator, Pine Script v5

Draws

On the price chart

Category

Trend, Volume, Volatility & bands

Markets

Any symbol

Timeframes

Any timeframe

Risk Notes

No repainting, lookahead or stacked entries were found in its code or README. That says nothing about how well it works: test it first.

Settings

The inputs you can change from the gear icon on the chart. Defaults are the author's.

SettingTypeDefaultVariable
ATR Periodint5window
Sourcesourcehl2source
ATR Multiplierfloat2.0multiplier
MethodstringSMAtypeMA
Length Shortint2smoothingLengthShort
Length Longint14smoothingLengthLong
Third-party code. ForexR did not write, test or endorse this script, and the summary above was generated automatically from its code and README. Indicators describe the past, and a strategy's backtest can look better than live trading, especially when a script repaints. Check it in the Strategy Tester and on a demo account before you rely on it.

Similar Pine Scripts

The Momentum Setup is a multi-indicator visual tool designed for trend and momentum analysis directly on the price chart. It combines Relative Strength Index (RSI), Money Flow Index (MFI), Commodity Channel Index (CCI), Supertrend, and a 250-bar percentage cha

Indicator v5 Oscillators & momentumTrend
Updated 1 year ago · MPL-2.0 GitHub repository of Momentum Setup - Ankush Bajaj Momentum Investing Setup

Adaptive MA Slope

by capissimo
114 stars

This indicator calculates Kaufman's Adaptive Moving Average (KAMA) on a chosen price source and determines trend direction based on the slope across a configurable lookback window. Signals can be filtered by short-term volatility expansion using ATR comparison

Indicator v4 Moving averagesTrend
Updated 1 year ago · None stated GitHub repository of Adaptive MA Slope
92 stars

This indicator plots a volume-weighted average price of the typical price on the chart and colors that line by ADX strength. It draws a standard-deviation envelope around VWAP and tints the fill green when plus DI leads and red when minus DI leads. Triangle ma

Indicator v5 TrendOscillators & momentum
Updated 2 months ago · Custom licence GitHub repository of Moon Dev VWAP x ADX Explosion

This overlay indicator marks Wyckoff-style buying and selling climaxes on the price chart. It filters trend with an exponential moving average and a higher-timeframe EMA, then flags accumulation or distribution when price and ATR sit relative to their swing av

Indicator v6 TrendVolatility & bands
Updated 1 year ago · MIT GitHub repository of Ultimate Wyckoff Indicator V3 (Refined)

Vein Exhaustion is a price-chart indicator that scores how stretched a directional move is before structure confirms a turn. It builds separate bull and bear scores from consecutive directional bars, EMA distance in ATR units, RSI and MFI extremes, volume-base

Indicator v6 TrendOscillators & momentum
Updated 2 days ago · MIT GitHub repository of Vein: Exhaustion [WavesUnchained]
9 stars

This indicator plots a volume-weighted moving average as a central trend baseline and surrounds it with ATR-based volatility bands. It colors the trend and optional price bars according to whether price is on the bullish or bearish side of those bands. Users c

Indicator v6 TrendMoving averages
Updated 4 months ago · MIT GitHub repository of Volume Weighted Trend [QuantAlgo]

More indicators

Trade Your Charts With a Regulated Broker

Compare brokers that connect to TradingView, with spreads, minimum deposits and leverage side by side.

Compare brokers