本页源码来自 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/
// © Uncle_the_shooter
//@version=6
indicator('Volatility Channel Oscillator', shorttitle = 'VCO', overlay = false, max_bars_back = 500, precision = 2)
// INPUTS
length = input.int(30, 'SMA Length / Volatility Window', group = 'Oscillator Settings')
scale = input.float(200.0, 'Band Scaling (%)', minval = 100.0, group = 'Oscillator Settings')
signalLength = input.int(20, 'SMA Length for Signal', group = 'Oscillator Settings')
overbought = input.float(50.0, title = 'Overbought Threshold', step = 0.1, group = 'Threshold Levels')
oversold = input.float(-50.0, title = 'Oversold Threshold', step = 0.1, group = 'Threshold Levels')
maOverbought = input.float(30.0, title = 'MA Overbought Threshold', step = 0.1, group = 'Threshold Levels')
maOversold = input.float(-30.0, title = 'MA Oversold Threshold', step = 0.1, group = 'Threshold Levels')
pivotLength = input.int(2, title = 'Pivot Length (Left/Right)', minval = 1, group = 'Divergence Settings')
calculateDivergence = input.bool(true, title = 'Enable Divergence Detection', group = 'Divergence Settings')
signalType = input.string('Overbought/Oversold', title = 'Signal Type', options = ['None', 'Overbought/Oversold', 'Zero Line', 'MA Zero Line', 'All'], group = 'Signal Settings')
// Colors
oscColorPositive = input.color(color.rgb(6, 162, 47), title="Bullish Oscillator Color", group = 'Style Settings')
oscColorNegative = input.color(color.rgb(207, 23, 23), title="Bearish Oscillator Color", group = 'Style Settings')
signalColor = input.color(color.yellow, title="Signal SMA Color", group = 'Style Settings')
zeroLineColor = input.color(color.gray, title="Zero Line Color", group = 'Style Settings')
overboughtColor = input.color(color.red, title="Overbought Line Color", group = 'Style Settings')
oversoldColor = input.color(color.green, title="Oversold Line Color", group = 'Style Settings')
bullDivColor = input.color(color.green, title = 'Bullish Divergence Color', group = 'Style Settings')
bearDivColor = input.color(color.red, title = 'Bearish Divergence Color', group = 'Style Settings')
textColor = input.color(color.white, title = 'Text Color', group = 'Style Settings')
// Gradient Settings
showZeroLineGradient = input.bool(true, title="Show Zero Line Gradient", group="Gradient Settings")
showOverboughtGradient = input.bool(true, title="Show Overbought Gradient", group="Gradient Settings")
showOversoldGradient = input.bool(true, title="Show Oversold Gradient", group="Gradient Settings")
showOscillatorGradient = input.bool(true, title="Show Oscillator Gradient", group="Gradient Settings")
showMaGradient = input.bool(true, title="Show Moving Average Gradient", group="Gradient Settings")
fillEnabled = input.bool(true, title="Enable Gradient Fill", group="Gradient Settings")
fillTransparency = input.int(70, title = 'Gradient Fill Transparency', minval = 0, maxval = 100, group = 'Gradient Settings')
bandTransparency = input.int(40, title = 'Band and Label Gradient Transparency', minval = 0, maxval = 100, group = 'Gradient Settings')
// Additional Settings
rangeUpper = 60
rangeLower = 5
var int gradientTransparency = 85
// Channel Logic
candleMid = (high + low) / 2
midSma = ta.sma(candleMid, length)
avgRange = ta.sma(high - low, length)
avgBody = ta.sma(math.abs(close - open), length)
offset = avgRange * scale / 100 * (1 + avgBody / math.max(avgRange, 1e-10))
upperBand = midSma + offset
lowerBand = midSma - offset
// Oscillator (No Smoothing)
bandWidth = math.max(upperBand - lowerBand, 1e-10)
rawBaseOsc = 100 * (close - lowerBand) / bandWidth - 50
oscValue = math.max(math.min(rawBaseOsc, 100), -100)
// SMA of Oscillator (Signal)
oscSignal = ta.sma(oscValue, signalLength)
// DIVERGENCE DETECTION
lookbackRight = pivotLength
lookbackLeft = pivotLength
pivotHighPrice = ta.pivothigh(high, lookbackLeft, lookbackRight)
pivotLowPrice = ta.pivotlow(low, lookbackLeft, lookbackRight)
pivotHighOsc = ta.pivothigh(oscValue, lookbackLeft, lookbackRight)
pivotLowOsc = ta.pivotlow(oscValue, lookbackLeft, lookbackRight)
// Divergence Conditions
var bool plFound = false
var bool phFound = false
var bool bullCond = false
var bool bearCond = false
oscLbr = oscValue[lookbackRight]
_inRange(bool cond) =>
bars = ta.barssince(cond)
rangeLower <= bars and bars <= rangeUpper
if calculateDivergence
plFound := not na(pivotLowOsc)
oscHl = oscLbr > ta.valuewhen(plFound, oscLbr, 1) and _inRange(plFound[1])
lowLbr = low[lookbackRight]
priceLl = lowLbr < ta.valuewhen(plFound, lowLbr, 1)
bullCond := priceLl and oscHl and plFound
phFound := not na(pivotHighOsc)
oscLh = oscLbr < ta.valuewhen(phFound, oscLbr, 1) and _inRange(phFound[1])
highLbr = high[lookbackRight]
priceHh = highLbr > ta.valuewhen(phFound, highLbr, 1)
bearCond := priceHh and oscLh and phFound
// DYNAMIC COLORS FOR BANDS
overbought_gradient_color = oscSignal >= maOverbought ? overboughtColor : zeroLineColor
oversold_gradient_color = oscSignal <= maOversold ? oversoldColor : zeroLineColor
// PLOTS
// Zero Line
hline(0, title="Zero Line", color=zeroLineColor, linestyle=hline.style_dashed)
plot(showZeroLineGradient ? 0 : na, title="Zero Line Gradient", color=color.new(zeroLineColor, gradientTransparency), linewidth=10, editable=false)
// Horizontal Lines (Overbought/Oversold)
hline(overbought, title="Overbought Level", color=overboughtColor, linestyle=hline.style_dashed)
plot(showOverboughtGradient ? overbought : na, title="Overbought Gradient", color=color.new(overbought_gradient_color, bandTransparency), linewidth=10, editable=false)
hline(oversold, title="Oversold Level", color=oversoldColor, linestyle=hline.style_dashed)
plot(showOversoldGradient ? oversold : na, title="Oversold Gradient", color=color.new(oversold_gradient_color, bandTransparency), linewidth=10, editable=false)
// Oscillator
oscColor = oscValue >= 0 ? oscColorPositive : oscColorNegative
pOsc = plot(oscValue, title="Oscillator", color=oscColor, linewidth=2)
plot(showOscillatorGradient ? oscValue : na, title="Oscillator Gradient", color=color.new(oscColor, gradientTransparency), linewidth=10)
// Signal (SMA of Oscillator)
pSignal = plot(oscSignal, title="Signal SMA", color=signalColor, linewidth=2, style=plot.style_line)
plot(showMaGradient ? oscSignal : na, title="Moving Average Gradient", color=color.new(signalColor, gradientTransparency), linewidth=6)
// Gradient Fill Between Oscillator and Zero Line
pMid1 = plot(oscValue * 0.8, title="Mid Layer 1", color=na, display=display.none)
pMid2 = plot(oscValue * 0.6, title="Mid Layer 2", color=na, display=display.none)
pMid3 = plot(oscValue * 0.4, title="Mid Layer 3", color=na, display=display.none)
pMid4 = plot(oscValue * 0.2, title="Mid Layer 4", color=na, display=display.none)
pZero = plot(0, title="Zero Line for Fill", color=na, display=display.none)
fill(pOsc, pMid1, color=fillEnabled ? color.new(oscColor, fillTransparency) : na, title="Fill Layer 1")
fill(pMid1, pMid2, color=fillEnabled ? color.new(oscColor, fillTransparency + 5) : na, title="Fill Layer 2")
fill(pMid2, pMid3, color=fillEnabled ? color.new(oscColor, fillTransparency + 10) : na, title="Fill Layer 3")
fill(pMid3, pMid4, color=fillEnabled ? color.new(oscColor, fillTransparency + 15) : na, title="Fill Layer 4")
fill(pMid4, pZero, color=fillEnabled ? color.new(oscColor, fillTransparency + 20) : na, title="Fill Layer 5")
// DIVERGENCE PLOTS
plotBullDiv = plot(calculateDivergence and plFound ? oscLbr : na, offset = -lookbackRight, title = 'Bullish Divergence', linewidth = 2, color = bullCond ? bullDivColor : color.new(color.white, 100))
plotBearDiv = plot(calculateDivergence and phFound ? oscLbr : na, offset = -lookbackRight, title = 'Bearish Divergence', linewidth = 2, color = bearCond ? bearDivColor : color.new(color.white, 100))
// DIVERGENCE LABELS
plotshape(bullCond ? oscLbr : na, offset = -lookbackRight, title = 'Bullish Divergence Label', text = ' Bull ', style = shape.labelup, location = location.absolute, color = color.new(bullDivColor, bandTransparency), textcolor = textColor, display = calculateDivergence ? display.all : display.none)
plotshape(bearCond ? oscLbr : na, offset = -lookbackRight, title = 'Bearish Divergence Label', text = ' Bear ', style = shape.labeldown, location = location.absolute, color = color.new(bearDivColor, bandTransparency), textcolor = textColor, display = calculateDivergence ? display.all : display.none)
// SIGNALS
longSignalObos = ta.crossover(oscValue, oversold)
shortSignalObos = ta.crossunder(oscValue, overbought)
longSignalZero = ta.crossover(oscValue, 0)
shortSignalZero = ta.crossunder(oscValue, 0)
longSignalMaZero = ta.crossover(oscSignal, 0)
shortSignalMaZero = ta.crossunder(oscSignal, 0)
// Plotting Signals Based on User Selection
showObos = signalType == 'Overbought/Oversold' or signalType == 'All'
showZero = signalType == 'Zero Line' or signalType == 'All'
showMaZero = signalType == 'MA Zero Line' or signalType == 'All'
plotshape(showObos and longSignalObos ? oscValue : na, title = 'Buy Signal (Oversold)', location = location.bottom, color = oversoldColor, style = shape.triangleup, size = size.tiny)
plotshape(showObos and shortSignalObos ? oscValue : na, title = 'Sell Signal (Overbought)', location = location.top, color = overboughtColor, style = shape.triangledown, size = size.tiny)
plotshape(showZero and longSignalZero ? oscValue : na, title = 'Buy Signal (Zero Line)', location = location.bottom, color = oversoldColor, style = shape.triangleup, size = size.tiny)
plotshape(showZero and shortSignalZero ? oscValue : na, title = 'Sell Signal (Zero Line)', location = location.top, color = overboughtColor, style = shape.triangledown, size = size.tiny)
plotshape(showMaZero and longSignalMaZero ? oscSignal : na, title = 'Buy Signal (MA Zero Line)', location = location.bottom, color = oversoldColor, style = shape.triangleup, size = size.tiny)
plotshape(showMaZero and shortSignalMaZero ? oscSignal : na, title = 'Sell Signal (MA Zero Line)', location = location.top, color = overboughtColor, style = shape.triangledown, size = size.tiny)
// ALERTS
alertcondition(bullCond, title = 'Bullish Divergence Detected', message = 'Bullish divergence detected at pivot, lookback right bars ago.')
alertcondition(bearCond, title = 'Bearish Divergence Detected', message = 'Bearish divergence detected at pivot, lookback right bars ago.')
alertcondition(longSignalObos, title = 'Buy Signal (Oversold)', message = 'Oscillator crossed above the oversold threshold.')
alertcondition(shortSignalObos, title = 'Sell Signal (Overbought)', message = 'Oscillator crossed below the overbought threshold.')
alertcondition(longSignalZero, title = 'Buy Signal (Zero Line)', message = 'Oscillator crossed above the zero line.')
alertcondition(shortSignalZero, title = 'Sell Signal (Zero Line)', message = 'Oscillator crossed below the zero line.')
alertcondition(longSignalMaZero, title = 'Buy Signal (MA Zero Line)', message = 'Moving Average crossed above the zero line.')
alertcondition(shortSignalMaZero, title = 'Sell Signal (MA Zero Line)', message = 'Moving Average crossed below the zero line.')