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).
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:
-
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.
-
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
Run cp .env.example .env and set all variables to your desired values.
If you don't have Poetry installed, you can do that here. Then, run poetry install
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).
-
Install pyinstaller with
pip install pyinstaller. We'll use this to bundlemain.pywithpython3so that you have a single executablemainfunction that a cron job can easily execute. -
From the project root, run
pyinstaller --add-data=".env:." --onefile coinbase_dca/main.py. This will create a single executable atdist/main, which you can run from anywhere using/path/to/dist/main. -
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
mainexecutable 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 + enablecommand 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
- tl;dr: make a plist file, and then run
- 🤷 if you're on Windows