답변완료
Hybrid EMA 지표 변환 문의(트레이딩뷰 자료)
아래와 같이 트레이딩뷰의 Hybrid EMA지표에 대한 소스를 복사를 했습니다.
변환하려고 해 봤는데 이해가 잘 되지 않아 변환 무의 드립니다.
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Uldisbebris
//@version=5
indicator("Hybrid EMA AlgoLearner", shorttitle="Hybrid EMA AlgoLearner", overlay=false)
// Parameters for EMAs
shortTermPeriod = 50
longTermPeriod = 200
// k-NN parameter
k = input.int(5, 'K - Number of neighbors')
// Calculate EMAs
shortTermEma = ta.ema(close, shortTermPeriod)
longTermEma = ta.ema(close, longTermPeriod)
// Custom k-NN Algorithm for weighted EMA
var float[] distances = array.new_float(0)
array.clear(distances)
for i = 1 to 100 by 1 // Loop through past 100 data points
distance = math.abs(shortTermEma - longTermEma[i])
array.push(distances, distance)
array.sort(distances)
k_distances = array.new_float(0)
for i = 0 to k - 1 by 1
array.push(k_distances, array.get(distances, i))
// Calculate weighted EMA based on closest k distances
weightShortTermEma = 0.0
totalWeight = 0.0
for i = 0 to k - 1 by 1
weight = array.get(k_distances, i)
weightShortTermEma += shortTermEma[i] * weight
totalWeight += weight
weightShortTermEma /= totalWeight
// Scale weightShortTermEma between 0 - 100
var float minEma = na
var float maxEma = na
// Instead of all the history, only look at the last N bars.
lookbackPeriod = input.int(400, 'lookbackPeriod')
minEma := ta.lowest(weightShortTermEma, lookbackPeriod)
maxEma := ta.highest(weightShortTermEma, lookbackPeriod)
scaledWeightShortTermEma = (weightShortTermEma - minEma) / (maxEma - minEma) * 100
//== plot
emaplot = plot(scaledWeightShortTermEma, title='Scaled Weighted Short-Term EMA', color = color.new(#a6a8a3, 0), linewidth = 1)
midLinePlot = plot(50, color = na, editable = false, display = display.none)
// Fill between plots and add horizontal lines
fill(emaplot, midLinePlot, 105, 85, top_color = color.new(#057ec4, 0), bottom_color = color.new(#6ca800, 100), title = "Overbought Gradient Fill")
fill(emaplot, midLinePlot, 15, -5, top_color = color.new(#a83c91, 100), bottom_color = color.new(#fcf801, 0), title = "Oversold Gradient Fill")
hline(15, color = color.new(#8b3131, 50))
hline(50, color = color.new(color.gray, 49))
hline(85, color = color.new(#2c5c2e, 50))
2024-05-20
948
글번호 179697
지표
답변완료
data2 색상 수식추가
늘 감사합니다.
아래 수식에 색상 수식을 추가해 주세요.
+5000일대 빨강색, -5000일때 파란색, 두께는 조절가능.
-------
input : 색상(Red),굵기(1);
var : tl(0);
if crossup(data2(c),5000) Then
{
var1 = C;
tl = TL_new(sDate,sTime,var1,NextBarSdate,NextBarStime,var1);
TL_SetColor(tl,색상);
TL_SetSize(tl,굵기);
PlaySound("C:₩예스트레이더₩data₩Sound₩alert.wav");
}
if CrossDown(data2(c),-5000) Then
{
var1 = C;
tl = TL_new(sDate,sTime,var1,NextBarSdate,NextBarStime,var1);
TL_SetColor(tl,색상);
TL_SetSize(tl,굵기);
PlaySound("C:₩예스트레이더₩data₩Sound₩alert.wav");
}
Plot13(var1, "방향");
---------------
감사합니다.
2024-05-19
574
글번호 179690
지표
답변완료
수식 문의드립니다.
안녕하세요.
일전에 만들어주신 수식인데요..
차트에 장착해보니 Plot3만 보이고 나머지 1,2,4가 보이질 않습니다.
Ai에게 물어보니
1. plot1, plot2, plot4 함수 호출 시기
plot1, plot2, plot4 함수는 매 바(bar)마다 호출되어야 하지만, 현재 코드에서는 longtsl, shorttsl, mid 값이 변경될 때만 호출되고 있습니다. 따라서 이들 값이 변경되지 않으면 차트에 표시되지 않습니다.
2. active 변수 사용
plot1, plot2, plot4 함수에서 active 변수를 사용하여 조건부 색상을 지정하고 있습니다. 하지만 active 변수는 longtsl이 변경될 때만 업데이트되므로, shorttsl이 변경되어도 차트 색상이 변경되지 않습니다.
해결 방법
1.plot1, plot2, plot4 함수를 매 바마다 호출하도록 수정합니다.
2.active 변수 대신 pos 변수를 사용하여 조건부 색상을 지정합니다.
이렇게 답변을 하길래 혼자 이래저래 해봤는데 잘 않됩니다.
죄송하지만 선 좀 보일수 있게 다시 한번 검토 부탁드립니다.
아래는 작성해주신 수식입니다.
input : stp("Auto");
input : prd(20);
input : perc(0.5);
input : bias("Auto");
input : upcol(Green);
input : dncol(Red);
input : upcol1(lime);
input : dncol1(maroon);
var : b(0),hi(0),lo(0),ph(0),pl(0),phl(0),pll(0),dir(0);
var : cnt(0);
Array : peaks[1000](0);
b = index;
hi = highest(high,prd*10);
lo = lowest(low,prd*10);
ph = iff(NthHighestBar(1,high, prd) == 0 , high , ph);
pl = iff(NthLowestBar(1,low, prd) == 0 , low , pl);
phL = iff(NthHighestBar(1,high, prd) == 0 , b , phL);
plL = iff(NthLowestBar(1,low, prd) == 0 , b , plL);
dir = iff(phL>plL , 1 , -1);
if dir!=dir[1] Then
{
In*sertArray(peaks, 0, iff(dir>0 , (ph[1]-pl)/ph[1] , (ph-pl[1])/pl[1])) ; //*삭제 (장착시에는 * 삭제했습니다)
cnt = cnt+1;
}
var : autocalc(0),SwitchTrue(0),SwitchFalse(0);
autocalc = iff(cnt == 0 ,0 , MedianArray(peaks, cnt));
if stp == "Auto" Then
{
SwitchTrue = low-(low*autocalc);
Switchfalse = high+(high*autocalc);
}
if stp == "Percentage" Then
{
SwitchTrue = low-(low*(perc/100));
Switchfalse = high+(high*(perc)/100);
}
if stp == "Pivot" Then
{
SwitchTrue = pl;
Switchfalse = ph;
}
var : longtsl(0),shorttsl(0),pos(0);
if low<=longtsl Then
{
longtsl = Switchtrue;
shorttsl = Switchfalse;
pos = -1;
}
if high>=shorttsl then
{
longtsl = Switchtrue;
shorttsl = Switchfalse;
pos = 1;
}
pos = iff(bias=="Bullish",1, IFf(bias=="Bearish",-1,pos));
var : active(False),mid(0);
active = longtsl==longtsl[1];
plot1(longtsl,"ltl",iff(active,IFF(pos>0,dncol,upcol),Black));
plot2(shorttsl,"stl",iff(active,iff(pos>0,upcol,dncol),Black));
plot3(close,"c");
mid = Avg(longtsl,shorttsl);
plot4(mid,"Mid",iff(active,rgb(125, 196, 255),black));
2024-05-19
867
글번호 179687
지표