# Target plots after buy signals

#### wtf_dude

##### Well-known member
I have zero experience with with programming based on bar count. Could somebody give this a shot for me: I want to use ATR trailing stop buy signals to plot target lines 2 ATR above the entry price as well as 1 ATR, 2 ATR, and 3 ATR above that. The key is plotting ONLY starting at the buy signal and extending right until the sell signal is hit. I've searched other threads and still can't seem to get it to work. Thanks in advance if you can help out!

I have zero experience with with programming based on bar count. Could somebody give this a shot for me: I want to use ATR trailing stop buy signals to plot target lines 2 ATR above the entry price as well as 1 ATR, 2 ATR, and 3 ATR above that. The key is plotting ONLY starting at the buy signal and extending right until the sell signal is hit. I've searched other threads and still can't seem to get it to work. Thanks in advance if you can help out!
What calculation and trigger are you using for the entry?

The ATR trailing stop study that's already in TOS.

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

@wtf_dude Sounds like you only want the long plot of ATR, you can try to remove the short code. Or you can add this condition to your plot and it should do what you want as well unless I misunderstood the request.
Code:
``def plotcond = if buysignal then 1 else if sellsignal then 0 else plotcond[1];``

no, I'm not plotting the buy signal. The default code already does that. I want to plot price targets that extend right above the price only while the buy signal is in effect.

Check out this thread's indicator to see what I'm aiming for. I just can't wrap my head around the bar coding logic atm. It's totally new to me

Something like this?
Please let me know if I misunderstood your request, this is just my interpretation.
You wanted to 2 ATR after the Buy or Sell Signal is triggered then also a 3rd, 4th and 5th ATR after that. The screen shot should show that. I also added the 1 ATR (for a total of 5). The original indicator trailing stop is there for the DOTS and I also included another optional stop which takes into account the close plus/minus the atr.

The Arrows indicate when the Buy and Sell signals trigger, that was just for reference, we can remove if you don't want them there since the Dots do the same thing.

Let me know if we are on the right track?

no, I'm not plotting the buy signal. The default code already does that. I want to plot price targets that extend right above the price only while the buy signal is in effect.

Check out this thread's indicator to see what I'm aiming for. I just can't wrap my head around the bar coding logic atm. It's totally new to me
@SuryaKiranC and I wrote that. It is the same logic and very reusable!

@SuryaKiranC and I wrote that. It is the same logic and very reusable!
DUDE, YES! That looks like it's exactly what I'm looking for! THANK YOU

Cool. I'll clean it up and post a version for you to use and we can tweak from there. I'll try and post it this afternoon.

Cool. I'll clean it up and post a version for you to use and we can tweak from there. I'll try and post it this afternoon.
Really appreciate it man. Would you possibly be able to include the lines of code if I differentiate and only show the current targets? HOPEFULLY, if I see how you're doing it I wont need to make newbie requests for bar code lol

Really appreciate it man. Would you possibly be able to include the lines of code if I differentiate and only show the current targets? HOPEFULLY, if I see how you're doing it I wont need to make newbie requests for bar code lol
As in only show the most current targets and not the previous? At the moment, I only know how to do that for the day (show previous day or not). If anyone knows how to do that for the most recent targets only, please do let me know.

Try this for a first draft.

Updated to include current targets code from @SleepyZ and the FIB Level Multipliers.

Code:
``````#ATR_Stoploss_Targets v1.2
#
#CHANGELOG
#
# 2021.05.12    v1.2    @cos251 - Added ADR Multipler for targets 1 through 5 based on screenshot from @wtf_dude
#                                 - Multipliers are based on FIB leves per wtf_dude
#
# 2021.05.11    v1.1    @cos251 - Added feature to plot only current ADR Targets and Stoploss
#                            - Feature credit goes to @SleepyZ (thx)
# 2021.05.06    v1.0    @cos251    - Initial release - ATRStoploss Standard TOS indicator with targets after BuySignal or SellSignal trigger
#
#
#CREDITS
# requested by "@wtf_dude"
# code improvements by "@SleepyZ"
#
#

# -- Standard ATR Stoploss Indicator TOS Version
input showlast_Targets_only = yes;
input stopLossMultiplier = 1.0;
input Target1Multiplier = .78;
input Target2Multiplier = 1.61;
input Target3Multiplier = 2.38;
input Target4Multiplier = 3.23;
input Target5Multiplier = 4.0;
input trailType = {default modified, unmodified};
input ATRPeriod = 5;
input ATRFactor = 3.5;
input firstTrade = {default long, short};
input averageType = AverageType.WILDERS;
input debug = no;

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)) {
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 arrowup = if BuySignal then low else Double.NaN;
plot arrowdown = if SellSignal then high else Double.NaN;
arrowup.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
arrowdown.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);
arrowup.SetLineWeight(5);
arrowdown.SetLineWeight(5);
arrowup.SetDefaultColor(Color.GREEN);
arrowdown.SetDefaultColor(Color.RED);
plot TrailingStop = trail;

TrailingStop.SetPaintingStrategy(PaintingStrategy.POINTS);
TrailingStop.DefineColor("Sell", GetColor(1));
TrailingStop.AssignValueColor(if state == state.long
then TrailingStop.Color("Sell")

################################################################
################################################################
input rangeType = { default "trueRange", "ATR", "ADR" };
input ATRlength = 14;
input ATRaverageType = AverageType.WILDERS;
def Range;
if rangeType == rangeType.ATR {
Range = MovingAverage(ATRaverageType, TrueRange(high, close, low), ATRlength);
} else if rangeType == rangeType.trueRange {
Range = trueRange;
} else {
Range = Average(high - low, 7);
}
# -- END ATR/ATR/TrueRange calc

# -- Logic for bar counter and state detector
def bnumUp;
def bnumDown;
def closeUpTrendStart;
def closeDownTrendStart;
def UpTrendBarCount;
def DownTrendBarCount;
def time;
bnumUp = BarNumber();
bnumDown = 0;
closeUpTrendStart = close;
closeDownTrendStart = 0;
UpTrendBarCount = 1;
DownTrendBarCount = 0;
time = GetTime();
} else if state == state.long {
bnumUp = bnumUp[1];
bnumDown = 0;
closeUpTrendStart = closeUpTrendStart[1];
closeDownTrendStart = 0;
UpTrendBarCount = UpTrendBarCount[1] + 1;
DownTrendBarCount = 0;
time = time[1];
} else if SellSignal {
bnumUp = 0;
bnumDown = BarNumber();
closeDownTrendStart = close;
closeUpTrendStart = 0;
UpTrendBarCount = 0;
DownTrendBarCount = 1;
time = GetTime();
} else if state == state.short {
bnumDown = bnumDown[1];
closeDownTrendStart = closeDownTrendStart[1];
DownTrendBarCount = DownTrendBarCount[1] + 1;
bnumUp = 0;
closeUpTrendStart = 0;
UpTrendBarCount = 0;
time = time[1];
} else {
bnumUp = 0;
bnumDown = 0;
closeUpTrendStart = 0;
closeDownTrendStart = 0;
UpTrendBarCount = 0;
DownTrendBarCount = 0;
time = Double.NaN;
}
def c;
if BarNumber() == bnumUp or BarNumber() == bnumDown {
c = Range;
} else if state == state.long or state == state.short {
c = c[1];
} else {
c = 0;
}

################################################################
##########       Target and Stop Loss Plots            #########
################################################################

def up = if arrowup   then bnumUp   else Double.NaN;
def dn = if arrowdown then bnumDown else Double.NaN;
plot BuySignalStopLoss = if showlast_Targets_only == yes and (bnumUp < HighestAll(up))
then Double.NaN
else if state == state.long
then closeUpTrendStart - (c * stopLossMultiplier)
else Double.NaN ;
plot oneADRPlus        = if showlast_Targets_only == yes and (bnumUp < Max(HighestAll(dn), HighestAll(up)))
then Double.NaN
else if state == state.long
then closeUpTrendStart + (c * Target1Multiplier)
else Double.NaN;
plot twoADRPlus        = if showlast_Targets_only == yes and (bnumUp < Max(HighestAll(dn), HighestAll(up)))
then Double.NaN
else if state == state.long
then closeUpTrendStart + (c * Target2Multiplier)
else Double.NaN;
plot threeADRPlus      = if showlast_Targets_only == yes and (bnumUp < Max(HighestAll(dn), HighestAll(up)))
then Double.NaN
else if state == state.long
then closeUpTrendStart + (c * Target3Multiplier)
else Double.NaN;
plot fourADRPlus       = if showlast_Targets_only == yes and (bnumUp < Max(HighestAll(dn), HighestAll(up)))
then Double.NaN
else if state == state.long
then closeUpTrendStart + (c * Target4Multiplier)
else Double.NaN;
plot fiveADRPlus       = if showlast_Targets_only == yes and (bnumUp < Max(HighestAll(dn), HighestAll(up)))
then Double.NaN
else if state == state.long
then closeUpTrendStart + (c * Target5Multiplier)
else Double.NaN;

plot SellSignalStopLoss = if showlast_Targets_only == yes and bnumDown < Max(HighestAll(dn), HighestAll(up))
then Double.NaN
else if state == state.short
then closeDownTrendStart + (c * stopLossMultiplier)
else Double.NaN;
plot oneADRMinus        = if showlast_Targets_only == yes and bnumDown < Max(HighestAll(dn), HighestAll(up))
then Double.NaN
else if state == state.short
then closeDownTrendStart - (c * Target1Multiplier)
else Double.NaN;
plot twoADRMinus        = if showlast_Targets_only == yes and bnumDown < Max(HighestAll(dn), HighestAll(up))
then Double.NaN
else if state == state.short
then closeDownTrendStart - (c * Target2Multiplier)
else Double.NaN;
plot threeADRMinus      = if showlast_Targets_only == yes and bnumDown < Max(HighestAll(dn), HighestAll(up))
then Double.NaN
else if state == state.short
then closeDownTrendStart - (c * Target3Multiplier)
else Double.NaN;
plot fourADRMinus       = if showlast_Targets_only == yes and bnumDown < Max(HighestAll(dn), HighestAll(up))
then Double.NaN
else if state == state.short
then closeDownTrendStart - (c * Target4Multiplier)
else Double.NaN;
plot fiveADRMinus       = if showlast_Targets_only == yes and bnumDown < Max(HighestAll(dn), HighestAll(up))
then Double.NaN
else if state == state.short
then closeDownTrendStart - (c * Target5Multiplier)
else Double.NaN;

SellSignalStopLoss.SetDefaultColor(Color.LIGHT_GREEN);

Last edited:
Try this for a first draft.

Code:
``````#ATR_Stoploss_Targets
#
#CHANGELOG
#
# 2021.05.06    @cos251    - Initial release - ATRStoploss Standard TOS indicator with targets after BuySignal or SellSignal trigger
#
#
#CREDITS
# requesed by "@wtf_dude"
#
#

# -- Standard ATR Stoploss Indicator TOS Version
input stopLossMultiplier = 1.0;
input trailType = {default modified, unmodified};
input ATRPeriod = 5;
input ATRFactor = 3.5;
input firstTrade = {default long, short};
input averageType = AverageType.WILDERS;
input debug = no;

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)) {
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 arrowup = if BuySignal then low else Double.NaN;
plot arrowdown = if SellSignal then high else Double.NaN;
arrowup.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
arrowdown.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);
arrowup.SetLineWeight(5);
arrowdown.SetLineweight(5);
arrowup.SetDefaultColor(Color.GREEN);
arrowdown.SetDefaultColor(Color.RED);
plot TrailingStop = trail;

TrailingStop.SetPaintingStrategy(PaintingStrategy.POINTS);
TrailingStop.DefineColor("Sell", GetColor(1));
TrailingStop.AssignValueColor(if state == state.long
then TrailingStop.Color("Sell")

################################################################
################################################################
input rangeType = { default "trueRange", "ATR", "ADR" };
input ATRlength = 14;
input ATRaverageType = AverageType.WILDERS;
def Range;
if rangeType == rangeType.ATR {
Range = MovingAverage(ATRaverageType, TrueRange(high, close, low), ATRlength);
} else if rangeType == rangeType.trueRange {
Range = trueRange;
} else {
Range = Average(high - low, 7);
}
# -- END ATR/ATR/TrueRange calc

# -- Logic for bar counter and state detector
def bnumUp;
def bnumDown;
def closeUpTrendStart;
def closeDownTrendStart;
def UpTrendBarCount;
def DownTrendBarCount;
def time;
bnumUp = BarNumber();
bnumDown = 0;
closeUpTrendStart = close;
closeDownTrendStart = 0;
UpTrendBarCount = 1;
DownTrendBarCount = 0;
time = GetTime();
} else if state == state.long {
bnumUp = bnumUp[1];
bnumDown = 0;
closeUpTrendStart = closeUpTrendStart[1];
closeDownTrendStart = 0;
UpTrendBarCount = UpTrendBarCount[1] + 1;
DownTrendBarCount = 0;
time = time[1];
} else if SellSignal {
bnumUp = 0;
bnumDown = BarNumber();
closeDownTrendStart = close;
closeUpTrendStart = 0;
UpTrendBarCount = 0;
DownTrendBarCount = 1;
time = GetTime();
} else if state == state.short {
bnumDown = bnumDown[1];
closeDownTrendStart = closeDownTrendStart[1];
DownTrendBarCount = DownTrendBarCount[1] + 1;
bnumUp = 0;
closeUpTrendStart = 0;
UpTrendBarCount = 0;
time = time[1];
} else {
bnumUp = 0;
bnumDown = 0;
closeUpTrendStart = 0;
closeDownTrendStart = 0;
UpTrendBarCount = 0;
DownTrendBarCount = 0;
time = double.nan;
}
def c;
if BarNumber() == bnumUp or BarNumber() == bnumDown {
c = Range;
} else if state == state.long or state == state.short {
c = c[1];
} else {
c = 0;
}

################################################################
##########       Target and Stop Loss Plots            #########
################################################################
plot BuySignalStopLoss = if state == state.long then closeUpTrendStart - (c * stopLossMultiplier) else Double.NaN;
plot oneADRPlus = if state == state.long then closeUpTrendStart + c else Double.NaN;
plot twoADRPlus = if state == state.long then closeUpTrendStart + c * 2 else Double.NaN;
plot threeADRPlus = if state == state.long then closeUpTrendStart + c * 3 else Double.NaN;
plot fourADRPlus = if state == state.long then closeUpTrendStart + c * 4 else Double.NaN;
plot fiveADRPlus = if state == state.long then closeUpTrendStart + c * 5 else Double.NaN;

plot SellSignalStopLoss = if state == state.short then closeDownTrendStart + (c * stopLossMultiplier)  else Double.NaN;
plot oneADRMinus = if state == state.short then closeDownTrendStart - c else Double.NaN;
plot twoADRMinus = if state == state.short then closeDownTrendStart - c * 2 else Double.NaN;
plot threeADRMinus = if state == state.short then closeDownTrendStart - c * 3 else Double.NaN;
plot fourADRMinus = if state == state.short then closeDownTrendStart - c * 4 else Double.NaN;
plot fiveADRMinus = if state == state.short then closeDownTrendStart - c * 5 else Double.NaN;

SellSignalStopLoss.SetDefaultColor(Color.LIGHT_GREEN);

Alright, good start. My thinking is on the candle where the buysignal arrow appears, the ATR is added from either the low of that candle or the ohlc4. The ATR trailing stop that's already built in is fine for the stoploss, so we won't need a second calc/multiplier. So it's Buy signal, cloud starts at low of candle, add 1 ATR to the High of the same candle, 2 ,3 4 5 etc. And vice versa. Following?

edit: I'm tweaking with it to see what I can come up with. Changed each ATR multiplier to an input. Also, you're a god for putting this together no way in hell I'd be able to figure that out lol

Last edited:
This should allow an option to just display only the most current set of targets

Code:
``````#ATR_Stoploss_Targets
#
#CHANGELOG
#
# 2021.05.06    @cos251    - Initial release - ATRStoploss Standard TOS indicator with targets after BuySignal or SellSignal trigger
#
#
#CREDITS
# requesed by "@wtf_dude"
#
#

# -- Standard ATR Stoploss Indicator TOS Version
input showlast_Targets_only = yes;
input stopLossMultiplier = 1.0;
input trailType = {default modified, unmodified};
input ATRPeriod = 5;
input ATRFactor = 3.5;
input firstTrade = {default long, short};
input averageType = AverageType.WILDERS;
input debug = no;

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)) {
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 arrowup = if BuySignal then low else Double.NaN;
plot arrowdown = if SellSignal then high else Double.NaN;
arrowup.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
arrowdown.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);
arrowup.SetLineWeight(5);
arrowdown.SetLineWeight(5);
arrowup.SetDefaultColor(Color.GREEN);
arrowdown.SetDefaultColor(Color.RED);
plot TrailingStop = trail;

TrailingStop.SetPaintingStrategy(PaintingStrategy.POINTS);
TrailingStop.DefineColor("Sell", GetColor(1));
TrailingStop.AssignValueColor(if state == state.long
then TrailingStop.Color("Sell")

################################################################
################################################################
input rangeType = { default "trueRange", "ATR", "ADR" };
input ATRlength = 14;
input ATRaverageType = AverageType.WILDERS;
def Range;
if rangeType == rangeType.ATR {
Range = MovingAverage(ATRaverageType, TrueRange(high, close, low), ATRlength);
} else if rangeType == rangeType.trueRange {
Range = trueRange;
} else {
Range = Average(high - low, 7);
}
# -- END ATR/ATR/TrueRange calc

# -- Logic for bar counter and state detector
def bnumUp;
def bnumDown;
def closeUpTrendStart;
def closeDownTrendStart;
def UpTrendBarCount;
def DownTrendBarCount;
def time;
bnumUp = BarNumber();
bnumDown = 0;
closeUpTrendStart = close;
closeDownTrendStart = 0;
UpTrendBarCount = 1;
DownTrendBarCount = 0;
time = GetTime();
} else if state == state.long {
bnumUp = bnumUp[1];
bnumDown = 0;
closeUpTrendStart = closeUpTrendStart[1];
closeDownTrendStart = 0;
UpTrendBarCount = UpTrendBarCount[1] + 1;
DownTrendBarCount = 0;
time = time[1];
} else if SellSignal {
bnumUp = 0;
bnumDown = BarNumber();
closeDownTrendStart = close;
closeUpTrendStart = 0;
UpTrendBarCount = 0;
DownTrendBarCount = 1;
time = GetTime();
} else if state == state.short {
bnumDown = bnumDown[1];
closeDownTrendStart = closeDownTrendStart[1];
DownTrendBarCount = DownTrendBarCount[1] + 1;
bnumUp = 0;
closeUpTrendStart = 0;
UpTrendBarCount = 0;
time = time[1];
} else {
bnumUp = 0;
bnumDown = 0;
closeUpTrendStart = 0;
closeDownTrendStart = 0;
UpTrendBarCount = 0;
DownTrendBarCount = 0;
time = Double.NaN;
}
def c;
if BarNumber() == bnumUp or BarNumber() == bnumDown {
c = Range;
} else if state == state.long or state == state.short {
c = c[1];
} else {
c = 0;
}

################################################################
##########       Target and Stop Loss Plots            #########
################################################################

def up = if arrowup   then bnumUp   else Double.NaN;
def dn = if arrowdown then bnumDown else Double.NaN;

plot BuySignalStopLoss = if showlast_Targets_only == yes and (bnumUp < HighestAll(up))
then Double.NaN
else if state == state.long
then closeUpTrendStart - (c * stopLossMultiplier)
else Double.NaN ;
plot oneADRPlus        = if showlast_Targets_only == yes and (bnumUp < Max(HighestAll(dn), HighestAll(up)))
then Double.NaN
else if state == state.long
then closeUpTrendStart + c
else Double.NaN;
plot twoADRPlus        = if showlast_Targets_only == yes and (bnumUp < Max(HighestAll(dn), HighestAll(up)))
then Double.NaN
else if state == state.long
then closeUpTrendStart + c * 2
else Double.NaN;
plot threeADRPlus      = if showlast_Targets_only == yes and (bnumUp < Max(HighestAll(dn), HighestAll(up)))
then Double.NaN
else if state == state.long
then closeUpTrendStart + c * 3
else Double.NaN;
plot fourADRPlus       = if showlast_Targets_only == yes and (bnumUp < Max(HighestAll(dn), HighestAll(up)))
then Double.NaN
else if state == state.long
then closeUpTrendStart + c * 4
else Double.NaN;
plot fiveADRPlus       = if showlast_Targets_only == yes and (bnumUp < Max(HighestAll(dn), HighestAll(up)))
then Double.NaN
else if state == state.long
then closeUpTrendStart + c * 5
else Double.NaN;

plot SellSignalStopLoss = if showlast_Targets_only == yes and bnumDown < Max(HighestAll(dn), HighestAll(up))
then Double.NaN
else if state == state.short
then closeDownTrendStart + (c * stopLossMultiplier)
else Double.NaN;
plot oneADRMinus        = if showlast_Targets_only == yes and bnumDown < Max(HighestAll(dn), HighestAll(up))
then Double.NaN
else if state == state.short
then closeDownTrendStart - c
else Double.NaN;
plot twoADRMinus        = if showlast_Targets_only == yes and bnumDown < Max(HighestAll(dn), HighestAll(up))
then Double.NaN
else if state == state.short
then closeDownTrendStart - c * 2
else Double.NaN;
plot threeADRMinus      = if showlast_Targets_only == yes and bnumDown < Max(HighestAll(dn), HighestAll(up))
then Double.NaN
else if state == state.short
then closeDownTrendStart - c * 3
else Double.NaN;
plot fourADRMinus       = if showlast_Targets_only == yes and bnumDown < Max(HighestAll(dn), HighestAll(up))
then Double.NaN
else if state == state.short
then closeDownTrendStart - c * 4
else Double.NaN;
plot fiveADRMinus       = if showlast_Targets_only == yes and bnumDown < Max(HighestAll(dn), HighestAll(up))
then Double.NaN
else if state == state.short
then closeDownTrendStart - c * 5
else Double.NaN;

SellSignalStopLoss.SetDefaultColor(Color.LIGHT_GREEN);

This should allow an option to just display only the most current set of targets

Code:
``````#ATR_Stoploss_Targets
#
#CHANGELOG
#
# 2021.05.06    @cos251    - Initial release - ATRStoploss Standard TOS indicator with targets after BuySignal or SellSignal trigger
#
#
#CREDITS
# requesed by "@wtf_dude"
#
#

# -- Standard ATR Stoploss Indicator TOS Version
input showlast_Targets_only = yes;
input stopLossMultiplier = 1.0;
input trailType = {default modified, unmodified};
input ATRPeriod = 5;
input ATRFactor = 3.5;
input firstTrade = {default long, short};
input averageType = AverageType.WILDERS;
input debug = no;

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)) {
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 arrowup = if BuySignal then low else Double.NaN;
plot arrowdown = if SellSignal then high else Double.NaN;
arrowup.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
arrowdown.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);
arrowup.SetLineWeight(5);
arrowdown.SetLineWeight(5);
arrowup.SetDefaultColor(Color.GREEN);
arrowdown.SetDefaultColor(Color.RED);
plot TrailingStop = trail;

TrailingStop.SetPaintingStrategy(PaintingStrategy.POINTS);
TrailingStop.DefineColor("Sell", GetColor(1));
TrailingStop.AssignValueColor(if state == state.long
then TrailingStop.Color("Sell")

################################################################
################################################################
input rangeType = { default "trueRange", "ATR", "ADR" };
input ATRlength = 14;
input ATRaverageType = AverageType.WILDERS;
def Range;
if rangeType == rangeType.ATR {
Range = MovingAverage(ATRaverageType, TrueRange(high, close, low), ATRlength);
} else if rangeType == rangeType.trueRange {
Range = trueRange;
} else {
Range = Average(high - low, 7);
}
# -- END ATR/ATR/TrueRange calc

# -- Logic for bar counter and state detector
def bnumUp;
def bnumDown;
def closeUpTrendStart;
def closeDownTrendStart;
def UpTrendBarCount;
def DownTrendBarCount;
def time;
bnumUp = BarNumber();
bnumDown = 0;
closeUpTrendStart = close;
closeDownTrendStart = 0;
UpTrendBarCount = 1;
DownTrendBarCount = 0;
time = GetTime();
} else if state == state.long {
bnumUp = bnumUp[1];
bnumDown = 0;
closeUpTrendStart = closeUpTrendStart[1];
closeDownTrendStart = 0;
UpTrendBarCount = UpTrendBarCount[1] + 1;
DownTrendBarCount = 0;
time = time[1];
} else if SellSignal {
bnumUp = 0;
bnumDown = BarNumber();
closeDownTrendStart = close;
closeUpTrendStart = 0;
UpTrendBarCount = 0;
DownTrendBarCount = 1;
time = GetTime();
} else if state == state.short {
bnumDown = bnumDown[1];
closeDownTrendStart = closeDownTrendStart[1];
DownTrendBarCount = DownTrendBarCount[1] + 1;
bnumUp = 0;
closeUpTrendStart = 0;
UpTrendBarCount = 0;
time = time[1];
} else {
bnumUp = 0;
bnumDown = 0;
closeUpTrendStart = 0;
closeDownTrendStart = 0;
UpTrendBarCount = 0;
DownTrendBarCount = 0;
time = Double.NaN;
}
def c;
if BarNumber() == bnumUp or BarNumber() == bnumDown {
c = Range;
} else if state == state.long or state == state.short {
c = c[1];
} else {
c = 0;
}

################################################################
##########       Target and Stop Loss Plots            #########
################################################################

def up = if arrowup   then bnumUp   else Double.NaN;
def dn = if arrowdown then bnumDown else Double.NaN;

plot BuySignalStopLoss = if showlast_Targets_only == yes and (bnumUp < HighestAll(up))
then Double.NaN
else if state == state.long
then closeUpTrendStart - (c * stopLossMultiplier)
else Double.NaN ;
plot oneADRPlus        = if showlast_Targets_only == yes and (bnumUp < Max(HighestAll(dn), HighestAll(up)))
then Double.NaN
else if state == state.long
then closeUpTrendStart + c
else Double.NaN;
plot twoADRPlus        = if showlast_Targets_only == yes and (bnumUp < Max(HighestAll(dn), HighestAll(up)))
then Double.NaN
else if state == state.long
then closeUpTrendStart + c * 2
else Double.NaN;
plot threeADRPlus      = if showlast_Targets_only == yes and (bnumUp < Max(HighestAll(dn), HighestAll(up)))
then Double.NaN
else if state == state.long
then closeUpTrendStart + c * 3
else Double.NaN;
plot fourADRPlus       = if showlast_Targets_only == yes and (bnumUp < Max(HighestAll(dn), HighestAll(up)))
then Double.NaN
else if state == state.long
then closeUpTrendStart + c * 4
else Double.NaN;
plot fiveADRPlus       = if showlast_Targets_only == yes and (bnumUp < Max(HighestAll(dn), HighestAll(up)))
then Double.NaN
else if state == state.long
then closeUpTrendStart + c * 5
else Double.NaN;

plot SellSignalStopLoss = if showlast_Targets_only == yes and bnumDown < Max(HighestAll(dn), HighestAll(up))
then Double.NaN
else if state == state.short
then closeDownTrendStart + (c * stopLossMultiplier)
else Double.NaN;
plot oneADRMinus        = if showlast_Targets_only == yes and bnumDown < Max(HighestAll(dn), HighestAll(up))
then Double.NaN
else if state == state.short
then closeDownTrendStart - c
else Double.NaN;
plot twoADRMinus        = if showlast_Targets_only == yes and bnumDown < Max(HighestAll(dn), HighestAll(up))
then Double.NaN
else if state == state.short
then closeDownTrendStart - c * 2
else Double.NaN;
plot threeADRMinus      = if showlast_Targets_only == yes and bnumDown < Max(HighestAll(dn), HighestAll(up))
then Double.NaN
else if state == state.short
then closeDownTrendStart - c * 3
else Double.NaN;
plot fourADRMinus       = if showlast_Targets_only == yes and bnumDown < Max(HighestAll(dn), HighestAll(up))
then Double.NaN
else if state == state.short
then closeDownTrendStart - c * 4
else Double.NaN;
plot fiveADRMinus       = if showlast_Targets_only == yes and bnumDown < Max(HighestAll(dn), HighestAll(up))
then Double.NaN
else if state == state.short
then closeDownTrendStart - c * 5
else Double.NaN;

SellSignalStopLoss.SetDefaultColor(Color.LIGHT_GREEN);

Nice!! Thanks

This should allow an option to just display only the most current set of targets

Code:
``````#ATR_Stoploss_Targets
#
#CHANGELOG
#
# 2021.05.06    @cos251    - Initial release - ATRStoploss Standard TOS indicator with targets after BuySignal or SellSignal trigger
#
#
#CREDITS
# requesed by "@wtf_dude"
#
#

# -- Standard ATR Stoploss Indicator TOS Version
input showlast_Targets_only = yes;
input stopLossMultiplier = 1.0;
input trailType = {default modified, unmodified};
input ATRPeriod = 5;
input ATRFactor = 3.5;
input firstTrade = {default long, short};
input averageType = AverageType.WILDERS;
input debug = no;

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)) {
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 arrowup = if BuySignal then low else Double.NaN;
plot arrowdown = if SellSignal then high else Double.NaN;
arrowup.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
arrowdown.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);
arrowup.SetLineWeight(5);
arrowdown.SetLineWeight(5);
arrowup.SetDefaultColor(Color.GREEN);
arrowdown.SetDefaultColor(Color.RED);
plot TrailingStop = trail;

TrailingStop.SetPaintingStrategy(PaintingStrategy.POINTS);
TrailingStop.DefineColor("Sell", GetColor(1));
TrailingStop.AssignValueColor(if state == state.long
then TrailingStop.Color("Sell")

################################################################
################################################################
input rangeType = { default "trueRange", "ATR", "ADR" };
input ATRlength = 14;
input ATRaverageType = AverageType.WILDERS;
def Range;
if rangeType == rangeType.ATR {
Range = MovingAverage(ATRaverageType, TrueRange(high, close, low), ATRlength);
} else if rangeType == rangeType.trueRange {
Range = trueRange;
} else {
Range = Average(high - low, 7);
}
# -- END ATR/ATR/TrueRange calc

# -- Logic for bar counter and state detector
def bnumUp;
def bnumDown;
def closeUpTrendStart;
def closeDownTrendStart;
def UpTrendBarCount;
def DownTrendBarCount;
def time;
bnumUp = BarNumber();
bnumDown = 0;
closeUpTrendStart = close;
closeDownTrendStart = 0;
UpTrendBarCount = 1;
DownTrendBarCount = 0;
time = GetTime();
} else if state == state.long {
bnumUp = bnumUp[1];
bnumDown = 0;
closeUpTrendStart = closeUpTrendStart[1];
closeDownTrendStart = 0;
UpTrendBarCount = UpTrendBarCount[1] + 1;
DownTrendBarCount = 0;
time = time[1];
} else if SellSignal {
bnumUp = 0;
bnumDown = BarNumber();
closeDownTrendStart = close;
closeUpTrendStart = 0;
UpTrendBarCount = 0;
DownTrendBarCount = 1;
time = GetTime();
} else if state == state.short {
bnumDown = bnumDown[1];
closeDownTrendStart = closeDownTrendStart[1];
DownTrendBarCount = DownTrendBarCount[1] + 1;
bnumUp = 0;
closeUpTrendStart = 0;
UpTrendBarCount = 0;
time = time[1];
} else {
bnumUp = 0;
bnumDown = 0;
closeUpTrendStart = 0;
closeDownTrendStart = 0;
UpTrendBarCount = 0;
DownTrendBarCount = 0;
time = Double.NaN;
}
def c;
if BarNumber() == bnumUp or BarNumber() == bnumDown {
c = Range;
} else if state == state.long or state == state.short {
c = c[1];
} else {
c = 0;
}

################################################################
##########       Target and Stop Loss Plots            #########
################################################################

def up = if arrowup   then bnumUp   else Double.NaN;
def dn = if arrowdown then bnumDown else Double.NaN;

plot BuySignalStopLoss = if showlast_Targets_only == yes and (bnumUp < HighestAll(up))
then Double.NaN
else if state == state.long
then closeUpTrendStart - (c * stopLossMultiplier)
else Double.NaN ;
plot oneADRPlus        = if showlast_Targets_only == yes and (bnumUp < Max(HighestAll(dn), HighestAll(up)))
then Double.NaN
else if state == state.long
then closeUpTrendStart + c
else Double.NaN;
plot twoADRPlus        = if showlast_Targets_only == yes and (bnumUp < Max(HighestAll(dn), HighestAll(up)))
then Double.NaN
else if state == state.long
then closeUpTrendStart + c * 2
else Double.NaN;
plot threeADRPlus      = if showlast_Targets_only == yes and (bnumUp < Max(HighestAll(dn), HighestAll(up)))
then Double.NaN
else if state == state.long
then closeUpTrendStart + c * 3
else Double.NaN;
plot fourADRPlus       = if showlast_Targets_only == yes and (bnumUp < Max(HighestAll(dn), HighestAll(up)))
then Double.NaN
else if state == state.long
then closeUpTrendStart + c * 4
else Double.NaN;
plot fiveADRPlus       = if showlast_Targets_only == yes and (bnumUp < Max(HighestAll(dn), HighestAll(up)))
then Double.NaN
else if state == state.long
then closeUpTrendStart + c * 5
else Double.NaN;

plot SellSignalStopLoss = if showlast_Targets_only == yes and bnumDown < Max(HighestAll(dn), HighestAll(up))
then Double.NaN
else if state == state.short
then closeDownTrendStart + (c * stopLossMultiplier)
else Double.NaN;
plot oneADRMinus        = if showlast_Targets_only == yes and bnumDown < Max(HighestAll(dn), HighestAll(up))
then Double.NaN
else if state == state.short
then closeDownTrendStart - c
else Double.NaN;
plot twoADRMinus        = if showlast_Targets_only == yes and bnumDown < Max(HighestAll(dn), HighestAll(up))
then Double.NaN
else if state == state.short
then closeDownTrendStart - c * 2
else Double.NaN;
plot threeADRMinus      = if showlast_Targets_only == yes and bnumDown < Max(HighestAll(dn), HighestAll(up))
then Double.NaN
else if state == state.short
then closeDownTrendStart - c * 3
else Double.NaN;
plot fourADRMinus       = if showlast_Targets_only == yes and bnumDown < Max(HighestAll(dn), HighestAll(up))
then Double.NaN
else if state == state.short
then closeDownTrendStart - c * 4
else Double.NaN;
plot fiveADRMinus       = if showlast_Targets_only == yes and bnumDown < Max(HighestAll(dn), HighestAll(up))
then Double.NaN
else if state == state.short
then closeDownTrendStart - c * 5
else Double.NaN;

SellSignalStopLoss.SetDefaultColor(Color.LIGHT_GREEN);

Beautiful, I had been trying to figure out how to do that for a while. Nice work. I will incorporate that to the RSM as a feature as well.

In case you guys are curious, I'm experimenting with incorporating pyramiding into my strategy. ie Adding .5%, .3%. .2% risk on each subsequent ATR. If you guys have any thoughts or suggestions, throw em out here. Also curious if you go-to settings for your ATR calculation. I still haven't found one I completely love yet.

Settings I'm using on daily if anybody is interested based on fibs of the atr.

Settings I'm using on daily if anybody is interested based on fibs of the atr.
@wtf_dude You nailed. Holy grail of FIBs. Ive copied the noted study from @cos251 & @SleepyZ but I dont see the "target1mult" etc. Please advise where this is coming from.

@wtf_dude You nailed. Holy grail of FIBs. Ive copied the noted study from @cos251 & @SleepyZ but I dont see the "target1mult" etc. Please advise where this is coming from.
"closeUpTrendStart + c *target1mult" so it looks like the trend switches and then the atr target is multiplied by the first close after the trend switch. @cos251 may have to chime in

87k+ Posts
260 Online

## 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
• 1 full year of unlimited support

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?