[Uhokang] Bollinger Band BB EMA SMMA SMA Multy timeframe
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © atomicbear96

//@version=5
indicator("[Uhokang] Bollinger Band BB EMA SMMA SMA Multy timeframe", overlay = true)

// import Timeframes 
import atomicbear96/MultyTimeframe/1 as MultyTimeframe
[big_interval, big_interval_2, big_mult, big_mult_2] = MultyTimeframe.MultTimeframes() 

// Safe Security 
nonRepaintingSecurity(sym, tf, src) =>
    request.security(sym, tf, src[barstate.isrealtime ? 1 : 0])[barstate.isrealtime ? 0 : 1]

// 1. Bollinger Band Multy timeframe
bb_len = input.int(20, "BB len", group = "Bollinger Band") 
bb_mult = input.float(2, "BB mult", group = "Bollinger Band") 
// bb
[bb_mid, bb_top, bb_bot] = ta.bb(close, bb_len, bb_mult)
// Timeframe BB Calc
f_bb(src, length, mult, _interval) =>
    float basis = nonRepaintingSecurity(syminfo.tickerid, _interval,ta.sma(src, length))
    float dev = nonRepaintingSecurity(syminfo.tickerid, _interval,mult * ta.stdev(src, length))
    [basis, basis + dev, basis - dev]
[bb_mid_big, bb_top_big, bb_bot_big] = f_bb(close, bb_len, bb_mult, big_interval)
[bb_mid_big_2, bb_top_big_2, bb_bot_big_2] = f_bb(close, bb_len, bb_mult, big_interval_2)

// draw
bb_color = input.color(color.new(color.navy,10), group = "Bollinger Band") 
bb_color_big = input.color(color.new(color.navy,70), group = "Bollinger Band") 
bb_color_big_2 = input.color(color.new(color.navy,90), group = "Bollinger Band") 

p_bb_mid = plot(bb_mid, color = bb_color, title = "BB Middle 1")
p_bb_top = plot(bb_top, color = bb_color, title = "BB Top 1")
p_bb_bot = plot(bb_bot, color = bb_color, title = "BB Bottom 1")
p_bb_mid_big = plot(bb_mid_big, color = bb_color_big, title = "BB Middle 2")
p_bb_top_big = plot(bb_top_big, color = bb_color_big, title = "BB Top 2")
p_bb_bot_big = plot(bb_bot_big, color = bb_color_big, title = "BB Bottom 2")
p_bb_mid_big_2 = plot(bb_mid_big_2, color = bb_color_big_2, title = "BB Middle 3")
p_bb_top_big_2 = plot(bb_top_big_2, color = bb_color_big_2, title = "BB Top 3")
p_bb_bot_big_2 = plot(bb_bot_big_2, color = bb_color_big_2, title = "BB Bottom 3")
fill(p_bb_top_big, p_bb_top_big_2, color = bb_color_big_2, title = "BB Top Big Zone")
fill(p_bb_bot_big, p_bb_bot_big_2, color = bb_color_big_2, title = "BB Bottom Big Zone")

// 2. Select Moving Averages Multy timeframe
mas_type = input.string("SMA", title = "Select Ma Type", options = ["EMA","SMMA","SMA"], group = "Select MA")


// 2-1. EMA Multy timeframe 
ema_len = input.int(200, minval=1, title="EMA Length", group = "EMA") 
ema_src = input(close, title="EMA Source", group = "EMA") 
ema = ta.ema(close, ema_len)
ema_big = nonRepaintingSecurity(syminfo.tickerid, big_interval, ema)
ema_big_2 = nonRepaintingSecurity(syminfo.tickerid, big_interval_2, ema)

// 2-2. SMMA Multy timeframe 
smma_len = input.int(7, minval=1, title="SMMA Length", group = "SMMA") 
smma_src = input(close, title="SMMA Source", group = "SMMA") 
smma = 0.0
smma_high = 0.0
smma_low = 0.0
smma := na(smma[1]) ? ta.sma(smma_src, smma_len) : (smma[1] * (smma_len - 1) + smma_src) / smma_len
smma_high := na(smma_high[1]) ? ta.sma(high, smma_len) : (smma_high[1] * (smma_len - 1) + high) / smma_len
smma_low := na(smma_low[1]) ? ta.sma(low, smma_len) : (smma_low[1] * (smma_len - 1) + low) / smma_len
// smma big
smma_big_src =  nonRepaintingSecurity(syminfo.tickerid, big_interval ,close)
smma_big = 0.0
smma_big := na(smma_big[big_mult]) ? nonRepaintingSecurity(syminfo.tickerid, big_interval ,ta.sma(smma_big_src, smma_len)) : (smma_big[big_mult]  * (smma_len - 1) + smma_big_src) / smma_len
// smma big 2
smma_big_src_2 =  nonRepaintingSecurity(syminfo.tickerid, big_interval_2 ,close)
smma_big_2 = 0.0
smma_big_2 := na(smma_big_2[big_mult_2]) ? nonRepaintingSecurity(syminfo.tickerid, big_interval_2 ,ta.sma(smma_big_src_2, smma_len)) : (smma_big_2[big_mult_2]  * (smma_len - 1) + smma_big_src_2) / smma_len

// 2-3. SMA Multy timeframe 
sma_len = input.int(20, minval=1, title="SMA Length", group = "SMA") 
sma_src = input(close, title="SMA Source", group = "SMA") 
sma = ta.sma(close, sma_len)
sma_big = nonRepaintingSecurity(syminfo.tickerid, big_interval, sma)
sma_big_2 = nonRepaintingSecurity(syminfo.tickerid, big_interval_2, sma)

// draw
mas = close
mas_big = close 
mas_big_2 = close 

if mas_type == "EMA"
    mas := ema
    mas_big := ema_big
    mas_big_2 := ema_big_2 
if mas_type == "SMMA"
    mas := smma
    mas_big := smma_big
    mas_big_2 := smma_big_2 
if mas_type == "SMA"
    mas := sma
    mas_big := sma_big
    mas_big_2 := sma_big_2 

mas_up_color = input.color(color.new(color.green,20), group = "Select MA")
mas_down_color = input.color(color.new(color.red,20), group = "Select MA")
mas_up_color_big = input.color(color.new(color.green,70), group = "Select MA")
mas_down_color_big = input.color(color.new(color.fuchsia,70), group = "Select MA")

mas_trend = mas > mas_big ? true : false 
mas_trend_color = mas_trend ? mas_up_color : mas_down_color
mas_trend_big = mas_big > mas_big_2 ? true : false 
mas_trend_big_color = mas_trend_big ? mas_up_color_big : mas_down_color_big
p_mas = plot(mas, color = mas_trend_color, title = "MA 1")
p_mas_big = plot(mas_big, color = mas_trend_color, title = "MA 2")
p_mas_big_2 = plot(mas_big_2, color = mas_trend_big_color , title = "MA 3")
fill(p_mas, p_mas_big, color = mas_trend_color, title = "MA Trend")
fill(p_mas_big, p_mas_big_2, color = mas_trend_big_color, title = "MA Big Trend")