Tracking Your Cryptocurrency Portfolio With Serverless Functions
Monitor your Bitcoin hoard with IBM Cloud Functions writing data to Cloudant
The value of crypto-currencies can vary greatly over time. If you’re lucky, some coins bought for a handful of dollars a few years ago can be worth thousands of dollars now.
There are many websites and apps that can help you monitor your portfolio, but what’s the fun in using a ready-made solution when you can build your own?!
Crypto pricing API
We can use an API from CryptoCompare to get the latest price of Bitcoin and Ether, two popular coins, in US dollars. We can call the API passing the symbols
USD as parameters. For example:
For a single currency, we can use an even simpler call:
Retrieving the data every minute
We can write a script to fetch this data every minute, writing a document to a Cloudant database each time.
The Node.js script to do this is very simple:
main function expects the Cloudant URL to be supplied as the
url key in its
args parameter. It calls the API and creates a JSON document which is written to Cloudant:
_id is the unique key for each Cloudant document. Here, it is actually a timestamp (the number of milliseconds since 1970) which will be unique and in time order for our "once per minute" polling of the API.
All we need now is to call the “main” function every minute.
We have a number of options here:
setIntervaltimer to call the
mainfunction every 60000 milliseconds.
- We leave the script doing a single execution as it does now and call it every minute using cron — a task scheduler found on most Unix-like systems.
- We could push our script to IBM Cloud Functions, IBM’s serverless platform, and instruct it to run it for us every minute.
We put the URL of our Cloudant service in the environment variable
CLOUDANT_URL and run a series of commands to deploy our Node.js file to IBM Cloud Functions:
The first two commands deploy the code, the last three trigger the code to be run every minute using the alarm package.
Once deployed, we should see the documents appearing in our Cloudant
Querying the data
Once we’ve left our data collection script running for a while, we can query the data using Cloudant’s API. Our data is already sorted in time order because we used a timestamp as the key
We can therefore get the first two hours of data:
The most recent hour of data:
The most recent data point:
Aggregating the data
We can use a function like this:
Here’s what it does:
- turn the
_idfield into an integer
- extract the year, month, day, hour, and minute
emita key/value pair where the key is the array
[year, month, day, hour, minute]and the value is the bitcoin price
Combined with the built-in
_stats reducer, we can use this index to create hierarchical aggregations of the bitcoin price:
The above query asks for all of the data grouped by year and month. Notice how the
_stats reducer gives us a sum/count/sumsqr (which we can use to calculate mean, variance, and standard deviation) and the maximum and minmum value per month. The
group_level parameter indicates how many levels into the key we wish to aggregate. For example:
We can also specify a
endkey to limit range of data we want to report on:
The above command asks for year/month/day data for the month of October 2017.
We could do more than just record the data. We could calculate technical indicators and use them to predict whether the market is heading upwards or downwards. We could send emails or alerts when certain market conditions prevail. We could write an algorithm that decides when it’s a good time to buy or sell and call a third-party API to actually make crypto-currency trades.
I’m not that brave, so I wrote a trading simulator that allows you to simulate crypto-currency trading using the last 2 hours of bitcoin data. You start with $5000 of cash and $5000 of bitcoin. Trade as many times as you like and see if you end up with more than $10000 at the end of the game.
Try it for yourself: https://glynnbird.github.io/markettrader/public/index.html . You can also find the code on GitHub at https://github.com/glynnbird/markettrader — cheers!