This post goes through the process of collecting and storing bitcoin price data using python, crontab, and a Raspberry Pi.
Collecting Bitcoin Price Data using Coinbase
To gather the price of bitcoin we are going to use Coinbase as our bitcoin price provider. The reason for this is they provide a free REST API to collect the price of bitcoin.
I am going to use python 3 to access this API and log the current price of bitcoin every minute. This information will be logged to a file which I can then preprocess to store and display the data in different ways. I will run this on a Raspberry Pi using its crontab facility.
Accessing Coinbase API
The Coinbase API allows for a number of metrics including buy, sell and spot price. I am going to be downloading the spot price of bitcoin for this post.
The API for spot price uses the URL
Here there are two parameters that we can set:
- coin – This is set to whatever coin you want to retrieve the currency for. I am going to set this to BTC (Bitcoin)
- currency – This will return the value of the coin in the currency chosen. Bitcoin is typically measured in United States Dollars so I will set this to USD
By setting the URL to
https://api.coinbase.com/v2/prices/BTC-USD/spot I will be able to retrieve the current price for bitcoin in United States Dollars.
Storing Bitcoin data over time
Using this API I can get the bitcoin data in python using the following code.
with urllib.request.urlopen("https://api.coinbase.com/v2/prices/BTC-USD/spot".format(coin=coin, currency=currency)) as response:
html = response.read().decode('utf-8')
current_data = json.loads(html)
Here I load up the bitcoin price data and then load it into a dict using
json.loads. I have to decode the string returned from
response.read() as this is received as bytes and
json.loads requires a string. These three lines of code require importing the
Now we have the data we want to add the current time to it. I do this using
current_data['time'] = time.time()
Similar to above this requires that the
time module is imported. Now I have the data I want to log I can write this to a file.
log_filename = os.path.join(os.path.dirname(os.path.realpath(__file__)), "BTC-USD-spot-price.log")
with open(log_filename, "a") as logfile:
logfile.write(json.dumps(current_data) + "\n")
The final three lines of code open a file called
BTC-USD-spot-price.log in append mode in the directory of the file. This means that anything written to it will be added to the end of the file.
I use the
json module again to save the dict holding the data to the file and add a new line. This means that each json dump will be on a new line for easy processing.
Setting up crontab to run the data collection
To run a program at a regular interval you can use crontab. This can be edited by running
crontab -e. To run a program every minute you can enter the following crontab.
* * * * * python3 /dev/get_prices.py
The stars indicate that it should run every minute, of every hour, of every day of the month, every month of the year, and every day of the week. This is how you use the crontab format to specify it runs every minute.
Now my program will run every minute on my Raspberry Pi so I will have a decent amount of collected data to review later.
Future work with Bitcoin data
Now I have a Raspberry Pi recording the price of Bitcoin over time I will be able to do some processing in the future. I am going to look at graphing this data and to visually see the changes over time.
In addition to graphing the price data I am going to analyse the different ways of storing this price data. Comparing the speed of retrieval and size of the data.