本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=6
// =====================================================================
// Adaptive Moving Average — Kalman
// Version: 1.0
//
// A single, honest Kalman-filter smoother for price. Treats price as a
// noisy observation of a hidden true level and reconstructs that level
// optimally, bar by bar.
//
// Unlike SMA/EMA, there is no fixed lag: the filter adapts to the
// balance between process noise (how fast the true level moves) and
// observation noise (how noisy the price is). Faster reaction in
// trends, more smoothing in chop.
//
// Ships with an optional signal line (a slower Kalman) so you get one
// built-in crossover signal. Nothing else — this is the clean version.
//
// No lookahead. Pine v6 strict.
// =====================================================================
indicator("Adaptive Moving Average — Kalman", shorttitle = "Kalman MA",
overlay = true, precision = 5,
max_labels_count = 50)
// =============================
// INPUTS
// =============================
grpSrc = "Source"
src = input.source(close, "Source", group = grpSrc)
grpKalman = "Kalman"
qMain = input.float(0.01, "Process variance (main)", minval = 0.0001, maxval = 10.0, step = 0.001, group = grpKalman,
tooltip = "How fast the hidden level is allowed to move per bar. Higher = more reactive line. Lower = smoother line.")
rMain = input.float(1.0, "Observation variance (main)", minval = 0.01, maxval = 100.0, step = 0.1, group = grpKalman,
tooltip = "How noisy we assume the price is. Higher = filter trusts price less, so the line is smoother.")
grpSig = "Signal line (optional)"
showSignal = input.bool(true, "Show signal line", group = grpSig)
qSig = input.float(0.002, "Process variance (signal)", minval = 0.0001, maxval = 10.0, step = 0.001, group = grpSig,
tooltip = "Slower Kalman for crossover signals. Leave lower than the main process variance.")
rSig = input.float(1.0, "Observation variance (signal)", minval = 0.01, maxval = 100.0, step = 0.1, group = grpSig)
grpColor = "Colours"
upCol = input.color(#26A69A, "Uptrend colour", group = grpColor)
downCol = input.color(#EF5350, "Downtrend colour", group = grpColor)
sigCol = input.color(#FFA726, "Signal line colour", group = grpColor)
colourByTrend = input.bool(true, "Colour the main line by trend", group = grpColor,
tooltip = "If true, the main line turns green when it's rising and red when it's falling.")
// =============================
// KALMAN FILTER (local level)
// =============================
// Model:
// x_t = x_{t-1} + w_t, w ~ N(0, q)
// y_t = x_t + v_t, v ~ N(0, r)
//
// Update (one step):
// P_pred = P_prev + q
// K = P_pred / (P_pred + r)
// x = x_prev + K * (y - x_prev)
// P = (1 - K) * P_pred
// =============================
var float xMain = na
var float pMain = na
if na(xMain)
xMain := src
pMain := 1.0
else
float pPred = pMain + qMain
float k = pPred / (pPred + rMain)
xMain := xMain + k * (src - xMain)
pMain := (1.0 - k) * pPred
var float xSig = na
var float pSig = na
if na(xSig)
xSig := src
pSig := 1.0
else
float pPredS = pSig + qSig
float kS = pPredS / (pPredS + rSig)
xSig := xSig + kS * (src - xSig)
pSig := (1.0 - kS) * pPredS
// =============================
// PLOT
// =============================
bool rising = not na(xMain) and not na(xMain[1]) and xMain > xMain[1]
color mainCol = colourByTrend ? (rising ? upCol : downCol) : #42A5F5
plot(xMain, "Kalman MA", color = mainCol, linewidth = 2)
plot(showSignal ? xSig : na, "Kalman signal", color = sigCol, linewidth = 1)
// =============================
// ALERTS
// =============================
bool crossUp = showSignal and not na(xMain) and not na(xSig) and not na(xMain[1]) and not na(xSig[1]) and xMain[1] <= xSig[1] and xMain > xSig
bool crossDown = showSignal and not na(xMain) and not na(xSig) and not na(xMain[1]) and not na(xSig[1]) and xMain[1] >= xSig[1] and xMain < xSig
bool flipUp = not na(xMain) and not na(xMain[1]) and not na(xMain[2]) and xMain[1] <= xMain[2] and xMain > xMain[1]
bool flipDown = not na(xMain) and not na(xMain[1]) and not na(xMain[2]) and xMain[1] >= xMain[2] and xMain < xMain[1]
alertcondition(crossUp, "Kalman bullish cross", "Main Kalman crossed above signal Kalman.")
alertcondition(crossDown, "Kalman bearish cross", "Main Kalman crossed below signal Kalman.")
alertcondition(flipUp, "Kalman slope flipped up", "Main Kalman slope flipped bullish.")
alertcondition(flipDown, "Kalman slope flipped down", "Main Kalman slope flipped bearish.")