# blackFLAG FTS SwingArms
# StudyName: blackFLAG_Futures_SwingArm_ATRTrail
# My preferred setting is 28 / 5 FOR ALL TIMEFRAMES
# Edited by: Jose Azcarate
# blackFLAG Futures Trading - FOR EDUCATIONAL PURPOSES ONLY
# TWITTER: @blackflagfuture
# ** Updated code below is to be used for custom watchlist column only ***
input trailType = {default modified, unmodified};
input ATRPeriod = 28;
input ATRFactor = 5;
input firstTrade = {default long, short};
input averageType = AverageType.WILDERS;
input fib1Level = 61.8;
input fib2Level = 78.6;
input fib3Level = 88.6;
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);
def ex = if BuySignal then high else if SellSignal then low else if state == state.long then Max(ex[1], high) else if state == state.short then Min(ex[1], low) else ex[1];
def TrailingStop = trail;
def f1 = ex + (trail - ex) * fib1Level / 100;
def f2 = ex + (trail - ex) * fib2Level / 100;
def f3 = ex + (trail - ex) * fib3Level / 100;
def l100 = trail + 0;
def Fib1 = f1;
def Fib2 = f2;
def Fib3 = f3;
def bullAboveZone = state == state.long and close > Fib1;
def bullZone2 = state == state.long and close <= Fib1 and close > Fib2;
def bullZone3 = state == state.long and close <= Fib2 and close > Fib3;
def bullZone4 = state == state.long and close <= Fib3 and close > TrailingStop;
def bearZone2 = state == state.short and close >= Fib1 and close < Fib2;
def bearZone3 = state == state.short and close >= Fib2 and close < Fib3;
def bearZone4 = state == state.short and close >= Fib3 and close < TrailingStop;
# watchlist
assignBackgroundColor(if state == state.long then Color.green else Color.red);
AddLabel(yes, if bullZone2 then "z2" else if bullZone3 then "z3" else if bullZone4 then "z4" else if bearZone2 then "z2" else if bearZone3 then "z3" else if bearZone4 then "z4" else " ", if bullZone4 or bearZone4 then color.white else Color.BLACK);