本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
// © Yaroslav 2026
//@version=6
indicator("KAMA Ribbon [Yaro]", "KAMA Ribbon", overlay = true)
// ---- Settings: what to show -> look ----
G_K = '1. KAMA Lines'
G_STY = '2. Colors & Lines'
// A plain price list: input.source would also list every other indicator's outputs
// The line tooltip sits here: an info icon on a KAMA row would push its Slow field to a new line
srcIn = input.string('Close', 'Source', options = ['Close', 'HL2', 'HLC3', 'OHLC4', 'HLCC4']
, group = G_K
, tooltip = 'Price the lines are built from. Each KAMA row: ER = efficiency-ratio length, how '
+ 'many bars KAMA judges trend vs chop over; Fast / Slow = EMA lengths it moves between in a '
+ 'clean trend and in chop. ER sets the pace more than Slow: with one ER for all, the lines '
+ 'lie on top of each other (S&P / Nasdaq test: 0.01-0.06 ATR apart). Defaults scale ER with '
+ 'Slow (about Slow / 3): ER 10 / 17 / 27 / 43 / 67, Slow 30 / 50 / 80 / 130 / 200, so '
+ 'neighbours sit 0.4-0.8 ATR apart and each line turns less often than the one before.')
src = switch srcIn
'HL2' => hl2
'HLC3' => hlc3
'OHLC4' => ohlc4
'HLCC4' => hlcc4
=> close
tfIn = input.timeframe('', 'Timeframe', group = G_K
, tooltip = 'Empty = chart timeframe. A higher one is read from its last closed bar, so the '
+ 'lines do not repaint; a lower one falls back to the chart timeframe.')
use1 = input.bool(true, 'KAMA 1', inline = 'k1', group = G_K)
er1 = input.int(10, 'ER', minval = 1, inline = 'k1', group = G_K)
fast1 = input.int(2, 'Fast', minval = 1, inline = 'k1', group = G_K)
slow1 = input.int(30, 'Slow', minval = 2, inline = 'k1', group = G_K)
use2 = input.bool(false, 'KAMA 2', inline = 'k2', group = G_K)
er2 = input.int(17, 'ER', minval = 1, inline = 'k2', group = G_K)
fast2 = input.int(2, 'Fast', minval = 1, inline = 'k2', group = G_K)
slow2 = input.int(50, 'Slow', minval = 2, inline = 'k2', group = G_K)
use3 = input.bool(false, 'KAMA 3', inline = 'k3', group = G_K)
er3 = input.int(27, 'ER', minval = 1, inline = 'k3', group = G_K)
fast3 = input.int(2, 'Fast', minval = 1, inline = 'k3', group = G_K)
slow3 = input.int(80, 'Slow', minval = 2, inline = 'k3', group = G_K)
use4 = input.bool(false, 'KAMA 4', inline = 'k4', group = G_K)
er4 = input.int(43, 'ER', minval = 1, inline = 'k4', group = G_K)
fast4 = input.int(2, 'Fast', minval = 1, inline = 'k4', group = G_K)
slow4 = input.int(130, 'Slow', minval = 2, inline = 'k4', group = G_K)
use5 = input.bool(false, 'KAMA 5', inline = 'k5', group = G_K)
er5 = input.int(67, 'ER', minval = 1, inline = 'k5', group = G_K)
fast5 = input.int(2, 'Fast', minval = 1, inline = 'k5', group = G_K)
slow5 = input.int(200, 'Slow', minval = 2, inline = 'k5', group = G_K)
theme = input.string('Auto', 'Theme', options = ['Auto', 'Dark', 'Light'], group = G_STY
, tooltip = 'Auto follows the chart background. Light darkens the lines (x0.6) for contrast '
+ 'on white.')
css1 = input.color(#f0e442, 'Lines', inline = 'css', group = G_STY)
css2 = input.color(#9ccc65, '', inline = 'css', group = G_STY)
css3 = input.color(#4dd0c4, '', inline = 'css', group = G_STY)
css4 = input.color(#a78bfa, '', inline = 'css', group = G_STY)
css5 = input.color(#f48fb1, '', inline = 'css', group = G_STY
, tooltip = 'KAMA 1 to 5, fast to slow, in hue order: yellow, green, teal, violet, pink. Every '
+ 'line keeps >= 3.6:1 contrast on dark and light charts, neighbours stay apart for '
+ 'color-blind viewers too, and none uses the support / resistance blue or orange.')
// ---- Theme ----
isDark = theme == 'Dark' or (theme == 'Auto'
and color.r(chart.bg_color) + color.g(chart.bg_color) + color.b(chart.bg_color) < 384)
solid(color c) => isDark ? c : color.rgb(color.r(c) * .6, color.g(c) * .6, color.b(c) * .6)
// ---- KAMA (Kaufman), same formula as KAMA Channel ----
kamaOf(float x, int erLen, int fast, int slow) =>
fastSC = 2.0 / (fast + 1)
slowSC = 2.0 / (slow + 1)
path = math.sum(math.abs(x - x[1]), erLen)
er = path != 0 ? math.abs(x - x[erLen]) / path : 0.0
sc = math.pow(er * (fastSC - slowSC) + slowSC, 2)
k = 0.0
k := na(k[1]) ? x : k[1] + sc * (x - k[1])
// Higher timeframe: last closed bar ([1] + lookahead_on = no repaint); chart or lower: as is
tfUse = timeframe.in_seconds(tfIn) < timeframe.in_seconds() ? '' : tfIn
htf = tfUse != '' and timeframe.in_seconds(tfUse) > timeframe.in_seconds()
OFF = htf ? 1 : 0
kamaLine(int erLen, int fast, int slow) =>
request.security(syminfo.tickerid, tfUse, kamaOf(src, erLen, fast, slow)[OFF]
, lookahead = htf ? barmerge.lookahead_on : barmerge.lookahead_off)
k1 = kamaLine(er1, fast1, slow1)
k2 = kamaLine(er2, fast2, slow2)
k3 = kamaLine(er3, fast3, slow3)
k4 = kamaLine(er4, fast4, slow4)
k5 = kamaLine(er5, fast5, slow5)
plot(use1 ? k1 : na, 'KAMA 1', solid(css1))
plot(use2 ? k2 : na, 'KAMA 2', solid(css2))
plot(use3 ? k3 : na, 'KAMA 3', solid(css3))
plot(use4 ? k4 : na, 'KAMA 4', solid(css4))
plot(use5 ? k5 : na, 'KAMA 5', solid(css5))