本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=6
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// (c) LonesomeTheBlue (original Chaikin Oscillator-AS idea) - v6 rewrite with Fisher Transform, trigger line and regular/hidden divergence engine
indicator("CHO-F Pro + Fisher + Div", shorttitle="CHO-F Pro", overlay=false, max_lines_count=100)
// ============================== INPUTS ==============================
g1 = "Chaikin Settings"
fastLen = input.int(3, "Fast EMA", minval=1, group=g1)
slowLen = input.int(10, "Slow EMA", minval=2, group=g1)
sigLen = input.int(9, "Histogram Smoothing", minval=1, maxval=50, group=g1, tooltip="Histogram = Chaikin - EMA(Chaikin, n). No separate histogram signal line is drawn.")
normOn = input.bool(true, "Normalize (volume / instrument independent scale)", group=g1)
normLen = input.int(100, "Normalization Length", minval=10, group=g1)
lvl = input.float(1.5, "Overbought / Oversold Level (+/-)", minval=0.5, step=0.25, group=g1)
showCho = input.bool(true, "Show Chaikin Line", group=g1)
showBg = input.bool(true, "Overbought / Oversold Background", group=g1)
showLvl = input.bool(true, "Show Overbought / Oversold Level Lines", group=g1)
showNeu = input.bool(true, "Fill Neutral Zone (between upper and lower level)", group=g1)
g1t = "Chaikin Trigger Line"
showTrig = input.bool(true, "Show Trigger Line", group=g1t)
trigType = input.string("WMA", "Trigger Average Type", options=["EMA", "SMA", "WMA"], group=g1t)
trigLen = input.int(3, "Trigger Length", minval=1, maxval=50, group=g1t, tooltip="Buy / Sell signals are generated when the Chaikin line crosses the trigger line.")
g1f = "Fisher Transform Settings"
fisherOn = input.bool(true, "Apply Fisher Transform", group=g1f)
fisherLen = input.int(9, "Fisher Length", minval=1, group=g1f)
g2 = "Signals"
sigOn = input.bool(true, "Combined Buy / Sell Signal (Chaikin x Trigger + Histogram momentum)", group=g2)
zoneFilter = input.bool(true, "Zone Filter (Buy: Chaikin < 0, Sell: Chaikin > 0)", group=g2)
confWin = input.int(1, "Combined Signal Window (trigger cross within last N bars)", minval=0, maxval=5, group=g2, tooltip="0 = trigger cross and histogram momentum on the same bar. 1 = the cross may be on this bar or the previous bar.")
extOnly = input.bool(false, "Extreme Zone Confirmation (+/- level breached within last N bars)", group=g2)
extLook = input.int(10, "Extreme Zone Lookback (bars)", minval=1, group=g2)
showPrice = input.bool(true, "Also Show BUY / SELL Labels on Price Chart", group=g2)
minGap = input.int(5, "Minimum Bars Between Signals", minval=0, group=g2)
g3 = "Divergence"
divOn = input.bool(true, "Show Divergence", group=g3)
divSrcIn = input.string("Histogram", "Source", options=["Chaikin", "Histogram"], group=g3)
divL = input.int(5, "Pivot Left", minval=1, group=g3)
divR = input.int(3, "Pivot Right", minval=1, group=g3)
rngMin = input.int(5, "Min Bars Between Pivots", minval=1, group=g3)
rngMax = input.int(60, "Max Bars Between Pivots", minval=5, group=g3)
pivZone = input.float(0.0, "Pivot Zone Threshold (low: < -threshold, high: > +threshold)", minval=0.0, step=0.1, group=g3)
showHid = input.bool(true, "Show Hidden Divergence", group=g3)
divLines = input.bool(true, "Draw Divergence Lines", group=g3)
g4 = "Colors"
colGrowAbove = input.color(#00796B, "Above Zero - Rising (Dark Green)", group=g4)
colFallAbove = input.color(#00E676, "Above Zero - Falling (Bright Green)", group=g4)
colGrowBelow = input.color(#FF5252, "Below Zero - Rising (Bright Red)", group=g4)
colFallBelow = input.color(#B71C1C, "Below Zero - Falling (Dark Red)", group=g4)
colCho = input.color(color.aqua, "Chaikin Line", group=g4)
colTrig = input.color(color.orange, "Trigger Line", group=g4)
colOB = input.color(#FF1744, "Overbought Level (+)", group=g4)
colOS = input.color(#00E676, "Oversold Level (-)", group=g4)
colNeu = input.color(#2962FF, "Neutral Zone Fill", group=g4)
neuTransp = input.int(88, "Neutral Zone Transparency (0-100)", minval=0, maxval=100, group=g4)
// ============================== VALIDATION ==============================
cumVol = ta.cum(nz(volume))
if fastLen >= slowLen
runtime.error("Fast EMA length must be smaller than Slow EMA length.")
if barstate.islast and cumVol == 0
runtime.error("No volume data for this instrument; Chaikin cannot be calculated.")
// ============================== CALCULATION ==============================
adl = ta.accdist
choRaw = ta.ema(adl, fastLen) - ta.ema(adl, slowLen)
msgRaw = ta.ema(choRaw, sigLen)
histRaw = choRaw - msgRaw
// Normalization
choScaleRaw = ta.sma(math.abs(choRaw), normLen)
histScaleRaw = ta.sma(math.abs(histRaw), normLen)
choScale = normOn ? (na(choScaleRaw) or choScaleRaw <= 0 ? na : choScaleRaw) : 1.0
histScale = normOn ? (na(histScaleRaw) or histScaleRaw <= 0 ? na : histScaleRaw) : 1.0
cho_base = choRaw / choScale
hist_base = histRaw / histScale
// Fisher Transform
f_fisher(src, len) =>
var float v = 0.0
var float fish = 0.0
maxAbs = ta.highest(math.abs(src), len)
nrm = maxAbs > 0 ? src / maxAbs : 0.0
nrm := math.max(-0.99, math.min(0.99, nrm))
v := 0.5 * nrm + 0.5 * nz(v[1])
fish := 0.5 * math.log((1.0 + v) / (1.0 - v)) + 0.5 * nz(fish[1])
fish
choFish = f_fisher(cho_base, fisherLen)
histFish = f_fisher(hist_base, fisherLen)
cho = fisherOn ? choFish : cho_base
hist = fisherOn ? histFish : hist_base
// Trigger line
trigEma = ta.ema(cho, trigLen)
trigSma = ta.sma(cho, trigLen)
trigWma = ta.wma(cho, trigLen)
trig = trigType == "EMA" ? trigEma : trigType == "SMA" ? trigSma : trigWma
trigUp = ta.crossover(cho, trig)
trigDn = ta.crossunder(cho, trig)
histRising = hist > nz(hist[1], hist)
histCol = hist >= 0 ? (histRising ? colGrowAbove : colFallAbove) : (histRising ? colGrowBelow : colFallBelow)
// ============================== DIVERGENCE ==============================
divSrc = divSrcIn == "Chaikin" ? cho : hist
pl = ta.pivotlow(divSrc, divL, divR)
ph = ta.pivothigh(divSrc, divL, divR)
var float lowOscPrev = na
var float lowPxPrev = na
var int lowIdxPrev = na
var float highOscPrev = na
var float highPxPrev = na
var int highIdxPrev = na
bool bullReg = false
bool bullHid = false
bool bearReg = false
bool bearHid = false
if divOn and not na(pl) and pl < -pivZone
pivIdx = bar_index - divR
pxLow = low[divR]
if not na(lowIdxPrev)
gapL = pivIdx - lowIdxPrev
if gapL >= rngMin and gapL <= rngMax
bullReg := pxLow < lowPxPrev and pl > lowOscPrev
bullHid := showHid and pxLow > lowPxPrev and pl < lowOscPrev
if divLines and (bullReg or bullHid)
line.new(lowIdxPrev, lowOscPrev, pivIdx, pl, color=bullReg ? color.green : color.teal, width=2)
lowOscPrev := pl
lowPxPrev := pxLow
lowIdxPrev := pivIdx
if divOn and not na(ph) and ph > pivZone
pivIdx = bar_index - divR
pxHigh = high[divR]
if not na(highIdxPrev)
gapH = pivIdx - highIdxPrev
if gapH >= rngMin and gapH <= rngMax
bearReg := pxHigh > highPxPrev and ph < highOscPrev
bearHid := showHid and pxHigh < highPxPrev and ph > highOscPrev
if divLines and (bearReg or bearHid)
line.new(highIdxPrev, highOscPrev, pivIdx, ph, color=bearReg ? color.red : color.maroon, width=2)
highOscPrev := ph
highPxPrev := pxHigh
highIdxPrev := pivIdx
// ============================== REVERSAL SIGNALS ==============================
nearOS = ta.lowest(cho, extLook) < -lvl
nearOB = ta.highest(cho, extLook) > lvl
var int lastSigBar = na
gapOk = na(lastSigBar) or (bar_index - lastSigBar >= minGap)
histUp = hist > nz(hist[1], hist)
histDn = hist < nz(hist[1], hist)
trigUpW = ta.highest(trigUp ? 1 : 0, confWin + 1) > 0
trigDnW = ta.highest(trigDn ? 1 : 0, confWin + 1) > 0
buyCmb = trigUpW and histUp and (not zoneFilter or cho < 0) and (not extOnly or nearOS)
sellCmb = trigDnW and histDn and (not zoneFilter or cho > 0) and (not extOnly or nearOB)
buyRaw = buyCmb and not buyCmb[1]
sellRaw = sellCmb and not sellCmb[1]
buySig = sigOn and buyRaw and gapOk and barstate.isconfirmed
sellSig = sigOn and sellRaw and gapOk and barstate.isconfirmed
if buySig or sellSig
lastSigBar := bar_index
// ============================== PLOTTING ==============================
hline(0, "Zero", color=color.new(color.gray, 30))
pLvlUp = plot(lvl, "Upper Level", color=showLvl ? color.new(colOB, 0) : na, linewidth=2)
pLvlDn = plot(-lvl, "Lower Level", color=showLvl ? color.new(colOS, 0) : na, linewidth=2)
fill(pLvlUp, pLvlDn, color=showNeu ? color.new(colNeu, neuTransp) : na, title="Neutral Zone")
bgcolor(showBg ? (cho > lvl ? color.new(color.red, 80) : cho < -lvl ? color.new(color.green, 80) : na) : na, title="Extreme Zone")
plot(hist, "Histogram", style=plot.style_columns, color=histCol)
plot(showCho ? cho : na, "Chaikin", color=color.new(colCho, 0), linewidth=3)
plot(showTrig ? trig : na, "Trigger", color=color.new(colTrig, 0), linewidth=1)
// Signal labels in the indicator pane
plotshape(buySig ? cho : na, "Buy", shape.labelup, location.absolute, color=color.new(color.green, 0), textcolor=color.white, text="BUY", size=size.small)
plotshape(sellSig ? cho : na, "Sell", shape.labeldown, location.absolute, color=color.new(color.red, 0), textcolor=color.white, text="SELL", size=size.small)
// Signal labels on the price chart
plotshape(showPrice and buySig, "Buy (Price)", shape.labelup, location.belowbar, color=color.new(color.green, 0), textcolor=color.white, text="BUY", size=size.small, force_overlay=true)
plotshape(showPrice and sellSig, "Sell (Price)", shape.labeldown, location.abovebar, color=color.new(color.red, 0), textcolor=color.white, text="SELL", size=size.small, force_overlay=true)
// Divergence labels (R = regular, H = hidden)
plotshape(bullReg ? divSrc[divR] : na, "Bullish Divergence", shape.labelup, location.absolute,
color=color.new(color.green, 0), textcolor=color.white, text="R", size=size.tiny, offset=-divR)
plotshape(bearReg ? divSrc[divR] : na, "Bearish Divergence", shape.labeldown, location.absolute,
color=color.new(color.red, 0), textcolor=color.white, text="R", size=size.tiny, offset=-divR)
plotshape(bullHid ? divSrc[divR] : na, "Hidden Bullish Divergence", shape.labelup, location.absolute,
color=color.new(color.teal, 0), textcolor=color.white, text="H", size=size.tiny, offset=-divR)
plotshape(bearHid ? divSrc[divR] : na, "Hidden Bearish Divergence", shape.labeldown, location.absolute,
color=color.new(color.maroon, 0), textcolor=color.white, text="H", size=size.tiny, offset=-divR)
// ============================== ALERTS ==============================
alertcondition(buySig, "CHO-F Buy", "CHO-F: Buy signal")
alertcondition(sellSig, "CHO-F Sell", "CHO-F: Sell signal")
alertcondition(bullReg, "CHO-F Bullish Divergence", "CHO-F: Regular bullish divergence")
alertcondition(bearReg, "CHO-F Bearish Divergence", "CHO-F: Regular bearish divergence")
alertcondition(bullHid, "CHO-F Hidden Bullish Divergence", "CHO-F: Hidden bullish divergence")
alertcondition(bearHid, "CHO-F Hidden Bearish Divergence", "CHO-F: Hidden bearish divergence")