Volatility Percentage Monitor
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/

// © Fab_Coin_

// Volatility Percentage Monitor (VPM)

//@version=5
indicator("Volatility Percentage Monitor", "VPM", overlay=false)


// HMI Inputs
// tf				    = input.timeframe(defval="1D", title="Timeframe", options=['1D', '1W', '1M'])
period_study		= input(1, title = "Study period       ", tooltip = 'Daily default (1)', inline = 'study')
study_bool			= input.bool(true, '', inline = 'study')
period_short	 	= input(15, title =	"Short term average ", tooltip = '10 days default (10)', inline = 'short')
short_bool			= input.bool(false, '', inline = 'short')
period_mid		 	= input(30, title =	"Mid term average   ", tooltip = 'Monthly default (30)', inline = 'mid')
mid_bool			= input.bool(false, '', inline = 'mid')
period_long		 	= input(100, title = "Longterm average  ", tooltip = 'Longterm default (100)', inline = 'long')
long_bool			= input.bool(false, '', inline = 'long')

avg_method 			= input.string(title = "Average method  ", defval="SMA", options=["RMA", "SMA", "EMA", "WMA"], tooltip = 'SMA standard setting')

std_dev_bool		= input.bool(true, 'Long term Std dev', 'Display Std Dev ON/OFF')

BBperiods 			= input.int(20, title = 'Periods', step = 1, inline = 'BB', group = 'Bollinger Bands Settings')
BBstdev 			= input.float(1.1, title = 'Std Dev', step = 0.1, inline = 'BB', group = 'Bollinger Bands Settings')
BB_bool 			= input(true, 'BB ON/OFF', inline = 'BB', group = 'Bollinger Bands Settings')


// Vars
var study_series	= 0.0
var short_series	= 0.0
var mid_series		= 0.0
var long_series		= 0.0
var stdev_series	= 0.0


// Functions and calc
avg_type(source, length) =>
	switch avg_method
		"RMA" => ta.rma(source, length)
		"SMA" => ta.sma(source, length)
		"EMA" => ta.ema(source, length)
		"WMA" => ta.wma(source, length)

study_series 	:= avg_type(ta.tr(true) / close * 100, period_study)
short_series 	:= avg_type(ta.tr(true) / close * 100, period_short)
mid_series 		:= avg_type(ta.tr(true) / close * 100, period_mid)
long_series 	:= avg_type(ta.tr(true) / close * 100, period_long)
stdev_series	:= ta.stdev(study_series, period_long)


// Bollinger calc
[mid, top, bottom] = ta.bb(study_series, BBperiods, BBstdev)


// Plot stuff
plot(study_bool ? study_series : na, title = "Study period", color = color.new(color.orange, 10), linewidth = 2, style = plot.style_linebr)
plot(short_bool ? short_series : na, title = "Short term", color = color.new(color.green, 20), linewidth = 2, style = plot.style_linebr)
plot(mid_bool ? mid_series : na, title = "Mid term", color = color.new(color.blue, 20), linewidth = 2, style = plot.style_linebr)
plot(long_bool ? long_series : na, title = "Long term", color = color.new(color.gray, 0), linewidth = 2, style = plot.style_linebr)
plot(std_dev_bool ? stdev_series : na, title = "Std dev Long term", color = color.new(color.red, 30), linewidth = 2, style = plot.style_linebr)

BBup 	= plot(BB_bool ? top : na, title='Top BB', color = color.new(color.aqua, 45), linewidth=1, style=plot.style_linebr)
BBdown 	= plot(BB_bool ? bottom : na, title='Bottom BB', color = color.new(color.blue, 45), linewidth=1, style=plot.style_linebr)
BBmid 	= plot(BB_bool ? mid : na, title='Middle BB', color = color.new(color.white, 50), linewidth=1, style=plot.style_linebr)
fill(BBup, BBmid, title = 'Volatility above average', color = color.new(color.aqua, 85))
fill(BBdown, BBmid, title = 'Volatility below average', color = color.new(color.navy, 85))


// Alerts
if close > mid
	alert('Mid volatility reach')
if close > top
	alert('Top Volatility reach')



// ...TheEnd...