Investing Performance with vs without fees
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Daveatt

// Inspiration: https://www.tutorialspoint.com/calculate-compound-interest-in-javascript

//@version=5
indicator("Investing Performance", overlay = false)

i_start_date            = input.int(1980, title = "Starting Year")
amount_invested         = input.float(5000, title = "Amount invested each year")
annualInterestRate      = input.float(10, title = "Average rate of Appreciation") * 0.01
annualManagerFeeRate    = input.float(2, title = "Fund Manager Fees") * 0.01
nb_compound_interest    = input.int(1, minval = 1, title = "Number of times that interest is compounded per unit", tooltip = "for example if interest is compounded monthly and t is in years then the value of n would be 12. If interest is compounded quarterly and t is in years then the value of n would be 4.")

if timeframe.period != "12M"
    runtime.error("The selected chart timeframe must be 12M")

//  Made by @RicardoSantos
//  Link: https://www.tradingview.com/script/7wlSFTY6-Function-Compound-Interest/
//  Description: Calculate compound interest for given values.
//f_compound_interest(_principle, _rate, _duration)=>
//    _return = _principle * (math.pow(1 + _rate / 100, _duration))

f_compound_interest(_principle, _rate, _duration, n) =>
   _return = _principle * (math.pow((1 + (_rate / n)), (n * _duration)))

var float pnl_without_fees = 0.
var float pnl_with_fees    = 0.

yearsElapsed = year(timenow) - i_start_date

var float totalInvested = amount_invested * yearsElapsed

// Calculate the final value of the investment with manager fees

pnl_without_fees := f_compound_interest(amount_invested, annualInterestRate, yearsElapsed, nb_compound_interest)
pnl_with_fees    := f_compound_interest(amount_invested, (annualInterestRate - annualManagerFeeRate), yearsElapsed, nb_compound_interest)

plot(annualInterestRate - annualManagerFeeRate, title = "test", display = display.data_window)

var table table_pnl = table.new(position.middle_center, 11, 2, bgcolor = color.white, border_color = color.black, border_width = 2, frame_color = color.black, frame_width = 2)

if barstate.islastconfirmedhistory

    pnl_delta                    = math.abs(pnl_with_fees - pnl_without_fees)

    str_yearsElapsed             = str.tostring(yearsElapsed)
    str_annualInterestRate       = str.tostring(annualInterestRate * 100) + "%"
    str_annualManagerFeeRate     = str.tostring(annualManagerFeeRate * 100) + "%"
    str_currentAmountWithFees    = str.tostring(pnl_with_fees, "#") + " " + syminfo.currency
    str_currentAmountWithoutFees = str.tostring(pnl_without_fees, "#") + " " + syminfo.currency
    str_totalInvestedAmount      = str.tostring(totalInvested, "#") + " " + syminfo.currency
    str_totalAmountWithFees      = str.tostring(pnl_with_fees + totalInvested, "#") + " " + syminfo.currency
    str_totalAmountWithoutFees   = str.tostring(pnl_without_fees + totalInvested, "#") + " " + syminfo.currency
    str_pnlDelta                 = str.tostring(pnl_delta, "#") + " " + syminfo.currency

    // Headers
    table.cell(table_pnl, 0, 0, text = "Start Year", text_color = color.black, text_size = size.normal, bgcolor = color.orange)
    table.cell(table_pnl, 1, 0, text = "End Year", text_color = color.black, text_size = size.normal, bgcolor = color.orange)
    table.cell(table_pnl, 2, 0, text = "Elapsed Years", text_color = color.black, text_size = size.normal, bgcolor = color.orange)
    table.cell(table_pnl, 3, 0, text = "Total\nInvested Amount", text_color = color.black, text_size = size.normal, bgcolor = color.orange)
    table.cell(table_pnl, 4, 0, text = "Annual\nAppreciation Rate", text_color = color.black, text_size = size.normal, bgcolor = color.orange)
    table.cell(table_pnl, 5, 0, text = "Annual\nFund Manager Rate", text_color = color.black, text_size = size.normal, bgcolor = color.orange)
    table.cell(table_pnl, 6, 0, text = "Cumulative\nPnl Without Fees", text_color = color.black, text_size = size.normal, bgcolor = color.orange)
    table.cell(table_pnl, 7, 0, text = "Cumulative\nPnl With Fees", text_color = color.black, text_size = size.normal, bgcolor = color.orange)
    table.cell(table_pnl, 8, 0, text = "Principle\n+ Pnl Without Fees", text_color = color.black, text_size = size.normal, bgcolor = color.orange)
    table.cell(table_pnl, 9, 0, text = "Principle\n+ Pnl With Fees", text_color = color.black, text_size = size.normal, bgcolor = color.orange)
    table.cell(table_pnl, 10, 0, text = "Delta", text_color = color.black, text_size = size.normal, bgcolor = color.orange)

    // Data
    table.cell(table_pnl, 0, 1, text = str.tostring(i_start_date), text_color = color.black, text_size = size.normal, bgcolor = color.white)
    table.cell(table_pnl, 1, 1, text = str.tostring(year(timenow)), text_color = color.black, text_size = size.normal, bgcolor = color.white)
    table.cell(table_pnl, 2, 1, text = str_yearsElapsed, text_color = color.black, text_size = size.normal, bgcolor = color.white)
    table.cell(table_pnl, 3, 1, text = str_totalInvestedAmount, text_color = color.black, text_size = size.normal, bgcolor = color.white)
    table.cell(table_pnl, 4, 1, text = str_annualInterestRate, text_color = color.black, text_size = size.normal, bgcolor = color.white)
    table.cell(table_pnl, 5, 1, text = str_annualManagerFeeRate, text_color = color.black, text_size = size.normal, bgcolor = color.white)
    table.cell(table_pnl, 6, 1, text = str_currentAmountWithoutFees, text_color = color.black, text_size = size.normal, bgcolor = color.white)
    table.cell(table_pnl, 7, 1, text = str_currentAmountWithFees, text_color = color.black, text_size = size.normal, bgcolor = color.white)
    table.cell(table_pnl, 8, 1, text = str_totalAmountWithoutFees, text_color = color.black, text_size = size.normal, bgcolor = color.white)
    table.cell(table_pnl, 9, 1, text = str_totalAmountWithFees, text_color = color.black, text_size = size.normal, bgcolor = color.white)
    table.cell(table_pnl, 10, 1, text = str_pnlDelta, text_color = color.black, text_size = size.normal, bgcolor = color.white)