True Range Momentum
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © maksumit

//@version=5
indicator("True Range Momentum", "TRM")

HL = input.bool(true, "High/Low")
D = input.bool(true, "Delay")
len = input.int(10, "Length", 1)
alen = input.int(5, "ATR Length", 1)
mult = input.float(3, "ATR Multiplier")
stoplag = input.int(10, "Stop Lag", 1)
tableC = input.color(color.gray, "Table Color")
tableT = input.color(color.white, "Table Text Color")
tableP = input.string(position.bottom_right, "Position", [position.top_left, position.top_center, position.top_right, position.middle_left, position.middle_center, position.middle_right, position.bottom_left, position.bottom_center, position.bottom_right])

//ATR
atr = ta.ema(ta.tr, alen)

//LONG STOP
h = ta.highest(HL ? high:close,len)
pls = (D ? h[1]:h) - (atr * mult)
ls = ta.highest(pls, stoplag)

//SHORT STOP
l = ta.lowest(HL ? low:close,len)
pss = (D ? l[1]:l) + (atr * mult)
ss = ta.lowest(pss, stoplag)

//ATR MOMENTUM
lM = (close - ls)
sM = (ss - close)

//Allocation
lA = lM > 0 and sM < 0 ? 100:lM > 0 and sM > 0 ? 50:0
sA = sM > 0 and lM < 0 ? 100:lM > 0 and sM > 0 ? 50:0

//PLOTS
plot(lM, "M+", color.green)
plot(sM, "M-", color.red)
hline(0, "0", color.white, hline.style_dashed)


var table myTable = table.new(tableP, 2, 7, border_width=1)
if barstate.islast

//Table Cells
    table.cell(myTable, 0,1, "Long: " + str.tostring(lA), text_color = tableT, bgcolor = tableC)
    table.cell(myTable, 1,1, "Short: " + str.tostring(sA), text_color = tableT, bgcolor = tableC)
    