0
# Market Data
1
2
Comprehensive market data access including exchange information, tickers, order books, trade history, and kline/candlestick data. All methods work with both synchronous and asynchronous clients.
3
4
## Capabilities
5
6
### Exchange Information
7
8
Get trading rules, symbols, and exchange metadata.
9
10
```python { .api }
11
def get_exchange_info(self) -> Dict: ...
12
def get_symbol_info(self, symbol) -> Optional[Dict]: ...
13
def get_products(self) -> Dict: ...
14
```
15
16
#### Usage Example
17
18
```python
19
# Get complete exchange information
20
exchange_info = client.get_exchange_info()
21
22
# Filter for USDT pairs
23
usdt_symbols = [s for s in exchange_info['symbols'] if s['quoteAsset'] == 'USDT']
24
print(f"USDT pairs: {len(usdt_symbols)}")
25
26
# Get specific symbol information
27
btc_info = client.get_symbol_info('BTCUSDT')
28
if btc_info:
29
print(f"BTC filters: {btc_info['filters']}")
30
```
31
32
### Price Tickers
33
34
Current price information and 24hr statistics.
35
36
```python { .api }
37
def get_all_tickers(self) -> List[Dict[str, str]]: ...
38
def get_ticker(self, **params): ...
39
def get_symbol_ticker(self, **params): ...
40
def get_symbol_ticker_window(self, **params): ...
41
def get_orderbook_ticker(self, **params): ...
42
def get_orderbook_tickers(self, **params) -> Dict: ...
43
```
44
45
#### Usage Example
46
47
```python
48
# Get all 24hr price tickers
49
all_tickers = client.get_all_tickers()
50
btc_ticker = next(t for t in all_tickers if t['symbol'] == 'BTCUSDT')
51
print(f"BTC price: {btc_ticker['price']}")
52
53
# Get specific symbol ticker with 24hr stats
54
btc_stats = client.get_ticker(symbol='BTCUSDT')
55
print(f"24hr change: {btc_stats['priceChangePercent']}%")
56
57
# Get order book ticker (best bid/ask)
58
orderbook_ticker = client.get_orderbook_ticker(symbol='BTCUSDT')
59
print(f"Best bid: {orderbook_ticker['bidPrice']}, Best ask: {orderbook_ticker['askPrice']}")
60
61
# Get custom window ticker
62
window_ticker = client.get_symbol_ticker_window(symbol='BTCUSDT', windowSize='1d')
63
```
64
65
### Order Book Data
66
67
Market depth and order book information.
68
69
```python { .api }
70
def get_order_book(self, **params) -> Dict: ...
71
def get_avg_price(self, **params): ...
72
```
73
74
#### Usage Example
75
76
```python
77
# Get order book with default depth (100)
78
order_book = client.get_order_book(symbol='BTCUSDT')
79
print(f"Top bid: {order_book['bids'][0]}")
80
print(f"Top ask: {order_book['asks'][0]}")
81
82
# Get order book with specific depth
83
deep_book = client.get_order_book(symbol='BTCUSDT', limit=1000)
84
85
# Get average price over last 5 minutes
86
avg_price = client.get_avg_price(symbol='BTCUSDT')
87
print(f"5min avg price: {avg_price['price']}")
88
```
89
90
### Trade Data
91
92
Recent and historical trade information.
93
94
```python { .api }
95
def get_recent_trades(self, **params) -> Dict: ...
96
def get_historical_trades(self, **params) -> Dict: ...
97
def get_aggregate_trades(self, **params) -> Dict: ...
98
def aggregate_trade_iter(self, symbol: str, start_str=None, last_id=None): ...
99
```
100
101
#### Usage Example
102
103
```python
104
# Get recent trades (no API key required)
105
recent_trades = client.get_recent_trades(symbol='BTCUSDT', limit=10)
106
for trade in recent_trades:
107
print(f"Price: {trade['price']}, Qty: {trade['qty']}, Time: {trade['time']}")
108
109
# Get historical trades (requires API key)
110
historical_trades = client.get_historical_trades(symbol='BTCUSDT', limit=100)
111
112
# Get aggregate trades with time range
113
agg_trades = client.get_aggregate_trades(
114
symbol='BTCUSDT',
115
startTime=1609459200000, # Timestamp in milliseconds
116
endTime=1609462800000,
117
limit=500
118
)
119
120
# Iterate through aggregate trades
121
for agg_trade in client.aggregate_trade_iter(symbol='BTCUSDT', start_str='1 day ago UTC'):
122
print(f"Agg trade: {agg_trade}")
123
if len(processed_trades) > 1000: # Limit iteration
124
break
125
```
126
127
### Kline/Candlestick Data
128
129
Historical and current kline data with various intervals.
130
131
```python { .api }
132
def get_klines(self, **params) -> Dict: ...
133
def get_ui_klines(self, **params) -> Dict: ...
134
def get_historical_klines(
135
self,
136
symbol: str,
137
interval: str,
138
start_str: str,
139
end_str: Optional[str] = None,
140
limit: int = 1000,
141
klines_type: HistoricalKlinesType = HistoricalKlinesType.SPOT,
142
) -> List[List]: ...
143
def get_historical_klines_generator(
144
self,
145
symbol: str,
146
interval: str,
147
start_str: str,
148
end_str: Optional[str] = None,
149
limit: int = 1000,
150
klines_type: HistoricalKlinesType = HistoricalKlinesType.SPOT,
151
): ...
152
```
153
154
#### Usage Example
155
156
```python
157
from binance import KLINE_INTERVAL_1HOUR, HistoricalKlinesType
158
159
# Get recent klines
160
klines = client.get_klines(
161
symbol='BTCUSDT',
162
interval=KLINE_INTERVAL_1HOUR,
163
limit=24 # Last 24 hours
164
)
165
166
# Each kline contains: [timestamp, open, high, low, close, volume, close_time, ...]
167
for kline in klines:
168
timestamp, open_price, high, low, close, volume = kline[:6]
169
print(f"Time: {timestamp}, OHLC: {open_price}/{high}/{low}/{close}, Vol: {volume}")
170
171
# Get historical klines with date strings
172
historical_klines = client.get_historical_klines(
173
symbol='BTCUSDT',
174
interval=KLINE_INTERVAL_1HOUR,
175
start_str='1 Jan 2024',
176
end_str='2 Jan 2024'
177
)
178
179
# Get futures klines
180
futures_klines = client.get_historical_klines(
181
symbol='BTCUSDT',
182
interval=KLINE_INTERVAL_1HOUR,
183
start_str='1 day ago UTC',
184
klines_type=HistoricalKlinesType.FUTURES
185
)
186
187
# Use generator for large datasets to avoid memory issues
188
kline_generator = client.get_historical_klines_generator(
189
symbol='BTCUSDT',
190
interval=KLINE_INTERVAL_1HOUR,
191
start_str='1 week ago UTC'
192
)
193
194
for kline_batch in kline_generator:
195
# Process batch of klines
196
print(f"Processing {len(kline_batch)} klines")
197
```
198
199
### System Status
200
201
Server connectivity and system status information.
202
203
```python { .api }
204
def ping(self) -> Dict: ...
205
def get_server_time(self) -> Dict: ...
206
def get_system_status(self): ...
207
```
208
209
#### Usage Example
210
211
```python
212
# Test connectivity
213
ping_result = client.ping()
214
print("Connection successful" if ping_result == {} else "Connection failed")
215
216
# Get server time
217
server_time = client.get_server_time()
218
print(f"Server timestamp: {server_time['serverTime']}")
219
220
# Check system status
221
system_status = client.get_system_status()
222
print(f"System status: {system_status['status']}") # 0 = normal, 1 = maintenance
223
```
224
225
### Advanced Market Data Options
226
227
#### Futures Market Data
228
229
```python
230
# Get futures exchange info
231
futures_info = client.futures_get_exchange_info()
232
233
# Get futures tickers
234
futures_tickers = client.futures_get_all_tickers()
235
236
# Get futures order book
237
futures_book = client.futures_get_order_book(symbol='BTCUSDT')
238
239
# Get funding rate
240
funding_rate = client.futures_funding_rate(symbol='BTCUSDT')
241
```
242
243
#### Options Market Data
244
245
```python
246
# Get options exchange info
247
options_info = client.options_get_exchange_info()
248
249
# Get options index price
250
index_price = client.options_get_index_price(underlying='BTC')
251
252
# Get options mark price
253
mark_price = client.options_get_mark_price(symbol='BTC-240329-70000-C')
254
```
255
256
## Data Types and Structures
257
258
### Kline Data Structure
259
260
Each kline is a list with the following structure:
261
```python
262
[
263
1609459200000, # Open time (timestamp)
264
"29000.00000000", # Open price
265
"29500.00000000", # High price
266
"28800.00000000", # Low price
267
"29200.00000000", # Close price
268
"100.50000000", # Volume
269
1609462799999, # Close time (timestamp)
270
"2950000.00000000", # Quote asset volume
271
1000, # Number of trades
272
"50.25000000", # Taker buy base asset volume
273
"1475000.00000000", # Taker buy quote asset volume
274
"0" # Ignore
275
]
276
```
277
278
### Ticker Data Structure
279
280
24hr ticker statistics:
281
```python
282
{
283
"symbol": "BTCUSDT",
284
"priceChange": "200.00000000",
285
"priceChangePercent": "0.69",
286
"weightedAvgPrice": "29100.50000000",
287
"prevClosePrice": "29000.00000000",
288
"lastPrice": "29200.00000000",
289
"lastQty": "0.10000000",
290
"bidPrice": "29199.00000000",
291
"askPrice": "29201.00000000",
292
"openPrice": "29000.00000000",
293
"highPrice": "29500.00000000",
294
"lowPrice": "28800.00000000",
295
"volume": "1000.50000000",
296
"quoteVolume": "29150000.00000000",
297
"openTime": 1609459200000,
298
"closeTime": 1609545599999,
299
"firstId": 100000,
300
"lastId": 200000,
301
"count": 100001
302
}
303
```