Binance trading bot github
Automated cryptocurrency trading bot
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.
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.
- 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.
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.
- 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.
- strategy — The trading strategy to use. See binance_trade_bot/strategies for more information
- buy_timeout/sell_timeout — Controls how many minutes to wait before cancelling a limit order (buy/sell) and returning to «scout» mode. 0 means that the order will never be cancelled prematurely.
- 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.
All of the options provided in user.cfg can also be configured using environment variables.
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.
The official image is available here and will update on every new change.
If you only want to start the SQLite browser
You can test the bot on historic data to see how it performs.
Feel free to modify that file to test and compare different settings and time periods
To make sure your code is properly formatted before making a pull request, remember to install pre-commit:
The scouting algorithm is unlikely to be changed. If you’d like to contribute an alternative method, add a new strategy.
Thanks to a group of talented developers, there is now a Telegram bot for remotely managing this project.
Support the Project
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.
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.
Источник
Binance trading bot github
Binance Trading Bot
Automated Binance trading bot with trailing buy/sell strategy
This is a test project. I am just testing my code.
I cannot guarantee whether you can make money or not.
So use it at your own risk! I have no responsibility for any loss or hardship incurred directly or indirectly by using this code. Read disclaimer before using this code.
Before updating the bot, make sure to record the last buy price in the note. It may lose the configuration or last buy price records.
Support manual trade
The bot is now supporting manual trade per symbol. It will automatically re-calculate the last buy price if exists.
Support all symbols
The bot is now supporting all symbols such as ETHBTC, XRPBTC. As a result, the maximum purchase amount configuration has been updated in the global configuration.
The max purchase amount now can be configured for the quote asset. The quote asset is the coin you use to buy the base asset. For example, for BTCUSDT, BTC is the base asset, USDT is the quote asset.
In the global configuration, you would see a field for «Maximum purchase amount for USDT». If you configure this, any newly added symbols, which uses USDT would be applied for the configured maximum purchase amount.
A default value will be dynamically calculated based on the symbol’s minimum notional value.
Support monitoring multiple coins simultaneously
The bot is now monitoring all coins every second. However, updating the indicators for the symbol cannot update every second because of the API limit. As a result, I had to introduce the lock mechanism for the symbol. The frontend will display the lock icon when the symbol is updating the indicators in the background. During updating the indicators, the bot will not process the order.
If interested, take a look this PR #77
The bot is now supporting Stop-Loss for preventing the loss than configured. In the configuration, you can enable Stop-Loss and set the maximum loss percentage along with the minutes of disabling buying action. The bot will calculate the Stop-Loss trigger price based on the last buy price.
For example, let’s assume it is configured Max loss percentage to be 0.8 and Temporary disable for buying to be 60. The last buy price is $100 . Then the bot will calculate the Stop-Loss trigger price as $80 . When the current price reaches $80 , the bot will place the market order.
At the moment, the bot only supports the market order.
Trailing Buy/Sell Bot
This bot is using the concept of trailing buy/sell order which allows following the price fall/rise.
Trailing Stop Orders About Trailing Stop Orders Concept you can find at Binance Official document
TL;DR Place orders at a fixed value or percentage when the price changes. Using this feature you can buy at the lowest possible price when buying down and sell at the highest possible price when selling up.
- The bot can monitor multiple symbols. All symbols will be monitored per second.
- The bot is using MongoDB to provide a persistence database. However, it does not use the latest MongoDB to support Raspberry Pi 32bit. Used MongoDB version is 3.2.20, which is provided by apcheamitru.
- The bot is tested/working with Linux and Raspberry Pi 4 32bit. Other platforms are not tested.
The bot will continuously monitor the lowest value for the period of the candles. Once the current price reaches the lowest price, then the bot will place a STOP-LOSS-LIMIT order to buy. If the current price continuously falls, then the bot will cancel the previous order and re-place the new STOP-LOSS-LIMIT order with the new price.
- The bot will not place a buy order if has enough coin (typically over $10 worth) to sell when reaches the trigger price for selling.
Let say, if the buy configurations are set as below:
- Maximum purchase amount: $50
- Trigger percentage: 1.005 (0.5%)
- Stop price percentage: 1.01 (1.0%)
- Limit price percentage: 1.011 (1.1%)
And the market is as below:
- Current price: $101
- Lowest price: $100
- Trigger price: $100.5
Then the bot will not place an order because the trigger price ($100.5) is less than the current price ($101).
In the next tick, the market changes as below:
- Current price: $100
- Lowest price: $100
- Trigger price: $100.5
The bot will place new STOP-LOSS-LIMIT order for buying because the current price ($100) is less than the trigger price ($100.5). For the simple calculation, I do not take an account for the commission. In real trading, the quantity may be different. The new buy order will be placed as below:
- Stop price: $100 * 1.01 = $101
- Limit price: $100 * 1.011 = $101.1
- Quantity: 0.49
In the next tick, the market changes as below:
- Current price: $99
- Current limit price: $99 * 1.011 = 100.089
- Open order stop price: $101
As the open order’s stop price ($101) is higher than the current limit price ($100.089), the bot will cancel the open order and place new STOP-LOSS-LIMIT order as below:
- Stop price: $99 * 1.01 = $99.99
- Limit price: $99 * 1.011 = $100.089
- Quantity: 0.49
If the price continuously falls, then the new buy order will be placed with the new price.
And if the market changes as below in the next tick:
Then the current price reaches the stop price ($99.99); hence, the order will be executed with the limit price ($100.089).
If there is enough balance for selling and the last buy price is recorded in the bot, then the bot will start monitoring the sell signal. Once the current price reaches the trigger price, then the bot will place a STOP-LOSS-LIMIT order to sell. If the current price continuously rises, then the bot will cancel the previous order and re-place the new STOP-LOSS-LIMIT order with the new price.
- If the coin is worth less than typically $10 (minimum notional value), then the bot will remove the last buy price because Binance does not allow to place an order of less than $10.
- If the bot does not have a record for the last buy price, the bot will not sell the coin.
Let say, if the sell configurations are set as below:
- Trigger percentage: 1.05 (5.0%)
- Stop price percentage: 0.98 (-2.0%)
- Limit price percentage: 0.979 (-2.1%)
And the market is as below:
- Coin owned: 0.5
- Current price: $100
- Last buy price: $100
- Trigger price: $100 * 1.05 = $105
Then the bot will not place an order because the trigger price ($105) is higher than the current price ($100).
If the price is continuously falling, then the bot will keep monitoring until the price reaches the trigger price.
In the next tick, the market changes as below:
- Current price: $105
- Trigger price: $105
The bot will place new STOP-LOSS-LIMIT order for selling because the current price ($105) is higher or equal than the trigger price ($105). For the simple calculation, I do not take an account for the commission. In real trading, the quantity may be different. The new sell order will be placed as below:
- Stop price: $105 * 0.98 = $102.9
- Limit price: $105 * 0.979 = $102.795
- Quantity: 0.5
In the next tick, the market changes as below:
- Current price: $106
- Current limit price: $103.774
- Open order stop price: $102.29
As the open order’s stop price ($102.29) is less than the current limit price ($103.774), the bot will cancel the open order and place new STOP-LOSS-LIMIT order as below:
- Stop price: $106 * 0.98 = $103.88
- Limit price: $106 * 0.979 = $103.774
- Quantity: 0.5
If the price continuously rises, then the new sell order will be placed with the new price.
And if the market changes as below in the next tick:
The the current price reaches the stop price ($103.88); hence, the order will be executed with the limit price ($103.774).
Sell Stop-Loss Scenario
Let say, if the sell Stop-Loss configurations are set as below:
- Max loss percentage: 0.90
- Temporary disable for buying (minutes): 60
And the market is as below:
- Current price: $95
- Last buy price: $100
- Stop-Loss price: $90
Then the bot will not place a Stop-Loss order because the Stop-Loss price ($90) is less than the current price ($95).
If the price is continuously falling, then the bot will keep monitoring until the price reaches the Stop-Loss price.
In the next tick, the market changes as below:
- Current price: $90
- Stop-Loss price: $90
The bot will place new MARKET order for selling because the current price ($90) is less or equal than the Stop-Loss price ($90). In real trading, the quantity may be different.
The bot will also set the symbol to be temporarily disabled for 60 minutes to avoid buying/sell continuously. In the frontend, the action will display the pause icon and how long left to be enabled again. The symbol can be enabled by clicking the play icon.
React.js based frontend communicating via Web Socket:
- List monitoring coins with buy/sell signals/open orders
- View account balances
- Manage global/symbol settings
- Delete caches that are not monitored
- Link to public URL
- Support Add to Home Screen
Use environment parameters to adjust parameters. Check /config/custom-environment-variables.json to see list of available environment parameters.
Or use the frontend to adjust configurations after launching the application.
Create .env file based on .env.dist .
Environment Key | Description | Sample Value |
---|---|---|
BINANCE_LIVE_API_KEY | Binance API key for live | (from Binance) |
BINANCE_LIVE_SECRET_KEY | Binance API secret for live | (from Binance) |
BINANCE_TEST_API_KEY | Binance API key for test | (from Binance Spot Test Network) |
BINANCE_TEST_SECRET_KEY | Binance API secret for test | (from Binance Spot Test Network) |
BINANCE_SLACK_ENABLED | Slack enable/disable | true |
BINANCE_SLACK_WEBHOOK_URL | Slack webhook URL | (from Slack) |
BINANCE_SLACK_CHANNEL | Slack channel | «#binance» |
BINANCE_SLACK_USERNAME | Slack username | Chris |
BINANCE_LOCAL_TUNNEL_ENABLED | Enable/Disable local tunnel | true |
BINANCE_LOCAL_TUNNEL_SUBDOMAIN | Local tunnel public URL subdomain | binance |
Launch/Update the bot with docker-compose
Pull latest code first:
If want production/live mode, then use the latest build image from DockerHub:
Or if using Raspberry Pi 4 32bit, must build again for Raspberry Pi:
Or if want development/test mode, then run below commands:
Open browser http://0.0.0.0:8080 to see the frontend
- When launching the application, it will notify public URL to the Slack.
- If you have any issue with the bot, you can check the log to find out what happened with the bot. Please take a look Troubleshooting
Install via Stackfile
In Portainer create new Stack
Copy content of docker-stack.yml or upload the file
Set environment keys for binance-bot in the docker-stack.yml
Launch and open browser http://0.0.0.0:8080 to see the frontend
Frontend Mobile | Setting | Manual Trade |
---|---|---|
Frontend Desktop |
---|
Please refer CHANGELOG.md to view the past changes.
- Update the bot to monitor all coins every second — #52
- Display release version to the frontend — #59
- Improve frontend & settings UI — #93#85
- Support all symbols — #104
- Add stop loss feature — #99
- Add manual buy/sell feature -#100
- Improve sell strategy with conditional stop price percentage based on the profit percentage — #94
- Add sudden drop buy strategy — #67
- Support Grid strategy for buy/sell to mitigate loss/increasing profit — #158
- Improve buy strategy with restricting purchase if the price is close to ATH — #82
- Secure frontend with the password authentication
- Display summary of transactions on the frontend — #160
- Add minimum required order amount — #84
- Manage setting profiles (save/change/load?/export?) — #151
- Filter symbols in the frontend — #120
- Improve notifications by supporting Apprise #106
- Support cool time after hitting the lowest price before buy — #105
- Add frontend option to disable sorting or improve sorting
- Reset global configuration to initial configuration — #97
- Support limit for active buy/sell orders — #147
- Develop simple setup screen for secrets
- Support multilingual frontend — #56
If you find this project helpful, feel free to make a small donation to the developer.
Thanks to all contributors ❤️ Click to see our heroes
I give no warranty and accepts no responsibility or liability for the accuracy or the completeness of the information and materials contained in this project. 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, business interruption or loss of information) resulting from or arising directly or indirectly from your use of or inability to use this code or any code linked to it, or from your reliance on the information and material on this code, even if I have been advised of the possibility of such damages in advance.
So use it at your own risk!
About
Automated Binance trading bot — Buy low/Sell high with stop loss limit/Trade multiple cryptocurrencies
Источник