BTC Risk Metric 
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Skywalking2874
//@version=5

indicator("Risk", overlay = false, max_bars_back=5000)

find_ath(_src) => 
    // Returns a series of the ATH value
    var ath = 0.0
    if _src > ath
        ath := _src
    ath

find_atl(_src) =>
    // Returns a series of the ATL value
    var atl = 2.5
    if _src < atl
        atl := _src
    atl

threeseventyfour = ta.sma(close, 374)

average = (math.log(close) - math.log(threeseventyfour)) * math.pow(bar_index, 0.395)

highest_value = find_ath(average)
lowest_value = find_atl(average)

average_normalized = (average - lowest_value) / (highest_value - lowest_value)

price_zero = threeseventyfour * math.exp((0.0*(highest_value-lowest_value)+lowest_value)/(math.pow(bar_index, 0.395)))
price_point_one = threeseventyfour * math.exp((0.1*(highest_value-lowest_value)+lowest_value)/(math.pow(bar_index, 0.395)))
price_point_two = threeseventyfour * math.exp((0.2*(highest_value-lowest_value)+lowest_value)/(math.pow(bar_index, 0.395)))
price_point_three = threeseventyfour * math.exp((0.3*(highest_value-lowest_value)+lowest_value)/(math.pow(bar_index, 0.395)))
price_point_four = threeseventyfour * math.exp((0.4*(highest_value-lowest_value)+lowest_value)/(math.pow(bar_index, 0.395)))
price_point_five = threeseventyfour * math.exp((0.5*(highest_value-lowest_value)+lowest_value)/(math.pow(bar_index, 0.395)))
price_point_six = threeseventyfour * math.exp((0.6*(highest_value-lowest_value)+lowest_value)/(math.pow(bar_index, 0.395)))
price_point_seven = threeseventyfour * math.exp((0.7*(highest_value-lowest_value)+lowest_value)/(math.pow(bar_index, 0.395)))
price_point_eight = threeseventyfour * math.exp((0.8*(highest_value-lowest_value)+lowest_value)/(math.pow(bar_index, 0.395)))
price_point_nine = threeseventyfour * math.exp((0.9*(highest_value-lowest_value)+lowest_value)/(math.pow(bar_index, 0.395)))
price_one = threeseventyfour * math.exp((1.0*(highest_value-lowest_value)+lowest_value)/(math.pow(bar_index, 0.395)))

price_zero_string = "" 
price_point_one_string = "" 
price_point_two_string = ""
price_point_three_string = "" 
price_point_four_string = "" 
price_point_five_string = "" 
price_point_six_string = ""
price_point_seven_string = ""
price_point_eight_string = "" 
price_point_nine_string = "" 
price_one_string = "" 

risk_prices = input.bool(true, "Display the price corresponding with risk thresholds", "The price associated to each risk threshhold will be displayed when this is turned on")

if barstate.islast and risk_prices
    price_zero_string := str.tostring(math.round(price_zero))
    price_point_one_string := str.tostring(math.round(price_point_one))
    price_point_two_string := str.tostring(math.round(price_point_two))
    price_point_three_string := str.tostring(math.round(price_point_three))
    price_point_four_string := str.tostring(math.round(price_point_four))
    price_point_five_string := str.tostring(math.round(price_point_five))
    price_point_six_string := str.tostring(math.round(price_point_six))
    price_point_seven_string := str.tostring(math.round(price_point_seven))
    price_point_eight_string := str.tostring(math.round(price_point_eight))
    price_point_nine_string := str.tostring(math.round(price_point_nine))
    price_one_string := str.tostring(math.round(price_one))
    
    label.new(x=(bar_index+300), y = 0.0, textcolor=color.new(#00a2a2, 0), text="Risk = 0.0   ➔   Price = $"+price_zero_string, style=label.style_none, size=size.large)
    label.new(x=(bar_index+300), y = 0.1, textcolor=color.new(#19abab, 0), text="Risk = 0.1   ➔   Price = $"+price_point_one_string, style=label.style_none, size=size.large)
    label.new(x=(bar_index+300), y = 0.2, textcolor=color.new(#32b4b4, 0), text="Risk = 0.2   ➔   Price = $"+price_point_two_string, style=label.style_none, size=size.large)
    label.new(x=(bar_index+300), y = 0.3, textcolor=color.new(#4cbdbd, 0), text="Risk = 0.3   ➔   Price = $"+price_point_three_string, style=label.style_none, size=size.large)
    label.new(x=(bar_index+300), y = 0.4, textcolor=color.new(#66c7c7, 0), text="Risk = 0.4   ➔   Price = $"+price_point_four_string, style=label.style_none, size=size.large)
    label.new(x=(bar_index+300), y = 0.5, textcolor=color.new(#7fd0d0, 0), text="Risk = 0.5   ➔   Price = $"+price_point_five_string, style=label.style_none, size=size.large)
    label.new(x=(bar_index+300), y = 0.6, textcolor=color.new(#e76666, 0), text="Risk = 0.6   ➔   Price = $"+price_point_six_string, style=label.style_none, size=size.large)
    label.new(x=(bar_index+300), y = 0.7, textcolor=color.new(#e34c4c, 0), text="Risk = 0.7   ➔   Price = $"+price_point_seven_string, style=label.style_none, size=size.large)
    label.new(x=(bar_index+300), y = 0.8, textcolor=color.new(#df3232, 0), text="Risk = 0.8   ➔   Price = $"+price_point_eight_string, style=label.style_none, size=size.large)
    label.new(x=(bar_index+300), y = 0.9, textcolor=color.new(#db1919, 0), text="Risk = 0.9   ➔   Price = $"+price_point_nine_string, style=label.style_none, size=size.large)
    label.new(x=(bar_index+300), y = 1.0, textcolor=color.new(#d80000, 0), text="Risk = 1.0   ➔   Price = $"+price_one_string, style=label.style_none, size=size.large)

transparant = color.rgb(0, 0, 0, 100)
bar_color = color.rgb(0, 128, 255)

gradient0 = color.from_gradient(average_normalized, 0, 0.1, #0000ff, #000bff)
gradient1 = color.from_gradient(average_normalized, 0.1, 0.2, #000bff, #0090ff)
gradient2 = color.from_gradient(average_normalized, 0.2, 0.3, #0090ff, #00fbff)
gradient3 = color.from_gradient(average_normalized, 0.3, 0.4, #00fbff, #00ff7e)
gradient4 = color.from_gradient(average_normalized, 0.4, 0.5, #00ff7e, #00ff37)
gradient5 = color.from_gradient(average_normalized, 0.5, 0.6, #00ff37, #94ff00)
gradient6 = color.from_gradient(average_normalized, 0.6, 0.7, #94ff00, #ffff00)
gradient7 = color.from_gradient(average_normalized, 0.7, 0.8, #ffff00, #ffb200)
gradient8 = color.from_gradient(average_normalized, 0.8, 0.9, #ffb200, #ff8900)
gradient9 = color.from_gradient(average_normalized, 0.9, 1.0, #ff8900, #ff0017)

fill0 = color.new(#005555, 61)
fill1 = color.new(#005555, 69)
fill2 = color.new(#005555, 76)
fill3 = color.new(#005555, 83)
fill4 = color.new(#005555, 90)
fill5 = color.new(#8b0000, 90)
fill6 = color.new(#8b0000, 83)
fill7 = color.new(#8b0000, 76)
fill8 = color.new(#8b0000, 69)
fill9 = color.new(#8b0000, 61)
bar_color := average_normalized < 0.1 ? gradient0 : 
  average_normalized < 0.2 ? gradient1 : 
  average_normalized < 0.3 ? gradient2 : 
  average_normalized < 0.4 ? gradient3 : 
  average_normalized < 0.5 ? gradient4 : 
  average_normalized < 0.6 ? gradient5 :
  average_normalized < 0.7 ? gradient6 : 
  average_normalized < 0.8 ? gradient7 :
  average_normalized < 0.9 ? gradient8 : gradient9

buy0 = hline(0.0, color=transparant, linestyle=hline.style_solid)
buy1 = hline(0.1, color=transparant, linestyle=hline.style_solid)
buy2 = hline(0.2, color=transparant, linestyle=hline.style_solid)
buy3 = hline(0.3, color=transparant, linestyle=hline.style_solid)
buy4 = hline(0.4, color=transparant, linestyle=hline.style_solid)
buy5 = hline(0.5, color=transparant, linestyle=hline.style_solid)
    
sell0 = hline(0.6, color=transparant, linestyle=hline.style_solid)
sell1 = hline(0.7, color=transparant, linestyle=hline.style_solid)
sell2 = hline(0.8, color=transparant, linestyle=hline.style_solid)
sell3 = hline(0.9, color=transparant, linestyle=hline.style_solid)
sell4 = hline(1.0, color=transparant, linestyle=hline.style_solid)
    
fill(buy0, buy1, fill0, "DCA 5x")
fill(buy1, buy2, fill1, "DCA 4x")
fill(buy2, buy3, fill2, "DCA 3x")
fill(buy3, buy4, fill3, "DCA 2x")
fill(buy4, buy5, fill4, "DCA 1x")
fill(sell0, sell1, fill5, "DCA 1y")
fill(sell1, sell2, fill6, "DCA 2y")
fill(sell2, sell3, fill7, "DCA 3y")
fill(sell3, sell4, fill8, "DCA 4y")

plot(average_normalized, color=bar_color, title = "Risk")