Mastering Portfolio Optimization of Cryptocurrency Assets in Python – DataDrivenInvestor

Oct 26
Disclaimer: Nothing in this article should be construed as investment advice. The information presented in this article is for educational purposes only. Please consult with a professional financial advisor for investment advice.
Portfolio optimization is an investment activity where an investor selects a set of assets such that profits are maximized while risk is minimized. A simple example is selecting a group of 10 stocks, for example from the S&P 500 list, that the investor predicts will increase in price in the future. Portfolio optimization is typically thought of in the context of investing in stocks, though it can be applied to real estate, art, and cryptocurrency investments as well.
Whether investing manually or algorithmically, the general rule of thumb is to invest in a diverse group of assets. For example, if you are building a portfolio of stocks, it is better to invest in stocks across disparate industries as opposed to within a single industry. Generally, the more diversified you are as an investor, the more protected you are from market volatility and risk.
This is also the case regarding investing in cryptocurrencies. There are a variety of disparate categories in crypto that allow investor to create diversified portfolios. This includes, the relatively large category of decentralized finance (DeFi), lending, e-commerce, NFTs, decentralized exchanges, gaming and more. In this context, it is better to have distributed positions in several distinct categories instead of heavily investing into a single category.
There are a wide variety of methods, simple and complex, that can be used for building a diverse portfolio. The simplest and safest method, in tradition markets, is investing in the S&P 500. Since it is made up of the top 500 companies in the US, it is inherently diverse and the investor does not have to worry about selecting individual assets. Another choice is manually selecting stocks of public companies that the investor values and ideally uses or buys products from. There are also a set of portfolio optimization algorithms that allows investors to programmatically build investment portfolios. In this article I will discuss how to use mean variance optimization to build a diversified cryptocurrency portfolio.
For my analysis, I will be writing code in Deepnote, which is a collaborative data science notebook that makes running reproducible experiments very easy.
To start, let’s navigate to Deepnote and create a new project (you can sign-up for free if you don’t already have an account).
Next, let’s create a new project called ‘cryptocurrency_analysis’ and within that define a notebook called ‘portfolio_optimization’:
We will pull crypto price data using the Pandas-Datareader library. You can easily install the library using pip in a terminal command line:
Next, let’s import the data reading in a new Python script:
We should pull crypto prices from a few different categories, so we’ll gather price data in DeFi, decentralized exchanges, lending, and gaming.
Let’s start by pulling a few stocks in DeFi. We will pull three years of stock price data for Cardano (ADA), Ripple (XRP,) and Polygon (MATIC)
First, let’s import Pandas and relax the display limits on rows and columns:
Next, let’s import the datetime module and define start and end dates:
Now we have everything we need to pull crypto prices. Let’s get data for Cardana (ADA):
Let’s define a function that uses this logic to pull crypto prices:
Now let’s pull prices for Cardano (ADA), Ripple (XRP), Polkadot (DOT), and Polygon (MATIC):
Let’s define another function that combines the dataframes of crypto prices:
Now, let’s pull crypto prices for the remaining categories:
Now we are in a good position to use a portfolio optimization algorithm. We will use the mean-variance optimization algorithm which works by selecting the least correlate assets that generate the highest returns. We start by installing and importing the PyPortfolioOpt library:
Next let’s calculate the mean historical return and the covariance matrix for our assets:
Next we will use efficient frontier to calculate the weights for each asset. The weights are calculating using the Sharpe ratio, which is the ratio between risk and return. The higher the Sharpe ratio the lower the risk and the higher the returns:
Let’s also display portfolio performance:
These values should obviously be taken with a grain of salt given the volatility of cryptocurrency markets.
Next let’s convert the weights to allocation values using an investment amount of $100000:
We see that based on the 3-year historical returns and risks our algorithm suggests we invest heavy into AAVE , MATIC, SAND, and MANA while taking moderate positions on the remaining cryptocurrencies.
The code from this post is available on GitHub.
Diversification is an important part of building an investment portfolio that is robust to market swings. In the context of investing in cryptocurrency, diversification can be achieved through selecting projects with disparate use-cases. There are application of crypto in the space of lending, e-commerce, real estate, decentralized exchanges and more. Further, selecting projects to invest in across these different use cases can be aided by algorithms such as mean-variance optimization. I encourage you to use this algorithm to play around with some asset allocations yourself. Thank you for reading!
Subscribe to DDIntel Here.
Visit our website here:
Join our network here:

empowerment through data, knowledge, and expertise. subscribe to DDIntel at
Rachayata Technologies
Evgeniya Davtyan
The Money Cog
InsiderFinance Wire
лина масалова
Latest In Crypto
The Capital
Writer for Built In & Towards Data Science. Cornell University Ph. D. in Chemical Physics.


Post expires at 5:39pm on Thursday April 27th, 2023

Leave a Reply

Next Post

Wix Becomes An Official React Native Partner - PR Newswire

Thu Oct 27 , 2022
Searching for your content… In-Language News Contact Us 888-776-0942 from 8 AM – 10 PM ET News provided byOct 25, 2022, 09:00 ETShare this articleWix joins Meta, Microsoft, Tesla, Shopify, and other market leaders to further advance React Native and open source communityNEW YORK, Oct. 25, 2022 /PRNewswire/ — Ltd. (NASDAQ: […]
%d bloggers like this: