Mean Reversion Oscillator [Alpha Extract]

AlphaExtract · study · 184 行 · 点赞 546 · TradingView 原页

本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。

Pine Script

//@version=6
indicator("Mean Reversion Oscillator [Alpha Extract]", overlay=false)
 
// Input Parameters
bb_length = input.int(20, title="Bollinger Length", minval=5, maxval=100)
bb_mult = input.float(2.0, title="BB Multiplier", minval=0.5, maxval=4.0, step=0.1)
rsi_length = input.int(14, title="RSI Length", minval=2, maxval=50)
stoch_k = input.int(14, title="Stochastic K", minval=5, maxval=50)
stoch_d = input.int(3, title="Stochastic D", minval=1, maxval=10)
mfi_length = input.int(14, title="MFI Length", minval=2, maxval=50)
composite_smooth = input.int(3, title="Composite Smoothing", minval=1, maxval=10)
 
// Threshold Inputs
extreme_level = input.float(80.0, title="Extreme Level", minval=70, maxval=90, step=5)
moderate_level = input.float(65.0, title="Moderate Level", minval=60, maxval=75, step=5)
 
// Style Inputs
show_composite = input.bool(true, title="Show Composite Oscillator")
show_individual = input.bool(false, title="Show Individual Components")
show_histogram = input.bool(true, title="Show Momentum Histogram")
show_signals = input.bool(true, title="Show Entry Signals")
show_divergence = input.bool(true, title="Show Divergence Alerts")
color_bars = input.bool(true, title="Color OHLC Bars")
 
// Color Theme
bull_color = input.color(color.new(#00ff88, 0), title="Bullish Color")
bear_color = input.color(color.new(#ff3366, 0), title="Bearish Color")
neutral_color = input.color(color.new(#888888, 0), title="Neutral Color")
extreme_color = input.color(#1100ff, title="Extreme Color")
 
// Source
src = close
high_src = high
low_src = low
 
// === OSCILLATOR CALCULATIONS ===
 
// 1. Bollinger %B 
bb_basis = ta.sma(src, bb_length)
bb_dev = bb_mult * ta.stdev(src, bb_length)
bb_upper = bb_basis + bb_dev
bb_lower = bb_basis - bb_dev
bb_percent = (src - bb_lower) / (bb_upper - bb_lower) * 100
 
// 2. RSI
rsi = ta.rsi(src, rsi_length)
 
// 3. Stochastic
stoch_k_val = ta.stoch(src, high_src, low_src, stoch_k)
stoch_d_val = ta.sma(stoch_k_val, stoch_d)
 
// 4. Money Flow Index
mfi = ta.mfi(close, mfi_length)
 
// 5. Williams %R 
williams_r = 100 + ta.wpr(stoch_k)
 
// === COMPOSITE MEAN REVERSION SCORE ===
normalized_bb = bb_percent
normalized_rsi = rsi
normalized_stoch = stoch_d_val
normalized_mfi = mfi
normalized_williams = williams_r
 
// Weighted composite (equal weights)
composite_raw = (normalized_bb + normalized_rsi + normalized_stoch + normalized_mfi + normalized_williams) / 5
composite = ta.sma(composite_raw, composite_smooth)
 
// === MOMENTUM AND DIVERGENCE ===
price_momentum = ta.mom(src, 10)
price_momentum_normalized = ta.rsi(price_momentum, 14)
 
// Oscillator momentum (rate of change)
osc_momentum = ta.mom(composite, 5)
histogram = osc_momentum
 
// Divergence detection
price_high = ta.pivothigh(high_src, 5, 5)
price_low = ta.pivotlow(low_src, 5, 5)
osc_high = ta.pivothigh(composite, 5, 5)
osc_low = ta.pivotlow(composite, 5, 5)
 
// === SIGNAL GENERATION ===
// Extreme levels
oversold_extreme = composite < (100 - extreme_level)
overbought_extreme = composite > extreme_level
oversold_moderate = composite < (100 - moderate_level)
overbought_moderate = composite > moderate_level
 
// Mean reversion entry signals
bullish_entry = ta.crossover(composite, 100 - extreme_level) and composite[1] < (100 - extreme_level)
bearish_entry = ta.crossunder(composite, extreme_level) and composite[1] > extreme_level
 
// Momentum confirmation
momentum_bullish = histogram > histogram[1]
momentum_bearish = histogram < histogram[1]
 
// Confirmed signals
confirmed_bullish = bullish_entry and momentum_bullish
confirmed_bearish = bearish_entry and momentum_bearish
 
// === VISUAL ELEMENTS ===
 
// Zero line and extreme levels
hline(50, "Midline", color=color.new(neutral_color, 70), linestyle=hline.style_dashed)
hline(extreme_level, "Overbought Extreme", color=color.new(bear_color, 60))
hline(100-extreme_level, "Oversold Extreme", color=color.new(bull_color, 60))
hline(moderate_level, "Overbought Moderate", color=color.new(bear_color, 80), linestyle=hline.style_dotted)
hline(100-moderate_level, "Oversold Moderate", color=color.new(bull_color, 80), linestyle=hline.style_dotted)
 
// Background coloring for extreme zones
bgcolor(overbought_extreme ? color.new(bear_color, 95) : oversold_extreme ? color.new(bull_color, 95) : overbought_moderate ? color.new(bear_color, 97) : oversold_moderate ? color.new(bull_color, 97) : na, title="Zone Background")
 
// Composite oscillator line with dynamic coloring
composite_color = composite > 80 ? bear_color :
                  composite < 20 ? bull_color :
                  composite > 65 ? color.new(bear_color, 30) :
                  composite < 35 ? color.new(bull_color, 30) : neutral_color
 
plot(show_composite ? composite : na, title="Composite MRO", color=composite_color, linewidth=3)
 
// Individual components
plot(show_individual ? rsi : na, title="RSI", color=color.new(color.blue, 70), linewidth=1)
plot(show_individual ? stoch_d_val : na, title="Stochastic", color=color.new(color.purple, 70), linewidth=1)
plot(show_individual ? mfi : na, title="MFI", color=color.new(color.orange, 70), linewidth=1)
 
// Momentum histogram
histogram_color = histogram > 0 ? color.new(#01b25f, 0) : color.new(#ce1159, 0)
 
plot(show_histogram ? histogram : na, title="Momentum Histogram", style=plot.style_histogram, 
     color=histogram_color, histbase=0, linewidth=2)
 
// Entry signals
plotshape(show_signals and confirmed_bullish, title="Bullish Entry", location=location.belowbar, 
          style=shape.circle, size=size.tiny, color=color.new(bull_color,40),force_overlay = true)
plotshape(show_signals and confirmed_bearish, title="Bearish Entry", location=location.abovebar, 
          style=shape.circle, size=size.tiny, color=color.new(bear_color,40), force_overlay = true)
 
// Divergence alerts (simplified)
divergence_bull = show_divergence and oversold_extreme 
divergence_bear = show_divergence and overbought_extreme 
 
plotshape(divergence_bull, title="Bullish Divergence", location=location.bottom, 
          style=shape.diamond, size=size.tiny, color=extreme_color)
plotshape(divergence_bear, title="Bearish Divergence", location=location.top, 
          style=shape.diamond, size=size.tiny, color=extreme_color)
 
// === INFORMATION TABLE ===
var table info_table = table.new(position.top_right, 2, 7, bgcolor=color.new(color.black, 85), border_width=1)
 
if barstate.islast
    table.cell(info_table, 0, 0, "MRO", text_color=color.white, text_size=size.normal, bgcolor=color.new(neutral_color, 70))
    table.cell(info_table, 1, 0, str.tostring(math.round(composite, 1)), text_color=composite_color, text_size=size.normal, bgcolor=color.new(neutral_color, 70))
    
    table.cell(info_table, 0, 1, "Status:", text_color=color.gray, text_size=size.small)
    status_text = overbought_extreme ? "EXTREME OB" :
                  oversold_extreme ? "EXTREME OS" :
                  overbought_moderate ? "MODERATE OB" :
                  oversold_moderate ? "MODERATE OS" : "NEUTRAL"
    status_color = overbought_extreme or oversold_extreme ? extreme_color :
                   overbought_moderate ? bear_color :
                   oversold_moderate ? bull_color : neutral_color
    table.cell(info_table, 1, 1, status_text, text_color=status_color, text_size=size.small)
    
    table.cell(info_table, 0, 2, "RSI:", text_color=color.gray, text_size=size.small)
    table.cell(info_table, 1, 2, str.tostring(math.round(rsi, 1)), text_color=rsi > 70 ? bear_color : rsi < 30 ? bull_color : neutral_color, text_size=size.small)
    
    table.cell(info_table, 0, 3, "Stoch:", text_color=color.gray, text_size=size.small)
    table.cell(info_table, 1, 3, str.tostring(math.round(stoch_d_val, 1)), text_color=stoch_d_val > 80 ? bear_color : stoch_d_val < 20 ? bull_color : neutral_color, text_size=size.small)
    
    table.cell(info_table, 0, 4, "MFI:", text_color=color.gray, text_size=size.small)
    table.cell(info_table, 1, 4, str.tostring(math.round(mfi, 1)), text_color=mfi > 80 ? bear_color : mfi < 20 ? bull_color : neutral_color, text_size=size.small)
    
    table.cell(info_table, 0, 5, "Momentum:", text_color=color.gray, text_size=size.small)
    mom_text = histogram > 0 ? "BULLISH" : "BEARISH"
    table.cell(info_table, 1, 5, mom_text, text_color=histogram > 0 ? bull_color : bear_color, text_size=size.small)
 
// === ALERTS ===
alertcondition(confirmed_bullish, title="Bullish Mean Reversion", message="MRO: Confirmed bullish mean reversion signal")
alertcondition(confirmed_bearish, title="Bearish Mean Reversion", message="MRO: Confirmed bearish mean reversion signal")
alertcondition(divergence_bull, title="Bullish Divergence", message="MRO: Potential bullish divergence detected")
alertcondition(divergence_bear, title="Bearish Divergence", message="MRO: Potential bearish divergence detected")
alertcondition(oversold_extreme, title="Extreme Oversold", message="MRO: Reached extreme oversold levels")
alertcondition(overbought_extreme, title="Extreme Overbought", message="MRO: Reached extreme overbought levels")

← 返回列表