How to Place an Order on Binance with Python
Python Trading Bot
Binance is the world’s largest crypto exchange. In a recent ranking score, it was trading close to 7 times more volume than its nearest competitor Coinbase. If you’re building or expanding your crypto trading bot, Binance has an impressive (if sparsely documented) API, great API limits, and is a fast innovator of new functionality.
One of the functions your trading bot will need is placing orders. In this short article, I’ll show you how.
Assumptions
I’ll assume that you’ve already figured out how to connect to Binance using your API_Key
and Secret_Key
. If not, check out my series on building a crypto trading bot on Binance where I show you how to store your keys securely, how to connect, and more.
I’ll also assume that you know how to manage your balance and that you’ve got the infrastructure in place to determine buy/sell prices, stop losses, and if this is testing or real.
Note. All trading is at your own risk.
What You’ll Need
To place an order on Binance, you need the following:
- Symbol — the currency pair you’d like to trade
- Side — whether this is a BUY or SELL
- Type — whether this is a LIMIT, MARKET, STOP_LOSS, STOP_LOSS_LIMIT, TAKE_PROFIT, TAKE_PROFIT_LIMIT or LIMIT_MAKER
- TimeInForce — how long your order will remain valid, options being GTC (Good Till Canceled), IOC (Immediate or Cancel), FOK (Fill or Kill)
- Quantity — the amount you’ll be purchasing
- Stop Price — the BUY_STOP or SELL_STOP price on the order
- Stop Limit Price — the Stop Loss to place on the order
- Trailing Delta — if you choose to put a trailing stop on your order, then you can set the percent you’d like to trail.
P.S. The trailing stop is a particularly nice feature for Binance. It’s not available as a function MetaTrader 5, and Coinbase is still figuring out the need for a Stop Loss.
Code
Here’s the code. If you’re not planning on adding a trailing stop, you can simply delete that part.
# Function to make a trade on Binance
def make_trade(symbol, action, type, timeLimit, quantity, stop_price, stop_limit_price, project_settings):
# Develop the params
params = {
"symbol": symbol,
"side": action,
"type": type,
"timeInForce": timeLimit,
"quantity": quantity,
"stopPrice": stop_price,
"stopLimitPrice": stop_limit_price,
"trailingDelta": project_settings['trailingStopPercent']
}
# See if we're testing. Default to yes.
if project_settings['Testing'] == "False":
print("Real Trade")
# Set the API Key
api_key = project_settings['BinanceKeys']['API_Key']
# Set the secret key
secret_key = project_settings['BinanceKeys']['Secret_Key']
# Setup the client
client = Spot(key=api_key, secret=secret_key)
else:
print("Testing Trading")
# Set the Test API Key
api_key = project_settings['TestKeys']['Test_API_Key']
# Set the Test Secret Key
secret_key = project_settings['TestKeys']['Test_Secret_Key']
client = Spot(key=api_key, secret=secret_key, base_url="https://testnet.binance.vision")
# Make the trade
try:
response = client.new_order(**params)
return response
except ConnectionRefusedError as error:
print(f"Found error. {error}")
Say Hi!
I love hearing from my readers, so feel free to reach out. It means a ton to me when you clap for my articles or drop a friendly comment — it helps me know that my content is helping.
❤