What’s new in PyCryptoBot 2?
Automated crypto trading on Coinbase Pro, Binance and Kucoin

For those unfamiliar with my project: https://github.com/whittlem/pycryptobot
The first version of the crypto trading bot using Coinbase Pro was very successful. For those who contributed by following me on Medium, applauding my articles, crypto donations and requesting paid-for features like adding the Binance exchange, it is very much appreciated. This project is completely free to use under the Apache 2.0 license but your contributions do make a difference to cover my time and effort.
The existing version (1.0.0) of the app in the “main” branch in my repo only supports Coinbase Pro. It works well in a bull market but was tested after Elon’s tweets last week which put the market into steep decline. I realised I needed to factor in code to make sure the bot does not attempt to trade in poor conditions. The new version (2.0.0) of the app in the “binance” branch in my repo supports both Coinbase Pro and Binance. It is also more resilient to poor market conditions and will only trade in a bull market. Although my 5 bots only traded twice in the last week of disaster it still made 6% on two trades which was okay considering the market conditions. Usually each bot makes around 5% every 1–3 days if not more.
I’m planning on merging the “binance” branch into “main” on the 1st of April 2021. I will create a release branch of the current version in case anyone has problems upgrading to Python 3.9.
I’m going to assume you read my previous article and have the existing version of the bot up and running.
Prerequisites
Make sure you have upgraded to Python 3.9.x
% python3 --version
Python 3.9.1If not, this resource is really good to help with installing and upgrading depending on your platform.
In your existing bot directory change to the “binance” branch.
pycryptobot % git fetch
remote: Enumerating objects: 17, done.
remote: Counting objects: 100% (17/17), done.
remote: Compressing objects: 100% (7/7), done.
remote: Total 13 (delta 9), reused 10 (delta 6), pack-reused 0
Unpacking objects: 100% (13/13), done.
From https://github.com/whittlem/pycryptobot
a01b480..d27bdde binance -> origin/binancepycryptobot % git checkout binance
Switched to branch 'binance'
Your branch is behind 'origin/binance' by 4 commits, and can be fast-forwarded.
(use "git pull" to update your local branch)pycryptobot % git pull
Already up to date.pycryptobot % git branch
* binance
mainYou will now want to install/update dependencies.
% python3 -m pip install -r requirements.txtIf you have go this far then you are good to go…
If for any reason you get stuck and want to revert you can do this.
% git checkout main
Switched to branch 'main'
Your branch is behind 'origin/main' by 10 commits, and can be fast-forwarded.
(use "git pull" to update your local branch)% git branch
binance
* main% git pull
Already up to date.It’s also worth mentioning that if you have altered or changed my code in any way it may prevent you from doing this.
This is how you can revert to the original state.
pycryptobot % git checkout .
Updated 1 path from the indexYou should not see any modified files.
% git status
On branch main
Your branch is behind 'origin/main' by 10 commits, and can be fast-forwarded.
(use "git pull" to update your local branch)nothing to commit, working tree cleanIf you want to make absolutely sure you revert all changes you can do this.
pycryptobot % git reset --hard
HEAD is now at 19ea276 minor updatesIf all else fails you could just make a backup of your config.json, delete the bot directory, and re-clone it as per my previous article, and put the config.json back.
I’m just covering all bases here but really you should just be able to change to the “binance” branch with no issue.
Configuration
I’ve tried to make sure the version 1 and 2 bots are backward compatible. The version 2 bot should still work fine with the version 1 config.json file format.
config.json (version 1.x)
{
"api_url" : "https://api.pro.coinbase.com",
"api_key" : "00000000000000000000000000000000",
"api_secret" : "0000000000000000/000000000000000000000000000000000000000000000000000000000000000000000",
"api_pass" : "00000000000"
}You just replace the “api_key”, “api_secret” and “api_pass” with your Coinbase Pro keys which you probably have already done. This still works with version 2.
config.json (version 2.x)
{
"binance" : {
"api_url" : "https://api.binance.com",
"api_key" : "0000000000000000000000000000000000000000000000000000000000000000",
"api_secret" : "0000000000000000000000000000000000000000000000000000000000000000",
"config" : {
"base_currency" : "BTC",
"quote_currency" : "USDT",
"granularity" : "1h",
"live" : 1,
"verbose" : 0
}
},
"coinbasepro" : {
"api_url" : "https://api.pro.coinbase.com",
"api_key" : "00000000000000000000000000000000",
"api_secret" : "0000000000000000/000000000000000000000000000000000000000000000000000000000000000000000",
"api_passphrase" : "00000000000",
"config" : {
"base_currency" : "BTC",
"quote_currency" : "GBP",
"granularity" : "3600",
"live" : 1,
"verbose" : 0
}
}
}A few important points to note here.
- You don’t need to have both “coinbasepro” and “binance” configuration for the bot to work. You need at least one but can have both.
- I used to have “cryptoMarket” and “fiatMarket” in the configuration. This was fine for Coinbase Pro and what I was using the bot for initially but it’s not technically correct. Usually trading pairs are between a base currency and quote currency which may be crypto to fiat or crypto to crypto.
- I renamed “api_pass” to “api_passphrase” for “coinbasepro”.
- Coinbase Pro has a numeric granularity whereas Binance as a string granularity. For example 1 hour for Coinbase Pro is “3600” but for Binance it is “1h”.
- All the “config” in the config.json can actually be passed in with arguments as well. The “config” section is optional. If you omit it then you need to pass each one with an argument. I personally like to just start the bots without arguments and have all the settings reloaded but it’s up to you.
Just an an example, in the config.json you can include “base_currency”, “quote_currency” and “granularity” but you could also start the bot like this which would do the same thing.
python3 pycryptobot --market BTCUSDT --granularity 1hArguments and Config
I wanted to avoid all of you having to edit the code directly and wanted to make the majority of config and settings available via the config.json and arguments.
--market <market>
E.g. --market BTC-GBP, --market BTCUSDT--granularity <granularity>
E.g. --granularity 3600, --granularity 1h--verbose <int>
E.g. 0 = verbose output (default), 1 = minimal output (my pref)--live <int>
E.g. 0 = test mode (default), 1 = live mode (executes live trades)--sim <fast|fast-sample|slow|slow-sample>fast - iterates through the last 300 intervals (probably hours) of data simulating what the bot would have done
fast-sample - iterates through a random 300 intervals of data over the last 5 years
slow - same as fast but iterates each second
slow-sample - same as fast but iterates each second--sellupperpcnt <int>
E.g. 5 = forced sell at 5% margin--selllowerpcnt <int>
E.g. -2 = forced sell at -2% marginThe “sellupperpcnt” and “selllowerpcnt” are optional but they are useful. I usually set my “selllowerpcnt” to -2 in most markets. In other markets I either have no upper limit or something suitable depending on the market. Maybe for Stellar I may have 8% but for Bitcoin maybe 3%. I think it’s better to lock in the profit more regularly but it’s just a personal preference.
Confirmation it works!
This is a Coinbase Pro simulation for BTC-GBP using the 1 day charts.
pycryptobot % python3 pycryptobot.py --exchange coinbasepro --market BTC-GBP --granularity 86400 --verbose 0 --selllowerpcnt -2 --sim fast
--------------------------------------------------------------------
| Python Crypto Bot |
--------------------------------------------------------------------
| Bot Mode : TEST - test trades using dummy funds :) |
| Bot Started : 2021-03-06 21:27:36.136885 |
====================================================================
| Sell Lower : -2.0% |
====================================================================
2020-05-11 | BTC-GBP | 86400 | Close: 6970.13 | EMA12/26: 6970.13 = 6970.13 | MACD: 0.0 = 0.0 | WAIT
2020-05-12 | BTC-GBP | 86400 | Close: 7210.0 | *^ EMA12/26: 7007.03 > 6987.89 ^* | *^ MACD: 19.13 > 3.82 ^* | WAIT
2020-05-13 | BTC-GBP | 86400 | Close: 7630.0 | ^ EMA12/26: 7102.87 > 7035.46 ^ | ^ MACD: 67.41 > 16.54 ^ | WAIT
*** Candlestick Detected: Three White Soldiers ("Strong - Reversal - Bullish Pattern - Up")<reduced output for example>2021-03-05 | BTC-GBP (BULL) | 86400 | Close: 35229.57 | ^ EMA12/26: 35078.86 > 34069.19 ^ | v MACD: 1009.66 < 1481.19 v | WAIT | Last Action: BUY | 75.79%
* Candlestick Detected: Hammer ("Weak - Reversal - Bullish Signal - Up")2021-03-06 | BTC-GBP (BULL) | 86400 | Close: 34999.61 | ^ EMA12/26: 35066.67 > 34138.11 ^ | v MACD: 928.55 < 1370.66 v | WAIT | Last Action: BUY | 75.63%Simulation SummaryBuy Count : 2
Sell Count : 2Margin : 60.46%** non-live simulation, assuming highest feesThis is a Binance simulation for DOGEUSDT using 6 hour charts.
pycryptobot % python3 pycryptobot.py --exchange binance --market DOGEUSDT --granularity 6h --verbose 0 --selllowerpcnt -2 --sim fast
--------------------------------------------------------------------
| Python Crypto Bot |
--------------------------------------------------------------------
| Bot Mode : TEST - test trades using dummy funds :) |
| Bot Started : 2021-03-06 21:34:26.917574 |
====================================================================
| Sell Lower : -2.0% |
====================================================================
2020-12-22 | DOGEUSDT | 6h | Close: 0.0046182 | EMA12/26: 0.0046182 = 0.0046182 | MACD: 0.0000 = 0.0000 | WAIT
2020-12-22 06:00:00 | DOGEUSDT | 6h | Close: 0.0046399 | *^ EMA12/26: 0.00462153 > 0.0046198 ^* | *^ MACD: 0.0000 > 0.0000 ^* | WAIT
2020-12-22 12:00:00 | DOGEUSDT | 6h | Close: 0.0045713 | *v EMA12/26: 0.0046138 < 0.00461621 v* | *v MACD: -0.0000 < -0.0000 v* | WAIT
2020-12-22 18:00:00 | DOGEUSDT | 6h | Close: 0.0045159 | v EMA12/26: 0.00459874 < 0.00460878 v | v MACD: -0.0000 < -0.0000 v | WAIT
2020-12-23 | DOGEUSDT | 6h | Close: 0.0044539 | v EMA12/26: 0.00457646 < 0.00459731 v | v MACD: -0.0000 < -0.0000 v | WAIT
2020-12-23 06:00:00 | DOGEUSDT | 6h | Close: 0.0041677 | v EMA12/26: 0.00451357 < 0.00456548 v | v MACD: -0.0001 < -0.0000 v | WAIT
2020-12-23 12:00:00 | DOGEUSDT | 6h | Close: 0.00409749 | v EMA12/26: 0.00444956 < 0.00453082 v | v MACD: -0.0001 < -0.0000 v | WAIT<reduced output for example>2021-03-06 12:00:00 | DOGEUSDT (BULL) | 6h | Close: 0.05 | v EMA12/26: 0.05 < 0.05 v | ^ MACD: -0.01 > -0.01 ^ | WAIT | Last Action: SELL
2021-03-06 18:00:00 | DOGEUSDT (BULL) | 6h | Close: 0.05 | *^ EMA12/26: 0.05 > 0.05 ^* | ^ MACD: 0.0 > -0.01 ^ | BUY | Last Action: SELL2021-03-06 18:00:00 | DOGEUSDT 6h | Close: 0.05 | BUY
Fibonacci Retracement Levels: {'ratio0_5': 0.04, 'ratio0_382': 0.05}
Support level of 0.0506003 formed at 2021-02-16 18:00:00Simulation SummaryBuy Count : 3
Sell Count : 3Margin : 39.85%** non-live simulation, assuming highest feesI get at least 2–5 emails a day from all of you asking questions or help with the bot. Feel free to reach out if you like, my email address is [email protected] and I will answer when I can. I think I may write a FAQ sometime with your questions and my answers as I think others will find them interesting.
Good luck :)
*** 09/2021 UPDATE ***
PyCryptoBot version 3 is here!
If you want to catch up with the project I recommend the following my Medium publication, Trading Data Analysis.
Michael Whittle
- If you enjoyed this, please follow me on Medium
- For more interesting articles, please follow my publication
- Interested in collaborating? Let’s connect on LinkedIn
- Support me and other Medium writers by signing up here
- Please don’t forget to clap for the article :) ← Thank you!
