本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=5
indicator(title="EMA Crossover with Ribbon", shorttitle="EMA Crossover + Ribbon", overlay=true, format=format.price, precision=2)
// ————— Inputs for User Customization —————
// Fast EMA Settings
fastEmaLength = input.int(title="Fast EMA Length", defval=9, minval=1, group="EMA Crossover Settings")
fastEmaColor = input.color(title="Fast EMA Color", defval=color.blue, group="EMA Crossover Settings")
slowEmaLength = input.int(title="Slow EMA Length", defval=21, minval=1, group="EMA Crossover Settings")
slowEmaColor = input.color(title="Slow EMA Color", defval=color.red, group="EMA Crossover Settings")
// Crossover EMA Ribbon Fill
show_crossover_fill = input.bool(title="Show Crossover EMA Fill", defval=true, group="EMA Crossover Settings")
crossover_fill_opacity = input.int(title="Crossover Fill Opacity", defval=80, minval=0, maxval=100, group="EMA Crossover Settings")
bullish_fill_color = input.color(title="Bullish Fill Color", defval=color.green, group="EMA Crossover Settings")
bearish_fill_color = input.color(title="Bearish Fill Color", defval=color.red, group="EMA Crossover Settings")
// EMA Ribbon Settings
ema1_length = input.int(title="EMA 1 Length", defval=5, minval=1, group="EMA Ribbon Settings")
ema1_color = input.color(title="EMA 1 Color", defval=color.purple, group="EMA Ribbon Settings")
ema1_thickness = input.int(title="EMA 1 Thickness", defval=1, minval=1, maxval=5, group="EMA Ribbon Settings")
ema2_length = input.int(title="EMA 2 Length", defval=10, minval=1, group="EMA Ribbon Settings")
ema2_color = input.color(title="EMA 2 Color", defval=color.blue, group="EMA Ribbon Settings")
ema2_thickness = input.int(title="EMA 2 Thickness", defval=1, minval=1, maxval=5, group="EMA Ribbon Settings")
ema3_length = input.int(title="EMA 3 Length", defval=20, minval=1, group="EMA Ribbon Settings")
ema3_color = input.color(title="EMA 3 Color", defval=color.teal, group="EMA Ribbon Settings")
ema3_thickness = input.int(title="EMA 3 Thickness", defval=1, minval=1, maxval=5, group="EMA Ribbon Settings")
ema4_length = input.int(title="EMA 4 Length", defval=30, minval=1, group="EMA Ribbon Settings")
ema4_color = input.color(title="EMA 4 Color", defval=color.orange, group="EMA Ribbon Settings")
ema4_thickness = input.int(title="EMA 4 Thickness", defval=1, minval=1, maxval=5, group="EMA Ribbon Settings")
ema5_length = input.int(title="EMA 5 Length", defval=50, minval=1, group="EMA Ribbon Settings")
ema5_color = input.color(title="EMA 5 Color", defval=color.red, group="EMA Ribbon Settings")
ema5_thickness = input.int(title="EMA 5 Thickness", defval=1, minval=1, maxval=5, group="EMA Ribbon Settings")
show_ema_ribbon = input.bool(title="Show EMA Ribbon", defval=true, group="EMA Ribbon Settings")
ribbon_transparency = input.int(title="Ribbon Transparency", defval=80, minval=0, maxval=100, group="EMA Ribbon Settings")
// Visual Settings
showCandleColors = input.bool(title="Show Trend-Colored Candles", defval=true, group="Visual Settings")
showDirectionArrows = input.bool(title="Show Direction Arrows on Every Candle", defval=true, group="Visual Settings")
showBackgroundTrend = input.bool(title="Show Background Trend Color", defval=true, group="Visual Settings")
background_trend_opacity = input.int(title="Background Trend Opacity", defval=95, minval=0, maxval=100, group="Visual Settings")
// Signal Colors
buySignalColor = input.color(title="Buy Signal Color", defval=color.green, group="Signal Colors")
sellSignalColor = input.color(title="Sell Signal Color", defval=color.red, group="Signal Colors")
bullishArrowColor = input.color(title="Bullish Arrow Color", defval=color.green, group="Signal Colors")
bearishArrowColor = input.color(title="Bearish Arrow Color", defval=color.red, group="Signal Colors")
// ————— Calculate Crossover EMAs —————
fastEma = ta.ema(close, fastEmaLength)
slowEma = ta.ema(close, slowEmaLength)
// ————— Calculate Ribbon EMAs —————
ema1 = ta.ema(close, ema1_length)
ema2 = ta.ema(close, ema2_length)
ema3 = ta.ema(close, ema3_length)
ema4 = ta.ema(close, ema4_length)
ema5 = ta.ema(close, ema5_length)
// ————— Determine Trend Direction for Candle Color —————
isBullish = fastEma > slowEma
isBearish = fastEma < slowEma
barColor = showCandleColors ? (isBullish ? color.green : color.red) : na
// ————— Plot Trend-Colored Candles —————
plotcandle(open, high, low, close, color=barColor)
// ————— Plot Crossover EMAs on Chart —————
p_fast = plot(series=fastEma, color=fastEmaColor, linewidth=2, title="Fast EMA")
p_slow = plot(series=slowEma, color=slowEmaColor, linewidth=2, title="Slow EMA")
// ————— Plot Crossover EMA Ribbon Fill —————
crossover_fill_color = isBullish ? color.new(bullish_fill_color, crossover_fill_opacity) : color.new(bearish_fill_color, crossover_fill_opacity)
fill(p_fast, p_slow, color=show_crossover_fill ? crossover_fill_color : na, title="Crossover EMA Fill")
// ————— Plot EMA Ribbon —————
// Use the 'display' parameter to conditionally show/hide the plots instead of using local scope
display_setting = show_ema_ribbon ? display.all : display.none
// Plot each EMA line for the ribbon with customizable thickness
p1 = plot(ema1, "EMA 1", color=ema1_color, linewidth=ema1_thickness, display=display_setting)
p2 = plot(ema2, "EMA 2", color=ema2_color, linewidth=ema2_thickness, display=display_setting)
p3 = plot(ema3, "EMA 3", color=ema3_color, linewidth=ema3_thickness, display=display_setting)
p4 = plot(ema4, "EMA 4", color=ema4_color, linewidth=ema4_thickness, display=display_setting)
p5 = plot(ema5, "EMA 5", color=ema5_color, linewidth=ema5_thickness, display=display_setting)
// Create ribbon fill between EMAs (only when ribbon is shown)
fill(p1, p2, color=show_ema_ribbon ? color.new(color.purple, ribbon_transparency) : na, title="Ribbon 1-2")
fill(p2, p3, color=show_ema_ribbon ? color.new(color.blue, ribbon_transparency) : na, title="Ribbon 2-3")
fill(p3, p4, color=show_ema_ribbon ? color.new(color.teal, ribbon_transparency) : na, title="Ribbon 3-4")
fill(p4, p5, color=show_ema_ribbon ? color.new(color.orange, ribbon_transparency) : na, title="Ribbon 4-5")
// ————— Define Crossover Conditions —————
buySignal = ta.crossover(fastEma, slowEma)
sellSignal = ta.crossunder(fastEma, slowEma)
// ————— Identify FIRST candle of a new trend —————
isFirstBullishCandle = buySignal
isFirstBearishCandle = sellSignal
// ————— Plot Buy and Sell Signals with different sizes —————
// First signal
plotshape(series=isFirstBullishCandle, title="First Buy Signal", color=buySignalColor,
style=shape.triangleup, location=location.belowbar, size=size.small)
plotshape(series=isFirstBearishCandle, title="First Sell Signal", color=sellSignalColor,
style=shape.triangledown, location=location.abovebar, size=size.small)
// Follow-up signals
plotshape(series=isBullish and not isFirstBullishCandle, title="Follow Buy Signal", color=buySignalColor,
style=shape.triangleup, location=location.belowbar, size=size.tiny)
plotshape(series=isBearish and not isFirstBearishCandle, title="Follow Sell Signal", color=sellSignalColor,
style=shape.triangledown, location=location.abovebar, size=size.tiny)
// ————— Plot Direction Arrow on EVERY Candle —————
bullishSeries = showDirectionArrows and isBullish
bearishSeries = showDirectionArrows and isBearish
plotshape(series=bullishSeries, title="Bullish Arrow", color=color.new(bullishArrowColor, 30),
style=shape.arrowup, location=location.belowbar, size=size.small)
plotshape(series=bearishSeries, title="Bearish Arrow", color=color.new(bearishArrowColor, 30),
style=shape.arrowdown, location=location.abovebar, size=size.small)
// ————— Background Color for Trend —————
bgcolor(color=showBackgroundTrend ? (isBullish ? color.new(color.green, 100 - background_trend_opacity) : color.new(color.red, 100 - background_trend_opacity)) : na)