ROC vs BTC
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © HayeTrading

//@version=5
indicator("ROC vs BTC")
roclen = input.int(10, "ROC Length")
roc = ta.roc(close, roclen)
btcroc = request.security("BINANCE:BTCUSDT", "", roc)
length = input(500, "Percent Order Lookback")
col = roc > 0 ? color.rgb(50,255,0,transp=20) : color.rgb(255,0,0,transp=20)

// Current ticker, create arrays and pos/neg counts
pos = array.new_float(0)
neg = array.new_float(0)
poscount = 0
negcount = 0

// Create BTC Arrays and pos/neg counts
btcpos = array.new_float(0)
btcneg = array.new_float(0)
btc_poscount = 0
btc_negcount = 0


for i=0 to length - 1
    if roc[i] > 0
        array.push(pos, roc[i])
        poscount += 1
    else if roc[i] < 0
        array.push(neg, roc[i])
        negcount += 1
    if btcroc[i] > 0
        array.push(btcpos, btcroc[i])
        btc_poscount += 1
    else if btcroc[i] < 0
        array.push(btcneg, btcroc[i])
        btc_negcount += 1

// Sort arrays, positive ascending, negative descending
array.sort(pos, order.ascending)
array.sort(neg, order.descending)
array.sort(btcpos, order.ascending)
array.sort(btcneg, order.descending)

// Calculate index of current ROC values within newly ordered arrays
posindex = array.indexof(pos, roc)
negindex = array.indexof(neg, roc)
btc_posindex = array.indexof(btcpos, btcroc)
btc_negindex = array.indexof(btcneg, btcroc)


// Calculate index values as percentage of array length, end values will be -100 to 100.
pospercent = ((posindex + 1) / poscount) * 100
negpercent = ((negindex + 1) / negcount) * 100
btc_pospercent = ((btc_posindex + 1) / btc_poscount) * 100
btc_negpercent = -(((btc_negindex + 1) / btc_negcount) * 100)
btc = btc_pospercent == 0 ? btc_negpercent : btc_pospercent

// Plot values. Negative ROC plotted as negative values.
// The highest ROC move down will be -100. Highest ROC move up will be 100.

plot(pospercent, style=plot.style_columns, color=col)
plot(-negpercent, style=plot.style_columns, color=col)
plot(btc, linewidth=2)
plot(50, style=plot.style_cross)
plot(-50, style=plot.style_cross)