# Moving Average with ATR Trailing Stop Strategy For ThinkOrSwim

#### TMac

##### New member
I am using a double moving average indicator and offset which I created myself. I would like to increase the reliability of the indicator by adding a second condition before a buy signal is produced. For example, a buy signal will not be produced if the ATR trailing stop is indicating downward movement. In the screenshot below these would be the second and third long entry orders.
The two indicators I am using are

Moving Average Indicator Strategy
and
ATR Trailing Stop.

Any suggestions or help is greatly appreciated!!
https://share.icloud.com/photos/024ZB5kuU6DwtvoNESCh1RH1A

Last edited by a moderator:

#### congtran1188

##### New member
check this out. order open when ema cross above/below and atr above/below trailing. close when either condition is not met
Ruby:
``````###################################
# Moving Average with ATR Trailing Stop Strategy
# @congtran1188
# requested by @TMac
input price = close;
input fastLength = 20;
input slowLength = 50;
input displace_fast = 0;
input displace_slow = 0;
input averageType = AverageType.EXPONENTIAL;

plot FastMA = MovingAverage(averageType, price, fastLength)[-displace_fast];
plot SlowMA = MovingAverage(averageType, price, slowLength)[-displace_slow];
FastMA.SetDefaultColor(GetColor(1));
SlowMA.SetDefaultColor(GetColor(2));

def ema1 = expaverage(close,8);
def ema2 = expaverage(close,20);
def crossup = fastma > slowma;
def crossdn = fastma < slowma;

def countup = if crossup and !crossup[1] then 1 else countup[1]+1;
def countdn = if crossdn and !crossdn[1] then 1 else countdn[1]+1;

addlabel(1, if crossup then "Buy, " + countup
else if crossdn then "Sell, " + countdn
else "Equal", color.black);
assignpricecolor(if crossup then color.green
else if crossdn then color.red
else color.gray);
####################################

#ATR Trailing Stop.
####################################
#
# TD Ameritrade IP Company, Inc. (c) 2009-2022
#

input trailType = {default modified, unmodified};
input ATRPeriod = 5;
input ATRFactor = 3.5;
input firstTrade = {default long, short};
input averageType1 = 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(averageType1, trueRange, ATRPeriod);

def state = {default init, long, short};
def trail;
switch (state[1]) {
case init:
if (!IsNaN(loss)) {
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("Sell", GetColor(1));
TrailingStop.AssignValueColor(if state == state.long
then TrailingStop.Color("Sell")
##################################

AssignPriceColor( if crossup and close<trail then color.white
else if crossdn and close>trail then color.white
else if crossup and close>trail then color.green
else if crossdn and close<trail then color.red else Color.black);

AddOrder(OrderType.BUY_AUTO, crossup and close>trail, tickcolor = Color.CYAN, arrowcolor = Color.CYAN, name = "BUY");
AddOrder(OrderType.SELL_TO_CLOSE, crossup and close<trail or crossdn and close>trail, tickcolor = Color.MAGENTA, arrowcolor = Color.MAGENTA, name = "Sell To Close");

################################################################
########## SELL ###########
################################################################

AddOrder(OrderType.SELL_AUTO, crossdn and close<trail , tickcolor = Color.MAGENTA, arrowcolor = Color.MAGENTA, name = "SELL");
AddOrder(OrderType.BUY_TO_CLOSE, crossup and close<trail or crossdn and close>trail , tickcolor = Color.CYAN, arrowcolor = Color.CYAN, name = "Buy To Close");``````

Last edited by a moderator:

#### congtran1188

##### New member
what is the parameter that you are using? i can work around to make it work

#### TMac

##### New member
what is the parameter that you are using? i can work around to make it work
That last version works great! thank you!

#### congtran1188

##### New member
this will only trade on trading hour. for some reason i dont find it profitable. what is you setting?
Ruby:
``````###############################################################
########## Initial Config #########
################################################################
input securityType = { default "stocks", "forex"};
input tradetype = { "long", "short", default "both" };
input mode = { default "superScalp", "normal"};
input configMode = { default "automatic", "manual"};
input timeMode = { default "shorter", "normal"};

def timeFrame = GetAggregationPeriod();
def lowestAggregation;
def middleAggregation;
def highestAggregation;
def extraHighAggregation;
def xxlAggregation;
def xxxlAggregation;
if timeFrame == AggregationPeriod.MIN and timeMode == timeMode.shorter {
lowestAggregation = AggregationPeriod.MIN;
middleAggregation = AggregationPeriod.TWO_MIN;
highestAggregation = AggregationPeriod.FOUR_MIN;
extraHighAggregation = AggregationPeriod.FIVE_MIN;
xxlAggregation = AggregationPeriod.TEN_MIN;
xxxlAggregation = AggregationPeriod.FIFTEEN_MIN;
} else if timeFrame == AggregationPeriod.MIN {
lowestAggregation = AggregationPeriod.MIN;
middleAggregation = AggregationPeriod.TWO_MIN;
highestAggregation = AggregationPeriod.FOUR_MIN;
extraHighAggregation = AggregationPeriod.FIVE_MIN;
xxlAggregation = AggregationPeriod.FIFTEEN_MIN;
xxxlAggregation = AggregationPeriod.TWENTY_MIN;
} else if timeFrame == AggregationPeriod.TWO_MIN and timeMode == timeMode.shorter {
lowestAggregation = AggregationPeriod.TWO_MIN;
middleAggregation = AggregationPeriod.FOUR_MIN;
highestAggregation = AggregationPeriod.FIVE_MIN;
extraHighAggregation = AggregationPeriod.TEN_MIN;
xxlAggregation = AggregationPeriod.FIFTEEN_MIN;
xxxlAggregation = AggregationPeriod.THIRTY_MIN;
} else if timeFrame == AggregationPeriod.TWO_MIN {
lowestAggregation = AggregationPeriod.TWO_MIN;
middleAggregation = AggregationPeriod.FIVE_MIN;
highestAggregation = AggregationPeriod.TEN_MIN;
extraHighAggregation = AggregationPeriod.FIFTEEN_MIN;
xxlAggregation = AggregationPeriod.THIRTY_MIN;
xxxlAggregation = AggregationPeriod.HOUR;
} else if timeFrame == AggregationPeriod.THREE_MIN and timeMode == timeMode.shorter {
lowestAggregation = AggregationPeriod.THREE_MIN;
middleAggregation = AggregationPeriod.FOUR_MIN;
highestAggregation = AggregationPeriod.FIVE_MIN;
extraHighAggregation = AggregationPeriod.TEN_MIN;
xxlAggregation = AggregationPeriod.FIFTEEN_MIN;
xxxlAggregation = AggregationPeriod.THIRTY_MIN;
} else if timeFrame == AggregationPeriod.THREE_MIN {
lowestAggregation = AggregationPeriod.THREE_MIN;
middleAggregation = AggregationPeriod.FIVE_MIN;
highestAggregation = AggregationPeriod.TEN_MIN;
extraHighAggregation = AggregationPeriod.FIFTEEN_MIN;
xxlAggregation = AggregationPeriod.THIRTY_MIN;
xxxlAggregation = AggregationPeriod.HOUR;
} else if timeFrame == AggregationPeriod.FOUR_MIN and timeMode == timeMode.shorter {
lowestAggregation = AggregationPeriod.FOUR_MIN;
middleAggregation = AggregationPeriod.FIVE_MIN;
highestAggregation = AggregationPeriod.TEN_MIN;
extraHighAggregation = AggregationPeriod.FIFTEEN_MIN;
xxlAggregation = AggregationPeriod.TWENTY_MIN;
xxxlAggregation = AggregationPeriod.THIRTY_MIN;
} else if timeFrame == AggregationPeriod.FOUR_MIN {
lowestAggregation = AggregationPeriod.FOUR_MIN;
middleAggregation = AggregationPeriod.FIVE_MIN;
highestAggregation = AggregationPeriod.TEN_MIN;
extraHighAggregation = AggregationPeriod.FIFTEEN_MIN;
xxlAggregation = AggregationPeriod.THIRTY_MIN;
xxxlAggregation = AggregationPeriod.HOUR;
} else if timeFrame == AggregationPeriod.FIVE_MIN {
lowestAggregation = AggregationPeriod.FIVE_MIN;
middleAggregation = AggregationPeriod.TEN_MIN;
highestAggregation = AggregationPeriod.FIFTEEN_MIN;
extraHighAggregation = AggregationPeriod.THIRTY_MIN;
xxlAggregation = AggregationPeriod.HOUR;
xxxlAggregation = AggregationPeriod.TWO_HOURS;
} else if timeFrame == AggregationPeriod.TEN_MIN {
lowestAggregation = AggregationPeriod.TEN_MIN;
middleAggregation = AggregationPeriod.FIFTEEN_MIN;
highestAggregation = AggregationPeriod.THIRTY_MIN;
extraHighAggregation = AggregationPeriod.HOUR;
xxlAggregation = AggregationPeriod.TWO_HOURS;
xxxlAggregation = AggregationPeriod.FOUR_HOURS;
} else if timeFrame == AggregationPeriod.FIFTEEN_MIN {
lowestAggregation = AggregationPeriod.FIFTEEN_MIN;
middleAggregation = AggregationPeriod.THIRTY_MIN;
highestAggregation = AggregationPeriod.HOUR;
extraHighAggregation = AggregationPeriod.TWO_HOURS;
xxlAggregation = AggregationPeriod.FOUR_HOURS;
xxxlAggregation = AggregationPeriod.DAY;
} else if timeFrame == AggregationPeriod.THIRTY_MIN {
lowestAggregation = AggregationPeriod.THIRTY_MIN;
middleAggregation = AggregationPeriod.HOUR;
highestAggregation = AggregationPeriod.TWO_HOURS;
extraHighAggregation = AggregationPeriod.FOUR_HOURS;
xxlAggregation = AggregationPeriod.DAY;
xxxlAggregation = AggregationPeriod.TWO_DAYS;
} else if timeFrame == AggregationPeriod.HOUR {
lowestAggregation = AggregationPeriod.HOUR;
middleAggregation = AggregationPeriod.TWO_HOURS;
highestAggregation = AggregationPeriod.FOUR_HOURS;
extraHighAggregation = AggregationPeriod.DAY;
xxlAggregation = AggregationPeriod.TWO_DAYS;
xxxlAggregation = AggregationPeriod.FOUR_DAYS;
} else if timeFrame == AggregationPeriod.TWO_HOURS {
lowestAggregation = AggregationPeriod.TWO_HOURS;
middleAggregation = AggregationPeriod.FOUR_HOURS;
highestAggregation = AggregationPeriod.DAY;
extraHighAggregation = AggregationPeriod.TWO_DAYS;
xxlAggregation = AggregationPeriod.FOUR_DAYS;
xxxlAggregation = AggregationPeriod.WEEK;
} else if timeFrame == AggregationPeriod.FOUR_HOURS {
lowestAggregation = AggregationPeriod.FOUR_HOURS;
middleAggregation = AggregationPeriod.DAY;
highestAggregation = AggregationPeriod.TWO_DAYS;
extraHighAggregation = AggregationPeriod.FOUR_DAYS;
xxlAggregation = AggregationPeriod.WEEK;
xxxlAggregation = AggregationPeriod.MONTH;
} else if timeFrame == AggregationPeriod.DAY {
lowestAggregation = AggregationPeriod.DAY;
middleAggregation = AggregationPeriod.TWO_DAYS;
highestAggregation = AggregationPeriod.FOUR_DAYS;
extraHighAggregation = AggregationPeriod.WEEK;
xxlAggregation = AggregationPeriod.MONTH;
xxxlAggregation = AggregationPeriod.QUARTER;
} else {
lowestAggregation = AggregationPeriod.TWO_DAYS;
middleAggregation = AggregationPeriod.FOUR_DAYS;
highestAggregation = AggregationPeriod.WEEK;
extraHighAggregation = AggregationPeriod.MONTH;
xxlAggregation = AggregationPeriod.QUARTER;
xxxlAggregation = AggregationPeriod.YEAR;
}

def testTimeFrames = if timeFrame == lowestAggregation and lowestAggregation < middleAggregation and middleAggregation < highestAggregation < extraHighAggregation then 1 else 0;
def CloseAllCondition = if timeFrame <= AggregationPeriod.FIFTEEN_MIN then 0 else if SecondsTillTime(1558) == 0 then 1 else 0;
def EOD = if timeFrame >= AggregationPeriod.FIFTEEN_MIN then 0 else if SecondsTillTime(1558) == 0 and SecondsFromTime(1558) == 0 then 1 else 0;
def Active;
if securityType == securityType.stocks {
Active = if (SecondsFromTime(0930) > 0 and SecondsTillTime(1558) > 0) then 1 else 0;
} else {
Active = 1;
#Active = if ((SecondsFromTime(0500) > 0 and SecondsTillTime(0800) > 0)) or ((SecondsFromTime(1700) > 0 and SecondsTillTime(2100) > 0)) then 1 else 0;
}

###################################
#indicator
input price = close;
input fastLength = 20;
input slowLength = 50;
input displace_fast = 0;
input displace_slow = 0;
input averageType = AverageType.EXPONENTIAL;

plot FastMA = MovingAverage(averageType, price, fastLength)[-displace_fast];
plot SlowMA = MovingAverage(averageType, price, slowLength)[-displace_slow];
FastMA.SetDefaultColor(GetColor(1));
SlowMA.SetDefaultColor(GetColor(2));

def ema1 = expaverage(close,8);
def ema2 = expaverage(close,20);
def crossup = fastma > slowma;
def crossdn = fastma < slowma;

def countup = if crossup and !crossup[1] then 1 else countup[1]+1;
def countdn = if crossdn and !crossdn[1] then 1 else countdn[1]+1;

addlabel(1, if crossup then "Buy, " + countup
else if crossdn then "Sell, " + countdn
else "Equal", color.black);
assignpricecolor(if crossup then color.green
else if crossdn then color.red
else color.gray);
####################################

#ATR Trailing Stop.
####################################
#
# TD Ameritrade IP Company, Inc. (c) 2009-2022
#

input trailType = {default modified, unmodified};
input ATRPeriod = 5;
input ATRFactor = 3.5;
input firstTrade = {default long, short};
input averageType1 = 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(averageType1, trueRange, ATRPeriod);

def state = {default init, long, short};
def trail;
switch (state[1]) {
case init:
if (!IsNaN(loss)) {
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("Sell", GetColor(1));
TrailingStop.AssignValueColor(if state == state.long
then TrailingStop.Color("Sell")
##################################

AssignPriceColor( if crossup and close<trail then color.white
else if crossdn and close>trail then color.white
else if crossup and close>trail then color.green
else if crossdn and close<trail then color.red else Color.black);
####################################

#######################

AddOrder(OrderType.BUY_AUTO, crossup and close>trail and Active and testTimeFrames, tickcolor = Color.CYAN, arrowcolor = Color.CYAN, name = "BUY");
AddOrder(OrderType.SELL_TO_CLOSE, (crossup and close<trail) or (crossdn and close>trail) and Active and testTimeFrames, price = open[-2], tickcolor = Color.MAGENTA, arrowcolor = Color.MAGENTA, name = "Sell To Close");

################################################################
########## SELL ###########
################################################################

AddOrder(OrderType.SELL_AUTO, crossdn and close<trail and Active and testTimeFrames, tickcolor = Color.MAGENTA, arrowcolor = Color.MAGENTA, name = "SELL");
AddOrder(OrderType.BUY_TO_CLOSE,( crossup and close<trail) or (crossdn and close>trail) and Active and testTimeFrames, price = open[-2], tickcolor = Color.CYAN, arrowcolor = Color.CYAN, name = "Buy To Close");

AddOrder(OrderType.SELL_TO_CLOSE, EOD[-1], close[-1], 0, Color.ORANGE, Color.ORANGE, "EOD LX");
AddOrder(OrderType.BUY_TO_CLOSE, EOD[-1], close[-1], 0, Color.ORANGE, Color.ORANGE, "EOD SX");

input barsFromEnd = 1;
def EOD1 = RegularTradingEnd(GetYYYYMMDD()) - GetTime() <= GetAggregationPeriod() * barsFromEnd - 1;
AddOrder(OrderType.SELL_TO_CLOSE, EOD1[-1], close[-1], 0, Color.ORANGE, Color.ORANGE, "EOD LX");
AddOrder(OrderType.BUY_TO_CLOSE, EOD1[-1], close[-1], 0, Color.ORANGE, Color.ORANGE, "EOD SX");``````

Last edited by a moderator:

## The Market Trading Game Changer

Join 2,500+ subscribers inside the useThinkScript VIP Membership Club
• Exclusive indicators
• Proven strategies & setups
• Private Discord community
• 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?