1 min read

How to List all Your Open Orders on Binance with Python

Learn how to list all your open orders on Binance using Python
Cute little clockwork robot which is holding a red arrow which points upwards. Sepia colors.
Python Trading Bot

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

  1. You’ve connected to the Binance API using an api_key and api_secret_key
  2. You’re using the Binance Testnet as well as their live Spot trading
  3. You’ve received a binance_testnet_api_key and binance_testnet_secret_key
  4. 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

  1. 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.