Multi HMA Slopes [Loxx]
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © loxx

//@version=5
indicator("Multi HMA Slopes [Loxx]",
     shorttitle="MHMAS [Loxx]", 
     overlay = false, 
     timeframe="", 
     timeframe_gaps = true)

import loxx/loxxexpandedsourcetypes/4

greencolor = #2DD204
redcolor = #D2042D 
bluecolor = #042dd2

smthtype = input.string("Kaufman", "Heikin-Ashi Better Caculation Type", options = ["AMA", "T3", "Kaufman"], group = "Source Settings")

srcin = input.string("Close", "Source", group= "Source Settings", 
     options = 
     ["Close", "Open", "High", "Low", "Median", "Typical", "Weighted", "Average", "Average Median Body", "Trend Biased", "Trend Biased (Extreme)", 
     "HA Close", "HA Open", "HA High", "HA Low", "HA Median", "HA Typical", "HA Weighted", "HA Average", "HA Average Median Body", "HA Trend Biased", "HA Trend Biased (Extreme)",
     "HAB Close", "HAB Open", "HAB High", "HAB Low", "HAB Median", "HAB Typical", "HAB Weighted", "HAB Average", "HAB Average Median Body", "HAB Trend Biased", "HAB Trend Biased (Extreme)"])

inpPeriod1 = input.int(30, "Period 1", group = "Basic Settings")  
inpPeriod2 = input.int(33, "Period 1", group = "Basic Settings")  
inpPeriod3 = input.int(36, "Period 1", group = "Basic Settings")  
inpPeriod4 = input.int(39, "Period 1", group = "Basic Settings")  
inpPeriod5 = input.int(42, "Period 1", group = "Basic Settings")  

colorbars = input.bool(true, "Color bars?", group = "UI Options")

showLongs = input.bool(true, "Show Longs?", group = "Signal Options")
showShorts = input.bool(true, "Show Shorts?", group = "Signal Options")
showCLongs = input.bool(true, "Show Continuation Longs?", group = "Signal Options")
showCShorts = input.bool(true, "Show Continuation Shorts?", group = "Signal Options")

kfl=input.float(0.666, title="* Kaufman's Adaptive MA (KAMA) Only - Fast End", group = "Moving Average Inputs")
ksl=input.float(0.0645, title="* Kaufman's Adaptive MA (KAMA) Only - Slow End", group = "Moving Average Inputs")
amafl = input.int(2, title="* Adaptive Moving Average (AMA) Only - Fast", group = "Moving Average Inputs")
amasl = input.int(30, title="* Adaptive Moving Average (AMA) Only - Slow", group = "Moving Average Inputs")

haclose = request.security(ticker.heikinashi(syminfo.tickerid), timeframe.period, close)
haopen = request.security(ticker.heikinashi(syminfo.tickerid), timeframe.period, open)
hahigh = request.security(ticker.heikinashi(syminfo.tickerid), timeframe.period, high)
halow = request.security(ticker.heikinashi(syminfo.tickerid), timeframe.period, low)
hamedian = request.security(ticker.heikinashi(syminfo.tickerid), timeframe.period, hl2)
hatypical = request.security(ticker.heikinashi(syminfo.tickerid), timeframe.period, hlc3)
haweighted = request.security(ticker.heikinashi(syminfo.tickerid), timeframe.period, hlcc4)
haaverage = request.security(ticker.heikinashi(syminfo.tickerid), timeframe.period, ohlc4)

src = switch srcin
	"Close" => loxxexpandedsourcetypes.rclose()
	"Open" => loxxexpandedsourcetypes.ropen()
	"High" => loxxexpandedsourcetypes.rhigh()
	"Low" => loxxexpandedsourcetypes.rlow()
	"Median" => loxxexpandedsourcetypes.rmedian()
	"Typical" => loxxexpandedsourcetypes.rtypical()
	"Weighted" => loxxexpandedsourcetypes.rweighted()
	"Average" => loxxexpandedsourcetypes.raverage()
    "Average Median Body" => loxxexpandedsourcetypes.ravemedbody()
	"Trend Biased" => loxxexpandedsourcetypes.rtrendb()
	"Trend Biased (Extreme)" => loxxexpandedsourcetypes.rtrendbext()
	"HA Close" => loxxexpandedsourcetypes.haclose(haclose)
	"HA Open" => loxxexpandedsourcetypes.haopen(haopen)
	"HA High" => loxxexpandedsourcetypes.hahigh(hahigh)
	"HA Low" => loxxexpandedsourcetypes.halow(halow)
	"HA Median" => loxxexpandedsourcetypes.hamedian(hamedian)
	"HA Typical" => loxxexpandedsourcetypes.hatypical(hatypical)
	"HA Weighted" => loxxexpandedsourcetypes.haweighted(haweighted)
	"HA Average" => loxxexpandedsourcetypes.haaverage(haaverage)
    "HA Average Median Body" => loxxexpandedsourcetypes.haavemedbody(haclose, haopen)
	"HA Trend Biased" => loxxexpandedsourcetypes.hatrendb(haclose, haopen, hahigh, halow)
	"HA Trend Biased (Extreme)" => loxxexpandedsourcetypes.hatrendbext(haclose, haopen, hahigh, halow)
	"HAB Close" => loxxexpandedsourcetypes.habclose(smthtype, amafl, amasl, kfl, ksl)
	"HAB Open" => loxxexpandedsourcetypes.habopen(smthtype, amafl, amasl, kfl, ksl)
	"HAB High" => loxxexpandedsourcetypes.habhigh(smthtype, amafl, amasl, kfl, ksl)
	"HAB Low" => loxxexpandedsourcetypes.hablow(smthtype, amafl, amasl, kfl, ksl)
	"HAB Median" => loxxexpandedsourcetypes.habmedian(smthtype, amafl, amasl, kfl, ksl)
	"HAB Typical" => loxxexpandedsourcetypes.habtypical(smthtype, amafl, amasl, kfl, ksl)
	"HAB Weighted" => loxxexpandedsourcetypes.habweighted(smthtype, amafl, amasl, kfl, ksl)
	"HAB Average" => loxxexpandedsourcetypes.habaverage(smthtype, amafl, amasl, kfl, ksl)
    "HAB Average Median Body" => loxxexpandedsourcetypes.habavemedbody(smthtype, amafl, amasl, kfl, ksl)
	"HAB Trend Biased" => loxxexpandedsourcetypes.habtrendb(smthtype, amafl, amasl, kfl, ksl)
	"HAB Trend Biased (Extreme)" => loxxexpandedsourcetypes.habtrendbext(smthtype, amafl, amasl, kfl, ksl)
	=> haclose
	
	
ma1 = ta.hma(src, inpPeriod1)
ma2 = ta.hma(src, inpPeriod2)
ma3 = ta.hma(src, inpPeriod3)
ma4 = ta.hma(src, inpPeriod4)
ma5 = ta.hma(src, inpPeriod5)

histou = 0.
histod = 0.

if ma1 > nz(ma1[1])
    histou += 1
    
if ma1 < nz(ma1[1])
    histod -= 1
    
if ma2 > nz(ma2[1])
    histou += 1
    
if ma2 < nz(ma2[1])
    histod -= 1
    
if ma3 > nz(ma3[1])
    histou += 1
    
if ma3 < nz(ma3[1])
    histod -= 1
    
if ma4 > nz(ma4[1])
    histou += 1
    
if ma4 < nz(ma4[1])
    histod -= 1
    
if ma5 > nz(ma5[1])
    histou += 1
    
if ma5 < nz(ma5[1])
    histod -= 1
    
trend = histou > 0 and histod == 0 ? 1 : histod < 0 and histou == 0 ? -1 : 0

plot(histou, color = greencolor, linewidth = 2, style = plot.style_columns)
plot(histod, color = redcolor, linewidth = 2, style = plot.style_columns)

barcolor(colorbars ? trend == 1 ? greencolor : trend == -1 ? redcolor : color.gray : na)

contLSwitch = 0
contLSwitch := nz(contLSwitch[1])
contLSwitch := trend == 1 ? 1 : trend == -1 ? -1 : contLSwitch

goLong = trend == 1 and trend[1] != 1 and nz(contLSwitch[1]) == -1
goShort = trend == -1 and trend[1] != -1 and nz(contLSwitch[1]) == 1

goContL = trend == 1 and trend[1] != 1 and nz(contLSwitch[1]) == 1
goContS = trend == -1 and trend[1] != -1 and nz(contLSwitch[1]) == -1

plotshape(showLongs and goLong, title = "Long", color = greencolor, textcolor = greencolor, text = "L", style = shape.triangleup, location = location.bottom, size = size.auto)
plotshape(showShorts and goShort, title = "Short", color = redcolor, textcolor = redcolor, text = "S", style = shape.triangledown, location = location.top, size = size.auto)

plotshape(showCLongs and goContL, title = "Continuation Long", color = color.yellow, textcolor = color.yellow, text = "CL", style = shape.triangleup, location = location.bottom, size = size.auto)
plotshape(showCShorts and goContS, title = "Continuation Short", color = color.fuchsia, textcolor = color.fuchsia, text = "CS", style = shape.triangledown, location = location.top, size = size.auto)

alertcondition(goLong, title = "Long", message = "Multi HMA Slopes [Loxx]: Long\nSymbol: {{ticker}}\nPrice: {{close}}")
alertcondition(goShort, title = "Short", message = "Multi HMA Slopes [Loxx]: Short\nSymbol: {{ticker}}\nPrice: {{close}}")

alertcondition(goContL, title = "Continuation Long", message = "Multi HMA Slopes [Loxx]: Continuation Long\nSymbol: {{ticker}}\nPrice: {{close}}")
alertcondition(goContS, title = "Continuation Short", message = "Multi HMA Slopes [Loxx]: Continuation Short\nSymbol: {{ticker}}\nPrice: {{close}}")

hline(7, color = color.new(color.white, 100), editable = false)
hline(-7, color = color.new(color.white, 100), editable = false)

