StockFetcher Forums · General Discussion · Average True Range Question?<< >>Post Follow-up
72 posts
msg #112914
Ignore trek
4/23/2013 8:59:48 AM

I know SF has an ATR indicator, but is it possible to convert it to an ATR trailing stop like
ThinkorSwim has?

I copied and pasted the code from TOS if anyone might can convert into SF code.




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);

rec state = {default init, long, short};
rec 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.DefineColor("Buy", GetColor(0));
TrailingStop.DefineColor("Sell", GetColor(1));
TrailingStop.AssignValueColor(if state == state.long
then TrailingStop.color("Sell")
else TrailingStop.color("Buy"));

4,538 posts
msg #112916
Ignore Kevin_in_GA
4/23/2013 10:53:58 AM

SF has a Chandelier exit. That might be helpful for you.

683 posts
msg #112917
Ignore duke56468
4/23/2013 11:53:47 AM

A volatility stop is probably what you want, SF has been asked for this in the past with no response.

21 posts
msg #108161
- Ignore paulwoll 9/24/2012 12:33:19 AM

Is there any way to make this calculation with the filters?
Would like to know when volstop changes to red on a daily chart.
See Volatilty Stop on to see what I am referring to. Usually in an uptrend the dots are green until it turns red.

573 posts
msg #108162 9/24/2012 9:52:26 AM

I too would be very interested in having the volatility stop available on SF. It is similar to the Sylvain Vervoort trailing ATR stop ( ) and both are very helpful in determining entry and exits.

412 posts
msg #112918
4/23/2013 2:17:49 PM

/* stop is 2 average trade ranges away from buy point */

set{stop, atr(20) * 2}
set{buystop, close - stop}
set{risk, stop / close}
set{risk%, risk * 100}

set{wager, 8000. * .0075}
set{equity, wager / risk20}
set{shareqty, equity / close}
set{total, 2000. / close}
set{totals, total * close}

/* the end */

You could adapt this to your needs


4,538 posts
msg #112919
Ignore Kevin_in_GA
4/23/2013 2:27:25 PM

I'm fairly sure this is what you are asking for ...


Parameters Period
Usage Chandelier Exit(22,2.5)

Description The Chandelier Exit (developed by Chuck LeBeau) is a volatility measure using the ATR to help set stop limits. The exit is computed by finding the highest value over the period and then subtracting a multiple of the ATR for that period. On StockFetcher, the position parameter is the ATR multiplier used to find the actual stop position.

Fetcher[show stocks where close is between 10 and 50 and draw Chandelier Exit(22,2.5)]

Fetcher[show stocks where close crossed below Chandelier Exit(22,2.5)]

72 posts
msg #112937
Ignore trek
4/24/2013 4:58:10 PM

Thanks to all those that responded.

I had looked at the Chandelier exit as well, but does not give the same
value as TOS ATR trailing stop. I don't know how to post the chart from TOS showing
this, but the values are different.

For example today on NFLX, TOS atr trail stop(10,2.5) is 191.51
while SF Chandelier exit(10,2.5) is 193.56

Thanks again for everyone's suggestions,


31 posts
msg #112997
Ignore paulwoll
4/26/2013 6:59:58 PM

Yes... I want the volatility stop. Annoys me I have to use the Cnandelier exit as I get a lot of false results in my scans. Focus on that rather than messing up the thumbnail flash view.

68 posts
msg #113007
Ignore miko
4/27/2013 4:17:39 PM

I think the difference is that the Chandelier exit is based on the high versus the TOS ATR trailing stop based on the close. So the Chandelier Exit almost always will be higher than the TOS. I thought I'd be able to reconcile the numbers in trek's example, but couldn't, maybe we have data differences.

683 posts
msg #114563
Ignore duke56468
7/19/2013 10:23:34 PM

Any hope for getting a volatility stop yet SF????

StockFetcher Forums · General Discussion · Average True Range Question?<< >>Post Follow-up

*** Disclaimer *** does not endorse or suggest any of the securities which are returned in any of the searches or filters. They are provided purely for informational and research purposes. does not recommend particular securities., Vestyl Software, L.L.C. and involved content providers shall not be liable for any errors or delays in the content, or for any actions taken based on the content.

Copyright 2016 - Vestyl Software L.L.C.Terms of Service | License | Questions or comments? Contact Us
EOD Data sources: DDFPlus & CSI Data Quotes delayed during active market hours. Delay times are at least 15 mins for NASDAQ, 20 mins for NYSE and Amex. Delayed intraday data provided by DDFPlus