I started investing in cryptocurrencies a while back. Nothing more than pocket change in bitcoin. But along the way, I started contributing a little more and a little more. The problem was I didn’t know if I was making any money off of my cryptocurrency purchases or how much. I had a jumble of purchases from Coinbase, Gemini, and now Robinhood. They were all at different prices - following dips, drips and sometimes spikes.
The actual exchanges themselves had very little reporting available and the only software service I could find seemed to evaluate my earnings incorrectly. I thought to take matters into my own hands.
I write financial reporting software for a living, so at least I knew how to calculate returns and basic programming. So, I set off in search of how to get my data and calculate my own returns.
The first step was getting the data from the exchanges. I started with Coinbase for no other reason than my google search showed the API for them first. The first thing you have to do when using the Coinbase API is to get yourself an API key and a secret passphrase form Coinbase. If you have an account, that’s no problem.
Now be prepared that you need to write down two specific pieces of information you will get during this process. Your API key and your API secret passphrase.
The steps are to select your profile and appropriate permissions, get an API key and a passphrase, and entering in your 2-Factor Authentication code
First, go to Settings and select the API tab. Next, select the New API button. You may be prompted to provide2-Factor Authentication. So, do this and proceed to the next step, selecting permissions. I selected the following based off of some research I did and how I wanted to use the API.
wallet:accounts:read wallet:buys:read wallet:contacts:read wallet:deposits:read wallet:notifications:read wallet:orders:read wallet:sells:read wallet:trades:read wallet:transactions:read wallet:user:read wallet:withdrawals:read
A box will appear and you will see an API key and API secret code. You want to make a note of it. You will need to enter it in your code later. OK. That wraps up the pre-requisites. Now how do you get your data?
In order to access the API from python, you have to import a library into your python script. The coinbase 2.1.0 library from Pypi.org will do the trick. So, go to the command prompt and install it.
pip install coinbase
Great. you’ve installed the Coinbase API library on your machine and can use it to get data from Coinbase. Go ahead and start a new python script and include this line of code in the beginning to import the Coinbase library
from coinbase.wallet.client import Client
After that you want to tell the Coinbase API what your keys are and your secret. The third line below connects to the API with the two vital pieces of information.
# Connect to coinbase
coinbase_API_key = "your key"
coinbase_API_secret= "your secret"
client = Client(coinbase_API_key, coinbase_API_secret)
OK. This will allow you to connect to the API when you run your program. But how about getting the information? We will start with something super simple in this short article to get you up and running. Let’s just show the current price of bitcoin.
First you set the currency code that you want the price displayed in. We will choose ‘USD’ for the US dollar.
currency_code = 'USD'
Get the price of BTC (bitcoin). BTC is the default coin so we don’t have to provide any more info than a call to the get_spot_price method.
price = client.get_spot_price(currency = currency_code)
The price object now has the current bitcoin price in its amount field. So let’s go get it.
print(price.amount)
That’s it. Let it rip and Coinbase just provided you back the current price for bitcoin.
In this article we showed you how to get your API key and passphrase from Coinbase. We then showed you how to connect to the API. Finally, we wrapped it up by getting some information from the API and printing it out to the screen.
I hope this helped you get yourself connected to Coinbase and maybe even peaked your curiosity and now you want to learn more. You can look forward to more of my articles as I walk through the process of getting your transactions from Coinbase and calculating those returns. If you just want to explore on your own, you can start at the documentation site.