avatarJames Hinton

Summary

The web content outlines a comprehensive guide on building a Crypto Trading Bot for Coinbase using Python 3, including steps to connect to the Coinbase Pro API, retrieve account details, and gather candlestick data for trading analysis.

Abstract

The provided web content is part of a series that guides readers through the process of creating a cryptocurrency trading bot using Python 3 to interface with Coinbase's trading platform. The author, James Hinton, details the necessary steps to establish a connection with Coinbase Pro's API, outlines how to retrieve and interpret live account information, and explains the method for collecting and normalizing candlestick data essential for implementing trading strategies such as the Engulfing Candle Strategy. The article emphasizes the importance of understanding Coinbase's API documentation and provides code snippets and instructions for setting up the trading bot. Additionally, the author introduces Tradeoxy, a new platform aimed at simplifying the process of algorithmic trading, and offers insights into the pros and cons of Coinbase's API compared to competitors like Binance.

Opinions

  • The author believes that Coinbase's API, despite some limitations, is robust and sufficient for the purposes of building a trading bot.
  • James Hinton values the ease of use and the potential for institutional-grade services that Coinbase offers, noting its clean user interface and reasonably featured REST API.
  • The author acknowledges the complexity and potential confusion in Coinbase's API documentation and the process of obtaining API keys, providing a step-by-step guide to navigate these challenges.
  • Hinton has identified a community need for simplified installation and configuration in algorithmic trading, leading to the creation of Tradeoxy, a platform designed to make trading bots more accessible to everyone.
  • The author encourages reader engagement and feedback, inviting readers to join the Tradeoxy early access program and to follow the project's progress on various social media platforms.
  • Hinton provides a referral link for Coinbase sign-up, indicating a potential affiliate relationship with the exchange.
  • The series is aimed at both novice and experienced traders, with the author suggesting that new traders might benefit from using crypto trading bots or engaging in copy trading.
Coinbase Crypto Trading Bot Image

Coinbase Crypto Trading Bot

How to Connect to Coinbase with Python 3

Keen to learn how to connect your Python 3 code to Coinbase? You’ve come to exactly the right place!

If you’ve been following my series on building a Coinbase Crypto Trading Bot, welcome back!

Let’s get into it.

About the Series

In this series, I show you how to build your own crypto trading bot to detect a market signal.

The series uses Python 3 to connect to Coinbase, then implements a modified version of the well-known Engulfing Candle Trading Strategy. Throughout the project, I show you how to set up your project in a way that makes it easy to expand with your own ideas and strategies in the future.

All code for the series can be found on the project GitHub. Each episode has working code samples you can copy and paste straight into your own crypto trading bot. I only ask that you throw me a shoutout and a follow on Medium, LinkedIn, or Twitter.

To help you check out your code, I’ve created several locations where the output from this tutorial is posted. The code checks the “BTC-USDC” trading pair and can be found in the #coinbase and #strategyone areas. Check it out on my website, Twitter, and Discord. Note. All trading is at your own risk. Do your own research.

In This Episode

By the end of this episode, you’ll have successfully connected to Coinbase using their Coinbase Pro API. You’ll be able to retrieve your balance and the candlesticks for “BTC-USDC”.

Update August 2023

I could never have anticipated how popular this series, along with a YouTube channel would become.

After a listening to my readers/viewers feedback, I realized that many people were spending huge amounts of time trying to solve installation/configuration problems — rather than experiencing the joy of algorithmic trading.

I want to change this narrative — and in so doing, open up algorithmic trading for everyone.

To do this, I’ve recently launched Tradeoxy: Trading For Everyone.

If you’re reading this, Tradeoxy will simplify 90% of this series, using a series of powerful API’s and easy to use tooling.

Currently it’s in the early-access / building stage and I’d be incredibly grateful if you’d join us on this adventure. Your feedback will help us shape a better product.

Join the early access program (for free) here.

You can also view our launch video, and follow our journey on LinkedIn, Twitter, Instagram, YouTube.

About Coinbase

Coinbase is one of the world’s largest centralized crypto exchanges. In many ways, it is a US-based competitor to Binance.

My observations of Coinbase are as follows:

Pros:

  • High focus on developing institutional-grade services for the crypto industry
  • Clean UI
  • Reasonably featured REST API
  • Sandbox trading area (for testing)

Cons:

  • API Documentation is somewhat confusing
  • REST API is limited compared to Binance (10 requests per second, only retrieves 300 candles at a time)
  • Confusing permissions model
  • Limited trading options — doesn’t include STOP LOSS or TAKE PROFIT options for orders (or I just haven’t found it, reference my ‘confusing API documentation note)

Regardless, it’s still an impressive API set and more than sufficient for our purposes.

By the way, if you haven’t signed up for Coinbase, feel free to use my referral link.

Sign Up

Signing up for Coinbase is relatively straightforward. You’ll need to go through some standard KYC (Know Your Customer) requirements. Follow the steps here.

API Keys

Getting API Keys

I’ll be honest. Signing up for your API Keys with Coinbase can be a bit confusing. Especially when you’re looking to do it as an individual, rather than an institution. With all the options for Coinbase Pro, Coinbase Prime and so many more, it really can be quite a challenge.

I eventually narrowed it down to the following steps:

  1. Head to your settings and find the API section (link to API section here)
  2. Sign in if not already
  3. Select ‘New API Key’
  4. Select the accounts you want to apply it to
  5. For the permissions, we need the following: wallet:accounts:read, wallet:buys:create, wallet:orders:create, wallet:sells:create, wallet:sells:read, wallet:trades:create, wallet:trades:read, wallet:transactions:read, wallet:user:read
  6. Make sure you save the following somewhere secure: api_key, api_secret

It’s also worth having a look at your security settings to make sure you’re happy with them.

Add API Keys to the Project

Let’s get these keys into our project. Update the settings.json file outlined in episode 1 so that it looks like the following (replace the key strings with the relevant keys):

Getting Data From Coinbase

Getting data from Coinbase can be done using their official Python library and a little bit of code from us.

Account Details

Let’s retrieve some details about our Coinbase account. This will demonstrate that we can:

  • Connect successfully to Coinbase
  • Retrieve live information

To do this, navigate to the folder coinbase_lib created in episode 1, and create a file get_account_details.py. If you’re joining without having completed episode 1, simply create the file.

In this file, add the following code:

Now, update main.py to retrieve the account details and print them to the screen:

Very nice. If you press ‘Play’ on your IDE now, you should get a nice long printout of information about your account!

Retrieve Candlestick Data

The Engulfing Candle Strategy (discussed in the next episode) is built upon candlestick analysis (also known as Klines). Therefore, our trading bot needs a way to retrieve and format this data.

The specific API call and returned format are found on Coinbase Exchange REST API page under ‘Get product candles’.

As with every exchange I’ve worked with, Coinbase has placed its own slant on the API data format, meaning our code will need to normalize this data. I’ve chosen to normalize my data gathering so that it matches my other tutorials (MetaTrader 5 and Binance), however, feel free to add your own twist.

The process we go through to retrieve and normalize the data is as follows:

  1. Convert a timeframe string into the number matching Coinbase API
  2. Retrieve the raw data with the maximum number of candles (300 for Coinbase, compared to 1000 with Binance)
  3. Iterate through the data and convert each candle into a dictionary with the fields symbol, time, low, high, open, close, volume, timeframe
  4. Append candle dictionary to an array called candlestick_data
  5. Convert candlestick_data into a Python Pandas Dataframe
  6. Return the dataframe

Here’s the code for retrieving the candlesticks:

If you jump back to your __main__ function and update it as below, you’ll be able to print out a formatted Dataframe of 300 candles of the BTC-USDT trading pair for the daily candle ❤

Win!!!

What’s Next

With our data gathering and normalization sorted, it’s now time to learn how to detect Engulfing Candles.

Check out my next episode to do just that!

List of Episodes

  1. How to Setup Your Crypto Trading Bot
  2. How to Connect to Coinbase with Python 3
  3. How to identify Engulfing Candles on Coinbase with Python
  4. How to Use the Engulfing Candle Strategy on Coinbase with Python

About Me

I love writing these How-To Guides, and I love hearing from my readers. Support me by using my referral link to sign up for Medium (only works if you sign up from a desktop) or drop me a note in the comments.

New to trading? Try crypto trading bots or copy trading

Coinbase
Algorithmic Trading
Python
Cryptocurrency
Crypto Exchange
Recommended from ReadMedium