Open Source in Finance. Project Okama

Open Source in Finance. Project Okama

An interesting paradox is observed in the field of fintech projects. On the one hand, you can hardly find an area where more investment is coming. That is why the most advanced technologies are concentrated in fintech: blockchain, artificial intelligence, big data, ML, etc. On the other hand, the smallest number of well-developed open source projects is observed in the financial sector. There are advanced software products such as the Bloomberg terminal, special programming languages ​​that were developed to solve financial problems. However, on GitHub you can count the popular open source finance projects on your fingers. The availability of financial data is also not very good. Of course, there is the excellent World Bank database. But such projects are unique. There are also not many of them. It is difficult to obtain actual macroeconomic data. And if we are talking about historical market information, then with rare exceptions, only paid options are possible. In the 21st century, almost every major company supports open source projects, often providing data with user-friendly software interfaces… even Microsoft has changed its mind about open source. But in the world of finance, everything remains roughly at the level of the 90s. Why so? For me, this question remains open. Perhaps fintech projects are too focused on making a quick buck. Mythical unicorns are much more important to them. Few people are ready to work if the project is designed for payback in more than 2-3 years. Perhaps the thing is the traditions of this industry, which have yet to be broken.

I have a feeling that in financial projects, as in any other area, it is long past time to understand that competitive advantage should not necessarily consist of some unique software and exclusive access to closed databases. The expertise and experience of the team using the tool (in this case, the software) is important. It is long past time to make financial market data as open and free as possible. Those who are used to thinking in old categories may become victims of AI. NVIDIA CEO Jensen Huang’s latest thoughts about the unenviable future of programming as a profession convince me of this even more.

The place of STP in Quantitative Finance

I am a mathematician by education and have been interested in Quantitative Finance (hereinafter quants) for about 15 years. When you start studying this branch of human knowledge, everything related to trading and technical analysis lies on the surface. Trading is popular and sells well. Many projects are connected with it.

But take a look at the content of the Chartered Financial Analyst (CFA) qualification exam, the most recognized and prestigious in the world of finance. About 60% of everything related to CFA quants is Modern Portfolio Theory (STP). Sometimes this direction is called “asset allocation”. Two more big quant topics in CFA are fundamental analysis and cash flow discounting. CFA textbooks also have many chapters on financial statistics and forecasting. Trading and speculation is only one small section of the three-volume book.

I am personally interested in STP because I analyze long-term investment strategies. From the point of view of practical usefulness, I did not find anything more effective. In addition, the mathematics of STP is interesting and fascinating … maybe this is the main personal motive, and the rest is just an attempt to prove everything rationally 🙂

Existing projects on STP

For me, the benchmark for quality and usefulness has always been the Portfolio Visualizer project. It implements many things from the field of STP and its continuation (CAPM, Factor analysis). A large part of the functionality is available for free. But the project is commercial, and some things are provided by subscription. This is normal. The visualizer, in my opinion, has two disadvantages.

  1. The data available for analysis is only the history of the US stock market and some international indices. There is only one base currency of the portfolio – the US dollar. It is not possible to assemble an investment portfolio consisting of securities of different markets (outside of the American market).

  2. This is a closed source project. That says it all. Use as is. What is considered unclear. Applications for new features are not accepted. There is no community.

In Russian reality, the Visualizer is simply not used. There are no securities of Mosbirzhi in the database. Russian stock indices as well. And this is a great project. I have used it for many years and sometimes I look there to see useful ideas.

Open source projects with STP

Everything is not too fun in open source quantum. No, of course there are quite famous projects like QuantLib. But this is again about trading.

Open source projects with STP that I like:

  • FinQuant – analysis of investment portfolios, optimization

  • PyPortfolioOpt – optimization of investment portfolios

  • Riskfolio-Lib – portfolio risk management, optimization

None of the open source projects I’m aware of provide anything like Visualizer. In a rare case, there is access to historical data. Usually, these are temporary benches with Yahoo Finance available to everyone.

Project okama

Our okama project is an open source Python library and free financial database.

A few years ago, we decided to collect generally accepted financial algorithms for STP and related areas (statistics, discounting, factor analysis, etc.). I no longer wanted to think every day about where to download the data of this or that index, currency or securities. Therefore, an important part of the project is a free database of historical data.

Okama allows you to work with sets of assets denominated in different currencies. All calculated return and risk metrics are referenced to the portfolio’s base currency.

What can okama do today? Here is a list of the main features:

  • Optimization of the investment portfolio (Mean-Variance Analysis – MVA)

  • Construction of the efficiency limit taking into account different periods of rebalancing

  • Forecasting portfolio indicators using the Monte Carlo method

  • Backtesting of yield distributions

  • Forecasting using distribution types (normal, lognormal)

  • Calculation of popular risk metrics: volatility, semi-dispersion, VaR, CVaR, etc.

  • Calculation of the dividend yield of various assets

  • Backtesting of arbitrary investment portfolios

  • Bringing portfolio indicators to a single currency

  • Comparison of indices of index funds: deviation, bias, beta, correlation, etc.

  • Macroeconomic indicators of various markets: inflation, bank rates, Central Bank rates

  • Visualization: efficiency limit, transition map (Transition Map), graphs for forecasting and displaying historical data

The okama database (access via REST API) contains the following historical data:

  • Prices of shares and funds for various markets (Russia, USA, EU, Israel, etc.)

  • Unit value and NAV of mutual investment funds (PIFs)

  • The value of commodity assets (gold, silver, oil, etc.)

  • Currency quotes (CB and FX)

  • Cryptocurrency quotes

  • Index values

  • Inflation in different countries

  • Rates of central banks of the world

  • Real estate prices

As an example of using the library, a simple site with financial widgets and the ability to search the database was created.

Examples of using okama

Installing the library:

pip install okama

We import it…

import okama as ok

Now you can create the simplest portfolio from indices of Russian stocks and bonds:

pf1 = ok.Portfolio(['RGBITR.INDX', 'MCFTR.INDX'], ccy='RUB')

RGBITR.INDX – Total yield index of federal loan bonds (OFZ). MCFTR.INDX – Index of the Moscow Stock Exchange of total profitability

Historical data is automatically pulled from the database. This entire example has 21 years of history, which is not bad for the Russian market.

By default, securities have equal weights. But if you need to create a portfolio, for example, 60/40, it is also simple:

pf2 = ok.Portfolio(['RGBITR.INDX', 'MCFTR.INDX'], ccy='RUB', weights=[.60, .40])

In the pf2 portfolio, all parameters are calculated in rubles. But you can create a similar portfolio and get risk and return in another currency (USD, EUR, PND, CNY, etc.).

Let’s make a portfolio of popular foreign ETFs and change the base currency of the portfolio to the US dollar:

pf3 = ok.Portfolio(['AGG.US', 'SPY.US', 'GLD.US'], weights=[.40, .30, .30], ccy='USD')

The full names of papers, their tickers and weights can be viewed in one table:


The portfolio balance history is viewed through the class property wealth_index:


The graph shows the portfolio balance history and accumulated inflation.

Summary table with the main indicators of risk and profitability of the portfolio based on historical data:

pf3.describe([1, 5, 10])  # получаем статистику доходности за 1, 5 и 10 лет

If you want something more advanced, you can create a strategy with withdrawals or deposits and set a portfolio rebalancing period.

weights = [0.60, 0.30, 0.10] 
pf4 = ok.Portfolio(
    ['RGBITR.INDX', 'MCFTR.INDX', 'GC.COMM'],   # Индекс ОФЗ, Индекс Мосбиржи, золото
    inflation=True,  # загружаем данные по инфляции
    symbol="Cons_portf.PF",  # внутренний тикер портфеля для отображения на графике
    rebalancing_period='year',  # период ребалансировки - один раз в год
    cashflow=-50_000,  # размер ежемесячных изъятий денег
    initial_amount=10_000_000,  # стартовые инвестиции
    discount_rate=None  # для дисконтирования будет использоваться размер инфляции

This strategy creates a conservative 60/30/10 portfolio of Russian bonds (OFZ index), Russian stocks (Mosbourzhi index) and gold (spot prices). The initial investment is equal to 10 million rubles. It is planned to withdraw 50,000 rubles every month. The portfolio is rebalanced once a year. You can consider it a “retirement portfolio”.

We test the strategy on historical data. This is the balance of the portfolio, taking into account monthly cash withdrawals:

pf4.dcf.wealth_index.plot()  # методы с дисконтированием начинаются с префикса Portfolio.dcf.

Investment portfolio balance and inflation

For more than 20 years of history, the balance of the portfolio has not “reset” despite withdrawals. Moreover, the portfolio kept pace with inflation most of the time. But the situation in 2021-2023 “stunned” him a little. The reason for this is a long period of rising rates, and as a result – a fall in the OFZ index. It is already clear from this graph that the OFZ index is not ideal for a pension portfolio. It is too volatile and sensitive to fluctuations in rates.

Let’s try to predict possible future scenarios for the pension strategy:

    distr="norm",  # тип распределения случайной величины - распределение Гаусса
    years=30,  # делаем прогноз на 30 лет
    backtest=True,  # график включает тестирование на исторических данных
    n=50  # количество сценариев

Forecasting investment strategy scenarios using the Monte Carlo method

Here we generate 50 random scenarios using the Monte Carlo method and see how many savings there are. The forecast horizon is 30 years. A normal distribution is used during scripting.

It is not difficult to see that in most scenarios the balance of the portfolio is zeroed out between 2035 and 2050. But there are optimistic scenarios when the portfolio remains afloat after 30 years. This example shows how you can visualize testing strategies on historical data and Monte Carlo forecasting. For professional use, of course, visualization is not enough. Quantitative analysis is required, which can be done using other methods in the library.

Many more examples of using okama can be seen in the Jupyter Notebooks (link below).

Main links of the okama project


This is the first publication of our project. Don’t forget to subscribe to our Habr blog, Python and Finance playlist on YouTube. We are always happy to make new friends :).

Related posts