Kaufman Efficiency Ratio (KER)

briankantanka · study · 93 行 · 点赞 14 · TradingView 原页

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

Pine Script

//@version=6
indicator("Kaufman Efficiency Ratio (KER)", shorttitle="KER", overlay=false, precision=3)
 
// ───────────────────────────────────────
// INPUTS
// ───────────────────────────────────────
groupCalc = "Calculation Settings"
length = input.int(10, "Lookback Period", minval=2, group=groupCalc)
src = input.source(close, "Price Source", group=groupCalc)
smoothLength = input.int(1, "Smoothing Length", minval=1, group=groupCalc)
 
groupLevels = "Efficiency Thresholds"
chopLevel = input.float(0.20, "Choppy Threshold", minval=0, maxval=1, step=0.05, group=groupLevels)
trendLevel = input.float(0.35, "Trending Threshold", minval=0, maxval=1, step=0.05, group=groupLevels)
 
groupDisplay = "Display Settings"
showDashboard = input.bool(true, "Show Dashboard", group=groupDisplay)
showBg = input.bool(true, "Highlight Trending Regimes", group=groupDisplay)
 
// ───────────────────────────────────────
// KAUFMAN EFFICIENCY RATIO
// ───────────────────────────────────────
 
// Net price change over lookback period
netChange = math.abs(src - src[length])
 
// Total absolute price movement
barChange = math.abs(src - src[1])
totalMovement = math.sum(barChange, length)
 
// Efficiency Ratio ranges from 0 to 1
erRaw = not na(totalMovement) and totalMovement > 0 ? netChange / totalMovement : na
 
// Optional smoothing
er = ta.sma(erRaw, smoothLength)
 
// ───────────────────────────────────────
// MARKET REGIME
// ───────────────────────────────────────
isTrending = er >= trendLevel
isChoppy = er < chopLevel
isMixed = not na(er) and not isTrending and not isChoppy
 
// Direction is separate from efficiency
isBullish = src > src[length]
isBearish = src < src[length]
 
// Colors
trendColor = color.lime
mixedColor = color.orange
chopColor = color.red
 
erColor = isTrending ? trendColor : isChoppy ? chopColor : mixedColor
 
// ───────────────────────────────────────
// PLOTS
// ───────────────────────────────────────
plot(er, "Efficiency Ratio", color=erColor, linewidth=2)
 
hline(1.0, "Maximum Efficiency", color=color.new(color.gray, 70))
hline(0.0, "Minimum Efficiency", color=color.new(color.gray, 70))
 
hline(chopLevel, "Choppy Threshold", color=color.red, linestyle=hline.style_dashed)
hline(trendLevel, "Trending Threshold", color=color.lime, linestyle=hline.style_dashed)
 
bgcolor(showBg and isTrending ? color.new(color.green, 90) : na)
 
// ───────────────────────────────────────
// DASHBOARD
// ───────────────────────────────────────
var table dashboard = table.new(position.top_right, 2, 3, border_width=1)
 
if barstate.islast
    if showDashboard
        regime = na(er) ? "N/A" : isTrending ? "TRENDING" : isChoppy ? "CHOPPY" : "MIXED"
        direction = na(src[length]) ? "N/A" : isBullish ? "BULLISH" : isBearish ? "BEARISH" : "FLAT"
 
        table.cell(dashboard, 0, 0, "Efficiency Ratio", bgcolor=color.black, text_color=color.white)
        table.cell(dashboard, 1, 0, str.tostring(er, "#.###"), bgcolor=color.black, text_color=erColor)
 
        table.cell(dashboard, 0, 1, "Market Regime", bgcolor=color.black, text_color=color.white)
        table.cell(dashboard, 1, 1, regime, bgcolor=color.black, text_color=erColor)
 
        table.cell(dashboard, 0, 2, "Price Direction", bgcolor=color.black, text_color=color.white)
        table.cell(dashboard, 1, 2, direction, bgcolor=color.black, text_color=color.white)
    else
        table.clear(dashboard, 0, 0, 1, 2)
 
// ───────────────────────────────────────
// ALERTS
// ───────────────────────────────────────
alertcondition(ta.crossover(er, trendLevel), "Trending Regime Started", "KER crossed above the trending threshold.")
alertcondition(ta.crossunder(er, chopLevel), "Choppy Regime Started", "KER crossed below the choppy threshold.")

← 返回列表