PyQuant News is on Substack

Looking for the newest weekly Python deep dives for algorithmic trading, market data analysis, and quant finance? Subscribe to the PyQuant Newsletter on Substack for free.

Subscribe for free 👉

How to make amazing dashboards to easily power alpha analysis

August 19, 2023
Facebook logo.
Twitter logo.
LinkedIn logo.
Newsletter issue count indicatorNewsletter total issues indicator
How to make amazing dashboards to easily power alpha analysis

How to make amazing dashboards to easily power alpha analysis

Principal component analysis (PCA) is used widely in data science. It’s a way to reduce the number of dimensions in a data set.

It's also used in quant finance to find alpha.

In a stock portfolio, a dimension might be a column of returns for one of the stocks.

Once you get the model built, you could spend your time tweaking the code.

Or, you can do it in an interactive dashboard to power your alpha analysis!

Today, we’ll build a Plotly Dash app that displays information about a factor analysis.

Let’s go!

How to make amazing dashboards to easily power alpha analysis

How to make amazing dashboards to easily power alpha analysis. PCA isolates the statistical return drivers of a portfolio.

PCA isolates the statistical return drivers of a portfolio. These drivers are called “alpha factors” (or just factors) because they create returns that are not explained by a benchmark.

Quants use factors in trading strategies.

First, they isolate the components. Then they buy the stocks with the largest exposure to a factor and sell the stocks with the smallest exposure to a factor.

Today, we’ll create a Plotly Dash app that accepts a list of ticker symbols, identifies the principal components of their returns, and generates plots to visualize the top factors.

Imports and set up

All the code we’ll write should be in a Python file called app.py. Make sure you install Dash, Dash Bootstrap Components, and Plotly.

Let’s get the imports out of the way.

Build the app components

Initialize the app and construct the components of the user interface.

Start with the text field to enter the list of ticker symbols, the dropdown to select the number of components, the date picker to select the range of data, and the submit button to run the app.

Combine the form elements and placeholders for visualizations to form the app layout.

Dash lets you build columns and rows in a typical grid layout using Python classes. These are then styled with the Bootstrap style library.

Build the callback

This code implements a callback function, update_graphs, which ties our user inputs to the visualization outputs.

Every time the user presses the submit button, this function fetches the data, performs the PCA, and updates the visualizations.

You define a callback by using the callback decorator provided by Dash.

This decorator specifies which component properties to watch (Input) and which to update (Output). In our app, clicking the submit button triggers the callback function.

The callback parses the user inputs, downloads data, completes PCA, and generates the visualizations.

When you’re ready, to to the command line and run python app.py. Then open your browser to the URL that prints on the screen.