BTCUSD Risk Oscillator
// 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

// Bitcoin Market Cycles (Taken from the Bitcoin All Time History Index):
// 1.  Bottom: 2010-07-17 : $0.05 (first recorded candle)
//     Top:    2011-06-08 : $29.60

// 2.  Bottom: 2011-11-18 : $2.05
//     Top:    2013-12-04 : $1237.96
    
// 3.  Bottom: 2015-01-14 : $182.00
//     Top:    2017-12-16 : $19423.67

// 4.  Bottom: 2018-12-15 : $3181.68
//     Top: TBD (Presumed local top @ 2021-04-13 : $63589.92)

// Bitcoin Halving Dates: 
// 1.   2012-11-28 : 50BTC/block - 7200 BTC/Day
// 2.   2016-07-09 : 25BTC/block - 3600 BTC/Day
// 3.   2020-05-11 : 12.5BTC/block - 1800 BTC/Day

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

normalize(_src, min, max) =>
    // Normalizes series from 0 to 1
    (_src - min) / (max - min)

//Halving Dates
halving1 = timestamp("2012-11-28")
halving2 = timestamp("2016-07-09")
halving3 = timestamp("2020-05-11")
    
//Normalization Dates Note: data from pre bottom2 will not be used 
start = timestamp("2010-07-17")
bottom2 = timestamp("2011-11-18")
bottom3 = timestamp("2015-01-14")
bottom4 = timestamp("2018-12-15")

//*********************************************** 50Day/50Week ***********************************************//
fastslow_ratio_min_cycle1 = 0.79
fastslow_ratio_max_cycle1 = 5.92
fastslow_ratio_min_cycle2 = 0.71
fastslow_ratio_max_cycle2 = 3.47
fastslow_ratio_min_cycle3 = 0.64
fastslow_ratio_max_cycle3 = 2.6
// fastslow_ratio_min_cycle4 = input.float(0.64, "Fast/Slow Ratio Minimum Value", 0, 1000, 0.01, "Input the minimum value displayed by the Fast/Slow Ratio")
// fastslow_ratio_max_cycle4 = input.float(2.11, "Fast/Slow Ratio Maximum Value", 0, 1000, 0.01, "Input the maximum value displayed by the Fast/Slow Ratio")
fastslow_ratio_min_cycle4 = 0.64
fastslow_ratio_max_cycle4 = 2.11

fiftyday = ta.ema(close, 50)
fiftyweek = request.security(syminfo.tickerid, "W", fiftyday)

fastslow_ratio = fiftyday / fiftyweek
fastslow_ratio_normalized = close  //create a dummy series 

if time >= bottom4
    fastslow_ratio_normalized := normalize(fastslow_ratio, fastslow_ratio_min_cycle4, fastslow_ratio_max_cycle4)

if time >= bottom3 and time < bottom4
    fastslow_ratio_normalized := normalize(fastslow_ratio, fastslow_ratio_min_cycle3, fastslow_ratio_max_cycle3)

if time >= bottom2 and time < bottom3
    fastslow_ratio_normalized := normalize(fastslow_ratio, fastslow_ratio_min_cycle2, fastslow_ratio_max_cycle2)

if time >= start and time < bottom2
    fastslow_ratio_normalized := normalize(fastslow_ratio, fastslow_ratio_min_cycle1, fastslow_ratio_max_cycle1)
 
// plot(fastslow_ratio_normalized, color = color.orange, title = "Normalized 50Day/50Week")

//*********************************************** Price/52W EMA ***********************************************//
pricebyfiftytwo_min_cycle1 = 0.49
pricebyfiftytwo_max_cycle1 = 5.08
pricebyfiftytwo_min_cycle2 = 0.47
pricebyfiftytwo_max_cycle2 = 8.91
pricebyfiftytwo_min_cycle3 = 0.43
pricebyfiftytwo_max_cycle3 = 4.70
pricebyfiftytwo_min_cycle4 = 0.48
pricebyfiftytwo_max_cycle4 = 3.09

fiftytwoweek_ema = request.security(syminfo.tickerid, "W", ta.ema(close, 52))

pricebyfiftytwo = close/fiftytwoweek_ema
pricebyfiftytwo_normalized = close  //create a dummy series 

if time >= bottom4
    pricebyfiftytwo_normalized := normalize(pricebyfiftytwo, pricebyfiftytwo_min_cycle4, pricebyfiftytwo_max_cycle4)

if time >= bottom3 and time < bottom4
    pricebyfiftytwo_normalized := normalize(pricebyfiftytwo, pricebyfiftytwo_min_cycle3, pricebyfiftytwo_max_cycle3)

if time >= bottom2 and time < bottom3
    pricebyfiftytwo_normalized := normalize(pricebyfiftytwo, pricebyfiftytwo_min_cycle2, pricebyfiftytwo_max_cycle2)

if time >= start and time < bottom2
    pricebyfiftytwo_normalized := normalize(pricebyfiftytwo, pricebyfiftytwo_min_cycle1, pricebyfiftytwo_max_cycle1)
    
// plot(pricebyfiftytwo_normalized, color = color.green, title = "Normalized Price/52W EMA")

//*********************************************** Price/400D SMA ***********************************************//
pricebyfourhundredday_min_cycle1 = 0.52
pricebyfourhundredday_max_cycle1 = 2.49
pricebyfourhundredday_min_cycle2 = 0.45
pricebyfourhundredday_max_cycle2 = 13.20
pricebyfourhundredday_min_cycle3 = 0.34
pricebyfourhundredday_max_cycle3 = 6.15
pricebyfourhundredday_min_cycle4 = 0.38
pricebyfourhundredday_max_cycle4 = 3.86

fourhundredday = ta.sma(close, 400)

pricebyfourhundredday = close/fourhundredday
pricebyfourhundredday_normalized = close  //create a dummy series 

if time >= bottom4
    pricebyfourhundredday_normalized := normalize(pricebyfourhundredday, pricebyfourhundredday_min_cycle4, pricebyfourhundredday_max_cycle4)

if time >= bottom3 and time < bottom4
    pricebyfourhundredday_normalized := normalize(pricebyfourhundredday, pricebyfourhundredday_min_cycle3, pricebyfourhundredday_max_cycle3)

if time >= bottom2 and time < bottom3
    pricebyfourhundredday_normalized := normalize(pricebyfourhundredday, pricebyfourhundredday_min_cycle2, pricebyfourhundredday_max_cycle2)

if time >= start and time < bottom2
    pricebyfourhundredday_normalized := normalize(pricebyfourhundredday, pricebyfourhundredday_min_cycle1, pricebyfourhundredday_max_cycle1)
    
// plot(pricebyfourhundredday_normalized, color = color.purple, title = "Normalized Price/52W EMA")

//*********************************************** Mayer Multiple ***********************************************//
mayer_multiple_min_cycle1 = 0.26
mayer_multiple_max_cycle1 = 13.26
mayer_multiple_min_cycle2 = 0.24
mayer_multiple_max_cycle2 = 8.25
mayer_multiple_min_cycle3 = 0.42
mayer_multiple_max_cycle3 = 3.74
// mayer_multiple_min_cycle4 = input.float(0.51, "Mayer Multiple Minimum Value", 0, 1000, 0.01, "Input the minimum value displayed by the Mayer Multiple")
// mayer_multiple_max_cycle4 = input.float(2.82, "Mayer Multiple Maximum Value", 0, 1000, 0.01, "Input the maximum value displayed by the Mayer Multiple")
mayer_multiple_min_cycle4 = 0.51
mayer_multiple_max_cycle4 = 2.82

mayer_multiple = close/ta.sma(close, 200)
mayer_multiple_normalized = close  //create a dummy series 

if time >= bottom4
    mayer_multiple_normalized := normalize(mayer_multiple, mayer_multiple_min_cycle4, mayer_multiple_max_cycle4)

if time >= bottom3 and time < bottom4
    mayer_multiple_normalized := normalize(mayer_multiple, mayer_multiple_min_cycle3, mayer_multiple_max_cycle3)

if time >= bottom2 and time < bottom3
    mayer_multiple_normalized := normalize(mayer_multiple, mayer_multiple_min_cycle2, mayer_multiple_max_cycle2)
    
if time >= start and time < bottom2
    mayer_multiple_normalized := normalize(mayer_multiple, mayer_multiple_min_cycle1, mayer_multiple_max_cycle1)
 
// plot(mayer_multiple_normalized, color = color.yellow, title = "Normalized Mayer Multiple")

//*********************************************** Puell Multiple ***********************************************//
puell_multiple_min_cycle1 = 0.45
puell_multiple_max_cycle1 = 4.74
puell_multiple_min_cycle2 = 0.41
puell_multiple_max_cycle2 = 12.35
puell_multiple_min_cycle3 = 0.36
puell_multiple_max_cycle3 = 5.73
// puell_multiple_min_cycle4 = input.float(0.4, "Puell Multiple Value", 0, 1000, 0.01, "Input the minimum value displayed by the Puell Multiple")
// puell_multiple_max_cycle4 = input.float(3.38, "Puell Multiple Value", 0, 1000, 0.01, "Input the maximum value displayed by the Puell Multiple")
puell_multiple_min_cycle4 = 0.4
puell_multiple_max_cycle4 = 3.38
btc_issuance = 7200*close

if time >= halving3
    btc_issuance := 1800*close

if time >= halving2 and time < halving3
    btc_issuance := 3600*close

btc_issuance_ma = ta.sma(btc_issuance, 365)
puell_multiple = btc_issuance/btc_issuance_ma
puell_multiple_normalized = close  //create a dummy series 

if time >= bottom4
    puell_multiple_normalized := normalize(puell_multiple, puell_multiple_min_cycle4, puell_multiple_max_cycle4)

if time >= bottom3 and time < bottom4
    puell_multiple_normalized := normalize(puell_multiple, puell_multiple_min_cycle3, puell_multiple_max_cycle3)

if time >= bottom2 and time < bottom3
    puell_multiple_normalized := normalize(puell_multiple, puell_multiple_min_cycle2, puell_multiple_max_cycle2)
    
if time >= start and time < bottom2
    puell_multiple_normalized := normalize(puell_multiple, puell_multiple_min_cycle1, puell_multiple_max_cycle1)

// plot(puell_multiple_normalized, color = color.blue, title = "Normalized Puell Multiple")

//*********************************************** Sharpe Ratio ***********************************************//
sharpe_ratio_min_cycle1 = -1.29
sharpe_ratio_max_cycle1 = 0.53
sharpe_ratio_min_cycle2 = -6.8
sharpe_ratio_max_cycle2 = 3.32
sharpe_ratio_min_cycle3 = -13.73
sharpe_ratio_max_cycle3 = 3.21
// sharpe_ratio_min_cycle4 = input.float(-10.51, "Sharpe Ratio Minimum Value", -1000, 1000, 0.01, "Input the minimum value displayed by the Sharpe Ratio")
// sharpe_ratio_max_cycle4 = input.float(0.39, "Sharpe Ratio Maximum Value", 0, 1000, 0.01, "Input the maximum value displayed by the Sharpe Ratio")
sharpe_ratio_min_cycle4 = -10.51
sharpe_ratio_max_cycle4 = 0.39


pct_change = ((close - close[1])/close[1])*100
pct_change_mean = ta.ema(pct_change, 200)
pct_change_stdev = ta.stdev(pct_change_mean, 200)
sharpe_ratio = (pct_change_mean-1)/pct_change_stdev
sharpe_ratio_normalized = close  //create a dummy series 

if time >= bottom4
    sharpe_ratio_normalized := normalize(sharpe_ratio, sharpe_ratio_min_cycle4, sharpe_ratio_max_cycle4)

if time >= bottom3 and time < bottom4
    sharpe_ratio_normalized := normalize(sharpe_ratio, sharpe_ratio_min_cycle3, sharpe_ratio_max_cycle3)
    
if time >= bottom2 and time < bottom3
    sharpe_ratio_normalized := normalize(sharpe_ratio, sharpe_ratio_min_cycle2, sharpe_ratio_max_cycle2)
    
if time >= start and time < bottom2
    sharpe_ratio_normalized := normalize(sharpe_ratio, sharpe_ratio_min_cycle1, sharpe_ratio_max_cycle1)

// plot(sharpe_ratio_normalized, color = color.white, title = "Normalozed Sharpe Ratio")

//*********************************************** Power Law Corridor ***********************************************//
// Days X-Axis Value
days = ta.barssince(time <= start)
offset = (start - timestamp("2009-01-03"))/86400000 // days between gensis block and start of data
d = days + offset

// Fair Value 
// a1 = input(-16.9859613,'FV a')
// b1 = input(5.8854167, 'FV b')
a1 = -16.9859613
b1 = 5.8854167
e1 = a1 + b1*math.log10(d)
y1 = math.pow(10,e1)

// Upper Band
// a2 = input(-13.2938766,'UB a')
// b2 = input(5.0123459,'UB b')
a2 = -13.2938766
b2 = 5.0123459
e2 = a2 + b2*math.log10(d)
y2 = math.pow(10,e2)

// Lower Band
// a3 = input(-17.3101322,'LB a')
// b3 = input(5.8621456,'LB b')
a3 = -17.3101322
b3 = 5.8621456
e3 = a3 + b3*math.log10(d)
y3 = math.pow(10,e3)

delta = close - y3
delta_normalized = delta/(y2-y3)
// plot(delta_normalized, color = color.red)

//*********************************************** Overall Risk ***********************************************//
overall_risk = input.float(0, "50D/50W Indicator Weight", 0, 1, 0.01)*fastslow_ratio_normalized +
  input.float(0.1666667, "Price/52W EMA", 0, 1, 0.01)*pricebyfiftytwo_normalized +
  input.float(0.1666667, "Price/400D SMA", 0, 1, 0.01)*pricebyfourhundredday_normalized +
  input.float(0.1666667, "Mayer Multiple Weight", 0, 1, 0.01)*mayer_multiple_normalized + 
  input.float(0.1666667, "Puell Multiple Weight", 0, 1, 0.01)*puell_multiple_normalized + 
  input.float(0.1666667, "Sharpe Ratio Weight", 0, 1, 0.01)*sharpe_ratio_normalized + 
  input.float(0.1666667, "Power Law Weight", 0, 1, 0.01)*delta_normalized 

//*********************************************** Plot ***********************************************//

green0 = color.new(color.green, 61)
green1 = color.new(color.green, 69)
green2 = color.new(color.green, 76)
green3 = color.new(color.green, 83)
green4 = color.new(color.green, 90)

red0 = color.new(color.red, 90)
red1 = color.new(color.red, 83)
red2 = color.new(color.red, 76)
red3 = color.new(color.red, 69)
red4 = color.new(color.red, 61)

transparant = color.rgb(0, 0, 0, 100)

buy0 = hline(0.0, color=transparant)
buy1 = hline(0.1, color=transparant)
buy2 = hline(0.2, color=transparant)
buy3 = hline(0.3, color=transparant)
buy4 = hline(0.4, color=transparant)
buy5 = hline(0.5, color=transparant)

sell0 = hline(0.6, color=transparant)
sell1 = hline(0.7, color=transparant)
sell2 = hline(0.8, color=transparant)
sell3 = hline(0.9, color=transparant)
sell4 = hline(1.0, color=transparant)

fill(buy0, buy1, green0, "DCA 5x")
fill(buy1, buy2, green1, "DCA 4x")
fill(buy2, buy3, green2, "DCA 3x")
fill(buy3, buy4, green3, "DCA 2x")
fill(buy4, buy5, green4, "DCA 1x")

fill(sell0, sell1, red0, "DCA 1y")
fill(sell1, sell2, red1, "DCA 2y")
fill(sell2, sell3, red2, "DCA 3y")
fill(sell3, sell4, red3, "DCA 4y")

plot(overall_risk, color=color.rgb(0, 128, 255), title = "Normalized Risk")





















