예스스탁
예스스탁 답변
2025-04-11 15:53:10
안녕하세요
예스스탁입니다.
input : atrMult(4.5);
input : atrlen(12);
input : rsmlen(3);
input : tplen(14);
var : i(0),sum(0),source(0);
var : alpha(0),atrValue1(0),upperLevel(0),lowerLevel(0);
var : previousLowerLevel(0),previousUpperLevel(0);
var : trendDirection(Nan),superTrendValue(Nan);
var : previousTrend(0);
if CurrentBar > 1 Then
{
sum = 0;
for i = 0 to rsmlen-1
{
sum = sum + c[i]*c[i];
}
source = sqrt(sum/rsmlen);
alpha = 1 / atrlen ;
atrValue1 = IFf(IsNan(atrValue1[1]) == true, ma(TrueRange,atrlen) , alpha * TrueRange + (1 - alpha) * IFf(isnan(atrValue1[1])==true,0,atrValue1[1]));
upperLevel = source + atrMult * atrValue1;
lowerLevel = source - atrMult * atrValue1;
previousLowerLevel = iff(isnan(lowerLevel[1])==true,0,lowerLevel[1]);
previousUpperLevel = iff(isnan(upperLevel[1])==true,0,upperLevel[1]);
// Ensure continuity of lower and upper bands
lowerLevel = iff(lowerLevel > previousLowerLevel or source[1] < previousLowerLevel , lowerLevel , previousLowerLevel);
upperLevel = iff(upperLevel < previousUpperLevel or source[1] > previousUpperLevel , upperLevel , previousUpperLevel);
// Determine direction and SuperTrend
previousTrend = superTrendValue[1];
// Initialize direction
if IsNan(atrValue1[1]) == true Then
trendDirection = 1;
else if previousTrend == previousUpperLevel Then
trendDirection = iff(source > upperLevel , -1 , 1);
else
trendDirection = iff(source < lowerLevel , 1 , -1);
// Set SuperTrend value based on direction
superTrendValue = iff(trendDirection == -1 , lowerLevel , upperLevel);
}
plot1(superTrendValue,"ST", iff(trendDirection > 0 , red,green));
Plot2((C+O)/2,"bodyMiddle");
var : dist(0),chg(0),lvlCol(0);
var : tp1(0),tp2(0),tp3(0),tp4(0),tp5(0),tp6(0),tp7(0);
var : printedtp1(0),printedtp2(0),printedtp3(0),printedtp4(0),printedtp5(0),printedtp6(0),printedtp7(0);
dist = abs(close-superTrendValue);
lvlCol = iff(trendDirection > 0 , red , green);
var : key1(0);
var : key2(0);
var : key3(0);
var : key4(0);
var : key5(0);
var : key6(0);
var : key7(0);
if CrossUp(trendDirection, 0) or CrossDown(trendDirection, 0) Then
{
//TL_Delete(keys);
printedtp1 = 0;
printedtp2 = 0;
printedtp3 = 0;
printedtp4 = 0;
printedtp5 = 0;
printedtp6 = 0;
printedtp7 = 0;
chg = abs(superTrendValue-superTrendValue[1]);
tp1 = superTrendValue[1] + IFF(trendDirection > 0 , -chg , chg);
tp2 = superTrendValue[1] + IFF(trendDirection > 0 , -chg * 2 , chg * 2);
tp3 = superTrendValue[1] + IFF(trendDirection > 0 , -chg * 3 , chg * 3);
tp4 = superTrendValue[1] + IFF(trendDirection > 0 , -chg * 4 , chg * 4);
tp5 = superTrendValue[1] + IFF(trendDirection > 0 , -chg * 5 , chg * 5);
tp6 = superTrendValue[1] + IFF(trendDirection > 0 , -chg * 6 , chg * 6);
tp7 = superTrendValue[1] + IFF(trendDirection > 0 , -chg * 7 , chg * 7);
key1 = TL_new(sdate[1],stime[1],tp1, sDate,sTime, tp1);
TL_SetColor(Key1,lvlCol);
TL_SetSize(Key1,2);
printedtp1 = 1;
}
Else
TL_SetEnd(key1,sDate,sTime,TP1);
var : u(0),d(0),alp(0),ur(0),dr(0),rs(0),R(0),tp(False);
u = max(dist - dist[1], 0);
d = max(dist[1] - dist, 0);
alp = 1/tplen;
ur = IFf(IsNan(ur[1]) == true, ma(u,tplen) , alp * u + (1 - alp) * IFf(isnan(ur[1])==true,0,ur[1]));
dr = IFf(IsNan(dr[1]) == true, ma(d,tplen) , alp * d + (1 - alp) * IFf(isnan(dr[1])==true,0,dr[1]));
rs = ur / dr;
R = 100 - 100 / (1 + rs);
tp = CrossDown(R, 60);
var : extreme(0);
var : extreme_tp1_dist(0);
var : extreme_tp2_dist(0);
var : extreme_tp3_dist(0);
var : extreme_tp4_dist(0);
var : extreme_tp5_dist(0);
var : extreme_tp6_dist(0);
var : extreme_tp7_dist(0);
extreme = iff(trendDirection > 0 , low , high);
extreme_tp1_dist = abs(extreme - tp1);
extreme_tp2_dist = abs(extreme - tp2);
extreme_tp3_dist = abs(extreme - tp3);
extreme_tp4_dist = abs(extreme - tp4);
extreme_tp5_dist = abs(extreme - tp5);
extreme_tp6_dist = abs(extreme - tp6);
extreme_tp7_dist = abs(extreme - tp7);
var : tx(0);
if tp and trendDirection > 0 Then
{
tx = Text_New(sDate,sTime,L,"X");
Text_SetStyle(tx,2,0);
Text_SetColor(tx,Red);
Text_SetSize(tx,20);
Text_SetBold(tx,1);
}
if tp and trendDirection < 0 Then
{
tx = Text_New(sDate,sTime,H,"X");
Text_SetStyle(tx,2,1);
Text_SetColor(tx,Green);
Text_SetSize(tx,20);
Text_SetBold(tx,1);
}
if printedtp2 == 0 and extreme_tp2_dist < extreme_tp1_dist Then
{
key2 = TL_new(sdate[1],stime[1],tp2, sDate,sTime, tp2);
TL_SetColor(Key2,lvlCol);
TL_SetSize(Key2,2);
printedtp2 = 1;
}
Else
{
if printedtp2 == 1 Then
TL_SetEnd(key2,sDate,sTime,TP2);
}
if printedtp3 == 0 and extreme_tp3_dist < extreme_tp2_dist Then
{
key3 = TL_new(sdate[1],stime[1],tp3, sDate,sTime, tp3);
TL_SetColor(Key3,lvlCol);
TL_SetSize(Key3,2);
printedtp3 = 1;
}
Else
{
if printedtp3 == 1 Then
TL_SetEnd(key3,sDate,sTime,TP3);
}
if printedtp4 == 0 and extreme_tp4_dist < extreme_tp3_dist Then
{
key4 = TL_new(sdate[1],stime[1],tp4, sDate,sTime, tp4);
TL_SetColor(Key4,lvlCol);
TL_SetSize(Key4,2);
printedtp4 = 1;
}
Else
{
if printedtp4 == 1 Then
TL_SetEnd(key4,sDate,sTime,TP4);
}
if printedtp5 == 0 and extreme_tp5_dist < extreme_tp4_dist Then
{
key5 = TL_new(sdate[1],stime[1],tp5, sDate,sTime, tp5);
TL_SetColor(Key5,lvlCol);
TL_SetSize(Key5,2);
printedtp5 = 1;
}
Else
{
if printedtp5 == 1 Then
TL_SetEnd(key5,sDate,sTime,TP5);
}
if printedtp6 == 0 and extreme_tp6_dist < extreme_tp5_dist Then
{
key6 = TL_new(sdate[1],stime[1],tp6, sDate,sTime, tp6);
TL_SetColor(Key6,lvlCol);
TL_SetSize(Key6,2);
printedtp6 = 1;
}
Else
{
if printedtp6 == 1 Then
TL_SetEnd(key6,sDate,sTime,TP6);
}
if printedtp7 == 0 and extreme_tp7_dist < extreme_tp6_dist Then
{
key7 = TL_new(sdate[1],stime[1],tp7, sDate,sTime, tp7);
TL_SetColor(Key7,lvlCol);
TL_SetSize(Key7,2);
printedtp7 = 1;
}
Else
{
if printedtp7 == 1 Then
TL_SetEnd(key7,sDate,sTime,TP7);
}
즐거운 하루되세요
> 이글루 님이 쓴 글입니다.
> 제목 : 수식변환 부탁드립니다.
> 안녕하세요
indicator("Power Root SuperTrend [AlgoAlpha]", "AlgoAlpha - Power Root", true, max_lines_count = 500)
import TradingView/ta/8
atrMult = input.float(4.5, "Factor")
atrlen = input.int(12, "ATR Length")
rsmlen = input.int(3, "Root-Mean-Square Length")
tplen = input.int(14, "RSI Take-Profit Length")
green = input.color(#00ffbb, "Bullish Color", group = "Appearance")
red = input.color(#ff1100, "Bearish Color", group = "Appearance")
// SuperTrend Function
superTrendCalc(multiplier, atrLength, source) =>
atrValue1 = ta.atr(atrLength)
upperLevel = source + multiplier * atrValue1
lowerLevel = source - multiplier * atrValue1
previousLowerLevel = nz(lowerLevel[1])
previousUpperLevel = nz(upperLevel[1])
// Ensure continuity of lower and upper bands
lowerLevel := lowerLevel > previousLowerLevel or source[1] < previousLowerLevel ? lowerLevel : previousLowerLevel
upperLevel := upperLevel < previousUpperLevel or source[1] > previousUpperLevel ? upperLevel : previousUpperLevel
// Determine direction and SuperTrend
int trendDirection = na
float trendValue = na
previousTrend = trendValue[1]
// Initialize direction
if na(atrValue1[1])
trendDirection := 1
else if previousTrend == previousUpperLevel
trendDirection := source > upperLevel ? -1 : 1
else
trendDirection := source < lowerLevel ? 1 : -1
// Set SuperTrend value based on direction
trendValue := trendDirection == -1 ? lowerLevel : upperLevel
[trendValue, trendDirection]
[superTrendValue, trendDirection] = superTrendCalc(atrMult, atrlen, ta.rms(close, rsmlen))
dist = math.abs(close-superTrendValue)
var chg = 0.0
var tp1 = 0.0
var tp2 = 0.0
var tp3 = 0.0
var tp4 = 0.0
var tp5 = 0.0
var tp6 = 0.0
var tp7 = 0.0
lvlCol = trendDirection > 0 ? red : green
var keys = array.new_line()
var printedtp1 = 0
var printedtp2 = 0
var printedtp3 = 0
var printedtp4 = 0
var printedtp5 = 0
var printedtp6 = 0
var printedtp7 = 0
if ta.cross(trendDirection, 0)
keys.clear()
printedtp1 := 0
printedtp2 := 0
printedtp3 := 0
printedtp4 := 0
printedtp5 := 0
printedtp6 := 0
printedtp7 := 0
chg := math.abs(superTrendValue-superTrendValue[1])
tp1 := superTrendValue[1] + (trendDirection > 0 ? -chg : chg)
tp2 := superTrendValue[1] + (trendDirection > 0 ? -chg * 2 : chg * 2)
tp3 := superTrendValue[1] + (trendDirection > 0 ? -chg * 3 : chg * 3)
tp4 := superTrendValue[1] + (trendDirection > 0 ? -chg * 4 : chg * 4)
tp5 := superTrendValue[1] + (trendDirection > 0 ? -chg * 5 : chg * 5)
tp6 := superTrendValue[1] + (trendDirection > 0 ? -chg * 6 : chg * 6)
tp7 := superTrendValue[1] + (trendDirection > 0 ? -chg * 7 : chg * 7)
keys.push(line.new(bar_index[1], tp1, bar_index, tp1, color = lvlCol, width = 2))
printedtp1 := 1
tp = ta.crossunder(ta.rsi(dist, tplen), 60)
extreme = trendDirection > 0 ? low : high
extreme_tp1_dist = math.abs(extreme - tp1)
extreme_tp2_dist = math.abs(extreme - tp2)
extreme_tp3_dist = math.abs(extreme - tp3)
extreme_tp4_dist = math.abs(extreme - tp4)
extreme_tp5_dist = math.abs(extreme - tp5)
extreme_tp6_dist = math.abs(extreme - tp6)
extreme_tp7_dist = math.abs(extreme - tp7)
p = plot(superTrendValue, color = trendDirection > 0 ? color.new(red, 70) : color.new(green, 70))
upTrend = plot(close > superTrendValue ? superTrendValue : na, color = color.new(green, 70), style = plot.style_linebr) //, force_overlay = true
downTrend = plot(close < superTrendValue ? superTrendValue : na, color = color.new(red, 70), style = plot.style_linebr, force_overlay = false) //, force_overlay = true
bodyMiddle = plot(barstate.isfirst ? na : (open + close) / 2, "Body Middle",display = display.none)
fill(bodyMiddle, upTrend, (open + close) / 2, superTrendValue, color.new(green, 95), color.new(green, 70))
fill(bodyMiddle, downTrend, superTrendValue, (open + close) / 2, color.new(red, 70), color.new(red, 95))
plotchar(tp and trendDirection > 0, "RSI-Based Shorts TP", "X", location.belowbar, red, size = size.tiny)
plotchar(tp and trendDirection < 0, "RSI-Based Longs TP", "X", location.abovebar, green, size = size.tiny)
if printedtp2 == 0 and extreme_tp2_dist < extreme_tp1_dist
keys.push(line.new(bar_index[1], tp2, bar_index, tp2, color = lvlCol, width = 2))
printedtp2 := 1
if printedtp3 == 0 and extreme_tp3_dist < extreme_tp2_dist
keys.push(line.new(bar_index[1], tp3, bar_index, tp3, color = lvlCol, width = 2))
printedtp3 := 1
if printedtp4 == 0 and extreme_tp4_dist < extreme_tp3_dist
keys.push(line.new(bar_index[1], tp4, bar_index, tp4, color = lvlCol, width = 2))
printedtp4 := 1
if printedtp5 == 0 and extreme_tp5_dist < extreme_tp4_dist
keys.push(line.new(bar_index[1], tp5, bar_index, tp5, color = lvlCol, width = 2))
printedtp5 := 1
if printedtp6 == 0 and extreme_tp6_dist < extreme_tp5_dist
keys.push(line.new(bar_index[1], tp6, bar_index, tp6, color = lvlCol, width = 2))
printedtp6 := 1
if printedtp7 == 0 and extreme_tp7_dist < extreme_tp6_dist
keys.push(line.new(bar_index[1], tp7, bar_index, tp7, color = lvlCol, width = 2))
printedtp7 := 1
if keys.size() > 0
aSZ = keys.size()
for i = aSZ - 1 to 0
keys.get(i).set_x2(bar_index)
// Alert when SuperTrend changes direction
alertcondition(ta.cross(trendDirection, 0), title="SuperTrend Direction Change", message="SuperTrend has changed direction")
// Alert when each TP line is drawn
alertcondition(printedtp1 == 1, title="TP1 Line Drawn", message="TP1 line has been drawn")
alertcondition(printedtp2 == 1, title="TP2 Line Drawn", message="TP2 line has been drawn")
alertcondition(printedtp3 == 1, title="TP3 Line Drawn", message="TP3 line has been drawn")
alertcondition(printedtp4 == 1, title="TP4 Line Drawn", message="TP4 line has been drawn")
alertcondition(printedtp5 == 1, title="TP5 Line Drawn", message="TP5 line has been drawn")
alertcondition(printedtp6 == 1, title="TP6 Line Drawn", message="TP6 line has been drawn")
alertcondition(printedtp7 == 1, title="TP7 Line Drawn", message="TP7 line has been drawn")
// Alert for crossing under RSI
alertcondition(tp, title="Take-Profit Condition", message="Take-Profit condition met")