-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Expand file tree
/
Copy pathllms.txt
More file actions
236 lines (188 loc) · 6.86 KB
/
llms.txt
File metadata and controls
236 lines (188 loc) · 6.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
# python-binance
> Unofficial Python SDK for the Binance cryptocurrency exchange. Covers Spot, Margin, Futures (USD-M & Coin-M), Options, Portfolio Margin, and WebSocket APIs with 797+ methods across sync and async clients.
## Docs
- [Getting Started](https://python-binance.readthedocs.io/en/latest/overview.html): Installation, authentication, setup
- [Full Endpoint Reference](./Endpoints.md): All methods mapped to Binance API endpoints
- [Comprehensive LLM Reference](./llms-full.txt): Every method with signature, description, and parameters
- [Binance API Docs](https://developers.binance.com/docs/): Official Binance API documentation
## Installation & Auth
```bash
pip install python-binance
```
```python
from binance import Client, AsyncClient
# Basic setup
client = Client(api_key, api_secret)
# Async setup
async_client = await AsyncClient.create(api_key, api_secret)
# Testnet
client = Client(api_key, api_secret, testnet=True)
# Demo trading (paper trading)
client = Client(api_key, api_secret, demo=True)
# RSA key authentication
client = Client(api_key, private_key=open("private_key.pem").read())
# Other TLD (e.g. Binance US)
client = Client(api_key, api_secret, tld="us")
```
## Core Concepts
- **All methods return plain Python dicts** (not custom objects). Parse responses using standard dict access.
- **Extra parameters**: Most methods accept `**params` — pass any Binance API parameter as a keyword argument.
- **Sync/async parity**: `Client` and `AsyncClient` have identical method names. Async methods need `await`.
- **Enums**: Import constants from `binance.enums` or use string values directly.
- **Exceptions**: Catch `BinanceAPIException` (has `.code` and `.message` attributes).
## Method Categories
- **Spot / General**: 197 methods
- **Futures USD-M**: 109 methods
- **Futures Coin-M**: 65 methods
- **Margin**: 211 methods
- **Options**: 46 methods
- **Portfolio Margin**: 103 methods
- **WebSocket API**: 43 methods
- **WebSocket Futures**: 15 methods
- **Gift Card**: 6 methods
- **Convert**: 2 methods
## Method Naming Conventions
| Prefix | Domain | Example |
|--------|--------|---------|
| `get_*` / `create_*` / `cancel_*` | Spot trading & account | `get_order_book()`, `create_order()` |
| `futures_*` | USD-M Futures | `futures_create_order()`, `futures_account()` |
| `futures_coin_*` | Coin-M Futures | `futures_coin_create_order()` |
| `margin_*` | Margin trading | `margin_borrow_repay()`, `margin_max_borrowable()` |
| `options_*` | Vanilla Options | `options_place_order()`, `options_account_info()` |
| `papi_*` | Portfolio Margin | `papi_create_um_order()`, `papi_get_balance()` |
| `ws_*` | WebSocket API (CRUD) | `ws_create_order()`, `ws_get_order()` |
| `ws_futures_*` | WebSocket Futures | `ws_futures_create_order()` |
| `gift_card_*` | Gift cards | `gift_card_create()`, `gift_card_redeem()` |
| `order_*` | Order helpers | `order_limit_buy()`, `order_market_sell()` |
| `stream_*` | User data stream | `stream_get_listen_key()`, `stream_keepalive()` |
## Key Enums
```python
from binance.enums import *
# Sides
SIDE_BUY = "BUY"
SIDE_SELL = "SELL"
# Order types (spot)
ORDER_TYPE_LIMIT = "LIMIT"
ORDER_TYPE_MARKET = "MARKET"
ORDER_TYPE_STOP_LOSS_LIMIT = "STOP_LOSS_LIMIT"
ORDER_TYPE_TAKE_PROFIT_LIMIT = "TAKE_PROFIT_LIMIT"
# Time in force
TIME_IN_FORCE_GTC = "GTC" # Good till cancelled
TIME_IN_FORCE_IOC = "IOC" # Immediate or cancel
TIME_IN_FORCE_FOK = "FOK" # Fill or kill
# Kline intervals
KLINE_INTERVAL_1MINUTE = "1m"
KLINE_INTERVAL_1HOUR = "1h"
KLINE_INTERVAL_1DAY = "1d"
KLINE_INTERVAL_1WEEK = "1w"
# Futures order types (also includes STOP, STOP_MARKET, TRAILING_STOP_MARKET)
FUTURE_ORDER_TYPE_LIMIT = "LIMIT"
FUTURE_ORDER_TYPE_MARKET = "MARKET"
```
## Common Tasks
### Get current price
```python
ticker = client.get_symbol_ticker(symbol="BTCUSDT")
print(ticker["price"]) # "50000.00000000"
```
### Get account balances
```python
account = client.get_account()
balances = [b for b in account["balances"] if float(b["free"]) > 0]
```
### Place a market buy order
```python
order = client.create_order(
symbol="BTCUSDT",
side=SIDE_BUY,
type=ORDER_TYPE_MARKET,
quoteOrderQty=100 # spend 100 USDT
)
```
### Place a limit sell order
```python
order = client.create_order(
symbol="BTCUSDT",
side=SIDE_SELL,
type=ORDER_TYPE_LIMIT,
timeInForce=TIME_IN_FORCE_GTC,
quantity=0.001,
price="60000"
)
```
### Get order status
```python
order = client.get_order(symbol="BTCUSDT", orderId=12345)
print(order["status"]) # "FILLED", "NEW", "CANCELED", etc.
```
### Cancel an order
```python
result = client.cancel_order(symbol="BTCUSDT", orderId=12345)
```
### Get historical klines (candlesticks)
```python
klines = client.get_historical_klines("BTCUSDT", Client.KLINE_INTERVAL_1HOUR, "1 day ago UTC")
# Each kline: [open_time, open, high, low, close, volume, close_time, ...]
```
### Get order book
```python
depth = client.get_order_book(symbol="BTCUSDT")
print(depth["bids"][:5]) # Top 5 bids [[price, qty], ...]
print(depth["asks"][:5]) # Top 5 asks
```
### Futures: place an order
```python
order = client.futures_create_order(
symbol="BTCUSDT",
side=SIDE_BUY,
type=FUTURE_ORDER_TYPE_MARKET,
quantity=0.001
)
```
### Futures: get position info
```python
positions = client.futures_position_information(symbol="BTCUSDT")
```
### WebSocket: stream real-time trades
```python
from binance import ThreadedWebsocketManager
twm = ThreadedWebsocketManager(api_key=api_key, api_secret=api_secret)
twm.start()
def handle_message(msg):
print(msg)
twm.start_kline_socket(callback=handle_message, symbol="BTCUSDT")
twm.join()
```
### Async WebSocket example
```python
from binance import AsyncClient, BinanceSocketManager
async def main():
client = await AsyncClient.create()
bsm = BinanceSocketManager(client)
async with bsm.trade_socket("BTCUSDT") as ts:
for _ in range(10):
msg = await ts.recv()
print(msg)
await client.close_connection()
```
## Error Handling
```python
from binance.exceptions import BinanceAPIException, BinanceOrderException
try:
order = client.create_order(...)
except BinanceAPIException as e:
print(e.code) # e.g., -1013
print(e.message) # e.g., "Filter failure: LOT_SIZE"
print(e.status_code) # HTTP status code
except BinanceOrderException as e:
print(e.code, e.message)
```
## WebSocket Managers
| Class | Use Case | Threading |
|-------|----------|-----------|
| `ThreadedWebsocketManager` | Simple threaded streaming | Thread-based |
| `BinanceSocketManager` | Async streaming | asyncio |
| `ThreadedDepthCacheManager` | Local order book cache (threaded) | Thread-based |
| `DepthCacheManager` | Local order book cache (async) | asyncio |
### Available socket types on BinanceSocketManager
`trade_socket`, `kline_socket`, `depth_socket`, `ticker_socket`, `symbol_ticker_socket`, `multiplex_socket`, `user_socket`, `futures_socket`, `coin_futures_socket`