Do you know a AFL-Code for ATR based Stop Loss like in the picture?

ATR

It is from TradingView, I'm looking for a code like this in AFL. Search in Google. The origin Code from TradingView comes below. Do you have any idea?

//@version=5
indicator(title='Average True Range Stop Loss Finder', shorttitle='ATR', overlay=true)
length = input.int(title='Length', defval=14, minval=1)
smoothing = input.string(title='Smoothing', defval='RMA', options=['RMA', 'SMA', 'EMA', 'WMA'])
m = input(1.5, 'Multiplier')
src1 = input(high)
src2 = input(low)
pline = input(true, 'Show Price Lines')
col1 = input(color.blue, 'ATR Text Color')
col2 = input.color(color.teal, 'Low Text Color', inline='1')
col3 = input.color(color.red, 'High Text Color', inline='2')

collong = input.color(color.teal, 'Low Line Color', inline='1')
colshort = input.color(color.red, 'High Line Color', inline='2')

ma_function(source, length) =>
    if smoothing == 'RMA'
        ta.rma(source, length)
    else
        if smoothing == 'SMA'
            ta.sma(source, length)
        else
            if smoothing == 'EMA'
                ta.ema(source, length)
            else
                ta.wma(source, length)

a = ma_function(ta.tr(true), length) * m
x = ma_function(ta.tr(true), length) * m + src1
x2 = src2 - ma_function(ta.tr(true), length) * m

p1 = plot(x, title='ATR Short Stop Loss', color=colshort, trackprice=pline ? true : false, transp=20)
p2 = plot(x2, title='ATR Long Stop Loss', color=collong, trackprice=pline ? true : false, transp=20)

var table Table = table.new(position.bottom_center, 3, 1, border_width=3)

f_fillCell(_table, _column, _row, _value, _timeframe) =>

    _cellText = _timeframe + str.tostring(_value, '#.#')
    table.cell(_table, _column, _row, _cellText, text_color=col1)
    table.cell_set_text_color(Table, 1, 0, color.new(col3, transp=0))
    table.cell_set_text_color(Table, 2, 0, color.new(col2, transp=0))

if barstate.islast
    f_fillCell(Table, 0, 0, a, 'ATR: ')
    f_fillCell(Table, 1, 0, x, 'H: ')
    f_fillCell(Table, 2, 0, x2, 'L: ')


@EveryDayBetter that does not look like a trailing stop but just ATR bands (also known as Keltner Bands) around a moving average.

You can search this forum for Keltner bands or the user library like here,
http://www.amibroker.com/members/library/formula.php?id=189

If you are actually looking for an example of ATR trailing stops, again you can search on this forum. Additionally you can find an example here
http://www.amibroker.com/members/traders/05-2009.html

3 Likes

The keyword "ATR bands" helped me to find the right code. Thank you very much.

1 Like

This topic was automatically closed 100 days after the last reply. New replies are no longer allowed.