#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"
#
#LINK
# https://usethinkscript.com/threads/target-plots-after-buy-signals.6529/
#
# -- 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)) {
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 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("Buy", GetColor(0));
TrailingStop.DefineColor("Sell", GetColor(1));
TrailingStop.AssignValueColor(if state == state.long
then TrailingStop.Color("Sell")
else TrailingStop.Color("Buy"));
AddLabel(debug, BuySignal);
AddLabel(debug, SellSignal);
AddLabel(debug, trail);
AddLabel(debug, state);
################################################################
########## ATR/ADR Calc #########
################################################################
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;
if BuySignal {
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;
}
AddLabel(debug, "bnumup:" + bnumUp);
AddLabel(debug, "closeUpTrendStart" + closeUpTrendStart);
AddLabel(debug, "bnumdown:" + bnumDown);
AddLabel(debug, "closeDownTrendStart" + closeDownTrendStart);
AddLabel(debug, "c:" + c);
AddLabel(debug, "Range:" + Range);
AddLabel(debug, "trueRange:" + trueRange);
AddLabel(debug, "time:" + time);
################################################################
########## Target and Stop Loss Plots #########
################################################################
def up = if arrowup then bnumUp else Double.NaN;
def dn = if arrowdown then bnumDown else Double.NaN;
AddLabel(debug,"up:"+up);
AddLabel(debug,"dn:"+dn);
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;
BuySignalStopLoss.SetDefaultColor(Color.LIGHT_RED);
oneADRPlus.SetDefaultColor(Color.LIGHT_GREEN);
twoADRPlus.SetDefaultColor(Color.LIGHT_GREEN);
threeADRPlus.SetDefaultColor(Color.LIGHT_GREEN);
fourADRPlus.SetDefaultColor(Color.LIGHT_GREEN);
fiveADRPlus.SetDefaultColor(Color.LIGHT_GREEN);
SellSignalStopLoss.SetDefaultColor(Color.LIGHT_GREEN);
oneADRMinus.SetDefaultColor(Color.LIGHT_RED);
twoADRMinus.SetDefaultColor(Color.LIGHT_RED);
threeADRMinus.SetDefaultColor(Color.LIGHT_RED);
fourADRMinus.SetDefaultColor(Color.LIGHT_RED);
fiveADRMinus.SetDefaultColor(Color.LIGHT_RED);
oneADRMinus.SetHiding(GetTime() < time);
AddCloud(fiveADRPlus, BuySignalStopLoss, Color.LIGHT_GREEN);
AddCloud(SellSignalStopLoss, fiveADRMinus, Color.PLUM);