Introducing go-financial ๐Ÿ“Š !

Introducing go-financial ๐Ÿ“Š !

ยท

4 min read

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.

repo table

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)

Loan Repayment Schedule 20 lakhs

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