SVE_TRENDS_Trail Trailing Stop For ThinkOrSwim

Join useThinkScript to post your question to a community of 21,000+ developers and traders.

From the July 2009 Stocks & Commodities magazine, Sylvain Vervoort, creator of the ATR Trailing Stop, is this short-term trailing stop written for MetaStock. Hoping someone can convert to ThinkScript.

I have the pdf of the article with a full description but not sure if I am allowed to post it. @MerryDay ?


Code:
{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)));
trends

10/12 11pm edit
chg an offset from -1 to 1 , lref


i think this is right, my first time converting a metastock
this draws 2 lines , for support and trend.

i guessed at what might be useful arrows.
..green/red arrows - when close crosses the support line. thinking, if close crosses, then the move might be sustained
..yellow arrows - (warn) when high or low crosses the support line. just part of a candle crossed, keep an eye on it.

converting notes at the end of the code

Code:
# 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;
#

F 5min
LdKH6P0.jpg
 
Last edited:
Thread starter Similar threads Forum Replies Date
S Filthy Trends Fibonacci Indicator for ThinkorSwim Custom 0
samer800 Smooth Trail V2 for ThinkOrSwim Custom 0

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
338 Online
Create Post

Similar threads

Similar threads

The Market Trading Game Changer

Join 2,500+ subscribers inside the useThinkScript VIP Membership Club
  • Exclusive indicators
  • Proven strategies & setups
  • Private Discord community
  • ‘Buy The Dip’ signal alerts
  • Exclusive members-only content
  • Add-ons and resources
  • 1 full year of unlimited support

Frequently Asked Questions

What is useThinkScript?

useThinkScript is the #1 community of stock market investors using indicators and other tools to power their trading strategies. Traders of all skill levels use our forums to learn about scripting and indicators, help each other, and discover new ways to gain an edge in the markets.

How do I get started?

We get it. Our forum can be intimidating, if not overwhelming. With thousands of topics, tens of thousands of posts, our community has created an incredibly deep knowledge base for stock traders. No one can ever exhaust every resource provided on our site.

If you are new, or just looking for guidance, here are some helpful links to get you started.

What are the benefits of VIP Membership?
VIP members get exclusive access to these proven and tested premium indicators: Buy the Dip, Advanced Market Moves 2.0, Take Profit, and Volatility Trading Range. In addition, VIP members get access to over 50 VIP-only custom indicators, add-ons, and strategies, private VIP-only forums, private Discord channel to discuss trades and strategies in real-time, customer support, trade alerts, and much more. Learn all about VIP membership here.
How can I access the premium indicators?
To access the premium indicators, which are plug and play ready, sign up for VIP membership here.
Back
Top