bullratio
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © slopip

//@version=5

// @description Calculate the profit/loss ratio of a permabull for configurable time range
library("bullratio")


// @function calculates the profit/loss ratio for a permabull of age len
// @param len the number of candles to include in the running bull ratio - 0 for all time 
// @returns series float of profit/loss percentage 
export bullratio(int len) =>
    var bullspend = 0.0
    var allbuys = 0.0
    
    if(len == 0)
        bullspend := ta.cum(close*volume)
        allbuys := ta.cum(volume)
    else 
        bullspend := math.sum(close*volume, len)
        allbuys := math.sum(volume, len)
    
    bullratio = ((close*allbuys)/bullspend)-1
    bullratio := bullratio > 0?  1-(bullspend/(close*allbuys)): bullratio