Crypto-DX Crypto Directional Index [chhslai]
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © chhslai
// Latest update: 3, Oct, 2022 - GMT+8 23:xx


//@version=5
indicator(title="Crypto-DX Crypto Directional Index [chhslai]", shorttitle="Crypto-DX @chhslai")


// --------------
// INPUT SETTINGS
// --------------
plotAverage = input.bool(true, title="Plot crypto index")
plotEach = input.bool(false, title="Plot 5 custom crypto")
plotAll = input.bool(false, title="Plot top 30 crypto")

rocLength = 21 //input(21, title="ROC length")
smoothingLength = 13 //input(13, title="EMA length")
labelArrow = input.string("⬅")
invisible = color.new(color.black, 100)

ticker1 = input.symbol("BINANCE:BTCUSDT", inline="1")
color1 = input.color(color.blue,title="", inline="1")
ticker2 = input.symbol("BINANCE:ETHUSDT", inline="2")
color2 = input.color(color.red, title="", inline="2")
ticker3 = input.symbol("BINANCE:BNBUSDT", inline="3")
color3 = input.color(color.yellow, title="", inline="3")
ticker4 = input.symbol("BINANCE:XRPUSDT", inline="4")
color4 = input.color(color.green, title="", inline="4")
ticker5 = input.symbol("BINANCE:ADAUSDT", inline="5")
color5 = input.color(color.teal, title="", inline="5")

// --------------
// MAIN FUNCTIONS
// --------------
calc_sroc(_ticker) =>
    src = close
    ema = ta.ema(src, smoothingLength)
    sroc = 100 * (ema - ema[rocLength])/ema[rocLength]
    [sroc]

plotEachlabel(_text, _loc, _color)=>
    var label la = na
    var counter = 0
    counter := counter + 1
    label.delete(la)
    la := plotEach ? label.new(
      x=timenow, y=_loc,
      text="                                        " + labelArrow + " " + _text, xloc=xloc.bar_time, yloc=yloc.price, color=invisible,
      style=label.style_none, textcolor=_color, size=size.small, textalign=text.align_left) : na

plotAlllabel(_text, _loc, _color)=>
    var label la = na
    var counter = 0
    counter := counter + 1
    label.delete(la)
    la := plotAll ? label.new(
      x=timenow, y=_loc,
      text="                              " + labelArrow + " " + _text, xloc=xloc.bar_time, yloc=yloc.price, color=invisible,
      style=label.style_none, textcolor=_color, size=size.small, textalign=text.align_left) : na

plotAveragelabel(_text, _loc, _color)=>
    var label la = na
    var counter = 0
    counter := counter + 1
    label.delete(la)
    la := plotAverage ? label.new(
      x=timenow, y=_loc,
      text="                                " + labelArrow + " " + _text, xloc=xloc.bar_time, yloc=yloc.price, color=invisible,
      style=label.style_none, textcolor=_color, size=size.small, textalign=text.align_left) : na

// -----------
// PLOT TOP 30 
// -----------
// Crypto Index is consist of TOP 30 cryptocurrencies Market Cap (2022.08.04)
// BTC, ETH, BNB, XRP, ADA, SOL, DOT, DOGE, MATIC, AVAX
// UNI, SHIB, TRX, ETC, LTC, FTT, CRO, LINK, NEAR
// ATOM, XMR, XLM, BCH, ALGO, APE, VET, FIL, ICP, FLOW, MANA
[BTC] = request.security("BINANCE:BTCUSDT", "", calc_sroc("BINANCE:BTCUSDT"))
[ETH] = request.security("BINANCE:ETHUSDT", "", calc_sroc("BINANCE:ETHUSDT"))
[BNB] = request.security("BINANCE:BNBUSDT", "", calc_sroc("BINANCE:BNBUSDT"))
[XRP] = request.security("BINANCE:XRPUSDT", "", calc_sroc("BINANCE:XRPUSDT"))
[ADA] = request.security("BINANCE:ADAUSDT", "", calc_sroc("BINANCE:ADAUSDT"))
[SOL] = request.security("BINANCE:SOLUSDT", "", calc_sroc("BINANCE:SOLUSDT"))
[DOT] = request.security("BINANCE:DOTUSDT", "", calc_sroc("BINANCE:DOTUSDT"))
[DOGE] = request.security("BINANCE:DOGEUSDT", "", calc_sroc("BINANCE:DOGEUSDT"))
[MATIC] = request.security("BINANCE:MATICUSDT", "", calc_sroc("BINANCE:MATICUSDT"))
[AVAX] = request.security("BINANCE:AVAXUSDT", "", calc_sroc("BINANCE:AVAXUSDT"))
[UNI] = request.security("BINANCE:UNIUSDT", "", calc_sroc("BINANCE:UNIUSDT"))
[SHIB] = request.security("BINANCE:SHIBUSDT", "", calc_sroc("BINANCE:SHIBUSDT"))
[TRX] = request.security("BINANCE:TRXUSDT", "", calc_sroc("BINANCE:TRXUSDT"))
[ETC] = request.security("BINANCE:ETCUSDT", "", calc_sroc("BINANCE:ETCUSDT"))
[LTC] = request.security("BINANCE:LTCUSDT", "", calc_sroc("BINANCE:LTCUSDT"))
[FTT] = request.security("BINANCE:FTTUSDT", "", calc_sroc("BINANCE:FTTUSDT"))
[CRO] = request.security("BYBIT:CROUSDT", "", calc_sroc("BYBIT:CROUSDT"))
[LINK] = request.security("BINANCE:LINKUSDT", "", calc_sroc("BINANCE:LINKUSDT"))
[NEAR] = request.security("BINANCE:NEARUSDT", "", calc_sroc("BINANCE:NEARUSDT"))
[ATOM] = request.security("BINANCE:ATOMUSDT", "", calc_sroc("BINANCE:ATOMUSDT"))
[XMR] = request.security("BINANCE:XMRUSDT", "", calc_sroc("BINANCE:XMRUSDT"))
[XLM] = request.security("BINANCE:XLMUSDT", "", calc_sroc("BINANCE:XLMUSDT"))
[BCH] = request.security("BINANCE:BCHUSDT", "", calc_sroc("BINANCE:BCHUSDT"))
[ALGO] = request.security("BINANCE:ALGOUSDT", "", calc_sroc("BINANCE:ALGOUSDT"))
[APE] = request.security("BINANCE:APEUSDT", "", calc_sroc("BINANCE:APEUSDT"))
[VET] = request.security("BINANCE:VETUSDT", "", calc_sroc("BINANCE:VETUSDT"))
[FIL] = request.security("BINANCE:FILUSDT", "", calc_sroc("BINANCE:FILUSDT"))
[ICP] = request.security("BINANCE:ICPUSDT", "", calc_sroc("BINANCE:ICPUSDT"))
[FLOW] = request.security("BINANCE:FLOWUSDT", "", calc_sroc("BINANCE:FLOWUSDT"))
[MANA] = request.security("BINANCE:MANAUSDT", "", calc_sroc("BINANCE:MANAUSDT"))

plot(plotAll ? BTC : na, title="BTC", linewidth=1, color=color.new(color.white, 65), display=display.pane)
plot(plotAll ? ETH : na, title="ETH", linewidth=1, color=color.new(color.white, 65), display=display.pane)
plot(plotAll ? BNB : na, title="BNB", linewidth=1, color=color.new(color.white, 65), display=display.pane)
plot(plotAll ? XRP : na, title="XRP", linewidth=1, color=color.new(color.white, 65), display=display.pane)
plot(plotAll ? ADA : na, title="ADA", linewidth=1, color=color.new(color.white, 65), display=display.pane)
plot(plotAll ? SOL : na, title="SOL", linewidth=1, color=color.new(color.white, 65), display=display.pane)
plot(plotAll ? DOT : na, title="DOT", linewidth=1, color=color.new(color.white, 65), display=display.pane)
plot(plotAll ? DOGE : na, title="DOGE", linewidth=1, color=color.new(color.white, 65), display=display.pane)
plot(plotAll ? MATIC : na, title="MATIC", linewidth=1, color=color.new(color.white, 65), display=display.pane)
plot(plotAll ? AVAX : na, title="AVAX", linewidth=1, color=color.new(color.white, 65), display=display.pane)
plot(plotAll ? UNI : na, title="UNI", linewidth=1, color=color.new(color.white, 65), display=display.pane)
plot(plotAll ? SHIB : na, title="SHIB", linewidth=1, color=color.new(color.white, 65), display=display.pane)
plot(plotAll ? TRX : na, title="TRX", linewidth=1, color=color.new(color.white, 65), display=display.pane)
plot(plotAll ? ETC : na, title="ETC", linewidth=1, color=color.new(color.white, 65), display=display.pane)
plot(plotAll ? LTC : na, title="LTC", linewidth=1, color=color.new(color.white, 65), display=display.pane)
plot(plotAll ? FTT : na, title="FTT", linewidth=1, color=color.new(color.white, 65), display=display.pane)
plot(plotAll ? CRO : na, title="CRO", linewidth=1, color=color.new(color.white, 65), display=display.pane)
plot(plotAll ? LINK : na, title="LINK", linewidth=1, color=color.new(color.white, 65), display=display.pane)
plot(plotAll ? NEAR : na, title="NEAR", linewidth=1, color=color.new(color.white, 65), display=display.pane)
plot(plotAll ? ATOM : na, title="ATOM", linewidth=1, color=color.new(color.white, 65), display=display.pane)
plot(plotAll ? XMR : na, title="XMR", linewidth=1, color=color.new(color.white, 65), display=display.pane)
plot(plotAll ? XLM : na, title="XLM", linewidth=1, color=color.new(color.white, 65), display=display.pane)
plot(plotAll ? BCH : na, title="BCH", linewidth=1, color=color.new(color.white, 65), display=display.pane)
plot(plotAll ? ALGO : na, title="ALGO", linewidth=1, color=color.new(color.white, 65), display=display.pane)
plot(plotAll ? APE : na, title="APE", linewidth=1, color=color.new(color.white, 65), display=display.pane)
plot(plotAll ? VET : na, title="VET", linewidth=1, color=color.new(color.white, 65), display=display.pane)
plot(plotAll ? FIL : na, title="FIL", linewidth=1, color=color.new(color.white, 65), display=display.pane)
plot(plotAll ? ICP : na, title="ICP", linewidth=1, color=color.new(color.white, 65), display=display.pane)
plot(plotAll ? FLOW : na, title="FLOW", linewidth=1, color=color.new(color.white, 65), display=display.pane)
plot(plotAll ? MANA : na, title="MANA", linewidth=1, color=color.new(color.white, 65), display=display.pane)

plotAlllabel("BTC", BTC, color.new(color.white, 65))
plotAlllabel("ETH", ETH, color.new(color.white, 65))
plotAlllabel("BNB", BNB, color.new(color.white, 65))
plotAlllabel("XRP", XRP, color.new(color.white, 65))
plotAlllabel("ADA", ADA, color.new(color.white, 65))
plotAlllabel("SOL", SOL, color.new(color.white, 65))
plotAlllabel("DOT", DOT, color.new(color.white, 65))
plotAlllabel("DOGE", DOGE, color.new(color.white, 65))
plotAlllabel("MATIC", MATIC, color.new(color.white, 65))
plotAlllabel("AVAX", AVAX, color.new(color.white, 65))
plotAlllabel("UNI", UNI, color.new(color.white, 65))
plotAlllabel("SHIB", SHIB, color.new(color.white, 65))
plotAlllabel("TRX", TRX, color.new(color.white, 65))
plotAlllabel("ETC", ETC, color.new(color.white, 65))
plotAlllabel("LTC", LTC, color.new(color.white, 65))
plotAlllabel("FTT", FTT, color.new(color.white, 65))
plotAlllabel("CRO", CRO, color.new(color.white, 65))
plotAlllabel("LINK", LINK, color.new(color.white, 65))
plotAlllabel("NEAR", NEAR, color.new(color.white, 65))
plotAlllabel("ATOM", ATOM, color.new(color.white, 65))
plotAlllabel("XMR", XMR, color.new(color.white, 65))
plotAlllabel("XLM", XLM, color.new(color.white, 65))
plotAlllabel("BCH", BCH, color.new(color.white, 65))
plotAlllabel("ALGO", ALGO, color.new(color.white, 65))
plotAlllabel("APE", APE, color.new(color.white, 65))
plotAlllabel("VET", VET, color.new(color.white, 65))
plotAlllabel("FIL", FIL, color.new(color.white, 65))
plotAlllabel("ICP", ICP, color.new(color.white, 65))
plotAlllabel("FLOW", FLOW, color.new(color.white, 65))
plotAlllabel("MANA", MANA, color.new(color.white, 65))

// -----------
// PLOT INDEX 
// -----------
asroc = math.avg(BTC, ETH, BNB, XRP, ADA, SOL, DOT, DOGE, MATIC, AVAX,
                 UNI, SHIB, TRX, ETC, LTC, FTT, CRO, LINK, NEAR,
                 ATOM, XMR, XLM, BCH, ALGO, APE, VET, FIL, ICP, FLOW, MANA)
plot(plotAverage ? asroc : na, title="Crypto Index Line", linewidth=3, color=color.white, display=display.pane)
plotAveragelabel("Crypto Index", asroc, color.white)
hline(0, title="Zero Level", linestyle=hline.style_dotted, color=#989898)

// -------------
// PLOT 5 CUSTOM 
// -------------
[sroc1] = request.security(ticker1, "", calc_sroc(ticker1))
plot(plotEach ? sroc1 : na, title="ticker1", linewidth=1, color=color1, display=display.pane)
plotEachlabel(str.tostring(ticker1), sroc1, color1)

[sroc2] = request.security(ticker2, "", calc_sroc(ticker2))
plot(plotEach ? sroc2 : na, title="ticker2", linewidth=1, color=color2, display=display.pane)
plotEachlabel(str.tostring(ticker2), sroc2, color2)

[sroc3] = request.security(ticker3, "", calc_sroc(ticker3))
plot(plotEach ? sroc3 : na, title="ticker3", linewidth=1, color=color3, display=display.pane)
plotEachlabel(str.tostring(ticker3), sroc3, color3)

[sroc4] = request.security(ticker4, "", calc_sroc(ticker4))
plot(plotEach ? sroc4 : na, title="ticker4", linewidth=1, color=color4, display=display.pane)
plotEachlabel(str.tostring(ticker4), sroc4, color4)

[sroc5] = request.security(ticker5, "", calc_sroc(ticker5))
plot(plotEach ? sroc5 : na, title="ticker5", linewidth=1, color=color5, display=display.pane)
plotEachlabel(str.tostring(ticker5), sroc5, color5)
