ПОЛИТИКА ЗА ПОВЕРИТЕЛНОСТ И ЗАЩИТА НА ЛИЧНИ ДАННИ

Using GO for Calculation

Financial data and ratios - one problem to solve with Go

Suppose we have many financial data for a lot of companies such as assets, profit, equity, liabilities, and so on. This data is recorded for different years and quarters. We want to calculate specific financial ratios and create charts for analysis of how these ratios changed in time. We need recalculation when the data are changed for the previous year. We must be able to make changes to the algorithm of ratios calculation.

PHP application is used for this task. PHP is an awesome tool for data visualization but there are problems when big calculations are needed. Often ratios calculations used more time than the maximum execution time for PHP. The solution with many short steps for calculations makes the application complicated.

PHP works easily with databases. The relational databases have different tools for data processing such as views, functions, and procedures. The ability to transfer calculations to the database also complicates the task.

The web applications have mostly layered architecture. The database is a separated layer that is realized often using an ORM (Object Relational Mapping) The most used ORM for PHP are Doctrine, Eloquent and Proper. All these ORMs are not fully applicable for custom and complicated databases. As an example, only Doctrine can work with a composite primary key.

Loading data into the database is relatively rare, for example, once a month. The processing of the input data does not depend on the calculations. No need for transactions.

Separating the calculations from the PHP code of the application we can use PHP only for visualization. Separating the calculations from the database will allow easy use of ORM – Doctrine.

The magical tool for separating the calculations can be the new GO language created by Google in 2009.

Go is open source. You can make executable file for different Operating Systems. Go language can be used for stand-alone web applications too. The language is C-based and has different packages including math, strings, and database.

Go uses objects but not classes. Go has a structure as data type and we can write functions that work with structures.

The database

Each type of data such as assets, profit, return on assets will be called an indicator. Some indicators are input data and others are calculated. Every indicator has a unique ID, name, and sometimes a formula.

Every company has a unique ID and name. We can add many different data like address, manager, business identifier as fields (columns) in every company record.

The date for the indicator and company is always the end of a quarter.

The combination of company, date, and indicator is unique, and we will use a composite primary key here.

Go for Calculation in MySQL Database

How to describe algorithm of calculation

Let us create a simple rule for the formula in the indicator table:

  • The formula is a string.
  • At the beginning is the name of the formula ended with “:”
  • Next is a human-readable string with arithmetic operators, abbreviated terms, and ID of indicators closed in []
  • The program can read the name of the function and the sequence of indicators.
  • The “func” with the same name in the application will “know” how to calculate the indicator

Examples:

Formula in the database Returned array for function and arguments
func1:[340]/[250] {func1:, 340, 250}
func2:[180]/([450]+[240) {func2:, 180, 450, 240}
adj:adj[168] {adj:, 168}
avr1:avr[450] {avr1:, 450}
sumsub:[140]-[228] {sumsub:, 140, -228}
Go for Calculation in MySQL Database

First step, second step, third step…

1. At the first step, we can make calculations with the independent input data such as capital ratio.

2. The profit is cumulative and the real profit only for one quarter must be calculated separately. The real profit for the second quarter is equal to profit for Q2 minus profit for Q1. That will be made with adj function.

3. As the second step, we can calculate the real ROE and ROA for every quarter.

4. It is possible to use the third step too.

GO code

Only one package – main. More on github!

Files:

  • - indicators.go – read indicators with formula, companies, and calculate how many rows must be updated
  • - func1.go – calculate simple ratio for given step: first step, second step, etc
  • - adj.go – make specific calculation for cumulative data
  • - sumsub.go – calculate indicator as sum from list of other indicators
  • - main.go – invoke functions in other files step by step
  • - setup.go – read connection string for the database and list of arguments