Ethereum: Binance futures API to retrieve filled order as signal

Retrieving Filled Order Signals from Binance Futures API

As you’re working with the Ethereum blockchain and Binance Futures API in your Python projects, you’re likely familiar with the basics of building an automated trading system. However, one important aspect often overlooked is getting notified when a trade has been filled. In this article, we’ll explore how to retrieve filled order signals from the Binance Futures API using their REST API.

Prerequisites

Before diving into the code, ensure you have:

  • A Binance Futures account with sufficient funds to place trades.

  • The binance and pandas libraries installed (pip install binance pandas).

  • Familiarity with Python basics (e.g., variables, data structures).

Connecting to the API

To retrieve filled orders, you’ll need to connect to the Binance Futures API. Here’s an example code snippet that demonstrates how to authenticate and obtain a signed URL for retrieving orders:

import binance

from binance.client import Client




Ethereum: Binance futures API to retrieve filled order as signal

Replace with your Binance API credentials

client_id = "YOUR_CLIENT_ID"

client_secret = "YOUR_CLIENT_SECRET"


Create a Binance client instance

api = Client(client_id=client_id, client_secret=client_secret)


Get the signed URL for retrieving orders

signed_url = api.get_order_history(

symbol="ETHUSDT",

limit=1000,

type="filled"

)

In this example:

  • We create a Client instance with your API credentials.

  • We use the get_order_history() method to retrieve an array of filled order objects. The type parameter is set to "filled", which indicates that we’re interested in filled orders.

Parsing Filled Order Data

Once you’ve obtained the signed URL, parse the JSON response to extract the relevant data:

import json


Extract the filled order IDs from the response

filled_order_ids = [order["id"] for order in json.loads(signed_url)["result"]["filledOrderList"]]


Use a database or file storage to store the filled orders (optional)


For this example, we'll simply print them out

for order_id in filled_order_ids:

print(f"Filled Order ID: {order_id}")

In this code:

  • We extract the id property from each filled order object and create a list of IDs.

  • You can use this list to store the filled orders in your database or file storage.

Setting Stop Losses

To set stop losses based on the filled orders, you’ll need to calculate the loss for each trade using the formula:

Loss = (Filled Order Price – Entry Price) \* Number of Filled Orders

You can then use this information to adjust your trading strategy. For example:

import pandas as pd


Load the filled order data into a Pandas DataFrame

df = pd.DataFrame({

"Filled Order ID": filled_order_ids,

"Filled Order Price": [order["filledPrice"] for order in json.loads(signed_url)["result"]["filledOrderList"]],

"Entry Price": [order["price"] for order in json.loads(signed_url)["result"]["filledOrderList"]]

})


Calculate the loss for each trade

df["Loss"] = (df["Filled Order Price"] - df["Entry Price"]) * len(df)


Adjust your stop losses based on the calculated losses

if len(df) > 0:

df["Stop Loss"] = df["Loss"].apply(lambda x: x / 2)

In this example:

  • We load the filled order data into a Pandas DataFrame.

  • We calculate the loss for each trade using the formula above.

  • We adjust your stop losses based on the calculated losses.

Full Code Example

Here’s an updated code snippet that demonstrates how to retrieve filled order signals and set stop losses:

“`python

import binance

from binance.