Dynamic EMA Ribbon

Lodestar_Runner · study · 153 行 · 点赞 21 · TradingView 原页

本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。

Pine Script

//@version=6
indicator("Dynamic EMA Ribbon", overlay=true, max_bars_back=500)
 
// ══════════════════════════════════════════════════════════════════════════════
// DYNAMIC EMA RIBBON
// ══════════════════════════════════════════════════════════════════════════════
// 
// This indicator plots the 9 EMA and 21 EMA and fills the space between them.
// 
// Color Logic:
// • Green fill  → EMA 9 is above EMA 21 (bullish bias)
// • Red fill    → EMA 9 is below EMA 21 (bearish bias)
// 
// Dynamic Strength:
// The opacity of the fill automatically adjusts based on how far apart the two
// EMAs are (measured in ATRs).
// • When the EMAs are close together → fill is more transparent
// • When the EMAs are farther apart  → fill becomes stronger/more opaque
// 
// Additional Feature:
// • Optional Buy/Sell arrows on EMA 9/21 crossover
//   Available for: 1-min, 3-min, 5-min, 10-min, 15-min
//   (signals only appear when you are on that exact timeframe)
// ══════════════════════════════════════════════════════════════════════════════
 
// ─── Inputs ────────────────────────────────────────────────────────────────
fastLen         = input.int(9,  "Fast EMA (9)", minval=1)
slowLen         = input.int(21, "Slow EMA (21)", minval=1)
src             = input.source(close, "Source")
 
// Base colors
colBull         = input.color(color.green, "Bullish Color")
colBear         = input.color(color.red,   "Bearish Color")
 
// Line colors (transparent by default)
colFast         = input.color(color.new(color.teal, 100),   "Fast EMA Color")
colSlow         = input.color(color.new(color.orange, 100), "Slow EMA Color")
lineWidth       = input.int(2, "EMA Line Width", minval=1, maxval=5)
 
// Dynamic fill strength
atrLen          = input.int(14, "ATR Length (for distance)", minval=1)
fullStrengthATR = input.float(1.5, "ATRs for full strength", minval=0.1, step=0.1)
minTransp       = input.int(20, "Min Transparency (far apart)", minval=0, maxval=100)
maxTransp       = input.int(85, "Max Transparency (close together)", minval=0, maxval=100)
 
// Signal settings
signalSize      = input.string("Tiny", "Signal Size", options=["Tiny", "Small", "Normal", "Large"])
 
show1m  = input.bool(true,  "Show 1-minute Signals")
show3m  = input.bool(false, "Show 3-minute Signals")
show5m  = input.bool(true,  "Show 5-minute Signals")
show10m = input.bool(false, "Show 10-minute Signals")
show15m = input.bool(true,  "Show 15-minute Signals")
 
// ─── Current Timeframe Ribbon ──────────────────────────────────────────────
emaFast = ta.ema(src, fastLen)
emaSlow = ta.ema(src, slowLen)
 
dist     = math.abs(emaFast - emaSlow)
atr      = ta.atr(atrLen)
normDist = atr > 0 ? dist / atr : 0
strength = math.min(normDist / fullStrengthATR, 1.0)
transp   = maxTransp - strength * (maxTransp - minTransp)
fillColor = emaFast > emaSlow ? color.new(colBull, transp) : color.new(colBear, transp)
 
// ─── Helper: Get crossover from a specific timeframe ───────────────────────
f_cross(tf) =>
    request.security(syminfo.tickerid, tf,
         [ta.crossover(ta.ema(src, fastLen), ta.ema(src, slowLen)),
          ta.crossunder(ta.ema(src, fastLen), ta.ema(src, slowLen))],
         lookahead = barmerge.lookahead_off)
 
// ─── Get signals from each timeframe ───────────────────────────────────────
[bull1, bear1]   = f_cross("1")
[bull3, bear3]   = f_cross("3")
[bull5, bear5]   = f_cross("5")
[bull10, bear10] = f_cross("10")
[bull15, bear15] = f_cross("15")
 
// Only show signals when the chart timeframe matches
buy1  = show1m  and timeframe.period == "1"  and bull1
sell1 = show1m  and timeframe.period == "1"  and bear1
 
buy3  = show3m  and timeframe.period == "3"  and bull3
sell3 = show3m  and timeframe.period == "3"  and bear3
 
buy5  = show5m  and timeframe.period == "5"  and bull5
sell5 = show5m  and timeframe.period == "5"  and bear5
 
buy10  = show10m and timeframe.period == "10" and bull10
sell10 = show10m and timeframe.period == "10" and bear10
 
buy15  = show15m and timeframe.period == "15" and bull15
sell15 = show15m and timeframe.period == "15" and bear15
 
// Combine for easier plotting
buySignal  = buy1 or buy3 or buy5 or buy10 or buy15
sellSignal = sell1 or sell3 or sell5 or sell10 or sell15
 
// ─── Plots ─────────────────────────────────────────────────────────────────
pFast = plot(emaFast, "EMA 9",  color=colFast, linewidth=lineWidth)
pSlow = plot(emaSlow, "EMA 21", color=colSlow, linewidth=lineWidth)
fill(pFast, pSlow, color=fillColor, title="Dynamic EMA Fill")
 
// Buy arrows
plotshape(buySignal and signalSize == "Tiny",   title="Buy Tiny",
     style=shape.triangleup, location=location.belowbar,
     color=color.new(color.lime, 0), size=size.tiny, text="Buy", textcolor=color.lime)
 
plotshape(buySignal and signalSize == "Small",  title="Buy Small",
     style=shape.triangleup, location=location.belowbar,
     color=color.new(color.lime, 0), size=size.small, text="Buy", textcolor=color.lime)
 
plotshape(buySignal and signalSize == "Normal", title="Buy Normal",
     style=shape.triangleup, location=location.belowbar,
     color=color.new(color.lime, 0), size=size.normal, text="Buy", textcolor=color.lime)
 
plotshape(buySignal and signalSize == "Large",  title="Buy Large",
     style=shape.triangleup, location=location.belowbar,
     color=color.new(color.lime, 0), size=size.large, text="Buy", textcolor=color.lime)
 
// Sell arrows
plotshape(sellSignal and signalSize == "Tiny",   title="Sell Tiny",
     style=shape.triangledown, location=location.abovebar,
     color=color.new(color.red, 0), size=size.tiny, text="Sell", textcolor=color.red)
 
plotshape(sellSignal and signalSize == "Small",  title="Sell Small",
     style=shape.triangledown, location=location.abovebar,
     color=color.new(color.red, 0), size=size.small, text="Sell", textcolor=color.red)
 
plotshape(sellSignal and signalSize == "Normal", title="Sell Normal",
     style=shape.triangledown, location=location.abovebar,
     color=color.new(color.red, 0), size=size.normal, text="Sell", textcolor=color.red)
 
plotshape(sellSignal and signalSize == "Large",  title="Sell Large",
     style=shape.triangledown, location=location.abovebar,
     color=color.new(color.red, 0), size=size.large, text="Sell", textcolor=color.red)
 
// ─── Alerts ────────────────────────────────────────────────────────────────
alertcondition(buy1,  "1m Buy",  "1-minute EMA 9 crossed above EMA 21")
alertcondition(sell1, "1m Sell", "1-minute EMA 9 crossed below EMA 21")
 
alertcondition(buy3,  "3m Buy",  "3-minute EMA 9 crossed above EMA 21")
alertcondition(sell3, "3m Sell", "3-minute EMA 9 crossed below EMA 21")
 
alertcondition(buy5,  "5m Buy",  "5-minute EMA 9 crossed above EMA 21")
alertcondition(sell5, "5m Sell", "5-minute EMA 9 crossed below EMA 21")
 
alertcondition(buy10,  "10m Buy",  "10-minute EMA 9 crossed above EMA 21")
alertcondition(sell10, "10m Sell", "10-minute EMA 9 crossed below EMA 21")
 
alertcondition(buy15,  "15m Buy",  "15-minute EMA 9 crossed above EMA 21")
alertcondition(sell15, "15m Sell", "15-minute EMA 9 crossed below EMA 21")

← 返回列表