Dominion - Bitcoin Altcoin Dominance [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 ('Dominion v2', 'Dominion', false, precision=3, timeframe='', explicit_plot_zorder=true)

// v2.2


string  D_BTC   =   'Bitcoin'
string  D_ETH   =   'Ethereum'
string  D_USD   =   'Stablecoins'
string  D_ALT   =   'Full Degen'
string  D_DEFI  =   'Total Defi'
string  D_FLIP  =   'Flippening'
string  D_CRNT  =   'Current Symbol'
string  D_CSTM  =   'Custom Input'


//INPUTS

selector    =   input.string    (D_BTC,     'Dominance:        ',       [D_BTC, D_ETH, D_USD, D_ALT, D_DEFI, D_FLIP, D_CRNT, D_CSTM],       inline='01',    group='Source')
filter      =   input.bool      (false,     'Filter',                                                                                       inline='01',    group='Source')
custom      =   input.string    ('',        'Custom (if available)',                                                                        inline='03',    group='Source')

maActive_1  =   input.bool      (false,     'MA 1',                                                                                         inline='11',    group='MA')
maType_1    =   input.string    ('EMA',     'Type',                     ['SMA', 'EMA', 'WMA', 'RMA'],                                       inline='11',    group='MA')
maLength_1  =   input.int       (8,         'Length',                                                       minval=1,                       inline='11',    group='MA')

maActive_2  =   input.bool      (false,     'MA 2',                                                                                         inline='12',    group='MA')
maType_2    =   input.string    ('EMA',     'Type',                     ['SMA', 'EMA', 'WMA', 'RMA'],                                       inline='12',    group='MA')
maLength_2  =   input.int       (21,        'Length',                                                       minval=1,                       inline='12',    group='MA')

maActive_3  =   input.bool      (false,     'MA 3',                                                                                         inline='13',    group='MA')
maType_3    =   input.string    ('EMA',     'Type',                     ['SMA', 'EMA', 'WMA', 'RMA'],                                       inline='13',    group='MA')
maLength_3  =   input.int       (55,        'Length',                                                       minval=1,                       inline='13',    group='MA')

btcColour   =   input.color     (#ffa600,   '',                                                                                             inline='21',    group='Visuals')
ethColour   =   input.color     (#00b7ff,   '',                                                                                             inline='21',    group='Visuals')
usdColour   =   input.color     (#ff0095,   '',                                                                                             inline='21',    group='Visuals')
altColour   =   input.color     (#00ff55,   '',                                                                                             inline='21',    group='Visuals')
defiColour  =   input.color     (#dd8c84,   '',                                                                                             inline='21',    group='Visuals')
flipColour  =   input.color     (#9932cc,   '',                                                                                             inline='21',    group='Visuals')
crntColour  =   input.color     (#808080,   '',                                                                                             inline='21',    group='Visuals')
cstmColour  =   input.color     (#40e0d0,   '',                                                                                             inline='21',    group='Visuals')

maColour_1  =   input.color     (#6cc941,   '',                                                                                             inline='22',    group='Visuals')
maColour_2  =   input.color     (#e9dd37,   '',                                                                                             inline='22',    group='Visuals')
maColour_3  =   input.color     (#b80718,   '',                                                                                             inline='22',    group='Visuals')

domOpac     =   input.int       (100,       'Opacity: Dom',                         minval=0,       maxval=100,         step=5,             inline='25',    group='Visuals')
maOpac      =   input.int       (60,        'MA',                                   minval=0,       maxval=100,         step=5,             inline='25',    group='Visuals')



// SOURCES

currentCrop =   str.endswith        (syminfo.ticker, 'PERP')        ? str.replace (syminfo.ticker, 'PERP', '', 0)       : syminfo.ticker
currentFix  =   str.replace         (currentCrop, syminfo.currency, '.D', 0)

customFix   =   str.endswith        (custom, '.D')                  ? custom            : str.format ('{0}.D', custom)


btcIndex    =   request.security    ('BTC.D',       '',     close,      ignore_invalid_symbol=true)
ethIndex    =   request.security    ('ETH.D',       '',     close,      ignore_invalid_symbol=true)
defiIndex   =   request.security    ('TOTALDEFI.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)
cstmIndex   =   request.security    (customFix,     '',     close,      ignore_invalid_symbol=true)
crntIndex   =   request.security    (currentFix,    '',     close,      ignore_invalid_symbol=true)



// PROCESS

domSwitch   =   switch selector
    D_BTC   =>  btcIndex
    D_ETH   =>  ethIndex
    D_DEFI  =>  defiIndex
    D_USD   =>  usdtIndex   +   usdcIndex
    D_FLIP  =>  btcIndex    -   ethIndex
    D_CRNT  =>  crntIndex
    D_CSTM  =>  cstmIndex
    D_ALT   =>  100 - (btcIndex + ethIndex + usdtIndex + usdcIndex)
//

domType     =   filter      ? ta.rma (domSwitch, 2)     : domSwitch

ma_1        =   switch maType_1
    'SMA'   =>  ta.sma (domType, maLength_1)
    'EMA'   =>  ta.ema (domType, maLength_1)
    'WMA'   =>  ta.wma (domType, maLength_1)
    'RMA'   =>  ta.rma (domType, maLength_1)

ma_2        =   switch maType_2
    'SMA'   =>  ta.sma (domType, maLength_2)
    'EMA'   =>  ta.ema (domType, maLength_2)
    'WMA'   =>  ta.wma (domType, maLength_2)
    'RMA'   =>  ta.rma (domType, maLength_2)

ma_3        =   switch maType_3
    'SMA'   =>  ta.sma (domType, maLength_3)
    'EMA'   =>  ta.ema (domType, maLength_3)
    'WMA'   =>  ta.wma (domType, maLength_3)
    'RMA'   =>  ta.rma (domType, maLength_3)
//


// VISUALS

domColour   =   switch selector
    D_BTC   =>  btcColour
    D_ETH   =>  ethColour
    D_DEFI  =>  defiColour
    D_USD   =>  usdColour
    D_FLIP  =>  flipColour
    D_CRNT  =>  crntColour
    D_CSTM  =>  cstmColour
    D_ALT   =>  altColour
//

maPlot_1    =   plot    (maActive_1     ? ma_1      : na,       'MA 1',         color.new (maColour_1, 100 - maOpac),     1)
maPlot_2    =   plot    (maActive_2     ? ma_2      : na,       'MA 2',         color.new (maColour_2, 100 - maOpac),     1)
maPlot_3    =   plot    (maActive_3     ? ma_3      : na,       'MA 3',         color.new (maColour_3, 100 - maOpac),     1)

domPlot     =   plot    (domType,                               'Dominance',    color.new (domColour, 100 - domOpac),  3)


// ALERTS

maxx_12     =   ta.cross        (ma_1, ma_2)
maxx_13     =   ta.cross        (ma_1, ma_3)

maxo_12     =   ta.crossover    (ma_1, ma_2)
maxo_13     =   ta.crossover    (ma_1, ma_3)
maxu_12     =   ta.crossunder   (ma_1, ma_2)
maxu_13     =   ta.crossunder   (ma_1, ma_3)


alertcondition (maxx_12,    'MA1 cross MA2',        'Dominion: MA1 has crossed MA2')
alertcondition (maxx_13,    'MA1 cross MA3',        'Dominion: MA1 has crossed MA3')

alertcondition (maxo_12,    'MA1 crossover MA2',    'Dominion: MA1 has crossed above MA2')
alertcondition (maxo_13,    'MA1 crossover MA3',    'Dominion: MA1 has crossed above MA3')

alertcondition (maxu_12,    'MA1 crossunder MA2',   'Dominion: MA1 has crossed below MA2')
alertcondition (maxu_13,    'MA1 crossunder MA3',   'Dominion: MA1 has crossed below MA3')







