本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
// This Pine Script code is subject to the Mozilla Public License 2.0.
// https://mozilla.org/MPL/2.0/
// © JesseV365
// Standalone RBEC extracted from OS Market Compass; original OS Flow Study formulas.
//@version=6
indicator("OS RBEC — RSI BWMA Extensions Channel", "OS RBEC", overlay=true, max_bars_back=5000)
bool showRbec = input.bool(true, "Show RBEC", group="01 — RBEC")
int RBEC_LENGTH = input.int(50, "BWMA Length", minval=3, maxval=500, group="01 — RBEC")
float RBEC_ALPHA = input.float(3.0, "BWMA Alpha", minval=1.0, maxval=20, step=0.1, group="01 — RBEC")
float RBEC_BETA = input.float(3.0, "BWMA Beta", minval=1.0, maxval=20, step=0.1, group="01 — RBEC")
int RBEC_RSI_LENGTH = input.int(14, "RSI Length", minval=2, maxval=500, group="01 — RBEC")
int RBEC_RSI_SMOOTHING = input.int(3, "RSI Smoothing", minval=1, maxval=100, group="01 — RBEC")
float RBEC_BULL_THRESHOLD = input.float(55.0, "Bull RSI Threshold", minval=50.1, maxval=99, step=0.1, group="01 — RBEC")
float RBEC_BEAR_THRESHOLD = input.float(45.0, "Bear RSI Threshold", minval=1, maxval=49.9, step=0.1, group="01 — RBEC")
int RBEC_CONFIRM_BARS = input.int(2, "Regime Confirmation Bars", minval=1, maxval=50, group="01 — RBEC")
int DEVIATION_LENGTH = input.int(50, "Deviation Length", minval=2, maxval=500, group="01 — RBEC")
float INNER_MULTIPLIER = input.float(1.0, "Inner Band Multiplier", minval=0.1, maxval=20, step=0.1, group="01 — RBEC")
float OUTER_MULTIPLIER = input.float(2.0, "Outer Band Multiplier", minval=0.1, maxval=50, step=0.1, group="01 — RBEC")
int WIDTH_SMOOTHING = input.int(3, "Width Smoothing", minval=1, maxval=100, group="01 — RBEC")
color BULL = input.color(#30FF85, "RBEC Bull", group="01 — RBEC")
color BEAR = input.color(#FF1100, "RBEC Bear", group="01 — RBEC")
color COUNTER = input.color(#FFA800, "RBEC Transition", group="01 — RBEC")
color INNER = input.color(#00A6BD, "Inner bands", group="01 — RBEC")
color OUTER = input.color(#9B45A5, "Outer bands", group="01 — RBEC")
string RBEC_GROUP = "01 — RBEC"
bool showRbecCenter = input.bool(true, "Show Center", group=RBEC_GROUP)
bool showRbecBands = input.bool(true, "Show Bands and Fills", group=RBEC_GROUP)
string rbecCenterStyle = input.string("Circles", "Center Style", options=["Circles", "Line"], group=RBEC_GROUP)
int rbecLineWidth = input.int(3, "Center Width", minval=1, maxval=5, group=RBEC_GROUP)
int rbecInnerLineTransparency = input.int(35, "Inner Line Transparency", minval=0, maxval=100, group=RBEC_GROUP)
int rbecOuterLineTransparency = input.int(48, "Outer Line Transparency", minval=0, maxval=100, group=RBEC_GROUP)
int rbecChannelTransparency = input.int(96, "Channel Fill Transparency", minval=0, maxval=100, group=RBEC_GROUP)
int rbecExtensionTransparency = input.int(94, "Extension Fill Transparency", minval=0, maxval=100, group=RBEC_GROUP)
f_bwma(float src, int length, float alphaShape, float betaShape) =>
float weightedSum = 0.0
float denominator = 0.0
if bar_index >= length - 1
for i = 0 to length - 1
float x = float(i) / float(math.max(length - 1, 1))
float weight = math.pow(x, alphaShape - 1.0) * math.pow(1.0 - x, betaShape - 1.0)
weightedSum += src[i] * weight
denominator += weight
denominator > 0 ? weightedSum / denominator : na
float rbecSource = input.source(close, "Price Source", group=RBEC_GROUP)
float rbecCenter = f_bwma(rbecSource, RBEC_LENGTH, RBEC_ALPHA, RBEC_BETA)
float rbecRsi = ta.rsi(rbecSource, RBEC_RSI_LENGTH)
float rbecRsiState = ta.ema(rbecRsi, RBEC_RSI_SMOOTHING)
float rbecSlope = rbecCenter - rbecCenter[1]
bool rbecBullCondition = rbecRsiState >= RBEC_BULL_THRESHOLD and rbecSource >= rbecCenter and rbecSlope >= 0
bool rbecBearCondition = rbecRsiState <= RBEC_BEAR_THRESHOLD and rbecSource <= rbecCenter and rbecSlope <= 0
bool rbecBullConfirmed = math.sum(rbecBullCondition ? 1.0 : 0.0, RBEC_CONFIRM_BARS) >= RBEC_CONFIRM_BARS
bool rbecBearConfirmed = math.sum(rbecBearCondition ? 1.0 : 0.0, RBEC_CONFIRM_BARS) >= RBEC_CONFIRM_BARS
var int rbecRegime = 0
if barstate.isconfirmed
if rbecBullConfirmed
rbecRegime := 1
else if rbecBearConfirmed
rbecRegime := -1
else if rbecRegime == 0 and not na(rbecRsiState)
rbecRegime := rbecRsiState >= 50 ? 1 : -1
bool bullCounterpressure = rbecRegime == 1 and (rbecRsi <= RBEC_BEAR_THRESHOLD or (rbecRsi < 50 and rbecSlope < 0))
bool bearCounterpressure = rbecRegime == -1 and (rbecRsi >= RBEC_BULL_THRESHOLD or (rbecRsi > 50 and rbecSlope > 0))
color rbecColor = na(rbecRsi) or rbecRegime == 0 ? color.gray : bullCounterpressure or bearCounterpressure ? COUNTER : rbecRegime == 1 ? (rbecRsi >= RBEC_BULL_THRESHOLD ? BULL : color.new(BULL, 28)) : rbecRsi <= RBEC_BEAR_THRESHOLD ? BEAR : color.new(BEAR, 28)
float residual = rbecSource - rbecCenter
float rawDeviation = ta.stdev(residual, DEVIATION_LENGTH)
float rbecDeviation = ta.ema(rawDeviation, WIDTH_SMOOTHING)
float channelWidth = not na(rbecDeviation) ? math.max(rbecDeviation, syminfo.mintick) : na
float innerUpper = rbecCenter + channelWidth * INNER_MULTIPLIER
float innerLower = rbecCenter - channelWidth * INNER_MULTIPLIER
float outerUpper = rbecCenter + channelWidth * OUTER_MULTIPLIER
float outerLower = rbecCenter - channelWidth * OUTER_MULTIPLIER
plot(showRbec and showRbecCenter ? rbecCenter : na, "RBEC Centerline", color=rbecColor, linewidth=rbecLineWidth, style=rbecCenterStyle == "Circles" ? plot.style_circles : plot.style_line)
pInnerUpper = plot(showRbec and showRbecBands ? innerUpper : na, "RBEC Inner Upper", color=color.new(INNER, rbecInnerLineTransparency))
pInnerLower = plot(showRbec and showRbecBands ? innerLower : na, "RBEC Inner Lower", color=color.new(INNER, rbecInnerLineTransparency))
pOuterUpper = plot(showRbec and showRbecBands ? outerUpper : na, "RBEC Outer Upper", color=color.new(OUTER, rbecOuterLineTransparency))
pOuterLower = plot(showRbec and showRbecBands ? outerLower : na, "RBEC Outer Lower", color=color.new(OUTER, rbecOuterLineTransparency))
fill(pInnerUpper, pInnerLower, showRbec and showRbecBands ? color.new(INNER, rbecChannelTransparency) : na, title="RBEC Normal Channel")
fill(pOuterUpper, pInnerUpper, showRbec and showRbecBands ? color.new(OUTER, rbecExtensionTransparency) : na, title="RBEC Upper Extension")
fill(pInnerLower, pOuterLower, showRbec and showRbecBands ? color.new(OUTER, rbecExtensionTransparency) : na, title="RBEC Lower Extension")
string DASH = "02 — Dashboard"
bool dashShow = input.bool(true, "Show Dashboard", group=DASH)
string dashLocation = input.string("Top Right", "Location", options=["Top Left","Top Center","Top Right","Middle Left","Middle Center","Middle Right","Bottom Left","Bottom Center","Bottom Right"], group=DASH)
string dashSize = input.string("Small", "Text Size", options=["Tiny","Small","Normal","Large","Huge"], group=DASH)
int dashAtrLength = input.int(14, "Slope ATR Length", minval=2, maxval=500, group=DASH)
float dashSlopeFlat = input.float(0.005, "Flat Slope Zone — ATR per Bar", minval=0, step=0.005, group=DASH, tooltip="Dashboard-only slope classification. Does not change the RBEC regime or channel.")
int dashBgTransparency = input.int(10, "Background Transparency", minval=0, maxval=100, group=DASH)
f_dashPosition(string value) =>
switch value
"Top Left" => position.top_left
"Top Center" => position.top_center
"Middle Left" => position.middle_left
"Middle Center" => position.middle_center
"Middle Right" => position.middle_right
"Bottom Left" => position.bottom_left
"Bottom Center" => position.bottom_center
"Bottom Right" => position.bottom_right
=> position.top_right
f_dashSize(string value) =>
switch value
"Tiny" => size.tiny
"Normal" => size.normal
"Large" => size.large
"Huge" => size.huge
=> size.small
f_signed(float value, string pattern) =>
na(value) ? "—" : (value>0 ? "+" : "")+str.tostring(value,pattern)
float dashAtr = math.max(ta.atr(dashAtrLength),syminfo.mintick)
float dashSlope = rbecSlope/dashAtr
// +/-100 corresponds to the actual selected inner bands, even if multiplier changes.
float dashChannelPct = 100.0*(close-rbecCenter)/(channelWidth*INNER_MULTIPLIER)
bool dashReady = not na(rbecCenter) and not na(channelWidth) and not na(rbecRsiState)
bool dashTransition = bullCounterpressure or bearCounterpressure
string dashState = not dashReady ? "WARMING UP" : rbecRegime==0 ? "UNCONFIRMED" : dashTransition ? (rbecRegime==1 ? "TRANSITION · BULL" : "TRANSITION · BEAR") : rbecRegime==1 ? "BULL" : "BEAR"
color dashStateColor = not dashReady or rbecRegime==0 ? color.gray : dashTransition ? COUNTER : rbecRegime==1 ? BULL : BEAR
color dashRsiColor = rbecRsiState>=RBEC_BULL_THRESHOLD ? BULL : rbecRsiState<=RBEC_BEAR_THRESHOLD ? BEAR : COUNTER
color dashSlopeColor = na(dashSlope) ? color.gray : dashSlope>dashSlopeFlat ? BULL : dashSlope< -dashSlopeFlat ? BEAR : COUNTER
string dashSlopeArrow = na(dashSlope) ? "—" : dashSlope>dashSlopeFlat ? "↑" : dashSlope< -dashSlopeFlat ? "↓" : "→"
color dashPositionColor = na(dashChannelPct) ? color.gray : dashChannelPct>0 ? BULL : dashChannelPct<0 ? BEAR : COUNTER
var table dashboard = table.new(f_dashPosition(dashLocation),2,4)
color dashBg = color.new(color.rgb(18,22,31),dashBgTransparency)
if barstate.islast
table.clear(dashboard,0,0,1,3)
if dashShow
table.cell(dashboard,0,0,"RBEC",text_color=color.white,bgcolor=dashBg,text_size=f_dashSize(dashSize),text_halign=text.align_left)
table.cell(dashboard,1,0,dashState,text_color=dashStateColor,bgcolor=dashBg,text_size=f_dashSize(dashSize),tooltip="Established regime, with transition when counterpressure appears. Initial regime is seeded by smoothed RSI at 50; later flips require the selected confirmation bars.")
table.cell(dashboard,0,1,"RSI · Smoothed",text_color=color.white,bgcolor=dashBg,text_size=f_dashSize(dashSize),text_halign=text.align_left)
table.cell(dashboard,1,1,na(rbecRsiState) ? "—" : str.tostring(rbecRsiState,"0.0"),text_color=dashRsiColor,bgcolor=dashBg,text_size=f_dashSize(dashSize),tooltip="EMA-smoothed RSI used in regime confirmation. Default thresholds: 55 bullish, 45 bearish. Raw RSI also contributes to centerline color/counterpressure.")
table.cell(dashboard,0,2,"Slope · ATR/bar",text_color=color.white,bgcolor=dashBg,text_size=f_dashSize(dashSize),text_halign=text.align_left)
table.cell(dashboard,1,2,f_signed(dashSlope,"0.000")+" "+dashSlopeArrow,text_color=dashSlopeColor,bgcolor=dashBg,text_size=f_dashSize(dashSize),tooltip="One-bar BWMA change divided by ATR. Arrow indicates rising, falling, or flat slope, not slope acceleration.")
table.cell(dashboard,0,3,"Channel Position",text_color=color.white,bgcolor=dashBg,text_size=f_dashSize(dashSize),text_halign=text.align_left)
table.cell(dashboard,1,3,f_signed(dashChannelPct,"0.0")+(na(dashChannelPct) ? "" : "%"),text_color=dashPositionColor,bgcolor=dashBg,text_size=f_dashSize(dashSize),tooltip="Chart close relative to center and inner bands: 0% center, +100% upper inner, -100% lower inner. Default outer bands are +/-200%. Not bounded; not a probability.")
// Closed-bar alerts for state changes. No backdated signals.
int rbecDisplayState = not dashReady or rbecRegime==0 ? 0 : dashTransition ? 2 : rbecRegime
alertcondition(barstate.isconfirmed and rbecRegime==1 and rbecRegime[1]!=1, "RBEC Bull Regime", "OS RBEC: bull regime on {{ticker}} {{interval}}")
alertcondition(barstate.isconfirmed and rbecRegime==-1 and rbecRegime[1]!=-1, "RBEC Bear Regime", "OS RBEC: bear regime on {{ticker}} {{interval}}")
alertcondition(barstate.isconfirmed and rbecDisplayState==2 and rbecDisplayState[1]!=2, "RBEC Transition", "OS RBEC: counterpressure / transition on {{ticker}} {{interval}}")
alertcondition(barstate.isconfirmed and rbecDisplayState!=rbecDisplayState[1], "RBEC State Change", "OS RBEC: display state changed on {{ticker}} {{interval}}")
plot(rbecRsi,"RBEC Raw RSI",display=display.data_window)
plot(rbecRsiState,"RBEC Smoothed RSI",display=display.data_window)
plot(dashSlope,"RBEC Slope ATR/bar",display=display.data_window)
plot(dashChannelPct,"RBEC Channel Position %",display=display.data_window)
plot(rbecRegime,"RBEC Regime (1 Bull / -1 Bear)",display=display.data_window)
plot(rbecDisplayState,"RBEC State (0 Unconfirmed / 1 Bull / -1 Bear / 2 Transition)",display=display.data_window)