Risk Indicator Overlay
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Trollplogen

//@version=4
study("Risk Indicator Overlay", overlay=true, precision=2)

//input
asset           = input(title="Asset", type=input.string, defval="BTC", options=["BTC", "ETH", "ADA", "XRP", "LINK", "VET", "FTM", "RUNE", "HBAR", "Total Crypto Mcap", "Other"], group="Asset and SMAs")
SMAset          = input(title="Selected SMA pair", type=input.string, defval="1D/20Wsma", options=["1D/20Wsma","10Dsma/50Wsma","50Dsma/50Wsma","Customized"], group="Asset and SMAs")

//plot input
ColorScheme     = input(title="Color Scheme", type=input.string, defval="Rainbow", options=["Rainbow", "Red/Blue", "Customized"], group = "Plots and Colors")
PlotRiskBands   = input(title="Plot Risk Bands (Theoretical)", type=input.bool, defval=false, group = "Plots and Colors")
PlotTheoreticalHighLow = input(title="Plot Theoretical High/Lows (According to 20W and 50W SMAs)", type=input.bool, defval=false, group = "Plots and Colors")
PlotLine        = input(title="Plot Price With Risk", type=input.bool, defval=true, group = "Plots and Colors")
PlotCircles     = input(title="Plot Circles (closing price)", type=input.bool, defval=false, group = "Plots and Colors")
PlotCandles     = input(title="Plot Candles", type=input.bool, defval=false, group = "Plots and Colors")
PlotSMAs        = input(title="Plot SMAs", type=input.bool, defval=false, group = "Plots and Colors")

//input customized SMA settings
dailySMAlength  = input(title="Daily SMA Length", defval=1, group = "Customized SMA Settings")
weeklySMAlength   = input(title="Weekly SMA Length", type=input.string, defval="20W SMA", options=["20W SMA", "50W SMA"], group = "Customized SMA Settings")

//input customized Log regression lines
UpperAcustom = input(20.0, title="Logarithmic Upper Regression Start", group="Custom Regression settings")
UpperBcustom = input(-2.0, title="Logarithmic Upper Regression Factor", group="Custom Regression settings")
LowerAcustom = input(0.3, title="Logarithmic Lower Regression Start", group="Custom Regression settings")
LowerBcustom = input(0.0, title="Logarithmic Lower Regression Factor", group="Custom Regression settings")
PlotRawRegr  = input(title="Plot Raw SMA Ratio and Regression Lines", type=input.bool, defval=false, group="Custom Regression settings")

HigherDeviationCustomRegA = input(20.0, title="Highest Ratio as Log Regression A+B*ln(x), A =", group="Settings for High/Low Theoretical Price")
HigherDeviationCustomRegB = input(-2.0, title="Highest Ratio as Log Regression A+B*ln(x), B =", group="Settings for High/Low Theoretical Price")
LowerDeviationCustom = input(0.2, title="Lowest Ratio between Daily Price and 20W/50W SMA", group="Settings for High/Low Theoretical Price")

//input customized color settings
CustomRisk1Color       = input(title="Risk 0.0 - 0.1", type=input.color, defval=color.new(#1b5e20,0), group = "Customized Risk Colors")
CustomRisk2Color       = input(title="Risk 0.1 - 0.2", type=input.color, defval=color.new(#388e3c,0), group = "Customized Risk Colors")
CustomRisk3Color       = input(title="Risk 0.2 - 0.3", type=input.color, defval=color.new(#66bb6a,0), group = "Customized Risk Colors")
CustomRisk4Color       = input(title="Risk 0.3 - 0.4", type=input.color, defval=color.new(#81c784,0), group = "Customized Risk Colors")
CustomRisk5Color       = input(title="Risk 0.4 - 0.5", type=input.color, defval=color.new(#a5d6a7,0), group = "Customized Risk Colors")
CustomRisk6Color       = input(title="Risk 0.5 - 0.6", type=input.color, defval=color.new(#faa1a4,0), group = "Customized Risk Colors")
CustomRisk7Color       = input(title="Risk 0.6 - 0.7", type=input.color, defval=color.new(#f77c80,0), group = "Customized Risk Colors")
CustomRisk8Color       = input(title="Risk 0.7 - 0.8", type=input.color, defval=color.new(#f7525f,0), group = "Customized Risk Colors")
CustomRisk9Color       = input(title="Risk 0.8 - 0.9", type=input.color, defval=color.new(#b22833,0), group = "Customized Risk Colors")
CustomUpperRiskColor   = input(title="Risk 0.9 - 1.0", type=input.color, defval=color.new(#801922,0), group = "Customized Risk Colors")

//Define colors
Risk1Color     = ColorScheme == "Rainbow" ? #311b92 : ColorScheme == "Red/Blue" ? #0c3299 : CustomRisk1Color   
Risk2Color     = ColorScheme == "Rainbow" ? #1848cc : ColorScheme == "Red/Blue" ? #1848cc : CustomRisk2Color   
Risk3Color     = ColorScheme == "Rainbow" ? #056656 : ColorScheme == "Red/Blue" ? #3179f5 : CustomRisk3Color   
Risk4Color     = ColorScheme == "Rainbow" ? #388e3c : ColorScheme == "Red/Blue" ? #5b9cf6 : CustomRisk4Color   
Risk5Color     = ColorScheme == "Rainbow" ? #81c784 : ColorScheme == "Red/Blue" ? #90bff9 : CustomRisk5Color   
Risk6Color     = ColorScheme == "Rainbow" ? #fff59d : ColorScheme == "Red/Blue" ? #faa1a4 : CustomRisk6Color   
Risk7Color     = ColorScheme == "Rainbow" ? #fbc02d : ColorScheme == "Red/Blue" ? #f77c80 : CustomRisk7Color   
Risk8Color     = ColorScheme == "Rainbow" ? #f57c00 : ColorScheme == "Red/Blue" ? #f7525f : CustomRisk8Color   
Risk9Color     = ColorScheme == "Rainbow" ? #f7525f : ColorScheme == "Red/Blue" ? #b22833 : CustomRisk9Color   
UpperRiskColor = ColorScheme == "Rainbow" ? #b22833 : ColorScheme == "Red/Blue" ? #801922 : CustomUpperRiskColor   

Risk1ColorTransp = color.new(Risk1Color,80)
Risk2ColorTransp = color.new(Risk2Color,80)
Risk3ColorTransp = color.new(Risk3Color,80)
Risk4ColorTransp = color.new(Risk4Color,80)
Risk5ColorTransp = color.new(Risk5Color,80)
Risk6ColorTransp = color.new(Risk6Color,80)
Risk7ColorTransp = color.new(Risk7Color,80)
Risk8ColorTransp = color.new(Risk8Color,80)
Risk9ColorTransp = color.new(Risk9Color,80)
UpperRiskColorTransp = color.new(UpperRiskColor,80)

//get values
sma1 = (SMAset == "1D/20Wsma") ? sma(close,1) : (SMAset == "10Dsma/50Wsma") ? sma(close,10) : (SMAset == "50Dsma/50Wsma") ? sma(close,50): (SMAset == "Customized") ? sma(close,dailySMAlength) :na
sma2 = (SMAset == "1D/20Wsma") ? sma(close,20) : (SMAset == "10Dsma/50Wsma" or SMAset == "50Dsma/50Wsma") ? sma(close,50) : (SMAset == "Customized") and (weeklySMAlength == "20W SMA") ? sma(close,20) : (SMAset == "Customized") and (weeklySMAlength == "50W SMA") ? sma(close,50) : na

shortSMA = security(syminfo.tickerid, "1D", sma1)
longSMA = security(syminfo.tickerid, "1W", sma2)

sma20w = security(syminfo.tickerid, "1W", sma(close,20))
sma50w = security(syminfo.tickerid, "1W", sma(close,50))

smaRatio = shortSMA/longSMA

//Set Logarithmic regression settings
UpperA = 0.0
UpperB = 0.0
LowerA = 0.0
LowerB = 0.0

if (asset == "BTC")
    UpperA := (SMAset == "1D/20Wsma") ? 28.412 : (SMAset == "10Dsma/50Wsma") ? 33.803 : (SMAset == "50Dsma/50Wsma") ? 12.528 : (SMAset == "Customized") ? UpperAcustom : na
    UpperB := (SMAset == "1D/20Wsma") ? -3.151 : (SMAset == "10Dsma/50Wsma") ? -3.64 : (SMAset == "50Dsma/50Wsma") ? -1.177 : (SMAset == "Customized") ? UpperBcustom : na
    LowerA := (SMAset == "1D/20Wsma") ? -0.74 : (SMAset == "10Dsma/50Wsma") ? 0.42 : (SMAset == "50Dsma/50Wsma") ? 0.51 : (SMAset == "Customized") ? LowerAcustom : na         //korrigert fra -0.739 0.425 0.45 0.52
    LowerB := (SMAset == "1D/20Wsma") ? 0.156 : (SMAset == "10Dsma/50Wsma") ? 0 : (SMAset == "50Dsma/50Wsma") ? 0 : (SMAset == "Customized") ? LowerBcustom : na
else if (asset == "ETH")
    UpperA := (SMAset == "1D/20Wsma") ? 19.9 : (SMAset == "10Dsma/50Wsma") ? 30.7 : (SMAset == "50Dsma/50Wsma") ? 10.2 : (SMAset == "Customized") ? UpperAcustom : na
    UpperB := (SMAset == "1D/20Wsma") ? -2.2 : (SMAset == "10Dsma/50Wsma") ? -3.3 : (SMAset == "50Dsma/50Wsma") ? -0.8 : (SMAset == "Customized") ? UpperBcustom : na
    LowerA := (SMAset == "1D/20Wsma") ? 0.35 : (SMAset == "10Dsma/50Wsma") ? 0.17 : (SMAset == "50Dsma/50Wsma") ? 0.25 : (SMAset == "Customized") ? LowerAcustom : na
    LowerB := (SMAset == "1D/20Wsma") ? 0 : (SMAset == "10Dsma/50Wsma") ? 0 : (SMAset == "50Dsma/50Wsma") ? 0 : (SMAset == "Customized") ? LowerBcustom : na
else if (asset == "Total Crypto Mcap")
    UpperA := (SMAset == "1D/20Wsma") ? 9.0 : (SMAset == "10Dsma/50Wsma") ? 13.4 : (SMAset == "50Dsma/50Wsma") ? 10.2 : (SMAset == "Customized") ? UpperAcustom : na
    UpperB := (SMAset == "1D/20Wsma") ? -0.8 : (SMAset == "10Dsma/50Wsma") ? -1.1 : (SMAset == "50Dsma/50Wsma") ? -0.9 : (SMAset == "Customized") ? UpperBcustom : na
    LowerA := (SMAset == "1D/20Wsma") ? 0.47 : (SMAset == "10Dsma/50Wsma") ? 0.33 : (SMAset == "50Dsma/50Wsma") ? 0.45 : (SMAset == "Customized") ? LowerAcustom : na
    LowerB := (SMAset == "1D/20Wsma") ? 0 : (SMAset == "10Dsma/50Wsma") ? 0 : (SMAset == "50Dsma/50Wsma") ? 0 : (SMAset == "Customized") ? LowerBcustom : na
else if (asset == "ADA")
    UpperA := (SMAset == "1D/20Wsma") ? 21.7 : (SMAset == "10Dsma/50Wsma") ? 26.0 : (SMAset == "50Dsma/50Wsma") ? 11.0 : (SMAset == "Customized") ? UpperAcustom : na
    UpperB := (SMAset == "1D/20Wsma") ? -2.4 : (SMAset == "10Dsma/50Wsma") ? -2.7 : (SMAset == "50Dsma/50Wsma") ? -0.9 : (SMAset == "Customized") ? UpperBcustom : na
    LowerA := (SMAset == "1D/20Wsma") ? 0.32 : (SMAset == "10Dsma/50Wsma") ? 0.12 : (SMAset == "50Dsma/50Wsma") ? 0.21 : (SMAset == "Customized") ? LowerAcustom : na
    LowerB := (SMAset == "1D/20Wsma") ? 0 : (SMAset == "10Dsma/50Wsma") ? 0 : (SMAset == "50Dsma/50Wsma") ? 0 : (SMAset == "Customized") ? LowerBcustom : na
else if (asset == "XRP")
    UpperA := (SMAset == "1D/20Wsma") ? 28.8 : (SMAset == "10Dsma/50Wsma") ? 41.7 : (SMAset == "50Dsma/50Wsma") ? 12.9 : (SMAset == "Customized") ? UpperAcustom : na
    UpperB := (SMAset == "1D/20Wsma") ? -2.9 : (SMAset == "10Dsma/50Wsma") ? -4.3 : (SMAset == "50Dsma/50Wsma") ? -1.1 : (SMAset == "Customized") ? UpperBcustom : na
    LowerA := (SMAset == "1D/20Wsma") ? -0.45 : (SMAset == "10Dsma/50Wsma") ? -0.1 : (SMAset == "50Dsma/50Wsma") ? -0.35 : (SMAset == "Customized") ? LowerAcustom : na
    LowerB := (SMAset == "1D/20Wsma") ? 0.121 : (SMAset == "10Dsma/50Wsma") ? 0.05 : (SMAset == "50Dsma/50Wsma") ? 0.1 : (SMAset == "Customized") ? LowerBcustom : na
else if (asset == "LINK")
    UpperA := (SMAset == "1D/20Wsma") ? 17.5 : (SMAset == "10Dsma/50Wsma") ? 22.4 : (SMAset == "50Dsma/50Wsma") ? 7.5 : (SMAset == "Customized") ? UpperAcustom : na
    UpperB := (SMAset == "1D/20Wsma") ? -1.9 : (SMAset == "10Dsma/50Wsma") ? -2.5 : (SMAset == "50Dsma/50Wsma") ? -0.6 : (SMAset == "Customized") ? UpperBcustom : na
    LowerA := (SMAset == "1D/20Wsma") ? 0.1 : (SMAset == "10Dsma/50Wsma") ? 0.45 : (SMAset == "50Dsma/50Wsma") ? 0.45 : (SMAset == "Customized") ? LowerAcustom : na
    LowerB := (SMAset == "1D/20Wsma") ? 0.05 : (SMAset == "10Dsma/50Wsma") ? 0 : (SMAset == "50Dsma/50Wsma") ? 0 : (SMAset == "Customized") ? LowerBcustom : na
else if (asset == "VET")
    UpperA := (SMAset == "1D/20Wsma") ? 22.5 : (SMAset == "10Dsma/50Wsma") ? 34.0 : (SMAset == "50Dsma/50Wsma") ? 11.8 : (SMAset == "Customized") ? UpperAcustom : na
    UpperB := (SMAset == "1D/20Wsma") ? -2.5 : (SMAset == "10Dsma/50Wsma") ? -3.9 : (SMAset == "50Dsma/50Wsma") ? -1.1 : (SMAset == "Customized") ? UpperBcustom : na
    LowerA := (SMAset == "1D/20Wsma") ? 0.27 : (SMAset == "10Dsma/50Wsma") ? 0.3 : (SMAset == "50Dsma/50Wsma") ? 0.35 : (SMAset == "Customized") ? LowerAcustom : na
    LowerB := (SMAset == "1D/20Wsma") ? 0 : (SMAset == "10Dsma/50Wsma") ? 0 : (SMAset == "50Dsma/50Wsma") ? 0 : (SMAset == "Customized") ? LowerBcustom : na
else if (asset == "FTM")
    UpperA := (SMAset == "1D/20Wsma") ? 19.3 : (SMAset == "10Dsma/50Wsma") ? 23.3 : (SMAset == "50Dsma/50Wsma") ? 13.3 : (SMAset == "Customized") ? UpperAcustom : na
    UpperB := (SMAset == "1D/20Wsma") ? -1.9 : (SMAset == "10Dsma/50Wsma") ? -2.5 : (SMAset == "50Dsma/50Wsma") ? -1.1 : (SMAset == "Customized") ? UpperBcustom : na
    LowerA := (SMAset == "1D/20Wsma") ? -1.44 : (SMAset == "10Dsma/50Wsma") ? -0.45 : (SMAset == "50Dsma/50Wsma") ? -0.45 : (SMAset == "Customized") ? LowerAcustom : na
    LowerB := (SMAset == "1D/20Wsma") ? 0.266 : (SMAset == "10Dsma/50Wsma") ? 0.1 : (SMAset == "50Dsma/50Wsma") ? 0.1 : (SMAset == "Customized") ? LowerBcustom : na
else if (asset == "RUNE")
    UpperA := (SMAset == "1D/20Wsma") ? 10.5 : (SMAset == "10Dsma/50Wsma") ? 21.9 : (SMAset == "50Dsma/50Wsma") ? 11.6 : (SMAset == "Customized") ? UpperAcustom : na
    UpperB := (SMAset == "1D/20Wsma") ? -1.0 : (SMAset == "10Dsma/50Wsma") ? -2.3 : (SMAset == "50Dsma/50Wsma") ? -1.1 : (SMAset == "Customized") ? UpperBcustom : na
    LowerA := (SMAset == "1D/20Wsma") ? 0.35 : (SMAset == "10Dsma/50Wsma") ? 0.3 : (SMAset == "50Dsma/50Wsma") ? 0.4 : (SMAset == "Customized") ? LowerAcustom : na
    LowerB := (SMAset == "1D/20Wsma") ? 0 : (SMAset == "10Dsma/50Wsma") ? 0 : (SMAset == "50Dsma/50Wsma") ? 0 : (SMAset == "Customized") ? LowerBcustom : na
else if (asset == "HBAR")
    UpperA := (SMAset == "1D/20Wsma") ? 16.4 : (SMAset == "10Dsma/50Wsma") ? 23.8 : (SMAset == "50Dsma/50Wsma") ? 6.9 : (SMAset == "Customized") ? UpperAcustom : na
    UpperB := (SMAset == "1D/20Wsma") ? -1.8 : (SMAset == "10Dsma/50Wsma") ? -2.8 : (SMAset == "50Dsma/50Wsma") ? -0.5 : (SMAset == "Customized") ? UpperBcustom : na
    LowerA := (SMAset == "1D/20Wsma") ? 0.45 : (SMAset == "10Dsma/50Wsma") ? 0.45 : (SMAset == "50Dsma/50Wsma") ? 0.6 : (SMAset == "Customized") ? LowerAcustom : na
    LowerB := (SMAset == "1D/20Wsma") ? 0 : (SMAset == "10Dsma/50Wsma") ? 0 : (SMAset == "50Dsma/50Wsma") ? 0 : (SMAset == "Customized") ? LowerBcustom : na
else if (asset == "Other")
    UpperA := UpperAcustom 
    UpperB := UpperBcustom
    LowerA := LowerAcustom
    LowerB := LowerBcustom
    

//Set time intervalls 
t=time
genesisBTC=timestamp(2010,7,17,0,0,0)   //BTC Index chart
genesisETH=timestamp(2015,8,7,0,0,0)    //Ethereum chart
genesisMcap=timestamp(2014,3,1,0,0,0)   //total crypto marketcap chart
genesisADA=timestamp(2017,11,30,0,0,0)  //ADA chart
genesisVET=timestamp(2018,7,5,0,0,0)    //VET chart
genesisXRP=timestamp(2013,8,4,0,0,0)    //XRP chart
genesisLink=timestamp(2017,9,28,0,0,0)  //LINK chart
genesisRune=timestamp(2019,7,23,0,0,0)  //RUNE chart
genesisFTM=timestamp(2018,10,30,0,0,0)  //FTM chart
genesisHbar=timestamp(2019,9,17,0,0,0)  //Hbar chart
//y=(t-genesis)/604800000

//Alternativt plot av Logaritmisk funksjon, men fungerer kun på én tidsoppløsning av gangen
y = asset == "BTC"? (t-genesisBTC)/86400000 : asset == "ETH"? (t-genesisETH)/86400000 : asset == "Total Crypto Mcap"? (t-genesisMcap)/86400000 : asset == "Total Crypto Mcap"? (t-genesisADA)/86400000 : asset == "Total Crypto Mcap"? (t-genesisVET)/86400000 : asset == "Total Crypto Mcap"? (t-genesisXRP)/86400000 : asset == "Total Crypto Mcap"? (t-genesisLink)/86400000 : asset == "Total Crypto Mcap"? (t-genesisRune)/86400000 : asset == "Total Crypto Mcap"? (t-genesisFTM)/86400000 : asset == "Total Crypto Mcap"? (t-genesisHbar)/86400000 : bar_index

reg1 = UpperA + UpperB*log(y)
reg2 = LowerA + LowerB*log(y)

//Plot Regression in overlay plot
RawRiskLevel = plot(PlotRawRegr ? smaRatio : na, title="SMA Ratio", color=color.white, linewidth=2)
UpperReg = plot(PlotRawRegr ? reg1 : na,title="Upper Regression Line", color=color.purple, linewidth=2)
LowerReg = plot(PlotRawRegr ? reg2 : na,title="Lower Regression Line", color=color.purple, linewidth=2)


LowerDeviation20W = 0.1
LowerDeviation50W = 0.1
sma20wLowestprice = 0.0
sma50wLowestprice = 0.0
LowerBorder = 0.0
HigherDeviation20W = 10.0
HigherDeviation50W = 10.0
sma20wHighestprice = 0.0
sma50wHighestprice = 0.0
HigherBorder = 0.0
//Mål på hvor lavt prisen teoretisk kan gå ifht SMA. Brukes til å sette laveste risikobånd
if (asset == "BTC")
    LowerDeviation20W := -0.74+0.156*log(y)         //korrigert: skal egentlig være -0.739
    LowerDeviation50W := 0.355                       //korrigert: skal egentlig være 0.37
    sma20wLowestprice := sma20w*LowerDeviation20W
    sma50wLowestprice := sma50w*LowerDeviation50W
    LowerBorder := max(sma20wLowestprice,sma50wLowestprice)
    
    HigherDeviation20W := 28.412-3.151*log(y)         //korrigert: skal egentlig være -0.739
    HigherDeviation50W := 46.625-5.156*log(y)                       //korrigert: skal egentlig være 0.37
    sma20wHighestprice := sma20w*HigherDeviation20W
    sma50wHighestprice := sma50w*HigherDeviation50W
    HigherBorder := min(sma20wHighestprice,sma50wHighestprice)
else if (asset == "ETH")
    LowerDeviation20W := 0.35         
    LowerDeviation50W := 0.16                       
    sma20wLowestprice := sma20w*LowerDeviation20W
    sma50wLowestprice := sma50w*LowerDeviation50W
    LowerBorder := max(sma20wLowestprice,sma50wLowestprice)
    
    HigherDeviation20W := 19.9-2.2*log(y)         
    HigherDeviation50W := 37.625-4.1*log(y)                      
    sma20wHighestprice := sma20w*HigherDeviation20W
    sma50wHighestprice := sma50w*HigherDeviation50W
    HigherBorder := min(sma20wHighestprice,sma50wHighestprice)
else if (asset == "Total Crypto Mcap")
    LowerDeviation20W := 0.47        
    LowerDeviation50W := 0.31                       
    sma20wLowestprice := sma20w*LowerDeviation20W
    sma50wLowestprice := sma50w*LowerDeviation50W
    LowerBorder := max(sma20wLowestprice,sma50wLowestprice)
    
    HigherDeviation20W := 9-0.8*log(y)         
    HigherDeviation50W := 25.8-2.7*log(y)                      
    sma20wHighestprice := sma20w*HigherDeviation20W
    sma50wHighestprice := sma50w*HigherDeviation50W
    HigherBorder := min(sma20wHighestprice,sma50wHighestprice)
else if (asset == "ADA")
    LowerDeviation20W := 0.32         
    LowerDeviation50W := 0.12                       
    sma20wLowestprice := sma20w*LowerDeviation20W
    sma50wLowestprice := sma50w*LowerDeviation50W
    LowerBorder := max(sma20wLowestprice,sma50wLowestprice)
    
    HigherDeviation20W := 21.7-2.4*log(y)         
    HigherDeviation50W := 35.8-3.9*log(y)                      
    sma20wHighestprice := sma20w*HigherDeviation20W
    sma50wHighestprice := sma50w*HigherDeviation50W
    HigherBorder := min(sma20wHighestprice,sma50wHighestprice)
else if (asset == "XRP")
    LowerDeviation20W := -0.45+0.121*log(y)         
    LowerDeviation50W := 0.15+0.03*log(y)                       
    sma20wLowestprice := sma20w*LowerDeviation20W
    sma50wLowestprice := sma50w*LowerDeviation50W
    LowerBorder := max(sma20wLowestprice,sma50wLowestprice)
    
    HigherDeviation20W := 62-7*log(y)         
    HigherDeviation50W := 45.8-4.5*log(y)                       
    sma20wHighestprice := sma20w*HigherDeviation20W
    sma50wHighestprice := sma50w*HigherDeviation50W
    HigherBorder := min(sma20wHighestprice,sma50wHighestprice)
else if (asset == "LINK")
    LowerDeviation20W := 0.1+0.05*log(y)         
    LowerDeviation50W := 0.2                       
    sma20wLowestprice := sma20w*LowerDeviation20W
    sma50wLowestprice := sma50w*LowerDeviation50W
    LowerBorder := max(sma20wLowestprice,sma50wLowestprice)
    
    HigherDeviation20W := 17.5-1.9*log(y)         
    HigherDeviation50W := 28.9-3.3*log(y)                      
    sma20wHighestprice := sma20w*HigherDeviation20W
    sma50wHighestprice := sma50w*HigherDeviation50W
    HigherBorder := min(sma20wHighestprice,sma50wHighestprice)
else if (asset == "VET")
    LowerDeviation20W := 0.27         
    LowerDeviation50W := 0.2
    sma20wLowestprice := sma20w*LowerDeviation20W
    sma50wLowestprice := sma50w*LowerDeviation50W
    LowerBorder := max(sma20wLowestprice,sma50wLowestprice)
    
    HigherDeviation20W := 22.5-2.5*log(y)         
    HigherDeviation50W := 37.4-4.1*log(y)                      
    sma20wHighestprice := sma20w*HigherDeviation20W
    sma50wHighestprice := sma50w*HigherDeviation50W
    HigherBorder := min(sma20wHighestprice,sma50wHighestprice)
else if (asset == "FTM")
    LowerDeviation20W := -1.44+0.266*log(y)          
    LowerDeviation50W := 0.16                       
    sma20wLowestprice := sma20w*LowerDeviation20W
    sma50wLowestprice := sma50w*LowerDeviation50W
    LowerBorder := max(sma20wLowestprice,sma50wLowestprice)
    
    HigherDeviation20W := 19.3-1.9*log(y)         
    HigherDeviation50W := 33.6-3.8*log(y)                      
    sma20wHighestprice := sma20w*HigherDeviation20W
    sma50wHighestprice := sma50w*HigherDeviation50W
    HigherBorder := min(sma20wHighestprice,sma50wHighestprice)
else if (asset == "RUNE")
    LowerDeviation20W := 0.35         
    LowerDeviation50W := 0.2                       
    sma20wLowestprice := sma20w*LowerDeviation20W
    sma50wLowestprice := sma50w*LowerDeviation50W
    LowerBorder := max(sma20wLowestprice,sma50wLowestprice)
    
    HigherDeviation20W := 10.5-1*log(y)         
    HigherDeviation50W := 20.6-1.9*log(y)                      
    sma20wHighestprice := sma20w*HigherDeviation20W
    sma50wHighestprice := sma50w*HigherDeviation50W
    HigherBorder := min(sma20wHighestprice,sma50wHighestprice)
else if (asset == "HBAR")
    LowerDeviation20W := 0.35         
    LowerDeviation50W := 0.2                       
    sma20wLowestprice := sma20w*LowerDeviation20W
    sma50wLowestprice := sma50w*LowerDeviation50W
    LowerBorder := max(sma20wLowestprice,sma50wLowestprice)
    
    HigherDeviation20W := 16.4-1.8*log(y)         
    HigherDeviation50W := 30.6-3.7*log(y)                      
    sma20wHighestprice := sma20w*HigherDeviation20W
    sma50wHighestprice := sma50w*HigherDeviation50W
    HigherBorder := min(sma20wHighestprice,sma50wHighestprice)

else if (asset == "Other" and weeklySMAlength == "20W SMA")
    LowerDeviation20W := LowerDeviationCustom
    HigherDeviation20W := HigherDeviationCustomRegA + HigherDeviationCustomRegB *log(y)
    sma20wLowestprice := sma20w*LowerDeviation20W
    sma20wHighestprice := sma20w*HigherDeviation20W
    LowerBorder := sma20wLowestprice
    HigherBorder := sma20wHighestprice

else if (asset == "Other" and weeklySMAlength == "50W SMA")
    LowerDeviation50W := LowerDeviationCustom
    HigherDeviation50W := HigherDeviationCustomRegA + HigherDeviationCustomRegB *log(y)
    sma50wLowestprice := sma50w*LowerDeviation50W
    sma50wHighestprice := sma50w*HigherDeviation50W
    LowerBorder := sma50wLowestprice
    HigherBorder := sma50wHighestprice

//Calculate Risk
Risk = (smaRatio - reg2) / (reg1 - reg2)

RiskColor = Risk < 0.1 ? Risk1Color : Risk < 0.2 ? Risk2Color : Risk < 0.3 ? Risk3Color : Risk < 0.4 ? Risk4Color : Risk < 0.5 ? Risk5Color : Risk < 0.6 ? Risk6Color : Risk < 0.7 ? Risk7Color : Risk < 0.8 ? Risk8Color : Risk < 0.9 ? Risk9Color : Risk < 2 ? UpperRiskColor : na

//Define Risk Bands

MaximumRisk = HigherBorder
Riskband9   = (Risk < 0.9) ? close + (HigherBorder-close)*(0.9-Risk)/(1-Risk) : (Risk > 0.9) ? close - (close-LowerBorder)*(Risk-0.9)/Risk : close
Riskband8   = (Risk < 0.8) ? close + (HigherBorder-close)*(0.8-Risk)/(1-Risk) : (Risk > 0.8) ? close - (close-LowerBorder)*(Risk-0.8)/Risk : close
Riskband7   = (Risk < 0.7) ? close + (HigherBorder-close)*(0.7-Risk)/(1-Risk) : (Risk > 0.7) ? close - (close-LowerBorder)*(Risk-0.7)/Risk : close
Riskband6   = (Risk < 0.6) ? close + (HigherBorder-close)*(0.6-Risk)/(1-Risk) : (Risk > 0.6) ? close - (close-LowerBorder)*(Risk-0.6)/Risk : close
Riskband5   = (Risk < 0.5) ? close + (HigherBorder-close)*(0.5-Risk)/(1-Risk) : (Risk > 0.5) ? close - (close-LowerBorder)*(Risk-0.5)/Risk : close
Riskband4   = (Risk < 0.4) ? close + (HigherBorder-close)*(0.4-Risk)/(1-Risk) : (Risk > 0.4) ? close - (close-LowerBorder)*(Risk-0.4)/Risk : close
Riskband3   = (Risk < 0.3) ? close + (HigherBorder-close)*(0.3-Risk)/(1-Risk) : (Risk > 0.3) ? close - (close-LowerBorder)*(Risk-0.3)/Risk : close
Riskband2   = (Risk < 0.2) ? close + (HigherBorder-close)*(0.2-Risk)/(1-Risk) : (Risk > 0.2) ? close - (close-LowerBorder)*(Risk-0.2)/Risk : close
Riskband1   = (Risk < 0.1) ? close + (HigherBorder-close)*(0.1-Risk)/(1-Risk) : (Risk > 0.1) ? close - (close-LowerBorder)*(Risk-0.1)/Risk : close
MinimumRisk = LowerBorder



RLmax = plot(PlotRiskBands ? MaximumRisk : na, color=UpperRiskColorTransp)
RL9 = plot(PlotRiskBands ? Riskband9 : na, color=Risk9ColorTransp)
RL8 = plot(PlotRiskBands ? Riskband8 : na, color=Risk8ColorTransp)
RL7 = plot(PlotRiskBands ? Riskband7 : na, color=Risk7ColorTransp)
RL6 = plot(PlotRiskBands ? Riskband6 : na, color=Risk6ColorTransp)
RL5 = plot(PlotRiskBands ? Riskband5 : na, color=Risk5ColorTransp)
RL4 = plot(PlotRiskBands ? Riskband4 : na, color=Risk4ColorTransp)
RL3 = plot(PlotRiskBands ? Riskband3 : na, color=Risk3ColorTransp)
RL2 = plot(PlotRiskBands ? Riskband2 : na, color=Risk2ColorTransp)
RL1 = plot(PlotRiskBands ? Riskband1 : na, color=Risk1ColorTransp)
RLmin = plot(PlotRiskBands ? MinimumRisk : na, color=Risk1ColorTransp)


fill(RLmax, RL9, color=UpperRiskColorTransp)
fill(RL9, RL8, color=Risk9ColorTransp)
fill(RL8, RL7, color=Risk8ColorTransp)
fill(RL7, RL6, color=Risk7ColorTransp)
fill(RL6, RL5, color=Risk6ColorTransp)
fill(RL5, RL4, color=Risk5ColorTransp)
fill(RL4, RL3, color=Risk4ColorTransp)
fill(RL3, RL2, color=Risk3ColorTransp)
fill(RL2, RL1, color=Risk2ColorTransp)
fill(RL1, RLmin, color=Risk1ColorTransp)

data = close
plot(PlotLine ? data : na, color=RiskColor, linewidth=4)
plotshape(PlotCircles ? data : na, style=shape.circle, location=location.absolute, size="tiny", color=RiskColor)
plotcandle(PlotCandles ? open : na, high, low, close, color=RiskColor)

plot(PlotSMAs? shortSMA : na, color=color.orange, linewidth=1)
plot(PlotSMAs? longSMA : na, color=color.maroon, linewidth=2)

plot(PlotTheoreticalHighLow? sma50wLowestprice : na, color=color.purple, linewidth=3)
plot(PlotTheoreticalHighLow? sma20wLowestprice : na, color=color.teal, linewidth=3)
plot(PlotTheoreticalHighLow? sma50wHighestprice : na, color=color.purple, linewidth=3)
plot(PlotTheoreticalHighLow? sma20wHighestprice : na, color=color.teal, linewidth=3)
