커뮤니티
변환 부탁 드립니다.
트레이팅 뷰 지표입니다.
사용가능 하도록 수정 부탁 드립니다.
//@version=6
indicator(
title="Kalman Adjusted Average True Range [BackQuant]",
shorttitle = "Kalman ATR [BackQuant]",
overlay=true)
// Define User Inputs
const string tooltip1 = "If T3 is selected as the moving average this will be the volume factor, if ALMA is selected it will be the sigma, ELSE it is nothing"
simple bool showAtr = input.bool(true, "Plot Kalman Atr on Chart?")
series float pricesource = input.source(close, "Kalman Price Source", group = "Calculation")
simple float processNoise = input.float(0.01, title="Process Noise", step = 0.01, group = "Calculation")
simple float measurementNoise = input.float(3.0, title="Measurement Noise", group = "Calculation")
simple int N = input.int(5, title="Filter Order", minval=1, group = "Calculation")
simple int periodAtr = input.int(5, "Period", group = "Kalman Atr")
simple float factorAtr = input.float(0.5, "Factor", step = 0.01, group = "Kalman Atr")
simple bool paintCandles = input.bool(false, "Paint Candles According to trend?")
simple bool showMA = input.bool(false, "Show Atr Moving Average as Confluence?",group = "Confluence")
string movingAverageType = input.string("Ema", title="MA Type", options=["SMA", "Hull", "Ema", "Wma", "Dema", "RMA", "LINREG", "ALMA"],group = "Confluence")
simple float vfsig = input.float(0.7, "Volume Factor if T3, Sigma if ALMA", group = "Confluence", tooltip = "If T3 is selected as the moving average this will be the volume factor, if ALMA is selected it will be the sigma, ELSE it is nothing")
simple int movingAveragePeriod = input.int(50, "Moving Average Period", group = "Confluence")
simple color longColour = input.color(#00ff00, "Long Colour", group = "Colors")
simple color shortColour = input.color(#ff0000, "Short Color", group = "Colors")
/////////////////////////////////////////////////////////////// © BackQuant ///////////////////////////////////////////////////////////////
// Function
var float[] stateEstimate = array.new_float(N, na)
var float[] errorCovariance = array.new_float(N, 100.0)
f_init(series float pricesource) =>
if na(array.get(stateEstimate, 0))
for i = 0 to N-1
array.set(stateEstimate, i, pricesource)
array.set(errorCovariance, i, 1.0)
f_kalman(series float pricesource) =>
// Prediction Step
predictedStateEstimate = array.new_float(N)
predictedErrorCovariance = array.new_float(N)
for i = 0 to N-1
array.set(predictedStateEstimate, i, array.get(stateEstimate, i)) // Simplified prediction
array.set(predictedErrorCovariance, i, array.get(errorCovariance, i) + processNoise)
kalmanGain = array.new_float(N)
for i = 0 to N-1
kg = array.get(predictedErrorCovariance, i) / (array.get(predictedErrorCovariance, i) + measurementNoise)
array.set(kalmanGain, i, kg)
array.set(stateEstimate, i, array.get(predictedStateEstimate, i) + kg * (pricesource - array.get(predictedStateEstimate, i)))
array.set(errorCovariance, i, (1 - kg) * array.get(predictedErrorCovariance, i))
array.get(stateEstimate, 0)
KalmanAtrWithBands(pricesource, lookback, atrFactor)=>
f_init(pricesource)
kalmanFilteredPrice = f_kalman(pricesource)
atr = ta.atr(lookback)
trueRange = atr * atrFactor
kalmanatr = kalmanFilteredPrice
kalmanatr := nz(kalmanatr[1], kalmanatr)
trueRangeUpper = kalmanFilteredPrice + trueRange
trueRangeLower = kalmanFilteredPrice - trueRange
if trueRangeLower > kalmanatr
kalmanatr := trueRangeLower
if trueRangeUpper < kalmanatr
kalmanatr := trueRangeUpper
kalmanatr
// Function Out
kalmanatr = KalmanAtrWithBands(pricesource, periodAtr, factorAtr)
/////////////////////////////////////////////////////////////// © BackQuant ///////////////////////////////////////////////////////////////
// Moving Average Switch Type
movingAverage(source, length, type, vfsig) =>
switch type
"SMA" => ta.sma(source, length)
"Hull" => ta.hma(source, length)
"Ema" => ta.ema(source, length)
"Wma" => ta.wma(source, length)
"Dema" => ta.dema(source, length)
"RMA" => ta.rma(source, length)
"LINREG" => ta.linreg(source, length, 0)
"ALMA" => ta.alma(source, length, 0, vfsig)
maOut = movingAverage(kalmanatr, movingAveragePeriod, movingAverageType, vfsig)
/////////////////////////////////////////////////////////////// © BackQuant ///////////////////////////////////////////////////////////////
// Conditions
kalmanatrLong = ta.crossover(kalmanatr, kalmanatr[1])
kalmanatrShort = ta.crossunder(kalmanatr, kalmanatr[1])
// Colour Condtions
var color Trend = #ffffff
if kalmanatrLong
Trend := longColour
if kalmanatrShort
Trend := shortColour
// Plotting
plot(
showAtr ? kalmanatr : na,
"ATR",
color=Trend,
linewidth = 2
)
barcolor(paintCandles ? Trend : na)
plot(showMA ? maOut : na, "Moving Average", color.white, 2, plot.style_line)
/////////////////////////////////////////////////////////////// © BackQuant ///////////////////////////////////////////////////////////////
// Alerts
alertcondition(
kalmanatrLong,
title="Kalman ATR Trend Up",
message="Kalman ATR Trend Up - {{ticker}} - {{interval}}"
)
alertcondition(
kalmanatrShort,
title="Kalman ATR Trend Down",
message="Kalman ATR Trend Down - {{ticker}} - {{interval}}"
)
답변 1
예스스탁 예스스탁 답변
2025-12-29 15:00:35