@luisjpf/alpaca-sdk 0.2.9 → 0.3.2
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 +9 -27
- package/dist/index.cjs +19 -7
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +78 -153
- package/dist/index.d.ts +78 -153
- package/dist/index.js +19 -7
- package/dist/index.js.map +1 -1
- package/package.json +32 -13
package/README.md
CHANGED
|
@@ -20,14 +20,7 @@ Modern, type-safe TypeScript SDK for Alpaca's Trading, Broker, and Market Data A
|
|
|
20
20
|
## Installation
|
|
21
21
|
|
|
22
22
|
```bash
|
|
23
|
-
# Full SDK (recommended)
|
|
24
23
|
pnpm add @luisjpf/alpaca-sdk
|
|
25
|
-
|
|
26
|
-
# Or individual packages
|
|
27
|
-
pnpm add @luisjpf/trading
|
|
28
|
-
pnpm add @luisjpf/market-data
|
|
29
|
-
pnpm add @luisjpf/broker
|
|
30
|
-
pnpm add @luisjpf/streaming
|
|
31
24
|
```
|
|
32
25
|
|
|
33
26
|
## Quick Start
|
|
@@ -61,29 +54,17 @@ const bars = await alpaca.marketData.stocks.getSymbolBars('AAPL', {
|
|
|
61
54
|
})
|
|
62
55
|
```
|
|
63
56
|
|
|
64
|
-
## Packages
|
|
65
|
-
|
|
66
|
-
| Package | Description |
|
|
67
|
-
| ---------------------- | ----------------------------------------------- |
|
|
68
|
-
| `@luisjpf/alpaca-sdk` | Complete SDK with all APIs |
|
|
69
|
-
| `@luisjpf/trading` | Trading API (orders, positions, account) |
|
|
70
|
-
| `@luisjpf/market-data` | Market Data API (stocks, crypto, options, news) |
|
|
71
|
-
| `@luisjpf/broker` | Broker API (sub-accounts, funding, KYC) |
|
|
72
|
-
| `@luisjpf/streaming` | WebSocket clients for real-time data |
|
|
73
|
-
| `@luisjpf/core` | Shared utilities (auth, errors, types) |
|
|
74
|
-
|
|
75
57
|
## Configuration
|
|
76
58
|
|
|
77
59
|
```typescript
|
|
78
|
-
import {
|
|
60
|
+
import { createAlpacaClient } from '@luisjpf/alpaca-sdk'
|
|
79
61
|
|
|
80
|
-
const
|
|
62
|
+
const alpaca = createAlpacaClient({
|
|
81
63
|
keyId: 'YOUR_API_KEY',
|
|
82
64
|
secretKey: 'YOUR_SECRET_KEY',
|
|
83
65
|
paper: true, // default: true
|
|
84
66
|
timeout: 30_000, // default: 30s
|
|
85
67
|
maxRetries: 2, // default: 2
|
|
86
|
-
baseUrl: 'custom-url', // optional override
|
|
87
68
|
})
|
|
88
69
|
```
|
|
89
70
|
|
|
@@ -97,7 +78,7 @@ The SDK automatically retries failed requests for:
|
|
|
97
78
|
- **500+ Server Errors** - Uses exponential backoff with jitter
|
|
98
79
|
|
|
99
80
|
```typescript
|
|
100
|
-
const
|
|
81
|
+
const alpaca = createAlpacaClient({
|
|
101
82
|
keyId: 'YOUR_API_KEY',
|
|
102
83
|
secretKey: 'YOUR_SECRET_KEY',
|
|
103
84
|
maxRetries: 2, // default: 2 (set to 0 to disable)
|
|
@@ -120,7 +101,7 @@ import {
|
|
|
120
101
|
ValidationError,
|
|
121
102
|
MarketClosedError,
|
|
122
103
|
ServerError,
|
|
123
|
-
} from '@luisjpf/
|
|
104
|
+
} from '@luisjpf/alpaca-sdk'
|
|
124
105
|
|
|
125
106
|
try {
|
|
126
107
|
await client.orders.create({ ... })
|
|
@@ -182,6 +163,7 @@ For type-safe error handling without `instanceof`:
|
|
|
182
163
|
|
|
183
164
|
```typescript
|
|
184
165
|
import {
|
|
166
|
+
AlpacaError,
|
|
185
167
|
isAuthenticationError,
|
|
186
168
|
isRateLimitError,
|
|
187
169
|
isInsufficientFundsError,
|
|
@@ -189,7 +171,7 @@ import {
|
|
|
189
171
|
isNotFoundError,
|
|
190
172
|
isMarketClosedError,
|
|
191
173
|
isServerError,
|
|
192
|
-
} from '@luisjpf/
|
|
174
|
+
} from '@luisjpf/alpaca-sdk'
|
|
193
175
|
|
|
194
176
|
try {
|
|
195
177
|
await client.orders.create({ ... })
|
|
@@ -213,7 +195,7 @@ Real-time market data and trade updates via WebSocket.
|
|
|
213
195
|
### Stock Data Streaming
|
|
214
196
|
|
|
215
197
|
```typescript
|
|
216
|
-
import { createStockStream } from '@luisjpf/
|
|
198
|
+
import { createStockStream } from '@luisjpf/alpaca-sdk'
|
|
217
199
|
|
|
218
200
|
const stream = createStockStream({
|
|
219
201
|
keyId: 'YOUR_API_KEY',
|
|
@@ -247,7 +229,7 @@ stream.subscribeForBars(['AAPL'])
|
|
|
247
229
|
### Crypto Data Streaming
|
|
248
230
|
|
|
249
231
|
```typescript
|
|
250
|
-
import { createCryptoStream } from '@luisjpf/
|
|
232
|
+
import { createCryptoStream } from '@luisjpf/alpaca-sdk'
|
|
251
233
|
|
|
252
234
|
const stream = createCryptoStream({
|
|
253
235
|
keyId: 'YOUR_API_KEY',
|
|
@@ -266,7 +248,7 @@ stream.subscribeForTrades(['BTC/USD', 'ETH/USD'])
|
|
|
266
248
|
### Trade Updates Streaming
|
|
267
249
|
|
|
268
250
|
```typescript
|
|
269
|
-
import { createTradeUpdatesStream } from '@luisjpf/
|
|
251
|
+
import { createTradeUpdatesStream } from '@luisjpf/alpaca-sdk'
|
|
270
252
|
|
|
271
253
|
const stream = createTradeUpdatesStream({
|
|
272
254
|
keyId: 'YOUR_API_KEY',
|
package/dist/index.cjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _nullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } } function _optionalChain(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; } var _class; var _class2; var _class3; var _class4;//
|
|
1
|
+
"use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _nullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } } function _optionalChain(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; } var _class; var _class2; var _class3; var _class4;// src/core/errors.ts
|
|
2
2
|
var ErrorType = {
|
|
3
3
|
Authentication: "authentication",
|
|
4
4
|
Forbidden: "forbidden",
|
|
@@ -201,6 +201,8 @@ var isValidationError = (e) => e.type === ErrorType.Validation;
|
|
|
201
201
|
var isInsufficientFundsError = (e) => e.type === ErrorType.InsufficientFunds;
|
|
202
202
|
var isMarketClosedError = (e) => e.type === ErrorType.MarketClosed;
|
|
203
203
|
var isServerError = (e) => e.type === ErrorType.Server;
|
|
204
|
+
|
|
205
|
+
// src/core/auth.ts
|
|
204
206
|
function validateCredential(value, name) {
|
|
205
207
|
if (!value || typeof value !== "string" || value.trim().length === 0) {
|
|
206
208
|
throw new Error(`${name} cannot be empty`);
|
|
@@ -245,6 +247,8 @@ function createWebSocketOAuth(token) {
|
|
|
245
247
|
secret: token
|
|
246
248
|
};
|
|
247
249
|
}
|
|
250
|
+
|
|
251
|
+
// src/core/client.ts
|
|
248
252
|
var ALPACA_URLS = {
|
|
249
253
|
trading: {
|
|
250
254
|
paper: "https://paper-api.alpaca.markets",
|
|
@@ -397,6 +401,8 @@ function createApiFetch(config, authType = "apiKey") {
|
|
|
397
401
|
);
|
|
398
402
|
};
|
|
399
403
|
}
|
|
404
|
+
|
|
405
|
+
// src/core/response.ts
|
|
400
406
|
function createAlpacaErrorFromResponse(error, response) {
|
|
401
407
|
const requestId = _nullishCoalesce(response.headers.get("x-request-id"), () => ( void 0));
|
|
402
408
|
const retryAfterHeader = response.headers.get("retry-after");
|
|
@@ -434,7 +440,7 @@ function unwrapOptional(result) {
|
|
|
434
440
|
return result.data;
|
|
435
441
|
}
|
|
436
442
|
|
|
437
|
-
//
|
|
443
|
+
// src/trading/client.ts
|
|
438
444
|
var _openapifetch = require('openapi-fetch'); var _openapifetch2 = _interopRequireDefault(_openapifetch);
|
|
439
445
|
function createTradingClient(config) {
|
|
440
446
|
const resolvedConfig = resolveConfig(config, "trading");
|
|
@@ -713,7 +719,7 @@ function createTradingClient(config) {
|
|
|
713
719
|
};
|
|
714
720
|
}
|
|
715
721
|
|
|
716
|
-
//
|
|
722
|
+
// src/market-data/client.ts
|
|
717
723
|
|
|
718
724
|
function createMarketDataClient(config) {
|
|
719
725
|
const resolvedConfig = resolveConfig(config, "marketData");
|
|
@@ -1119,7 +1125,7 @@ function createMarketDataClient(config) {
|
|
|
1119
1125
|
};
|
|
1120
1126
|
}
|
|
1121
1127
|
|
|
1122
|
-
//
|
|
1128
|
+
// src/broker/client.ts
|
|
1123
1129
|
|
|
1124
1130
|
function createBrokerClient(config) {
|
|
1125
1131
|
const resolvedConfig = resolveConfig(config, "broker");
|
|
@@ -1438,11 +1444,9 @@ function createBrokerClient(config) {
|
|
|
1438
1444
|
};
|
|
1439
1445
|
}
|
|
1440
1446
|
|
|
1441
|
-
//
|
|
1447
|
+
// src/streaming/base-stream.ts
|
|
1442
1448
|
var _ws = require('ws'); var _ws2 = _interopRequireDefault(_ws);
|
|
1443
1449
|
var _msgpack = require('@msgpack/msgpack');
|
|
1444
|
-
|
|
1445
|
-
|
|
1446
1450
|
var RECONNECT_INITIAL_DELAY = 1e3;
|
|
1447
1451
|
var RECONNECT_MAX_DELAY = 3e4;
|
|
1448
1452
|
var RECONNECT_BACKOFF_MULTIPLIER = 2;
|
|
@@ -1730,6 +1734,8 @@ var BaseStream = (_class = class {
|
|
|
1730
1734
|
}
|
|
1731
1735
|
}
|
|
1732
1736
|
}, _class);
|
|
1737
|
+
|
|
1738
|
+
// src/streaming/stock-stream.ts
|
|
1733
1739
|
var STOCK_STREAM_BASE_URL = "wss://stream.data.alpaca.markets/v2";
|
|
1734
1740
|
var StockStreamImpl = (_class2 = class extends BaseStream {
|
|
1735
1741
|
|
|
@@ -1915,6 +1921,8 @@ function createStockStream(config) {
|
|
|
1915
1921
|
}
|
|
1916
1922
|
};
|
|
1917
1923
|
}
|
|
1924
|
+
|
|
1925
|
+
// src/streaming/crypto-stream.ts
|
|
1918
1926
|
var CRYPTO_STREAM_BASE_URL = "wss://stream.data.alpaca.markets/v1beta3/crypto";
|
|
1919
1927
|
var CryptoStreamImpl = (_class3 = class extends BaseStream {
|
|
1920
1928
|
|
|
@@ -2100,6 +2108,10 @@ function createCryptoStream(config) {
|
|
|
2100
2108
|
}
|
|
2101
2109
|
};
|
|
2102
2110
|
}
|
|
2111
|
+
|
|
2112
|
+
// src/streaming/trade-updates-stream.ts
|
|
2113
|
+
|
|
2114
|
+
|
|
2103
2115
|
var PAPER_TRADING_URL = "wss://paper-api.alpaca.markets/stream";
|
|
2104
2116
|
var LIVE_TRADING_URL = "wss://api.alpaca.markets/stream";
|
|
2105
2117
|
var RECONNECT_INITIAL_DELAY2 = 1e3;
|