本页源码来自 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/
// © D_QUANT
//@version=6
indicator(title="Mean Reversion & Momentum Hybrid | D_QUANT ",shorttitle = "M-R-M-H", overlay=false)
//--- User Inputs ---\\
source = input.source(close,title = 'Source', group = 'Core Settings')
bb_length = input.int(20, "Bollinger Bands Length", minval=1, group ='Core Settings')
bb_mult = input.float(2.0, "Bollinger Bands Multiplier", minval=0.1, step=0.1, group ='Core Settings')
min_bb_width = input.float(0.5, "Minimum BB Width (% of Price)", minval=0.0, step=0.1, group ='Core Settings')
//--- Bollinger Bands Percentage User Inputs ---\\
// These inputs control the source and thresholds for the Bollinger Bands Percentage calculation
LongThreshold = input.float(55, "BB% Long Threshold (L)", group="Bollinger Bands Percentage Settings")
ShortThreshold= input.float(45, "BB% Short Threshold (S)", group="Bollinger Bands Percentage Settings")
//--- ATR RMA User Inputs ---\\
Lookback = input.int(15, "RMA length", group ='Trend Filter Parameters')
ATR_Lookback = input.int(20, "ATR length", group ='Trend Filter Parameters' )
//--- 75th 25th Percentile Momentum User Inputs ---\\
length = input.int(25,title = 'Momentum Length', group = 'Momentum Parameters')
mult_75 = input.float(1.3,step=0.1,title = 'mult_75', group = 'Momentum Parameters')
mult_25 = input.float(1.3,step=0.1,title = 'mult_25', group = 'Momentum Parameters')
percentile_75 = ta.percentile_nearest_rank(source,length,75)
percentile_25 = ta.percentile_nearest_rank(source,length,25)
//--- Coloring Inputs ---\\
bc = input.bool(true, "Bar Coloring ?")
use_bg_color = input.bool(false, "Use Background Color for Signals")
//--- CALCULATIONS ---\\
// --- Bollinger Bands Calculations ---\\
// Note: This calculates the standard Bollinger Bands for width calculation,
// but the 'positionBetweenBands' from Bollinger Bands Percentage will determine the price's position.
[bb_middle, bb_upper, bb_lower] = ta.bb(close, bb_length, bb_mult)
bb_width = (bb_upper - bb_lower) / bb_middle * 100 // Width as % of middle band
//--- RMA + ATR for trend filter ---\\
//--- Calculating RMA & ATR ---\\
float RMA = ta.rma(source, Lookback)
float atr = ta.atr(ATR_Lookback)
// Conditions
longFIL = source > RMA + atr
shortFIL = source < RMA - atr
var score = 0
if longFIL and not shortFIL
score := 1
if shortFIL
score := -1
//--- 75th 25th Percentile Momentum Calculations ---\\
LongMomentum = close > percentile_75 + mult_75*atr
ShortMomentum = close < percentile_25 - mult_25*atr
var sig = 0
if longFIL and not shortFIL
sig :=1
if shortFIL
sig :=-1
bb_percent_src = (close + open + high + low) / 4
//--- Bollinger Bands Percentage Calculations ---\\
// These bands are specifically for the %B calculation, using the chosen 'bb_percent_src'
bb_percent_basis = ta.sma(bb_percent_src, bb_length)
bb_percent_dev = bb_mult * ta.stdev(bb_percent_src, bb_length)
bb_percent_upper = bb_percent_basis + bb_percent_dev
bb_percent_lower = bb_percent_basis - bb_percent_dev
//--- Bolinger Bands Percentage Calculation ---\\
positionBetweenBands = 100 * (bb_percent_src - bb_percent_lower) / (bb_percent_upper - bb_percent_lower)
//--- BB% Signal Logic ---\\
var bb_percent_Sig = 0
bb_percent_Sig := positionBetweenBands > LongThreshold ? 1 : positionBetweenBands < ShortThreshold ? -1 : 0
//--- Combined Signal Conditions ---\\
// Buy:
// - BB% shows price is in the upper band zone (above long threshold),
// - Trend filter confirms bullish bias (price > RMA + ATR)
// - Momentum confirms bullish breakout (price > 75th percentile + ATR multiplier)
// - BB width is above the minimum to avoid low-volatility signals
buy_condition = (bb_percent_Sig == 1) and sig == 1 and score == 1 and bb_width > min_bb_width
// Sell:
// - BB% shows price is in the lower band zone (below short threshold)
// - Trend filter confirms bearish bias (price < RMA - ATR)
// - Momentum confirms bearish breakdown (price < 25th percentile - ATR multiplier)
// - BB width is above the minimum to avoid low-volatility signals
sell_condition = (bb_percent_Sig == -1) and sig == -1 and score == -1 and bb_width > min_bb_width
//--- PLOTTING ---\\
// Bar Coloring
color1 = buy_condition ? color.rgb(0, 123, 167) : sell_condition ? color.rgb(200, 60, 60) : color.rgb(76, 78, 82)
color = bc ? color1 : na
plotcandle(open, high, low, close, 'BarColor', color = color, bordercolor = color, wickcolor = color, force_overlay = true)
//--- Background color logic (global scope) ---\\
bg_color = use_bg_color ? (buy_condition ? color.new(color.green, 90) : sell_condition ? color.new(color.red, 90) : na) : na
bgcolor(bg_color)
//--- NEW VISUAL STYLE ---\\
// Map Osci to the signal value
Osci = if buy_condition
1.0
else if sell_condition
-1.0
else
0.0
// Map oscicolcol to the color based on buy/sell conditions
oscicol = if buy_condition
color.rgb(0, 123, 167)
else if sell_condition
color.rgb(200, 60, 60)
else
color.rgb(95, 92, 92)
// Define colup and coldn based on buy/sell colors
colup = color.rgb(0, 123, 167)
coldn = color.rgb(200, 60, 60)
hline(0, color = color.rgb(77, 74, 74, 79), title = "Zero Line")
//--- Gradient Fade Oscillator ---\\
// Memory for fading effect
var float fadeSignal = 0.0
// Decay rate: 10% per bar
decayRate = 0.1
// Update fade signal
fadeSignal := buy_condition ? 1.0 : sell_condition ? -1.0 : fadeSignal * (1 - decayRate)
// Normalize to range 0-1 for color blending
absFade = math.abs(fadeSignal)
isBuy = fadeSignal > 0
isSell = fadeSignal < 0
// Define base colors
baseBuyColor = color.rgb(0, 123, 167)
baseSellColor = color.rgb(200, 60, 60)
neutralColor = color.rgb(255, 255, 255)
// Interpolate color based on fade strength
gradientColor = isBuy ? color.from_gradient(absFade, 0, 1, neutralColor, baseBuyColor) :
isSell ? color.from_gradient(absFade, 0, 1, neutralColor, baseSellColor) :
color.rgb(95, 92, 92)
//--- Oscillator Gradient Line & Area Fill ---\\
plot_zero = plot(0, title="Zero Line (Plot)", color=color.new(color.white, 100))
// Main Oscillator Line with Gradient
plot_signal = plot(fadeSignal, color=gradientColor, linewidth=2, title="Gradient Signal Line")
// Gradient Area Fill between zero and signal
fill(plot_zero, plot_signal, color=color.new(gradientColor, 80), title="Gradient Fill Under Oscillator")