Better Heiken-Ashi Candles w/ Expanded Source Types [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("Better Heiken-Ashi Candles w/ Expanded Source Types [Loxx]", 
     shorttitle = "BHACECT [Loxx]", 
     overlay = false, 
     timeframe="", 
     timeframe_gaps = true)

greencolor = #2DD204
redcolor = #D2042D 

_ama(src, len, fl, sl)=>
    flout = 2/(fl + 1)
    slout = 2/(sl + 1)
    hh = ta.highest(len + 1)
    ll = ta.lowest(len + 1)
    mltp = hh - ll != 0 ? math.abs(2 * src - ll - hh) / (hh - ll) : 0
    ssc = mltp * (flout - slout) + slout
    ama = 0.
    ama := nz(ama[1]) + math.pow(ssc, 2) * (src - nz(ama[1]))
    ama
    
_t3(src, len) =>
    xe1_1 = ta.ema(src, len)
    xe2_1 = ta.ema(xe1_1, len)
    xe3_1 = ta.ema(xe2_1, len)
    xe4_1 = ta.ema(xe3_1, len)
    xe5_1 = ta.ema(xe4_1, len)
    xe6_1 = ta.ema(xe5_1, len)
    b_1 = 0.7
    c1_1 = -b_1 * b_1 * b_1
    c2_1 = 3 * b_1 * b_1 + 3 * b_1 * b_1 * b_1
    c3_1 = -6 * b_1 * b_1 - 3 * b_1 - 3 * b_1 * b_1 * b_1
    c4_1 = 1 + 3 * b_1 + b_1 * b_1 * b_1 + 3 * b_1 * b_1
    nT3Average = c1_1 * xe6_1 + c2_1 * xe5_1 + c3_1 * xe4_1 + c4_1 * xe3_1
    nT3Average

_kama(src, len, kama_fastend, kama_slowend) =>
    xvnoise = math.abs(src - src[1])
    nfastend = kama_fastend
    nslowend = kama_slowend
    nsignal = math.abs(src - src[len])
    nnoise = math.sum(xvnoise, len)
    nefratio = nnoise != 0 ? nsignal / nnoise : 0
    nsmooth = math.pow(nefratio * (nfastend - nslowend) + nslowend, 2)
    nAMA = 0.0
    nAMA := nz(nAMA[1]) + nsmooth * (src - nz(nAMA[1]))
    nAMA
    
_trendreg(o, h, l, c)=>
    compute = 0.
    if (c > o)
        compute := ((h + c)/2.0)
    else  
        compute := ((l + c)/2.0)
    compute

_trendext(o, h, l, c) =>
    compute = 0.
    if (c > o) 
        compute := h
    else if (c < o) 
        compute := l
    else 
        compute := c
    compute    


candletype = input.string("Regular", "Candle Type", options = ["Regular", "HA Regular", "HA Better"], group=  "Basic Settings")
smthtype = input.string("AMA", "Filter Options", options = ["AMA", "T3", "Kaufman"], group=  "Basic Settings")


srcoption = input.string("Close", "SMA Source Options", group= "SMA 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)"])

smalen = input.int(60, title="SMA Length", group = "SMA Settings")


kama_fastend=input.float(0.666, title="* Kaufman's Adaptive MA (KAMA) Only - Fast End", group = "Moving Average Inputs")
kama_slowend=input.float(0.0645, title="* Kaufman's Adaptive MA (KAMA) Only - Slow End", group = "Moving Average Inputs")

fastLength = input.int(2, title="* Adaptive Moving Average (AMA) Only - Fast", group = "Moving Average Inputs")
slowLength = input.int(30, title="* Adaptive Moving Average (AMA) Only - Slow", group = "Moving Average Inputs")
colorbars = input.bool(false, "Color bars?", group = "UI Options")

rclose = close 
ropen = open
rhigh = high
rlow = low
rmedian =  hl2
rtypical =  hlc3
rweighted =  hlcc4
raverage =  ohlc4
ravemedbody = (open + close)/2
rtrendb = _trendreg(ropen, rhigh, rlow, rclose)
rtrendbext = _trendext(ropen, rhigh, rlow, rclose)

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)
haavemedbody = (haopen + haclose)/2
hatrendb = _trendreg(haopen, hahigh, halow, haclose)
hatrendbext = _trendext(haopen, hahigh, halow, haclose)

habingest = (ropen + rclose)/2 + (((rclose - ropen)/(rhigh - rlow)) * math.abs((rclose - ropen)/2))
habclose = smthtype == "AMA" ? _ama(habingest, 2, fastLength, slowLength) : smthtype == "T3" ? _t3(habingest, 3) : _kama(habingest, 2, kama_fastend, kama_slowend)
habopen = 0.
habopen := (nz(habopen[1]) + nz(habclose[1]))/2
habhigh = math.max(rhigh, habopen, habclose)
hablow = math.min(rlow, habopen, habclose)
habmedian = (habhigh + hablow)/2
habtypical = (habhigh + hablow + habclose)/3
habweighted = (habhigh + hablow + habclose + habclose)/4
habaverage = (habopen + habhigh + hablow + habclose)/4
habavemedbody = (habopen + habclose)/2
habtrendb = _trendreg(habopen, habhigh, hablow, habclose)
habtrendbext = _trendext(habopen, habhigh, hablow, habclose)

close_out = candletype == "Regular" ? rclose : candletype == "HA Regular" ? haclose : habclose
open_out = candletype == "Regular" ? ropen : candletype == "HA Regular" ? haopen : habopen
high_out = candletype == "Regular" ? rhigh : candletype == "HA Regular" ? hahigh : habhigh
low_out = candletype == "Regular" ? rlow : candletype == "HA Regular" ? halow : hablow

colorout = close_out >= open_out ? greencolor : redcolor

plotcandle(open_out, high_out, low_out, close_out, color = colorout, wickcolor = colorout)  
barcolor(colorbars ? colorout : na)

float srcma = switch srcoption
	"Close" => rclose
	"Open" => ropen
	"High" => rhigh
	"Low" => rlow
	"Median" => rmedian
	"Typical" => rtypical
	"Weighted" => rweighted
	"Average" => raverage
    "Average Median Body" => ravemedbody
	"Trend Biased" => rtrendb
	"Trend Biased (Extreme)" => rtrendbext

	"HA Close" => haclose
	"HA Open" => haopen
	"HA High" => hahigh
	"HA Low" => halow
	"HA Median" => hamedian
	"HA Typical" => hatypical
	"HA Weighted" => haweighted
	"HA Average" => haaverage
    "HA Average Median Body" => haavemedbody
	"HA Trend Biased" => hatrendb
	"HA Trend Biased (Extreme)" => hatrendbext
	
	"HAB Close" => habclose
	"HAB Open" => habopen
	"HAB High" => habhigh
	"HAB Low" => hablow
	"HAB Median" => habmedian
	"HAB Typical" => habtypical
	"HAB Weighted" => habweighted
	"HAB Average" => habaverage
    "HAB Average Median Body" => habavemedbody
	"HAB Trend Biased" => habtrendb
	"HAB Trend Biased (Extreme)" => habtrendbext

	=> haclose



outma = ta.sma(srcma, 14)




plot(outma, color = srcma >= outma ? color.yellow : color.fuchsia, linewidth = 2)
















