How to Place an Order on Binance with Python
Learn how to place an order on the world’s largets crypto exchange.
Learn how to place an order on the world’s largets crypto exchange.
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.
What would it take for Crypto, AI, and Cyber to truly change the world?
No spam. Unsubscribe anytime.
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.
To place an order on Binance, you need the following:
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.
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}")
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.
❤