ATR Trailing Stop Alerts


1
0

Hi Pete, I like to use the ATRTrailingStop study on Thinkorswim for short term trading and it would be very helpful if this study had the option for study based alerts (text/email/push notifications). Unfortunately, Thinkorswim does not list this study in the Condition Wizard when creating an alert. Ideally, the alert would trigger when the price crossed above and below the ATRTrailingStop line by two bars (to prevent false alerts) or when the Buy and Sell signals trigger when using the ATRTrailingStopLE and ATRTrailingStopSE strategies. Thanks in advance and I look forward to your input!

Marked as spam
Posted by (Questions: 1, Answers: 1)
Asked on January 7, 2017 2:38 pm
3862 views
4
Private answer

Ok, the reason this study does not appear in the Study Alerts is because the code is to complex for that tool to evaluate. We can do this very simply on a Chart based study. But the only way you are going to get text/email alerts using this study is to build a custom scan. They recently added functionality to the scan engine that enables it to send text/email alerts when symbols are added to the results. (for a 15 minute chart you could get text/email alerts every 15 minutes. You can also set this up to run daily or hourly and just send you a list of symbols in the results.

You can read more about this in the release notes: http://toslc.thinkorswim.com/center/release/rel-09-10-2016.html#section_1

So I have two versions of the code for you. One to run scans (you will need to configure the scan to create alerts as instructed in the link above). The other is to place on a live chart, with audible and visual alerts added.

Note: Let me know if there are issues copying this code and I will update this post to include a couple of text files.

Don’t forget to up-vote this answer if it solves your question! Thanks.

Here is the code for the Scan:

#
# TD Ameritrade IP Company, Inc. (c) 2009-2016
#
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);
# move comment markers dependin on which of these two you want to run
plot scan = BuySignal;
#plot scan = SellSignal;

Here is the code for the chart study with alerts:
#
# TD Ameritrade IP Company, Inc. (c) 2009-2016
#
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;
}
}
plot BuySignal = if Crosses(state == state.long, 0, CrossingDirection.ABOVE) then low else Double.NaN;
BuySignal.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
plot SellSignal = if Crosses(state == state.short, 0, CrossingDirection.ABOVE) then high else Double.NaN;
SellSignal.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
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"));
Alert(BuySignal, "Trail Stop Long Entry", Alert.BAR, Sound.RING);
Alert(SellSignal, "Trail Stop Short Entry", Alert.BAR, Sound.RING);

Marked as spam
Posted by (Questions: 37, Answers: 4118)
Answered on January 7, 2017 4:19 pm
0

Thanks Pete! Both of your methods work great and I appreciate the additional arrow plots on the chart study!

( at January 7, 2017 5:43 pm)
0
Hello Pete, As a follow-up on this ATRtrailing Stop, I am using this in a scan (Daily timeframe) in conjunction with a Stochastic scan (1hour). The issue I’m having is an alert is supposed to notify me when the Stochastic K crosses above the D and the price is above the ATRtrailing Stop. Simple, however the alerts are firing at random and not when the conditions are met. I’ve contacted TOS support and discussed this with them and they don’t seem to know why this is happening. Are you experiencing this with alerts set on time-frames lower than daily?
( at February 22, 2017 4:10 pm)
0

Please start a new post for this so we can discuss this further without cluttering up this one. I will link to the new post from here once we get that established. In your post, please be sure to include a screenshot showing how your scan is setup.

( at February 22, 2017 5:11 pm)
0
Works well Pete, but how can I change the scan to give me alerts after 1 bar? I use it primarily on Daily charts with different ATR settings, but I'm getting the alerts 2 bars after I'd like to. Thanks, KC
( at February 7, 2020 12:57 pm)
0
The alerts are currently set to trigger on the very same bars as the arrows which indicate the change in direction. There is no way to make the alerts trigger before the arrows appear. Unless you can read the future. In which case you don't need any alerts... lol
( at February 7, 2020 1:19 pm)
0
Hi Pete, I was looking to see how this scan would work and get an error when trying it out. Any thoughts? After pasting the scan code to the thinkScript editor I get the following error message: rec usage is not allowed in this context
( at February 20, 2020 4:59 pm)
0
The only way this could happen is if you are instead using the Study Alert or Conditional Order tools. This error would never appear when adding this code to a Study Filter in a custom scan. (I just tested it to be sure)
( at February 20, 2020 6:35 pm)
0
Hi Pete, I tried to submit a comment on this forum somewhere but cant find now and want to be sure I am alerted to your response. I'd like have the strategy initiate a "buy" or "sell" as soon as price bar crosses trailing atr in real-time, i.e. even if it flip flops back and forth on the time frame I'm looking at. is that possible? thank you so much. amazing stuff here
( at October 26, 2020 7:05 pm)
0
That is already included as a built-in strategy that comes with Thinkorswim. The following post explains some of the details you are lacking: https://www.hahn-tech.com/ans/how-to-make-a-atrtrailstoplx/
( at October 26, 2020 8:38 pm)