🔵 Getting Started with the Coinbase Advanced Trade API
About a year ago I released a Python wrapper for the Coinbase Advanced Trade API
Since then, a lot has happened
Our wrapper has grown to over 40 stars and 20 forks (legendary)
Coinbase has released their own official Python SDK
And Coinbase introduced a new authentication method for Advanced Trade
Coinbase releasing their own official SDK has been incredible, huge shout out to their development team, and I'm hoping more companies in the space follow suit.
Now that they're providing that base level infrastructure, wrappers like mine can focus on solving problems for specific audiences (in our case, primarily dollar cost averaging and augmented DCA)
What's New?
I've made two major changes to the wrapper
- New Authentication Method: the wrapper now supports the new Coinbase Cloud authentication method. This means more secure and efficient API interactions.
- Built on Official SDK: these new features are built on top of the official SDK, ensuring improved stability and compatibility with Coinbase's API. This means less maintenance and fewer issues with base Coinbase functionality.
from coinbase_advanced_trader.enhanced_rest_client import EnhancedRESTClient
client = EnhancedRESTClient(api_key=api_key, api_secret=api_secret)
# Perform a market buy
client.fiat_market_buy("BTC-USDC", "10")
# Perform a limit buy
client.fiat_limit_buy("BTC-USDC", "10")
Building on top of the official SDK has allowed us to make the code in our wrapper very concise like the above examples for creating limit orders or market orders.
These changes (among others) are live now over in the wrapper
Legacy Support
I made these changes while maintaining the structure of the wrapper for legacy authentication and existing users (if you're using the current wrapper - you don't need to upgrade)
The legacy authentication method is still supported but moved to a separate module. It will not receive the latest updates from the Coinbase SDK. To use the legacy method:
from coinbase_advanced_trader.legacy.legacy_config import set_api_credentials
from coinbase_advanced_trader.legacy.strategies.limit_order_strategies import fiat_limit_buy
legacy_key = "your_legacy_key"
legacy_secret = "your_legacy_secret"
set_api_credentials(legacy_key, legacy_secret)
# Use legacy functions
limit_buy_order = fiat_limit_buy("BTC-USDC", 10)
Cloud Support
To run this code in the cloud simply add the layer.zip that is attached to the release into your AWS cloud environment and migrate your code.
Here are the steps I used to generate a compatible layer.zip locally if you want to make your own:
virtualenv venv
source venv/bin/activate
mkdir python
echo "coinbase-advancedtrade-python" > requirements.txt
docker run --rm -v "$PWD":/var/task public.ecr.aws/sam/build-python3.9:latest /bin/sh -c "pip install --platform manylinux2014_x86_64 --implementation cp --python 3.9 --only-binary=:all: --upgrade -r requirements.txt -t python"
zip -r layer.zip python
Note: you will need Docker, VirtualEnv, and Python installed locally to use these commands. The layer this generates is only compatible with x86_64 architecture and Python 3.9
What's Next?
Next I'll be including functionality to support dollar cost averaging using the AlphaSquared risk number and their custom strategies.
If you get stuck with your development at any point, join my Discord or Coinbase's Discord and me and the other developers in there can help you debug or point you in the right direction.
Check out this video if you need help getting started with Coinbase's official SDK