
Portfolio Optimisation
A Novel Approach using bt and Dash
This page describes the basic idea behind the creation of the Plotly Dash and bt package based web app: here.
Check out the blogs here.
Augmenting bt Algos Implementation: A Recipe Driven Strategy Runner
Every good coder appreciates the importance of a config driven approach to implementation. It allows the user to experiment with various types of inputs without making too many changes to the codebase. If we would like to implement multiple bt Algos strategies on a piece of data and most importantly, compare them, we might have to write repetitive bits of code to compile and visualize the results. To be fair, bt algos does support multiple strategy run in a very intuitive manner, I have tried a different approach of creating a recipe: a json based config that allows users to easily manipulate the parameters of any strategy.
Below is a simple example of the WeighMeanVar strategy being implemented, which is basically sharpe ratio minimization.
-
WeighMeanStrategy is the name of a strategy chosen by user. The overall recipe can have multiple such strategies with different underlying params.
-
rebalance_freq: As the name suggests this decides how often the portfolio will be rebalanced. Choose among: RunYearly, RunMonthly, RunQuarterly, RunWeekly, RunDaily. Custom periods are not yet supported.
-
select: Specifies the assets to select from the data being backtested.
-
RunAfterDate: Date after which the strategy is implemented
-
optimiser: Most important parameter! The name is the bt Algos optimiser (choose any from documentation) and the args is the paramters being passed to the optimiser.
Strategy | bt name | Description |
|---|---|---|
Target Volatility | TargetVol | Updates weights based on annualised volatility desired |
Inverse Volatility Weighting | WeighInvVol | With inverse volatility weighting, assets are weighted in inverse proportion to the volatility of their returns. Assets that are more volatile receive lower weights. Selecting a lookback period is required to calculate assets' volatility. |
Mean Variance Optimisation | WeighMeanVar | Calculate weights based on Markovitz Mean Variance Optimisation |
Buy and Hold | RunOnce | Passive investment strategy where an investor buys a security and holds it for a long period, regardless of short-term fluctuations. |
Some examples of bt Strategies
Multiple Parameter Support
Now here’s the steroid boost we provide in our implementation: if you pass a list of values in a parameter, multiple strategies will be implemented with the different combinations. For example if we pass the following in optimiser:
The strategy runner will internally convert the above into two strategies: WeighMeanStrategy.1 and WeighMeanStrategy.2 which will run independently with the two lookbacks.
Please note that although the lookbacks are passed as pandas date offsets typically in bt Algos, for json implementation purposes we pass a string based on the ISO 8601 duration format.

Plotly Dash: A Cherry on Top!
Honestly, if the recipe driven approach was being provided as a simple function or class method to users for experimenting on their own, it’s not a major improvement on anything that is not already available in bt package. But the advantage of this approach comes to fruition when it is paired with a Python Dash based UI implementation. To summarize, Plotly Dash is a Python framework for building lightweight analytical web applications. It requires minimal knowledge of front end and very easy for Python users to pick up. It has basic front end components like user input fields, dropdowns and buttons that are very easy to link to backend Python processes through app callbacks functionality.
Below is a simple illustration of how we can run a backtest by selecting assets after the recipe has been validated.
In addition to the above, the bt results object we get after running a backtest has several other interesting data points
like transactions, monthly returns, drawdowns across time. All of this can be conveniently visualized through the
dash app. The ease of parameter variation and trying out various optimizers on a dataset makes this combination
of bt and plotly a definite winner. This is essentially a low code implementation of bt on real data, with enough
customizations available to the user to justify its usage.
Click here to get started on the app!