本页源码来自 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/
// © Soulshifted
//
// ══════════════════════════════════════════════════════════════════════════════
// Riverstone Ribbon (RS-Ribbon) · EMA ribbon with cascade cross signals
// ══════════════════════════════════════════════════════════════════════════════
//
// WHAT IT DOES
// Draws an eight-EMA ribbon (8/12/21/50/72/100/150/200) and marks the
// cascade cross: a pullback turns much of the ribbon upside down, then the
// 8 EMA crosses back above the 12. Meant to mark the end of a deep pullback
// inside a longer uptrend, not every 8/12 cross. 200 SMA/EMA areas show the
// long-term trend the signal is filtered against. Built and tested on SPY
// daily (1994-2026).
//
// RIBBON
// · Eight EMAs of the close. Only the 8 and 12 lines draw by default; the
// others are hidden but still drive the shading (tick them in Style)
// · Each band between neighboring EMAs is shaded while inverted (faster
// below slower); turn off "Shade Only Inversions" to shade upright bands
//
// CASCADE CROSS
// · While the 8 EMA is below the 12, counts the inverted neighboring pairs
// (8/12, 12/21, 21/50, 50/72, 72/100, 100/150, 150/200)
// · Reaching the threshold (default 4) at any point arms the dip
// · The next 8-over-12 cross is the cascade cross; every 8-over-12 cross
// resets the count, so each dip gives at most one signal
// · QUALIFIED (purple "RIBBON" label): close above the chart's 200 SMA.
// Optional: 50 SMA higher than 20 bars ago (off by default)
// · Crosses that don't qualify: small triangles; hover says which rule failed
// SPY daily 1994-2026, entry at the close of qualified crosses, exits at a
// 3 x ATR(14) close stop (2-6%), a 1.25x intraday hard stop, RSI(14) 75 or
// 60 bars, no costs: 26 trades, 69% wins, profit factor 3.1, avg +3.2%.
// SPX 1983-2019: 68% / PF 2.8. QQQ/IWM/XLK: 52% / PF 1.5. SPX before 1983:
// 37% wins. Small sample; historical observations, not predictions.
//
// 200 AREAS (30% opacity)
// · Daily 200 SMA (blue) and EMA (purple), from the daily chart
// · Chart 200 SMA and EMA: red and orange below daily; daily colors on
// weekly and higher; hidden on the daily chart (same as the daily 200s)
//
// NOTES
// · Crosses are evaluated live and can appear or disappear before the bar
// closes; only closed-bar labels are final. Alerts: "Ribbon Cascade
// Cross (qualified)", set to Once Per Bar Close.
// · On intraday charts the daily 200s step once per day on history and
// update live during the session.
// · Pine Logs (off by default) write each cross and its close-to-close
// change 5/10/20/40 bars later.
// · For chart analysis and education only; not financial advice.
//
//@version=6
indicator("Riverstone Ribbon", shorttitle = "RS-Ribbon", overlay=true, max_labels_count = 500)
// --- Inputs: Visibility & Colors ---
group_ema = "EMA Line Settings"
c8 = input.color(#ffffff, "EMA 8 Color", group=group_ema)
c12 = input.color(#000000, "EMA 12 Color", group=group_ema)
c21 = input.color(#ff9800, "EMA 21 Color", group=group_ema)
c50 = input.color(#fbc02d, "EMA 50 Color", group=group_ema)
c72 = input.color(#00a826, "EMA 72 Color", group=group_ema)
c100 = input.color(#008697, "EMA 100 Color", group=group_ema)
c150 = input.color(#2962ff, "EMA 150 Color", group=group_ema)
c200 = input.color(#673ab7, "EMA 200 Color", group=group_ema)
group_show = "Show/Hide Lines"
show_ema8 = input.bool(true, "Show 8", group=group_show, inline="8")
show_ema12 = input.bool(true, "Show 12", group=group_show, inline="12")
show_ema21 = input.bool(true, "Show 21", group=group_show, inline="21")
show_ema50 = input.bool(true, "Show 50", group=group_show, inline="50")
show_ema72 = input.bool(true, "Show 72", group=group_show, inline="72")
show_ema100 = input.bool(true, "Show 100", group=group_show, inline="100")
show_ema150 = input.bool(true, "Show 150", group=group_show, inline="150")
show_ema200 = input.bool(true, "Show 200", group=group_show, inline="200")
group_fill = "Shading Settings"
show_fill = input.bool(true, "Enable All Shading", group=group_fill)
inv_only = input.bool(true, "Shade Only Inversions", group=group_fill,
tooltip = "Shades a band only while it is inverted (the faster EMA below the slower one), so the cascade stands out. Turn off to also shade the upright bands.")
transp = 0
group_cc = "Cascade Cross Signal"
useCC = input.bool(true, "Mark Cascade Crosses", group=group_cc,
tooltip = "A cascade cross: while the 8 EMA is below the 12 EMA, the cascade inverts (at least this many neighboring EMA pairs upside down: faster below slower); then the 8 crosses back above the 12. Marked QUALIFIED when the qualifying rules below pass.\n\nHistorical test on SPY daily, 1994-2026, default rules: 26 trades, 69% wins, profit factor 3.1, average +3.2%. Weaker on QQQ/IWM/XLK (52% wins) and on SPX before 1983 (37% wins). The test method and exit rules are in the script description. Past results, not predictions.")
ccPairs = input.int(4, "Cascade: Inverted Pairs At Least", minval=1, maxval=7, group=group_cc,
tooltip = "How many of the 7 neighboring EMA pairs (8/12, 12/21, 21/50, 50/72, 72/100, 100/150, 150/200) must be inverted before a cross counts. 4 tested best; 5 or 6 gave fewer trades and no better results.")
ccNeed200 = input.bool(true, "Qualify: Close Above 200 SMA", group=group_cc)
ccNeed50 = input.bool(false, "Qualify: 50 SMA Rising Over 20 Bars", group=group_cc,
tooltip = "Off by default. A real 4+ pair cascade almost always turns the 50 SMA down, so this rule leaves very few signals (2 on SPY since 1994).")
ccCol = input.color(#b88fff, "Qualified Label Color", group=group_cc)
ccShowNo = input.bool(true, "Also Mark Crosses That Don't Qualify", group=group_cc, inline="nq")
ccNoCol = input.color(#000000, "", group=group_cc, inline="nq")
ccLog = input.bool(false, "Write Pine Logs (signals and 5/10/20/40-bar results)", group=group_cc)
group_200 = "200 Moving Averages (Areas)"
showD200S = input.bool(true, "Daily 200 SMA", group=group_200, inline="d200s")
colD200S = input.color(color.new(#2962ff, 70), "", group=group_200, inline="d200s")
showD200E = input.bool(true, "Daily 200 EMA", group=group_200, inline="d200e")
colD200E = input.color(color.new(#673ab7, 70), "", group=group_200, inline="d200e")
showC200S = input.bool(true, "Chart 200 SMA", group=group_200, inline="c200s")
colC200S = input.color(color.new(#f23645, 70), "", group=group_200, inline="c200s",
tooltip = "This color is used on charts below the daily. On weekly and higher charts the chart 200s use the Daily colors. On the daily chart itself the chart 200s are hidden, since they are the same lines as the Daily 200s.")
showC200E = input.bool(true, "Chart 200 EMA", group=group_200, inline="c200e")
colC200E = input.color(color.new(#ff9800, 70), "", group=group_200, inline="c200e")
// --- Calculation of EMAs ---
ema8 = ta.ema(close, 8)
ema12 = ta.ema(close, 12)
ema21 = ta.ema(close, 21)
ema50 = ta.ema(close, 50)
ema72 = ta.ema(close, 72)
ema100 = ta.ema(close, 100)
ema150 = ta.ema(close, 150)
ema200 = ta.ema(close, 200)
sma200 = ta.sma(close, 200)
// --- 200 Moving Average Areas (drawn first so the ribbon sits on top) ---
d200S = request.security(syminfo.tickerid, "D", ta.sma(close, 200))
d200E = request.security(syminfo.tickerid, "D", ta.ema(close, 200))
isIntraday = timeframe.in_seconds() < timeframe.in_seconds("D")
isDailyTF = timeframe.isdaily and timeframe.multiplier == 1
c200SCol = isIntraday ? colC200S : colD200S
c200ECol = isIntraday ? colC200E : colD200E
plot(showD200S ? d200S : na, color=colD200S, title="Daily 200 SMA", style=plot.style_area, linewidth=1, display=display.all - display.price_scale)
plot(showD200E ? d200E : na, color=colD200E, title="Daily 200 EMA", style=plot.style_area, linewidth=1, display=display.all - display.price_scale)
plot(showC200S and not isDailyTF ? sma200 : na, color=c200SCol, title="Chart 200 SMA", style=plot.style_area, linewidth=1, display=display.all - display.price_scale)
plot(showC200E and not isDailyTF ? ema200 : na, color=c200ECol, title="Chart 200 EMA", style=plot.style_area, linewidth=1, display=display.all - display.price_scale)
// --- Plotting Lines ---
// 8 and 12 lines show by default. 21-200 lines start hidden (unchecked in the Style tab) but still
// feed the shading; tick them in the Style tab to draw them.
p8 = plot(show_ema8 ? ema8 : na, color=c8, title="EMA 8", linewidth=3, display=display.all - display.price_scale)
p12 = plot(show_ema12 ? ema12 : na, color=c12, title="EMA 12", linewidth=3, display=display.all - display.price_scale)
p21 = plot(show_ema21 ? ema21 : na, color=c21, title="EMA 21", linewidth=3, display=display.none)
p50 = plot(show_ema50 ? ema50 : na, color=c50, title="EMA 50", linewidth=3, display=display.none)
p72 = plot(show_ema72 ? ema72 : na, color=c72, title="EMA 72", linewidth=3, display=display.none)
p100 = plot(show_ema100 ? ema100 : na, color=c100, title="EMA 100", linewidth=3, display=display.none)
p150 = plot(show_ema150 ? ema150 : na, color=c150, title="EMA 150", linewidth=3, display=display.none)
p200 = plot(show_ema200 ? ema200 : na, color=c200, title="EMA 200", linewidth=3, display=display.none)
// --- Individual Zone Fills ---
// 150-200
color_150_200 = ema150 > ema200 ? (inv_only ? na : color.new(#b88fff, transp)) : color.new(#673ab7, transp)
fill(p150, p200, show_fill ? color_150_200 : na, title="150-200 Fill")
// 100-150
color_100_150 = ema100 > ema150 ? (inv_only ? na : color.new(#7ea0ff, transp)) : color.new(#283593, transp)
fill(p100, p150, show_fill ? color_100_150 : na, title="100-150 Fill")
// 72-100
color_72_100 = ema72 > ema100 ? (inv_only ? na : color.new(#00eeff, transp)) : color.new(#006064, transp)
fill(p72, p100, show_fill ? color_72_100 : na, title="72-100 Fill")
// 50-72
color_50_72 = ema50 > ema72 ? (inv_only ? na : color.new(#afff9a, transp)) : color.new(#1b5e20, transp)
fill(p50, p72, show_fill ? color_50_72 : na, title="50-72 Fill")
// 21-50
color_21_50 = ema21 > ema50 ? (inv_only ? na : color.new(#fff9c4, transp)) : color.new(#fbc02d, transp)
fill(p21, p50, show_fill ? color_21_50 : na, title="21-50 Fill")
// 12-21
color_12_21 = ema12 > ema21 ? (inv_only ? na : color.new(#ffcc80, transp)) : color.new(#e65100, transp)
fill(p12, p21, show_fill ? color_12_21 : na, title="12-21 Fill")
// 8-12
color_8_12 = ema8 > ema12 ? (inv_only ? na : color.new(#faa1a4, transp)) : color.new(#92000d, transp)
fill(p8, p12, show_fill ? color_8_12 : na, title="8-12 Fill")
// --- Cascade Cross Signal ---
// Inverted pairs: how many neighboring EMAs are upside down (faster below slower)
invPairs = (ema8 < ema12 ? 1 : 0) + (ema12 < ema21 ? 1 : 0) + (ema21 < ema50 ? 1 : 0) + (ema50 < ema72 ? 1 : 0) +
(ema72 < ema100 ? 1 : 0) + (ema100 < ema150 ? 1 : 0) + (ema150 < ema200 ? 1 : 0)
sma50 = ta.sma(close, 50)
slope50 = (sma50 / sma50[20] - 1) * 100
crossUp = ta.crossover(ema8, ema12)
var bool ccArmed = false
var int ccMaxInv = 0
if invPairs >= ccPairs and ema8 < ema12 // the cascade must happen during this dip (8 below 12)
ccArmed := true
ccMaxInv := math.max(ccMaxInv, invPairs)
ccRaw = useCC and ccArmed and crossUp
ok200 = not ccNeed200 or close > sma200
ok50 = not ccNeed50 or slope50 > 0
ccQual = ccRaw and ok200 and ok50
int ccDepth = ccMaxInv
if crossUp // every 8-over-12 cross ends the dip, signal or not
ccArmed := false
ccMaxInv := 0
string ccWhy = (ok200 ? "" : "close below 200 SMA") + (not ok200 and not ok50 ? ", " : "") + (ok50 ? "" : "50 SMA falling (" + str.tostring(slope50, "#.##") + "% over 20 bars)")
string ccTip = "Cascade cross: 8 EMA back above 12 EMA after " + str.tostring(ccDepth) + " of 7 pairs inverted"
+ "\nClose " + str.tostring(close, "#.##") + " · 200 SMA " + str.tostring(sma200, "#.##") + " · 50 SMA 20-bar change " + str.tostring(slope50, "#.##") + "%"
if ccQual
label.new(bar_index, low, "RIBBON", yloc = yloc.belowbar, style = label.style_label_up, color = ccCol, textcolor = color.white, size = size.small,
tooltip = "QUALIFIED " + ccTip + "\nHistorical test (SPY daily 1994-2026, 26 trades): 69% wins, average +3.2%. Not a prediction.")
else if ccRaw and ccShowNo
label.new(bar_index, low, "", yloc = yloc.belowbar, style = label.style_triangleup, color = ccNoCol, size = size.tiny,
tooltip = "Not qualified (" + ccWhy + ")\n" + ccTip)
alertcondition(ccQual, "Ribbon Cascade Cross (qualified)", "RS-Ribbon: qualified cascade cross on {{ticker}} @ {{close}}")
// Logs: one line per cascade cross, then the close-to-close result 5, 10, 20 and 40 bars later
var array<int> ccBars = array.new_int()
var array<float> ccPx = array.new_float()
var array<bool> ccQ = array.new_bool()
if barstate.isconfirmed and ccLog
if ccRaw
log.info(syminfo.ticker + " RIBBON " + str.format_time(time, "yyyy-MM-dd", syminfo.timezone) + " | " + (ccQual ? "QUALIFIED" : "NOT QUALIFIED (" + ccWhy + ")")
+ " | Close: " + str.tostring(close, "#.##") + " | Pairs inverted (max): " + str.tostring(ccDepth) + "/7 | 50 SMA 20-bar: " + str.tostring(slope50, "#.##") + "% | vs 200 SMA: " + str.tostring((close / sma200 - 1) * 100, "#.##") + "%")
array.push(ccBars, bar_index)
array.push(ccPx, close)
array.push(ccQ, ccQual)
if array.size(ccBars) > 0
for j = array.size(ccBars) - 1 to 0
int age = bar_index - array.get(ccBars, j)
if age == 5 or age == 10 or age == 20 or age == 40
log.info(syminfo.ticker + " RIBBON RESULT " + str.format_time(time, "yyyy-MM-dd", syminfo.timezone) + " | " + (array.get(ccQ, j) ? "QUALIFIED" : "NOT QUALIFIED")
+ " | Signal " + str.tostring(age) + " bars ago @ " + str.tostring(array.get(ccPx, j), "#.##") + " | Change: " + str.tostring((close / array.get(ccPx, j) - 1) * 100, "#.##") + "%")
if age >= 40
array.remove(ccBars, j)
array.remove(ccPx, j)
array.remove(ccQ, j)