Bitcoin Risk Long Term indicator
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
//@version=5
// © Julien-PH

indicator(title="Bitcoin Risk Long Term indicator", shorttitle="BTC Risk LT", overlay=false)

//---- Commons Variables
bwClose         = request.security("INDEX:BTCUSD", "W", close)
bwHigh          = request.security("INDEX:BTCUSD", "W", high)
bwLow           = request.security("INDEX:BTCUSD", "W", low)

//---- Display
display_input   = input.bool(false, title="Display raw input indicators ?"  , group="Display")
offsetDisplay   = input.int(defval=-100, title="Offset raw input indicators", group="Display")
display_output  = input.bool(true,  title="Display output indicators ?"     , group="Display")


//######## INPUT
//---- BTC log curve
// INPUTS
HighIntercept   = input.float(1.097   , title="Top Curve Intercept"   , step=0.001    , group="Logarithme curve Settings")
HighSlope       = input.float(0.00076 , title="Top Curve Slope"       , step=0.00001  , group="Logarithme curve Settings")
LowIntercept    = input.float(-2.9    , title="Bottom Curve Intercept", step=0.001    , group="Logarithme curve Settings")
LowSlope        = input.float(0.0011  , title="Bottom Curve Slope"    , step=0.00001  , group="Logarithme curve Settings")

// TIME CALCS
TimeIndex       = time < 1279670400000 ? 3.0 : (time - 1279670400000) / 86400000
Weight          = (math.log10(TimeIndex + 10) * TimeIndex * TimeIndex - TimeIndex) / 30000
// TOP OF CHANNEL INTERCEPT & SLOPE CALCS
HighSlopeCum    = HighSlope * TimeIndex
HighLogDev      = TimeIndex > 2 ? math.log(Weight) + HighIntercept + HighSlopeCum : na
// BOTTOM OF CHANNEL INTERCEPT & SLOPE CALCS
LowSlopeCum     = LowSlope * TimeIndex
LowLogDev       = TimeIndex > 2 ? math.log(Weight) + LowIntercept  + LowSlopeCum  : na
// TOTAL CHANNEL LOG RANGE
//LogRange        = HighLogDev - LowLogDev
// Curve
HighDev         = math.pow(2.718281828459, HighLogDev)
LowDev          = math.pow(2.718281828459, LowLogDev)
ChannelRange    = HighDev - LowDev
// Delta price/curves
logCurveHighValue   = math.max(math.min( (((bwHigh - LowDev) / ChannelRange)*100) ,100),0)
logCurveLowValue    = math.max(math.min( (((bwLow  - LowDev) / ChannelRange)*100) ,100),0)
//logCurveHighValue2   = math.max(math.min( (((math.log(bwHigh) - LowDev) / ChannelRange)*100) ,100),0) //TODO tester avec racine de e
//logCurveLowValue2    = math.max(math.min( (((math.log(bwLow)  - LowDev) / ChannelRange)*100) ,100),0)

// Plot
display_LogCurve    = input.bool(true, title="Display logarithme curve indicators ?"                    , group="Logarithme curve Settings", tooltip="Strongly based on 'Bitcoin Logarithmic Growth Curves & Zones' develop by mabonyi but in a oscillator")
//colorScheme         = input.color(color.new(color.white,0) , title="Logarithme curve color"             , group="Logarithme curve Settings")
colorLogCurve       = input.color(color.new(color.white,0) , title="Logarithme curve top&bottom color"  , group="Logarithme curve Settings")
colorFillLogCurve   = input.color(color.new(color.blue ,60), title="Logarithme curve fill color"        , group="Logarithme curve Settings")

//p10000              = plot(HighDev, color=colorScheme, title="Top of log channel")
//p0                  = plot(LowDev , color=colorScheme, title="Bottom of log channel")
plotLogCurveHigh    = plot(display_input ? display_LogCurve ? logCurveHighValue+offsetDisplay : na : na, title="Distance from highest logarithme curve", color=colorLogCurve, linewidth = 1)
plotLogCurveLow     = plot(display_input ? display_LogCurve ? logCurveLowValue +offsetDisplay : na : na, title="Distance from lowest logarithme curve" , color=colorLogCurve, linewidth = 1)
fill(plotLogCurveHigh, plotLogCurveLow, color=colorFillLogCurve)


//---- WaveTrend
display_wt          = input.bool(false, title="Display WaveTrend indicator ?", group="WaveTrend Settings", tooltip="WaveTrend Oscillator develop by LazyBear")
display_wtDelta     = input.bool(false, title="Display WaveTrend delta ?"    , group="WaveTrend Settings", tooltip="Represents the difference between the WaveTrend and its SMA")
length1WT           = input.int(10, minval=1, title="Channel Length"         , group="WaveTrend Settings")
length2WT           = input.int(21, minval=1, title="Average Length"         , group="WaveTrend Settings")
wtMethod(l1, l2) =>
    esa = ta.ema(hlc3, l1)
    ci = (hlc3 - esa) / (0.015 * ta.ema(math.abs(hlc3 - esa), l1))
    wt1 = nz(math.max(math.min( ta.ema(ci, l2)*0.45 + 40 ,100),0),50)
    wt2 = nz(ta.sma(wt1,4),50)
    [nz(wt1,0),nz(wt2,0)]

[wt1,wt2]           = request.security("INDEX:BTCUSD", "W", wtMethod(length1WT,length2WT) )
wtDelta             = math.max(math.min( (wt1 - wt2)*5 + 50 ,100),0)

// Plot
colorWT1            = input.color(color.new(color.aqua,0)  , title="WaveTrend color"        , group="WaveTrend Settings")
colorWT2            = input.color(color.new(color.aqua,50) , title="WaveTrend MA color"     , group="WaveTrend Settings")
colorWTBull         = input.color(color.new(color.red,50)  , title="Bull WaveTrend color"   , group="WaveTrend Settings")
colorWTBear         = input.color(color.new(color.green,50), title="Bear WaveTrend color"   , group="WaveTrend Settings")
pwt1                = plot(display_input ? display_wt ? wt1+offsetDisplay : na : na, title="WaveTrend"    , color=colorWT1, linewidth = 1)
pwt2                = plot(display_input ? display_wt ? wt2+offsetDisplay : na : na, title="WaveTrendMA"  , color=colorWT2, linewidth = 1)
//fill(pwt1,pwt2, wt1<wt2 ? colorWTBull : colorWTBear)
plot(display_input ? display_wtDelta ? wtDelta+offsetDisplay : na : na, title="WaveTrend Delta"    , color=colorWT1, linewidth = 1)


//---- Distance ATH
display_DistanceATH     = input.bool(false, title="Display distance from ATH indicator ?"  , group="Distance ATH Settings")
distance_from_ATH       = nz(100+request.security("GLASSNODE:BTC_ATHDRAWDOWN", "W", close)*100,0)

// Plot
colorDistanceATH        = input.color(color.new(color.navy,0) , title="Distance ATH color", group="Distance ATH Settings")
plot(display_input ? display_DistanceATH ? distance_from_ATH+offsetDisplay : na : na, title="Distance from ATH (%)", color=colorDistanceATH, linewidth = 1)


distanceMA(l,amp,offset) =>
    smaFix = nz(request.security("INDEX:BTCUSD", "W", nz(ta.sma(close, l),LowDev) ),0)
    math.max(math.min( (((bwClose - smaFix) / (amp*smaFix))*100)+offset ,100),0)


//---- Distance short weekly moving average
display_DistanceShortSMA= input.bool(false,title="Display distance from short weekly moving average ?"  , group="Distance short MA weekly Settings")
lengthSSMA              = input.int(  7  , title="Short MA Length", minval=1                            , group="Distance short MA weekly Settings")
amplSSMA                = input.float(1.0, title="Short MA Amplitude"                                   , group="Distance short MA weekly Settings")
offsetSSMA              = input.int(  30 , title="Short MA Offset"                                      , group="Distance short MA weekly Settings")
distance_close_shortsma = distanceMA(lengthSSMA,amplSSMA,offsetSSMA)

// Plot
colorDistanceShortSMA   = input.color(color.new(color.aqua,0), title="Distance short MA weekly color"   , group="Distance short MA weekly Settings")
plot(display_input ? display_DistanceShortSMA ? distance_close_shortsma+offsetDisplay : na : na, title='Distance from short MA Weekly', color=colorDistanceShortSMA, linewidth=1)


//---- Distance long weekly moving average
display_DistanceLongSMA = input.bool(true, title="Display distance from long weekly moving average ?"   , group="Distance long MA weekly Settings")
lengthLSMA              = input.int( 200 , title="Long MA Length", minval=1                             , group="Distance long MA weekly Settings")
amplLSMA                = input.float(6.0, title="Long MA Amplitude"                                    , group="Distance long MA weekly Settings")
offsetLSMA              = input.int(  0  , title="Long MA Offset"                                       , group="Distance long MA weekly Settings")
distance_close_longsma  = distanceMA(lengthLSMA,amplLSMA,offsetLSMA)

// Plot
colorDistanceLongSMA    = input.color(color.new(color.aqua,0), title="Distance long MA weekly color"    , group="Distance long MA weekly Settings")
plot(display_input ? display_DistanceLongSMA ? distance_close_longsma+offsetDisplay : na : na , title='Distance from long MA Weekly'  , color=colorDistanceLongSMA , linewidth=1)


//---- Williams Percent Range
display_wr      = input.bool(false, title="Display Williams Percent Range indicator ?"  , group="Williams %R Settings")
lengthWR        = input.int(14, minval=1, title="Williams %R Length"                    , group="Williams %R Settings")
williamsRangeMethod(length) =>
	(100 * (close - ta.highest(length)) / (ta.highest(length) - ta.lowest(length))) + 100
	
wr              = nz(request.security("INDEX:BTCUSD", "W", williamsRangeMethod(lengthWR) ),0)

// Plot
colorWR         = input.color(color.new(color.purple,0) , title="Williams %R color", group="Williams %R Settings")
plot(display_input ? display_wr ? wr+offsetDisplay : na : na, title="W%R", color=colorWR, linewidth=1)


//---- Momentum adjusted
display_mom     = input.bool(false, title="Display Momentum indicator ?", group="Momentum adjusted Settings")
momLengthInput  = input.int(10, minval=1, title="Mom Length"            , group="Momentum adjusted Settings")
securityMom     = request.security("INDEX:BTCUSD", "W", close[momLengthInput]/close)
mom             = nz(math.max(math.min( ((1 - securityMom) + 2.3)*30 ,100),0),0)

// Plot
colorMom        = input.color(color.rgb(0,0,255,0), title="Mom color"   , group="Momentum adjusted Settings")
plot(display_input ? display_mom ? mom+offsetDisplay : na : na, title="Mom", color=colorMom, linewidth=1)


//---- RSI + Stoch
//RSI
display_rsi     = input.bool(false, title="Display RSI indicator ?" , group="Relative Strength Index Settings")
rsiLengthInput  = input.int(14, minval=1, title="RSI Length"        , group="Relative Strength Index Settings")
rsi             = nz(request.security("INDEX:BTCUSD", "W", ta.rsi(close,rsiLengthInput)),0)

//Stoch
display_stoch   = input.bool(true , title="Display Stochastic indicators ?" , group="Stochastic Settings")
display_kdDelta = input.bool(false, title="Display delta between K and D ?" , group="Stochastic Settings", tooltip="Represents the difference between the stochastic and its SMA")
smoothK         = input.int(3, minval=1, title="K Length"                   , group="Stochastic Settings")
smoothD         = input.int(3, minval=1, title="D Length"                   , group="Stochastic Settings")
k               = nz(request.security("INDEX:BTCUSD", "W", ta.sma(ta.stoch(rsi, rsi, rsi, rsiLengthInput), smoothK)),0)
d               = nz(request.security("INDEX:BTCUSD", "W", ta.sma(k, smoothD)),0)
kdDelta         = nz(math.max(math.min( (k - d) + 50 ,100),0),0)

// Plot
colorRSI            = input.color(color.rgb(255,127,0,0)    , title="RSI color"         , group="Relative Strength Index Settings")
colorStoch          = input.color(color.new(color.orange,0) , title="Stochastic color"  , group="Stochastic Settings")
//colorStochD         = input.color(color.new(color.blue  ,0) , title="Stochastic D color", group="Stochastic Settings")
plot(display_input ?  display_rsi  ? rsi+offsetDisplay       : na : na, title="RSI"       , color=colorRSI    , linewidth=1)
plot(display_input ?  display_stoch  ? k+offsetDisplay       : na : na, title="K"         , color=colorStoch  , linewidth=1)
//plot(display_input ? display_stoch ? d+offsetDisplay       : na : na, title="D"         , color=colorStochD , linewidth=1)
plot(display_input ? display_kdDelta ? kdDelta+offsetDisplay : na : na, title="KDDelta"   , color=colorStoch  , linewidth=1)


//---- Relative Volatility Index
display_rvi     = input.bool(false, title="Display RVI indicator ?"         , group="Relative Volatility Index Settings")
lengthStDevRVI  = input.int(10, minval=1, title="RVI Length price deviation", group="Relative Volatility Index Settings")
lengthEMARVI    = input.int(14, minval=1, title="RVI Length EMA"            , group="Relative Volatility Index Settings")
stddevRVI       = nz(request.security("INDEX:BTCUSD", "W", ta.stdev(close,lengthStDevRVI)),0)
upperRVI        = nz(request.security("INDEX:BTCUSD", "W", ta.ema(ta.change(close) <= 0 ? 0 : stddevRVI, lengthEMARVI)),0)
lowerRVI        = nz(request.security("INDEX:BTCUSD", "W", ta.ema(ta.change(close) >  0 ? 0 : stddevRVI, lengthEMARVI)),0)
rvi             = nz(upperRVI / (upperRVI + lowerRVI) * 100,0)

// Plot
colorRVI        = input.color(color.new(color.purple,0) , title="RVI color", group="Relative Volatility Index Settings")
plot(display_input ? display_rvi ? rvi+offsetDisplay : na : na, title="RVI", color=colorRVI, linewidth=1)


//---- True Strength Indicator
display_tsi     = input.bool(false, title="Display TSI indicator ?"         , group="True Strength Indicator Settings")
longTSI         = input.int(25, minval=1, title="TSI Long Length"           , group="True Strength Indicator Settings")
shortTSI        = input.int(13, minval=1, title="TSI Short Length"          , group="True Strength Indicator Settings")

double_smoothTSI(closeTSI, longTSI, shortTSI) =>
	ta.ema(ta.ema(closeTSI, longTSI), shortTSI)
double_smoothed_pcTSI       = nz(request.security("INDEX:BTCUSD", "W", double_smoothTSI(ta.change(close), longTSI, shortTSI)),0)
double_smoothed_abs_pcTSI   = nz(request.security("INDEX:BTCUSD", "W", double_smoothTSI(math.abs(ta.change(close)), longTSI, shortTSI)),0)
tsi                         = nz(math.max(math.min( 70 * ((double_smoothed_pcTSI / double_smoothed_abs_pcTSI) + 0.4) ,100),0),0)

// Plot
colorTSI        = input.color(color.new(color.yellow,0), title="TSI color"  , group="True Strength Indicator Settings")
plot(display_input ? display_tsi ? tsi+offsetDisplay : na : na, title="TSI", color=colorTSI, linewidth=1)


//---- Bollinger Bands
display_bb  = input.bool(true, title="Display BB ratio indicator ?"                              , group="Bollinger Bands Settings")
lengthBB    = input.int(    20 , minval=1  , step=1  , title="Bollinger Period Length"           , group="Bollinger Bands Settings")
multBB      = input.float(  2.0, minval=0.1, step=0.1, title="Bollinger Bands Standard Deviation", group="Bollinger Bands Settings")
basisBB     = request.security("INDEX:BTCUSD", "W", ta.sma(close, lengthBB))
devBB       = request.security("INDEX:BTCUSD", "W", multBB * ta.stdev(close, lengthBB))
upperBB     = basisBB + devBB
lowerBB     = basisBB - devBB
RangeBB     = upperBB - lowerBB
ratioBB     = nz(math.max(math.min( (((bwClose - lowerBB) / RangeBB)*100) ,100),0),0)

// Plot
colorBB     = input.color(color.new(color.red,0) , title="BB ratio color", group="Bollinger Bands Settings")
plot(display_input ? display_bb ? ratioBB+offsetDisplay : na : na, title="BB", color=colorBB, linewidth=1)


//---- Donchian oscillator
display_dc  = input.bool(false              , title="Display Donchian  indicator ?" , group="Donchian Oscillator Settings")
lengthDC    = input.int(20, minval=1, step=1, title="Donchian Period Length"        , group="Donchian Oscillator Settings")
ratioDC     = nz(request.security("INDEX:BTCUSD", "W", ((close-ta.lowest(lengthDC))/(ta.highest(lengthDC)-ta.lowest(lengthDC)))*100 ),0)

// Plot
colorDC     = input.color(color.new(color.blue,0) , title="DC ratio color", group="Donchian Oscillator Settings")
plot(display_input ? display_dc ? ratioDC+offsetDisplay : na : na, title="DC", color=colorDC, linewidth=1)


//---- Schaff Trend Cycle
display_stc         = input.bool(true,title="Display Schaff Trend Cycle indicator ?", group="Schaff Trend Cycle Settings")
lengthSTC           = input.int(10, minval=1, title="Schaff Trend Cycle length"     , group="Schaff Trend Cycle Settings")
fastLengthSTC       = input.int(23, minval=1, title="Schaff Trend Cycle fast length", group="Schaff Trend Cycle Settings")
slowLengthSTC       = input.int(50, minval=1, title="Schaff Trend Cycle slow length", group="Schaff Trend Cycle Settings")
stcMethod(length, fastLength, slowLength) =>
    m       = ta.ema(close, fastLength) - ta.ema(close, slowLength)
    Kstc    = nz(fixnan(ta.stoch(m, m, m, length)))
    Dstc    = ta.ema(Kstc, 3)
    KDstc   = nz(fixnan(ta.stoch(Dstc, Dstc, Dstc, 3)))
    stc     = ta.ema(KDstc, 3)
    stc     := math.max(math.min(stc, 100), 0)
    
stc         = nz(request.security("INDEX:BTCUSD", "W", stcMethod(lengthSTC,fastLengthSTC,slowLengthSTC)),0)

// Plot
colorSTC    = input.color(color.rgb(128,0,128,0), title="Schaff Trend Cycle color" , group="Schaff Trend Cycle Settings")
plot(display_input ? display_stc ? stc+offsetDisplay : na : na, title="STC", color=colorSTC, linewidth=1)


//---- Aroon
display_aroonUp     = input.bool(true ,title="Display AroonUp indicator ?"      , group="Aroon Settings")
display_aroonDown   = input.bool(false,title="Display AroonDown indicator ?"    , group="Aroon Settings")
lengthAroonUp       = input.int(14, minval=1, title="AroonUp length"            , group="Aroon Settings")
lengthAroonDown     = input.int(14, minval=1, title="AroonDown length"          , group="Aroon Settings")
aroonUp         = nz(request.security("INDEX:BTCUSD", "W",( 100 * (ta.highestbars(high, lengthAroonUp+1)   + lengthAroonUp)  /lengthAroonUp  )       ),0)
aroonDown       = nz(request.security("INDEX:BTCUSD", "W",(-100 * (ta.lowestbars(low  , lengthAroonDown+1) + lengthAroonDown)/lengthAroonDown) + 100 ),0)

// Plot
colorAroonUp    = input.color(color.orange  , title="AroonUp color"     , group="Aroon Settings")
colorAroonDown  = input.color(color.blue    , title="AroonDown color"   , group="Aroon Settings")
plot(display_input ? display_aroonUp    ? aroonUp    +offsetDisplay : na : na, title="AroonUp"   , color=colorAroonUp    , linewidth=1)
plot(display_input ? display_aroonDown  ? aroonDown  +offsetDisplay : na : na, title="AroonDown" , color=colorAroonDown  , linewidth=1)


//---- Ultimate Oscillator
display_uo      = input.bool(false,title="Display Ultimate Oscillator ?", group="Ultimate Oscillator Settings")
length1UO       = input.int(7 , minval=1, title = "Fast UO Length")
length2UO       = input.int(14, minval=1, title = "Middle UO Length")
length3UO       = input.int(28, minval=1, title = "Slow UO Length")
average(bp, tr, length) => math.sum(bp, length) / math.sum(tr, length)
uoMethod(l1,l2,l3) =>
    highUO          = math.max(high, close[1])
    lowUO           = math.min(low, close[1])
    bpUO            = close - highUO
    trUO            = highUO - lowUO
    uoAvg7          = average(bpUO, trUO, l1)
    uoAvg14         = average(bpUO, trUO, l2)
    uoAvg28         = average(bpUO, trUO, l2)
    uo              = 100 * (4*uoAvg7 + 2*uoAvg14 +uoAvg28)/7

uo              = nz(math.max(math.min( request.security("INDEX:BTCUSD", "W", uoMethod(length1UO,length2UO,length3UO)*1.5 + 100) ,100),0),0)

// Plot
colorUO         = input.color(color.red  , title="UO color"     , group="Ultimate Oscillator Settings")
plot(display_input ? display_uo ? uo+offsetDisplay : na : na, title="UO", color=colorUO, linewidth=1)


//---- Money Flow Index
display_mfi = input.bool(false, title="Display Money Flow Index indicator ?", group="Money Flow Index Settings")
lengthMFI   = input.int(14, minval=1, maxval=2000   , title="MFI Length"    , group="Money Flow Index Settings")
mfi         = nz(request.security("INDEX:BTCUSD", "W", ta.mfi(hlc3, lengthMFI)),0)

// Plot
colorMFI    = input.color(color.new(color.green,0)  , title="MFI color"     , group="Money Flow Index Settings")
plot(display_input ? display_mfi ? mfi+offsetDisplay : na : na, title="MFI", color=colorMFI, linewidth=1)


//---- Chaikin Money Flow
display_cmf     = input.bool(false, title="Display Chaikin Money Flow indicator ?"      , group="Chaikin Money Flow Settings")
lengthCMF       = input.int(21, minval=1, maxval=1000, title="Chaikin Money Flow Length", group="Chaikin Money Flow Settings")
mfvCMF          = nz(request.security("INDEX:BTCUSD", "W", math.sum(((high == low) ? 0 : ((close - low) - (high - close)) / (high - low)) * volume, lengthCMF) / math.sum(volume, lengthCMF) ),0)
cmf             = nz(math.max(math.min( (mfvCMF * 150) + 30 ,100),0),0)

// Plot
colorCMF        = input.color(color.new(color.lime,0), title="Chaikin Money Flow color" , group="Chaikin Money Flow Settings")
plot(display_input ? display_cmf ? cmf+offsetDisplay : na : na, title="CMF", color=colorCMF, linewidth=1)


//---- Volume Weighted Moving Average
display_vwma    = input.bool(false, title="Display distance from Volume Weighted Moving Average indicator ?", group="Volume Weighted Moving Average Settings")
lengthVWMA      = input.int(16, minval=1, maxval=1000, title="Volume Weighted Moving Average Length"        , group="Volume Weighted Moving Average Settings")
vwmaReq         = nz(request.security("INDEX:BTCUSD", "W", close/ta.vwma(close, lengthVWMA)),0)
vwma            = nz(math.max(math.min( vwmaReq*50 - 25 ,100),0),0)

// Plot
colorVWMA       = input.color(color.new(color.silver,0), title="Volume Weighted Moving Average color"       , group="Volume Weighted Moving Average Settings")
plot(display_input ? display_vwma ? vwma+offsetDisplay : na : na, title="VWMA", color=colorVWMA, linewidth=1)


//---- Volume Oscillator
display_vo      = input.bool(false, title="Display Volume Oscillator ?" , group="Volume Oscillator Settings")
shortLenVO      = input.int(5 , minval=1, title = "Short Length")
longLenVO       = input.int(10, minval=1, title = "Long Length")
vo              = nz(request.security("INDEX:BTCUSD", "W", math.max(math.min((100 * (ta.ema(volume, shortLenVO) - ta.ema(volume, longLenVO)) / ta.ema(volume, longLenVO)) + 50,100),0) ),0)

// Plot
colorVO         = input.color(color.red, title="Volume Oscillator color", group="Volume Oscillator Settings")
plot(display_input ? display_vo ? vo+offsetDisplay : na : na, title="VO", color=colorVO, linewidth=1)


//---- BTC Supply / BTC Max Supply
display_supply      = input.bool(false, title="Display Bitcoin supply ratio ?"      , group="Supply Settings", tooltip="Represents the ratio between the current and max supply of bitcoin")
supplyBTC           = nz(request.security("GLASSNODE:BTC_SUPPLY", "W", close),0)
supplyRatio         = supplyBTC/210000 //21M divide by 100

//Plot
colorSupply         = input.color(color.new(color.white,0), title="Supply color"    , group="Supply Settings")
plot(display_input ? display_supply ? supplyRatio+offsetDisplay : na : na, title='Supply Ratio' , color=colorSupply , linewidth=1)


//---- Mayer Multiple
display_mayer       = input.bool(false, title="Display Mayer Multiple indicator ?"  , group="Mayer Settings")
psma_length         = input.int(200, minval=1, title="Price SMA Length"             , group="Mayer Settings")
mayerMultiple       = nz(request.security("INDEX:BTCUSD", "D", ((close / ta.sma(close, psma_length))*600)/(100-supplyRatio/1.3) ),0)

//Plot
colorMayer          = input.color(color.new(color.maroon,0), title="Mayer color"    , group="Mayer Settings")
plot(display_input ? display_mayer ? mayerMultiple+offsetDisplay : na : na, title='Mayer Multiple' , color=colorMayer , linewidth=1)


//---- Moon Phases
display_moon= input.bool(false, title="Display Moon phases ?", group="Moon phases Settings")
cycle       = 2551442876.8992
day         = 8.64e+7
moon        = ((timestamp("2021-01-13:05:00") + time + day)%cycle/cycle)*100
moonCycle   = (moon<50 ? moon : 100-moon)*2

//Plot
colorMoon   = input.color(color.new(color.gray,0), title="Moon phases color", group="Moon phases Settings")
plot(display_input ? display_moon ? moonCycle+offsetDisplay : na : na, title='Moon phases' , color=colorMoon , linewidth=1)


//---- Heikin Ashi
display_haDelta = input.bool(false, title="Display Delta between price Heikin Ashi ?", group="Heikin Ashi Indicator Settings", tooltip="Represents the difference between close price and close Heikin Ashi")
display_haTrend = input.bool(false, title="Display Heikin Ashi trend indicator ?"    , group="Heikin Ashi Indicator Settings", tooltip="100 if Heikin Ashi candle is green, 0 if red")
ha_open         = nz(request.security(ticker.heikinashi("INDEX:BTCUSD"), "W", open),0)
ha_close        = nz(request.security(ticker.heikinashi("INDEX:BTCUSD"), "W", close),0)
haDelta         = nz(math.max(math.min( (bwClose / ha_close) * 200 - 150 ,100),0),0)
//haTrend         = nz(math.max(math.min( (ha_close / ha_open) * 100 - 50  ,100),0),0)
haTrend         = nz( ha_close > ha_open ? 100 : 0 ,0)

// Plot
colorHA         = input.color(color.new(color.silver,0), title="Heikin Ashi color"  , group="Heikin Ashi Indicator Settings")
plot(display_input ? display_haDelta ? haDelta+offsetDisplay : na : na, title="HA Delta", color=colorHA, linewidth=1)
plot(display_input ? display_haTrend ? haTrend+offsetDisplay : na : na, title="HA Trend", color=colorHA, linewidth=1)




//######## OUTPUT
// Variable
avgIndicators() =>
    sumIndicators       = (display_LogCurve ? logCurveHighValue : 0) + (display_wt ? wt1 : 0) + (display_DistanceATH ? distance_from_ATH : 0) + (display_DistanceShortSMA ? distance_close_shortsma : 0) + (display_DistanceLongSMA ? distance_close_longsma : 0) + (display_wr ? wr : 0) + (display_mom ? mom : 0) + (display_rsi ? rsi : 0) + (display_stoch ? k : 0) + (display_rvi ? rvi : 0) + (display_tsi ? tsi : 0) + (display_haDelta ? haDelta : 0) + (display_haTrend ? haTrend : 0) + (display_bb ? ratioBB : 0) + (display_dc ? ratioDC : 0) + (display_stc ? stc : 0) + (display_aroonUp ? aroonUp : 0) + (display_aroonDown ? aroonDown : 0) + (display_uo ? uo : 0) + (display_mfi ? mfi : 0) + (display_cmf ? cmf : 0) + (display_vwma ? vwma : 0) + (display_mayer ? mayerMultiple : 0) + (display_moon ? moonCycle : 0)
    nbIndicators        = (display_LogCurve ? 1 : 0) + (display_wt ? 1 : 0) + (display_DistanceATH ? 1 : 0) + (display_DistanceShortSMA ? 1 : 0) + (display_DistanceLongSMA ? 1 : 0) + (display_wr ? 1 : 0) + (display_mom ? 1 : 0) + (display_rsi ? 1 : 0) + (display_stoch ? 1 : 0) + (display_rvi ? 1 : 0) + (display_tsi ? 1 : 0) + (display_haDelta ? 1 : 0) + (display_haTrend ? 1 : 0) + (display_bb ? 1 : 0) + (display_dc ? 1 : 0) + (display_stc ? 1 : 0) + (display_aroonUp ? 1 : 0) + (display_aroonDown ? 1 : 0) + (display_uo ? 1 : 0) + (display_mfi ? 1 : 0) + (display_cmf ? 1 : 0) + (display_vwma ? 1 : 0) + (display_mayer ? 1 : 0) + (display_moon ? 1 : 0)
    sumIndicators / nbIndicators


//---- Risk Indicator
riskRatio           = avgIndicators()

display_Risk        = input.bool(true, title="Display Risk indicator ?"                 , group="Risk Settings", tooltip="It is a synthetic output indicator that simply averages all the input indicators you have selected")
barcolor_Risk       = input.bool(true,title="Display barcolor with Risk indicator ?"    , group="Risk Settings")

levelRiskSafe       = input.int(10, minval=0, maxval=100, title="Level risk safe"       , group="Risk Settings")
colorRiskSafe       = input.color(color.rgb(0,0,255,0)  , title="Risk safe color"       , group="Risk Settings")
levelRiskOk         = input.int(30, minval=0, maxval=100, title="Level risk ok"         , group="Risk Settings")
colorRiskOk         = input.color(color.rgb(0,255,0,0)  , title="Risk ok color"         , group="Risk Settings")
levelRiskWarn       = input.int(50, minval=0, maxval=100, title="Level risk warn"       , group="Risk Settings")
colorRiskWarn       = input.color(color.rgb(255,255,0,0), title="Risk warning color"    , group="Risk Settings")
levelRiskDanger     = input.int(90, minval=0, maxval=100, title="Level risk danger"     , group="Risk Settings")
colorRiskDanger     = input.color(color.rgb(255,0,0,0)  , title="Risk danger color"     , group="Risk Settings")

colorRisk           = riskRatio > levelRiskOk ? riskRatio > levelRiskWarn ? color.from_gradient(riskRatio, levelRiskWarn, levelRiskDanger, colorRiskWarn, colorRiskDanger) : color.from_gradient(riskRatio, levelRiskOk, levelRiskWarn, colorRiskOk, colorRiskWarn) : color.from_gradient(riskRatio, levelRiskSafe, levelRiskOk, colorRiskSafe, colorRiskOk)


// Plot
band1               = hline(display_input   ? offsetDisplay : 0 , color=color.gray, linestyle=hline.style_solid)
band0               = hline(0                                   , color=color.gray, linestyle=hline.style_solid)
bandMinus1          = hline(display_output  ? 100 : 0           , color=color.gray, linestyle=hline.style_solid)
plot(display_output ? display_Risk ? riskRatio  : na : na, title="Risk Ratio", color=colorRisk, linewidth = 2)
barcolor(color=barcolor_Risk ? colorRisk : na)
