HTF MA's BG
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
//@version=5
indicator('HTF MA\'s BG', overlay=true)
InputTime = input.string("0730-1100:23456", "Time Input")
t = time(timeframe.period, InputTime, "America/Denver")   //denver time plus 2 hours
//Set Red background color if outside of session time

ma(source, length, type) =>
    switch type
        "SMA" => ta.sma(source, length)
        "Bollinger Bands" => ta.sma(source, length)
        "EMA" => ta.ema(source, length)
        "SMMA (RMA)" => ta.rma(source, length)
        "WMA" => ta.wma(source, length)
        "VWMA" => ta.vwma(source, length)
        "HMA" => ta.wma(2*ta.wma(source, length/2)-ta.wma(source, length), math.floor(math.sqrt(length)))

ShortSMMA = input.int(50, minval=1, title="Short MA")
MedSMMA   = input.int(50, minval=1, title="Medium MA")

MinimumStopLossValue = input.float(1.0, minval=0.0, step=0.1, title="Minimum Stop Loss Value")

maTypeInput = input.string("HMA", title="MA 3 Type", options=["SMA", "Bollinger Bands", "EMA", "SMMA (RMA)", "WMA", "VWMA", "HMA"], group="MA Settings")

src1 = input(close, title='Source')
smma1 = 0.0
sma_1 = ma(src1, ShortSMMA, maTypeInput)
HTF_sma_1 = request.security(syminfo.tickerid, '2', sma_1, barmerge.gaps_on, barmerge.lookahead_off)
//plot(sma_1, title="Short MA", color=color.new(color.white, 0))
plot(HTF_sma_1, title="HTF Short MA", color=color.new(color.white, 0))


src2 = input(close, title='Source')
smma2 = 0.0
sma_2 = ma(src2, MedSMMA, maTypeInput)
HTF_sma_2 = request.security(syminfo.tickerid, '3', sma_2, barmerge.gaps_on, barmerge.lookahead_off)
//plot(sma_2, title="Medium MA", color=color.new(color.green, 0))
plot(HTF_sma_2, title="HTF Medium MA", color=color.new(color.green, 0))


MAShort = HTF_sma_1 < HTF_sma_2 
MALong = HTF_sma_1 > HTF_sma_2 

sessioncolor = color.purple

sessioncolor := not t ? color.purple : t and (close > HTF_sma_1 or close > HTF_sma_1[1]) ? color.green : t and (close < HTF_sma_1 or close < HTF_sma_1[1]) ? color.red : nz(sessioncolor[1])
bgcolor(color.new(sessioncolor, 75))

//////////////////////////////////////////////////////////////////////////
// Settings for 5min chart, BTCUSDC. For Other coin, change the parameters
//////////////////////////////////////////////////////////////////////////

// Color variables
upColor   = color.green
midColor  = color.gray
downColor = color.red   

// Source
src_fastrangefilter = input(defval=hl2, title="Source")

// Sampling Period
// Settings for 5min chart, BTCUSDC. For Other coin, change the paremeters
per_fastrangefilter = input.int(defval=24, minval=1, title="Sampling Period")

// Range Multiplier
mult_fastrangefilter = input.float(defval=2.7, minval=0.1, title="Range Multiplier")

// Smooth Average Range
smoothrng(x, t, m) =>
    wper = t * 2 - 1
    avrng = ta.ema(math.abs(x - x[1]), t)
    smoothrng = ta.ema(avrng, wper) * m
    smoothrng
smrng = smoothrng(src_fastrangefilter, per_fastrangefilter, mult_fastrangefilter)

// Range Filter
rngfilt(x, r) =>
    rngfilt = x
    rngfilt := x > nz(rngfilt[1]) ? x - r < nz(rngfilt[1]) ? nz(rngfilt[1]) : x - r : 
       x + r > nz(rngfilt[1]) ? nz(rngfilt[1]) : x + r
    rngfilt
filt = rngfilt(src_fastrangefilter, smrng)

// Filter Direction
upward = 0.0
upward := filt > filt[1] ? nz(upward[1]) + 1 : filt < filt[1] ? 0 : nz(upward[1])
downward = 0.0
downward := filt < filt[1] ? nz(downward[1]) + 1 : filt > filt[1] ? 0 : nz(downward[1])

// Colors
filtcolor = upward > 0 ? upColor : downward > 0 ? downColor : midColor
filtplot = plot(filt, color=filtcolor, linewidth=2, title="Fast Range Filter")

barcolor = src_fastrangefilter > filt and src_fastrangefilter > src_fastrangefilter[1] and upward > 0 ? upColor :
   src_fastrangefilter > filt and src_fastrangefilter < src_fastrangefilter[1] and upward > 0 ? upColor : 
   src_fastrangefilter < filt and src_fastrangefilter < src_fastrangefilter[1] and downward > 0 ? downColor : 
   src_fastrangefilter < filt and src_fastrangefilter > src_fastrangefilter[1] and downward > 0 ? downColor : midColor
// Bar Color
barcolor(barcolor)
shortSignal = (sessioncolor == color.red) and ((barcolor[1] == upColor and barcolor == midColor) or (barcolor[1] == midColor and barcolor == downColor)  or (barcolor[1] == upColor and barcolor == downColor))
longSignal = (sessioncolor == color.green) and ((barcolor[1] == downColor and barcolor == midColor) or (barcolor[1] == midColor and barcolor == upColor) or (barcolor[1] == downColor and barcolor == upColor))

plotshape(longSignal and not longSignal[1], title="Buy Signal", style=shape.triangleup, size=size.tiny, location=location.belowbar, color=color.green)
plotshape(shortSignal and not shortSignal[1], title="Sell Signal", style=shape.triangledown, size=size.tiny, location=location.abovebar, color=color.red)

//write table
var string GP2 = "Display"
string  tableYposInput = input.string("top", "Panel position", inline = "11", options = ["top", "middle", "bottom"], group = GP2)
string  tableXposInput = input.string("right", "", inline = "11", options = ["left", "center", "right"], group = GP2)
color   bullColorInput = input.color(color.new(color.green, 30), "Bull", inline = "12", group = GP2)
color   bearColorInput = input.color(color.new(color.red, 30), "Bear", inline = "12", group = GP2)
color   neutColorInput = input.color(color.new(color.gray, 30), "Neutral", inline = "12", group = GP2)
var table panel = table.new(tableYposInput + "_" + tableXposInput, 4, 7)
if barstate.islast
    // Table header.
    table.cell(panel, 0, 0, "SL Size", bgcolor = bearColorInput)
SL = 0.0    
SL := (sessioncolor == color.red) and close > open ? (math.max(MinimumStopLossValue,high[2]-close)) : (sessioncolor == color.red) and close < open ? (math.max(MinimumStopLossValue,high[1]-close)) : (sessioncolor == color.green) and close > open ? (math.max(1,close-low[1])) : (math.max(MinimumStopLossValue,close-low[2]))
table.cell(panel, 0, 1, str.tostring(SL), bgcolor = neutColorInput)