Table of Contents
The Why
I, like many people, have purchased a bit of cryptocurrency. I have also dipped my toe into the world of personal finance, so, naturally I’ve wondered about the current value of cryptocurrency in my family’s investment portfolio.
Starting with a Manual Process
Answering this question initially involved a manual process in Google Sheets. I would visit CoinMarketCap to copy the USD spot price of a particular cryptocurrency and plug it into a spreadsheet. After using the spreadsheet to calculate our small slice of crypto, I would take all new calculated values and save them as a historical snapshot. Here’s what this looks like (with dummy data):
At the top, is a chart containing the original amount paid for a given cryptocurrency in addition to the quantity purchased. By multiplying the USD spot price by the quantity owned, I can find out the current value and calculate any gains or losses of the investment. The bottom portion of the sheet tracks the current value over time so that I can see how individual investments compare to one another.
I quickly learned two things. The result of this time-consuming process is worthwhile, but my time is better spent elsewhere.
Now, about the Automated Process
It turns out that CoinMarketCap has a well-documented API, so I spent time learning about it along with Google Apps Script to find out how these two can do all the legwork that I no longer felt like doing.
Fetching Crypto Data
First thing to do was to pull price data in via an API call to CoinMarketCap. That was done as follows:
|
|
Next was to figure out how to get the script to do two things:
- Calculate the new value of each cryptocurrency investment
- Take a snapshot of the value for that day so that it can be viewed over time
Calculating the Current Value of Crypto
The script uses a few constants in order to accurately update the table data at the top of the sheet. It then loops over each cryptocurrency, fetches its current price from the json data, and uses the updated price to calculate its updated value. Once that’s done, it updates these values for each cryptocurrency in the spreadsheet.
|
|
Saving the Snapshot of Crypto Value
I’ve already done most of the legwork for the second ask of the script, as I already have the current value of each cryptocurrency. Now I just need to append these values to the right spot. The chart visually capturing historical data expects the data format to be as follows: date, cryptoVal1, cryptoVal2, etc… and that’s what the script delivers.
|
|
If you’re curious as to what todaysDateAsString looks like, it looks like the following:
|
|
Woot! Now, at the push of a button, the script can do both of the things that I was previously doing manually. Additionally, it can be automated so that it doesn’t even require a button push. Time saved to go dig into some other aspect of our finances.
Helpful Resources
If you’re interested, the full script can be found as a gist here: Tracking Changes in Cryptocurrency Value.
If you find yourself on a related journey, you may also find the following resources useful: