###################################
# Moving Average with ATR Trailing Stop Strategy
# @congtran1188
# requested by @TMac
input price = close;
input fastLength = 20;
input slowLength = 50;
input displace_fast = 0;
input displace_slow = 0;
input averageType = AverageType.EXPONENTIAL;
plot FastMA = MovingAverage(averageType, price, fastLength)[-displace_fast];
plot SlowMA = MovingAverage(averageType, price, slowLength)[-displace_slow];
FastMA.SetDefaultColor(GetColor(1));
SlowMA.SetDefaultColor(GetColor(2));
def ema1 = expaverage(close,8);
def ema2 = expaverage(close,20);
def crossup = fastma > slowma;
def crossdn = fastma < slowma;
def countup = if crossup and !crossup[1] then 1 else countup[1]+1;
def countdn = if crossdn and !crossdn[1] then 1 else countdn[1]+1;
addlabel(1, if crossup then "Buy, " + countup
else if crossdn then "Sell, " + countdn
else "Equal", color.black);
assignpricecolor(if crossup then color.green
else if crossdn then color.red
else color.gray);
####################################
#ATR Trailing Stop.
####################################
#
# TD Ameritrade IP Company, Inc. (c) 2009-2022
#
input trailType = {default modified, unmodified};
input ATRPeriod = 5;
input ATRFactor = 3.5;
input firstTrade = {default long, short};
input averageType1 = 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(averageType1, 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"));
##################################
AssignPriceColor( if crossup and close<trail then color.white
else if crossdn and close>trail then color.white
else if crossup and close>trail then color.green
else if crossdn and close<trail then color.red else Color.black);
AddOrder(OrderType.BUY_AUTO, crossup and close>trail, tickcolor = Color.CYAN, arrowcolor = Color.CYAN, name = "BUY");
AddOrder(OrderType.SELL_TO_CLOSE, crossup and close<trail or crossdn and close>trail, tickcolor = Color.MAGENTA, arrowcolor = Color.MAGENTA, name = "Sell To Close");
################################################################
########## SELL ###########
################################################################
AddOrder(OrderType.SELL_AUTO, crossdn and close<trail , tickcolor = Color.MAGENTA, arrowcolor = Color.MAGENTA, name = "SELL");
AddOrder(OrderType.BUY_TO_CLOSE, crossup and close<trail or crossdn and close>trail , tickcolor = Color.CYAN, arrowcolor = Color.CYAN, name = "Buy To Close");