Building a financial data product

Exploring computational finance with Streamlit

Note: nothing herein is financial advice. This article is made only for informative purposes, not to be used for investment decisions.

When researching different methodologies in computational finance, I found a lack of tooling that would enable me to interactively play with different parameters and observe the changes in the model's output. While the mathematical formulation is undeniably the fuel behind the models, I find that it sometimes lacks in providing an intuitive understanding that direct interaction with the model mechanisms can offer.

A clear choice as a solution is building a data app in Streamlit. It offers the pythonic workflow that data scientists are used to while also enabling the essentials of flexibility and sharability of a proper web framework.

I decided to focus on three areas in quantitative analysis in finance:

All code used for the app is openly available in the Github repo. Also, feel free to play with the web app (deployed on Heroku).

Simulation methodology is taken from Python for Finance book. Definitely recommend a read if you're interested in diving deeper into the topic.

We would like to simulate how a certain asset’s price might evolve over time.

We simulate GBM (Geometric Brownian motion) by using historical price patterns. To do that, we leverage the Monte Carlo method to draw a sample at random from the empirical distribution and aim to approximate an expectation of a function that would otherwise be probably impossible to compute in an efficient way. This method also gives us desirable properties of estimators, such as consistency and unbiasedness.

In the app, the simulation is performed for one month in advance (November 2020) while taking into account historical data between January 2020 and October 2020. We then compare the simulated and realized prices (ground truth). For this reason, we also don't include the last month's price data for training of the simulation model, only for validating the final results.

You can choose one of the FAANG companies to simulate price for and the number of simulations to perform.

Price simulator

We would like to evaluate portfolio risk with a metric that estimates the worst expected loss.

For this, we use the value-at-risk (VaR) measure which reports this value at a given level of confidence over a certain time horizon and under normal market conditions. It's often used in the industry to estimate the asset amount needed to cover possible losses. Under the hood, it’s based on a hypothetical profit-and-loss probability density function.

To get a better intuitive understanding, it’s best to look at an example: If the 1-day 95% VaR of our portfolio is $100 this means that 95% of the time (under normal market conditions), we will not lose more than $100 by holding our portfolio over one day.

There are various ways to calculate VaR. We will be using the Monte Carlo method.

While it gives nice properties for ease of communicating risk, it has its downsides when it comes to strong assumptions about the distribution of market dynamics.

To put it bluntly, David Einhorn compared VaR to “an airbag that works all the time, except when you have a car accident”. By this, he meant that the VaR measure fails to take into account Black Swan events and tail risks in general.

That being said, it gives a good intuitive understanding of economic mechanisms if we understand the model’s limitations.

In the app, we model the portfolio risk of a preset portfolio. We assume ownership of 1 share in each of the FAANG companies. For this portfolio, we evaluate risk using the VaR metric from individual security prices. We take into account historical price data from 2018–01–01 until the day before analysis (t-1).

Risk evaluator

We would like to optimize our portfolio’s return while taking into account the risk of individual assets.

One of the best ways for that is to invest our funds into a set of ETFs (exchange-traded funds). ETFs are affordable and well established financial products used to invest in a diversified basket of assets.

To go even further, we can optimize our portfolio to invest in multiple ETFs. In the app, there is a selection of some of the most traded ETFs with a short description and historical data in the last 5 years.

We define two inputs:

  • ETFs to potentially add to our portfolio (we determine which are included in the optimized portfolio with the model)
  • Investment amount (as ETFs are traded in discrete amounts you might have some residual amount)

To optimize performance we use MVO (mean-variance optimization) to maximize the portfolio’s Sharpe ratio (risk-adjusted return). We show discrete quantities of how to distribute the investment amount.

Portfolio optimizer

In this article, I described three commonly used computational methods in finance and how they were embedded in an app.

If you haven't yet, feel free to play with the web app and see how the model output changes as you change the available parameters. There is also a lot of areas of improvement in the tools to enable more degrees of freedom (additional parameters, filters) in exploration.

Thanks for taking the time to read, please feel free to leave feedback.

Data Scientist. Tinkering with tinkering

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store