커뮤니티
부탁드립니다
수고하십니다
아래수식을 오류 없게 수정부탁드립니다
inputs:
ShortLength(50),
LongLength(150),
RetestSignals(false),
CandleColor(true),
UpperColor(Magenta),
LowerColor(Blue);
variables:
ATRValue(0),
ShortKalman(0),
LongKalman(0),
// Kalman filter variables for short
ShortEstimate(0),
ShortErrorEst(1.0),
ShortKalmanGain(0),
ShortPrediction(0),
ShortErrorMeas(0),
ShortInitialized(false),
// Kalman filter variables for long
LongEstimate(0),
LongErrorEst(1.0),
LongKalmanGain(0),
LongPrediction(0),
LongErrorMeas(0),
LongInitialized(false),
TrendUp(false),
PrevTrendUp(false),
TrendColor(0),
TrendColor1(0),
CandleCol(0),
UpperBoxTop(0),
UpperBoxBottom(0),
UpperBoxStart(0),
LowerBoxTop(0),
LowerBoxBottom(0),
LowerBoxStart(0),
BoxActive(false),
R(0.01),
Q(0.1);
// Calculate ATR
ATRValue = AvgTrueRange(200) * 0.5;
// Kalman Filter for Short Length
ShortErrorMeas = R * ShortLength;
if not ShortInitialized then begin
ShortEstimate = Close;
ShortInitialized = true;
end;
ShortPrediction = ShortEstimate;
ShortKalmanGain = ShortErrorEst / (ShortErrorEst + ShortErrorMeas);
ShortEstimate = ShortPrediction + ShortKalmanGain * (Close - ShortPrediction);
ShortErrorEst = (1 - ShortKalmanGain) * ShortErrorEst + Q / ShortLength;
ShortKalman = ShortEstimate;
// Kalman Filter for Long Length
LongErrorMeas = R * LongLength;
if not LongInitialized then begin
LongEstimate = Close;
LongInitialized = true;
end;
LongPrediction = LongEstimate;
LongKalmanGain = LongErrorEst / (LongErrorEst + LongErrorMeas);
LongEstimate = LongPrediction + LongKalmanGain * (Close - LongPrediction);
LongErrorEst = (1 - LongKalmanGain) * LongErrorEst + Q / LongLength;
LongKalman = LongEstimate;
// Determine trend
PrevTrendUp = TrendUp;
TrendUp = ShortKalman > LongKalman;
// Set colors
if TrendUp then
TrendColor = UpperColor
else
TrendColor = LowerColor;
if ShortKalman > ShortKalman[2] then
TrendColor1 = UpperColor
else
TrendColor1 = LowerColor;
// Candle coloring
if CandleColor then begin
if TrendUp and ShortKalman > ShortKalman[2] then
CandleCol = UpperColor
else if not TrendUp and ShortKalman < ShortKalman[2] then
CandleCol = LowerColor
else
CandleCol = DarkGray;
end;
// Plot Kalman filters
Plot1(ShortKalman, "Short Kalman", TrendColor1);
Plot2(LongKalman, "Long Kalman", TrendColor);
// Trend change detection and signals
if TrendUp and not PrevTrendUp then begin
// Uptrend signal
Text_New(Date, Time, Low, "UP");
// Create lower box
LowerBoxStart = CurrentBar;
LowerBoxTop = Low + ATRValue;
LowerBoxBottom = Low;
BoxActive = true;
end;
if not TrendUp and PrevTrendUp then begin
// Downtrend signal
Text_New(Date, Time, High, "DN");
// Create upper box
UpperBoxStart = CurrentBar;
UpperBoxTop = High;
UpperBoxBottom = High - ATRValue;
BoxActive = true;
end;
// Retest signals
if RetestSignals then begin
if not TrendUp and BoxActive then begin
if High < UpperBoxBottom and High[1] >= UpperBoxBottom then
Text_New(Date, Time, High[1], "x");
end;
if TrendUp and BoxActive then begin
if Low > LowerBoxTop and Low[1] <= LowerBoxTop then
Text_New(Date, Time, Low[1], "+");
end;
end;
// Apply candle coloring
if CandleColor then
PlotPB(Open, High, Low, Close, CandleCol, CandleCol);
답변 1
예스스탁 예스스탁 답변
2025-10-27 16:57:17