# convert_meta_trail
#https://usethinkscript.com/threads/convert-metastock-sve_trends_trail-trailing-stop.16900/
#Unanswered Convert MetaStock SVE_TRENDS_Trail trailing stop
#{SVE_TRENDS_Trail trailing stop function}
#atrfact:=Input(“ATR multiplication :”,1,10,2.8);
#period:=Input(“ATR Period :”,1,100,10);
#HiLo:=If(H-L<1.5*Mov(H-L,period,S),H-L, 1.5*Mov(H-L,period,S));
#Href:=If(L<=Ref(H,-1),H-Ref(C,-1),(H-Ref(C,-1))-(L-Ref(H,-1))/2);
#Lref:=If(H>=Ref(L,-1),Ref(C,-1)-L,(Ref(C,-1)-L)-(Ref(L,-1)-H)/2);
#diff1:=Max(HiLo,Href);
#diff2:=Max(diff1,Lref);
#ATRmod:=Wilders(diff2,period);
#loss:=atrfact*ATRmod;
#resistance:= C + loss;
#support:=If(L>=Ref(L,-2) AND Ref(L,-1)>=Ref(L,-2) AND Ref(L,-3)>=Ref(L,-2) AND Ref(L,-4)>= Ref(L,-2),Ref(L,-2),If(L>Ref(H,-1)*1.0013,Ref(H,-1)*0.9945,If(L>PREV*1.1,PREV*1.05,PREV)));
#trends:=If(H>PREV AND Ref(H,-1)>PREV,Max(PREV,support),If(H<PREV AND Ref(H,-1)<PREV Min(PREV,resistance),If(H>=PREV,support,resistance)));
def na = double.nan;
def bn = barnumber();
#{SVE_TRENDS_Trail trailing stop function}
input atrfact = 2.8;
input period = 10;
def o = open;
def h = high;
def l = low;
def c = close;
def price = close;
input MA1_len = 3;
input MA1_type = AverageType.SIMPLE;
# Mov(h - l, period, S)
def ma1 = MovingAverage(MA1_type, h - l, period);
def n = 1.5;
#def HiLo = If(h - l < (1.5 * Mov(h - l, period, S)) , (h - l) , (1.5 * Mov(h - l, period, S)));
def HiLo = If(h - l < (n * ma1) , (h - l) , (n * ma1));
def Href = If(l <= GetValue(h, 1), h - GetValue(c, 1), (h - GetValue(c, 1)) - (l - GetValue(h, 1)) / 2);
def Lref = If(h >= GetValue(l, 1), GetValue(c, 1) - l, (GetValue(c, 1) - l) - (GetValue(l, 1) - h) / 2);
def diff1 = Max(HiLo, Href);
def diff2 = Max(diff1, Lref);
#def ATRmod = Wilders(diff2, period);
input MA2_type = AverageType.WILDERS;
def atrmod = MovingAverage(MA2_type, diff2, period);
def loss = atrfact * atrmod;
def resistance = c + loss;
def support = If(l >= GetValue(l, 2) and GetValue(l, 1) >= GetValue(l, 2) and GetValue(l, 3) >= GetValue(l, 2) and GetValue(l, 4) >= GetValue(l, 2), GetValue(l, 2),
If(l > GetValue(h, 1) * 1.0013, GetValue(h, 1) * 0.9945, If(l > support[1] * 1.1, support[1] * 1.05,
support[1])));
def trends = If(H > trends[1] AND getvalue(H,1) > trends[1], Max(trends[1], support),
If(H < trends[1] AND getvalue(H,1) < trends[1], Min(trends[1], resistance),
If(H >= trends[1], support, resistance)
));
plot z1 = support;
z1.SetDefaultColor(Color.green);
#z1.setlineweight(1);
z1.hidebubble();
plot z2 = trends;
z2.SetDefaultColor(Color.cyan);
#z2.setlineweight(1);
z2.hidebubble();
#z2.SetStyle(Curve.MEDIUM_DASH);
#--------------------------
# add arrows on support line
def suppxup = close[1] < support[1] and close[0] > support[0];
def suppxdwn = close[1] > support[1] and close[0] < support[0];
def vert = 0.001;
plot z3 = if suppxup then low*(1-vert) else na;
z3.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
z3.SetDefaultColor(Color.green);
z3.setlineweight(4);
z3.hidebubble();
plot z4 = if suppxdwn then high*(1+vert) else na;
z4.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);
z4.SetDefaultColor(Color.red);
z4.setlineweight(4);
z4.hidebubble();
# yellow warn arrors
def warnup = if close[1] < support[1] and high > support then 1 else 0;
def warndwn = if close[1] > support[1] and low < support then 1 else 0;
plot z5 = if warnup then low*(1-vert) else na;
z5.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
z5.SetDefaultColor(Color.yellow);
z5.setlineweight(3);
z5.hidebubble();
plot z6 = if warndwn then high*(1+vert) else na;
z6.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);
z6.SetDefaultColor(Color.yellow);
z6.setlineweight(3);
z6.hidebubble();
#-----------------------------
# ref stuff
#metastock functions
#https://kumo.swcp.com/stocks/metacorr.htm
# Moving Average
# mov(data, periods, method),
# where method is one of
# SIMPLE(S),
# EXPONENTIAL(E),
# TIMESERIES(T),
# TRIANGULAR(TRI),
# WEIGHTED(W),
# VARIABLE(VAR),
# VOLUMEADJUSTED(VOL)
# prev
# https://www.quantshare.com/item-1194-prev-previous-value-of-the-same-formula#:~:text=In%20Metastock%2C%20the%20%22PREV%22,Value%20of%20this%20Formula%5D%3B
# is the previous value of variable , x[1] is prev value
# def x = x[1] + 1;
#