agentbroker 1.0.1__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- agentbroker-1.0.1/PKG-INFO +377 -0
- agentbroker-1.0.1/README.md +342 -0
- agentbroker-1.0.1/agentbroker/__init__.py +78 -0
- agentbroker-1.0.1/agentbroker/client.py +696 -0
- agentbroker-1.0.1/agentbroker/exceptions.py +61 -0
- agentbroker-1.0.1/agentbroker/models.py +298 -0
- agentbroker-1.0.1/agentbroker/websocket.py +260 -0
- agentbroker-1.0.1/agentbroker.egg-info/PKG-INFO +377 -0
- agentbroker-1.0.1/agentbroker.egg-info/SOURCES.txt +12 -0
- agentbroker-1.0.1/agentbroker.egg-info/dependency_links.txt +1 -0
- agentbroker-1.0.1/agentbroker.egg-info/requires.txt +13 -0
- agentbroker-1.0.1/agentbroker.egg-info/top_level.txt +1 -0
- agentbroker-1.0.1/pyproject.toml +62 -0
- agentbroker-1.0.1/setup.cfg +4 -0
|
@@ -0,0 +1,377 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: agentbroker
|
|
3
|
+
Version: 1.0.1
|
|
4
|
+
Summary: Python SDK for AgentBroker — programmatic crypto trading for autonomous agents
|
|
5
|
+
License: MIT
|
|
6
|
+
Project-URL: Homepage, https://agentbroker.polsia.app
|
|
7
|
+
Project-URL: Documentation, https://agentbroker.polsia.app/docs
|
|
8
|
+
Project-URL: Repository, https://github.com/Polsia-Inc/agentbroker
|
|
9
|
+
Project-URL: Bug Tracker, https://github.com/Polsia-Inc/agentbroker/issues
|
|
10
|
+
Keywords: agentbroker,trading,crypto,sdk,api,algorithmic-trading,defi,solana
|
|
11
|
+
Classifier: Development Status :: 5 - Production/Stable
|
|
12
|
+
Classifier: Intended Audience :: Developers
|
|
13
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
14
|
+
Classifier: Programming Language :: Python :: 3
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.8
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
20
|
+
Classifier: Topic :: Office/Business :: Financial
|
|
21
|
+
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
22
|
+
Requires-Python: >=3.8
|
|
23
|
+
Description-Content-Type: text/markdown
|
|
24
|
+
Requires-Dist: requests>=2.28.0
|
|
25
|
+
Provides-Extra: ws
|
|
26
|
+
Requires-Dist: websocket-client>=1.6.0; extra == "ws"
|
|
27
|
+
Provides-Extra: dev
|
|
28
|
+
Requires-Dist: pytest>=7.0; extra == "dev"
|
|
29
|
+
Requires-Dist: pytest-mock>=3.10; extra == "dev"
|
|
30
|
+
Requires-Dist: responses>=0.23; extra == "dev"
|
|
31
|
+
Requires-Dist: black>=23.0; extra == "dev"
|
|
32
|
+
Requires-Dist: isort>=5.12; extra == "dev"
|
|
33
|
+
Requires-Dist: mypy>=1.0; extra == "dev"
|
|
34
|
+
Requires-Dist: websocket-client>=1.6.0; extra == "dev"
|
|
35
|
+
|
|
36
|
+
# AgentBroker Python SDK
|
|
37
|
+
|
|
38
|
+
[](https://pypi.org/project/agentbroker/)
|
|
39
|
+
[](https://pypi.org/project/agentbroker/)
|
|
40
|
+
[](https://python.org)
|
|
41
|
+
[](LICENSE)
|
|
42
|
+
|
|
43
|
+
Official Python SDK for [AgentBroker](https://agentbroker.polsia.app) — the crypto trading platform built for autonomous agents.
|
|
44
|
+
|
|
45
|
+
## Features
|
|
46
|
+
|
|
47
|
+
- **Full API coverage** — registration, deposits, orders, market data, WebSocket streaming
|
|
48
|
+
- **Type hints throughout** — autocomplete-friendly with `dataclass` response models
|
|
49
|
+
- **Smart error handling** — typed exceptions for auth failures, insufficient balance, not found, etc.
|
|
50
|
+
- **WebSocket helpers** — decorator-style event handlers with auto-reconnect
|
|
51
|
+
- **Zero extra dependencies** — only `requests` required; `websocket-client` optional for WS
|
|
52
|
+
|
|
53
|
+
## Installation
|
|
54
|
+
|
|
55
|
+
```bash
|
|
56
|
+
pip install agentbroker
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
With WebSocket support:
|
|
60
|
+
|
|
61
|
+
```bash
|
|
62
|
+
pip install "agentbroker[ws]"
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
## Quickstart
|
|
66
|
+
|
|
67
|
+
### 1. Register an agent
|
|
68
|
+
|
|
69
|
+
```python
|
|
70
|
+
from agentbroker import AgentBroker
|
|
71
|
+
|
|
72
|
+
agent = AgentBroker.register(
|
|
73
|
+
name="my-trading-bot",
|
|
74
|
+
email="me@example.com",
|
|
75
|
+
preferred_pairs=["BTC-USDC", "ETH-USDC"],
|
|
76
|
+
)
|
|
77
|
+
|
|
78
|
+
print(agent.api_key) # ← save this! it won't be shown again
|
|
79
|
+
# ab_live_xxxxxxxxxxxxx
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
> **Sandbox mode** — free 10,000 virtual USDC, no real funds needed:
|
|
83
|
+
> ```python
|
|
84
|
+
> agent = AgentBroker.register(name="my-bot", mode="sandbox")
|
|
85
|
+
> # starts with 10,000 virtual USDC — withdrawals disabled
|
|
86
|
+
> ```
|
|
87
|
+
|
|
88
|
+
### 2. Deposit USDC & trade
|
|
89
|
+
|
|
90
|
+
```python
|
|
91
|
+
client = AgentBroker(api_key="ab_live_xxxxxxxxxxxxx")
|
|
92
|
+
|
|
93
|
+
# Deposit
|
|
94
|
+
client.deposit(amount=1000)
|
|
95
|
+
|
|
96
|
+
# Choose a strategy
|
|
97
|
+
client.select_strategy("momentum") # "momentum" | "grid" | "mean_reversion"
|
|
98
|
+
|
|
99
|
+
# Market buy
|
|
100
|
+
order = client.place_order(
|
|
101
|
+
pair="BTC-USDC",
|
|
102
|
+
side="buy",
|
|
103
|
+
type="market",
|
|
104
|
+
quantity=0.001,
|
|
105
|
+
)
|
|
106
|
+
print(f"Order #{order.order_id}: {order.status}")
|
|
107
|
+
|
|
108
|
+
# Limit sell
|
|
109
|
+
order = client.place_order(
|
|
110
|
+
pair="BTC-USDC",
|
|
111
|
+
side="sell",
|
|
112
|
+
type="limit",
|
|
113
|
+
quantity=0.001,
|
|
114
|
+
price=120_000, # USDC
|
|
115
|
+
)
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
### 3. Real-time events via WebSocket
|
|
119
|
+
|
|
120
|
+
```python
|
|
121
|
+
from agentbroker import AgentBroker
|
|
122
|
+
|
|
123
|
+
client = AgentBroker(api_key="ab_live_xxxxxxxxxxxxx")
|
|
124
|
+
ws = client.connect_ws()
|
|
125
|
+
|
|
126
|
+
@ws.on_trade
|
|
127
|
+
def handle_trade(event):
|
|
128
|
+
print(f"Fill! {event['pair']} {event['side']} @ {event['price']}")
|
|
129
|
+
|
|
130
|
+
@ws.on_balance
|
|
131
|
+
def handle_balance(event):
|
|
132
|
+
print(f"Balance updated: {event['balance_usdc']} USDC")
|
|
133
|
+
|
|
134
|
+
@ws.on_connected
|
|
135
|
+
def on_connect():
|
|
136
|
+
print("WebSocket connected ✓")
|
|
137
|
+
|
|
138
|
+
ws.run_forever() # blocks — use ws.connect() for background thread
|
|
139
|
+
```
|
|
140
|
+
|
|
141
|
+
## API Reference
|
|
142
|
+
|
|
143
|
+
### `AgentBroker(api_key, base_url, timeout)`
|
|
144
|
+
|
|
145
|
+
```python
|
|
146
|
+
client = AgentBroker(api_key="ab_live_...", timeout=30)
|
|
147
|
+
```
|
|
148
|
+
|
|
149
|
+
| Parameter | Type | Default | Description |
|
|
150
|
+
|-----------|------|---------|-------------|
|
|
151
|
+
| `api_key` | `str` | `None` | API key (required for authenticated endpoints) |
|
|
152
|
+
| `base_url` | `str` | `https://agentbroker.polsia.app` | API base URL |
|
|
153
|
+
| `timeout` | `int` | `30` | HTTP timeout in seconds |
|
|
154
|
+
|
|
155
|
+
---
|
|
156
|
+
|
|
157
|
+
### Registration
|
|
158
|
+
|
|
159
|
+
#### `AgentBroker.register(name, email, callback_url, preferred_pairs)` → `Agent`
|
|
160
|
+
|
|
161
|
+
Register a new agent. Returns `Agent` with `api_key` set.
|
|
162
|
+
|
|
163
|
+
```python
|
|
164
|
+
agent = AgentBroker.register(
|
|
165
|
+
name="arb-bot-v2",
|
|
166
|
+
email="trader@example.com",
|
|
167
|
+
callback_url="https://mybot.example.com/webhooks/trades",
|
|
168
|
+
preferred_pairs=["BTC-USDC", "ETH-USDC", "SOL-USDC"],
|
|
169
|
+
)
|
|
170
|
+
print(agent.api_key)
|
|
171
|
+
```
|
|
172
|
+
|
|
173
|
+
---
|
|
174
|
+
|
|
175
|
+
### Account
|
|
176
|
+
|
|
177
|
+
| Method | Returns | Description |
|
|
178
|
+
|--------|---------|-------------|
|
|
179
|
+
| `client.get_profile()` | `Agent` | Full profile: balance, strategy, trade count |
|
|
180
|
+
| `client.update_profile(callback_url, preferred_pairs)` | `Agent` | Update webhook URL / pairs |
|
|
181
|
+
| `client.get_account()` | `Account` | Balance + totals summary |
|
|
182
|
+
| `client.get_balance()` | `float` | Current USDC balance |
|
|
183
|
+
|
|
184
|
+
---
|
|
185
|
+
|
|
186
|
+
### Deposits & Withdrawals
|
|
187
|
+
|
|
188
|
+
| Method | Returns | Description |
|
|
189
|
+
|--------|---------|-------------|
|
|
190
|
+
| `client.deposit(amount, currency, tx_hash)` | `Deposit` | Credit USDC to balance |
|
|
191
|
+
| `client.get_deposits()` | `list[Deposit]` | Deposit history |
|
|
192
|
+
| `client.withdraw(amount, address)` | `Withdrawal` | Withdraw USDC |
|
|
193
|
+
| `client.get_withdrawals()` | `list[Withdrawal]` | Withdrawal history |
|
|
194
|
+
| `client.get_balance_history()` | `list[BalanceHistory]` | All balance events |
|
|
195
|
+
|
|
196
|
+
---
|
|
197
|
+
|
|
198
|
+
### Strategies
|
|
199
|
+
|
|
200
|
+
| Method | Returns | Description |
|
|
201
|
+
|--------|---------|-------------|
|
|
202
|
+
| `client.get_strategies()` | `list[Strategy]` | Available strategies |
|
|
203
|
+
| `client.select_strategy(name)` | `dict` | Activate a strategy |
|
|
204
|
+
|
|
205
|
+
Available strategies: `"momentum"`, `"grid"`, `"mean_reversion"`
|
|
206
|
+
|
|
207
|
+
---
|
|
208
|
+
|
|
209
|
+
### Orders
|
|
210
|
+
|
|
211
|
+
| Method | Returns | Description |
|
|
212
|
+
|--------|---------|-------------|
|
|
213
|
+
| `client.place_order(pair, side, type, quantity, price)` | `Order` | Place limit or market order |
|
|
214
|
+
| `client.get_orders(status, pair, limit)` | `list[Order]` | List orders |
|
|
215
|
+
| `client.cancel_order(order_id)` | `dict` | Cancel open order |
|
|
216
|
+
| `client.get_trades(pair, limit)` | `list[Trade]` | Executed trades |
|
|
217
|
+
|
|
218
|
+
```python
|
|
219
|
+
# Market buy
|
|
220
|
+
order = client.place_order("ETH-USDC", "buy", "market", quantity=0.1)
|
|
221
|
+
|
|
222
|
+
# Limit sell
|
|
223
|
+
order = client.place_order("ETH-USDC", "sell", "limit", quantity=0.1, price=3500)
|
|
224
|
+
|
|
225
|
+
# List open orders
|
|
226
|
+
open_orders = client.get_orders(status="open", pair="BTC-USDC")
|
|
227
|
+
|
|
228
|
+
# Cancel
|
|
229
|
+
client.cancel_order(order.order_id)
|
|
230
|
+
```
|
|
231
|
+
|
|
232
|
+
---
|
|
233
|
+
|
|
234
|
+
### Market Data *(public, no API key required)*
|
|
235
|
+
|
|
236
|
+
| Method | Returns | Description |
|
|
237
|
+
|--------|---------|-------------|
|
|
238
|
+
| `client.get_prices()` | `dict[str, Price]` | All pair prices |
|
|
239
|
+
| `client.get_price(pair)` | `Price` | Single pair price |
|
|
240
|
+
| `client.get_orderbook(pair)` | `OrderBook` | Bids/asks for a pair |
|
|
241
|
+
| `client.get_pairs()` | `list[str]` | Supported trading pairs |
|
|
242
|
+
|
|
243
|
+
```python
|
|
244
|
+
# No API key needed
|
|
245
|
+
client = AgentBroker()
|
|
246
|
+
|
|
247
|
+
prices = client.get_prices()
|
|
248
|
+
btc = prices["BTC-USDC"]
|
|
249
|
+
print(f"BTC: ${btc.price:,.2f}")
|
|
250
|
+
|
|
251
|
+
ob = client.get_orderbook("ETH-USDC")
|
|
252
|
+
print(f"Best bid: {ob.bids[0].price}, Best ask: {ob.asks[0].price}")
|
|
253
|
+
|
|
254
|
+
pairs = client.get_pairs()
|
|
255
|
+
# ["BTC-USDC", "ETH-USDC", "SOL-USDC", ...]
|
|
256
|
+
```
|
|
257
|
+
|
|
258
|
+
---
|
|
259
|
+
|
|
260
|
+
### WebSocket
|
|
261
|
+
|
|
262
|
+
```python
|
|
263
|
+
ws = client.connect_ws()
|
|
264
|
+
|
|
265
|
+
@ws.on_trade
|
|
266
|
+
def on_trade(event): ...
|
|
267
|
+
|
|
268
|
+
@ws.on_balance
|
|
269
|
+
def on_balance(event): ...
|
|
270
|
+
|
|
271
|
+
@ws.on_orderbook
|
|
272
|
+
def on_orderbook(event): ...
|
|
273
|
+
|
|
274
|
+
@ws.on_connected
|
|
275
|
+
def on_connected(): ...
|
|
276
|
+
|
|
277
|
+
@ws.on_disconnect
|
|
278
|
+
def on_disconnect(code, msg): ...
|
|
279
|
+
|
|
280
|
+
@ws.on_error
|
|
281
|
+
def on_error(err): ...
|
|
282
|
+
|
|
283
|
+
# Subscribe / unsubscribe channels at runtime
|
|
284
|
+
ws.subscribe("trades")
|
|
285
|
+
ws.unsubscribe("orderbook")
|
|
286
|
+
|
|
287
|
+
# Run in background thread
|
|
288
|
+
ws.connect()
|
|
289
|
+
|
|
290
|
+
# Or block main thread
|
|
291
|
+
ws.run_forever()
|
|
292
|
+
|
|
293
|
+
# Close permanently
|
|
294
|
+
ws.close()
|
|
295
|
+
```
|
|
296
|
+
|
|
297
|
+
**Channels:** `"trades"`, `"balance"`, `"orderbook"`
|
|
298
|
+
|
|
299
|
+
---
|
|
300
|
+
|
|
301
|
+
### Exceptions
|
|
302
|
+
|
|
303
|
+
```python
|
|
304
|
+
from agentbroker.exceptions import (
|
|
305
|
+
AgentBrokerError, # Base class
|
|
306
|
+
AuthenticationError, # 401 — missing/invalid API key
|
|
307
|
+
ValidationError, # 400 — bad request params
|
|
308
|
+
InsufficientBalanceError,# 400 with code INSUFFICIENT_BALANCE
|
|
309
|
+
NotFoundError, # 404 — resource not found
|
|
310
|
+
RateLimitError, # 429 — too many requests
|
|
311
|
+
ServerError, # 5xx — server-side error
|
|
312
|
+
ConnectionError, # Network failure / timeout
|
|
313
|
+
WebSocketError, # WS connection issues
|
|
314
|
+
)
|
|
315
|
+
|
|
316
|
+
try:
|
|
317
|
+
order = client.place_order("BTC-USDC", "buy", "market", quantity=10)
|
|
318
|
+
except InsufficientBalanceError as e:
|
|
319
|
+
print(f"Need {e.required} USDC, have {e.available} USDC")
|
|
320
|
+
except AuthenticationError:
|
|
321
|
+
print("Check your API key")
|
|
322
|
+
except AgentBrokerError as e:
|
|
323
|
+
print(f"[{e.code}] {e.message}")
|
|
324
|
+
```
|
|
325
|
+
|
|
326
|
+
---
|
|
327
|
+
|
|
328
|
+
## Examples
|
|
329
|
+
|
|
330
|
+
| File | Description |
|
|
331
|
+
|------|-------------|
|
|
332
|
+
| [`examples/basic_trade.py`](examples/basic_trade.py) | Register, deposit, place market + limit orders |
|
|
333
|
+
| [`examples/momentum_bot.py`](examples/momentum_bot.py) | Trend-following bot with stop-loss + WebSocket balance updates |
|
|
334
|
+
| [`examples/arbitrage_scanner.py`](examples/arbitrage_scanner.py) | Scan order books for wide spreads + triangular arb signals |
|
|
335
|
+
|
|
336
|
+
Run any example:
|
|
337
|
+
|
|
338
|
+
```bash
|
|
339
|
+
export AGENTBROKER_API_KEY=ab_live_your_key_here
|
|
340
|
+
python examples/basic_trade.py
|
|
341
|
+
```
|
|
342
|
+
|
|
343
|
+
---
|
|
344
|
+
|
|
345
|
+
## Environment Variables
|
|
346
|
+
|
|
347
|
+
| Variable | Description |
|
|
348
|
+
|----------|-------------|
|
|
349
|
+
| `AGENTBROKER_API_KEY` | Your API key (used by examples) |
|
|
350
|
+
| `AGENTBROKER_BASE_URL` | Override base URL (default: `https://agentbroker.polsia.app`) |
|
|
351
|
+
|
|
352
|
+
---
|
|
353
|
+
|
|
354
|
+
## Development
|
|
355
|
+
|
|
356
|
+
```bash
|
|
357
|
+
git clone https://github.com/Polsia-Inc/agentbroker
|
|
358
|
+
cd agentbroker/python-sdk
|
|
359
|
+
|
|
360
|
+
pip install -e ".[dev]"
|
|
361
|
+
|
|
362
|
+
# Run tests
|
|
363
|
+
pytest
|
|
364
|
+
|
|
365
|
+
# Format
|
|
366
|
+
black agentbroker/ examples/
|
|
367
|
+
isort agentbroker/ examples/
|
|
368
|
+
|
|
369
|
+
# Type check
|
|
370
|
+
mypy agentbroker/
|
|
371
|
+
```
|
|
372
|
+
|
|
373
|
+
---
|
|
374
|
+
|
|
375
|
+
## License
|
|
376
|
+
|
|
377
|
+
MIT © AgentBroker
|
|
@@ -0,0 +1,342 @@
|
|
|
1
|
+
# AgentBroker Python SDK
|
|
2
|
+
|
|
3
|
+
[](https://pypi.org/project/agentbroker/)
|
|
4
|
+
[](https://pypi.org/project/agentbroker/)
|
|
5
|
+
[](https://python.org)
|
|
6
|
+
[](LICENSE)
|
|
7
|
+
|
|
8
|
+
Official Python SDK for [AgentBroker](https://agentbroker.polsia.app) — the crypto trading platform built for autonomous agents.
|
|
9
|
+
|
|
10
|
+
## Features
|
|
11
|
+
|
|
12
|
+
- **Full API coverage** — registration, deposits, orders, market data, WebSocket streaming
|
|
13
|
+
- **Type hints throughout** — autocomplete-friendly with `dataclass` response models
|
|
14
|
+
- **Smart error handling** — typed exceptions for auth failures, insufficient balance, not found, etc.
|
|
15
|
+
- **WebSocket helpers** — decorator-style event handlers with auto-reconnect
|
|
16
|
+
- **Zero extra dependencies** — only `requests` required; `websocket-client` optional for WS
|
|
17
|
+
|
|
18
|
+
## Installation
|
|
19
|
+
|
|
20
|
+
```bash
|
|
21
|
+
pip install agentbroker
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
With WebSocket support:
|
|
25
|
+
|
|
26
|
+
```bash
|
|
27
|
+
pip install "agentbroker[ws]"
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
## Quickstart
|
|
31
|
+
|
|
32
|
+
### 1. Register an agent
|
|
33
|
+
|
|
34
|
+
```python
|
|
35
|
+
from agentbroker import AgentBroker
|
|
36
|
+
|
|
37
|
+
agent = AgentBroker.register(
|
|
38
|
+
name="my-trading-bot",
|
|
39
|
+
email="me@example.com",
|
|
40
|
+
preferred_pairs=["BTC-USDC", "ETH-USDC"],
|
|
41
|
+
)
|
|
42
|
+
|
|
43
|
+
print(agent.api_key) # ← save this! it won't be shown again
|
|
44
|
+
# ab_live_xxxxxxxxxxxxx
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
> **Sandbox mode** — free 10,000 virtual USDC, no real funds needed:
|
|
48
|
+
> ```python
|
|
49
|
+
> agent = AgentBroker.register(name="my-bot", mode="sandbox")
|
|
50
|
+
> # starts with 10,000 virtual USDC — withdrawals disabled
|
|
51
|
+
> ```
|
|
52
|
+
|
|
53
|
+
### 2. Deposit USDC & trade
|
|
54
|
+
|
|
55
|
+
```python
|
|
56
|
+
client = AgentBroker(api_key="ab_live_xxxxxxxxxxxxx")
|
|
57
|
+
|
|
58
|
+
# Deposit
|
|
59
|
+
client.deposit(amount=1000)
|
|
60
|
+
|
|
61
|
+
# Choose a strategy
|
|
62
|
+
client.select_strategy("momentum") # "momentum" | "grid" | "mean_reversion"
|
|
63
|
+
|
|
64
|
+
# Market buy
|
|
65
|
+
order = client.place_order(
|
|
66
|
+
pair="BTC-USDC",
|
|
67
|
+
side="buy",
|
|
68
|
+
type="market",
|
|
69
|
+
quantity=0.001,
|
|
70
|
+
)
|
|
71
|
+
print(f"Order #{order.order_id}: {order.status}")
|
|
72
|
+
|
|
73
|
+
# Limit sell
|
|
74
|
+
order = client.place_order(
|
|
75
|
+
pair="BTC-USDC",
|
|
76
|
+
side="sell",
|
|
77
|
+
type="limit",
|
|
78
|
+
quantity=0.001,
|
|
79
|
+
price=120_000, # USDC
|
|
80
|
+
)
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
### 3. Real-time events via WebSocket
|
|
84
|
+
|
|
85
|
+
```python
|
|
86
|
+
from agentbroker import AgentBroker
|
|
87
|
+
|
|
88
|
+
client = AgentBroker(api_key="ab_live_xxxxxxxxxxxxx")
|
|
89
|
+
ws = client.connect_ws()
|
|
90
|
+
|
|
91
|
+
@ws.on_trade
|
|
92
|
+
def handle_trade(event):
|
|
93
|
+
print(f"Fill! {event['pair']} {event['side']} @ {event['price']}")
|
|
94
|
+
|
|
95
|
+
@ws.on_balance
|
|
96
|
+
def handle_balance(event):
|
|
97
|
+
print(f"Balance updated: {event['balance_usdc']} USDC")
|
|
98
|
+
|
|
99
|
+
@ws.on_connected
|
|
100
|
+
def on_connect():
|
|
101
|
+
print("WebSocket connected ✓")
|
|
102
|
+
|
|
103
|
+
ws.run_forever() # blocks — use ws.connect() for background thread
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
## API Reference
|
|
107
|
+
|
|
108
|
+
### `AgentBroker(api_key, base_url, timeout)`
|
|
109
|
+
|
|
110
|
+
```python
|
|
111
|
+
client = AgentBroker(api_key="ab_live_...", timeout=30)
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
| Parameter | Type | Default | Description |
|
|
115
|
+
|-----------|------|---------|-------------|
|
|
116
|
+
| `api_key` | `str` | `None` | API key (required for authenticated endpoints) |
|
|
117
|
+
| `base_url` | `str` | `https://agentbroker.polsia.app` | API base URL |
|
|
118
|
+
| `timeout` | `int` | `30` | HTTP timeout in seconds |
|
|
119
|
+
|
|
120
|
+
---
|
|
121
|
+
|
|
122
|
+
### Registration
|
|
123
|
+
|
|
124
|
+
#### `AgentBroker.register(name, email, callback_url, preferred_pairs)` → `Agent`
|
|
125
|
+
|
|
126
|
+
Register a new agent. Returns `Agent` with `api_key` set.
|
|
127
|
+
|
|
128
|
+
```python
|
|
129
|
+
agent = AgentBroker.register(
|
|
130
|
+
name="arb-bot-v2",
|
|
131
|
+
email="trader@example.com",
|
|
132
|
+
callback_url="https://mybot.example.com/webhooks/trades",
|
|
133
|
+
preferred_pairs=["BTC-USDC", "ETH-USDC", "SOL-USDC"],
|
|
134
|
+
)
|
|
135
|
+
print(agent.api_key)
|
|
136
|
+
```
|
|
137
|
+
|
|
138
|
+
---
|
|
139
|
+
|
|
140
|
+
### Account
|
|
141
|
+
|
|
142
|
+
| Method | Returns | Description |
|
|
143
|
+
|--------|---------|-------------|
|
|
144
|
+
| `client.get_profile()` | `Agent` | Full profile: balance, strategy, trade count |
|
|
145
|
+
| `client.update_profile(callback_url, preferred_pairs)` | `Agent` | Update webhook URL / pairs |
|
|
146
|
+
| `client.get_account()` | `Account` | Balance + totals summary |
|
|
147
|
+
| `client.get_balance()` | `float` | Current USDC balance |
|
|
148
|
+
|
|
149
|
+
---
|
|
150
|
+
|
|
151
|
+
### Deposits & Withdrawals
|
|
152
|
+
|
|
153
|
+
| Method | Returns | Description |
|
|
154
|
+
|--------|---------|-------------|
|
|
155
|
+
| `client.deposit(amount, currency, tx_hash)` | `Deposit` | Credit USDC to balance |
|
|
156
|
+
| `client.get_deposits()` | `list[Deposit]` | Deposit history |
|
|
157
|
+
| `client.withdraw(amount, address)` | `Withdrawal` | Withdraw USDC |
|
|
158
|
+
| `client.get_withdrawals()` | `list[Withdrawal]` | Withdrawal history |
|
|
159
|
+
| `client.get_balance_history()` | `list[BalanceHistory]` | All balance events |
|
|
160
|
+
|
|
161
|
+
---
|
|
162
|
+
|
|
163
|
+
### Strategies
|
|
164
|
+
|
|
165
|
+
| Method | Returns | Description |
|
|
166
|
+
|--------|---------|-------------|
|
|
167
|
+
| `client.get_strategies()` | `list[Strategy]` | Available strategies |
|
|
168
|
+
| `client.select_strategy(name)` | `dict` | Activate a strategy |
|
|
169
|
+
|
|
170
|
+
Available strategies: `"momentum"`, `"grid"`, `"mean_reversion"`
|
|
171
|
+
|
|
172
|
+
---
|
|
173
|
+
|
|
174
|
+
### Orders
|
|
175
|
+
|
|
176
|
+
| Method | Returns | Description |
|
|
177
|
+
|--------|---------|-------------|
|
|
178
|
+
| `client.place_order(pair, side, type, quantity, price)` | `Order` | Place limit or market order |
|
|
179
|
+
| `client.get_orders(status, pair, limit)` | `list[Order]` | List orders |
|
|
180
|
+
| `client.cancel_order(order_id)` | `dict` | Cancel open order |
|
|
181
|
+
| `client.get_trades(pair, limit)` | `list[Trade]` | Executed trades |
|
|
182
|
+
|
|
183
|
+
```python
|
|
184
|
+
# Market buy
|
|
185
|
+
order = client.place_order("ETH-USDC", "buy", "market", quantity=0.1)
|
|
186
|
+
|
|
187
|
+
# Limit sell
|
|
188
|
+
order = client.place_order("ETH-USDC", "sell", "limit", quantity=0.1, price=3500)
|
|
189
|
+
|
|
190
|
+
# List open orders
|
|
191
|
+
open_orders = client.get_orders(status="open", pair="BTC-USDC")
|
|
192
|
+
|
|
193
|
+
# Cancel
|
|
194
|
+
client.cancel_order(order.order_id)
|
|
195
|
+
```
|
|
196
|
+
|
|
197
|
+
---
|
|
198
|
+
|
|
199
|
+
### Market Data *(public, no API key required)*
|
|
200
|
+
|
|
201
|
+
| Method | Returns | Description |
|
|
202
|
+
|--------|---------|-------------|
|
|
203
|
+
| `client.get_prices()` | `dict[str, Price]` | All pair prices |
|
|
204
|
+
| `client.get_price(pair)` | `Price` | Single pair price |
|
|
205
|
+
| `client.get_orderbook(pair)` | `OrderBook` | Bids/asks for a pair |
|
|
206
|
+
| `client.get_pairs()` | `list[str]` | Supported trading pairs |
|
|
207
|
+
|
|
208
|
+
```python
|
|
209
|
+
# No API key needed
|
|
210
|
+
client = AgentBroker()
|
|
211
|
+
|
|
212
|
+
prices = client.get_prices()
|
|
213
|
+
btc = prices["BTC-USDC"]
|
|
214
|
+
print(f"BTC: ${btc.price:,.2f}")
|
|
215
|
+
|
|
216
|
+
ob = client.get_orderbook("ETH-USDC")
|
|
217
|
+
print(f"Best bid: {ob.bids[0].price}, Best ask: {ob.asks[0].price}")
|
|
218
|
+
|
|
219
|
+
pairs = client.get_pairs()
|
|
220
|
+
# ["BTC-USDC", "ETH-USDC", "SOL-USDC", ...]
|
|
221
|
+
```
|
|
222
|
+
|
|
223
|
+
---
|
|
224
|
+
|
|
225
|
+
### WebSocket
|
|
226
|
+
|
|
227
|
+
```python
|
|
228
|
+
ws = client.connect_ws()
|
|
229
|
+
|
|
230
|
+
@ws.on_trade
|
|
231
|
+
def on_trade(event): ...
|
|
232
|
+
|
|
233
|
+
@ws.on_balance
|
|
234
|
+
def on_balance(event): ...
|
|
235
|
+
|
|
236
|
+
@ws.on_orderbook
|
|
237
|
+
def on_orderbook(event): ...
|
|
238
|
+
|
|
239
|
+
@ws.on_connected
|
|
240
|
+
def on_connected(): ...
|
|
241
|
+
|
|
242
|
+
@ws.on_disconnect
|
|
243
|
+
def on_disconnect(code, msg): ...
|
|
244
|
+
|
|
245
|
+
@ws.on_error
|
|
246
|
+
def on_error(err): ...
|
|
247
|
+
|
|
248
|
+
# Subscribe / unsubscribe channels at runtime
|
|
249
|
+
ws.subscribe("trades")
|
|
250
|
+
ws.unsubscribe("orderbook")
|
|
251
|
+
|
|
252
|
+
# Run in background thread
|
|
253
|
+
ws.connect()
|
|
254
|
+
|
|
255
|
+
# Or block main thread
|
|
256
|
+
ws.run_forever()
|
|
257
|
+
|
|
258
|
+
# Close permanently
|
|
259
|
+
ws.close()
|
|
260
|
+
```
|
|
261
|
+
|
|
262
|
+
**Channels:** `"trades"`, `"balance"`, `"orderbook"`
|
|
263
|
+
|
|
264
|
+
---
|
|
265
|
+
|
|
266
|
+
### Exceptions
|
|
267
|
+
|
|
268
|
+
```python
|
|
269
|
+
from agentbroker.exceptions import (
|
|
270
|
+
AgentBrokerError, # Base class
|
|
271
|
+
AuthenticationError, # 401 — missing/invalid API key
|
|
272
|
+
ValidationError, # 400 — bad request params
|
|
273
|
+
InsufficientBalanceError,# 400 with code INSUFFICIENT_BALANCE
|
|
274
|
+
NotFoundError, # 404 — resource not found
|
|
275
|
+
RateLimitError, # 429 — too many requests
|
|
276
|
+
ServerError, # 5xx — server-side error
|
|
277
|
+
ConnectionError, # Network failure / timeout
|
|
278
|
+
WebSocketError, # WS connection issues
|
|
279
|
+
)
|
|
280
|
+
|
|
281
|
+
try:
|
|
282
|
+
order = client.place_order("BTC-USDC", "buy", "market", quantity=10)
|
|
283
|
+
except InsufficientBalanceError as e:
|
|
284
|
+
print(f"Need {e.required} USDC, have {e.available} USDC")
|
|
285
|
+
except AuthenticationError:
|
|
286
|
+
print("Check your API key")
|
|
287
|
+
except AgentBrokerError as e:
|
|
288
|
+
print(f"[{e.code}] {e.message}")
|
|
289
|
+
```
|
|
290
|
+
|
|
291
|
+
---
|
|
292
|
+
|
|
293
|
+
## Examples
|
|
294
|
+
|
|
295
|
+
| File | Description |
|
|
296
|
+
|------|-------------|
|
|
297
|
+
| [`examples/basic_trade.py`](examples/basic_trade.py) | Register, deposit, place market + limit orders |
|
|
298
|
+
| [`examples/momentum_bot.py`](examples/momentum_bot.py) | Trend-following bot with stop-loss + WebSocket balance updates |
|
|
299
|
+
| [`examples/arbitrage_scanner.py`](examples/arbitrage_scanner.py) | Scan order books for wide spreads + triangular arb signals |
|
|
300
|
+
|
|
301
|
+
Run any example:
|
|
302
|
+
|
|
303
|
+
```bash
|
|
304
|
+
export AGENTBROKER_API_KEY=ab_live_your_key_here
|
|
305
|
+
python examples/basic_trade.py
|
|
306
|
+
```
|
|
307
|
+
|
|
308
|
+
---
|
|
309
|
+
|
|
310
|
+
## Environment Variables
|
|
311
|
+
|
|
312
|
+
| Variable | Description |
|
|
313
|
+
|----------|-------------|
|
|
314
|
+
| `AGENTBROKER_API_KEY` | Your API key (used by examples) |
|
|
315
|
+
| `AGENTBROKER_BASE_URL` | Override base URL (default: `https://agentbroker.polsia.app`) |
|
|
316
|
+
|
|
317
|
+
---
|
|
318
|
+
|
|
319
|
+
## Development
|
|
320
|
+
|
|
321
|
+
```bash
|
|
322
|
+
git clone https://github.com/Polsia-Inc/agentbroker
|
|
323
|
+
cd agentbroker/python-sdk
|
|
324
|
+
|
|
325
|
+
pip install -e ".[dev]"
|
|
326
|
+
|
|
327
|
+
# Run tests
|
|
328
|
+
pytest
|
|
329
|
+
|
|
330
|
+
# Format
|
|
331
|
+
black agentbroker/ examples/
|
|
332
|
+
isort agentbroker/ examples/
|
|
333
|
+
|
|
334
|
+
# Type check
|
|
335
|
+
mypy agentbroker/
|
|
336
|
+
```
|
|
337
|
+
|
|
338
|
+
---
|
|
339
|
+
|
|
340
|
+
## License
|
|
341
|
+
|
|
342
|
+
MIT © AgentBroker
|