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

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

// ----------------------------------------------}
// LIBRARY
// ----------------------------------------------{

import weak_hand/RiskManagement/1 as risk_management

// ----------------------------------------------}
// INPUTS
// ----------------------------------------------{

int length      = input.int(14, "Length", minval = 1)

color col_apc   = input.color(#B71C1C, "", inline = "apc")
color col_atr   = input.color(#F57F17, "", inline = "atr")
color col_mpf   = input.color(#E65100, "", inline = "mpf")
color col_cross = input.color(#4DD0E1, "", inline = "cross")

bool show_apc = input.bool(true, "Absolut Price Change"     , inline = "apc")
bool show_atr = input.bool(true, "Average True Range"       , inline = "atr")
bool show_mpf = input.bool(true, "Maximum Price Fluctuation", inline = "mpf")
bool show_bg  = input.bool(true, "MPF > APC Cross"          , inline = "cross")

//int length_high = input.int(50, "Length", minval = 20)
int length_high = 50
bool col_theme  = input.bool(false, "Dark Theme" )

// ----------------------------------------------}
// CALCULATIONS
// ----------------------------------------------{

float apc = risk_management.absolute_price_changes(length)
float mpf = risk_management.maximum_price_fluctuation(length)
float atr = ta.atr(length)

float highest_apc = ta.highest(apc, length_high)
float highest_mpf = ta.highest(mpf, length_high)
float highest_atr = ta.highest(atr, length_high)

color col_cross_gradient = color.from_gradient(mpf, 0, highest_mpf, col_theme ? color.black : color.white, col_cross)

// ----------------------------------------------}
// OUTPUTS
// ----------------------------------------------{

plot_apc  = plot(show_apc ? apc : na, "APC", col_apc, 1, editable = false)
plot_mpf  = plot(show_mpf ? mpf : na, "MPF", col_mpf, 1, editable = false)
plot_atr  = plot(show_atr ? atr : na, "ATR", col_atr, 1, editable = false)
plot_zero = plot(0, "Zero Line", na, editable = false, display = display.none)

fill(plot_zero, plot_apc, highest_apc, 0, bottom_color = show_apc ? (col_theme ? color.black : color.white) : na, top_color = show_apc ? col_apc : na, editable = false)
fill(plot_zero, plot_mpf, highest_mpf, 0, bottom_color = show_mpf ? (col_theme ? color.black : color.white) : na, top_color = show_mpf ? col_mpf : na, editable = false)
fill(plot_zero, plot_atr, highest_atr, 0, bottom_color = show_atr ? (col_theme ? color.black : color.white) : na, top_color = show_atr ? col_atr : na, editable = false)

bgcolor(show_bg ? (mpf > apc ? col_cross_gradient : na) : na, editable = false)
