MyVolatilityBandss
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © ClassicScott

//@version=5

// @description Just a lil' library of volatility bands that I use in some scripts
library("MyVolatilityBandss")


// @function Bollinger Bands
// @param src float
// @param lkbk int
// @param mult float
// @param basis float
// @param band_width float
// @returns Bollinger Bands with an outer band of varying thickness adjustable by the band_width input
export bollingerbands(float src, int lkbk, float mult, float basis, float band_width) =>
	dev = mult * ta.stdev(src, lkbk)

	upper = basis + dev
	lower = basis - dev

	width = (upper - lower) / band_width

	inner_upper = upper - width
	inner_lower = lower + width

	[basis, upper, lower, inner_upper, inner_lower]


// @function Donchian Channels
// @param src float
// @param lkbk int
// @param band_width float
// @returns Donchian Channels with an outer band of varying thickness adjustable by the band_width input
export donchianchannels(float src, int lkbk, float band_width) =>
	upper = ta.highest(src, lkbk)
	lower = ta.lowest(src, lkbk)

	width = (upper - lower) / band_width

	inner_upper = upper - width
	inner_lower = lower + width

	basis = math.avg(upper, lower)

	[basis, upper, lower, inner_upper, inner_lower]


// @function Double Half Donchian Channels
// @param src float
// @param lkbk int
// @param divisor float
// @returns two adjustable bases calculated using Donchian Channels calculation that can act as a measure of volatility for below chart indicators
export doublehalfdonchianchannels(float src, int lkbk, float divisor) =>
	_high = ta.highest(src, lkbk)
	_low = ta.lowest(src, lkbk)

	high_vol = _high / divisor
	low_vol = _low / divisor

	[high_vol, low_vol]


// @function Keltner Channels
// @param src float
// @param lkbk int
// @param mult float
// @param atr_lkbk int
// @param basis float
// @param band_width float
// @returns Keltner Channels with an outer band of varying thickness adjustable by the band_width input
export keltnerchannels(int atr_lkbk, float mult, float basis, float band_width) =>
	upper = basis + ta.atr(atr_lkbk) * mult
	lower = basis - ta.atr(atr_lkbk) * mult

	width = (upper - lower) / band_width
	
	inner_upper = upper - width
	inner_lower = lower + width

	[basis, upper, lower, inner_upper, inner_lower]