How to List all Your Open Orders on Binance with Python
Learn how to list all your open orders on Binance using Python
Python Trading Bot
How to List all Your Open Orders on Binance with Python
When running an algorithmic trading bot, there are times when it’s handy to list all your open trades. This can give you a rapid overview of your various risk factors, and your currently open commitments.
You can do this programmatically through Binance, using their advanced API. Even better, if you’re using Python, they have an official Python Library to help make life easier.
The Binance API documentation can be slightly confusing/overwhelming to work through, so I’ve extracted a few bits and pieces to construct the function below.
Assumptions
- You’ve connected to the Binance API using an
api_key
andapi_secret_key
- You’re using the Binance Testnet as well as their live Spot trading
- You’ve received a
binance_testnet_api_key
andbinance_testnet_secret_key
- You know how to trade and the risks involved
P.S. If you’d like to see the full story on developing all the connections and keys you need in Python, check out my series “How to Build a Crypto Trading Bot on Binance with Python”.
Code
What You Need
- The keys listed above. That’s all ❤
Function
# Function to query open trades
def query_open_trades(project_settings):
# See if we're testing. Default to yes.
if project_settings['Testing'] == "False":
# 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:
# 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")
# Cancel the trade
try:
response = client.get_open_orders()
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.
❤