Bogdan Ciocoiu - Sniper Entry
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © BogdanCiocoiu
 
//@version=5

// What is Sniper Entry 
// Sniper Entry is a set indicator that encapsulates a collection of pre-configured scripts using specific variables that enable users to extract signals by interpreting market behaviour quickly, suitable for 1-3min scalping. This instrument is a tool that acts as a confluence for traders to make decisions concerning current market conditions. This indicator does not apply solely to an asset.
// 
// What Sniper Entry is not
// Sniper Entry is not interpreting fundamental analysis and will also not be providing out of box market signals. Instead, it will provide a collection of integrated and significantly improved open-source subscripts designed to help traders speculate on market trends. Traders must apply their strategies and configure Sniper Entry accordingly to maximise the script's output.
// 
// Originality and usefulness
// The collection of subscripts encapsulated in this tool makes it unique in the Trading View ecosystem. This indicator enables traders to consider entry positions or exit positions by comparing similar algorithms at once.
// 
// Its usefulness also emerges from the unique configurations embedded in the indicator's settings, which are different from those of the original scripts.
// 
// This indicator's originality is also reflected in how its modules are integrated, including the integration of the settings.
// 
// Open-source reuse
// I used the following open-source resources, which I simplified significantly and pre-configured for short term scalping. The source codes for the below are already in the public domain, including the following links listed below.
// 
// https://www.tradingview.com/scripts/rangefilter/ (open source)
// https://www.tradingview.com/script/mS0vb7DA-DH-True-Price-DOTS-for-Heikin-Ashi/ (open source and generic algorithm)
// https://www.tradingview.com/scripts/chandelier/ (open source)
// https://www.tradingview.com/script/hg92pFwS-Hull-Suite/ (open source)
// https://www.tradingview.com/script/whJATyGU-UT-Bot/ (open source)
// https://www.tradingview.com/ideas/movingaverage/ (generic MA algorithm and open source)
// https://www.tradingview.com/script/WwiSR6vK-VWAP-Standard-Deviation/ (generic VWAP algorithm and open source)

indicator("Bogdan Ciocoiu - Sniper Entry", shorttitle="BC - Sniper Entry", format=format.price, precision=2, timeframe="", timeframe_gaps=true, explicit_plot_zorder=true, overlay=true)

// -----------------------------------------------------------------------------------------------------------------------------------------------------------
// Range filter -- inputs
// -----------------------------------------------------------------------------------------------------------------------------------------------------------

_rf_1_source = input(defval=hl2, title="RF1", inline="RF1", group="Range filters")
_rf_1_period = input(30, title="Period", inline="RF1", group="Range filters")
_rf_1_multiplier = input(2.6, title="Multiplier", inline="RF1", group="Range filters")

_rf_2_source = input(defval=ohlc4, title="RF2", inline="RF2", group="Range filters")
_rf_2_period = input(48, title="Period", inline="RF2", group="Range filters")
_rf_2_multiplier = input(3.4, title="Multiplier", inline="RF2", group="Range filters")

_rf_transparency = input(50, title="Transp", inline="RF", group="Range filters")
_rf_border = input(2, title="Width", inline="RF", group="Range filters")

_rf_show_label = input(true, "Label", inline="RF", group="Range filters")
_rf_show_shape = input(true, "Shape", inline="RF", group="Range filters")


// -----------------------------------------------------------------------------------------------------------------------------------------------------------
// Range filter -- delivery
// -----------------------------------------------------------------------------------------------------------------------------------------------------------

_rf_smoothrng(x, t, m) =>
    wper = t * 2 - 1
    avrng = ta.ema(math.abs(x - x[1]), t)
    smoothrng = ta.ema(avrng, wper) * m
    smoothrng
_rf_1_smrng = _rf_smoothrng(_rf_1_source, _rf_1_period, _rf_1_multiplier)
_rf_2_smrng = _rf_smoothrng(_rf_2_source, _rf_2_period, _rf_2_multiplier)

_rf_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
_rf_1_filt = _rf_rngfilt(_rf_1_source, _rf_1_smrng)
_rf_2_filt = _rf_rngfilt(_rf_2_source, _rf_2_smrng)

_rf_1_upward = 0.0
_rf_1_upward := _rf_1_filt > _rf_1_filt[1] ? nz(_rf_1_upward[1]) + 1 : _rf_1_filt < _rf_1_filt[1] ? 0 : nz(_rf_1_upward[1])
_rf_1_downward = 0.0
_rf_1_downward := _rf_1_filt < _rf_1_filt[1] ? nz(_rf_1_downward[1]) + 1 : _rf_1_filt > _rf_1_filt[1] ? 0 : nz(_rf_1_downward[1])
_rf_2_upward = 0.0
_rf_2_upward := _rf_2_filt > _rf_2_filt[1] ? nz(_rf_2_upward[1]) + 1 : _rf_1_filt < _rf_1_filt[1] ? 0 : nz(_rf_2_upward[1])
_rf_2_downward = 0.0
_rf_2_downward := _rf_2_filt < _rf_2_filt[1] ? nz(_rf_2_downward[1]) + 1 : _rf_1_filt > _rf_1_filt[1] ? 0 : nz(_rf_2_downward[1])

_rf_1_plot_colour = (_rf_1_upward > 0) ? color.green : (_rf_1_downward > 0) ? color.red : color.yellow
_rf_2_plot_colour = (_rf_2_upward > 0) ? color.green : (_rf_2_downward > 0) ? color.red : color.yellow
_rf_bands_colour = (_rf_1_upward > 0 and _rf_2_upward > 0) ? color.green : (_rf_1_downward > 0 and _rf_2_downward > 0) ? color.red : color.yellow

_rf_1_band_plot = plot(_rf_1_filt, color=(_rf_show_shape) ? color.new(_rf_1_plot_colour, _rf_transparency) : na, title="RF 1 line", linewidth=_rf_border)
_rf_2_band_plot = plot(_rf_2_filt, color=(_rf_show_shape) ? color.new(_rf_2_plot_colour, _rf_transparency) : na, title="RF 2 line", linewidth=_rf_border)

_rf_1_long_cond = bool(na)
_rf_1_short_cond = bool(na)
_rf_1_long_cond := _rf_1_source > _rf_1_filt and _rf_1_source > _rf_1_source[1] and _rf_1_upward > 0 or _rf_1_source > _rf_1_filt and _rf_1_source < _rf_1_source[1] and _rf_1_upward > 0
_rf_1_short_cond := _rf_1_source < _rf_1_filt and _rf_1_source < _rf_1_source[1] and _rf_1_downward > 0 or _rf_1_source < _rf_1_filt and _rf_1_source > _rf_1_source[1] and _rf_1_downward > 0

_rf_1_cond_initiate = 0
_rf_1_cond_initiate := _rf_1_long_cond ? 1 : _rf_1_short_cond ? -1 : _rf_1_cond_initiate[1]
_rf_1_long_condition = _rf_1_long_cond and _rf_1_cond_initiate[1] == -1
_rf_1_short_condition = _rf_1_short_cond and _rf_1_cond_initiate[1] == 1

plotshape(_rf_1_long_condition and _rf_show_label, title="RF 1 buy signal", text="RF", textcolor=color.white, style=shape.labelup, location=location.belowbar, color=color.green)
plotshape(_rf_1_short_condition and _rf_show_label, title="RF 1 sell signal", text="RF", textcolor=color.white, style=shape.labeldown, location=location.abovebar, color=color.red)

// -----------------------------------------------------------------------------------------------------------------------------------------------------------
// HeikinAshiDot -- inputs
// -----------------------------------------------------------------------------------------------------------------------------------------------------------
_real_close_show = input(true, "", inline="Close dot", group="Close price dot")

_real_close_source = input.source(close, title="", inline="Close dot", group="Close price dot")
_real_close_colour = input.color(color.white, title="Color", inline="Close dot", group="Close price dot")
_real_close_size = input.int(3, title="Size", inline="Close dot", group="Close price dot")

// -----------------------------------------------------------------------------------------------------------------------------------------------------------
// HeikinAshiDot -- delivery
// -----------------------------------------------------------------------------------------------------------------------------------------------------------

_real_close = ticker.new(syminfo.prefix, syminfo.ticker)
_real_close_request = request.security(_real_close, timeframe.period, _real_close_source)
plot(_real_close_request, "Close price", style=plot.style_circles, linewidth=_real_close_size, color= (_real_close_show) ? _real_close_colour : na)

// -----------------------------------------------------------------------------------------------------------------------------------------------------------
// Chandelier exit -- inputs
// -----------------------------------------------------------------------------------------------------------------------------------------------------------

_ce_show = input(true, title="", inline="CE", group="Chandelier Exit")
_ce_period = input(1, title="ATR period", inline="CE", group="Chandelier Exit")
_ce_multiplier = input(0.6, title="ATR multiplier", inline="CE", group="Chandelier Exit")
_ce_use_close = input(true, title="Close", inline="CE", group="Chandelier Exit")

_ce_atr = _ce_multiplier * ta.atr(_ce_period)

_ce_long = (_ce_use_close ? ta.highest(close, _ce_period) : ta.highest(_ce_period)) - _ce_atr
_ce_long_previous = nz(_ce_long[1], _ce_long) 
_ce_long := close[1] > _ce_long_previous ? math.max(_ce_long, _ce_long_previous) : _ce_long

_ce_short = (_ce_use_close ? ta.lowest(close, _ce_period) : ta.lowest(_ce_period)) + _ce_atr
_ce_short_previous = nz(_ce_short[1], _ce_short)
_ce_short := close[1] < _ce_short_previous ? math.min(_ce_short, _ce_short_previous) : _ce_short

var int _ce_direction = 1
_ce_direction := close > _ce_short_previous ? 1 : close < _ce_long_previous ? -1 : _ce_direction

_ce_signal_buy = _ce_direction == 1 and _ce_direction[1] == -1
_ce_signal_sell = _ce_direction == -1 and _ce_direction[1] == 1
_ce_change_condition = _ce_direction != _ce_direction[1]

plotshape(_ce_signal_buy and _ce_show ? _ce_long : na, title="CE buy signal", text="CE", location=location.belowbar, style=shape.labelup, color=color.green, textcolor=color.white)
plotshape(_ce_signal_sell and _ce_show ? _ce_short : na, title="CE sell signal", text="CE", location=location.abovebar, style=shape.labeldown, color=color.red, textcolor=color.white)

// -----------------------------------------------------------------------------------------------------------------------------------------------------------
// Hull Suite -- inputs
// -----------------------------------------------------------------------------------------------------------------------------------------------------------

_hull_suite_visual  = input(true, title="", inline="Hull suite", group="Hull suite")
_hull_suite_source = input(close, title="", inline="Hull suite", group="Hull suite")
_hull_suite_switch = input.string("Hma", title="", options=["Hma", "Thma", "Ehma"], inline="Hull suite", group="Hull suite")
_hull_suite_length = input(55, title="Length", inline="Hull suite", group="Hull suite")
_hull_suite_multiplier = input(1.0, title="Multiplier", inline="Hull suite", group="Hull suite")
_hull_suite_transparency = input(50, title="Transp", inline="Hull suite", group="Hull suite")
_hull_suite_higher_tf = input(false, title="HTF", inline="Hull suite", group="Hull suite")
_hull_suite_higher_value = input.string("240", title="", inline="Hull suite", group="Hull suite")

// -----------------------------------------------------------------------------------------------------------------------------------------------------------
// Hull Suite -- delivery
// -----------------------------------------------------------------------------------------------------------------------------------------------------------

_hull_suite_HMA(_src, _length) =>  ta.wma(2 * ta.wma(_src, _length / 2) - ta.wma(_src, _length), math.round(math.sqrt(_length)))
_hull_suite_EHMA(_src, _length) =>  ta.ema(2 * ta.ema(_src, _length / 2) - ta.ema(_src, _length), math.round(math.sqrt(_length)))
_hull_suite_THMA(_src, _length) =>  ta.wma(ta.wma(_src,_length / 3) * 3 - ta.wma(_src, _length / 2) - ta.wma(_src, _length), _length)

_hull_suite_mode(_hull_suite_switch, src, len) =>
      _hull_suite_switch == "Hma"  ? _hull_suite_HMA(src, len) :
      _hull_suite_switch == "Ehma" ? _hull_suite_EHMA(src, len) : 
      _hull_suite_switch == "Thma" ? _hull_suite_THMA(src, len/2) : na

_hull_suite_pre_security = _hull_suite_mode(_hull_suite_switch, _hull_suite_source, int(_hull_suite_length * _hull_suite_multiplier))
_hull_suite_line_sources = _hull_suite_higher_tf ? request.security(syminfo.ticker, _hull_suite_higher_value, _hull_suite_pre_security) : _hull_suite_pre_security
_hull_suite_line_m = _hull_suite_line_sources[0]
_hull_suite_line_s = _hull_suite_line_sources[2]

_hull_suite_color = _hull_suite_line_sources > _hull_suite_line_sources[2] ? color.new(color.green, _hull_suite_transparency) : color.new(color.red, _hull_suite_transparency)
_hull_suite_line_1 = plot(_hull_suite_visual ? _hull_suite_line_m : na, title="Hull 1 line", color=_hull_suite_color)
_hull_suite_line_2 = plot(_hull_suite_visual ? _hull_suite_line_s : na, title="Hull 2 line", color=_hull_suite_color)

fill(_hull_suite_line_1, _hull_suite_line_2, title="Hull band filler", color=_hull_suite_color)

// -----------------------------------------------------------------------------------------------------------------------------------------------------------
// UT bot -- inputs
// -----------------------------------------------------------------------------------------------------------------------------------------------------------

_ut_src = close 
_ut_key_show_label = input(true, "", inline="UT", group="UT bot")
_ut_key_show_trailing_stop = input(true, "Trail", inline="UT", group="UT bot")
_ut_key_value = input.float(1, title = "Sensitivity", step = .5, inline="UT", group="UT bot")
_ut_atr_period = input(10, title="ATR period", inline="UT", group="UT bot")

// -----------------------------------------------------------------------------------------------------------------------------------------------------------
// UT bot -- delivery
// -----------------------------------------------------------------------------------------------------------------------------------------------------------

_ut_bot_atr = ta.atr(_ut_atr_period)
_ut_loss = _ut_key_value * _ut_bot_atr

_ut_atr_trailing_stop = 0.0
_ut_atr_trailing_stop := (_ut_src > nz(_ut_atr_trailing_stop[1], 0) and _ut_src[1] > nz(_ut_atr_trailing_stop[1], 0)) ? (math.max(nz(_ut_atr_trailing_stop[1]), _ut_src - _ut_loss)) : (_ut_src < nz(_ut_atr_trailing_stop[1], 0) and _ut_src[1] < nz(_ut_atr_trailing_stop[1], 0)) ? ( math.min(nz(_ut_atr_trailing_stop[1]), _ut_src + _ut_loss)) : (_ut_src > nz(_ut_atr_trailing_stop[1], 0)) ? (_ut_src - _ut_loss) : (_ut_src + _ut_loss) 

_ut_pos = 0
_ut_pos := (_ut_src[1] < nz(_ut_atr_trailing_stop[1], 0) and _ut_src > nz(_ut_atr_trailing_stop[1], 0)) ? 1 : (_ut_src[1] > nz(_ut_atr_trailing_stop[1], 0) and _ut_src < nz(_ut_atr_trailing_stop[1], 0)) ? -1 : nz(_ut_pos[1], 0)
   
_ut_xcolor = _ut_pos == -1 ? color.red: _ut_pos == 1 ? color.green : color.blue

plot(_ut_atr_trailing_stop, color=_ut_key_show_trailing_stop ? _ut_xcolor : na, title="UT trailing stop")

_ut_buy = ta.crossover(_ut_src, _ut_atr_trailing_stop) 
_ut_sell = ta.crossunder(_ut_src, _ut_atr_trailing_stop)

plotshape(_ut_key_show_label ? _ut_buy : na, title="UT buy signal", text="UT", style=shape.labelup, location=location.belowbar, color=color.green, textcolor=color.white)
plotshape(_ut_key_show_label ? _ut_sell : na, title="UT sell signal", text="UT", style=shape.labeldown, location=location.abovebar, color=color.red, textcolor=color.white)

// -----------------------------------------------------------------------------------------------------------------------------------------------------------
// MA -- inputs
// -----------------------------------------------------------------------------------------------------------------------------------------------------------

_ma_1_on = input(false, "1", inline="MA switch", group="MA")
_ma_2_on = input(false, "2", inline="MA switch", group="MA")
_ma_3_on = input(false, "3", inline="MA switch", group="MA")
_ma_4_on = input(false, "4", inline="MA switch", group="MA")
_ma_5_on = input(false, "5", inline="MA switch", group="MA")
_ma_6_on = input(false, "6", inline="MA switch", group="MA")
_ma_7_on = input(false, "7", inline="MA switch", group="MA")
_ma_8_on = input(false, "8", inline="MA switch", group="MA")
_ma_9_on = input(false, "9", inline="MA switch", group="MA")
_ma_10_on = input(false, "10", inline="MA switch", group="MA")

_ma_1_source = input.source(close, "", inline="MA1", group="MA")
_ma_1_length = input(5, "", inline="MA1", group="MA")
_ma_1_type = input.string("EMA", title="", options=["SMA", "EMA", "WMA", "VWMA", "SMMA"], inline="MA1", group="MA")
_ma_1_color = input.color(color.white, "", inline="MA1", group="MA")

_ma_2_source = input.source(close, "", inline="MA2", group="MA")
_ma_2_length = input(21, "", inline="MA2", group="MA")
_ma_2_type = input.string("EMA", title="", options=["SMA", "EMA", "WMA", "VWMA", "SMMA"], inline="MA2", group="MA")
_ma_2_color = input.color(color.silver, "", inline="MA2", group="MA")

_ma_3_source = input.source(close, "", inline="MA3", group="MA")
_ma_3_length = input(63, "", inline="MA3", group="MA")
_ma_3_type = input.string("EMA", title="", options=["SMA", "EMA", "WMA", "VWMA", "SMMA"], inline="MA3", group="MA")
_ma_3_color = input.color(color.teal, "", inline="MA3", group="MA")

_ma_4_source = input.source(close, "", inline="MA4", group="MA")
_ma_4_length = input(144, "", inline="MA4", group="MA")
_ma_4_type = input.string("EMA", title="", options=["SMA", "EMA", "WMA", "VWMA", "SMMA"], inline="MA4", group="MA")
_ma_4_color = input.color(color.white, "", inline="MA4", group="MA")

_ma_5_source = input.source(close, "", inline="MA5", group="MA")
_ma_5_length = input(200, "", inline="MA5", group="MA")
_ma_5_type = input.string("SMA", title="", options=["SMA", "EMA", "WMA", "VWMA", "SMMA"], inline="MA5", group="MA")
_ma_5_color = input.color(color.orange, "", inline="MA5", group="MA")

_ma_6_source = input.source(close, "", inline="MA6", group="MA")
_ma_6_length = input(18, "", inline="MA6", group="MA")
_ma_6_type = input.string("SMA", title="", options=["SMA", "EMA", "WMA", "VWMA", "SMMA"], inline="MA6", group="MA")
_ma_6_color = input.color(color.olive, "", inline="MA6", group="MA")

_ma_7_source = input.source(close, "", inline="MA7", group="MA")
_ma_7_length = input(8, "", inline="MA7", group="MA")
_ma_7_type = input.string("EMA", title="", options=["SMA", "EMA", "WMA", "VWMA", "SMMA"], inline="MA7", group="MA")
_ma_7_color = input.color(color.teal, "", inline="MA7", group="MA")

_ma_8_source = input.source(close, "", inline="MA8", group="MA")
_ma_8_length = input(10, "", inline="MA8", group="MA")
_ma_8_type = input.string("SMMA", title="", options=["SMA", "EMA", "WMA", "VWMA", "SMMA"], inline="MA8", group="MA")
_ma_8_color = input.color(color.purple, "", inline="MA8", group="MA")

_ma_9_source = input.source(close, "", inline="MA9", group="MA")
_ma_9_length = input(144, "", inline="MA9", group="MA")
_ma_9_type = input.string("SMA", title="", options=["SMA", "EMA", "WMA", "VWMA", "SMMA"], inline="MA9", group="MA")
_ma_9_color = input.color(color.silver, "", inline="MA9", group="MA")

_ma_10_source = input.source(close, "", inline="MA10", group="MA")
_ma_10_length = input(200, "", inline="MA10", group="MA")
_ma_10_type = input.string("EMA", title="", options=["SMA", "EMA", "WMA", "VWMA", "SMMA"], inline="MA10", group="MA")
_ma_10_color = input.color(color.lime, "", inline="MA10", group="MA")

// -----------------------------------------------------------------------------------------------------------------------------------------------------------
// MA -- delivery
// -----------------------------------------------------------------------------------------------------------------------------------------------------------

_ma (_src, _len, _type) =>
    switch _type
        "SMA" => ta.sma(_src, _len)
        "EMA" => ta.ema(_src, _len)
        "WMA" => ta.wma(_src, _len)
        "VWMA" => ta.vwma(_src, _len)
        "SMMA" =>
            _ma = ta.sma ( _src, _len )
            _ma_final = 0.0
            _ma_final := na ( _ma_final[1] ) ? _ma : ( _ma_final[1] * ( _len - 1 ) + _src ) / _len
            
_ma_1_output = _ma(_ma_1_source, _ma_1_length, _ma_1_type)
_ma_2_output = _ma(_ma_2_source, _ma_2_length, _ma_2_type)
_ma_3_output = _ma(_ma_3_source, _ma_3_length, _ma_3_type)
_ma_4_output = _ma(_ma_4_source, _ma_4_length, _ma_4_type)
_ma_5_output = _ma(_ma_5_source, _ma_5_length, _ma_5_type)
_ma_6_output = _ma(_ma_6_source, _ma_6_length, _ma_6_type)
_ma_7_output = _ma(_ma_7_source, _ma_7_length, _ma_7_type)
_ma_8_output = _ma(_ma_8_source, _ma_8_length, _ma_8_type)
_ma_9_output = _ma(_ma_9_source, _ma_9_length, _ma_9_type)
_ma_10_output = _ma(_ma_10_source, _ma_10_length, _ma_10_type)

_ma_1_line = plot(_ma_1_on ? _ma_1_output : na, "MA 1 line", color=_ma_1_color)
_ma_2_line = plot(_ma_2_on ? _ma_2_output : na, "MA 2 line", color=_ma_2_color)
_ma_3_line = plot(_ma_3_on ? _ma_3_output : na, "MA 3 line", color=_ma_3_color)
_ma_4_line = plot(_ma_4_on ? _ma_4_output : na, "MA 4 line", color=_ma_4_color)
_ma_5_line = plot(_ma_5_on ? _ma_5_output : na, "MA 5 line", color=_ma_5_color)
_ma_6_line = plot(_ma_6_on ? _ma_6_output : na, "MA 6 line", color=_ma_6_color)
_ma_7_line = plot(_ma_7_on ? _ma_7_output : na, "MA 7 line", color=_ma_7_color)
_ma_8_line = plot(_ma_8_on ? _ma_8_output : na, "MA 8 line", color=_ma_8_color)
_ma_9_line = plot(_ma_9_on ? _ma_9_output : na, "MA 9 line", color=_ma_9_color)
_ma_10_line = plot(_ma_10_on ? _ma_10_output : na, "MA 10 line", color=_ma_10_color)

// -----------------------------------------------------------------------------------------------------------------------------------------------------------
// Trendfill -- inputs
// -----------------------------------------------------------------------------------------------------------------------------------------------------------

_trend_fill_show = input(false, "", inline="Trend fill", group="Trend fill")
_trend_fill_items = input.string("3", title="Trend across first", options=["2", "3", "4", "5"], inline="Trend fill", group="Trend fill")
_trend_fill_transparency = input.int(90, "Transparency", inline="Trend fill", group="Trend fill")

// -----------------------------------------------------------------------------------------------------------------------------------------------------------
// Trendfill -- delivery
// -----------------------------------------------------------------------------------------------------------------------------------------------------------

_trend_fill_color = _trend_fill_show ? color.new(color.white, 100) : na

if _trend_fill_items=="2"
    if _ma_1_output>_ma_2_output
        _trend_fill_color := color.new(color.green, _trend_fill_transparency)
    if _ma_1_output<_ma_2_output
        _trend_fill_color := color.new(color.red, _trend_fill_transparency)

if _trend_fill_items=="3"
    if _ma_1_output>_ma_2_output and _ma_2_output>_ma_3_output
        _trend_fill_color := color.new(color.green, _trend_fill_transparency)
    if _ma_1_output<_ma_2_output and _ma_2_output<_ma_3_output
        _trend_fill_color := color.new(color.red, _trend_fill_transparency)

if _trend_fill_items=="4"
    if _ma_1_output>_ma_2_output and _ma_2_output>_ma_3_output and _ma_3_output>_ma_4_output
        _trend_fill_color := color.new(color.green, _trend_fill_transparency)
    if _ma_1_output<_ma_2_output and _ma_2_output<_ma_3_output and _ma_3_output<_ma_4_output
        _trend_fill_color := color.new(color.red, _trend_fill_transparency)
    
if _trend_fill_items=="5"
    if _ma_1_output>_ma_2_output and _ma_2_output>_ma_3_output and _ma_3_output>_ma_4_output and _ma_4_output>_ma_5_output
        _trend_fill_color := color.new(color.green, _trend_fill_transparency)
    if _ma_1_output<_ma_2_output and _ma_2_output<_ma_3_output and _ma_3_output<_ma_4_output and _ma_4_output<_ma_5_output
        _trend_fill_color := color.new(color.red, _trend_fill_transparency)
    
fill(_ma_1_line, _ma_2_line, _trend_fill_items=="2" and _trend_fill_show ? _trend_fill_color : na, fillgaps=false)
fill(_ma_1_line, _ma_3_line, _trend_fill_items=="3" and _trend_fill_show ? _trend_fill_color : na, fillgaps=false)
fill(_ma_1_line, _ma_4_line, _trend_fill_items=="4" and _trend_fill_show ? _trend_fill_color : na, fillgaps=false)
fill(_ma_1_line, _ma_5_line, _trend_fill_items=="5" and _trend_fill_show ? _trend_fill_color : na, fillgaps=false)

// -----------------------------------------------------------------------------------------------------------------------------------------------------------
// Vwap standard deviations -- inputs
// -----------------------------------------------------------------------------------------------------------------------------------------------------------

_vsd_color_0 = input.color(color.yellow, "", inline="Vwap standard deviations", group="Vwap standard deviations")
_vsd_set_0 = input(true, "", inline="Vwap standard deviations", group="Vwap standard deviations")

_vsd_color_1 = input.color(color.orange, "", inline="Vwap standard deviations", group="Vwap standard deviations")
_vsd_set_1 = input(true, "", inline="Vwap standard deviations", group="Vwap standard deviations")

_vsd_color_2 = input.color(color.red, "", inline="Vwap standard deviations", group="Vwap standard deviations")
_vsd_set_2 = input(true, "", inline="Vwap standard deviations", group="Vwap standard deviations")

_vsd_color_3 = input.color(color.blue, "", inline="Vwap standard deviations", group="Vwap standard deviations")
_vsd_set_3 = input(true, "", inline="Vwap standard deviations", group="Vwap standard deviations")

_vsd_color_4 = input.color(color.green, "", inline="Vwap standard deviations", group="Vwap standard deviations")
_vsd_set_4 = input(true, "", inline="Vwap standard deviations", group="Vwap standard deviations")

_vsd_src = input(hlc3, "", inline="Vwap standard deviations settings", group="Vwap standard deviations")
    
_vsd_vwap_func(_vsd_src, _vsd_is_new_period) =>
	var float _vsd_sum_src_vol = na
	var float _vsd_sum_vol = na
    var float _vsd_sum_src_src_vol = na

	_vsd_sum_src_vol := _vsd_is_new_period ? _vsd_src * volume : _vsd_src * volume + _vsd_sum_src_vol[1]
	_vsd_sum_vol := _vsd_is_new_period ? volume : volume + _vsd_sum_vol[1]
	_vsd_sum_src_src_vol := _vsd_is_new_period ? volume * math.pow(_vsd_src, 2) : volume * math.pow(_vsd_src, 2) + _vsd_sum_src_src_vol[1]

	_vsd_vwap = _vsd_sum_src_vol / _vsd_sum_vol
	
	_vsd_variance = _vsd_sum_src_src_vol / _vsd_sum_vol - math.pow(_vsd_vwap, 2)
	_vsd_variance := _vsd_variance < 0 ? 0 : _vsd_variance
	_vsd_deviation = math.sqrt(_vsd_variance)

	_vsd_lower_1 = _vsd_vwap - _vsd_deviation
	_vsd_upper_1 = _vsd_vwap + _vsd_deviation

	_vsd_lower_2 = _vsd_vwap - _vsd_deviation * 2
	_vsd_upper_2 = _vsd_vwap + _vsd_deviation * 2

	_vsd_lower_3 = _vsd_vwap - _vsd_deviation * 3
	_vsd_upper_3 = _vsd_vwap + _vsd_deviation * 3

	_vsd_lower_4 = _vsd_vwap - _vsd_deviation * 4
	_vsd_upper_4 = _vsd_vwap + _vsd_deviation * 4

	[_vsd_vwap, _vsd_lower_1, _vsd_upper_1, _vsd_lower_2, _vsd_upper_2, _vsd_lower_3, _vsd_upper_3, _vsd_lower_4, _vsd_upper_4]

_vsd_anchor = "Session"

_vsd_is_new_period = switch _vsd_anchor
	"Session" => ta.change(time("D"))
	=> false

isEsdAnchor = _vsd_anchor == "Earnings" or _vsd_anchor == "Dividends" or _vsd_anchor == "Splits"
if na(_vsd_src[1]) and not isEsdAnchor
	_vsd_is_new_period := true

[_vsd_vwap, _vsd_bot_1, _vsd_top_1, _vsd_bot_2, _vsd_top_2, _vsd_bot_3, _vsd_top_3, _vsd_bot_4, _vsd_top_4] = _vsd_vwap_func(_vsd_src, _vsd_is_new_period)

plot(_vsd_set_0 ? _vsd_vwap : na, title="VWAP", color=_vsd_color_0)

plot(_vsd_set_1 ? _vsd_top_1 : na, title="VWAP x2 upper band", color=_vsd_color_1)
plot(_vsd_set_1 ? _vsd_bot_1 : na, title="VWAP x2 lower band", color=_vsd_color_1)

plot(_vsd_set_2 ? _vsd_top_2 : na, title="VWAP x2 upper band", color=_vsd_color_2)
plot(_vsd_set_2 ? _vsd_bot_2 : na, title="VWAP x2 lower band", color=_vsd_color_2)

plot(_vsd_set_3 ? _vsd_top_3 : na, title="VWAP x2 upper band", color=_vsd_color_3)
plot(_vsd_set_3 ? _vsd_bot_3 : na, title="VWAP x2 lower band", color=_vsd_color_3)

plot(_vsd_set_4 ? _vsd_top_4 : na, title="VWAP x2 upper band", color=_vsd_color_4)
plot(_vsd_set_4 ? _vsd_bot_4 : na, title="VWAP x2 lower band", color=_vsd_color_4)

 