Donchian Trend Signals
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © OmegaTools

//@version=5
indicator("Donchian Trend Signals", "DTS", overlay = true)

lnt = input(21, "Length")
cmp = input.bool(false, "Complex mode", "Transform the average line into the average of multiple length average line, respectively of half the length you have chosen, the length, and double the length")

lnt05 = math.round(lnt / 2)
lnt1 = math.round(lnt * 2)

h0 = ta.highest(close, lnt)
l0 = ta.lowest(close, lnt)
m0 = math.avg(h0, l0)

h05 = ta.highest(close, lnt05)
l05 = ta.lowest(close, lnt05)
m05 = math.avg(h05, l05)

h1 = ta.highest(close, lnt1)
l1 = ta.lowest(close, lnt1)
m1 = math.avg(h1, l1)

m = cmp == false ? m0 : math.avg(m0, m05, m1)
h = cmp == false ? h0 : math.avg(h0, h05, h1)
l = cmp == false ? l0 : math.avg(l0, l05, l1)

trend = 0
trend := if m > m[1]
    trend := 1
else if m < m[1]
    trend := -1
else if m == m[1]
    trend := trend[1]
col = trend == 1 ? #2962ff : trend == -1 ? #e91e63 : color.gray

vol = volume > ta.sma(volume, 9)
sgrab = high > h and close < h and vol
bgrab = low < l and close > l and vol
rng = math.avg(high - low, math.abs(close-open))
enb = rng > rng[1] and close > open
ens = rng > rng[1] and close < open

buy = true
sell = true
lbr = close > h[1]
sbr = close < l[1]
lastbs = 0
lastbs := lbr ? nz(lastbs[1]) + 1 : sbr ? 0 : nz(lastbs[1])
lastss = 0
lastss := sbr ? nz(lastss[1]) + 1 : lbr ? 0 : nz(lastss[1])
if lastbs[1] > 0
    buy := false
if lastss[1] > 0
    sell := false

filt = (math.abs(close - open) * 1.25) > (high - low)

plot(h, "Higher price", color.gray, 1, plot.style_linebr)
plot(l, "Lower price", color.gray, 1, plot.style_linebr)
plot(m, "Average price", col, 2)
barcolor(col, title = "Trend coloring")
plotshape(bgrab, "Liquidity buy", shape.diamond, location.belowbar, enb ? #2962ff : color.gray)
plotshape(sgrab, "Liquidity sell", shape.diamond, location.abovebar, ens ? #e91e63 : color.gray)
plotshape(lbr and buy and vol, "Breakout buy", shape.triangleup, location.belowbar,  filt ? #2962ff : color.gray)
plotshape(sbr and sell and vol, "Breakout sell", shape.triangledown, location.abovebar,  filt ? #2962ff : color.gray)