本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=6
indicator("All Oscillators Price Overlay", shorttitle="All Oscillators Price Overlay", overlay=true, format=format.price, precision=2)
// ── Custom Functions ── //
f_ultosc(len1, len2, len3) =>
tl = math.min(low, nz(close[1], low))
bp = close - tl
tr = math.max(high, nz(close[1], high)) - tl
avg1 = math.sum(bp, len1) / math.sum(tr, len1)
avg2 = math.sum(bp, len2) / math.sum(tr, len2)
avg3 = math.sum(bp, len3) / math.sum(tr, len3)
100 * (4 * avg1 + 2 * avg2 + avg3) / 7
// ── Standard Inputs ── //
groupOsc = "Main Oscillator Settings"
oscType = input.string("RSI", "Oscillator", options=["RSI", "Stochastic %K", "Stochastic RSI", "CCI", "MFI", "Williams %R", "Momentum", "ROC", "Ultimate Oscillator", "CMO", "TSI", "DPO", "Fisher Transform", "RVI", "MACD Histogram"], group=groupOsc)
length = input.int(14, "Primary Length", minval=2, group=groupOsc)
upper = input.float(70, "Upper Boundary", minval=50, maxval=100, group=groupOsc)
lower = input.float(30, "Lower Boundary", minval=0, maxval=50, group=groupOsc)
// ── Advanced Oscillator Inputs ── //
groupAdv = "Advanced Oscillator Inputs"
stochSmooth = input.int(3, "Stochastic %K Smoothing", minval=1, group=groupAdv)
macdFast = input.int(12, "MACD Fast Length", minval=1, group=groupAdv)
macdSlow = input.int(26, "MACD Slow Length", minval=1, group=groupAdv)
macdSignal = input.int(9, "MACD Signal Length", minval=1, group=groupAdv)
ultLen1 = input.int(7, "Ultimate Osc Length 1", minval=1, group=groupAdv)
ultLen2 = input.int(14, "Ultimate Osc Length 2", minval=1, group=groupAdv)
ultLen3 = input.int(28, "Ultimate Osc Length 3", minval=1, group=groupAdv)
// ── Visual Settings (Fill) ── //
groupFill = "Signal Fill (Solid Colors)"
showFill = input.bool(true, "Show Overbought/Oversold Fill", group=groupFill)
fillTransparency = input.int(60, "Fill Transparency", minval=0, maxval=100, step=5, group=groupFill, tooltip="0 = Solid, 100 = Invisible")
obColorRaw = input.color(#33E643, "Overbought Color Base", group=groupFill)
osColorRaw = input.color(#F22738, "Oversold Color Base", group=groupFill)
obColor = color.new(obColorRaw, fillTransparency)
osColor = color.new(osColorRaw, fillTransparency)
// ── Global Calculations for Persistent State ── //
priceRangeFisher = ta.highest(hl2, length) - ta.lowest(hl2, length)
pricePositionFisher = priceRangeFisher != 0 ? 2 * (hl2 - ta.lowest(hl2, length)) / priceRangeFisher - 1 : 0.0
var float fisherValue = 0.0
fisherInput = math.max(-0.999, math.min(0.999, 0.66 * pricePositionFisher + 0.67 * nz(pricePositionFisher[1])))
fisherValue := 0.5 * math.log((1 + fisherInput) / (1 - fisherInput)) + 0.5 * nz(fisherValue[1])
// ── Oscillator Logic ── //
float oscRaw = na
switch oscType
"RSI" =>
oscRaw := ta.rsi(close, length)
"Stochastic %K" =>
oscRaw := ta.sma(ta.stoch(close, high, low, length), stochSmooth)
"Stochastic RSI" =>
rsiValue = ta.rsi(close, length)
rsiLow = ta.lowest(rsiValue, length)
rsiHigh = ta.highest(rsiValue, length)
oscRaw := ta.sma(rsiHigh != rsiLow ? 100 * (rsiValue - rsiLow) / (rsiHigh - rsiLow) : 50.0, stochSmooth)
"CCI" =>
oscRaw := 50 + 50 * math.atan(ta.cci(hlc3, length) / 100.0) / (math.pi / 2)
"MFI" =>
oscRaw := ta.mfi(hlc3, length)
"Williams %R" =>
oscRaw := 100 + ta.wpr(length)
"Momentum" =>
oscRaw := 50 + 50 * math.atan((close - close[length]) / ta.atr(length)) / (math.pi / 2)
"ROC" =>
oscRaw := 50 + 50 * math.atan(ta.roc(close, length) / 10.0) / (math.pi / 2)
"Ultimate Oscillator" =>
oscRaw := f_ultosc(ultLen1, ultLen2, ultLen3)
"CMO" =>
oscRaw := (ta.cmo(close, length) + 100) / 2
"TSI" =>
oscRaw := 50 + 50 * math.atan(ta.tsi(close, length * 2, length) / 25.0) / (math.pi / 2)
"DPO" =>
oscRaw := 50 + 50 * math.atan((close - ta.sma(close, length)[int(math.floor(length / 2.0)) + 1]) / ta.atr(length)) / (math.pi / 2)
"Fisher Transform" =>
oscRaw := 50 + 50 * math.atan(fisherValue) / (math.pi / 2)
"RVI" =>
rviDen = ta.sma(high - low, length)
oscRaw := 50 + 50 * math.atan((rviDen != 0 ? ta.sma(close - open, length) / rviDen : 0.0) * 4) / (math.pi / 2)
"MACD Histogram" =>
[macdLine, signalLine, macdHist] = ta.macd(close, macdFast, macdSlow, macdSignal)
oscRaw := 50 + 50 * math.atan(macdHist / ta.atr(length)) / (math.pi / 2)
// Normalize any overflow inside 0 to 100 bounds
oscClamped = math.max(0.0, math.min(100.0, oscRaw))
// ── Adaptive price boundaries ── //
center = ta.rma(close, length)
priceChange = math.abs(close - close[1])
avgChange = ta.rma(priceChange, length)
maxWidth = avgChange * (length - 1)
upperBand = center + (upper - 50) / 50 * maxWidth
lowerBand = center - (50 - lower) / 50 * maxWidth
// ── Invisible Plots for Fill Bounds ── //
pUpper = plot(upperBand, "Upper Boundary", display=display.none)
pLower = plot(lowerBand, "Lower Boundary", display=display.none)
pClose = plot(close, "Close Reference", display=display.none)
// ── Solid Oscillator-driven fills ── //
// FIX: Removed the 'close > upperBand' and 'close < lowerBand' constraints.
// The fill is now driven entirely by the selected oscillator's values.
isOB = oscClamped >= upper
isOS = oscClamped <= lower
fillColorOB = showFill and isOB ? obColor : na
fillColorOS = showFill and isOS ? osColor : na
fill(pUpper, pClose, color=fillColorOB, title="Overbought Solid Fill")
fill(pLower, pClose, color=fillColorOS, title="Oversold Solid Fill")
// ── Alerts ── //
alertcondition(ta.crossover(oscClamped, upper), "Oscillator Overbought", "Selected oscillator crossed above the upper boundary.")
alertcondition(ta.crossunder(oscClamped, lower), "Oscillator Oversold", "Selected oscillator crossed below the lower boundary.")