ATRTrailingStop Cloud

QED

New member
VIP
The following code is the standard Charles Schwab ATRTrailingStop code provided in Thinkorswim.

Question: what piece of code would need to be added to it in order to have a green cloud when the price is above the ATRTrailingStop and a red cloud when the price is below the ATRTrailingStop?

I appreciate any assistance. Thank you.

———————

# Charles Schwab & Co. (c) 2009-2025

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

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

else TrailingStop.Color("Buy"));
 
Solution
The following code is the standard Charles Schwab ATRTrailingStop code provided in Thinkorswim.

Question: what piece of code would need to be added to it in order to have a green cloud when the price is above the ATRTrailingStop and a red cloud when the price is below the ATRTrailingStop?

I appreciate any assistance. Thank you.

———————

# Charles Schwab & Co. (c) 2009-2025

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...
The following code is the standard Charles Schwab ATRTrailingStop code provided in Thinkorswim.

Question: what piece of code would need to be added to it in order to have a green cloud when the price is above the ATRTrailingStop and a red cloud when the price is below the ATRTrailingStop?

I appreciate any assistance. Thank you.

———————

# Charles Schwab & Co. (c) 2009-2025

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

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

else TrailingStop.Color("Buy"));

you didn't say where and when you wanted to see clouds.

this draws clouds from the trail line, above or below.

Code:
#atr_trail_cloud
#https://usethinkscript.com/threads/atrtrailingstop-cloud.20400/
#ATRTrailingStop Cloud

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

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") else TrailingStop.Color("Buy"));

#----------------------

def na = double.nan;
def bn = barnumber();
def pos = Double.POSITIVE_INFINITY;
def neg = Double.negative_INFINITY;

def dir = if close > trail then 1
 else if close < trail then -1
 else dir[1];

def cld1_top = if close > trail then pos else na;
def cld1_bot = if close > trail then trail else na;
addcloud(cld1_top, cld1_bot, color.green);

def cld2_top = if close < trail then trail else na;
def cld2_bot = if close < trail then neg else na;
addcloud(cld2_top, cld2_bot, color.red);


addchartbubble(0, low,
dir
, color.yellow, no);
#
 

Attachments

  • img1.JPG
    img1.JPG
    64.8 KB · Views: 51
Solution
you didn't say where and when you wanted to see clouds.

this draws clouds from the trail line, above or below.

Code:
#atr_trail_cloud
#https://usethinkscript.com/threads/atrtrailingstop-cloud.20400/
#ATRTrailingStop Cloud

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

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") else TrailingStop.Color("Buy"));

#----------------------

def na = double.nan;
def bn = barnumber();
def pos = Double.POSITIVE_INFINITY;
def neg = Double.negative_INFINITY;

def dir = if close > trail then 1
 else if close < trail then -1
 else dir[1];

def cld1_top = if close > trail then pos else na;
def cld1_bot = if close > trail then trail else na;
addcloud(cld1_top, cld1_bot, color.green);

def cld2_top = if close < trail then trail else na;
def cld2_bot = if close < trail then neg else na;
addcloud(cld2_top, cld2_bot, color.red);


addchartbubble(0, low,
dir
, color.yellow, no);
#
@halcyonguy, thank you!

Question: If I wanted to cut the cloud off at the "one" simple moving average, would I replace "Double.POSITIVE_INFINITY" and "Double.negative_INFINITY" with "Average(close,1)" or would I need to add or do something else?
 

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

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
369 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