Risk Indicator
// 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=false, precision=3)

//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
PlotRawRatio    = input(title="Plot Raw SMA Ratio", type=input.bool, defval=false, group="Plots")
PlotRegression  = input(title="Plot Regression Lines", type=input.bool, defval=false, group="Plots")
PlotRiskLevel   = input(title="Plot Normalized Risk", type=input.bool, defval=true, group="Plots")
PlotBuySell     = input(title="Plot Buy Sell Limits", type=input.bool, defval=true, group="Plots")

buyLimit        = input(0.3, title="Buy Limit", group="Buy & Sell Limits")
sellLimit       = input(0.7, title="Sell Limit", group="Buy & Sell Limits")

//input customized SMA settings
dailySMAlength  = input(title="Daily SMA Length", defval=10, 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")

//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)

smaRatio = shortSMA/longSMA                  //Simple Relation
//smaRatio = (shortSMA-longSMA)/longSMA   //Deviation from longSMA in percent

//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)

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

//plotting
RawRiskLevel = plot(PlotRawRatio ? smaRatio : na, title="RiskLevel", color=color.white, linewidth=2)
UpperReg = plot(PlotRegression ? reg1 : na,title="Upper Regression Line", color=color.purple, linewidth=2)
LowerReg = plot(PlotRegression ? reg2 : na,title="Lower Regression Line", color=color.purple, linewidth=2)
RiskLevel = plot(PlotRiskLevel ? Risk : na, title="Normalized Risk Level", color=color.white, linewidth=2)
MaxLevel = plot(PlotRiskLevel ? 1 : na, title="Normalized Risk Level", color=color.gray, linewidth=1)
MinLevel = plot(PlotRiskLevel ? 0 : na, title="Normalized Risk Level", color=color.gray, linewidth=1)

sellLevel = plot(PlotBuySell ? sellLimit : na, title="Sell Limit", color=color.new(color.red,50))
buyLevel  = plot(PlotBuySell ? buyLimit : na, title="Sell Limit", color=color.new(color.green,50))
//fill
sellColor = Risk > sellLimit? color.new(color.red,50) : na
buyColor = Risk < buyLimit? color.new(color.green,50) : na
neutralColor = color.new(color.gray, 60)

fill(sellLevel, RiskLevel, color=sellColor)
fill(buyLevel, RiskLevel, color=buyColor)
fill(MinLevel,MaxLevel, color=color.new(color.gray,90))
