EMAs Daily Reset
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © SamRecio

//@version=5
indicator("EMAs Daily Reset", overlay = true)

recalc = input.timeframe("D", title = "Calculation Period:", tooltip = "Moving Averages will recalculate after every period has lapsed.")

type1 = input.string("EMA", title = "MA 1", options=["SMA", "EMA", "RMA", "WMA", "VWMA"], inline = "one")
len1 = input.int(21, minval = 1, title = "", inline = "one")
src1 = input.string("high", options = ["open", "high", "low", "close", "hl2", "hlc3", "ohlc4", "hlcc4", "HoB", "LoB"], title = "", inline = "one")
color1 = input.color(color.rgb(0,110,179), title = "", inline = "one")
type2 = input.string("EMA", title = "MA 2", options=["SMA", "EMA", "RMA", "WMA", "VWMA"], inline = "two")
len2 = input.int(13, minval = 1, title = "", inline = "two")
src2 = input.string("close", options = ["open", "high", "low", "close", "hl2", "hlc3", "ohlc4", "hlcc4", "HoB", "LoB"], title = "", inline = "two")
color2 = input.color(color.rgb(197,70,68), title = "", inline = "two")
type3 = input.string("EMA", title = "MA 3", options=["SMA", "EMA", "RMA", "WMA", "VWMA"], inline = "three")
len3 = input.int(5, minval = 1, title = "", inline = "three")
src3 = input.string("low", options = ["open", "high", "low", "close", "hl2", "hlc3", "ohlc4", "hlcc4", "HoB", "LoB"], title = "", inline = "three")
color3 = input.color(color.rgb(246,235,97), title = "", inline = "three")

src_get(_input) =>
    _input == "open"?open:
     _input == "high"?high:
     _input == "low"?low:
     _input == "close"?close:
     _input == "hl2"?hl2:
     _input == "hlc3"?hlc3:
     _input == "ohlc4"?ohlc4:
     _input == "hlcc4"?hlcc4:
     _input == "HoB"?math.max(open,close):
     _input == "LoB"?math.min(open,close):
     na

newday = timeframe.change(recalc)
bs_nd = ta.barssince(newday) + 1
//EMA CALC/////////////////////////////////////////////////////
day_ma(_type,s,l) =>
    v_len = bs_nd < l?bs_nd:l
    var float ma = na
    if _type == "EMA"
        k = 2/(v_len + 1)
        ma := (s*k) + (nz(ma[1])*(1-k))
    if _type == "RMA"
        a = (1/v_len)
	    ma := a * s + (1 - a) * nz(ma[1])
    if _type == "SMA"
        ma := ta.sma(s,v_len)
    if _type == "WMA"
        ma := ta.wma(s,v_len)    
    if _type == "VWMA"
        ma := ta.vwma(s,v_len)
    ma
//////////////////////////////////////////////////////////////
high_ma = day_ma(type1,src_get(src1),len1)
close_ma = day_ma(type2,src_get(src2),len2)
low_ma = day_ma(type3,src_get(src3),len3)

plot(high_ma, color = newday?color.new(color3,100):color1, title = "MA 1")
plot(close_ma, color = newday?color.new(color1,100):color2, title = "MA 2")
plot(low_ma, color = newday?color.new(color2,100):color3, title = "MA 3")

