img

How to Setup Your Crypto Trading Bot for Coinbase with Python

img
valuezone 10 November 2022

How to Setup Your Crypto Trading Bot for Coinbase with Python

Welcome to my new series on automated trading using Coinbase and Python 3.

Automated trading can be a lot of fun. It combines quantitative analysis, scripting, and pattern identification disciplines to develop various strategies. When you find a solid strategy it can also be incredibly lucrative!

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 MediumLinkedIn, 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 a websiteTwitter, and DiscordNote. All trading is at your own risk. Do your own research.

In This Episode

By the end of this episode, you’ll have set up your project for the rest of the series. You’ll learn how to import your sensitive settings in a reasonably secure manner.

Project Setup

An effective code base is extensible, manageable, and easily upgradeable. This is particularly important for an algorithmic trading bot as it enables you to rapidly add new indicators, strategies, and quantitative analysis outcomes to your bot.

A common way to section a code base is through functional areas. This allows you to group similar types of code functionality together, making it easier for you to manage.

This is the approach we’ll be using for this series. To get started, create a series of folders in your project titled as follows:

  • indicators. This contains all calculations for indicators
  • coinbase_lib. This contains all direct interfacing with Coinbase, including the Coinbase Pro API
  • strategies. This contains functionality for running your strategies

By the way, if you’re looking for a great IDE, I use Pycharm. It’s an excellent IDE, has great code completion, and will help you keep your code nice and PEP8 compliant. The Community Edition is free if you’re not using it for commercial purposes.

Next, create a file called main.py in your folder. In this file add the following:

# Press the green button in the gutter to run the script.
if __name__ == '__main__':
print("Welcome to Python Trading Bot")
view rawmain.py hosted with ❤ by GitHub
Initial main.py for How to Setup Your Crypto Trading Bot for Coinbase with Python

Login Details

Any live trading requires you to interface with an exchange. Even if it’s an Automatic Money Maker like PancakeSwap, you’ll still need to somehow interact with a live capital market.

Inevitably, these exchanges require a credential exchange. This allows the exchange to verify that you (or your trading bot) are who you say you are and are authorized to do what you want to do. Your trading bot will require these credentials, and we need to store them in a safe manner.

Safe storage of credentials can be done through several methods. You can use a credential storage system like Google Cloud Secret Manager or use environmental variables.

For our project, we want a way that is reasonably secure and easily extensible. To achieve this, we’ll set up a .json file, then import the settings into our program at program startup. This method means that the only time our credentials are exposed is:

  • On disk (you will need to save your credentials somewhere)
  • In memory, while the program is operating
  • Over the wire in an HTTPS call

While not foolproof, this approach offers a reasonable balance between security and functionality. It is suitable for at-home personal use (although if you’re dealing with large amounts of money or commercial applications I’d recommend a different solution).

Let’s do this now:

  • Create a file called settings.json on disk somewhere
  • Immediately add settings.json to your .gitignore file (if using Git or some other VCS)
  • Go back to main.py and add a function to import the settings and a variable with the settings.json file location. Here’s what main.py should look like:
  • import json
    import os
    # Variable for the location of settings.json
    import_filepath = "settings.json"
    # Function to import settings from settings.json
    def get_project_settings(importFilepath):
    # Test the filepath to sure it exists
    if os.path.exists(importFilepath):
    # Open the file
    f = open(importFilepath, "r")
    # Get the information from file
    project_settings = json.load(f)
    # Close the file
    f.close()
    # Return project settings to program
    return project_settings
    else:
    return ImportError
    # Press the green button in the gutter to run the script.
    if __name__ == '__main__':
    # Import project settings
    project_settings = get_project_settings(import_filepath)
    print(project_settings)
    view rawmain.py hosted with ❤ by GitHub
Main.py for How to Setup Your Crypto Trading Bot for Coinbase with Python

Next Up: Connect to Coinbase

It might not seem like much, but you’ve made a solid start on your project! Nice work.

In the next episode, I’ll show you how to connect to Coinbase and start retrieving market data. See you there.