예스스탁
예스스탁 답변
2025-03-12 14:25:44
안녕하세요
예스스탁입니다.
올려주신 식은 차트상 마지막봉의 값을 기준으로 과거봉에 다시 그리는 내용입니다.
예스랭귀지의 plot은 현재시점에서 과거봉에 그릴수가 없어서
텍스트 함수로 출력하게 작성해 드립니다.
해당식이 텍스트함수로 현재봉 기준 과거 500봉에 그리고
예스랭귀지의 구조가 차트 과거봉부터 현재봉으로 오면서 그리게 되어
매봉 과거 500봉에 대해 그리고 지우기를 반복하게 되므로
차트에 조회된 봉이 많으면 적용 후 로드가 심해 프로그램이 다운될수도 있습니다.
차트의 조회봉수를 가급적 500봉 이상 1000봉 이하 정도로 조회해서 적용하시기 바랍니다.
input : hh(8),mult(3),src(close),upCss(Teal),dnCss(Red);
var : n(0),i(0),w(0),den(0),out(0),mae(0),upper(0),lower(0);
Array : ln[500](0),coefs[500](0);
Array : nwe[500](0),uptx[500](0),dntx[500](0),uptx1[500](0),dntx1[500](0);
#gauss(x, h) => math.exp(-(math.pow(x, 2)/(hh * hh * 2)))
n = index;
den = 0;
for i = 0 to 499
{
ln[i] = 0;# array.push(ln,line.new(na,na,na,na))
w = exp(-(pow(i, 2)/(hh *hh * 2)));
coefs[i] = w;
den = den + coefs[i];
}
out = 0;
for i = 0 to 499
{
out = out +(src[i] * coefs[i]);
}
out = out/den;
mae = ma(abs(src - out), 499) * mult;
upper = out + mae;
lower = out - mae;
var : sae(0),y2(0),y1(0),j(0);
var : sum(0),sumw(0);
if Index >= 500 Then
{
for i = 0 to min(499,n - 1)
{
nwe[i] = 0;
Text_Delete(uptx[i]);
Text_Delete(dntx[i]);
Text_Delete(uptx1[i]);
Text_Delete(dntx1[i]);
}
sae = 0;
for i = 0 to min(499,n - 1)
{
sum = 0;
sumw = 0;
for j = 0 to min(499,n - 1)
{
w = exp(-(pow(i-j, 2)/(hh *hh * 2)));
sum = sum + src[j] * w;
sumw = sumw + w;
}
y2 = sum / sumw;
sae = sae + abs(src[i] - y2);
nwe[i] = y2;
}
sae = sae / min(499,n - 1) * mult;
for i = 0 to min(499,n - 1)
{
if (i%2 == 0) Then
{
uptx[i] = text_new(sDate[i],sTime[i],nwe[i]+sae,"-");
Text_SetColor(uptx[i],upcss);
dntx[i] = text_new(sDate[i],sTime[i],nwe[i]-sae,"-");
Text_SetColor(dntx[i],dncss);
if src[i] > nwe[i] + sae and src[i+1] < nwe[i] + sae Then
{
dntx1[i] = text_new(sDate[i],sTime[i],nwe[i]+sae,"▼");
Text_SetColor(dntx1[i],dncss);
}
if src[i] < nwe[i] - sae and src[i+1] > nwe[i] - sae Then
{
uptx1[i] = text_new(sDate[i],sTime[i],nwe[i]+sae,"▲");
Text_SetColor(uptx1[i],upcss);
}
}
y1 = nwe[i];
}
}
즐거운 하루되세요
> 이글루 님이 쓴 글입니다.
> 제목 : 수식변환 부탁드립니다.
> 안녕하세요
// This work is licensed under a Attribution-NonCommercial-ShareAlike 4.0 International (CC BY-NC-SA 4.0) https://creativecommons.org/licenses/by-nc-sa/4.0/
// © LuxAlgo
//@version=5
indicator("Nadaraya-Watson Envelope [LuxAlgo]", "LuxAlgo - Nadaraya-Watson Envelope", overlay = true, max_lines_count = 500, max_labels_count = 500, max_bars_back=500)
//------------------------------------------------------------------------------
//Settings
//-----------------------------------------------------------------------------{
h = input.float(8.,'Bandwidth', minval = 0)
mult = input.float(3., minval = 0)
src = input(close, 'Source')
repaint = input(true, 'Repainting Smoothing', tooltip = 'Repainting is an effect where the indicators historical output is subject to change over time. Disabling repainting will cause the indicator to output the endpoints of the calculations')
//Style
upCss = input.color(color.teal, 'Colors', inline = 'inline1', group = 'Style')
dnCss = input.color(color.red, '', inline = 'inline1', group = 'Style')
//-----------------------------------------------------------------------------}
//Functions
//-----------------------------------------------------------------------------{
//Gaussian window
gauss(x, h) => math.exp(-(math.pow(x, 2)/(h * h * 2)))
//-----------------------------------------------------------------------------}
//Append lines
//-----------------------------------------------------------------------------{
n = bar_index
var ln = array.new_line(0)
if barstate.isfirst and repaint
for i = 0 to 499
array.push(ln,line.new(na,na,na,na))
//-----------------------------------------------------------------------------}
//End point method
//-----------------------------------------------------------------------------{
var coefs = array.new_float(0)
var den = 0.
if barstate.isfirst and not repaint
for i = 0 to 499
w = gauss(i, h)
coefs.push(w)
den := coefs.sum()
out = 0.
if not repaint
for i = 0 to 499
out += src[i] * coefs.get(i)
out /= den
mae = ta.sma(math.abs(src - out), 499) * mult
upper = out + mae
lower = out - mae
//-----------------------------------------------------------------------------}
//Compute and display NWE
//-----------------------------------------------------------------------------{
float y2 = na
float y1 = na
nwe = array.new<float>(0)
if barstate.islast and repaint
sae = 0.
//Compute and set NWE point
for i = 0 to math.min(499,n - 1)
sum = 0.
sumw = 0.
//Compute weighted mean
for j = 0 to math.min(499,n - 1)
w = gauss(i - j, h)
sum += src[j] * w
sumw += w
y2 := sum / sumw
sae += math.abs(src[i] - y2)
nwe.push(y2)
sae := sae / math.min(499,n - 1) * mult
for i = 0 to math.min(499,n - 1)
if i%2
line.new(n-i+1, y1 + sae, n-i, nwe.get(i) + sae, color = upCss)
line.new(n-i+1, y1 - sae, n-i, nwe.get(i) - sae, color = dnCss)
if src[i] > nwe.get(i) + sae and src[i+1] < nwe.get(i) + sae
label.new(n-i, src[i], '▼', color = color(na), style = label.style_label_down, textcolor = dnCss, textalign = text.align_center)
if src[i] < nwe.get(i) - sae and src[i+1] > nwe.get(i) - sae
label.new(n-i, src[i], '▲', color = color(na), style = label.style_label_up, textcolor = upCss, textalign = text.align_center)
y1 := nwe.get(i)
//-----------------------------------------------------------------------------}
//Dashboard
//-----------------------------------------------------------------------------{
var tb = table.new(position.top_right, 1, 1
, bgcolor = #1e222d
, border_color = #373a46
, border_width = 1
, frame_color = #373a46
, frame_width = 1)
if repaint
tb.cell(0, 0, 'Repainting Mode Enabled', text_color = color.white, text_size = size.small)
//-----------------------------------------------------------------------------}
//Plot
//-----------------------------------------------------------------------------}
plot(repaint ? na : out + mae, 'Upper', upCss)
plot(repaint ? na : out - mae, 'Lower', dnCss)
//Crossing Arrows
plotshape(ta.crossunder(close, out - mae) ? low : na, "Crossunder", shape.labelup, location.absolute, color(na), 0 , text = '▲', textcolor = upCss, size = size.tiny)
plotshape(ta.crossover(close, out + mae) ? high : na, "Crossover", shape.labeldown, location.absolute, color(na), 0 , text = '▼', textcolor = dnCss, size = size.tiny)
//-----------------------------------------------------------------------------}