Degen Dominator - (Crypto Dominance Tool) - [mutantdog]
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © mutantdog

//@version=5
indicator ('Degen Dominator', 'Degen Dominator', false, format.volume, 3, timeframe='')


// v1.1

string  D01     =   'EMA Centre'
string  D02     =   'SMA Centre'
string  D03     =   'Midrange Centre'
string  D04     =   'Median Centre'

string  D51     =   'Simple Delta'
string  D52     =   'Weighted Delta'
string  D53     =   'Running Delta'
string  D61     =   'RSI'
string  D62     =   'CMO'

string  D71     =   'Hard Floor'
string  D72     =   'Soft Floor'



//INPUTS

oscMode     =   input.string    (D01,           'Oscillator Mode',      [D01, D02, D03, D04, D51, D52, D53, D61, D62, D71, D72],    inline='11')
oscLength   =   input.int       (34,            'Length',                       minval=2,                                           inline='11')
postFilter  =   input.bool      (true,          'Post Filter')

btcColour   =   input.color     (#ffa600,       '',                                                                                 inline='51',    group='Main')
btcOpacP    =   input.int       (60,            '',                             minval=0,       maxval=100,     step=5,             inline='51',    group='Main')
btcOpacF    =   input.int       (25,            '',                             minval=0,       maxval=100,     step=5,             inline='51',    group='Main')
btcActive   =   input.bool      (true,          'Bitcoin',                                                                          inline='51',    group='Main')

ethColour   =   input.color     (#00b7ff,       '',                                                                                 inline='52',    group='Main')
ethOpacP    =   input.int       (45,            '',                             minval=0,       maxval=100,     step=5,             inline='52',    group='Main')
ethOpacF    =   input.int       (15,            '',                             minval=0,       maxval=100,     step=5,             inline='52',    group='Main')
ethActive   =   input.bool      (true,          'Ethereum',                                                                         inline='52',    group='Main')

altColour   =   input.color     (#00ff55,       '',                                                                                 inline='53',    group='Main')
altOpacP    =   input.int       (60,            '',                             minval=0,       maxval=100,     step=5,             inline='53',    group='Main')
altOpacF    =   input.int       (25,            '',                             minval=0,       maxval=100,     step=5,             inline='53',    group='Main')
altActive   =   input.bool      (true,          'Altcoins',                                                                         inline='53',    group='Main')

usdColour   =   input.color     (#ff0095,       '',                                                                                 inline='54',    group='Main')
usdOpacP    =   input.int       (45,            '',                             minval=0,       maxval=100,     step=5,             inline='54',    group='Main')
usdOpacF    =   input.int       (15,            '',                             minval=0,       maxval=100,     step=5,             inline='54',    group='Main')
usdActive   =   input.bool      (true,          'Stablecoins',                                                                      inline='54',    group='Main')

defiColour  =   input.color     (#dd8c84,       '',                                                                                 inline='55',    group='Extra')
defiOpacP   =   input.int       (30,            '',                             minval=0,       maxval=100,     step=5,             inline='55',    group='Extra')
defiOpacF   =   input.int       (10,            '',                             minval=0,       maxval=100,     step=5,             inline='55',    group='Extra')
defiActive  =   input.bool      (false,         'DeFi',                                                                             inline='55',    group='Extra')

crntColour  =   input.color     (#808080,       '',                                                                                 inline='56',    group='Extra')
crntOpacP   =   input.int       (30,            '',                             minval=0,       maxval=100,     step=5,             inline='56',    group='Extra')
crntOpacF   =   input.int       (10,            '',                             minval=0,       maxval=100,     step=5,             inline='56',    group='Extra')
crntActive  =   input.bool      (false,         'Current Symbol',                                                                   inline='56',    group='Extra')

cstmColour  =   input.color     (#40E0D0,       '',                                                                                 inline='57',    group='Extra')
cstmOpacP   =   input.int       (30,            '',                             minval=0,       maxval=100,     step=5,             inline='57',    group='Extra')
cstmOpacF   =   input.int       (10,            '',                             minval=0,       maxval=100,     step=5,             inline='57',    group='Extra')
cstmActive  =   input.bool      (false,         '',                                                                                 inline='57',    group='Extra')
cstmInput   =   input.string    ('',            '',                                                                                 inline='57',    group='Extra')



// SOURCES

customFix   =   str.endswith        (cstmInput, '.D')               ?   cstmInput                                       :   str.format ('{0}.D', cstmInput)
currentCrop =   str.endswith        (syminfo.ticker, 'PERP')        ?   str.replace (syminfo.ticker, 'PERP', '', 0)     :   
                 str.endswith       (syminfo.ticker, '.P')          ?   str.replace (syminfo.ticker, '.P', '', 0)       :   syminfo.ticker
currentFix  =   str.replace         (currentCrop, syminfo.currency, '.D', 0)

btcIndex    =   request.security    ('BTC.D',       '',     close,      ignore_invalid_symbol=true)
ethIndex    =   request.security    ('ETH.D',       '',     close,      ignore_invalid_symbol=true)
usdtIndex   =   request.security    ('USDT.D',      '',     close,      ignore_invalid_symbol=true)
usdcIndex   =   request.security    ('USDC.D',      '',     close,      ignore_invalid_symbol=true)
defiIndex   =   request.security    ('TOTALDEFI.D', '',     close,      ignore_invalid_symbol=true)
cstmIndex   =   request.security    (customFix,     '',     close,      ignore_invalid_symbol=true)
crntIndex   =   request.security    (currentFix,    '',     close,      ignore_invalid_symbol=true)

altIndex    =   100         -   (btcIndex + ethIndex + usdtIndex + usdcIndex)
usdIndex    =   usdtIndex   +   usdcIndex


// PROCESS

mode_selector (src, len, sel, filt) =>
    upDelta     =   math.max (ta.change (src), 0)
    dnDelta     =   math.min (ta.change (src), 0)

    selector    =   switch sel
        D01     =>  src     -   ta.ema (src, len)
        D02     =>  src     -   ta.sma (src, len)
        D03     =>  src     -   ta.rma (ta.lowest (src, len) + (ta.range (src, len) / 2), 3)
        D04     =>  src     -   ta.median (src, len)

        D51     =>  ta.change (src, len)
        D52     =>  len     *   ta.wma (upDelta + dnDelta, len)
        D53     =>  len     *   ta.rma (upDelta + dnDelta, len)

        D71     =>  src     -   ta.lowest (src, len)
        D72     =>  src     -   ta.rma (ta.lowest (src, len), 3)

        D61     =>  ta.rma (upDelta + dnDelta, len)     /   ta.rma (upDelta - dnDelta, len)
        D62     =>  math.sum (upDelta + dnDelta, len)   /   math.sum (upDelta - dnDelta, len)

    filt        ?   ta.rma (selector, 3)    :   selector
//



btcDom      =   btcActive   ?   mode_selector (btcIndex,  oscLength,  oscMode,    postFilter)   :   na
ethDom      =   ethActive   ?   mode_selector (ethIndex,  oscLength,  oscMode,    postFilter)   :   na
altDom      =   altActive   ?   mode_selector (altIndex,  oscLength,  oscMode,    postFilter)   :   na
usdDom      =   usdActive   ?   mode_selector (usdIndex,  oscLength,  oscMode,    postFilter)   :   na
defiDom     =   defiActive  ?   mode_selector (defiIndex, oscLength,  oscMode,    postFilter)   :   na
crntDom     =   crntActive  ?   mode_selector (crntIndex, oscLength,  oscMode,    postFilter)   :   na
cstmDom     =   cstmActive  ?   mode_selector (cstmIndex, oscLength,  oscMode,    postFilter)   :   na



// PLOTS

zeroPlot    =   plot (0,                                                                                                editable=false,     display=display.none)

btcPlot     =   plot (btcDom,       'Bitcoin',      color.new (btcColour,   100 - btcOpacP),    1)
ethPlot     =   plot (ethDom,       'Ethereum',     color.new (ethColour,   100 - ethOpacP),    1)
altPlot     =   plot (altDom,       'Altcoins',     color.new (altColour,   100 - altOpacP),    1)
usdPlot     =   plot (usdDom,       'Stablecoins',  color.new (usdColour,   100 - usdOpacP),    1)
defiPlot    =   plot (defiDom,      'Defi',         color.new (defiColour,  100 - defiOpacP),   1)
crntPlot    =   plot (crntDom,      'Current',      color.new (crntColour,  100 - crntOpacP),   1)
cstmPlot    =   plot (cstmDom,      'Custom',       color.new (cstmColour,  100 - cstmOpacP),   1)

btcFill     =   plot (btcDom,       'Bitcoin',      color.new (btcColour,   100 - btcOpacF),    0,  plot.style_area,                        display=display.pane)
ethFill     =   plot (ethDom,       'Ethereum',     color.new (ethColour,   100 - ethOpacF),    0,  plot.style_area,                        display=display.pane)
altFill     =   plot (altDom,       'Altcoins',     color.new (altColour,   100 - altOpacF),    0,  plot.style_area,                        display=display.pane)
usdFill     =   plot (usdDom,       'Stablecoins',  color.new (usdColour,   100 - usdOpacF),    0,  plot.style_area,                        display=display.pane)
defiFill    =   plot (defiDom,      'Defi',         color.new (defiColour,  100 - defiOpacF),   0,  plot.style_area,                        display=display.pane)
crntFill    =   plot (crntDom,      'Current',      color.new (crntColour,  100 - crntOpacF),   0,  plot.style_area,                        display=display.pane)
cstmFill    =   plot (cstmDom,      'Custom',       color.new (cstmColour,  100 - cstmOpacF),   0,  plot.style_area,                        display=display.pane)


btcWindow   =   plot (btcIndex,     '*Bitcoin',     btcColour,                                                          editable=false,     display=display.data_window)
ethWindow   =   plot (ethIndex,     '*Ethereum',    ethColour,                                                          editable=false,     display=display.data_window)
altWindow   =   plot (altIndex,     '*Altcoins',    altColour,                                                          editable=false,     display=display.data_window)
usdWindow   =   plot (usdIndex,     '*Stablecoins', usdColour,                                                          editable=false,     display=display.data_window)
defiWindow  =   plot (defiIndex,    '*DeFi',        defiColour,                                                         editable=false,     display=display.data_window)
crntWindow  =   plot (crntIndex,    '*Current',     crntColour,                                                         editable=false,     display=display.data_window)
cstmWindow  =   plot (cstmIndex,    '*Custom',      cstmColour,                                                         editable=false,     display=display.data_window)



// ALERTS

alertcondition  (ta.crossover   (btcDom, 0),    '01: Bitcoin Above 0',      'Degen Alert:   Bitcoin Dominator has crossed above 0')
alertcondition  (ta.crossunder  (btcDom, 0),    '01: Bitcoin Below 0',      'Degen Alert:   Bitcoin Dominator has crossed below 0')

alertcondition  (ta.crossover   (ethDom, 0),    '02: Ethereum Above 0',     'Degen Alert:   Ethereum Dominator has crossed above 0')
alertcondition  (ta.crossunder  (ethDom, 0),    '02: Ethereum Below 0',     'Degen Alert:   Ethereum Dominator has crossed below 0')

alertcondition  (ta.crossover   (altDom, 0),    '03: Altcoins Above 0',     'Degen Alert:   Altcoin Dominator has crossed above 0')
alertcondition  (ta.crossunder  (altDom, 0),    '03: Altcoins Below 0',     'Degen Alert:   Altcoin Dominator has crossed below 0')

alertcondition  (ta.crossover   (usdDom, 0),    '04: Stablecoins Above 0',  'Degen Alert:   Stablecoin Dominator has crossed above 0')
alertcondition  (ta.crossunder  (usdDom, 0),    '04: Stablecoins Below 0',  'Degen Alert:   Stablecoin Dominator has crossed below 0')

alertcondition  (ta.crossover   (defiDom, 0),   '05: Defi Above 0',         'Degen Alert:   Defi Dominator has crossed above 0')
alertcondition  (ta.crossunder  (defiDom, 0),   '05: Defi Below 0',         'Degen Alert:   Defi Dominator has crossed below 0')

alertcondition  (ta.crossover   (crntDom, 0),   '06: Current Above 0',      'Degen Alert:   Current Coin Dominator has crossed above 0')
alertcondition  (ta.crossunder  (crntDom, 0),   '06: Current Below 0',      'Degen Alert:   Current Coin Dominator has crossed below 0')

alertcondition  (ta.crossover   (cstmDom, 0),   '07: Custom Above 0',       'Degen Alert:   Custom Coin Dominator has crossed above 0')
alertcondition  (ta.crossunder  (cstmDom, 0),   '07: Custom Below 0',       'Degen Alert:   Custom Coin Dominator has crossed below 0')


