Automate Your Own Crypto Index Fund Using Python

Share:
Indexes
  1. Gathering Market Data
  2. Making the Purchases
  3. Conclusion
  4. Script
Photo by Kanchanara on Unsplash

I love index funds. The vast majority of my stock portfolio is ETFs such as VOO and VTI. They make investing easy and relatively low risk because you don’t have to pick and choose individual stocks. You just buy up a diversified group of assets without having to do much thinking or deliberating. VOO is my favorite because in one swoop I can buy all 500 of the top companies in the US weighted on market cap! However, when it comes to cryptocurrencies this convenience doesn’t exist. I’m forced to pick and choose which individual coins I want to own and I have to buy them separately. Because of this, I set out to mimic the idea behind an ETF for cryptocurrencies by building a program that will automatically buy the top 10 cryptocurrencies weighted by market cap on a regular basis.

Note: This explanation assumes you already have API access to both Coinmarketcap and Coinbase Pro. I will not be going over generating these API keys but it is easily Google-able.

You can find the entire script at the bottom of this article.

Gathering Market Data

I used CoinmarketCap’s API to gather data on all the top cryptocurrencies in descending order of market cap. But even though the first 10 results are technically the top 10 cryptos by market cap, I don’t actually want to buy all of these coins. Bitcoin and Ethereum make up an overwhelming majority of the total market cap, currently something like 60% at the time of this writing. I decided to exclude Bitcoin and Ethereum so they wouldn’t dominate my new index. I make separate, regular purchases of both of those anyway. I also decided to exclude any stablecoins (cryptocurrenies that are pegged to another asset like the USD or gold) from the index as well. From there it was just a matter of looping through each cryptocurrency on the descending order list, checking if it is available to purchase on CoinbasePro, verifying it was not on my exclusion list, and appending it to my final index until I had 10 total entries. At the time of this writing, here was my list:

ADA-USD, SOL-USD, XRP-USD, DOT-USD, DOGE-USD, AVAX-USD, MATIC-USD, SHIB-USD, CRO-USD, DAI-USD

Along with collecting the 10 ticker symbols, I also collected each of their respective market caps. I then took those market caps, summed them up to get a total market cap, and then divided each of their respective market caps by this new sum. This gives the percentage each coin holds of the total market cap:

[0.1814675292572192, 0.16734747490789195, 0.14977539432039758, 0.09711480515652574, 0.09440821221936758, 0.0885413337393536, 0.0602569231455905, 0.059124324765594524, 0.052751600711518146, 0.04921240177654113]

Making the Purchases

The three pieces I needed to make a purchase were the ticker symbol, the percentage of total market cap, and the total dollar amount I wanted to spend, which is a user input at the beginning of the script.

I created an API key for my CoinbasePro account and used a library called cbpro to connect via Python. cpbro makes it really easy to connect to your CoinbasePro account and make purchases (as well as many other cool things like checking account balances, selling assets, etc).

I multiplied each market cap percentage by the total amount of USD I wanted to spend on the transaction, then rounded the values down to avoid over purchasing due to any fluctuations in the market caps or fees. For a $500 transaction, this gave me my final purchase amount for each coin:

[90, 83, 74, 48, 47, 44, 30, 29, 26, 24]

From there I just created a for loop to purchase each ticker symbol with the specified purchase amount calculated above!

Conclusion

This script has brought me peace of mind when it comes to buying cryptocurrencies. I no longer have deliberate on which coins to buy and how much to buy of each. I just enter a total transaction amount and let the script do all the work.

This script can also be easily manipulated to buy not just the top 10 coins, but the top 20, or 50, or 100 as well as add or remove specific coins from the exclusion list based on your preferences. It would also be relatively simple to hard code in a transaction amount and wrap this script in a while loop or use task scheduler to have it run weekly, monthly, or quarterly.

Script