如何通过Binance API获取与仪表盘一致的期货仓位历史数据
Fetch Binance Futures Historical Position Data (Matching Dashboard)
Your existing calls don't target the right endpoint:
futures_account_trades()returns individual trade records, not aggregated position history.futures_position_information()only retrieves currently active positions.
The correct method to get historical closed positions (matching the Binance dashboard's "Position History" tab) is futures_position_history() (for USDT-margined futures) or futures_coin_position_history() (for COIN-margined futures).
Implementation Steps
1. Basic Fetch with Python-Binance
Use this method to pull historical position data, then export it to a spreadsheet-friendly format like CSV/Excel.
from binance.client import Client import pandas as pd # Initialize client (replace with your API keys) client = Client(api_key="YOUR_API_KEY", api_secret="YOUR_API_SECRET") # Fetch position history (customize parameters as needed) position_data = client.futures_position_history( symbol="BTCUSDT", # Optional: filter by specific pair startTime=1690000000000, # Optional: start timestamp (ms) limit=1000 # Max results per request (1000 is the cap) ) # Convert to DataFrame and export to CSV df = pd.DataFrame(position_data) df.to_csv("binance_futures_position_history.csv", index=False) # For Excel export: # df.to_excel("binance_futures_position_history.xlsx", index=False)
2. Handle Pagination
Since each request can return a maximum of 1000 records, use pagination to fetch all historical data:
all_positions = [] start_time = 1690000000000 # Starting timestamp (adjust to your needs) batch_limit = 1000 while True: batch = client.futures_position_history( startTime=start_time, limit=batch_limit ) if not batch: break all_positions.extend(batch) # Update start time to the next millisecond after the last record's update time start_time = int(batch[-1]["updateTime"]) + 1 # Export full dataset pd.DataFrame(all_positions).to_csv("full_position_history.csv", index=False)
3. Key Data Fields (Matching Dashboard)
The returned data includes fields you'll see in the Binance dashboard:
symbol: Trading pair (e.g., BTCUSDT)positionAmt: Total position sizeentryPrice: Average entry pricemarkPrice: Mark price when the position was closedrealizedProfit: Total realized profit/loss from the positionupdateTime: Timestamp (ms) when the position was closed/updatedtype: Position direction (LONG/SHORT)
Important Notes
- Ensure your API key has Futures trading permission enabled in Binance's API settings.
- Respect Binance's API rate limits to avoid temporary bans.
- For COIN-margined futures, replace
futures_position_history()withfutures_coin_position_history().
内容的提问来源于stack exchange,提问作者Jože Strožer
相关产品推荐
相关产品推荐

