PrevHighLow Trend Indicator
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © tarasenko_

//@version=5
indicator("PrevHighLow Trend Indicator", shorttitle = "PHLTI", overlay=true, timeframe="")


// Inputs
SmMA(source, length, type) =>
    var m = matrix.new<float>(1, length, 0)
    for i = 0 to length-1
        matrix.set(m, 0, i, source)
    type == "SMA" ? ta.sma(source, length) :
     type == "EMA" ? ta.ema(source, length) :
     type == "SMMA (RMA)" ? ta.rma(source, length) :
     type == "WMA" ? ta.wma(source, length) :
     type == "VWMA" ? ta.vwma(source, length) :
     type == "HMA" ? ta.hma(source, length) :
     type == "ALMA" ? ta.alma(source, length, 0.85, 6) :
     type == "SWMA" ? ta.swma(source) :
     type == "RMA" ? ta.rma(source, length) :
     type == "VWAP" ? ta.vwap(source) :
     type == "LSMA" ? ta.linreg(source, length, 0) :
     na

useSourceSmoothing = input(false, "Use source smoothing?", group="Source smoothing", tooltip="Source smoothing eliminates noise in price source data and thus helps you get much \"cleaner\" price data.\n\nNote: it only affects signals!\n\nDisabled by default. Default source - open.")
ma_type   = input.string("SMA", title="MA type", options=["SMA", "EMA", "SMMA (RMA)", "WMA", "VWMA", "HMA", "ALMA", "SWMA", "RMA", "VWAP", "LSMA"], group="Source smoothing")
ma_source = input(open, title="MA source", group="Source smoothing")
ma_period = input.int(20, title="MA period", minval=1, group="Source smoothing")
src = useSourceSmoothing ? SmMA(ma_source, ma_period, ma_type) : open

p1 = input.int(50, "Slow period", minval=1, group = "Main settings", tooltip="This MA's purpose is to show main trend.\n\nAlso may work as support/resistance.")
p2 = input.int(10, "Fast period", minval=1, group = "Main settings", tooltip="This MA shows faster trend AND controls filters.\n\nMay work as support/resistance as well as slow MA, but as it is fast, it is kinda risky to use it in this way.")

useF = input(true, "Use filters at all?", group="Filter switch", tooltip="Filter is connected to the Fast MA, so whenever you change it, filter will be also changed.")

useRSI = input(false, "Use RSI filter?", group="Filter switch", tooltip="RSI usually shows good reversals, if customised well.")
rsi_p = input.int(14, "RSI period", group="RSI Filter Settings")
rsi_src = input(open, "RSI source", group="RSI Filter Settings")
rsi_hhl = input.int(60, "RSI higher band", group = "RSI Filter Settings", minval=0)
rsi_lhl = input.int(40, "RSI lower band", group="RSI Filter Settings", minval=0)

useMACD = input(false, "Use MACD filter?", group="Filter switch", tooltip="MACD usually shows nice trend, if customised well.")
macd_src = input(open, "MACD source", group="MACD Filter Settings")
macd_fastlen = input.int(10, "MACD fast length", minval = 1, group="MACD Filter Settings")
macd_slowlen = input.int(6, "MACD low length", minval = 1, group="MACD Filter Settings")
macd_siglen = input.int(2, "MACD signal length", minval = 1, group="MACD Filter Settings")

useMACDRSI = input(false, "Use MACD+RSI filter?", group="Filter switch", tooltip="Basically summarises advantages of MACD and RSI.")

useCOG = input(false, 'Use COG filter?', group="Filter switch", tooltip="COG uses advanced statistic and Fibonacci ratio.\n\nGood for identifying trend, like MACD, but better time to time.")
cog_period = input.int(5, "COG period", group="COG Filter Settings")
cog_src = input(open, "COG source", group="COG Filter Settings")

useMACDCOG = input(false, "Use MACD+COG filter?", group="Filter switch", tooltip="Helps you find more accurate trends, if customised well.")

useMACDCOGRSI = input(false, "Use MCR filter?", group="Filter switch", tooltip="Helps you find more accurate trends, if customised well.")

useOffset = input(true, "Use offest?", group = "Offset", tooltip="Offset helps time signals better.")
useClassicOffset = input(true, "Use classic offest?", group="Classic offset", tooltip="Classic offset is 1.")
classicOffsetPeriod = input.int(1, "Classic offset period", minval=1, group="Classic offset")
useATROffset = input(false, "Use ATR offest?", group="ATR Offset")
ATROffsetPeriod = input.int(3, "ATR offset period", group="ATR Offset", minval=1)


// Declarations and calculations
classicOffset=useOffset and useClassicOffset? classicOffsetPeriod : 0
ATROffset = useOffset and useATROffset ? int(ta.atr(ATROffsetPeriod)) : 0
h1 = ta.highest(p1)
l1 = ta.lowest(p1)
h2 = ta.highest(p2)
l2 = ta.lowest(p2)
lh12 = (l1[1]+h1[1])/2
lh22 = (l2[1]+h2[1])/2
rsi = ta.rsi(rsi_src, rsi_p)
[macd_line, signal_line, hist_line] = ta.macd(macd_src, macd_fastlen, macd_slowlen, macd_siglen)
cog = ta.cog(src, cog_period)


// Signals
cog_sell = cog < cog[1]
cog_buy = cog > cog[1]

macd_sell = signal_line > macd_line and ((lh22 > src and lh12 > src) or (lh22 < src and lh12 < src))
macd_buy = signal_line < macd_line and ((src > lh22 and lh12 < src) or (src < lh22 and lh12 > src))

rsi_sell = math.max(rsi, rsi[1]) >= rsi_hhl //and ((lh22 > src and lh12 > src) or (lh22 < src and lh12 < src))
rsi_buy = math.min(rsi, rsi[1]) <= rsi_lhl //and ((src > lh22 and src > lh12) or (src < lh22 and src < lh12))

strong_sell = rsi_sell  and macd_sell  and cog_sell and ((lh12 > src and lh22 > src) or (lh12 < src and lh22 < src))
strong_buy = rsi_buy and macd_buy and cog_buy and ((src > lh22 and src > lh12) or (src < lh22 and src < lh12))

macdrsi_sell = rsi_sell and macd_sell
macdrsi_buy = rsi_sell and macd_buy

macdcog_sell = macd_sell and cog_sell
macdcog_buy = macd_buy and cog_buy

mcr_sell = macd_sell and cog_sell and rsi_sell
mcr_buy = macd_buy and cog_buy and rsi_buy


// Coloring
colour1 = open > lh12 ? #00cc00 : open < lh12 ?  #ff0000 : #ffad00
colour2 = open > lh22 ? #00cc00 : open < lh22 ?  #ff0000 : #ffad00
colour_fill = open > lh12 ? #00cc0020 : open < lh12 ?  #ff000020 : #ffad0020


// Plotting
ma1 = plot(lh12, color = colour1, linewidth = 2, style=plot.style_stepline, title="Slow MA")
ma2 = plot(src > lh22 ? low : src < lh22 ? high : lh22, color = #00000000)
color_line = plot(lh22, color = colour2, linewidth = 2, style=plot.style_circles, title="Fast MA")
fill(ma1, ma2, color = colour_fill, title="Cloud")


// Labeling signals
// Strong signals' labeling
plotshape(useF and strong_sell and strong_sell[1]!=true, title="Strong sell", style=shape.labeldown, color=#ff0000, text = "SS", textcolor=color.white, offset=classicOffset+ATROffset)
plotshape(useF and strong_buy and strong_buy[1]!=true, title="Strong buy", style=shape.labelup, color=#00cc00, text = "SB", textcolor=color.white, location=location.belowbar, offset=classicOffset+ATROffset)

// RSI signals' labeling
plotshape(useF and useRSI and rsi_sell and rsi_sell[1]!=true, title="RSI sell", style=shape.labeldown, color=#6200ca, text = "RS", textcolor=color.white, offset=classicOffset+ATROffset)
plotshape(useF and useRSI and rsi_buy and rsi_buy[1]!=true, title="RSI buy", style=shape.labelup, color=#ed00a0, text = "RB", textcolor=color.white, location=location.belowbar, offset=classicOffset+ATROffset)

// MACD signals' labeling
plotshape(useF and useMACD and macd_sell and macd_sell[1]!=true, title="MACD sell", style=shape.labeldown, color=#ff7000, text = "MS", textcolor=color.white, offset=classicOffset+ATROffset)
plotshape(useF and useMACD and macd_buy and macd_buy[1]!=true, title="MACD buy", style=shape.labelup, color=#0057cc, text = "MB", textcolor=color.white, location=location.belowbar, offset=classicOffset+ATROffset)

// MACD+RSI signals' labeling
plotshape(useF and useMACDRSI and macdrsi_sell and macdrsi_sell[1]!=true, title="MACD+RSI sell", style=shape.labeldown, color=#000000, text = "MRS", textcolor=color.white, offset=classicOffset+ATROffset)
plotshape(useF and useMACDRSI and macdrsi_buy and macdrsi_buy[1]!=true, title="MACD+RSI buy", style=shape.labelup, color=#3c3c3c, text = "MRB", textcolor=color.white, location=location.belowbar, offset=classicOffset+ATROffset)

// COG signals' labeling
plotshape(useF and useCOG and cog_sell and cog_sell[1]!=true, title="COG sell", style=shape.labeldown, color=#ff1a00, text = "CS", textcolor=color.white, offset=classicOffset+ATROffset)
plotshape(useF and useCOG and cog_buy and cog_buy[1]!=true, title="COG buy", style=shape.labelup, color=#77bc15, text = "CB", textcolor=color.white, location=location.belowbar, offset=classicOffset+ATROffset)

// MACD+COG signals' labeling
plotshape(useF and useMACDCOG and macdcog_sell and macdcog_sell[1]!=true, title="MACD+COG sell", style=shape.labeldown, color=#9f3737, text = "MCS", textcolor=color.white, offset=classicOffset+ATROffset)
plotshape(useF and useMACDCOG and macdcog_buy and macdcog_buy[1]!=true, title="MACD+COG buy", style=shape.labelup, color=#086d0b, text = "MCB", textcolor=color.white, location=location.belowbar, offset=classicOffset+ATROffset)

// MCR signals' labeling
plotshape(useF and useMACDCOGRSI and mcr_sell and mcr_sell[1]!=true, title="MCR sell", style=shape.labeldown, color=#9f3737, text = "MCRS", textcolor=color.white, offset=classicOffset+ATROffset)
plotshape(useF and useMACDCOGRSI and mcr_buy and mcr_buy[1]!=true, title="MCR buy", style=shape.labelup, color=#086d0b, text = "MCRB", textcolor=color.white, location=location.belowbar, offset=classicOffset+ATROffset)

// Alerts for RSI signals
alertcondition(useF and useRSI and rsi_sell and rsi_sell[1]!=true, message="RSI Sell", title="SHORT")
alertcondition(useF and useRSI and rsi_buy and rsi_buy[1]!=true, message="RSI Buy", title="LONG")
alertcondition(useF and useRSI and ((rsi_buy and rsi_buy[1]!=true) or (rsi_sell and rsi_sell[1]!=true)), message="RSI Signal", title="SIGNAL")