本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
// This Pine Script® code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © orbitonetv
//@version=6
indicator("Divergence Hunter [OrbitOne]", "Divergence Hunter [OrbitOne]", overlay = false, max_lines_count = 500, max_labels_count = 500)
// ════════ OSCILLATOR ════════
gO = "Oscillator"
oscSrc = input.string("RSI", "Oscillator", options = ["RSI", "MACD", "Stochastic", "MFI", "CCI"], group = gO)
oscLen = input.int(14, "Length", minval = 2, group = gO, tooltip = "Used by RSI, Stochastic, MFI and CCI. MACD uses 12 / 26 / 9.")
useZone = input.bool(false, "Only in overbought / oversold zone", group = gO, tooltip = "RSI, Stochastic and MFI only: bullish divergences need a pivot below the oversold level, bearish ones above the overbought level.")
obLvl = input.float(70, "Overbought", group = gO)
osLvl = input.float(30, "Oversold", group = gO)
// ════════ DIVERGENCES ════════
gD = "Divergences"
lbL = input.int(5, "Pivot lookback left", minval = 1, maxval = 50, group = gD)
lbR = input.int(5, "Pivot lookback right", minval = 1, maxval = 50, group = gD, tooltip = "A pivot is confirmed after this many bars. Smaller = earlier signals, more noise.")
minR = input.int(5, "Min bars between pivots", minval = 1, group = gD)
maxR = input.int(60, "Max bars between pivots", minval = 5, group = gD)
showReg = input.bool(true, "Regular divergences (reversal)", group = gD)
showHid = input.bool(false, "Hidden divergences (continuation)", group = gD)
onPrice = input.bool(true, "Draw on price chart", group = gD)
maxDraw = input.int(30, "Divergences kept", minval = 5, maxval = 150, group = gD)
// ════════ STYLE ════════
gS = "Style"
bullCol = input.color(#089981, "Bullish", group = gS)
bearCol = input.color(#F23645, "Bearish", group = gS)
oscCol = input.color(#7E57C2, "Oscillator line", group = gS)
showDb = input.bool(true, "Show dashboard", group = gS)
dbPos = input.string("Bottom right", "Dashboard position", options = ["Top right", "Bottom right", "Top left", "Bottom left"], group = gS)
// ════════ CALCULATION ════════
rsiV = ta.rsi(close, oscLen)
[macdV, macdSig, macdHist] = ta.macd(close, 12, 26, 9)
stochV = ta.sma(ta.stoch(close, high, low, oscLen), 3)
mfiV = ta.mfi(hlc3, oscLen)
cciV = ta.cci(hlc3, oscLen)
osc = oscSrc == "RSI" ? rsiV : oscSrc == "MACD" ? macdV : oscSrc == "Stochastic" ? stochV : oscSrc == "MFI" ? mfiV : cciV
bounded = oscSrc == "RSI" or oscSrc == "Stochastic" or oscSrc == "MFI"
plPiv = ta.pivotlow(osc, lbL, lbR)
phPiv = ta.pivothigh(osc, lbL, lbR)
okBar = barstate.isconfirmed
var float pOscL = na
var float pPrL = na
var int pBarL = na
var float pOscH = na
var float pPrH = na
var int pBarH = na
var array<line> lns = array.new<line>()
var array<label> lbs = array.new<label>()
var int nBull = 0
var int nBear = 0
var string lastT = "—"
var int lastB = na
f_keep(line a, line b, label c) =>
lns.push(a)
if not na(b)
lns.push(b)
if not na(c)
lbs.push(c)
while lns.size() > maxDraw * 2
line.delete(lns.shift())
while lbs.size() > maxDraw
label.delete(lbs.shift())
f_draw(int x1, float o1, float p1, int x2, float o2, float p2, bool bull, bool hidden) =>
color c = bull ? bullCol : bearCol
string st = hidden ? line.style_dashed : line.style_solid
line lo = line.new(x1, o1, x2, o2, color = c, width = 2, style = st)
line lp = onPrice ? line.new(x1, p1, x2, p2, color = c, width = 2, style = st, force_overlay = true) : na
string txt = (hidden ? "H " : "") + (bull ? "Bull" : "Bear")
label lb = onPrice ? label.new(x2, p2, txt, style = bull ? label.style_label_up : label.style_label_down, color = c, textcolor = color.white, size = size.tiny, force_overlay = true) : na
f_keep(lo, lp, lb)
bullReg = false
bullHid = false
bearReg = false
bearHid = false
if okBar and not na(plPiv)
float o = osc[lbR]
float p = low[lbR]
int b = bar_index - lbR
if not na(pBarL) and b - pBarL >= minR and b - pBarL <= maxR
bool zoneOk = not useZone or not bounded or math.min(o, pOscL) < osLvl
bullReg := showReg and zoneOk and o > pOscL and p < pPrL
bullHid := showHid and o < pOscL and p > pPrL
if bullReg or bullHid
f_draw(pBarL, pOscL, pPrL, b, o, p, true, bullHid)
nBull += 1
lastT := bullHid ? "Hidden bullish" : "Bullish"
lastB := bar_index
pOscL := o
pPrL := p
pBarL := b
if okBar and not na(phPiv)
float o = osc[lbR]
float p = high[lbR]
int b = bar_index - lbR
if not na(pBarH) and b - pBarH >= minR and b - pBarH <= maxR
bool zoneOk = not useZone or not bounded or math.max(o, pOscH) > obLvl
bearReg := showReg and zoneOk and o < pOscH and p > pPrH
bearHid := showHid and o > pOscH and p < pPrH
if bearReg or bearHid
f_draw(pBarH, pOscH, pPrH, b, o, p, false, bearHid)
nBear += 1
lastT := bearHid ? "Hidden bearish" : "Bearish"
lastB := bar_index
pOscH := o
pPrH := p
pBarH := b
// ════════ PLOTS ════════
plot(osc, "Oscillator", color = oscCol, linewidth = 2)
plot(bounded ? obLvl : na, "Overbought", color = color.new(color.gray, 40), display = display.all - display.status_line)
plot(bounded ? osLvl : na, "Oversold", color = color.new(color.gray, 40), display = display.all - display.status_line)
plot(bounded ? 50 : 0, "Middle", color = color.new(color.gray, 70), display = display.all - display.status_line)
// ════════ DASHBOARD ════════
var table db = table.new(dbPos == "Top right" ? position.top_right : dbPos == "Bottom right" ? position.bottom_right : dbPos == "Top left" ? position.top_left : position.bottom_left, 2, 4, border_width = 1)
f_row(int r, string k, string v, color c) =>
table.cell(db, 0, r, k, bgcolor = color.new(color.gray, 35), text_color = color.white, text_size = size.tiny)
table.cell(db, 1, r, v, bgcolor = c, text_color = color.white, text_size = size.tiny)
if showDb and barstate.isfirst
table.merge_cells(db, 0, 0, 1, 0)
if showDb and barstate.islast
table.cell(db, 0, 0, "Divergence Hunter · " + oscSrc, bgcolor = color.new(color.black, 30), text_color = color.white, text_size = size.tiny)
color lc = str.contains(lastT, "ullish") ? bullCol : str.contains(lastT, "earish") ? bearCol : color.gray
f_row(1, "Last", lastT, lc)
f_row(2, "Bars ago", na(lastB) ? "—" : str.tostring(bar_index - lastB), color.new(color.gray, 20))
f_row(3, "Found (bull / bear)", str.tostring(nBull) + " / " + str.tostring(nBear), color.new(color.gray, 20))
// ════════ ALERTS ════════
alertcondition(bullReg, "Bullish divergence", "Bullish divergence | {{ticker}} | {{interval}} | {{close}}")
alertcondition(bearReg, "Bearish divergence", "Bearish divergence | {{ticker}} | {{interval}} | {{close}}")
alertcondition(bullHid, "Hidden bullish divergence", "Hidden bullish divergence | {{ticker}} | {{interval}} | {{close}}")
alertcondition(bearHid, "Hidden bearish divergence", "Hidden bearish divergence | {{ticker}} | {{interval}} | {{close}}")
alertcondition(bullReg or bearReg or bullHid or bearHid, "Any divergence", "Divergence | {{ticker}} | {{interval}} | {{close}}")