8 min read

🔵 How to Automate Trades in Coinbase Advanced Trader

Background

In 2022 the largest US cryptocurrency exchange, Coinbase, announced that they would be sunsetting Coinbase Pro and transitioning the users of Coinbase Pro over to their new platform Coinbase Advanced Trader

Overall this is a positive step forward as it integrates the features of Coinbase Pro (formerly GDAX) natively into Coinbase

Unfortunately, for the institutional and retail users of the Coinbase Pro API, this means refactoring all of your code to fit within Coinbase's new /v3/ (!!) API

Coinbase is now on /v3/

For some users of more niche features available on Coinbase Pro's API, this means a loss of functionality (until Coinbase replicates the functionality in the new API, if they ever do)

Fortunately for the rest of us it just means waiting for someone to distribute working code so that we can automate our trades and continue to take advantage of Coinbase Advanced Trader's much lower fees

Luckily for you, I am that someone

The Code

Unfortunately, at the time of this writing, I haven't been able to find a Python wrapper for the new Coinbase /v3/ API

This meant coding the connection from scratch and resulted in a much longer file than what we've dealt with previously on the channel.

The upside is that we no longer need a layer.zip to run and automate this code up in AWS

Without further ado, here's the code I wrote to place limit orders (buys/sells):

Place limit orders on Coinbase Advanced Trader for just below/above the spot price for your buys/sells. Executable and automatable in AWS
Place limit orders on Coinbase Advanced Trader for just below/above the spot price for your buys/sells. Executable and automatable in AWS - cbAT_limit_orders.py

Setting Up Your API Connection

This code should work for you once you plug in a Coinbase API key and API secret that have the permissions to place the relevant trades and read from the relevant accounts

You can generate keys by going to Coinbase.com -> Profile Picture -> Settings -> API -> New API Key

Unfortunately, this isn't as straightforward as it was back on Coinbase Pro

List of Coinbase Advanced Trader Permissions

The list in Advanced Trader is massive and makes testing a complete nightmare

I recommend generating an initial API key with every permission and scaling back from there removing unnecessary permissions over time

Once you've set up your API keys, you should only need to change data in the lambda_handler function

Specifically:

1) The my_side variable that determines if you are buying (Side.BUY.name) or selling (Side.SELL.name)

2) The my_trading_pair variable "BTC-USD", "ETH-USD", "BTC-EUR", "ETH-BTC" etc (you can find the current full list by running the getAllProductInfo() function)

Available trading pair codes at time of writing

BTC-USD, ETH-USD, SOL-USD, USDT-USD, AVAX-USD, DOGE-USD, MATIC-USD, GALA-USD, SHIB-USD, ETH-USDT, BTC-USDT, ADA-USD, LINK-USD, LTC-USD, MANA-USD, ATOM-USD, AAVE-USD, ALGO-USD, ETH-EUR, SOL-USDT, DOT-USD, UNI-USD, BTC-EUR, XLM-USD, FIL-USD, FET-USD, CRV-USD, BCH-USD, APE-USD, GALA-USDT, SOL-EUR, YFI-USD, OP-USD, JASMY-USD, ICP-USD, ETC-USD, GMT-USD, NEAR-USD, QNT-USD, ETH-GBP, GRT-USD, LRC-USD, XTZ-USD, COMP-USD, APT-USD, USDT-EUR, SAND-USD, BTC-GBP, CBETH-USD, CHZ-USD, AXS-USD, YFII-USD, EOS-USD, HBAR-USD, SUSHI-USD, SNX-USD, MKR-USD, MATIC-USDT, AVAX-EUR, LDO-USD, SPELL-USD, CRO-USD, FLOW-USD, RLC-USD, DOGE-EUR, 1INCH-USD, SHIB-EUR, SOL-GBP, ANKR-USD, BAND-USD, MIR-USD, REN-USD, ADA-EUR, MASK-USD, XYO-USD, LCX-USD, IMX-USD, ZEC-USD, RNDR-USD, FIDA-USD, AVAX-USDT, KNC-USD, MANA-EUR, USDT-GBP, ZRX-USD, VGX-USD, DOGE-USDT, LTC-EUR, MATIC-EUR, ROSE-USD, ENJ-USD, OCEAN-USD, GST-USD, APE-USDT, PYR-USD, NMR-USD, MATIC-GBP, MAGIC-USD, AMP-USD, DAI-USD, ACH-USD, ORCA-USD, OMG-USD, ENS-USD, CGLD-USD, GALA-EUR, GAL-USD, CHZ-USDT, APE-EUR, TRB-USD, USDT-USDC, SYN-USD, HFT-USD, SHIB-GBP, SKL-USD, CTSI-USD, DOT-USDT, NEAR-USDT, COTI-USD, DASH-USD, DOGE-GBP, MPL-USD, MINA-USD, ADA-GBP, FET-USDT, LPT-USD, NKN-USD, BAL-USD, UMA-USD, SHIB-USDT, FARM-USD, BAT-USD, IOTX-USD, PERP-USD, ATOM-USDT, BUSD-USD, LINK-EUR, ALGO-EUR, CVX-USD, RLY-USD, XCN-USDT, WBTC-USD, DESO-USD, RBN-USD, FORTH-USD, DOT-EUR, DDX-USD, OMG-EUR, XCN-USD, JUP-USD, AAVE-EUR, KSM-USD, BOND-USD, SUSHI-EUR, LTC-GBP, NU-USD, ATOM-GBP, INJ-USD, HIGH-USD, AXS-USDT, GODS-USD, CRV-EUR, OGN-USD, MEDIA-USD, STORJ-USD, REQ-USD, PLA-USD, STG-USD, STX-USD, UNFI-USD, QSP-USD, PRQ-USD, API3-USD, ILV-USD, CLV-USD, CELR-USD, ADA-USDT, INV-USD, RARI-USD, SAND-USDT, ATOM-EUR, EGLD-USD, AAVE-GBP, DOT-GBP, GFI-USD, HBAR-USDT, CHZ-GBP, MNDE-USD, FOX-USD, SNX-EUR, ANKR-EUR, OXT-USD, HFT-USDT, ZRX-EUR, SUSHI-GBP, 00-USD, GMT-USDT, OP-USDT, XYO-USDT, GRT-EUR, TIME-USD, POLY-USD, LOOM-USD, MDT-USD, ICP-EUR, LOKA-USD, ALCX-USD, AURORA-USD, NCT-USD, WAMPL-USD, BCH-EUR, CHZ-EUR, MUSE-USD, BTRST-USD, MONA-USD, USDC-EUR, SHPING-USD, BIT-USD, SUKU-USD, BICO-USD, FLOW-USDT, LCX-USDT, KEEP-USD, MASK-GBP, SNX-GBP, FIL-EUR, COVAL-USD, DNT-USD, ICP-USDT, CLV-EUR, VGX-EUR, MSOL-USD, XLM-EUR, METIS-USD, INDEX-USD, DYP-USD, LINK-GBP, BCH-GBP, AXS-EUR, RNDR-USDT, WAXL-USD, FIL-GBP, QNT-USDT, MATH-USD, LINK-USDT, CRV-GBP, FIS-USD, EOS-EUR, ANT-USD, MIR-EUR, MLN-USD, CVC-USD, TRAC-USD, GTC-USD, ZEN-USD, ALICE-USD, ASM-USD, SPELL-USDT, SWFTC-USD, ALGO-GBP, ETH-DAI, WCFG-USD, XYO-EUR, AIOZ-USD, BAT-EUR, QI-USD, BLZ-USD, SUPER-USD, MIR-GBP, BNT-USD, RAD-USD, HOPR-USD, GRT-GBP, TONE-USD, REP-USD, ERN-USD, LQTY-USD, GAL-USDT, MXC-USD, CRO-USDT, UMA-EUR, CTX-USD, LRC-USDT, ENS-USDT, PNG-USD, ETC-EUR, UNI-EUR, C98-USD, RARE-USD, AGLD-USD, NMR-EUR, NEST-USD, KRL-USD, USDC-GBP, FORTH-EUR, FX-USD, MCO2-USD, DIA-USD, DREP-USD, ETC-GBP, VGX-USDT, BAND-GBP, QUICK-USD, TRU-USD, DDX-EUR, JASMY-USDT, ICP-GBP, KSM-USDT, ANKR-GBP, PRO-USD, SYLO-USD, XLM-USDT, DESO-USDT, IMX-USDT, GLM-USD, APT-USDT, ENS-EUR, STG-USDT, FIDA-USDT, FORT-USD, OMG-GBP, XTZ-EUR, BADGER-USD, ROSE-USDT, PERP-EUR, 1INCH-EUR, AVT-USD, LIT-USD, ALEPH-USD, BOBA-USD, MTL-USD, ORN-USD, MASK-EUR, RAD-EUR, POWR-USD, CGLD-EUR, RNDR-EUR, MASK-USDT, ARPA-USD, FORTH-GBP, PLU-USD, METIS-USDT, XTZ-GBP, RPL-USD, SKL-EUR, CRPT-USD, RLY-USDT, IDEX-USD, PERP-USDT, IOTX-EUR, POLY-USDT, DAR-USD, POND-USD, AUCTION-USD, ATA-USD, ELA-USD, ABT-USD, BADGER-EUR, AST-USD, BIT-USDT, ZEN-USDT, NMR-GBP, MINA-USDT, BTRST-USDT, BAND-EUR, SUKU-EUR, RLY-EUR, CRO-EUR, SNT-USD, 1INCH-GBP, STX-USDT, POLS-USDT, ALCX-EUR, BICO-USDT, DEXT-USD, FIS-USDT, PRQ-USDT, RAD-USDT, BNT-GBP, POLS-USD, MINA-EUR, C98-USDT, OOKI-USD, ARPA-EUR, AERGO-USD, MDT-USDT, GHST-USD, GNO-USD, ACH-USDT, GYEN-USD, CGLD-GBP, NU-EUR, REQ-EUR, CLV-GBP, BOBA-USDT, API3-USDT, ARPA-USDT, SUKU-USDT, DREP-USDT, PUNDIX-USD, NU-GBP, NCT-USDT, SUPER-USDT, KRL-USDT, REQ-USDT, FIDA-EUR, NEST-USDT, AIOZ-USDT, SYLO-USDT, UNI-GBP, QSP-USDT, ENJ-USDT, BTRST-EUR, BOND-USDT, COVAL-USDT, LCX-EUR, NCT-EUR, ALCX-USDT, NKN-GBP, RAD-GBP, TIME-USDT, UMA-GBP, WCFG-USDT, HOPR-USDT, SKL-GBP, BICO-EUR, ASM-USDT, CTX-EUR, PAX-USD, FARM-USDT, DESO-EUR, ERN-USDT, AUCTION-USDT, GUSD-USD, KRL-EUR, RLY-GBP, NKN-EUR, BNT-EUR, DYP-USDT, ELA-USDT, POWR-USDT, REQ-GBP, TRAC-USDT, BTRST-GBP, TRU-USDT, POND-USDT, POWR-EUR, MATH-USDT, WAMPL-USDT, FOX-USDT, IDEX-USDT, DDX-USDT, AUCTION-EUR, MCO2-USDT, CLV-USDT, AGLD-USDT, LQTY-USDT, SHPING-USDT, INDEX-USDT, ERN-EUR, BADGER-USDT, CTX-USDT, MEDIA-USDT, CBETH-ETH, FORT-USDT, SHPING-EUR, TRU-EUR, GNO-USDT, ORN-USDT, ETH-BTC, RAI-USD, WCFG-EUR, ATA-USDT, DIA-USDT, LQTY-EUR, TRAC-EUR, SOL-ETH, DIA-EUR, SOL-BTC, ADA-ETH, MANA-ETH, LINK-ETH, PAX-USDT, DOGE-BTC, ATOM-BTC, MATIC-BTC, AVAX-BTC, SUSHI-ETH, LTC-BTC, ADA-BTC, MANA-BTC, FIL-BTC, SUSHI-BTC, DOT-BTC, SNX-BTC, BAT-ETH, AXS-BTC, COMP-BTC, AAVE-BTC, EOS-BTC, WBTC-BTC, ZEC-BTC, UNI-BTC, CRV-BTC, ALGO-BTC, LINK-BTC, YFI-BTC, MKR-BTC, FORTH-BTC, ICP-BTC, MIR-BTC, KNC-BTC, BAL-BTC, ZRX-BTC, BCH-BTC, REN-BTC, XLM-BTC, TRB-BTC, NMR-BTC, BAND-BTC, GRT-BTC, DASH-BTC, NU-BTC, RLC-BTC, XTZ-BTC, ENJ-BTC, LRC-BTC, STORJ-BTC, XYO-BTC, ANKR-BTC, ETC-BTC, UMA-BTC, REP-BTC, OMG-BTC, BAT-BTC, CGLD-BTC, OGN-BTC, BNT-BTC, REQ-BTC, 1INCH-BTC, BTRST-BTC, ZEN-BTC, WCFG-BTC, TRU-BTC, SKL-BTC, RAD-BTC, CTSI-BTC, NKN-BTC, ORN-BTC

3) The usd_order_size variable that takes in the USD (or other base currency amount that you want to trade. A base currency is the second currency in a trading pair - EUR in BTC-EUR, BTC in EOS-BTC, ETH in BAT-ETH etc) if you're wanting to buy $X a day/week/month - you'll put that amount here

4) The factor variable sets your limit price slightly above or below the spot price (below if you are buying and above if you are selling). This way your order will be counted as a maker order from a fee perspective (because you are providing liquidity to the order book). But because the number is so close to 1 (.998 and 1.002 by default) you should get faster order execution and should rarely if ever have an order that never fills.

5) Optionally you can change my_limit_price and my_order_size to some static amount (ie. 30000.00 limit price and .0001 order size) if you want to set a static order, but I think most people will just place a limit order at whatever the current price is and will be able to leave these fields alone.

What Next and Thank You's

I should have a full YouTube tutorial out for this code soon

I plan to automate deposits and withdrawals into Advanced Trader next so subscribe to the blog for that code and the YouTube channel for the full tutorial

I was able to get this code out much faster thanks to jmicko and daghz on the Coinbase Cloud Forum - if either of you ever read this, thank you

Getting on my soap box here at the end, daghz (a "novice coder") was able to generate a connection to Advanced Trader using OpenAI's ChatGPT

Often times when I'm interacting with a new API, making the initial connection is the hardest part

Using daghz's code as a base, I was able to get limit orders working and I'll be using the same framework to tackle any new trading strategies as well as deposits and withdrawals (unless there's a completely different API Coinbase is using to process deposits and withdrawals)

The craziest part about all of this, is that ChatGPT "stopped training" on data after 2021

Coinbase Advanced Trader was released in 2022

Follow me on Twitter: https://twitter.com/RhettRe

ChatGPT is going to make it a whole lot easier for any of you who consider yourselves "novice coders" to learn to code much faster

If you want a tutorial or a blog post on how I use ChatGPT for programming, let me know

Email me at rhett@rhett.blog or dm me on Twitter @rhettre if you found this helpful or if you got stuck - I love hearing from you guys

See you next week