Python

Empyrial – The Easiest Way to Optimize Portfolios in Python

Empyrial – The Easiest Way to Optimize Portfolios in Python

Portfolio calculation and optimizations are very basic tasks in Quantitative Finance. But people who are just started programming might have issues with them. Empyrial library in Python might be a solution for simple portfolio tasks. It encloses data downloading and all computations behind a super simple interface. You can literally code optimization in 1-2 lines of code. In this article, I will show an example of how you can do that.

You can install Empyrial with pip in python:

pip install empyrial

Next, you have to import it in your script:

from empyrial import *

The first step is to define your portfolio object using the Engine function:

p = Engine(
    start_date = "2019-01-01", 
    portfolio = ["AAPL", "TSLA", "UBER", "MSFT", "GME", "FB", "AMZN", "V"], 
    weights = [0.1, 0.2, 0.05, 0.1, 0.2, 0.05, 0.2, 0.1],
    benchmark = ["SPY"]
)

In the Engine function you can specify:

  • start_date/end_date – date range when do you want to compute your portfolio
  • portfolio – List of stocks you want to include in your portfolio. Empyrial takes data from Yahoo Finance so these should be valid symbols from YF.
  • weights – List of weights corresponding to your stocks from “portfolio”
  • benchmark – Symbol for the benchmark you want to compare your portfolio with.

To run portfolio optimization you have to execute the following command using empyrial() function:

empyrial(p)

In just a few seconds Empyrial will get the data from Yahoo Finance, will compute your portfolio, and output to you quite a lot of results:

Basic Metrics:

Returns:

Performance vs Benchmark:

Monthly Returns:

and few more plots with drawdowns and rolling metrics.

Also, you can optimize your portfolio in Empyrial using the optimizer function. Here is an example of how you can run it:

p = Engine(
    start_date = "2019-01-01", 
    portfolio = ["AAPL", "TSLA", "UBER", "MSFT", "GME", "FB", "AMZN", "V"], 
)
optimizer(p, "EF")

This code will use efficient uses “EF” Optimizer – Global Efficient Frontier. But you can also choose another 2:

  • MV: Mean-Variance 
  • HRP: Hierarchical Risk Parity

Optimization will output you basic statistic and optimal weights:

Also, you’ll see how a hood optimized portfolio performs vs non-optimized.

In Empyrial you can also try to predict the performance of your portfolio in the future. For that you can use oracle function:

oracle(p)

This will run 10 different time series prediction models for each of your stocks and you’ll also see a summary of all stocks for the entire portfolio:

Empyrial is a pretty raw library, it was released only 2 weeks ago, but I think that already it’s pretty interesting. It allows you to compute simple portfolio tasks in better seconds. Of course, it misses quite a lot of features. It will be once to see more flexibility. But Empyrial is only starting its way so I will keep my eye on it and will keep you posted about interesting new features.

Here is Empyrial’s Github.

Leave a Comment

Your email address will not be published. Required fields are marked *

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

Pine Script Programming Courses
Pine Script Programming Courses
Learn to build your own TradingView Indicators and Strategies
Sidebar Signup Form
If you want to be the first in this business, subscribe to the latest news