本页源码来自 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
// https://mozilla.org/MPL/2.0/
// © JesseV365
//
// OS RSI Envelope
// A price-chart envelope derived from the price changes required for RSI to
// reach configurable upper and lower thresholds. The dashboard reports RSI,
// price location within the envelope, envelope width, and centerline slope.
// Metrics describe current conditions; they are not trade recommendations or
// probabilities.
//
// RSI price-envelope calculation carried forward from OS Flow Study /
// OS Market Compass.
//@version=6
indicator("OS RSI Envelope", shorttitle="OS RSI Env", overlay=true, max_bars_back=5000)
// ── Envelope ────────────────────────────────────────────────────────────────
string RSI_GROUP = "01 — RSI Envelope"
bool showEnvelope = input.bool(true, "Show RSI Envelope", group=RSI_GROUP)
int rsiLength = input.int(14, "RSI Length", minval=2, group=RSI_GROUP)
int upperThreshold = input.int(70, "Upper RSI Threshold", minval=51, maxval=99, group=RSI_GROUP)
int lowerThreshold = input.int(30, "Lower RSI Threshold", minval=1, maxval=49, group=RSI_GROUP)
int bandSmoothing = input.int(3, "Band Smoothing", minval=1, maxval=20, group=RSI_GROUP,
tooltip="EMA smoothing applied to the price boundaries. Set to 1 for no additional smoothing.")
// ── Appearance ──────────────────────────────────────────────────────────────
string VISUAL_GROUP = "02 — Appearance"
color upperColor = input.color(color.rgb(232, 35, 255), "Upper / Resistance", group=VISUAL_GROUP)
color lowerColor = input.color(color.rgb(0, 210, 255), "Lower / Support", group=VISUAL_GROUP)
color centerColor = input.color(color.rgb(170, 176, 190), "Center", group=VISUAL_GROUP)
int bandWidth = input.int(2, "Band Width", minval=1, maxval=5, group=VISUAL_GROUP)
int fillTransparency = input.int(91, "Envelope Fill Transparency", minval=0, maxval=100, group=VISUAL_GROUP)
bool showCenter = input.bool(true, "Show Center", group=VISUAL_GROUP)
// ── Dashboard ───────────────────────────────────────────────────────────────
string DASH_GROUP = "03 — Dashboard"
bool showDashboard = input.bool(true, "Show Dashboard", group=DASH_GROUP)
string dashboardLocation = 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_GROUP)
string dashboardSize = input.string("Small", "Text Size", options=["Tiny", "Small", "Normal", "Large", "Huge"], group=DASH_GROUP)
int slopeAtrLength = input.int(14, "Slope ATR Length", minval=2, maxval=500, group=DASH_GROUP)
float flatSlopeGate = input.float(0.005, "Flat Slope Zone — ATR per Bar", minval=0.0, step=0.005, group=DASH_GROUP,
tooltip="Center slope inside this ATR-per-bar range is shown as flat. This only affects the dashboard.")
int dashboardTransparency = input.int(10, "Background Transparency", minval=0, maxval=100, group=DASH_GROUP)
color risingColor = input.color(#64D168, "Rising / Upper RSI", group=DASH_GROUP)
color fallingColor = input.color(#FF0000, "Falling / Lower RSI", group=DASH_GROUP)
color flatColor = input.color(#FFD54F, "Flat / Midpoint", group=DASH_GROUP)
f_dashboardPosition(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_dashboardSize(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)
// Implied price boundaries where RSI would reach the selected thresholds.
f_rsiPriceBands(float source, int length, int upperRsi, int lowerRsi) =>
int emaPeriod = 2 * length - 1
float upMove = math.max(source - source[1], 0.0)
float downMove = math.max(source[1] - source, 0.0)
float avgUp = ta.ema(upMove, emaPeriod)
float avgDown = ta.ema(downMove, emaPeriod)
float upperOffset = (length - 1) * (avgDown * upperRsi / (100.0 - upperRsi) - avgUp)
float rawUpper = upperOffset >= 0.0 ? source + upperOffset : source + upperOffset * (100.0 - upperRsi) / upperRsi
float lowerOffset = (length - 1) * (avgDown * lowerRsi / (100.0 - lowerRsi) - avgUp)
float rawLower = lowerOffset >= 0.0 ? source + lowerOffset : source + lowerOffset * (100.0 - lowerRsi) / lowerRsi
[rawUpper, rawLower]
[rawUpper, rawLower] = f_rsiPriceBands(close, rsiLength, upperThreshold, lowerThreshold)
float upperBand = bandSmoothing == 1 ? rawUpper : ta.ema(rawUpper, bandSmoothing)
float lowerBand = bandSmoothing == 1 ? rawLower : ta.ema(rawLower, bandSmoothing)
float centerLine = math.avg(upperBand, lowerBand)
float bandRange = upperBand - lowerBand
float envelopePosition = 100.0 * (close - lowerBand) / math.max(bandRange, syminfo.mintick)
float rsiValue = ta.rsi(close, rsiLength)
color envelopeFill = color.from_gradient(
math.max(0.0, math.min(100.0, envelopePosition)), 0.0, 100.0,
color.new(lowerColor, fillTransparency), color.new(upperColor, fillTransparency))
pUpper = plot(showEnvelope ? upperBand : na, "RSI Upper Price Boundary", color=upperColor, linewidth=bandWidth)
pLower = plot(showEnvelope ? lowerBand : na, "RSI Lower Price Boundary", color=lowerColor, linewidth=bandWidth)
plot(showEnvelope and showCenter ? centerLine : na, "RSI Envelope Center", color=color.new(centerColor, 25), linewidth=1)
fill(pUpper, pLower, color=showEnvelope ? envelopeFill : na, title="RSI Envelope Fill")
// ── Dashboard metrics ────────────────────────────────────────────────────────
float atr = math.max(ta.atr(slopeAtrLength), syminfo.mintick)
float centerSlope = (centerLine - centerLine[1]) / atr
bool envelopeReady = not na(bandRange) and bandRange > 0.0
string positionState = not envelopeReady ? "Warming up" : envelopePosition < 0.0 ? "BELOW" : envelopePosition > 100.0 ? "ABOVE" : envelopePosition < 50.0 ? "LOWER HALF" : envelopePosition > 50.0 ? "UPPER HALF" : "MIDPOINT"
string positionText = not envelopeReady ? "Warming up" : str.tostring(envelopePosition, "0.0") + "% · " + positionState
color slopeColor = na(centerSlope) ? color.gray : centerSlope > flatSlopeGate ? risingColor : centerSlope < -flatSlopeGate ? fallingColor : flatColor
string slopeArrow = na(centerSlope) ? "—" : centerSlope > flatSlopeGate ? "↑" : centerSlope < -flatSlopeGate ? "↓" : "→"
color rsiColor = na(rsiValue) ? color.gray : rsiValue >= upperThreshold ? upperColor : rsiValue <= lowerThreshold ? lowerColor : centerColor
color positionColor = not envelopeReady ? color.gray : envelopePosition > 100.0 ? upperColor : envelopePosition < 0.0 ? lowerColor : envelopePosition > 50.0 ? upperColor : envelopePosition < 50.0 ? lowerColor : flatColor
string widthArrow = not envelopeReady or na(bandRange[1]) ? "" : bandRange > bandRange[1] ? " ↑" : bandRange < bandRange[1] ? " ↓" : " →"
var table dashboard = table.new(f_dashboardPosition(dashboardLocation), 2, 4,
frame_color=color.new(color.gray, 65), frame_width=1, border_color=color.new(color.gray, 80), border_width=1)
color dashboardBg = color.new(color.rgb(18, 22, 31), dashboardTransparency)
if barstate.islast
table.clear(dashboard, 0, 0, 1, 3)
if showDashboard
table.cell(dashboard, 0, 0, "RSI · " + str.tostring(rsiLength), text_color=color.white, bgcolor=dashboardBg, text_size=f_dashboardSize(dashboardSize), text_halign=text.align_left)
table.cell(dashboard, 1, 0, na(rsiValue) ? "—" : str.tostring(rsiValue, "0.0"), text_color=rsiColor, bgcolor=dashboardBg, text_size=f_dashboardSize(dashboardSize),
tooltip="Current chart-timeframe RSI. Thresholds are shown by the upper and lower price boundaries.")
table.cell(dashboard, 0, 1, "Envelope Position", text_color=color.white, bgcolor=dashboardBg, text_size=f_dashboardSize(dashboardSize), text_halign=text.align_left)
table.cell(dashboard, 1, 1, positionText, text_color=positionColor, bgcolor=dashboardBg, text_size=f_dashboardSize(dashboardSize),
tooltip="0% is the lower boundary, 50% the midpoint, and 100% the upper boundary. Below 0% or above 100% means price is outside the envelope. This is a location metric, not a probability.")
table.cell(dashboard, 0, 2, "Width · Points", text_color=color.white, bgcolor=dashboardBg, text_size=f_dashboardSize(dashboardSize), text_halign=text.align_left)
table.cell(dashboard, 1, 2, not envelopeReady ? "—" : str.tostring(bandRange, format.mintick) + widthArrow, text_color=centerColor, bgcolor=dashboardBg, text_size=f_dashboardSize(dashboardSize),
tooltip="Upper boundary minus lower boundary in price points. The arrow indicates widening, narrowing, or no change; it does not indicate direction.")
table.cell(dashboard, 0, 3, "Center Slope · ATR/bar", text_color=color.white, bgcolor=dashboardBg, text_size=f_dashboardSize(dashboardSize), text_halign=text.align_left)
table.cell(dashboard, 1, 3, f_signed(centerSlope, "0.000") + " " + slopeArrow, text_color=slopeColor, bgcolor=dashboardBg, text_size=f_dashboardSize(dashboardSize),
tooltip="One-bar centerline change divided by ATR. Up is rising, down is falling, and right is within the configured flat-slope zone. The flat zone only affects this dashboard display.")
// Boundary alerts report confirmed closes; they are not entry/exit instructions.
bool crossAboveUpper = ta.crossover(close, upperBand)
bool crossBelowLower = ta.crossunder(close, lowerBand)
bool returnFromAbove = ta.crossunder(close, upperBand) and close >= lowerBand
bool returnFromBelow = ta.crossover(close, lowerBand) and close <= upperBand
bool crossCenterUp = ta.crossover(close, centerLine)
bool crossCenterDown = ta.crossunder(close, centerLine)
alertcondition(barstate.isconfirmed and crossAboveUpper, "Above Upper Envelope", "OS RSI Envelope: close crossed above upper boundary on {{ticker}} {{interval}}")
alertcondition(barstate.isconfirmed and crossBelowLower, "Below Lower Envelope", "OS RSI Envelope: close crossed below lower boundary on {{ticker}} {{interval}}")
alertcondition(barstate.isconfirmed and (returnFromAbove or returnFromBelow), "Return Inside Envelope", "OS RSI Envelope: close crossed back inside envelope on {{ticker}} {{interval}}")
alertcondition(barstate.isconfirmed and crossCenterUp, "Above Midpoint", "OS RSI Envelope: close crossed above midpoint on {{ticker}} {{interval}}")
alertcondition(barstate.isconfirmed and crossCenterDown, "Below Midpoint", "OS RSI Envelope: close crossed below midpoint on {{ticker}} {{interval}}")
// Numeric diagnostics for TradingView's Data Window.
plot(rsiValue, "RSI Value", display=display.data_window)
plot(envelopePosition, "Envelope Position %", display=display.data_window)
plot(bandRange, "Envelope Width (Points)", display=display.data_window)
plot(centerSlope, "Envelope Center Slope (ATR/bar)", display=display.data_window)