The script allows you to place a buy order and automatically place an OCO order to secure (and exit) your trade. It's using the binance API python wrapper from @sammchardy python-binance. If you want to create Binance account, here's my referral link: earn 10% commission on the fees from my trades.
Even if the scripts are easy to understand, errors exists that why the Issues section is important to report any bug that you may find. I recommend to use the latest release because it has been tested multiple times compared to the other branches.
- Create an API key after logging in to your Binance account https://www.binance.com/en/my/settings/api-management:
- Check both
Enable Reading
andEnable Spot & Margin Trading
- Save carefully your
API key
and yourSecret key
(⚠️ the last one won't be visible again at your next login) - Add your Binance API key to a environement variable called
API_KEY
- Add your Binance Secret key to a environement variable called
SECRET_KEY
- Check both
- Install Python 3.6+ (I'm using Anaconda for instance)
- Clone the repository
git clone https://github.com/UPetit/python-binance-profit.git
- Install the dependencies
pip install -r requirements.txt
- Linux version without anaconda
# restrict lib install to the local venv
python3 -m venv venv
source ./venv/bin/activate
pip install -r requirements.txt
The script will check that both the price and the quantity are compliant with Binance rules for the selected symbol. If both are validated, the buy order will be sent to the market and the script will wait until it's filled. Once it's executed the OCO order will be prepared: the price with profit will be calculated according to the profit percentage that has been provided, same for the stoploss price. The OCO order will be sent to the market and the script will return the two related orders and then quit.
- First, choose the crypto pair you want to trade. We call it the symbol (string).
Example: If you want to trade BTC with USDT, the symbol will be BTCUSDT (as long as this is an available symbol in Binance).
- For a limit buy order:
- Define how much of the base asset you want to buy. We call it the quantity (Decimal).
- Define what is the price (for 1 unit of the base asset) you want to buy the quantity defined above. We call it the price (Decimal).
Example: if you trade the symbol BTCUSDT, you need to define the quantity of BTC (base asset) and the BTC price in USDT (quote asset) you're willing to pay.
- For a market buy order:
- Define the amount of quote asset you want to spend. We call it the total (Decimal).
Example: if you trade with BTCUSDT, you need to define how much you want to spend in USDT for BTC.
- Finally have to define your profit and stoploss percentages to exit the trade (this will be applied to the OCO sell order). We call them respectively profit (Decimal) and loss (Decimal).
If you want to make 2% of profit and put a stoploss a 1%, your profit should be 2 and the loss 1 (as Decimals between 0.0 and 100.0)
How to know the prices formats ? Go to the Binance market of your symbol you want to trade, check the current prices and quantities going through the market to know how many decimals you can use for both of them. For instance for BTCUSDT: the BTC quantity is using 6 decimals and the USDT price is using 2 decimals.
- Run the script using the parameters you've just defined by replacing with your values: Limit buy order followed by an OCO sell order:
python execute_orders.py --symbol YOUR_SYMBOL --buy_type limit --quantity YOUR_QUANTITY --price YOUR_PRICE --profit YOUR_PROFIT --loss YOUR_LOSS
Example: If you want to trade BTC against USDT, buy 0.251897 BTC at 31488.69 USDT (per BTC) and then you want to sell it to make a 4% profit and a potential loss of 1%, you'll execute the script like this:
python execute_orders.py --symbol BTCUSDT --buy_type limit --quantity 0.251897 --price 31488.69 --profit 4 --loss 1
Market buy order followed by an OCO sell order:
python execute_orders.py --symbol YOUR_SYMBOL --buy_type market --total TOTAL_AMOUNT --profit YOUR_PROFIT --loss YOUR_LOSS
Example: If you want to trade BTC against USDT, buy 100 USDT of BTC and then you want to sell it to make a 4% profit and a potential loss of 1%, you'll execute the script like this:
python execute_orders.py --symbol BTCUSDT --buy_type market --total 100.0 --profit 4 --loss 1
If you want to support this project financially, you can make a donation to any of the following address:
- BTC address:
198eFqDXEcZXQjo66wGCdgz3VJFFRWEwXg
- ETH ERC20 address:
0xaa83fcaa0755123572d6543338af3cad2df2fcda
- LTC address:
Lh5tKw4qEX19pL3KB8kQwHkCPPRU972Aeg
- PayPal: (https://paypal.me/UlyssePETIT)[https://paypal.me/UlyssePETIT]
Enjoy! Don't hesitate to send me feedbacks and report issues :) Thank you!