예스스탁
예스스탁 답변
2025-02-24 16:03:35
안녕하세요
예스스탁입니다.
input : short_len(50);
input : long_len(150);
input : retest_sig(false);
input : candle_color(true);
input : upper_col(Green);
input : lower_col(Maroon);
var : alpha(0),AR(0),A(0);
var : R(0.01),Q(0.1);
var : short_estimate(Nan),short_error_est(1.0),short_error_meas(R * (short_len));
var : short_kalman_gain(0),short_prediction(Nan),short_kalman(0);
var : Long_estimate(Nan),Long_error_est(1.0),Long_error_meas(R * (Long_len));
var : Long_kalman_gain(0),Long_prediction(Nan),Long_kalman(0);
var : trend_up(False);
var : trend_col(0),trend_col1(0),candle_col(0);
var : tx1(0),tx2(0),box1(0),box2(0);
alpha = 1 / 200 ;
A = iff(IsNan(A[1]) == true , ma(TrueRange, 200) , alpha * TrueRange + (1 - alpha) * iff(IsNan(A[1]) == true,0,A[1]));
AR = A*0.5;
if isnan(short_estimate) == true Then
short_estimate = C[1];
short_prediction = short_estimate;
short_kalman_gain = short_error_est / (short_error_est + short_error_meas);
short_estimate = short_prediction + short_kalman_gain * (C - short_prediction);
short_error_est = (1 - short_kalman_gain) * short_error_est + Q / (short_len);
short_kalman = short_estimate;
if isnan(Long_estimate) == true Then
Long_estimate = C[1];
Long_prediction = Long_estimate;
Long_kalman_gain = Long_error_est / (Long_error_est + Long_error_meas);
Long_estimate = Long_prediction + Long_kalman_gain * (C - Long_prediction);
Long_error_est = (1 - Long_kalman_gain) * Long_error_est + Q / (Long_len);
Long_kalman = Long_estimate;
trend_up = short_kalman > long_kalman;
trend_col = iff(trend_up , upper_col , lower_col);
trend_col1 = iff(short_kalman > short_kalman[2] , upper_col , lower_col);
candle_col = iff(candle_color ,
IFf(trend_up == true and short_kalman > short_kalman[2] , upper_col ,
iff(trend_up == False and short_kalman < short_kalman[2] , lower_col , gray)) , Black);
if Crossup(short_kalman,long_kalman) Then
{
tx1 = text_new(sDate,sTime,min(L,long_kalman),"▲");
tx2 = text_new(sDate,sTime,min(L,long_kalman),NewLine+NumToStr(C,2));
Text_SetStyle(tx1,2,0);
Text_SetStyle(tx2,2,0);
Text_SetColor(tx1,trend_col);
Text_SetColor(tx2,trend_col);
var1 = Low+AR;
box1 = Box_New(sDate,sTime,Low,NextBarSdate,NextBarStime,high);
Box_SetColor(box1,trend_col);
Box_SetFill(box1,true);
}
Else
Box_SetEnd(box1,sDate,stime,var1);
if CrossDown(short_kalman,long_kalman) Then
{
tx1 = text_new(sDate,sTime,max(H,long_kalman),"▼");
tx2 = text_new(sDate,sTime,max(H,long_kalman),NumToStr(C,2)+NewLine);
Text_SetStyle(tx1,2,1);
Text_SetStyle(tx2,2,1);
Text_SetColor(tx1,trend_col);
Text_SetColor(tx2,trend_col);
var2 = high-A;
box2 = Box_New(sDate,sTime,high,NextBarSdate,NextBarStime,var2);
Box_SetColor(box2,trend_col);
Box_SetFill(box2,true);
}
Else
Box_SetEnd(box2,sDate,stime,var2);
plot1(short_kalman, "Short Kalman", trend_col1);
plot2(long_kalman, "Long Kalman", trend_col);
즐거운 하루되세요
> 레전드 님이 쓴 글입니다.
> 제목 : 문의
> 예스로 부탁드립니다.
uodate가 금지어라고 해서 뛰어쓰기 했습니다
indicator("Kalman Trend Levels [BigBeluga]", overlay = true, max_labels_count = 500, max_boxes_count = 500)
// INPUTS ――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――{
int short_len = input.int(50)
int long_len = input.int(150)
bool retest_sig = input.bool(false, "Retest Signals")
bool candle_color = input.bool(true, "Candle Color")
color upper_col = input.color(#13bd6e, "up", inline = "colors")
color lower_col = input.color(#af0d4b, "dn", inline = "colors")
// }
// CALCULATIONS――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――{
float atr = ta.atr(200) *0.5
var lower_box = box(na)
var upper_box = box(na)
// Kalman filter function
kalman_filter(src, length, R = 0.01, Q = 0.1) =>
// Initialize variables
var float estimate = na
var float error_est = 1.0
var float error_meas = R * (length)
var float kalman_gain = 0.0
var float prediction = na
// Initialize the estimate with the first value of the source
if na(estimate)
estimate := src[1]
// Prediction step
prediction := estimate
// Up date Kalman gain
kalman_gain := error_est / (error_est + error_meas)
// Up date estimate with measurement correction
estimate := prediction + kalman_gain * (src - prediction)
// Up date error estimates
error_est := (1 - kalman_gain) * error_est + Q / (length) // Adjust process noise based on length
estimate
float short_kalman = kalman_filter(close, short_len)
float long_kalman = kalman_filter(close, long_len)
bool trend_up = short_kalman > long_kalman
color trend_col = trend_up ? upper_col : lower_col
color trend_col1 = short_kalman > short_kalman[2] ? upper_col : lower_col
color candle_col = candle_color ? (trend_up and short_kalman > short_kalman[2] ? upper_col : not trend_up and short_kalman < short_kalman[2] ? lower_col : color.gray) : na
// }
// PLOT ――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――{
if trend_up and not trend_up[1]
label.new(bar_index, short_kalman, "🡹₩n" + str.tostring(math.round(close,1)), color = color(na), textcolor = upper_col, style = label.style_label_up, size = size.normal)
lower_box := box.new(bar_index, low+atr, bar_index, low, border_color = na, bgcolor = color.new(upper_col, 60))
if not ta.change(trend_up)
lower_box.set_right(bar_index)
if trend_up[1] and not trend_up
label.new(bar_index, short_kalman, str.tostring(math.round(close,1))+"₩n🢃", color = color(na), textcolor = lower_col, style = label.style_label_down, size = size.normal)
upper_box := box.new(bar_index, high, bar_index, high-atr, border_color = na, bgcolor = color.new(lower_col, 60))
if not ta.change(trend_up)
upper_box.set_right(bar_index)
if retest_sig
if high < upper_box.get_bottom() and high[1]>= upper_box.get_bottom() //or high < lower_box.get_bottom() and high[1]>= lower_box.get_bottom()
label.new(bar_index-1, high[1], "x", color = color(na), textcolor = lower_col, style = label.style_label_down, size = size.normal)
if low > lower_box.get_top() and low[1]<= lower_box.get_top()
label.new(bar_index-1, low[1], "+", color = color(na), textcolor = upper_col, style = label.style_label_up, size = size.normal)
p1 = plot(short_kalman, "Short Kalman", color = trend_col1)
p2 = plot(long_kalman, "Long Kalman", linewidth = 2, color = trend_col)
fill(p1, p2, short_kalman, long_kalman, na, color.new(trend_col, 80))
plotcandle(open, high, low, close, title='Title', color = candle_col, wickcolor=candle_col, bordercolor = candle_col)
// }