Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Melvillian/coinbase_dca

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

35 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Dollar Cost Averaging Bot

A simple DCA (dollar cost averaging) bot that uses the Coinbase Python SDK to executes a sell of a product (e.g. "BTC-USD") for some amount of USD (e.g. $500). It is expected that you will run this in some sort of cron job so that it runs every day (or whatever interval you want).

Getting Started

Creating Secure Coinbase API Key

Create an account on Coinbase and create an API key and secret, which you can do here. Make sure you generate a "Trading Key", not a "General Key", because Trading Keys are the new authentication system that Coinbase uses.

You can provide an IP address (or IP address range) from which this script is allowed to execute. You have two options, 1 much easier but less secure, and 2 secure but much harder:

  1. Don't set a value, which will allow any IP address to access the API. Obviously if your API key is discovered by attackers (because you commit it to a public repo, or your local machine is compromised) you could have all of your coin sold for some shitcoin. So beware of this option.

  2. Setup a VPN with a static IP and run your script from there. I don't do this because it's complicated and I secure my local machine well enough

Setup Envvars

Run cp .env.example .env and set all variables to your desired values.

Install Poetry

If you don't have Poetry installed, you can do that here. Then, run poetry install

Execute the Trade

poetry run main

This will execute the buy or sell for the specified dollar amount, based on your .env variables.

Building A Single Binary Executable, Suitable For A Cron Job (Linux) or LaunchD (Mac) to Execute Periodically

On its own this script cannot implement DCA, because to correctly do DCA you need to sell/buy at a fixed interval (such as once a day) over a long time period. In order to make that as easy as possible, we can build a single binary executable that can be run directly by a cron job (or Mac's equivalent, launchd).

Build Steps

  1. Install pyinstaller with pip install pyinstaller. We'll use this to bundle main.py with python3 so that you have a single executable main function that a cron job can easily execute.

  2. From the project root, run pyinstaller --add-data=".env:." --onefile coinbase_dca/main.py. This will create a single executable at dist/main, which you can run from anywhere using /path/to/dist/main.

  3. Finally, execute your buy/sell periodically using either

    • cron if on Linux
    • launchd if you're using a Mac
      • tl;dr: make a plist file, and then run launchctl bootstrap gui/$(id -u) ~/Library/LaunchAgents/your.plist && launchctl enable gui/$(id -u) com.alex.coinbase_dca. The script will now run according to the schedule set in your plist.
      • When you want to update your Python code, simply run Step 2 above which will create an updated main executable that the launchctl script will run
      • When you want to update your .plist (for example, if you want to change the schedule) simply update and save your .plist file, then run the bootstrap + enable command from a couple steps above
      • When you want to disable the script (because you want to stop buying/selling), run launchctl bootout gui/$(id -u) ~/Library/LaunchAgents/your.plist
    • 🤷 if you're on Windows

About

Dollar Cost Averaging bot using Coinbase pro

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 2

  •  
  •  

Languages