finance

package module
v1.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 1, 2020 License: MIT Imports: 2 Imported by: 0

README

finance

GoDoc Build Status codecov Release

Financial functions with the Excel function names and parameter order, along with an installment calculator.

Require go version >= 1.13.

Available Functions

  • PMT: calculates the payment for a loan based on constant payments and a constant interest rate.
  • IPMT: returns the interest payment for a given period for an investment based on periodic, constant payments and a constant interest rate.
  • PPMT: returns the payment on the principal for a given period for an investment based on periodic, constant payments and a constant interest rate.
  • PV: returns the present value of an investment.
  • FV: returns the future value of an investment based on periodic, constant payments and a constant interest rate.
  • NPER: returns the number of periods for an investment based on periodic, constant payments and a constant interest rate.
  • RATE: calculates interest rate per period of an annuity.

Installment Calculator

Demo usage:

import (
    "fmt"
    "github.com/hyperjiang/finance"
)

loan := finance.Loan{
    AnnualRate: 0.07,
    Periods:    12,
    Amount:     1000000,
    Method:     finance.EqualPayment,
}

installments := loan.CalculateInstallments()

fmt.Println(installments)

Documentation

Overview

Package finance implements financial functions with the Excel function names and parameter order.

Terms Definition

rate: the interest rate for the loan. (monthly rate) nper: the total number of payments for the loan. pv: the present value, or the total amount that a series of future payments is worth now; also known as the principal. fv: the future value, or a cash balance you want to attain after the last payment is made. pmt: the payment made each period and cannot change over the life of the annuity. due: adjust the payment to the beginning or end of the period. 0, At the end of the period; 1, At the beginning of the period.

Index

Constants

View Source
const (
	EqualPayment = iota
	EqualPrincipal
)

installment methods

Variables

View Source
var Accuracy = 1.0e-6

Accuracy is the accuracy in numeric approximations

View Source
var MaxIterations = 100

MaxIterations is the max iterations

View Source
var Precision = 2

Precision is the precision in installment

Functions

func FV

func FV(rate float64, nper int, pmt float64, pv float64, due float64) float64

FV returns the future value of an investment based on periodic, constant payments and a constant interest rate.

For a more complete description of the arguments in FV, see the PV function.

If rate = 0: FV = -(PV + PMT * nper)

Else

/             nper \
| 1 - (1 + rate)     |                 nper

FV = PMT * (1 + rate * due) * | ------------------ | - PV * (1 + rate)

\        rate      /

func IPMT

func IPMT(rate float64, per int, nper int, pv float64, fv float64, due float64) float64

IPMT returns the interest payment for a given period for an investment based on periodic, constant payments and a constant interest rate.

func NPER

func NPER(rate float64, pmt float64, pv float64, fv float64, due float64) float64

NPER returns the number of periods for an investment based on periodic, constant payments and a constant interest rate.

If rate = 0:

-(FV + PV)

nper = -----------

PMT

Else

     / PMT * (1 + rate * due) - FV * rate \
log | ------------------------------------ |
     \ PMT * (1 + rate * due) + PV * rate /

nper = -----------------------------------------------

log (1 + rate)

func PMT

func PMT(rate float64, nper int, pv float64, fv float64, due float64) float64

PMT calculates the payment for a loan based on constant payments and a constant interest rate.

If rate = 0:

-(FV + PV)

PMT = ------------

nper

Else

                   nper
FV + PV * (1 + rate)

PMT = --------------------------------------------

                    /             nper \
                   | 1 - (1 + rate)     |
(1 + rate * due) * | ------------------ |
                    \        rate      /

func PPMT

func PPMT(rate float64, per int, nper int, pv float64, fv float64, due float64) float64

PPMT returns the payment on the principal for a given period for an investment based on periodic, constant payments and a constant interest rate.

func PV

func PV(rate float64, nper int, pmt float64, fv float64, due float64) float64

PV returns the present value of an investment. The present value is the total amount that a series of future payments is worth now. For example, when you borrow money, the loan amount is the present value to the lender.

If rate = 0: PV = -(FV + PMT * nper)

Else

                         /              nper \
                         | 1 - (1 + rate)    |
PMT * (1 + rate * due) * | ----------------- | - FV
                         \        rate       /

PV = ------------------------------------------------------

         nper
(1 + rate)

func RATE

func RATE(nper int, pmt float64, pv float64, fv float64, due float64, guess float64) float64

RATE calculates interest rate per period of an annuity.

The basic rate formula derivation is to solve for the future value taking into account the present value: https://en.wikipedia.org/wiki/Future_value

Types

type Installment

type Installment struct {
	Period          int
	Payment         float64
	Principal       float64
	Interest        float64
	RemainingAmount float64
}

Installment is an installment

type Loan

type Loan struct {
	Amount     float64
	Periods    int
	AnnualRate float64
	Method     int
}

Loan is a loan, default method is EqualPayment

func (Loan) CalculateInstallments

func (loan Loan) CalculateInstallments() []Installment

CalculateInstallments calculates installments

func (Loan) CalculateInterest

func (loan Loan) CalculateInterest(period int) float64

CalculateInterest calculates interest in given period

func (Loan) CalculatePayment

func (loan Loan) CalculatePayment(period int) float64

CalculatePayment calculates payment in given period

func (Loan) CalculatePrincipal

func (loan Loan) CalculatePrincipal(period int) float64

CalculatePrincipal calculates principal in given period

func (Loan) CalculateTotalInterest

func (loan Loan) CalculateTotalInterest() float64

CalculateTotalInterest calculates total interest

func (Loan) CalculateTotalPayment

func (loan Loan) CalculateTotalPayment() float64

CalculateTotalPayment calculates total payment

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL