conversion of Swingarm ATR Trailing Stop

Viewing 12 posts - 1 through 12 (of 12 total)
  • Author
    Posts
  • #139711 quote
    discojones
    Participant
    Junior

    Background from the author of the strategy including pictures:

    The SwingArm is an idea I had using the ATRTrailingStop and modifying it to have FIB Retracements into it. “I am not a coder…” I hired someone to help me out in getting it done. 

    It can be used on any timeframe. The best for me (day trading) is the 10-minute chart. For Swing Trading, the 4 hours chart works well. Alerts can be set up using an ATRTrailingStop scan

     

    Thank you in advance!

    The Code:

    # 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.POINTS);
    TrailingStop.DefineColor("Long", Color.GREEN);
    TrailingStop.DefineColor("Short", Color.RED);
    TrailingStop.AssignValueColor(if state == state.long
    then TrailingStop.Color("Long")
    else TrailingStop.Color("Short"));
    
    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);`
    Razz thanked this post
    grdZgcX.jpg grdZgcX.jpg
    #139802 quote
    Nicolas
    Keymaster
    Master

    I added this conversion on my list, please remind me if you don’t get it before August! Thanks 😉

    Razz and discojones thanked this post
    #139849 quote
    discojones
    Participant
    Junior

    Thanks Nicolas!

    #140119 quote
    Razz
    Participant
    Master

    Hello Nicolas the indicator looks very good I hope you can integrate it in Pro Realtime. Thanks in advance .

    discojones thanked this post
    #140120 quote
    Nicolas
    Keymaster
    Master

    Seems to be a simple ATR trailing stop with bands made with Fibonacci levels. Will look at that conversion asap.

    discojones thanked this post
    #140605 quote
    Razz
    Participant
    Master

    Hello Nicolas did you have some time to have a look at it? Think they probably have more important things to do.

    greetings

    #140743 quote
    Nicolas
    Keymaster
    Master

    I’m working on it, please be patient 😉

    Razz thanked this post
    #140745 quote
    Nicolas
    Keymaster
    Master

    Here is the translated code, please confirm it works as the original one so I can make it available in the library, thank you.

    //PRC_Swingarm ATR Trailing Stop | indicator
    //03.08.2020
    //Nicolas @ www.prorealcode.com
    //Sharing ProRealTime knowledge
    //converted from TOS
    //https://www.prorealcode.com/topic/conversion-of-swingarm-atr-trailing-stop/
    
    // --- settings 
    trailType = 1  //1=modified, 0=unmodified
    ATRPeriod = 28
    ATRFactor = 5
    firstTrade = 0 //0= long, 1= short
    averageType = 3 //0 = SMA 1 = EMA 2 = WMA 3 = Wilder 4 = Triangular 5 = End point 6 = Time series 7 = Hull (PRT v11 only) 8 = ZeroLag  (PRT v11 only)
    //--- end of settings
     
    fib1Level = 61.8
    fib2Level = 78.6
    fib3Level = 88.6
    
    HiLo = Min(high - low, 1.5 * Average[ATRPeriod](range))
     
    if low <= high[1] then 
    Href = high - close[1] 
    else 
    Href = (high - close[1]) - 0.5 * (low - high[1])
    endif
    
    if high >= low[1] then 
    Lref = close[1] - low
    else 
    Lref = (close[1] - low) - 0.5 * (low[1] - high)
    endif
    
    //case modified:
    if trailType = 1 then 
    trueRange = Max(HiLo, Max(HRef, LRef))
    else
    //case unmodified
    trueRange = tr(close) // TrueRange(high, close, low)
    endif
    
    iloss = ATRFactor * Average[ATRPeriod,averageType](trueRange)
    
    once init=0
    if init=0 then 
    if firsttrade=0 then
    state = 0
    trail = close - iloss
    else
    state = 1 
    trail = close + iloss
    endif
    init=1
    endif 
    
    //case long:
    if state[1] = 0 then 
    if (close > trail[1]) then 
    state = 0
    trail = Max(trail[1], close - iloss)
    else 
    state = 1
    trail = close + iloss
    endif
    endif
    //case short:
    if state[1] = 1 then 
    if (close < trail[1]) then 
    state = 1
    trail = Min(trail[1], close + iloss)
    else 
    state = 0
    trail = close - iloss
    endif
    endif
    
    BuySignal = state<>state[1] and state = 0 
    SellSignal = state<>state[1] and state = 1 
     
    if BuySignal then 
    ex = high 
    elsif SellSignal then 
    ex = low 
    else 
    if state = 0 then 
    ex = Max(ex[1], high) 
    elsif state = 1 then 
    ex = Min(ex[1], low) 
    else 
    ex = ex[1]
    endif
    endif
     
    TrailingStop = trail
    if state = 0 then 
    r=0
    g=255
    else
    r=255
    g=0
    endif
    
    f1 = ex + (trail - ex) * fib1Level / 100
    f2 = ex + (trail - ex) * fib2Level / 100
    f3 = ex + (trail - ex) * fib3Level / 100
    
    l1 = state[1] = 0 and close crosses under f1[1]
    l2 = state[1] = 0 and close crosses under f2[1]
    l3 = state[1] = 0 and close crosses under f3[1]
    s1 = state[1] = 1 and close crosses over f1[1]
    s2 = state[1] = 1 and close crosses over f2[1]
    s3 = state[1] = 1 and close crosses over f3[1]
    
    atr = AverageTrueRange[14](close)
     
    if l1 then 
    y =low - atr 
    elsif l2 then 
    y=low - 1.5 * atr 
    elsif l3 then 
    y=low - 2 * atr 
    elsif s1 then 
    y=high + atr 
    elsif s2 then 
    y=high + 1.5 * atr 
    elsif s3 then 
    y=high + 2 * atr 
    else 
    y=0
    endif
    
    if y>0 then 
    if y>close then 
    drawarrowdown(barindex,y) coloured(r,g,0)
    else
    drawarrowup(barindex,y) coloured(r,g,0)
    endif
    endif
    
    return TrailingStop coloured(r,g,0) style(line,3) as "ATR Trailing Stop" ,state as "state", ex coloured(r,g,0) style(point,4) as "Extremum", f1 coloured(168,168,168), f2 coloured(168,168,168), f3 coloured(168,168,168)
    

    I think that the arrows are way too far from the price, but that’s how it is coded in the original code?!

    Razz and discojones thanked this post
    swing-atr-trailing-stop.png swing-atr-trailing-stop.png
    #140785 quote
    Razz
    Participant
    Master

    Hello Nicolas Thank you for your implementation, I do not know the original indicator but your version looks good. I think I will deactivate the arrows first. Great job thanks

    #140793 quote
    discojones
    Participant
    Junior

    Thanks Nicolas that looks great!

     

    I agree regarding the arrows

    #140814 quote
    Nicolas
    Keymaster
    Master

    Ok, I will replace the arrows closer to the price and post it in the library for everyone to benefit of this trend following indicator.

    discojones thanked this post
    #140819 quote
    Nicolas
    Keymaster
    Master

    I published the final version in our ProRealTime code library here: SwingARM ATR Trailing Stop

    discojones thanked this post
Viewing 12 posts - 1 through 12 (of 12 total)
  • You must be logged in to reply to this topic.

conversion of Swingarm ATR Trailing Stop


ProBuilder: Indicators & Custom Tools

New Reply
Author
author-avatar
discojones @discojones Participant
Summary

This topic contains 11 replies,
has 3 voices, and was last updated by Nicolas
5 years, 6 months ago.

Topic Details
Forum: ProBuilder: Indicators & Custom Tools
Language: English
Started: 07/20/2020
Status: Active
Attachments: 2 files
Logo Logo
Loading...