@nemesis-oss/binance-sdk 1.0.0
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.
- package/README.md +198 -0
- package/dist/index-D2T_Rhwj.d.cts +2055 -0
- package/dist/index-D2T_Rhwj.d.ts +2055 -0
- package/dist/index.cjs +4586 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +327 -0
- package/dist/index.d.ts +327 -0
- package/dist/index.js +4402 -0
- package/dist/index.js.map +1 -0
- package/dist/mcp/index.cjs +4090 -0
- package/dist/mcp/index.cjs.map +1 -0
- package/dist/mcp/index.d.cts +1 -0
- package/dist/mcp/index.d.ts +1 -0
- package/dist/mcp/index.js +4067 -0
- package/dist/mcp/index.js.map +1 -0
- package/dist/tools/index.cjs +1538 -0
- package/dist/tools/index.cjs.map +1 -0
- package/dist/tools/index.d.cts +3 -0
- package/dist/tools/index.d.ts +3 -0
- package/dist/tools/index.js +1494 -0
- package/dist/tools/index.js.map +1 -0
- package/package.json +86 -0
package/README.md
ADDED
|
@@ -0,0 +1,198 @@
|
|
|
1
|
+
# binance-sdk
|
|
2
|
+
|
|
3
|
+
TypeScript SDK for Binance — **Spot + USD-M Futures**, REST + WebSocket, public market data
|
|
4
|
+
and authenticated trading, with zod-validated typed responses.
|
|
5
|
+
|
|
6
|
+
Canonical Binance client for the `trading-workspace` `sdk/` directory (mirrors `sdk/dhanhq-ts`'s
|
|
7
|
+
role for DhanHQ). Feature-parity with `binance-client-js` (REST + WS), with typed schemas.
|
|
8
|
+
|
|
9
|
+
## Install
|
|
10
|
+
|
|
11
|
+
```bash
|
|
12
|
+
npm install @nemesis-oss/binance-sdk
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
## Usage
|
|
16
|
+
|
|
17
|
+
```typescript
|
|
18
|
+
import { BinanceClient } from '@nemesis-oss/binance-sdk';
|
|
19
|
+
|
|
20
|
+
const client = new BinanceClient({
|
|
21
|
+
apiKey: 'YOUR_API_KEY', // only needed for authenticated endpoints
|
|
22
|
+
apiSecret: 'YOUR_API_SECRET',
|
|
23
|
+
testnet: true, // or demo: true; defaults to live
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
// Public market data (no keys needed)
|
|
27
|
+
const klines = await client.spot.market.klines('SOLUSDT', '15m', { limit: 500 });
|
|
28
|
+
const funding = await client.futures.data.fundingRateHistory('ETHUSDT', { limit: 100 });
|
|
29
|
+
const oi = await client.futures.data.openInterest('XRPUSDT');
|
|
30
|
+
|
|
31
|
+
// Authenticated account
|
|
32
|
+
const balance = await client.futures.account.balance();
|
|
33
|
+
const positions = await client.futures.account.positionRisk();
|
|
34
|
+
|
|
35
|
+
// Composite ops — sizing/rounding handled for you
|
|
36
|
+
const sizing = await client.futures.ops.sizePosition({
|
|
37
|
+
symbol: 'BTCUSDT',
|
|
38
|
+
side: 'BUY',
|
|
39
|
+
stopPrice: 59000,
|
|
40
|
+
riskAmount: 100, // or riskPct: 1
|
|
41
|
+
leverage: 10,
|
|
42
|
+
});
|
|
43
|
+
if (sizing.ok) {
|
|
44
|
+
await client.futures.ops.placeBracketOrder({
|
|
45
|
+
symbol: 'BTCUSDT',
|
|
46
|
+
side: 'BUY',
|
|
47
|
+
quantity: sizing.quantityStr,
|
|
48
|
+
stopLossPrice: 59000,
|
|
49
|
+
takeProfitPrice: 63000,
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
await client.futures.ops.closePosition({ symbol: 'BTCUSDT' });
|
|
53
|
+
|
|
54
|
+
// Trading
|
|
55
|
+
const order = await client.futures.trading.createOrder({
|
|
56
|
+
symbol: 'BTCUSDT',
|
|
57
|
+
side: 'BUY',
|
|
58
|
+
type: 'LIMIT',
|
|
59
|
+
quantity: 0.01,
|
|
60
|
+
price: 60000,
|
|
61
|
+
timeInForce: 'GTC',
|
|
62
|
+
});
|
|
63
|
+
await client.futures.trading.cancelOrder('BTCUSDT', { orderId: order.orderId });
|
|
64
|
+
|
|
65
|
+
// Spot account + trading
|
|
66
|
+
const spotAccount = await client.spot.account.account();
|
|
67
|
+
const spotOrder = await client.spot.trading.createOrder({
|
|
68
|
+
symbol: 'BTCUSDT',
|
|
69
|
+
side: 'BUY',
|
|
70
|
+
type: 'LIMIT',
|
|
71
|
+
quantity: '0.001',
|
|
72
|
+
price: '60000',
|
|
73
|
+
timeInForce: 'GTC',
|
|
74
|
+
});
|
|
75
|
+
await client.spot.trading.cancelOrder('BTCUSDT', { orderId: spotOrder.orderId });
|
|
76
|
+
|
|
77
|
+
// Market WebSocket (combined stream, auto-reconnect)
|
|
78
|
+
client.futures.ws.subscribe([
|
|
79
|
+
client.futures.ws.kline('SOLUSDT', '15m'),
|
|
80
|
+
client.futures.ws.markPrice('ETHUSDT', '1s'),
|
|
81
|
+
]);
|
|
82
|
+
client.futures.ws.on('message', (stream, payload) => console.log(stream, payload));
|
|
83
|
+
|
|
84
|
+
// User data stream (listenKey lifecycle managed automatically)
|
|
85
|
+
const listenKey = await client.startUserStream();
|
|
86
|
+
client.futures.wsUser.on('ORDER_TRADE_UPDATE', (event) => console.log(event.o));
|
|
87
|
+
client.futures.wsUser.on('ACCOUNT_UPDATE', (event) => console.log(event.a));
|
|
88
|
+
client.closeUserStream();
|
|
89
|
+
|
|
90
|
+
// Spot user data stream
|
|
91
|
+
await client.startSpotUserStream();
|
|
92
|
+
client.spot.wsUser.on('executionReport', (event) => console.log(event.s));
|
|
93
|
+
client.closeSpotUserStream();
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
## API Surface
|
|
97
|
+
|
|
98
|
+
### Spot (`client.spot`)
|
|
99
|
+
|
|
100
|
+
- `market` — public REST (klines + uiKlines, tickers incl. rolling-window & trading-day, depth,
|
|
101
|
+
trades, aggTrades, exchangeInfo, avgPrice)
|
|
102
|
+
- `account` — authenticated account (account info, myTrades, myPreventedMatches, commission, rate limits)
|
|
103
|
+
- `trading` — order lifecycle (create/test/get/cancel, open/all orders, cancelReplace, OCO order lists)
|
|
104
|
+
- `userStream` — listenKey lifecycle (create / keep-alive / close)
|
|
105
|
+
- `ws` — market WebSocket streams (kline, trade, aggTrade, depth incl. diff-depth, ticker incl.
|
|
106
|
+
rolling-window, bookTicker, miniTicker, avgPrice + all-market/arr variants)
|
|
107
|
+
- `wsUser` — spot user data stream (executionReport, outboundAccountPosition, balanceUpdate, listStatus)
|
|
108
|
+
|
|
109
|
+
### Futures (`client.futures`)
|
|
110
|
+
|
|
111
|
+
- `market` — public REST market data (klines incl. continuous/index/mark/premium-index variants,
|
|
112
|
+
tickers, depth incl. RPI depth, trades, aggTrades, exchangeInfo)
|
|
113
|
+
- `data` — futures analytics (funding rate, premium index, open interest, long/short ratios, basis,
|
|
114
|
+
delivery price, insurance fund balance, ADL risk, force orders, index constituents, delist schedule)
|
|
115
|
+
- `account` — authenticated account endpoints (balance v2/v3, account v2/v3, positionRisk v2/v3,
|
|
116
|
+
account config, income, userTrades, commission, leverage brackets, position mode, multi-assets
|
|
117
|
+
margin, fee burn, API trading status, portfolio margin account info, position margin history,
|
|
118
|
+
rate limit orders, order/trade/income data downloads)
|
|
119
|
+
- `trading` — order lifecycle (create/test/get/cancel/modify, open/all orders, batch orders,
|
|
120
|
+
algo orders, order-modify history, leverage/margin/countdown-cancel config, Convert
|
|
121
|
+
quote/accept/status)
|
|
122
|
+
- `ops` — composite operations layered over the above: `symbolRules` (typed tick/step/notional
|
|
123
|
+
filters), `quantize`, `sizePosition` (risk-based sizing), `closePosition`, `marketSnapshot`,
|
|
124
|
+
`accountOverview`, `placeBracketOrder`
|
|
125
|
+
- `userStream` — listenKey lifecycle (create / keep-alive / close)
|
|
126
|
+
- `ws` — market WebSocket streams (kline, continuous/index/mark klines, aggTrade, trade, depth
|
|
127
|
+
incl. full order-book diff-depth, ticker, rolling-window ticker + all-market variant, mark price,
|
|
128
|
+
book ticker, mini ticker, liquidations, composite index, asset index + all-market/arr variants)
|
|
129
|
+
- `wsUser` — user data stream (ACCOUNT_UPDATE, ORDER_TRADE_UPDATE, MARGIN_CALL; auto-reconnect)
|
|
130
|
+
- `wsApi` — WebSocket API: signed trading (order.place/cancel/modify/status, algoOrder.place/cancel,
|
|
131
|
+
orderList.place/cancel/status, account.status/position, userDataStream.start/ping/stop) and public
|
|
132
|
+
market data (time, exchangeInfo, klines, aggTrades, trades, depth, avgPrice,
|
|
133
|
+
ticker.price/bookTicker/24hr)
|
|
134
|
+
|
|
135
|
+
### Client options
|
|
136
|
+
|
|
137
|
+
`apiKey`, `apiSecret`, `testnet`, `demo`, `recvWindow`, `apiBase`, `wsBase`, `wsUserBase`,
|
|
138
|
+
`wsApiBase`, `timeoutMs`, `maxRetries`, `retryBaseDelayMs`, `retryMaxDelayMs`.
|
|
139
|
+
|
|
140
|
+
### Errors
|
|
141
|
+
|
|
142
|
+
`BinanceError` base, `BinanceAuthError`, `BinanceApiError` (code + status), `RateLimitError`,
|
|
143
|
+
`NetworkError`.
|
|
144
|
+
|
|
145
|
+
### Paper trading
|
|
146
|
+
|
|
147
|
+
Simulates fills against live public prices — nothing is sent to the exchange. Margin is locked
|
|
148
|
+
at the requested leverage and released pro-rata as a position is reduced.
|
|
149
|
+
|
|
150
|
+
```typescript
|
|
151
|
+
import { PaperTradingEngine } from '@nemesis-oss/binance-sdk';
|
|
152
|
+
|
|
153
|
+
const engine = new PaperTradingEngine({ initialBalance: 10_000 });
|
|
154
|
+
await engine.placeOrder({ symbol: 'BTCUSDT', side: 'BUY', type: 'MARKET', quantity: 0.05, leverage: 5 });
|
|
155
|
+
await engine.updatePositions(); // re-mark against live prices
|
|
156
|
+
const close = await engine.placeOrder({ symbol: 'BTCUSDT', side: 'SELL', type: 'MARKET', quantity: 0.05 });
|
|
157
|
+
console.log(close.realizedPnl, engine.getAccountInfo().balance);
|
|
158
|
+
```
|
|
159
|
+
|
|
160
|
+
## LLM Tools & MCP
|
|
161
|
+
|
|
162
|
+
The SDK ships a framework-agnostic tool layer plus an MCP server, so any function-calling agent
|
|
163
|
+
(OpenAI, Anthropic/Claude, MCP hosts) can drive the client.
|
|
164
|
+
|
|
165
|
+
```ts
|
|
166
|
+
import { BinanceClient, createFuturesToolkit, toolkitToFormats } from '@nemesis-oss/binance-sdk';
|
|
167
|
+
const tk = createFuturesToolkit(new BinanceClient({ apiKey, apiSecret, testnet }));
|
|
168
|
+
const { openai, anthropic, mcp } = toolkitToFormats(tk); // tool schemas per format
|
|
169
|
+
```
|
|
170
|
+
|
|
171
|
+
- **Tool groups**: `market`, `account`, `trading` (USD-M futures), `spot` (Spot), `ws`
|
|
172
|
+
(streams + WS API), `derived` (composites like size/close/bracket), `paper`.
|
|
173
|
+
- **MCP server**: `npx binance-sdk-mcp` (stdio) auto-registers every tool plus reference
|
|
174
|
+
resources (`binance://futures/symbols`, `binance://futures/premium-index`, `binance://spot/symbols`).
|
|
175
|
+
For local development against source instead of the published package, point your MCP host at
|
|
176
|
+
`mcp-config/local-dev.json` (runs `npx tsx src/mcp/index.ts` directly).
|
|
177
|
+
- **Agent skills**: Markdown skills under `skills/` — futures trading / market-data / algo /
|
|
178
|
+
portfolio-margin, plus spot trading / market-data — for Skills-Hub-style agents.
|
|
179
|
+
|
|
180
|
+
## Examples
|
|
181
|
+
|
|
182
|
+
```bash
|
|
183
|
+
npx tsx examples/quickstart.ts # market data, symbol rules, risk-based sizing
|
|
184
|
+
npx tsx examples/ws-streams.ts # live kline / trade / mark-price / book streams
|
|
185
|
+
npx tsx examples/paper-trading.ts # simulated position lifecycle
|
|
186
|
+
```
|
|
187
|
+
|
|
188
|
+
## Development
|
|
189
|
+
|
|
190
|
+
```bash
|
|
191
|
+
npm install
|
|
192
|
+
npm test # vitest, HTTP/WS mocked
|
|
193
|
+
npm run typecheck
|
|
194
|
+
npm run build # tsup -> dist/ (ESM + CJS + .d.ts)
|
|
195
|
+
npm run smoke # hits live public Binance endpoints, no keys needed
|
|
196
|
+
```
|
|
197
|
+
|
|
198
|
+
CI runs typecheck, build and tests on Node 18/20/22; pushing a `v*` tag publishes to npm.
|