// ____ __ ___ ________ ___________ ___________ __ ____ ___
// / __ )/ / / | / ____/ //_/ ____/ |/_ __< / // / / __ |__ \
// / __ / / / /| |/ / / ,< / / / /| | / / / / // /_/ / / __/ /
// / /_/ / /___/ ___ / /___/ /| / /___/ ___ |/ / / /__ __/ /_/ / __/
// /_____/_____/_/ |_\____/_/ |_\____/_/ |_/_/ /_/ /_/ \____/____/
// This source code is subject to the terms of the Mozilla Public License 2.0 at <a href="https://mozilla.org/MPL/2.0/%22 rel="nofollow noopener noreferrer" target="_blank" class="apply-common-tooltip" title="https://mozilla.org/MPL/2.0/%22%3Emozilla.org/MPL/2.0/%3C/a%3E
// © blackcat1402
//@version=5
indicator(title="[blackcat] L2 Twisted Pair Indicator", shorttitle="TPI", overlay=true)
//define DEMA
DEMA_function(src, length) =>
ema1 = ta.ema(src, length)
ema2 = ta.ema(ema1, length)
2 * ema1 - ema2
//define TEMA
TEMA_function(src, length) =>
ema1 = ta.ema(src, length)
ema2 = ta.ema(ema1, length)
ema3 = ta.ema(ema2, length)
3 * (ema1 - ema2) + ema3
//input
swi = input.string(title="Switch", options=["EMA", "DEMA", "TEMA"], defval="EMA")
ma(src, length) =>
out = swi == "DEMA" ? DEMA_function(src, length) : swi == "TEMA" ? TEMA_function(src, length) : ta.ema(src, length)
out
//Twisted Pair algorithm
HJ_1 = (high + low + close) / 3 * volume
HJ_2 = ma((ma(HJ_1, 3) / ma(volume, 3) + ma(HJ_1, 6) / ma(volume, 6) + ma(HJ_1, 12) / ma(volume, 12) + ma(HJ_1, 24) / ma(volume, 24)) / 4, 13)
HJ_3 = 1.08 * HJ_2
HJ_4 = ma(HJ_3 - (ma(close, 3) - HJ_3), 5)
A7 = HJ_4 <= HJ_3 ? HJ_4 : HJ_3
HJ_5 = 2 * HJ_3 - A7
A9 = HJ_5 >= HJ_3 ? HJ_5 : HJ_3
WATCH = A7 == A9 ? A7 : na
plot(A7, color=color.yellow, linewidth=2)
plot(A9, color=color.yellow, linewidth=2)
plot(WATCH, color=color.green, linewidth=2, style = plot.style_steplinebr)
HJ_6 = close[1] * 1.1 - close < 0.01 and high == close
HJ_7 = HJ_3 >= HJ_3[1] and A7 < A7[1] and close > HJ_3 and open > HJ_3
// plot candle color indications
plotcandle(open, (open + close) / 2, open, (open + close) / 2, color=HJ_7 ? color.yellow : na)
plotcandle(close, (open + close) / 2, close, (open + close) / 2, color=HJ_7 ? color.red : na)