Sign up
Viewing an AMP page:
Go to original source

Introducing go-financial 📊 !

Content

Overview

At Razorpay , we recently worked on building a collection system. For that, we needed an amortization schedule generator in go . Currently, in go, there are no packages which offer complete solution to this problem. There is numpy-financial in python, consisting of fundamental financial functions, but it only covers the reducing interest rate cases.

Introducing, go-finanicial 🎉 !

It's a go-native port of the numpy-financial library and includes amortization schedule generation.

You can either use the low-level functions for financial calculations or utilize the higher-level amortization functions. For now, only 4 functions were ported on a priority basis.

PRs are most welcome for the remaining functions. You can find the contributing guidelines here. 🧑‍💻

Demo

If you have to generate the schedule for a loan of 20 lakhs over 15years at 12% p.a., you can define the config as follows:

package main

import (
    "time"

    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() {
    // Config part
    loc, err := time.LoadLocation("Asia/Kolkata")
    if err != nil {
        panic("location loading error")
    }
    currentDate := time.Now().In(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: 2000000,
        // InterestType can be flat or reducing.
        InterestType:   interesttype.REDUCING,
        // Interest is in basis points
        Interest:       1200, 
        PaymentPeriod:  paymentperiod.ENDING,
        Round:          true,
    }
    // Amortization part
}

Then, to generate the amortization schedule, you just need to call GenerateTable method on the amortization object.


    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")

Run complete code on go-playground

The PlotRows function above generates an interactive stacked bar plot of the payments, principal and interest across the period.

For loan repayment, the payment which is made every period is constant throughout the duration of the loan(light-blue bars). Since the interest type was reducing, so higher interest is collected in the beginning and as time period passes, the interest contribution of the payment reduces gradually(dark-blue bars). Likewise, the contribution made to the principal from the payment increases over time.(red bars)

This was just one use case of the go-financial package. You can find detailed examples for more use cases and complete documentation at go-docs.

Gopher artwork credit: ashleymcnamara

Comments (1)

Comment

Great article and library.

I actually did something quite similar for JavaScript/TypeScript that works on the browser, node.js and Deno: github.com/lmammino/financial

Mentioning this because it might be a useful comparison in terms of implementation (simpler than numpy) and tests.

Thanks. Yeah, even this package uses a scalar version of the numpy formulas. Will check out your repo.

3