feat: upgrade margin socket to use websocket api#1670
Merged
carlosmiei merged 1 commit intosammchardy:masterfrom Feb 13, 2026
Merged
feat: upgrade margin socket to use websocket api#1670carlosmiei merged 1 commit intosammchardy:masterfrom
carlosmiei merged 1 commit intosammchardy:masterfrom
Conversation
Collaborator
Author
|
Test ran ""Test: place a tiny margin order on ETH/USDT and confirm the event arrives on the margin WebSocket.
Set environment variables:
- BINANCE_API_KEY
- BINANCE_API_SECRET
"""
import asyncio
import os
from binance.async_client import AsyncClient
from binance.ws.streams import BinanceSocketManager
async def main():
api_key = os.getenv(
"BINANCE_API_KEY",
"",
)
api_secret = os.getenv(
"BINANCE_API_SECRET",
"",
)
client = await AsyncClient.create(api_key=api_key, api_secret=api_secret)
bm = BinanceSocketManager(client)
# --- 0. Show margin account balances ---
print("Fetching margin account balances ...")
margin_account = await client.get_margin_account()
print(f" Margin level: {margin_account.get('marginLevel', '?')}")
print(f" Total asset (BTC): {margin_account.get('totalAssetOfBtc', '?')}")
print(f" Total liability (BTC): {margin_account.get('totalLiabilityOfBtc', '?')}")
for asset in margin_account.get("userAssets", []):
free = float(asset["free"])
locked = float(asset["locked"])
borrowed = float(asset["borrowed"])
if free > 0 or locked > 0 or borrowed > 0:
print(f" {asset['asset']:>6s} free={asset['free']} locked={asset['locked']} borrowed={asset['borrowed']}")
print()
# --- 1. Connect margin websocket ---
print("Connecting margin websocket ...")
margin_ws = bm.margin_socket()
await margin_ws.connect()
print(f" connected (subscription_id={margin_ws._subscription_id})")
# --- 2. Place a small LIMIT BUY far below market so it won't fill ---
symbol = "ETHUSDT"
# Ensure notional > $10. 0.007 ETH * 1500 = $10.50
# Price well below market to avoid a fill.
quantity = "0.007"
price = "1500.00"
print(f"Placing limit buy: {quantity} {symbol} @ {price} ...")
order = await client.create_margin_order(
symbol=symbol,
side="BUY",
type="LIMIT",
timeInForce="GTC",
quantity=quantity,
price=price,
sideEffectType="MARGIN_BUY",
)
order_id = order["orderId"]
print(f" order placed (orderId={order_id})")
# --- 3. Wait for the executionReport event on the websocket ---
print("Waiting for margin websocket event ...")
try:
msg = await asyncio.wait_for(margin_ws.recv(), timeout=10)
print(f" received event: {msg.get('e', '?')} status={msg.get('X', '?')}")
print(f" full payload:\n {msg}")
except asyncio.TimeoutError:
print(" TIMEOUT: no event received within 10 s")
# --- 4. Cancel the order ---
print(f"Cancelling order {order_id} ...")
await client.cancel_margin_order(symbol=symbol, orderId=order_id)
print(" cancelled")
# Try to catch the cancel event too
try:
msg = await asyncio.wait_for(margin_ws.recv(), timeout=10)
print(f" cancel event: {msg.get('e', '?')} status={msg.get('X', '?')}")
except asyncio.TimeoutError:
print(" no cancel event received (timeout)")
# --- 5. Clean up ---
await margin_ws.__aexit__(None, None, None)
await client.close_connection()
print("Done.")
if __name__ == "__main__":
asyncio.run(main())Result |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fix #1668
Note: awaiting key to perform testing, don't merge yet.