Need to create a custom scan/alert for ATR Trailing Stop change

B

bigpemby

New member
One of my favorite indicators/TOS studies that I use for the basis of some of my strategies is the ATR trailing stop. I am trying to find a way to generate a scan that would give me results of a recent change in this indicator. Meaning it has switched from Buy to Sell or Sell to Buy. In a perfect world I would then be able to setup alerts based on this change and in a dream world be able to setup an alert to when price is within (X) distance of the current ATR trailing stop buy or sell. Ok, that was a mouthful. Any help would be appreciated. THANKS!
 
inthefutures

inthefutures

Active member
seems simple enough add a alert line and then an if statement for the closing price
 
B

bigpemby

New member
Not sure if you are meaning to add a study alert or something else. This particular study does not show up to be selectable when trying to setup a study alert. I also would like to be able SCAN for this condition which will require a custom scan script I am afraid.
 
wtf_dude

wtf_dude

Active member
Not sure if you are meaning to add a study alert or something else. This particular study does not show up to be selectable when trying to setup a study alert. I also would like to be able SCAN for this condition which will require a custom scan script I am afraid.
Nah, what he's saying is set is your TS and see where the dots plot. Then just set an individual stock alert on the price that the TS sets. Works just fine
 
Last edited:
B

bigpemby

New member
A static price alert won't work because the ATR trailing stop is dynamically changing with each bar. Maybe I am confused. See image link attached. I want to be alerted when the dots change to pink or change to blue. Then be able to scan for a daily/hourly condition change to find symbols that have recently changed.

 
wtf_dude

wtf_dude

Active member
A static price alert won't work because the ATR trailing stop is dynamically changing with each bar. Maybe I am confused. See image link attached. I want to be alerted when the dots change to pink or change to blue. Then be able to scan for a daily/hourly condition change to find symbols that have recently changed.

Oh duh, sorry man I forgot the fact that I change the ATR settings to a much wider (50 day) period to eliminate chops personally. I'll have to let somebody else take this one on. I'm sure it can be done just by just ripping and modifying the code from the ATR TS study itself.
 
B

bigpemby

New member
Thanks for your help. I am certainly a newbie when it comes to thinkscript.
 
wtf_dude

wtf_dude

Active member
A static price alert won't work because the ATR trailing stop is dynamically changing with each bar. Maybe I am confused. See image link attached. I want to be alerted when the dots change to pink or change to blue. Then be able to scan for a daily/hourly condition change to find symbols that have recently changed.
Hey, I finally sat down and looked this. This will at least add alerts to the study itself when it flips from buy to sell on the stop. As far as a scan or adding the percentage, you'll have to code that in yourself.

Code:
# TD Ameritrade IP Company, Inc. (c) 2009-2020
# alert mod by WTF_Dude
#

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





# Alerts:

input alerttext="ATR stop switch";
input UseAlerts = {false, default true};
input AlertType = {default "BAR", "ONCE", "TICK"};
input AlertSound = {"Bell", "Chimes", default "Ding", "NoSound", "Ring"};
Alert(BuySignal, alerttext, AlertType, AlertSound);
Alert(SellSignal,alerttext, AlertType, AlertSound);
 
Last edited:
B

bigpemby

New member
This is fantastic! Thank you very much. Wish I could code. I have another post I made yesterday about an exit order I am trying to create. It sure seems simple enough but I just cannot get the mechanics to work out.
 
M

modernmind

New member
This is fantastic! Thank you very much. Wish I could code. I have another post I made yesterday about an exit order I am trying to create. It sure seems simple enough but I just cannot get the mechanics to work out.
Did you ever find a solution to your scan request for the change in ATR direction? I'm interested in this scan as well.
 
S

ShadowJack

New member
I am using the following custom filter to scan for stocks that change direction today

Code:
input trailType = {default modified, unmodified};
input periods = 21;
input multiplier = 3.0;
input firstTrade = {default long, short};
input averageType = AverageType.WILDERS;
input indicator = {both, default long, short};

def yesterdayATR = ATRTrailingStop("trail type" = trailType, "atr period" = periods, "atr factor" = multiplier, "first trade" = firstTrade, "average type" = averageType) from 1 bar ago;
def yesterdayPrice = close from 1 bar ago;
def todayATR = ATRTrailingStop("trail type" = trailType, "atr period" = periods, "atr factor" = multiplier, "first trade" = firstTrade, "average type" = averageType);
def todayPrice = close;

def buyIndicator = (indicator == indicator.both or indicator == indicator.long) and (todayATR <= todayPrice and yesterdayATR >= yesterdayPrice);
def sellIndicator = (indicator == indicator.both or indicator == indicator.short) and (todayATR >= todayPrice and yesterdayATR <= yesterdayPrice);

plot keep = if buyIndicator or sellIndicator then todayATR else Double.NaN;
 
T

TechGuy

New member
@wtf_dude Hi, I am new to Thinkscript and I appreciate your help. I am trying to program a scan using your example above for a price going either up or down though an ATR trailing stop. I have my scan on an ATR period of 5 and a Factor of 3.5. I have the scan timeframe set currently at a 3 minute interval. However, the scan does not accurately show new longs and shorts. What am I missing? Thanks.
 
wtf_dude

wtf_dude

Active member
Hi, I am new to Thinkscript and I appreciate your help. I am trying to program a scan using your example above for a price going either up or down though an ATR trailing stop. I have my scan on an ATR period of 5 and a Factor of 3.5. I have the scan timeframe set currently at a 3 minute interval. However, the scan does not accurately show new longs and shorts. What am I missing? Thanks.
hmm probably just need to correct your condition wizard. So on the pop up menu select:

First column select price, close
middle column select "crosses above" (to get a blue dot/bull)
last column select study, ATRtrailingstop and leave the default settings
 
T

TechGuy

New member
hmm probably just need to correct your condition wizard. So on the pop up menu select:

First column select price, close
middle column select "crosses above" (to get a blue dot/bull)
last column select study, ATRtrailingstop and leave the default settings
Hi Dude, please help me understand your solution since I am a newbie at this. Does this mean change your formula that you posted April 25, 2020? I really appreciate your help on scanning for stocks that have broken the ATR trailing stop up or down. I can set up a scan for each type. When I input your scan and use a 3 min timeframe, many of the stocks dont satisfy the condition if I pull up a chart that has the ATR trailing stop formula and see where the price is. Thanks.
 
wtf_dude

wtf_dude

Active member
Hi Dude, please help me understand your solution since I am a newbie at this. Does this mean change your formula that you posted April 25, 2020? I really appreciate your help on scanning for stocks that have broken the ATR trailing stop up or down. I can set up a scan for each type. When I input your scan and use a 3 min timeframe, many of the stocks dont satisfy the condition if I pull up a chart that has the ATR trailing stop formula and see where the price is. Thanks.
Ok so you want to take the code to the CHART tab first. Create a new study and paste the code there. Save it under a memorable name.

Now go to the SCAN tab. Select "add filter" then "study"

where it says "adxcrossover" by default, click the dropdown and select the bottom option which is "custom"

new menu pops up. Click delete next to the adxcrossover line.

near the bottom you'll see "add condition"

click that and itll take you to a menu where you can follow the previous instructions..

If it still doesnt make sense, just youtube "condition wizard" or custom scans, etc. There are tons of tutorials
 
T

TechGuy

New member
@wtf_dude Hi Dude. Just programmed it like you said. I have to wait until tomorrow to see if it picks up anything to make sure I did it right. I will go to the websites you suggested. Thanks again.
 

Similar threads

Top