Skip to content

MasaiasuOse/binance-trade-bot

 
 

Repository files navigation

binance-trade-bot

github docker

Automated cryptocurrency trading bot

Why?

This project was inspired by the observation that all cryptocurrencies pretty much behave in the same way. When one spikes, they all spike, and when one takes a dive, they all do. Pretty much. Moreover, all coins follow Bitcoin's lead; the difference is their phase offset.

So, if coins are basically oscillating with respect to each other, it seems smart to trade the rising coin for the falling coin, and then trade back when the ratio is reversed.

How?

The trading is done in the Binance market platform, which of course, does not have markets for every altcoin pair. The workaround for this is to use a bridge currency that will complement missing pairs. The default bridge currency is Tether (USDT), which is stable by design and compatible with nearly every coin on the platform.

Coin A → USDT → Coin B

The way the bot takes advantage of the observed behaviour is to always downgrade from the "strong" coin to the "weak" coin, under the assumption that at some point the tables will turn. It will then return to the original coin, ultimately holding more of it than it did originally. This is done while taking into consideration the trading fees.

Coin A → USDT → Coin B

Coin B → USDT → Coin C

...

Coin C → USDT → Coin A

The bot jumps between a configured set of coins on the condition that it does not return to a coin unless it is profitable in respect to the amount held last. This means that we will never end up having less of a certain coin. The risk is that one of the coins may freefall relative to the others all of a sudden, attracting our reverse greedy algorithm.

Binance Setup

  • Create a Binance account (Includes my referral link, I'll be super grateful if you use it).
  • Enable Two-factor Authentication.
  • Create a new API key.
  • Get a cryptocurrency. If its symbol is not in the default list, add it.

Tool Setup

Install Python dependencies

Run the following line in the terminal: pip install -r requirements.txt.

Create user configuration

Create a .cfg file named user.cfg based off .user.cfg.example, then add your API keys and current coin.

The configuration file consists of the following fields:

  • api_key - Binance API key generated in the Binance account setup stage.
  • api_secret_key - Binance secret key generated in the Binance account setup stage.
  • current_coin - This is your starting coin of choice. This should be one of the coins from your supported coin list. If you want to start from your bridge currency, leave this field empty - the bot will select a random coin from your supported coin list and buy it.
  • bridge - Your bridge currency of choice. Notice that different bridges will allow different sets of supported coins. For example, there may be a Binance particular-coin/USDT pair but no particular-coin/BUSD pair.
  • tld - 'com' or 'us', depending on your region. Default is 'com'.
  • hourToKeepScoutHistory - Controls how many hours of scouting values are kept in the database. After the amount of time specified has passed, the information will be deleted.
  • use_margin - 'true' to use scout_margin. 'false' to use scout_multiplier.
  • scout_multiplier - Controls the value by which the difference between the current state of coin ratios and previous state of ratios is multiplied. For bigger values, the bot will wait for bigger margins to arrive before making a trade.
  • scout_margin - Minimum percentage coin gain per trade. 0.8 translates to a scout multiplier of 5 at 0.1% fee.
  • strategy - The trading strategy to use. See binance_trade_bot/strategies for more information
  • scout_sleep_time - Controls how many seconds bot should wait between analysis of current prices. Since the bot now operates on websockets this value should be set to something low (like 1), the reasons to set it above 1 are when you observe high CPU usage by bot or you got api errors about requests weight limit.
  • enable_paper_trading - (true or false default False) run bot with virtual wallet to check its performance without risking any money.

Environment Variables

All of the options provided in user.cfg can also be configured using environment variables.

CURRENT_COIN_SYMBOL:
SUPPORTED_COIN_LIST: "XLM TRX ICX EOS IOTA ONT QTUM ETC ADA XMR DASH NEO ATOM DOGE VET BAT OMG BTT"
BRIDGE_SYMBOL: USDT
API_KEY: vmPUZE6mv9SD5VNHk4HlWFsOr6aKE2zvsw0MuIgwCIPy6utIco14y7Ju91duEh8A
API_SECRET_KEY: NhqPtmdSJYdKjVHjA7PZj4Mge3R5YNiP1e3UZjInClVN65XAbvqqM6A7H5fATj0j
SCOUT_MULTIPLIER: 5
SCOUT_SLEEP_TIME: 1
TLD: com
STRATEGY: default
ENABLE_PAPER_TRADING: False

Paying Fees with BNB

You can use BNB to pay for any fees on the Binance platform, which will reduce all fees by 25%. In order to support this benefit, the bot will always perform the following operations:

  • Automatically detect that you have BNB fee payment enabled.
  • Make sure that you have enough BNB in your account to pay the fee of the inspected trade.
  • Take into consideration the discount when calculating the trade threshold.

Notifications with Apprise

Apprise allows the bot to send notifications to all of the most popular notification services available such as: Telegram, Discord, Slack, Amazon SNS, Gotify, etc.

To set this up you need to create a apprise.yml file in the config directory.

There is an example version of this file to get you started.

If you are interested in running a Telegram bot, more information can be found at Telegram's official documentation.

Run

python -m binance_trade_bot

Docker

Please remember that this is a fork. To maintain the security of your API key it relies on local builds.

Build and run locally

  1. Clone this git to a location of your choice: git clone https://github.com/masaiasuose/binance-trade-bot masaiasuose-binance-trade-bot
  2. Change to the directory: cd masaiasuose-binance-trade-bot
  3. Build the container locally (this may take a few minutes depending on your hardware): docker build . -t masaiasuose-binance-trade-bot
  4. Follow the steps in Create user configuration to ensure you have created a user.cfg file in the directory created in step 2. If you have already done this, continue to step 5.
  5. Run docker-compose up docker-compose up

To update, repeat steps 1 through 5. These commands above can also be added to a shell script to automate the process.

If you only want to start the SQLite browser

docker-compose up -d sqlitebrowser

Bot-testing

You can test the bot on historic data with backtesting or with paper trading on live data to see how it performs. Backtesting is much faster but isn't as accurate as paper trading.

Backtesting

python backtest.py

Feel free to modify that file to test and compare different settings and time periods

Papertrading

You can enable paper trading via the user.cfg and change the starting amount to use with the following line in crypto_trading.py:

manager = BinanceAPIManager.create_manager_paper_trading(config, db, logger, {config.BRIDGE.symbol: 1_000.0})

Database warmup

You can warmup your database with coins wich you might want to add later to your supported coin list. This should prevent uncontrolled jumps when you add a new coin to your supported coin list.

After the execution you should wait one or two trades of the bot before adding any new coin to your list.

By running the script without parameters, it will warm up the bots default database with all available coins for the bridge.

python3 database_warmup.py

If you want to specify a separate db file you can use the -d or --dbfile parameter. If not provided, the script will use the bots default db file.

python3 database_warmup.py -d data/warmup.db

You can also specify the coins you want to warmup with the -c or --coinlist parameter. If not provided the script will warmup all coins available for the bridge.

python3 database_warmup.py -c 'ADA BTC ETH LTC'

Showing the current ratio changes

This fork is along with scout history also saving the current changes in coin ratios. Those ratios are used by the bot to determine whether a jump to an another coin will yield more amount of the coin compared to a previous holding of that coin. If you are using the BTB-manager-telegram (see below), then shown ratios (Current ratios and Next coin buttons) are actually computed again, and mainly, not so accurately (fixed fee value).

You can use the show_ratio_changes.py script to show the latest values of ratio changes:

python3 show_ratio_changes.py

You can add it to custom scripts (config/custom_scripts.json) in BTB-manager-telegram:

"Show ratio changes": "python3 ../binance-trade-bot/show_ratio_changes.py"

Developing

To make sure your code is properly formatted before making a pull request, remember to install pre-commit:

pip install pre-commit
pre-commit install

The scouting algorithm is unlikely to be changed. If you'd like to contribute an alternative method, add a new strategy.

Related Projects

Thanks to a group of talented developers, there is now a Telegram bot for remotely managing this project.

Support the Project

Fist of all, support the originator of this bot and buy him a coffee. ☕

Buy Me A Coffee

If you like my adjustments and you want to support me I would appreciate to get a coffee too. 😜

Buy Me A Coffee

Join the Chat

FAQ

A list of answers to what seem to be the most frequently asked questions can be found in our discord server, in the corresponding channel.

Disclaimer

This project is for informational purposes only. You should not construe any such information or other material as legal, tax, investment, financial, or other advice. Nothing contained here constitutes a solicitation, recommendation, endorsement, or offer by me or any third party service provider to buy or sell any securities or other financial instruments in this or in any other jurisdiction in which such solicitation or offer would be unlawful under the securities laws of such jurisdiction.

If you plan to use real money, USE AT YOUR OWN RISK.

Under no circumstances will I be held responsible or liable in any way for any claims, damages, losses, expenses, costs, or liabilities whatsoever, including, without limitation, any direct or indirect damages for loss of profits.

About

Automated cryptocurrency trading bot

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Python 99.8%
  • Dockerfile 0.2%