NexusFi: Find Your Edge


Home Menu

 





ATR Trailing Stop: Convert Thinkscript to Easylanguage


Discussion in MultiCharts

Updated
      Top Posters
    1. looks_one sptrader with 2 posts (4 thanks)
    2. looks_two Loop with 2 posts (0 thanks)
    3. looks_3 kamisyed with 1 posts (0 thanks)
    4. looks_4 ABCTG with 1 posts (0 thanks)
    1. trending_up 11,294 views
    2. thumb_up 4 thanks given
    3. group 6 followers
    1. forum 7 posts
    2. attach_file 0 attachments




 
Search this Thread
  #1 (permalink)
kamisyed
Abu Dhabi + United Arab Emirates
 
Posts: 2 since Oct 2013
Thanks Given: 0
Thanks Received: 0

Hi, I'm new to easylanguage and having a really rough time changing over from ToS to Multicharts. I dont use any complex strategies and have never had to program in thinkscript so I'm generally new to programming as well.

I was hoping someone could please please help me convert this Thinkscript code for an ATR trailing stop to Easylanguage for multicharts.

I am specifically looking for an Indicator that will plot the ATR stops on my chart regardless of whether or not I have an actual market position (For instance, ToS assumes I have a hypothetical long when starting the plot and this is fine; i.e. no need for an actual position). I am not looking for a signal as these are already programmed into multicharts but for some reason dont plot.

Alternatively, since the below code has customization for modified ATR stops aswell which I'm not interested in, if anyone has their own version of a standard ATR Trailing stop indicator I would really appreciate that as well!

Please help!


input trailType = {default modified, unmodified};
input ATRPeriod = 5;
input ATRFactor = 3.5;
input firstTrade = {default long, short};

assert(ATRFactor > 0, "'atr factor' must be positive: " + ATRFactor);

def HiLo = Min(high - low, 1.5 * Average(high - low, ATRPeriod));
def HRef = if low <= high[1]
then high - close[1]
else (high - close[1]) - 0.5 * (low - high[1]);
def LRef = if high >= low[1]
then close[1] - low
else (close[1] - low) - 0.5 * (low[1] - high);
def ATRMod = ExpAverage(Max(HiLo, Max(HRef, LRef)), 2 * ATRPeriod - 1);

def loss;
switch (trailType) {
case modified:
loss = ATRFactor * ATRMod;
case unmodified:
loss = ATRFactor * AvgTrueRange(high, close, low, ATRPeriod);
}

def state = {default init, long, short};
def trail;
switch (state[1]) {
case init:
if (!IsNaN(loss)) {
switch (firstTrade) {
case long:
state = state.long;
trail = close - loss;
case short:
state = state.short;
trail = close + loss;
}
} else {
state = state.init;
trail = Double.NaN;
}
case long:
if (close > trail[1]) {
state = state.long;
trail = Max(trail[1], close - loss);
}
else {
state = state.short;
trail = close + loss;
}
case short:
if (close < trail[1]) {
state = state.short;
trail = Min(trail[1], close + loss);
}
else {
state = state.long;
trail = close - loss;
}
}

def BuySignal = Crosses(state == state.long, 0, CrossingDirection.Above);
def SellSignal = Crosses(state == state.short, 0, CrossingDirection.Above);

plot TrailingStop = trail;

TrailingStop.SetPaintingStrategy(PaintingStrategy.POINTS);
TrailingStop.DefineColor("Buy", GetColor(0));
TrailingStop.DefineColor("Sell", GetColor(1));
TrailingStop.AssignValueColor(if state == state.long
then TrailingStop.color("Sell")


Reply With Quote

Can you help answer these questions
from other members on NexusFi?
ATFX Suspends Prop Trading Unit ATFunded -- Full Review …
Funded Trading Evaluation Firms
Warsh Drops Easing Bias, December Hike Now Above 50% -- …
Prediction Markets & Event Contracts
Four New E-mini Futures Launch June 29 -- Russell 3000, …
Emini and Emicro Index
Iran and Israel Stand Down: What $9M in Peace Contracts …
Prediction Markets & Event Contracts
Fabrication or Framework? Irans Denied MOU Explains the …
Prediction Markets & Event Contracts
 
Best Threads (Most Thanked)
in the last 7 days on NexusFi
NexusFi site changelog and issues/problem reporting
16 thanks
Darmok and Jalad at Tanagra
3 thanks
CME Expands 24/7 Trading to WTI Crude Oil and Gold -- We …
1 thanks
Big Mike in Ecuador
1 thanks
30 Sessions
1 thanks
  #2 (permalink)
crazybobo1
cumming Georgia
 
Posts: 1 since Aug 2014
Thanks Given: 1
Thanks Received: 0

I am trying to find the same thing. I have looked everywhere for ATR trailing stop indicator for tradestation, and can only find a strategy, not an indicator.


Reply With Quote
  #3 (permalink)
 
sptrader's Avatar
 sptrader 
Colorado
 
Experience: Advanced
Platform: MultiCharts
Broker: IB & Iqfeed
Trading: ES , CL
Posts: 535 since Apr 2010
Thanks Given: 1,097
Thanks Received: 702


Here is a basic ATR stop indicator.. for multicharts (not tested in TS), not sure who the author is either...

 
Code
Inputs: Price(Close),atrPeriod(14),atrMult(1.75);
    
Vars:
    atr(0), ts(0), shstp(0), lgstp(0), trend(0), TriggerPrice(0), dir(0);
    
atr = AvgTrueRange(AtrPeriod);

lgstp = Price - atr * atrMult;
shstp = Price + atr * atrMult;

TriggerPrice = close; 

if TriggerPrice >= ts[1] then
    trend = 1
else if TriggerPrice < ts[1] then
    trend = -1;

if trend > 0 then
    if trend[1] < 0 then ts=lgstp[0]
    else if lgstp >= ts[1] then ts=lgstp[0]
    else ts=ts[1];

if trend < 0 then
    if trend[1] > 0 then ts=shstp[0]
    else if shstp <= ts[1] then ts=shstp[0]
    else ts=ts[1];

Plot1(ts,"Long Stop");
Plot2(ts,"Short Stop");
Plot3(ts,"ATR Stop",iff(trend = 1,GetPlotColor(1),GetPlotColor(2)));


Reply With Quote
  #4 (permalink)
 Loop 
Sydney
 
Experience: Intermediate
Platform: Multichart
Trading: Gold
Posts: 26 since Aug 2016
Thanks Given: 22
Thanks Received: 4

How do I turn code below into system so it buys/sells when it changes direction?

And what does ts in the code mean?

Many thanxx

Loop















sptrader View Post
Here is a basic ATR stop indicator.. for multicharts (not tested in TS), not sure who the author is either...

 
Code
Inputs: Price(Close),atrPeriod(14),atrMult(1.75);
    
Vars:
    atr(0), ts(0), shstp(0), lgstp(0), trend(0), TriggerPrice(0), dir(0);
    
atr = AvgTrueRange(AtrPeriod);

lgstp = Price - atr * atrMult;
shstp = Price + atr * atrMult;

TriggerPrice = close; 

if TriggerPrice >= ts[1] then
    trend = 1
else if TriggerPrice < ts[1] then
    trend = -1;

if trend > 0 then
    if trend[1] < 0 then ts=lgstp[0]
    else if lgstp >= ts[1] then ts=lgstp[0]
    else ts=ts[1];

if trend < 0 then
    if trend[1] > 0 then ts=shstp[0]
    else if shstp <= ts[1] then ts=shstp[0]
    else ts=ts[1];

Plot1(ts,"Long Stop");
Plot2(ts,"Short Stop");
Plot3(ts,"ATR Stop",iff(trend = 1,GetPlotColor(1),GetPlotColor(2)));


Reply With Quote
  #5 (permalink)
 
sptrader's Avatar
 sptrader 
Colorado
 
Experience: Advanced
Platform: MultiCharts
Broker: IB & Iqfeed
Trading: ES , CL
Posts: 535 since Apr 2010
Thanks Given: 1,097
Thanks Received: 702

It's NOT my code so my "Guess" would be that TS stands for "Trailing Stop"...

Here is a simple conversion from the stop indicator to a basic system...(untested and not my code).

 
Code
Inputs: Price(Close),atrPeriod(14),atrMult(1.75);
    
Vars:
    atr(0), ts(0), shstp(0), lgstp(0), trend(0), TriggerPrice(0), dir(0);
    
atr = AvgTrueRange(AtrPeriod);

lgstp = Price - atr * atrMult;
shstp = Price + atr * atrMult;

TriggerPrice = close; 

if TriggerPrice >= ts[1] then
    trend = 1
else if TriggerPrice < ts[1] then
    trend = -1;

if trend > 0 then
    if trend[1] < 0 then ts=lgstp[0]
    else if lgstp >= ts[1] then ts=lgstp[0]
    else ts=ts[1];

if trend < 0 then
    if trend[1] > 0 then ts=shstp[0]
    else if shstp <= ts[1] then ts=shstp[0]
    else ts=ts[1];

if trend[1] < 0 and trend >= 0 then buy next bar at market;
if trend[1] > 0 and trend < 0 then sellshort next bar at market;


Reply With Quote
Thanked by:
  #6 (permalink)
 Loop 
Sydney
 
Experience: Intermediate
Platform: Multichart
Trading: Gold
Posts: 26 since Aug 2016
Thanks Given: 22
Thanks Received: 4

Thank you!!!

Do you know where I can find more code like this?


Reply With Quote
  #7 (permalink)
 ABCTG   is a Vendor
 
Posts: 2,448 since Apr 2013
Thanks Given: 494
Thanks Received: 1,643

Loop,

you can find a lot via google for example. Multicharts also has a lot build in and a knowledge-base with code on their website as well as the forum.

Besides that the elite section here on NexusFi offers tools for Multicharts for download, too.

Regards,

ABCTG


Loop View Post
Thank you!!!

Do you know where I can find more code like this?


Follow me on X Reply With Quote
  #8 (permalink)
Hawkeye1
Cleveland Ohio
 
Posts: 8 since Feb 2017
Thanks Given: 0
Thanks Received: 0

thanks for the code


Reply With Quote




Last Updated on March 31, 2017


© 2026 NexusFi®, s.a., All Rights Reserved.
Av Ricardo J. Alfaro, Century Tower, Panama City, Panama, Ph: +507 833-9432 (Panama and Intl), +1 888-312-3001 (USA and Canada)
All information is for educational use only and is not investment advice. There is a substantial risk of loss in trading commodity futures, stocks, options and foreign exchange products. Past performance is not indicative of future results.
About Us - Contact Us - Site Rules, Acceptable Use, and Terms and Conditions - Downloads - Top
no new posts