Swingarm Indicator Conversion from TOS

Hi all,

I have Jose’s permission to ask for a conversion of his TOS indicator from TOS to Tradovate. The code is listed on his website and usethinkscript forum. All rights and credits are his, and full permission has been granted from him to share his code for help with conversion from TOS to Tradovate.

https://blackflagfuturestrading.com/swingarm-and-usethinkscript/

TOS chart share is below:

tos.mx/jhDv0Pt

Code is below:

Original Code From: TD Ameritrade IP Company, Inc. (c) 2009-2020

Original StudyName: ATRTrailingStop

Type: Study

blackFLAG FTS SwingArms

Edited by: Jose Azcarate

blackFLAG Futures Trading - FOR EDUCATIONAL PURPOSES ONLY

TWITTER: @blackflagfuture

Settings Vary. My preferred setting is 28 / 5 But also use 30 / 8 and 5 / 3.5 depending on strategy.

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];

plot TrailingStop = trail;

TrailingStop.SetPaintingStrategy(PaintingStrategy.LINE);
TrailingStop.DefineColor(“Long”, Color.GREEN);
TrailingStop.DefineColor(“Short”, Color.RED);
TrailingStop.AssignValueColor(if state == state.long
then TrailingStop.Color(“Long”)
else TrailingStop.Color(“Short”));
TrailingStop.SetLineWeight(3);

plot Extremum = ex;
Extremum.SetPaintingStrategy(PaintingStrategy.POINTS);
Extremum.DefineColor(“HH”, Color.GREEN);
Extremum.DefineColor(“LL”, Color.RED);
Extremum.AssignValueColor(if state == state.long
then Extremum.Color(“HH”)
else Extremum.Color(“LL”));
Extremum.Hide();

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;

plot Fib1 = f1;
Fib1.SetPaintingStrategy(PaintingStrategy.POINTS);
Fib1.SetDefaultColor(Color.BLACK);
Fib1.Hide();

plot Fib2 = f2;
Fib2.SetPaintingStrategy(PaintingStrategy.POINTS);
Fib2.SetDefaultColor(Color.BLACK);
Fib2.Hide();

plot Fib3 = f3;
Fib3.SetPaintingStrategy(PaintingStrategy.POINTS);
Fib3.SetDefaultColor(Color.BLACK);
Fib3.Hide();

AddCloud(f1, f2, Color.LIGHT_GREEN, Color.LIGHT_RED, no);
AddCloud(f2, f3, Color.GREEN, Color.RED, no);
AddCloud(f3, l100, Color.DARK_GREEN, Color.DARK_RED, no);

def l1 = state[1] == state.long and close crosses below f1[1];
def l2 = state[1] == state.long and close crosses below f2[1];
def l3 = state[1] == state.long and close crosses below f3[1];
def s1 = state[1] == state.short and close crosses above f1[1];
def s2 = state[1] == state.short and close crosses above f2[1];
def s3 = state[1] == state.short and close crosses above f3[1];

def atr = Average(TrueRange(high, close, low), 14);

plot LS1 = if l1 then low - atr else Double.NaN;
plot LS2 = if l2 then low - 1.5 * atr else Double.NaN;
plot LS3 = if l3 then low - 2 * atr else Double.NaN;
plot SS1 = if s1 then high + atr else Double.NaN;
plot SS2 = if s2 then high + 1.5 * atr else Double.NaN;
plot SS3 = if s3 then high + 2 * atr else Double.NaN;

LS1.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
LS1.SetDefaultColor(Color.GREEN);
LS1.SetLineWeight(1);
LS1.Hide();
LS2.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
LS2.SetDefaultColor(Color.GREEN);
LS2.SetLineWeight(1);
LS2.Hide();
LS3.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
LS3.SetDefaultColor(Color.GREEN);
LS3.SetLineWeight(1);
LS3.Hide();

SS1.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);
SS1.SetDefaultColor(Color.RED);
SS1.SetLineWeight(1);
SS1.Hide();
SS2.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);
SS2.SetDefaultColor(Color.RED);
SS2.SetLineWeight(1);
SS2.Hide();
SS3.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);
SS3.SetDefaultColor(Color.RED);
SS3.SetLineWeight(1);
SS3.Hide();

Alert(l1, “Price crossed below Fib1 level in long trend”, Alert.BAR, Sound.Bell);
Alert(l2, “Price crossed below Fib2 level in long trend”, Alert.BAR, Sound.Bell);
Alert(l3, “Price crossed below Fib3 level in long trend”, Alert.BAR, Sound.Bell);
Alert(s1, “Price crossed above Fib1 level in short trend”, Alert.BAR, Sound.Bell);
Alert(s2, “Price crossed above Fib2 level in short trend”, Alert.BAR, Sound.Bell);
Alert(s3, “Price crossed above Fib3 level in short trend”, Alert.BAR, Sound.Bell);

AddLabel(yes, if ohlc4 > TrailingStop then “BULLISH SWINGARM” else “BEARISH SWINGARM”, if ohlc4 > TrailingStop then Color.GREEN else Color.Red);

It appears a member of the community may have made this indicator available in the Community Indicators area

Yes, he’s from the usethinkscript community, Xzeryn who wrote the entire script for Tradovate. I tested with him on the indicator and it now works as intended.

1 Like

I can see the fib retracement zones with this indicator, but I do not see the swing arms. Is this correct?

Swingarm is the name. You will see the zones within the fib levels.

Got it, thanks. I will review the thread on that thinkscript forum. What time frame do you find it works best on for futures?

I don’t use it the official way, but you can ask questions in the forum, the original creator Jose will answer them.

1 Like

I just looked in the community indicators and didn’t see it, any idea what it is labeled under?

How can we get swingarm indicator in tradovate? i did not find it under community indicators

Try the “ATR trail stop w/Fib levels” indicator in the community area. I think this is near to the swingarm.