Save on Coinbase fees by using simple Coinbase Pro API
Background
Being a cryptocurrency HODL investor, it is tough to time any buys. So I generally perform DCA(Dollar-cost averaging) at frequent intervals in Coinbase. However, Coinbase charges a lot of fees on trades. In this article, I will show to use Coinbase pro API and avoid a lot of charges.
Coinbase vs. Coinbase pro
Coinbase is a well-known cryptocurrency exchange owned by Coinbase Global Inc. Both let you use fiat money (such as U.S. dollars or Euro) to buy various currencies. Also, it is one of the most trusted platforms in the crypto space.
Beginner investors appreciate the simplicity of Coinbase, whereas seasoned users like the advanced options Coinbase Pro provides. Below is a quick comparison.
There are a lot of other features which can be compared. But I wanted to focus on recurring buys. Coinbase has recurring purchases as a functionality, but Coinbase Pro(CP) does not have it. It is one of the basic ones which I am looking for in CP.
I have been using recurring buys for more than two years. By calculating the trades, it seems I have paid a lot of fees on Coinbase. Finally, I decided to wake up and do something about it. Use Coinbase pro API and automate my buys to save my charges.
Tools used
Before getting started, we need to create a Coinbase pro API key, which can be created here.
- Make a note of the passphrase, secret, and API key.
2. We also need a Coinbase pro official python package.
pip install cbpro
3. Finally, we needed airflow, which is detailed in the below article.
Or cloud function such as lambda can be used to schedule the execution.
Buy using API
Below are the steps we have to follow to buy
- Connect with the CP API
- Deposit cash from the account
- Buy your favorite crypto
- Check balance after trade
- Schedule the trade
Connect the CP API
CP has two connections, one is authenticated client, and the other is a public client. The public client can provide quotes, history about BTC, ETH, etc.
auth_client = cbpro.AuthenticatedClient(KEY, SECRET, PASSPHRASE)public_client = cbpro.PublicClient()
Deposit cash from the account
As a next step, we need to add cash into the account if required. To find this ID, you need to perform the get_payment_methods method and select the ID of a suitable payment method that you have created in CP.
#Check and get the ID
auth_client.get_payment_methods()# Deposit the amount into account
auth_client.deposit(amt_to_buy, 'USD','xxxxxxxxxxxxxx')
You can be creative and add your logic here. Once we execute the deposit command, we need to give ~20 secs to settle down.
Buy your favorite crypto.
Here is your chance to buy your crypto using API. To buy crypto, we need to know what our ticker is and the amount to buy. There is a limit and market order.
# product full name is example: DOGE-USDauth_client.place_market_order(product_full_nm, 'buy', funds=amt_to_buy)
We can create a machine learning model or create our own rules before we buy it. Also, give ~20 secs to settle down. I have two types of model,
- Simple rule — if the 24 hour % is less than 5%, then buy
- Model rule — Use ML model to buy based on prediction
As a HODL investor, I generally do not sell. If I sell, I will do it manually. However, the same logic can be applied to sell orders as well. Would you please check the below article to go into detail about using an AI model with rules
Check balance after the trade.
We need to check the balance of the account after trade to make sure the transaction is complete.
balance_after = auth_client.get_account(account_id= get_account_id)['available']if balance_after > balance_before:
print("success")
Schedule the trade
We need to wrap it into a function. We have two options to schedule
- Schedule the function as a lambda function
- Put the function into an airflow
I have chosen to put it in an airflow. Here is my python operator call.
Yes, the verdict is out. I have also been buying DOGE along with my other favorites.
Closing thoughts
If you have a little bit of technical knowledge, it is easy to save a lot on Coinbase fees. It is ideal for DCA and a HODL type investor.
- We have seen how to connect to Coinbase Pro API.
- Perform trades using Coinbase Pro API.
- Schedule our trades using cloud function or airflow.
References:
Get Code
Please subscribe to my newsletter to get the complete working code for my articles and other updates.