Heiken Ashi smoothed
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © nomnol

//@version=5
indicator(title = "Heiken Ashi smoothed", shorttitle="HA smoothed", overlay=true)

// === Input ===
ma1_len = input(6, title="MA 01")
ma1_type = input.string("SMMA", "ma1_type", options=["SMA", "EMA", "SMMA", "LWMA", "ALMA", "HMA"])
ma2_len = input(2, title="MA 02")
ma2_type = input.string("LWMA", "ma2_type", options=["SMA", "EMA", "SMMA", "LWMA", "ALMA", "HMA"])

// === MA 01 Filter ===
ma1_o = 0.0
ma1_h = 0.0
ma1_l = 0.0
ma1_c = 0.0

if ma1_type == "SMA"
    ma1_o := ta.sma(open, ma1_len)
    ma1_h := ta.sma(high, ma1_len)
    ma1_l := ta.sma(low, ma1_len)
    ma1_c := ta.sma(close, ma1_len)
else if ma1_type == "EMA"
    ma1_o := ta.ema(open, ma1_len)
    ma1_h := ta.ema(high, ma1_len)
    ma1_l := ta.ema(low, ma1_len)
    ma1_c := ta.ema(close, ma1_len)
else if ma1_type == "SMMA"
    ma1_o := ta.rma(open, ma1_len)
    ma1_h := ta.rma(high, ma1_len)
    ma1_l := ta.rma(low, ma1_len)
    ma1_c := ta.rma(close, ma1_len)
else if ma1_type == "LWMA"
    ma1_o := ta.wma(open, ma1_len)
    ma1_h := ta.wma(high, ma1_len)
    ma1_l := ta.wma(low, ma1_len)
    ma1_c := ta.wma(close, ma1_len)
else if ma1_type == "ALMA"
    alma1_offset = 0.85 * (ma1_len - 1)
    alma1_sigma = ma1_len / 6
    ma1_o := ta.alma(open, ma1_len, alma1_offset, alma1_sigma)
    ma1_h := ta.alma(high, ma1_len, alma1_offset, alma1_sigma)
    ma1_l := ta.alma(low, ma1_len, alma1_offset, alma1_sigma)
    ma1_c := ta.alma(close, ma1_len, alma1_offset, alma1_sigma)
else if ma1_type == "HMA"
    ma1_o := ta.hma(open, ma1_len)
    ma1_h := ta.hma(high, ma1_len)
    ma1_l := ta.hma(low, ma1_len)
    ma1_c := ta.hma(close, ma1_len)
// else if ma1_type == "SMMA"
//     ma1_o := na(ma1_o[1]) ? ta.sma(open, ma1_len) : (ma1_o[1] * (ma1_len - 1) + open) / ma1_len
//     ma1_h := na(ma1_h[1]) ? ta.sma(high, ma1_len) : (ma1_h[1] * (ma1_len - 1) + high) / ma1_len
//     ma1_l := na(ma1_l[1]) ? ta.sma(low, ma1_len) : (ma1_l[1] * (ma1_len - 1) + low) / ma1_len
//     ma1_c := na(ma1_c[1]) ? ta.sma(close, ma1_len) : (ma1_c[1] * (ma1_len - 1) + close) / ma1_len

// ===  PLOTITING===
// ma1_col=ma1_o>ma1_c ? color.red : color.green
// plotcandle(ma1_o, ma1_h, ma1_l, ma1_c, title="MA1", color=ma1_col)

// === HA calculator ===
ha_o = 0.0
ha_c = 0.0
ha_o := na(ha_o[1]) ? (ma1_o[1] + ma1_h[1] + ma1_l[1] + ma1_c[1]) / 4 : (ha_o[1] + ha_c[1]) / 2
ha_h = ma1_h
ha_l = ma1_l
ha_c := (ma1_o + ma1_h + ma1_l + ma1_c) / 4

// ===  PLOTITING===
// ha_col=ha_o>ha_c ? color.red : color.green
// plotcandle(ha_o, ha_h, ha_l, ha_c, title="HA", color=ha_col)

// === MA 02 Filter ===
ma2_o = 0.0
ma2_h = 0.0
ma2_l = 0.0
ma2_c = 0.0

if ma2_type == "SMA"
    ma2_o := ta.sma(ha_o, ma2_len)
    ma2_h := ta.sma(ha_h, ma2_len)
    ma2_l := ta.sma(ha_l, ma2_len)
    ma2_c := ta.sma(ha_c, ma2_len)
else if ma2_type == "EMA"
    ma2_o := ta.ema(ha_o, ma2_len)
    ma2_h := ta.ema(ha_h, ma2_len)
    ma2_l := ta.ema(ha_l, ma2_len)
    ma2_c := ta.ema(ha_c, ma2_len)
else if ma2_type == "SMMA"
    ma2_o := ta.rma(ha_o, ma2_len)
    ma2_h := ta.rma(ha_h, ma2_len)
    ma2_l := ta.rma(ha_l, ma2_len)
    ma2_c := ta.rma(ha_c, ma2_len)
else if ma2_type == "LWMA"
    ma2_o := ta.wma(ha_o, ma2_len)
    ma2_h := ta.wma(ha_h, ma2_len)
    ma2_l := ta.wma(ha_l, ma2_len)
    ma2_c := ta.wma(ha_c, ma2_len)
else if ma2_type == "ALMA"
    alma2_offset = 0.85 * (ma2_len - 1)
    alma2_sigma = ma2_len / 6
    ma2_o := ta.alma(ha_o, ma2_len, alma2_offset, alma2_sigma)
    ma2_h := ta.alma(ha_h, ma2_len, alma2_offset, alma2_sigma)
    ma2_l := ta.alma(ha_l, ma2_len, alma2_offset, alma2_sigma)
    ma2_c := ta.alma(ha_c, ma2_len, alma2_offset, alma2_sigma)
else if ma2_type == "HMA"
    ma2_o := ta.hma(ha_o, ma2_len)
    ma2_h := ta.hma(ha_h, ma2_len)
    ma2_l := ta.hma(ha_l, ma2_len)
    ma2_c := ta.hma(ha_c, ma2_len)

// ===  PLOTITING===
ma2_col=ma2_o>ma2_c ? color.red : color.green
plotcandle(ma2_o, ma2_h, ma2_l, ma2_c, title="HA Smoothed", color=ma2_col)



// === HA calculator ===
// ha_t = ticker.heikinashi(syminfo.tickerid)
// ha_p = timeframe.period
// ha_o = request.security(ha_t, ha_p, ma1_o)
// ha_h = request.security(ha_t, ha_p, ma1_h)
// ha_l = request.security(ha_t, ha_p, ma1_l)
// ha_c = request.security(ha_t, ha_p, ma1_c)