本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=6
indicator("Trend Reversal Watch | Oscillator", shorttitle="Rev Osc", overlay=false)
// Separate-pane view of reversal pressure, divergence setups, and structure breaks.
string grpSetup = "Reversal Setup"
int pivotLeft = input.int(5, "Pivot bars left", minval=1, group=grpSetup)
int pivotRight = input.int(5, "Pivot bars right", minval=1, group=grpSetup)
int setupExpiryBars = input.int(30, "Maximum bars from setup to trigger", minval=1, group=grpSetup)
int structureLookback = input.int(8, "Structure break lookback", minval=2, group=grpSetup)
string grpIndicators = "Indicator Settings"
int bbLength = input.int(20, "Bollinger length", minval=2, group=grpIndicators)
float bbDeviation = input.float(2.0, "Bollinger standard deviations", minval=0.5, step=0.1, group=grpIndicators)
int rsiLength = input.int(14, "RSI length", minval=2, group=grpIndicators)
float rsiOversold = input.float(30.0, "RSI oversold level", minval=5, maxval=50, step=1, group=grpIndicators)
float rsiOverbought = input.float(70.0, "RSI overbought level", minval=50, maxval=95, step=1, group=grpIndicators)
int diLength = input.int(14, "DI length", minval=2, group=grpIndicators)
int adxSmoothing = input.int(14, "ADX smoothing", minval=2, group=grpIndicators)
float adxContextLevel = input.float(20.0, "ADX trend-context level", minval=5, step=0.5, group=grpIndicators)
string grpVisual = "Oscillator Display"
string visualStyle = input.string("Trend + Reversals", "Visual mode", options=["Trend + Reversals", "Signal States", "Contrarian Pressure (Advanced)", "Bull/Bear Pressure (Advanced)", "Compact Dashboard"], group=grpVisual)
bool showGuides = input.bool(true, "Show guide levels", group=grpVisual)
string panelPosition = input.string("Top Right", "Dashboard position", options=["Top Right", "Top Left", "Bottom Right", "Bottom Left"], group=grpVisual)
// Core calculations
float basis = ta.sma(close, bbLength)
float bandWidth = bbDeviation * ta.stdev(close, bbLength)
float upperBand = basis + bandWidth
float lowerBand = basis - bandWidth
float rsi = ta.rsi(close, rsiLength)
[plusDI, minusDI, adx] = ta.dmi(diLength, adxSmoothing)
// Compare confirmed pivots with RSI at the matching pivot bars.
float pivotLow = ta.pivotlow(low, pivotLeft, pivotRight)
float pivotHigh = ta.pivothigh(high, pivotLeft, pivotRight)
var float previousPivotLow = na
var float previousPivotLowRsi = na
var float previousPivotHigh = na
var float previousPivotHighRsi = na
bool newBullishDivergence = false
bool newBearishDivergence = false
if not na(pivotLow) and not na(rsi[pivotRight])
float pivotLowRsi = rsi[pivotRight]
newBullishDivergence := not na(previousPivotLow) and pivotLow < previousPivotLow and pivotLowRsi > previousPivotLowRsi
previousPivotLow := pivotLow
previousPivotLowRsi := pivotLowRsi
if not na(pivotHigh) and not na(rsi[pivotRight])
float pivotHighRsi = rsi[pivotRight]
newBearishDivergence := not na(previousPivotHigh) and pivotHigh > previousPivotHigh and pivotHighRsi < previousPivotHighRsi
previousPivotHigh := pivotHigh
previousPivotHighRsi := pivotHighRsi
bool bullishStretchAtPivot = not na(pivotLow) and (low[pivotRight] < lowerBand[pivotRight] or rsi[pivotRight] <= rsiOversold)
bool bearishStretchAtPivot = not na(pivotHigh) and (high[pivotRight] > upperBand[pivotRight] or rsi[pivotRight] >= rsiOverbought)
bool bullishSetupStarted = newBullishDivergence and bullishStretchAtPivot
bool bearishSetupStarted = newBearishDivergence and bearishStretchAtPivot
var bool pendingBullish = false
var bool pendingBearish = false
var int bullishSetupBar = na
var int bearishSetupBar = na
var float bullishTrigger = na
var float bearishTrigger = na
if bullishSetupStarted
pendingBullish := true
pendingBearish := false
bullishSetupBar := bar_index
bearishSetupBar := na
bullishTrigger := ta.highest(high, structureLookback)[1]
bearishTrigger := na
if bearishSetupStarted
pendingBearish := true
pendingBullish := false
bearishSetupBar := bar_index
bullishSetupBar := na
bearishTrigger := ta.lowest(low, structureLookback)[1]
bullishTrigger := na
if pendingBullish and not na(bullishSetupBar) and bar_index - bullishSetupBar > setupExpiryBars
pendingBullish := false
bullishTrigger := na
if pendingBearish and not na(bearishSetupBar) and bar_index - bearishSetupBar > setupExpiryBars
pendingBearish := false
bearishTrigger := na
bool bullishSignal = barstate.isconfirmed and pendingBullish and bar_index > bullishSetupBar and not na(bullishTrigger) and close > bullishTrigger
bool bearishSignal = barstate.isconfirmed and pendingBearish and bar_index > bearishSetupBar and not na(bearishTrigger) and close < bearishTrigger
if bullishSignal
pendingBullish := false
bullishTrigger := na
if bearishSignal
pendingBearish := false
bearishTrigger := na
// Normalize extension and setup state into readable 0–100 pressure values.
float rsiBullPressure = math.max(0.0, math.min(100.0, (50.0 - rsi) / math.max(50.0 - rsiOversold, 1.0) * 100.0))
float rsiBearPressure = math.max(0.0, math.min(100.0, (rsi - 50.0) / math.max(rsiOverbought - 50.0, 1.0) * 100.0))
float bbBullPressure = bandWidth > 0 ? math.max(0.0, math.min(100.0, (lowerBand - close) / bandWidth * 100.0)) : 0.0
float bbBearPressure = bandWidth > 0 ? math.max(0.0, math.min(100.0, (close - upperBand) / bandWidth * 100.0)) : 0.0
float bullishPressure = math.max(rsiBullPressure, bbBullPressure)
float bearishPressure = math.max(rsiBearPressure, bbBearPressure)
bullishPressure := pendingBullish ? math.max(bullishPressure, 70.0) : bullishPressure
bearishPressure := pendingBearish ? math.max(bearishPressure, 70.0) : bearishPressure
bullishPressure := bullishSignal ? 100.0 : bullishPressure
bearishPressure := bearishSignal ? 100.0 : bearishPressure
float netPressure = bullishPressure - bearishPressure
float signedTrendStrength = plusDI > minusDI ? adx : minusDI > plusDI ? -adx : 0.0
float stateValue = bullishSignal ? 100.0 : bearishSignal ? -100.0 : pendingBullish ? 60.0 : pendingBearish ? -60.0 : bullishPressure >= 50.0 ? 25.0 : bearishPressure >= 50.0 ? -25.0 : 0.0
color bullColor = color.rgb(35, 205, 145)
color bearColor = color.rgb(245, 85, 105)
color setupMarkerColor = color.rgb(255, 185, 55)
color neutralColor = color.rgb(130, 145, 165)
color pressureColor = netPressure >= 50 ? bullColor : netPressure <= -50 ? bearColor : netPressure > 0 ? color.new(bullColor, 35) : netPressure < 0 ? color.new(bearColor, 35) : neutralColor
// Default mode: signed ADX shows current DI direction and trend strength.
color trendColor = adx < adxContextLevel ? color.new(neutralColor, 30) : signedTrendStrength > 0 ? bullColor : signedTrendStrength < 0 ? bearColor : neutralColor
color trendGlow = color.new(trendColor, 82)
bgcolor(visualStyle == "Trend + Reversals" and adx >= adxContextLevel ? color.new(signedTrendStrength > 0 ? bullColor : bearColor, 96) : na, title="Trend Context Glow")
plot(visualStyle == "Trend + Reversals" ? signedTrendStrength : na, "Trend Strength Glow", style=plot.style_columns, color=trendGlow, linewidth=10)
plot(visualStyle == "Trend + Reversals" ? signedTrendStrength : na, "Current Trend Strength", style=plot.style_columns, color=trendColor, linewidth=4)
plot(visualStyle == "Trend + Reversals" and showGuides ? adxContextLevel : na, "Bullish Context Guide", color=color.new(bullColor, 70))
plot(visualStyle == "Trend + Reversals" and showGuides ? -adxContextLevel : na, "Bearish Context Guide", color=color.new(bearColor, 70))
plotshape(visualStyle == "Trend + Reversals" and bullishSetupStarted ? -math.max(adx, adxContextLevel) : na, "Bullish Reversal Setup", shape.diamond, location.absolute, setupMarkerColor, size=size.small, text="WATCH", textcolor=color.black)
plotshape(visualStyle == "Trend + Reversals" and bearishSetupStarted ? math.max(adx, adxContextLevel) : na, "Bearish Reversal Setup", shape.diamond, location.absolute, setupMarkerColor, size=size.small, text="WATCH", textcolor=color.black)
plotshape(visualStyle == "Trend + Reversals" and bullishSignal ? math.max(adx, adxContextLevel) : na, "Bullish Reversal Confirmed", shape.labelup, location.absolute, bullColor, size=size.small, text="REV UP", textcolor=color.white)
plotshape(visualStyle == "Trend + Reversals" and bearishSignal ? -math.max(adx, adxContextLevel) : na, "Bearish Reversal Confirmed", shape.labeldown, location.absolute, bearColor, size=size.small, text="REV DN", textcolor=color.white)
// Signal States separates reversal setups from confirmed structure breaks.
color stateColor = bullishSignal ? bullColor : bearishSignal ? bearColor : pendingBullish or pendingBearish ? setupMarkerColor : neutralColor
plot(visualStyle == "Signal States" ? stateValue : na, "Reversal State", style=plot.style_columns, color=stateColor, linewidth=7)
// Advanced modes: countertrend reversal potential, separate from current trend direction.
plot(visualStyle == "Contrarian Pressure (Advanced)" ? netPressure : na, "Net Reversal Pressure", style=plot.style_columns, color=pressureColor, linewidth=4)
plot(visualStyle == "Bull/Bear Pressure (Advanced)" ? bullishPressure : na, "Bullish Reversal Potential", color=bullColor, style=plot.style_area, linewidth=2)
plot(visualStyle == "Bull/Bear Pressure (Advanced)" ? -bearishPressure : na, "Bearish Reversal Potential", color=bearColor, style=plot.style_area, linewidth=2)
// Guide levels
plot(showGuides and visualStyle == "Trend + Reversals" ? 0.0 : na, "Trend Zero Line", color=color.new(neutralColor, 55))
plot(showGuides and visualStyle == "Contrarian Pressure (Advanced)" ? 50.0 : na, "Bull Pressure Guide", color=color.new(bullColor, 65))
plot(showGuides and visualStyle == "Contrarian Pressure (Advanced)" ? -50.0 : na, "Bear Pressure Guide", color=color.new(bearColor, 65))
plot(showGuides and (visualStyle == "Contrarian Pressure (Advanced)" or visualStyle == "Signal States" or visualStyle == "Bull/Bear Pressure (Advanced)") ? 0.0 : na, "Zero Line", color=color.new(neutralColor, 55))
plotshape(visualStyle == "Signal States" and bullishSignal ? 100.0 : na, "Bullish Reversal Confirmed", shape.circle, location.absolute, bullColor, size=size.small, text="+", textcolor=color.black)
plotshape(visualStyle == "Signal States" and bearishSignal ? -100.0 : na, "Bearish Reversal Confirmed", shape.circle, location.absolute, bearColor, size=size.small, text="−", textcolor=color.white)
// Compact context panel
f_panel_position(string choice) =>
choice == "Top Left" ? position.top_left : choice == "Bottom Right" ? position.bottom_right : choice == "Bottom Left" ? position.bottom_left : position.top_right
string setupState = pendingBullish ? "Bullish setup: waiting for break" : pendingBearish ? "Bearish setup: waiting for break" : bullishSignal ? "Bullish reversal confirmed" : bearishSignal ? "Bearish reversal confirmed" : "No active setup"
color setupColor = pendingBullish or pendingBearish ? setupMarkerColor : bullishSignal ? bullColor : bearishSignal ? bearColor : color.rgb(85, 95, 110)
var table panel = table.new(f_panel_position(panelPosition), 2, 5, border_width=1, frame_color=color.new(color.gray, 55), border_color=color.new(color.gray, 80))
if barstate.islast and (visualStyle == "Compact Dashboard" or visualStyle == "Trend + Reversals")
if visualStyle == "Trend + Reversals"
string trendState = adx < adxContextLevel ? "Weak / unclear" : plusDI > minusDI ? "Uptrend" : minusDI > plusDI ? "Downtrend" : "Neutral"
string reversalState = pendingBullish ? "Bull setup · waiting for break" : pendingBearish ? "Bear setup · waiting for break" : bullishSignal ? "Bull reversal confirmed" : bearishSignal ? "Bear reversal confirmed" : "No active setup"
color trendStateColor = adx < adxContextLevel ? neutralColor : plusDI > minusDI ? bullColor : minusDI > plusDI ? bearColor : neutralColor
table.cell(panel, 0, 0, "TREND + REVERSALS", text_color=color.white, bgcolor=color.rgb(30, 38, 52), text_size=size.small)
table.cell(panel, 1, 0, trendState, text_color=color.white, bgcolor=trendStateColor, text_size=size.small)
table.cell(panel, 0, 1, "Trend strength", text_color=color.white, bgcolor=color.rgb(38, 46, 60), text_size=size.small)
table.cell(panel, 1, 1, "ADX " + str.tostring(adx, "#.0"), text_color=color.white, bgcolor=color.rgb(38, 46, 60), text_size=size.small)
table.cell(panel, 0, 2, "Reversal status", text_color=color.white, bgcolor=color.rgb(38, 46, 60), text_size=size.small)
table.cell(panel, 1, 2, reversalState, text_color=color.white, bgcolor=setupState == "No active setup" ? color.rgb(38, 46, 60) : setupMarkerColor, text_size=size.small)
table.cell(panel, 0, 3, "RSI", text_color=color.white, bgcolor=color.rgb(38, 46, 60), text_size=size.small)
table.cell(panel, 1, 3, str.tostring(rsi, "#.0"), text_color=color.white, bgcolor=color.rgb(38, 46, 60), text_size=size.small)
table.cell(panel, 0, 4, "DI direction", text_color=color.white, bgcolor=color.rgb(38, 46, 60), text_size=size.small)
table.cell(panel, 1, 4, plusDI > minusDI ? "Bullish" : minusDI > plusDI ? "Bearish" : "Neutral", text_color=color.white, bgcolor=color.rgb(38, 46, 60), text_size=size.small)
else
table.cell(panel, 0, 0, "REVERSAL WATCH", text_color=color.white, bgcolor=color.rgb(30, 38, 52), text_size=size.small)
table.cell(panel, 1, 0, setupState, text_color=color.white, bgcolor=setupColor, text_size=size.small)
table.cell(panel, 0, 1, "Bull reversal potential", text_color=color.white, bgcolor=color.rgb(38, 46, 60), text_size=size.small)
table.cell(panel, 1, 1, str.tostring(bullishPressure, "#.0") + "%", text_color=color.white, bgcolor=color.rgb(38, 46, 60), text_size=size.small)
table.cell(panel, 0, 2, "Bear reversal potential", text_color=color.white, bgcolor=color.rgb(38, 46, 60), text_size=size.small)
table.cell(panel, 1, 2, str.tostring(bearishPressure, "#.0") + "%", text_color=color.white, bgcolor=color.rgb(38, 46, 60), text_size=size.small)
table.cell(panel, 0, 3, "ADX", text_color=color.white, bgcolor=color.rgb(38, 46, 60), text_size=size.small)
table.cell(panel, 1, 3, str.tostring(adx, "#.0") + (adx >= adxContextLevel ? " · trend context" : " · weak trend"), text_color=color.white, bgcolor=color.rgb(38, 46, 60), text_size=size.small)
table.cell(panel, 0, 4, "DI direction", text_color=color.white, bgcolor=color.rgb(38, 46, 60), text_size=size.small)
table.cell(panel, 1, 4, plusDI > minusDI ? "Bullish" : minusDI > plusDI ? "Bearish" : "Neutral", text_color=color.white, bgcolor=color.rgb(38, 46, 60), text_size=size.small)
alertcondition(bullishSignal, "Bullish Reversal Confirmed", "Bullish divergence at an extreme followed by an upside structure break.")
alertcondition(bearishSignal, "Bearish Reversal Confirmed", "Bearish divergence at an extreme followed by a downside structure break.")