本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=6
indicator(title="EMA Projection Ribbon", shorttitle="EMA Proj Ribbon", overlay=true, max_lines_count=50)
src = input(close, title="Source")
// ===== EMA lengths & colors =====
GRP_L = "EMAs"
len1 = input.int(9, "EMA 1", minval = 1, inline = "1", group = GRP_L)
col1 = input.color(#00E5FF, "", inline = "1", group = GRP_L)
prj1 = input.bool(true, "Project", inline = "1", group = GRP_L)
len2 = input.int(21, "EMA 2", minval = 1, inline = "2", group = GRP_L)
col2 = input.color(#2196F3, "", inline = "2", group = GRP_L)
prj2 = input.bool(true, "Project", inline = "2", group = GRP_L)
len3 = input.int(50, "EMA 3", minval = 1, inline = "3", group = GRP_L)
col3 = input.color(#FFEB3B, "", inline = "3", group = GRP_L)
prj3 = input.bool(true, "Project", inline = "3", group = GRP_L)
len4 = input.int(100, "EMA 4", minval = 1, inline = "4", group = GRP_L)
col4 = input.color(#FF9800, "", inline = "4", group = GRP_L)
prj4 = input.bool(true, "Project", inline = "4", group = GRP_L)
len5 = input.int(200, "EMA 5", minval = 1, inline = "5", group = GRP_L)
col5 = input.color(#F44336, "", inline = "5", group = GRP_L)
prj5 = input.bool(true, "Project", inline = "5", group = GRP_L)
fillRibbon = input.bool(true, "Fill ribbon", group = GRP_L)
e1 = ta.ema(src, len1)
e2 = ta.ema(src, len2)
e3 = ta.ema(src, len3)
e4 = ta.ema(src, len4)
e5 = ta.ema(src, len5)
p1 = plot(e1, "EMA 1", col1, 2)
p2 = plot(e2, "EMA 2", col2, 2)
p3 = plot(e3, "EMA 3", col3, 2)
p4 = plot(e4, "EMA 4", col4, 2)
p5 = plot(e5, "EMA 5", col5, 2)
// Ribbon color: green when stacked bullish, red when stacked bearish, gray when mixed
bullStack = e1 > e2 and e2 > e3 and e3 > e4 and e4 > e5
bearStack = e1 < e2 and e2 < e3 and e3 < e4 and e4 < e5
ribCol = bullStack ? color.new(color.green, 85) : bearStack ? color.new(color.red, 85) : color.new(color.gray, 90)
fill(p1, p2, fillRibbon ? ribCol : na, "Ribbon 1-2")
fill(p2, p3, fillRibbon ? ribCol : na, "Ribbon 2-3")
fill(p3, p4, fillRibbon ? ribCol : na, "Ribbon 3-4")
fill(p4, p5, fillRibbon ? ribCol : na, "Ribbon 4-5")
// ===== Projection =====
GRP_P = "Projection"
showProj = input.bool(true, "Show projection ribbon", group = GRP_P)
projBars = input.int(10, "Bars to project forward", minval = 1, maxval = 500, group = GRP_P)
slopeLen = input.int(3, "Slope lookback (bars)", minval = 1, maxval = 100, group = GRP_P, tooltip = "Average change per bar over this many bars is used to extend each EMA forward.")
projFill = input.bool(true, "Fill projected ribbon", group = GRP_P)
slopeOf(float e) => (e - e[slopeLen]) / slopeLen
s1 = slopeOf(e1)
s2 = slopeOf(e2)
s3 = slopeOf(e3)
s4 = slopeOf(e4)
s5 = slopeOf(e5)
var array<line> projLines = array.new<line>()
var array<linefill> projFills = array.new<linefill>()
if barstate.islast
for f in projFills
linefill.delete(f)
projFills.clear()
for l in projLines
line.delete(l)
projLines.clear()
if showProj
emas = array.from(e1, e2, e3, e4, e5)
slopes = array.from(s1, s2, s3, s4, s5)
cols = array.from(col1, col2, col3, col4, col5)
ons = array.from(prj1, prj2, prj3, prj4, prj5)
ends = array.new<float>()
for i = 0 to 4
if ons.get(i)
e = emas.get(i)
s = slopes.get(i)
projLines.push(line.new(bar_index, e, bar_index + projBars, e + s * projBars, color = cols.get(i), style = line.style_dashed, width = 2))
ends.push(e + s * projBars)
n = projLines.size()
if projFill and n > 1
// Color the projected ribbon by how the projected EMAs (that are turned on) end up stacked
projBull = true
projBear = true
for i = 0 to n - 2
if ends.get(i) <= ends.get(i + 1)
projBull := false
if ends.get(i) >= ends.get(i + 1)
projBear := false
pCol = projBull ? color.new(color.green, 88) : projBear ? color.new(color.red, 88) : color.new(color.gray, 92)
for i = 0 to n - 2
projFills.push(linefill.new(projLines.get(i), projLines.get(i + 1), pCol))
// ===== Direction box =====
GRP_T = "Direction Box"
showBox = input.bool(true, "Show direction box", group = GRP_T)
boxPos = input.string("Top Right", "Position", options = ["Top Right", "Top Left", "Bottom Right", "Bottom Left", "Middle Right"], group = GRP_T)
boxSize = input.string("Small", "Text size", options = ["Tiny", "Small", "Normal", "Large"], group = GRP_T)
tblPos = switch boxPos
"Top Left" => position.top_left
"Bottom Right" => position.bottom_right
"Bottom Left" => position.bottom_left
"Middle Right" => position.middle_right
=> position.top_right
txtSize = switch boxSize
"Tiny" => size.tiny
"Normal" => size.normal
"Large" => size.large
=> size.small
var table dirTbl = table.new(tblPos, 3, 7, bgcolor = color.new(color.black, 20), border_width = 1, border_color = color.new(color.gray, 60))
if barstate.isfirst
table.merge_cells(dirTbl, 0, 6, 2, 6)
if barstate.islast and showBox
lens = array.from(len1, len2, len3, len4, len5)
slopes = array.from(s1, s2, s3, s4, s5)
cols = array.from(col1, col2, col3, col4, col5)
table.cell(dirTbl, 0, 0, "EMA", text_color = color.white, text_size = txtSize)
table.cell(dirTbl, 1, 0, "Direction", text_color = color.white, text_size = txtSize)
table.cell(dirTbl, 2, 0, "Slope/bar", text_color = color.white, text_size = txtSize)
upCnt = 0
for i = 0 to 4
s = slopes.get(i)
if s > 0
upCnt += 1
dTxt = s > 0 ? "▲ UP" : s < 0 ? "▼ DOWN" : "► FLAT"
dCol = s > 0 ? color.new(color.green, 20) : s < 0 ? color.new(color.red, 20) : color.new(color.gray, 20)
table.cell(dirTbl, 0, i + 1, "EMA " + str.tostring(lens.get(i)), text_color = cols.get(i), text_size = txtSize)
table.cell(dirTbl, 1, i + 1, dTxt, bgcolor = dCol, text_color = color.white, text_size = txtSize)
table.cell(dirTbl, 2, i + 1, str.tostring(s, format.mintick), text_color = color.white, text_size = txtSize)
sumTxt = upCnt == 5 ? "RIBBON: ALL UP ▲" : upCnt == 0 ? "RIBBON: ALL DOWN ▼" : "RIBBON: MIXED (" + str.tostring(upCnt) + "/5 up)"
sumCol = upCnt == 5 ? color.new(color.green, 10) : upCnt == 0 ? color.new(color.red, 10) : color.new(color.gray, 30)
table.cell(dirTbl, 0, 6, sumTxt, bgcolor = sumCol, text_color = color.white, text_size = txtSize)