Crypto Relative Performance and Profitability
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © mcamps-nl

//@version=4
study("Crypto Relative Performance and Profitability")

var symTotalCryptoCap = "CRYPTOCAP:TOTAL"
var dailyTimeFramePeriod = "D"

totalOpen = security(symTotalCryptoCap, dailyTimeFramePeriod, open)
totalClose = security(symTotalCryptoCap, timeframe.period, close)
totalChangeFraction = (totalClose - totalOpen) / totalOpen

currentOpen = security(syminfo.tickerid, dailyTimeFramePeriod, open)
currentClose = close
currentChangeFraction = (currentClose - currentOpen) / currentOpen

diff = currentChangeFraction - totalChangeFraction

performance = (diff[0] * diff[1]) > 0 or (diff[1] * diff[2]) > 0 ? diff * 100 : 0

plotColor =  currentChangeFraction >= 0 ? color.green : color.red
plot0 = plot(0, color=plotColor)
plotPerformance = plot(performance, color=plotColor)
fill(plot0, plotPerformance, color= color.new(plotColor, 70))