DIY: Code a Crypto Portfolio Manager
Code your portfolio manager

DIY: Code a Crypto Portfolio Manager

By Creepto | Creepto on Crypto | 22 May 2020


The Problem

If you own more than one crypto coin, you probably check the value of your entire portfolio several times a day. There are several apps, web sites, and services out there, that would gladly manage your portfolio for you, if you:

  1. Use their services exclusively or,
  2. Share the contents of your portfolio with them

Since privacy is a value ingrained in many crypto users, sharing our entire portfolio with a single entity – whose privacy agreement we didn’t read (admit it!) – is intimidating.

What should you do if you just want the simple coin prices and portfolio value to be calculated locally, with no sharing involved?

The Solution: Build Your Own Portfolio Manager

In the following sections we’ll cover several ways you can create your own portfolio manager.
The code we’ll look is meant for educational purposes only. It is by no means a complete application:

  • The design is basic.
  • There’s almost no error handling.
  • There’s no input verification.
  • It lacks proper tests.

You are more than welcome to enhance this code on your own, and turn it into a fuller app.

All code in this article can be found in the GitHub repo, or can be downloaded as a single zip file.

The code is MIT licensed, meaning you can use and re-use it however you want.

No Coding Experience Needed

You do not need to be a developer to use the code covered in these articles. You can easily just download the code, update the portfolio part locally, and run it.

If you are a developer, the code would give you a good basis to start building a better portfolio management application, with more whistles and bells.

What Do You Need to Use the Code?

  • A computer/laptop (the operating system you’re using doesn’t matter).
  • A simple text editor to edit the portfolio file.
    If you have a code editor (like VS Code, Sublime, Atom etc.) it’ll be easier to edit the code, but it’s not mandatory.
  • For the HTML version, you’ll need a modern browser (Brave, Firefox, Chrome, Safari).
  • For the Python version, you’ll need Python installed.
  • For the NodeJS version, you’ll need NodeJS installed.

Functions Our Portfolio Manager Needs to Perform

  1. We need a way to capture all the coins/tokens we own.
  2. We need to capture how many units we own from each coin.
  3. We need to get the current price of each coin. We’ll use CoinGecko’s API for this step.
  4. A simple multiplication and addition will show our portfolio’s current value.
  5. Show the results.
  6. Show the date/time those results were calculated.

The Code

1.      The Portfolio File

In every language folder, your portfolio is kept in a JSON (JavaScript Object Notation) format file:

As you can see, it’s an array of objects, each with two properties: the coin ID, and the number of units you own of it.
You can get a list of all the coin IDs supported by CoinGecko API, browse to this URL, and just search for the coin you want to get its ID. Notice that IDs are all lower-case.

Portfolio in JSON

As you can see, it’s an array of objects, each with two properties: the coin ID, and the number of units you own of it.
You can get a list of all the coin IDs supported by CoinGecko API, browse to this URL, and just search for the coin you want to get its ID. Notice that IDs are all lower-case.

2.      The Code Base – 3 Approaches

After you download the code, look at the subfolders, and choose the code you feel more comfortable with. Despite being written in different languages, they produce the same results.
If you’re a non-coder, the HTML option is probably your best bet.

The HTML Approach

The HTML folder includes four files:

  1. portfolio.html – the file you’ll open in your browser.
  2. portfolio.js – the file you’ll edit.
  3. main.js – the file containing the solution code.
  4. main.css – a stylesheet file that controls the look and feel of the presented portfolio.

Edit the portfolio.js file, to reflect your portfolio. Then open the portfolio.html file in your browser. You should see a table similar to this:

HTML table

You can customize the fonts, colors, alignment etc. in the main.css stylesheet file.

The Python Approach

To use the Python code, make sure you have Python installed on your machine.
Python3 recommended, although this code should work with Python2 as well. Open a console/terminal window and type `python -V` to verify the version.

Edit the portfolio.json file to reflect your portfolio, and run the portfolio.py file `python portfolio.py`.
You should see this result in your console:

Python result

The NodeJS Approach

Ensure NodeJS is installed on your machine, or go download it from NodeJS.org – I recommend the latest version. Open a console/terminal window and type `node -v` to verify the version.

Edit the portfolio.json file to reflect your portfolio, and run the portfolio.js file `node index.js` (you can also type `npm start`). You should see this result in your console:

NodeJS result

3.      Explanation

While there are three code bases (two written in JavaScript and one in Python), the code itself is very simple, and follows the approach we listed above:

  1. The portfolio file is read. (In the HTML solution, it’s pre-included in the HTML.)
  2. The list of IDs is taken from the portfolio, and turned into a comma-separated string.
  3. Together with the currency you want to convert to, the IDs are used to construct the CoinGecko API URL.
  4. The API is called, and the results are parsed.
  5. The results are iterated through, and your portfolio units are multiplied by the prices.
  6. Total values are summarized.
  7. A result table is presented. (The HTML version is a bit more involved.)
  8. The date the portfolio was last updated is printed.

4.      Security

This is the point to explain and verify that the three code bases are safe to use. You can browse through the code and verify that:

  1. There’s a single API call, and all it contains is coin IDs, and a currency symbol (units are not sent).
  2. No personal information was collected or sent out.
  3. If you fear that your IP is somehow being collected by CoinGecko, use a VPN (always smart when browsing the web).
  4. The easiest way to verify that the HTML code does nothing more than advertised, is to open Developer Tools in your browser (CTRL+Shift+I on Windows CMD+SHIFT+I on Mac). Go to the Network pane and reload the page. You should be able to see the four files loaded, and the single API call:
    Network pane
  5. Clicking that API call line (the last one) should show you what was sent to the API, and what returned.

Summary – You Are a Crypto Portfolio Master

The sample code we reviewed here, demonstrates how easy it is to:

  1. Manage your portfolio on your own.
  2. Access API endpoints to get up-to-date pricing data.
  3. Write code that accesses APIs, with no need for any tools (other than a text editor).

You can use the code provided as is, or better yet, improve it.

Add personal flair, some color, maybe pull in more info from the CoinGecko API (like change, coin market cap, etc.). Make this app your own.

If you find any major bugs feel free to open issues, or even fix the code and suggest pull requests.

If you’d like to see more coding posts, please leave a comment. I can also accommodate other programming languages, if asked nicely 😀.

Most of all: learn, enjoy, and stay safe!


Creepto
Creepto

I'm a developer and technologist, and have tried every framework and gadget at least once. I used to blog religiously and am hoping to find my way back to writing. My current passion is getting "laypeople" to appreciate and use crypto products.


Creepto on Crypto
Creepto on Crypto

Detailing the adventures of a techie into the wonderful and terrifying world of crypto products. Let's explore exchanges, staking, interest earning, and smart contracts together.

Send a $0.01 microtip in crypto to the author, and earn yourself as you read!

20% to author / 80% to me.
We pay the tips from our rewards pool.