• Stars
    star
    307
  • Rank 136,109 (Top 3 %)
  • Language
    Go
  • License
    MIT License
  • Created almost 4 years ago
  • Updated 7 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A go port of numpy-financial functions and more.

go-financial

This package is a go native port of the numpy-financial package with some additional helper
functions.

The functions in this package are a scalar version of their vectorised counterparts in
the numpy-financial library.

unit-tests status Go Report Card codecov GoDoc Release PRs Welcome

Currently, only some functions are ported,
which are as follows:

numpy-financial function go native function ported? info
fv βœ… Computes the future value
ipmt βœ… Computes interest payment for a loan
pmt βœ… Computes the fixed periodic payment(principal + interest) made against a loan amount
ppmt βœ… Computes principal payment for a loan
nper βœ… Computes the number of periodic payments
pv βœ… Computes the present value of a payment
rate βœ… Computes the rate of interest per period
irr Computes the internal rate of return
npv βœ… Computes the net present value of a series of cash flow
mirr Computes the modified internal rate of return

Index

While the numpy-financial package contains a set of elementary financial functions, this pkg also contains some helper functions on top of it. Their usage and description can be found below:

Detailed documentation is available at godoc.

Amortisation(Generate Table)

To generate the schedule for a loan of 20 lakhs over 15years at 12%p.a., you can do the following:

package main

import (
	"time"

	"github.com/shopspring/decimal"

	financial "github.com/razorpay/go-financial"
	"github.com/razorpay/go-financial/enums/frequency"
	"github.com/razorpay/go-financial/enums/interesttype"
	"github.com/razorpay/go-financial/enums/paymentperiod"
)

func main() {
	loc, err := time.LoadLocation("Asia/Kolkata")
	if err != nil {
		panic("location loading error")
	}
	currentDate := time.Date(2009, 11, 11, 4, 30, 0, 0, loc)
	config := financial.Config{

		// start date is inclusive
		StartDate: currentDate,

		// end date is inclusive.
		EndDate:   currentDate.AddDate(15, 0, 0).AddDate(0, 0, -1),
		Frequency: frequency.ANNUALLY,

		// AmountBorrowed is in paisa
		AmountBorrowed: decimal.NewFromInt(200000000),

		// InterestType can be flat or reducing
		InterestType: interesttype.REDUCING,

		// interest is in basis points
		Interest: decimal.NewFromInt(1200),

		// amount is paid at the end of the period
		PaymentPeriod: paymentperiod.ENDING,

		// all values will be rounded
		EnableRounding: true,

		// it will be rounded to nearest int
		RoundingPlaces: 0,

		// no error is tolerated
		RoundingErrorTolerance: decimal.Zero,
	}
	amortization, err := financial.NewAmortization(&config)
	if err != nil {
		panic(err)
	}

	rows, err := amortization.GenerateTable()
	if err != nil {
		panic(err)
	}
	// Generates json output of the data
	financial.PrintRows(rows)
	// Generates a html file with plots of the given data.
	financial.PlotRows(rows, "20lakh-loan-repayment-schedule")
} 
  

Generated plot

Fv

func Fv(rate decimal.Decimal, nper int64, pmt decimal.Decimal, pv decimal.Decimal, when paymentperiod.Type) decimal.Decimal 

Params:

 pv   : a present value 
rate  : an interest rate compounded once per period 
nper  : total number of periods 
pmt   : a (fixed) payment, paid either at the beginning (when =  1)
        or the end (when = 0) of each period 
when  : specification of whether payment is made at the beginning (when = 1)
        or the end (when = 0) of each period  

Fv computes future value at the end of some periods(nper).

Example(Fv)

If an investment has a 6% p.a. rate of return, compounded annually, and you are investing β‚Ή 10,000 at the end of each year with initial investment of β‚Ή 10,000, how much amount will you get at the end of 10 years ?

package main

import (
	"fmt"

	gofinancial "github.com/razorpay/go-financial"
	"github.com/razorpay/go-financial/enums/paymentperiod"
	"github.com/shopspring/decimal"
)

func main() {
	rate := decimal.NewFromFloat(0.06)
	nper := int64(10)
	payment := decimal.NewFromInt(-10000)
	pv := decimal.NewFromInt(-10000)
	when := paymentperiod.ENDING

	fv := gofinancial.Fv(rate, nper, payment, pv, when)
	fmt.Printf("fv:%v", fv.Round(0))
	// Output:
	// fv:149716
}

Run on go-playground

Pv

func Pv(rate decimal.Decimal, nper int64, pmt decimal.Decimal, fv decimal.Decimal, when paymentperiod.Type) decimal.Decimal 

Params:

 fv	: a future value
 rate	: an interest rate compounded once per period
 nper	: total number of periods
 pmt	: a (fixed) payment, paid either
	  at the beginning (when =  1) or the end (when = 0) of each period
 when	: specification of whether payment is made
	  at the beginning (when = 1) or the end
	  (when = 0) of each period

Pv computes present value some periods(nper) before the future value.

Example(Pv)

If an investment has a 6% p.a. rate of return, compounded annually, and you wish to possess β‚Ή 1,49,716 at the end of 10 peroids while providing β‚Ή 10,000 per period, how much should you put as your initial deposit ?

package main

import (
	"fmt"

	gofinancial "github.com/razorpay/go-financial"
	"github.com/razorpay/go-financial/enums/paymentperiod"
	"github.com/shopspring/decimal"
)

func main() {
	rate := decimal.NewFromFloat(0.06)
	nper := int64(10)
	payment := decimal.NewFromInt(-10000)
	fv := decimal.NewFromInt(149716)
	when := paymentperiod.ENDING

	pv := gofinancial.Pv(rate, nper, payment, fv, when)
	fmt.Printf("pv:%v", pv.Round(0))
	// Output:
	// pv:-10000	
}

Run on go-playground

Npv

func Npv(rate decimal.Decimal, values []decimal.Decimal) decimal.Decimal 

Params:

 rate	: a discount rate compounded once per period
 values	: the value of the cash flow for that time period. Values provided here must be an array of float64

Npv computes net present value based on the discount rate and the values of cash flow over the course of the cash flow period

Example(Npv)

Given a rate of 0.281 per period and initial deposit of 100 followed by withdrawls of 39, 59, 55, 20. What is the net present value of the cash flow ?

package main

import (
	"fmt"
	gofinancial "github.com/razorpay/go-financial"	
	"github.com/shopspring/decimal"
)

func main() {
	rate :=  decimal.NewFromFloat(0.281)
	values := []decimal.Decimal{decimal.NewFromInt(-100), decimal.NewFromInt(39), decimal.NewFromInt(59), decimal.NewFromInt(55), decimal.NewFromInt(20)}
	npv := gofinancial.Npv(rate, values)
	fmt.Printf("npv:%v", npv)
	// Output:
	// npv: -0.008478591638426
}

Run on go-playground

Pmt

func Pmt(rate decimal.Decimal, nper int64, pv decimal.Decimal, fv decimal.Decimal, when paymentperiod.Type) decimal.Decimal

Params:

rate  : rate of interest compounded once per period 
nper  : total number of periods to be compounded for 
pv    : present value (e.g., an amount borrowed) 
fv    : future value (e.g., 0) 
when  : specification of whether payment is made at the
        beginning (when = 1) or the end (when = 0) of each period  

Pmt compute the fixed payment(principal + interest) against a loan amount ( fv = 0).
It can also be used to calculate the recurring payments needed to achieve a certain future value given an initial deposit, a fixed periodically compounded interest rate, and the total number of periods.

Example(Pmt-Loan)

If you have a loan of 1,00,000 to be paid after 2 years, with 18% p.a. compounded annually, how much total payment will you have to do each month? This example generates the total monthly payment(principal plus interest) needed for a loan of 1,00,000 over 2 years with 18% rate of interest compounded monthly

package main

import (
	"fmt"
	gofinancial "github.com/razorpay/go-financial"
	"github.com/razorpay/go-financial/enums/paymentperiod"
	"github.com/shopspring/decimal"
)

func main() {
	rate := decimal.NewFromFloat(0.18 / 12)
	nper := int64(12 * 2)
	pv := decimal.NewFromInt(100000)
	fv := decimal.NewFromInt(0)
	when := paymentperiod.ENDING
	pmt := gofinancial.Pmt(rate, nper, pv, fv, when)
	fmt.Printf("payment:%v", pmt.Round(0))
        // Output:
        // payment:-4992
}

Run on go-playground

Example(Pmt-Investment)

If an investment gives 6% rate of return compounded annually, how much amount should you invest each month to get 10,00,000 amount after 10 years?

package main

import (
	"fmt"
	gofinancial "github.com/razorpay/go-financial"
	"github.com/razorpay/go-financial/enums/paymentperiod"
	"github.com/shopspring/decimal"
)

func main() {
	rate := decimal.NewFromFloat(0.06)
	nper := int64(10)
	pv := decimal.NewFromInt(0)
	fv := decimal.NewFromInt(1000000)
	when := paymentperiod.BEGINNING
	pmt := gofinancial.Pmt(rate, nper, pv, fv, when)
	fmt.Printf("payment each year:%v", pmt.Round(0))
        // Output:
        // payment each year:-71574
}

Run on go-playground

IPmt

func IPmt(rate decimal.Decimal, per int64, nper int64, pv decimal.Decimal, fv decimal.Decimal, when paymentperiod.Type) decimal.Decimal   

IPmt computes interest payment for a loan under a given period.

Params:

rate  : rate of interest compounded once per period 
per   : period under consideration 
nper  : total number of periods to be compounded for 
pv    : present value (e.g., an amount borrowed) 
fv    : future value (e.g., 0) 
when  : specification of whether payment is made at the
        beginning (when = 1) or the end (when = 0) of each period  

Example(IPmt-Loan)

If you have a loan of 1,00,000 to be paid after 2 years, with 18% p.a. compounded annually, how much of the total payment done each month will be interest ?

package main

import (
	"fmt"
	gofinancial "github.com/razorpay/go-financial"
	"github.com/razorpay/go-financial/enums/paymentperiod"
	"github.com/shopspring/decimal"
)

func main() {
	rate := decimal.NewFromFloat(0.18 / 12)
	nper := int64(12 * 2)
	pv := decimal.NewFromInt(100000)
	fv := decimal.NewFromInt(0)
	when := paymentperiod.ENDING

	for i := int64(0); i < nper; i++ {
		ipmt := gofinancial.IPmt(rate, i+1, nper, pv, fv, when)
		fmt.Printf("period:%d interest:%v\n", i+1, ipmt.Round(0))
	}
	// Output:
	// period:1 interest:-1500
	// period:2 interest:-1448
	// period:3 interest:-1394
	// period:4 interest:-1340
	// period:5 interest:-1286
	// period:6 interest:-1230
	// period:7 interest:-1174
	// period:8 interest:-1116
	// period:9 interest:-1058
	// period:10 interest:-999
	// period:11 interest:-939
	// period:12 interest:-879
	// period:13 interest:-817
	// period:14 interest:-754
	// period:15 interest:-691
	// period:16 interest:-626
	// period:17 interest:-561
	// period:18 interest:-494
	// period:19 interest:-427
	// period:20 interest:-358
	// period:21 interest:-289
	// period:22 interest:-218
	// period:23 interest:-146
	// period:24 interest:-74
}

Run on go-playground

Example(IPmt-Investment)

If an investment gives 6% rate of return compounded annually, how much interest will you earn each year against your yearly payments(71574) to get 10,00,000 amount after 10 years

package main

import (
	"fmt"
	gofinancial "github.com/razorpay/go-financial"
	"github.com/razorpay/go-financial/enums/paymentperiod"
	"github.com/shopspring/decimal"
)

func main() {
	rate := decimal.NewFromFloat(0.06)
	nper := int64(10)
	pv := decimal.NewFromInt(0)
	fv := decimal.NewFromInt(1000000)
	when := paymentperiod.BEGINNING

	for i := int64(1); i < nper+1; i++ {
		ipmt := gofinancial.IPmt(rate, i+1, nper, pv, fv, when)
		fmt.Printf("period:%d interest earned:%v\n", i, ipmt.Round(0))
	}
	// Output:
	// period:1 interest earned:4294
	// period:2 interest earned:8846
	// period:3 interest earned:13672
	// period:4 interest earned:18786
	// period:5 interest earned:24208
	// period:6 interest earned:29955
	// period:7 interest earned:36047
	// period:8 interest earned:42504
	// period:9 interest earned:49348
	// period:10 interest earned:56604
}

Run on go-playground

PPmt

func PPmt(rate decimal.Decimal, per int64, nper int64, pv decimal.Decimal, fv decimal.Decimal, when paymentperiod.Type) decimal.Decimal   

PPmt computes principal payment for a loan under a given period.

Params:

rate  : rate of interest compounded once per period 
per   : period under consideration 
nper  : total number of periods to be compounded for 
pv    : present value (e.g., an amount borrowed) 
fv    : future value (e.g., 0) 
when  : specification of whether payment is made at 
        the beginning (when = 1) or the end (when = 0) of each period  

Example(PPmt-Loan)

If you have a loan of 1,00,000 to be paid after 2 years, with 18% p.a. compounded annually, how much total payment done each month will be principal ?

package main

import (
	"fmt"
	gofinancial "github.com/razorpay/go-financial"
	"github.com/razorpay/go-financial/enums/paymentperiod"
	"github.com/shopspring/decimal"
)

func main() {
	rate := decimal.NewFromFloat(0.18 / 12)
	nper := int64(12 * 2)
	pv := decimal.NewFromInt(100000)
	fv := decimal.NewFromInt(0)
	when := paymentperiod.ENDING

	for i := int64(0); i < nper; i++ {
		ppmt := gofinancial.PPmt(rate, i+1, nper, pv, fv, when)
		fmt.Printf("period:%d principal:%v\n", i+1, ppmt.Round(0))
	}
	// Output:
	// period:1 principal:-3492
	// period:2 principal:-3545
	// period:3 principal:-3598
	// period:4 principal:-3652
	// period:5 principal:-3707
	// period:6 principal:-3762
	// period:7 principal:-3819
	// period:8 principal:-3876
	// period:9 principal:-3934
	// period:10 principal:-3993
	// period:11 principal:-4053
	// period:12 principal:-4114
	// period:13 principal:-4176
	// period:14 principal:-4238
	// period:15 principal:-4302
	// period:16 principal:-4366
	// period:17 principal:-4432
	// period:18 principal:-4498
	// period:19 principal:-4566
	// period:20 principal:-4634
	// period:21 principal:-4704
	// period:22 principal:-4774
	// period:23 principal:-4846
	// period:24 principal:-4919
}

Run on go-playground

Nper

func Nper(rate decimal.Decimal, pmt decimal.Decimal, pv decimal.Decimal, fv decimal.Decimal, when paymentperiod.Type) (result decimal.Decimal, err error)  

Params:

rate   : an interest rate compounded once per period
pmt    : a (fixed) payment, paid either at the beginning (when =  1)
         or the end (when = 0) of each period
pv     : a present value
fv     : a future value
when   : specification of whether payment is made at the beginning (when = 1)
         or the end (when = 0) of each period  

Nper computes the number of periodic payments.

Example(Nper-Loan)

If a loan has a 6% annual interest, compounded monthly, and you only have $200/month to pay towards the loan, how long would it take to pay-off the loan of $5,000?

package main

import (
	"fmt"

	gofinancial "github.com/razorpay/go-financial"
	"github.com/razorpay/go-financial/enums/paymentperiod"
	"github.com/shopspring/decimal"
)


func main() {
	rate := decimal.NewFromFloat(0.06 / 12)
	fv := decimal.NewFromFloat(0)
	payment := decimal.NewFromFloat(-200)
	pv := decimal.NewFromFloat(5000)
	when := paymentperiod.ENDING

	nper,err := gofinancial.Nper(rate, payment, pv, fv, when)
	if err != nil{
		fmt.Printf("error:%v\n", err)
	}
	fmt.Printf("nper:%v",nper.Ceil())
	// Output:
	// nper:27
}

Run on go-playground

Rate

func Rate(pv, fv, pmt decimal.Decimal, nper int64, when paymentperiod.Type, maxIter int64, tolerance, initialGuess decimal.Decimal) (decimal.Decimal, error) 

Params:

pv     : a present value
fv     : a future value
pmt    : a (fixed) payment, paid either at the beginning (when =  1)
         or the end (when = 0) of each period
nper   : total number of periods to be compounded for
when   : specification of whether payment is made at the beginning (when = 1)
         or the end (when = 0) of each period
maxIter	: total number of iterations for which function should run
tolerance : tolerance threshold for acceptable result
initialGuess : an initial guess amount to start from

Returns:

rate    : a value for the corresponding rate
error   : returns nil if rate difference is less than the threshold (returns an error conversely)

Rate computes the interest rate to ensure a balanced cashflow equation

Example(Rate-Investment)

If an investment of $2000 is done and an amount of $100 is added at the start of each period, for what periodic interest rate would the invester be able to withdraw $3000 after the end of 4 periods ? (assuming 100 iterations, 1e-6 threshold and 0.1 as initial guessing point)

package main

import (
	"fmt"
	gofinancial "github.com/razorpay/go-financial"
	"github.com/razorpay/go-financial/enums/paymentperiod"
	"github.com/shopspring/decimal"
)

func main() {
	fv := decimal.NewFromFloat(-3000)
	pmt := decimal.NewFromFloat(100)
	pv := decimal.NewFromFloat(2000)
	when := paymentperiod.BEGINNING
	nper := decimal.NewFromInt(4)
	maxIter := 100
	tolerance := decimal.NewFromFloat(1e-6)
	initialGuess := decimal.NewFromFloat(0.1),

	rate, err := gofinancial.Rate(pv, fv, pmt, nper, when, maxIter, tolerance, initialGuess)
	if err != nil {
		fmt.Printf(err)
	} else {
		fmt.Printf("rate: %v ", rate)
	}
	// Output:
	// rate: 0.06106257989825202
}

Run on go-playground

More Repositories

1

blade

Design System that powers Razorpay
TypeScript
488
star
2

ifsc

🏦 IFSC Codes Repository
HTML
341
star
3

razorpay-php

Razorpay PHP Library
PHP
169
star
4

razorpay-python

Razorpay Python SDK
Python
153
star
5

razorpay-node

Razorpay node.js bindings
JavaScript
146
star
6

devstack

Razorpay DevX cloud on laptop solution
Go
126
star
7

react-native-razorpay

React Native wrapper for Razorpay's mobile SDKs
Java
123
star
8

razorpay-flutter

Razorpay Flutter Plugin
Dart
107
star
9

razorpay-android-sample-app

Sample app demonstrating integration of Razorpay checkout
Java
88
star
10

ifsc-api

🏦 Standalone API for IFSC codes
CSS
77
star
11

concierge

Web Management & Secure Access Control of AWS Security Groups. Allows you to create IP leases for incoming connections to AWS instances. Also, allows creating invites to allow guest access by URL/Email Invites.Uses two factor authentication for additional security.
Go
68
star
12

razorpay-ruby

Razorpay Ruby SDK
Ruby
62
star
13

react-native-apollo-devtools

Flipper plugin to debug apollo client cache for React Native
TypeScript
62
star
14

razorpay-java

Razorpay Java SDK
Java
56
star
15

razorpay-cordova

Cordova wrapper around Razorpay's Android and iOS SDKs
Shell
51
star
16

metro

The Service Bus!
Go
47
star
17

shubhcron

A drop-in cron replacement that can run your jobs on auspicious timings.
Go
44
star
18

razorpay-woocommerce

Razorpay Payment Extension for Woocommerce
PHP
40
star
19

public-presentations

A curated list of public talks and articles by Razorpay
39
star
20

razorpay-go

Razorpay Go SDK
Go
35
star
21

alohomora

alohomora is razorpay's secret distribution system
Python
32
star
22

razorpay-php-testapp

Sample App for Razorpay PHP Integration
PHP
27
star
23

razorpay-magento

Razorpay Payment Extension for Magento
PHP
27
star
24

trino-gateway

Traffic routing for Trino Clusters
Go
24
star
25

razorpay-python-testapp

Razorpay test app for python
HTML
22
star
26

razorpay-pod

πŸ“± CocoaPod implementation of Razorpay's Payment SDK. Refer for instructions:
Objective-C
21
star
27

razorpay-cordova-sample-app

πŸ“± Razorpay Sample Application for Cordova, Ionic v1 and Ionic v2
JavaScript
16
star
28

opensource.razorpay.com

Razorpay Open Source showcase website
TypeScript
16
star
29

razorpay-opencart

Razorpay Payment Extension for Opencart
PHP
16
star
30

i18nify

One stop solution for all your internationalisation needs.
JavaScript
15
star
31

razorpay-whmcs

Razorpay Payment Extension for WHMCS
PHP
15
star
32

razorpay-dot-net

Razorpay .NET SDK
C#
15
star
33

razorpay-ios-sample-app

πŸ“± Sample app demonstrating integration of Razorpay iOS Framework
Objective-C
14
star
34

bhadra

Bhadra is a Vulnerability Management Platform to handle all security issues
HTML
13
star
35

razorpay-android-custom-sample-app

Sample app to demonstrate the custom UI integration
Kotlin
13
star
36

razorpay-capacitor

Capacitor wrapper around Razorpay's Android and iOS SDKs
TypeScript
12
star
37

razorpay-edd

Razorpay plugin for Wordpress Easy Digital Downloads
PHP
11
star
38

razorpay-prestashop

Prestashop plugin for Razorpay
PHP
11
star
39

razorpay-cscart

Razorpay Payment Extension for CS Cart
PHP
10
star
40

razorpay-dotnet-testapp

Test app for .net integration
ASP.NET
9
star
41

lytics.js

Simpler analytics using HTML attributes.
JavaScript
9
star
42

razorpay-arastta

Razorpay Payment Extension for Arastta stores
PHP
9
star
43

sorting-hat

Post new signup data to Slack using the clearbit API
Ruby
8
star
44

razorpay-quick-payments

Razorpay Quick Payments plugin for WordPress
PHP
8
star
45

razorpay-flutter-customui

Razorpay Flutter Plugin for Customui
Dart
6
star
46

razorpay-woocommerce-subscriptions

Razorpay WooCommerce Subscriptions Plugin
PHP
6
star
47

component.js

Simple and Opinionated way to build UI Components
JavaScript
6
star
48

razorpay-java-testapp

Sample app for Razorpay Java Integration
Java
5
star
49

darts

🎯 The Razorpay Dart Board Championship Hall of Fame
HTML
5
star
50

terraform-aws-ssl-ciphers

Common SSL Negotiation Policies to be used with AWS ELBs using Terraform
HCL
5
star
51

common-frontend-utils

Common Frontend Utils
JavaScript
5
star
52

wercker-ubuntu14.04-ansible1.6

Ubuntu 14.04 with Ansible 1.6 box for http://wercker.com/
5
star
53

gollum-setup

Scripts to help you setup gollum
Ruby
3
star
54

teamcolony-rss

RSS Feed generator for teamcolony reports
Ruby
3
star
55

razorpay-customui-pod

iOS CustomUI SDK of Razorpay
Objective-C
3
star
56

kong-template

Kong template for plugin development
Lua
3
star
57

.github

Razorpay's Public Profile Page
3
star
58

cryptocows-whitepaper

Technical Documentation for Razorpay CryptoCows
2
star
59

ftx-hackathon

Wiki for the FTX Hackathon
2
star
60

citadel

Documentation Platform
JavaScript
1
star
61

etcd-backup

Docker file for etcd-backup running as a k8s cron job.
Shell
1
star
62

go-openssl

OpenSSL bindings for Golang
Go
1
star
63

dot-settings

JavaScript
1
star
64

lqext

Transaction aware events dispatch
PHP
1
star
65

RazorpaySalesDemo

CSS
1
star
66

subscriptions-magento-plugin

Subscriptions plugin for magento.
PHP
1
star
67

payment-button-wordpress-plugin

Payment button plugin for wordpress.
PHP
1
star
68

omnipay-razorpay

Razorpay plugin for Omnipay Payment Processing Library
PHP
1
star