ATR Trailing Stop without flip state

jb217

New member
Hi, everyone. I am trying to modify the TOS ATR Trailing Stop script so that instead of flipping to a down condition, it just continues to plot the last line until it starts a new up. Does that make sense? I only trade long so I don't care about the downtrend, but I do still want the last place that the line was even if the price is now below there. Is there a simplified version of the script somewhere that will do that? I've been chopping out parts of the script, but it just breaks things.

Reference Picture

Using the reference center picture as an example, in Jan the trailing stop was at $43.50. I want it to continue to plot $43.50 until April when it starts a new trend.
 
Solution
Hi, everyone. I am trying to modify the TOS ATR Trailing Stop script so that instead of flipping to a down condition, it just continues to plot the last line until it starts a new up. Does that make sense? I only trade long so I don't care about the downtrend, but I do still want the last place that the line was even if the price is now below there. Is there a simplified version of the script somewhere that will do that? I've been chopping out parts of the script, but it just breaks things.

Reference Picture

Using the reference center picture as an example, in Jan the trailing stop was at $43.50. I want it to continue to plot $43.50 until April when it starts a new trend.

See if this helps. It plots the state.long as...

rad14733

Well-known member
VIP
@jb217 Based on your description, it wouldn't be a trailing stop but, rather, just a reference line such as a Support and Resistance line based on a previous ATR... How would you come about logic for that without having every point extend...???
 

SleepyZ

Well-known member
Hi, everyone. I am trying to modify the TOS ATR Trailing Stop script so that instead of flipping to a down condition, it just continues to plot the last line until it starts a new up. Does that make sense? I only trade long so I don't care about the downtrend, but I do still want the last place that the line was even if the price is now below there. Is there a simplified version of the script somewhere that will do that? I've been chopping out parts of the script, but it just breaks things.

Reference Picture

Using the reference center picture as an example, in Jan the trailing stop was at $43.50. I want it to continue to plot $43.50 until April when it starts a new trend.

See if this helps. It plots the state.long as green and the extension of it yellow until the next state.long, skipping the plot of the state.short line.

Capture.jpg
Code:
#
# TD Ameritrade IP Company, Inc. (c) 2009-2021
#

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

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 trueRange;
switch (trailType) {
case modified:
    trueRange = Max(HiLo, Max(HRef, LRef));
case unmodified:
    trueRange = TrueRange(high, close, low);
}
def loss = ATRFactor * MovingAverage(averageType, trueRange, 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);

def trailext = if isnan(close) then trailext[1] else if state==state.long then trail else trailext[1];
plot TrailingStop = trailext;

TrailingStop.SetPaintingStrategy(PaintingStrategy.POINTS);
TrailingStop.DefineColor("Buy", GetColor(0));
TrailingStop.DefineColor("Sell", GetColor(1));
TrailingStop.AssignValueColor(if !isnan(close) and state == state.long
    then color.green
    else color.yellow);
 
Solution

jb217

New member
See if this helps. It plots the state.long as green and the extension of it yellow until the next state.long, skipping the plot of the state.short line.
Yes! This is perfect! Thank you!
Trying to understand what it is doing. When the state filps it has no base number to start with so isnan gives it one to start with. From there it continues to print the last bar until the state flips again. Is that correct? I had been trying to reference the previous bar but that would make sense if the flip was clearing it. Why though is Trailext picking up the Trail value instead of the bar close value?
 

SleepyZ

Well-known member
It is just plotting the trailing stop for the state.long. It ignores the trailing stop for state.short and instead plots the last trailing value for state.long until a new state.long occurs. Finallly, the isnan(close) is used to extend the last state.long trailing stop value into the right expansion. I am not sure what you are intending when you want to use the bar close value.
 

Similar threads

Top