本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=4
// OBV oscillator
study(title="On-Balance Volume with Divergence and Pivot Points ", shorttitle="OBV Osc", format=format.volume, precision=0, resolution="")
src = input(close, title="Source")
shortLength = input(1, title="Short OBV period")
longLength = input(20, title="Long OBV period")
pivR = input(title="OBV Wave Pivot Lookback Right", defval=1)
pivL = input(title="OBV Wave Pivot Lookback Left", defval=20)
// 1 - Compute regular OBV
obv = cum(change(src) > 0 ? +volume : change(src) < 0 ? -volume : 0*volume)
// 2 - Compute OBV ocillator
obvOsc = ema(obv, shortLength) - ema(obv, longLength)
obvColor = obvOsc > 0 ? color.new(color.aqua,60) : color.new(color.orange,60)
// Divergence
up = rma(max(change(src), 0), shortLength)
down = rma(-min(change(src), 0), shortLength)
lbR = input(title="Divergence Pivot Lookback Right", defval=2)
lbL = input(title="Divergence Pivot Lookback Left", defval=5)
rangeUpper = input(title="Max of Lookback Range", defval=100)
rangeLower = input(title="Min of Lookback Range", defval=2)
plotBull = input(title="Plot Bullish", defval=true)
plotHiddenBull = input(title="Plot Hidden Bullish", defval=true)
plotBear = input(title="Plot Bearish", defval=true)
plotHiddenBear = input(title="Plot Hidden Bearish", defval=true)
bearColor = color.new(color.red, 0)
bullColor = color.new(color.green, 0)
hiddenBullColor = color.new(color.aqua, 0)
hiddenBearColor = color.new(color.orange, 0)
textColor = color.new(color.white, 0)
noneColor = color.new(color.white, 100)
osc = ema(obv, shortLength) - ema(obv, longLength)
plFound = na(pivotlow(osc, lbL, lbR)) ? false : true
phFound = na(pivothigh(osc, lbL, lbR)) ? false : true
_inRange(cond) =>
bars = barssince(cond == true)
rangeLower <= bars and bars <= rangeUpper
//------------------------------------------------------------------------------
// Regular Bullish
// Osc: Higher Low
oscHL = osc[lbR] > valuewhen(plFound, osc[lbR], 1) and _inRange(plFound[1])
// Price: Lower Low
priceLL = low[lbR] < valuewhen(plFound, low[lbR], 1)
bullCond = plotBull and priceLL and oscHL and plFound
plot(
plFound ? osc[lbR] : na,
offset=-lbR,
title="Regular Bullish",
linewidth=1,
color=(bullCond ? bullColor : noneColor),
transp=0
)
plotshape(
bullCond ? osc[lbR] : na,
offset=-lbR,
title="Regular Bullish Label",
style=shape.labelup,
location=location.absolute,
size=size.tiny,
color=bullColor,
textcolor=textColor,
transp=0
)
//------------------------------------------------------------------------------
// Hidden Bullish
// Osc: Lower Low
oscLL = osc[lbR] < valuewhen(plFound, osc[lbR], 1) and _inRange(plFound[1])
// Price: Higher Low
priceHL = low[lbR] > valuewhen(plFound, low[lbR], 1)
hiddenBullCond = plotHiddenBull and priceHL and oscLL and plFound
plot(
plFound ? osc[lbR] : na,
offset=-lbR,
title="Hidden Bullish",
linewidth=1,
color=(hiddenBullCond ? hiddenBullColor : noneColor),
transp=0
)
plotshape(
hiddenBullCond ? osc[lbR] : na,
offset=-lbR,
title="Hidden Bullish Label",
style=shape.labelup,
location=location.absolute,
size=size.tiny,
color=hiddenBullColor,
textcolor=textColor,
transp=0
)
//------------------------------------------------------------------------------
// Regular Bearish
// Osc: Lower High
oscLH = osc[lbR] < valuewhen(phFound, osc[lbR], 1) and _inRange(phFound[1])
// Price: Higher High
priceHH = high[lbR] > valuewhen(phFound, high[lbR], 1)
bearCond = plotBear and priceHH and oscLH and phFound
plot(
phFound ? osc[lbR] : na,
offset=-lbR,
title="Regular Bearish",
linewidth=1,
color=(bearCond ? bearColor : noneColor),
transp=0
)
plotshape(
bearCond ? osc[lbR] : na,
offset=-lbR,
title="Regular Bearish Label",
style=shape.labeldown,
location=location.absolute,
size=size.tiny,
color=bearColor,
textcolor=textColor,
transp=0
)
//------------------------------------------------------------------------------
// Hidden Bearish
// Osc: Higher High
oscHH = osc[lbR] > valuewhen(phFound, osc[lbR], 1) and _inRange(phFound[1])
// Price: Lower High
priceLH = high[lbR] < valuewhen(phFound, high[lbR], 1)
hiddenBearCond = plotHiddenBear and priceLH and oscHH and phFound
plot(
phFound ? osc[lbR] : na,
offset=-lbR,
title="Hidden Bearish",
linewidth=1,
color=(hiddenBearCond ? hiddenBearColor : noneColor),
transp=0
)
plotshape(
hiddenBearCond ? osc[lbR] : na,
offset=-lbR,
title="Hidden Bearish Label",
style=shape.labeldown,
location=location.absolute,
color=hiddenBearColor,
size=size.tiny,
textcolor=textColor,
transp=0
)
// Plots
plot(0, title="Zero Line", color=color.new(color.white, 40), style=plot.style_line, linewidth=1)
PHCond = obvOsc > 0 ? pivothigh(obvOsc, pivL, pivR) : na
PLCond = obvOsc < 0 ? pivotlow(obvOsc, pivL, pivR) : na
plot(obvOsc, color=obvColor, title="OBV Oscillator", linewidth=3, style=plot.style_area)
plot(obvOsc, color=color.new(color.white,10), title="OBV line", linewidth=1, style=plot.style_line)
plotshape(PHCond, title="OBV Pivot High", location=location.absolute, style=shape.triangledown, size=size.auto, color=color.new(color.white,00))
plotshape(PLCond, title="OBV Pivot Low", location=location.absolute, style=shape.triangleup, size=size.auto, color=color.new(color.white,00))
crossoverCond1 = crossunder(obvOsc, 0)
crossoverCond2 = crossover(obvOsc, 0)
//Alerts
alertcondition(PHCond, title="Pivot High", message="Pivot High {{ticker}}")
alertcondition(PLCond, title="Pivot Low", message="Pivot Low {{ticker}}")
alertcondition(crossoverCond1, title="OBV Crossed Up", message="OBV Cross Up {{ticker}}")
alertcondition(crossoverCond2, title="OBV Crossed Down", message="OBV Cross Down {{ticker}}")
alertcondition(bullCond, title="Bull Diversion", message="OBV Regular Bull Div {{ticker}}")
alertcondition(bearCond, title="Bear Diversion", message="OBV Regular Bear Div {{ticker}}")
alertcondition(hiddenBullCond, title="Hidden Bull Diversion", message="OBV Hidden Bull Div {{ticker}}")
alertcondition(hiddenBearCond, title="Hidden Bear Diversion", message="OBV Hidden Bear Div {{ticker}}")