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

//@version=5

// @description Method for compound interest.
library("FunctionCompoundInterest")

import RicardoSantos/DebugConsole/2 as console
[__T, __C] = console.init(20)

// @function Computes compound interest for given duration.
// @param principal float, the principal or starting value.
// @param rate float, the rate of interest.
// @param duration float, the period of growth.
// @returns float.
export simple_compound (float principal, float rate, float duration) => //{
    principal * math.pow(1.0 + rate / 100.0, duration)
// usage:
console.queue_one(__C, str.format('Compound interest for the principal (${0}) with interest rate ({1}%) for a duration ({2} periods) = total ${3}', 10000.0, 3.6, 12.0, simple_compound(10000.0, 3.6, 12.0)))
//{ remarks:
//}}}

// @function Computes variable compound interest for given duration.
// @param principal float, the principal or starting value.
// @param rates float array, the rates of interest.
// @param duration int, the period of growth.
// @returns float array.
export variable_compound (float principal, float[] rates, int duration) => //{
    int _size_r = array.size(rates)
    float _accretion = principal
    for _i = 1 to duration
        _accretion := simple_compound(_accretion, array.get(rates, (_i - 1) % _size_r), 1)
    _accretion
// usage:
float[] rates = array.from(3.2, 4.1, 7.8)
console.queue_one(__C, str.format('Compound interest for the principal (${0}) with variable interest rates ({1}%) for a duration ({2} periods) = total ${3}', 10000.0, str.tostring(rates, '#.##'), 12, variable_compound(10000.0, rates, 12)))
//{ remarks:
//}}}

// @function Computes variable compound interest for given duration.
// @param principal float, the principal or starting value.
// @param rates float array, the rates of interest.
// @param duration int, the period of growth.
// @returns float array.
export simple_compound_array (float principal, float rate, int duration) => //{
    float[] _accretion = array.new_float(duration + 1, principal)
    for _i = 1 to duration
        array.set(_accretion, _i, simple_compound(array.get(_accretion, _i - 1), rate, 1))
    _accretion
// usage:
console.queue_one(__C, str.format('Compound interest for the principal (${0}) with interest rate ({1}%) for a duration ({2} periods)\n\t\t\t\t\t\t\t\t = total ${3}', 10000.0, 3.6, 12, str.tostring(simple_compound_array(10000.0, 3.6, 12), '#.##')))
//{ remarks:
//}}}

// @function Computes variable compound interest for given duration.
// @param principal float, the principal or starting value.
// @param rates float array, the rates of interest.
// @param duration int, the period of growth.
// @returns float array.
export variable_compound_array (float principal, float[] rates, int duration) => //{
    int _size_r = array.size(rates)
    float[] _accretion = array.new_float(duration + 1, principal)
    
    for _i = 1 to duration
        array.set(_accretion, _i, simple_compound(array.get(_accretion, _i - 1), array.get(rates, (_i - 1) % _size_r), 1))
    _accretion
// usage:
console.queue_one(__C, str.format('Compound interest for the principal (${0}) with variable interest rates ({1}%) for a duration ({2} periods)\n\t\t\t\t\t\t\t\t = total ${3}', 10000.0, str.tostring(rates, '#.##'), 12, str.tostring(variable_compound_array(10000.0, rates, 12), '#.##')))
//{ remarks:
//}}}

// @function compound interest periods
// @param principal float, value of investment.
// @param final float, target final value.
// @param rate float, interest rate.
export compound_interest_periods (float principal, float final, float rate) => //{
    math.log(final / principal) / math.log(1.0 + rate)
// usage:
console.queue_one(__C, str.format('Compound interest to achieve final (${1}) from principal (${0}) with interest rates ({2}%) will require a duration: \n\t\t\t\t\t\t\t\t = {3} periods', 10000.0, 20000, str.tostring(8.0, '#.##'), str.tostring(compound_interest_periods(10000.0, 20000.0, 0.08), '#.##')))
//{ remarks:
//}}}

// @function compound interest principal
// @param final float, target final value.
// @param rate float, interest rate.
// @param period float, period of investment.
export compound_interest_principal (float final, float rate, float period) => //{
    final / math.pow(1 + rate, period)
// usage:
console.queue_one(__C, str.format('Principal with final (${0}) with interest rates ({1}%) and duration of {2} periods: \n\t\t\t\t\t\t\t\t = ${3}', 20000, str.tostring(8.0, '#.##'), 9, str.tostring(compound_interest_principal(20000.0, 0.08, 9.0), '#.##')))
//{ remarks:
//}}}



console.update(__T, __C)