@adaptic/utils 0.0.99
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 +916 -0
- package/dist/index.cjs +71971 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.mjs +71677 -0
- package/dist/index.mjs.map +1 -0
- package/dist/test.js +7799 -0
- package/dist/test.js.map +1 -0
- package/dist/types/__tests__/alpaca-functions.test.d.ts +2 -0
- package/dist/types/__tests__/alpaca-functions.test.d.ts.map +1 -0
- package/dist/types/__tests__/api-endpoints.test.d.ts +2 -0
- package/dist/types/__tests__/api-endpoints.test.d.ts.map +1 -0
- package/dist/types/__tests__/asset-allocation.test.d.ts +2 -0
- package/dist/types/__tests__/asset-allocation.test.d.ts.map +1 -0
- package/dist/types/__tests__/auth-validator.test.d.ts +2 -0
- package/dist/types/__tests__/auth-validator.test.d.ts.map +1 -0
- package/dist/types/__tests__/cache.test.d.ts +2 -0
- package/dist/types/__tests__/cache.test.d.ts.map +1 -0
- package/dist/types/__tests__/errors.test.d.ts +2 -0
- package/dist/types/__tests__/errors.test.d.ts.map +1 -0
- package/dist/types/__tests__/financial-regression.test.d.ts +2 -0
- package/dist/types/__tests__/financial-regression.test.d.ts.map +1 -0
- package/dist/types/__tests__/format-tools.test.d.ts +2 -0
- package/dist/types/__tests__/format-tools.test.d.ts.map +1 -0
- package/dist/types/__tests__/http-keep-alive.test.d.ts +2 -0
- package/dist/types/__tests__/http-keep-alive.test.d.ts.map +1 -0
- package/dist/types/__tests__/http-timeout.test.d.ts +2 -0
- package/dist/types/__tests__/http-timeout.test.d.ts.map +1 -0
- package/dist/types/__tests__/logger.test.d.ts +2 -0
- package/dist/types/__tests__/logger.test.d.ts.map +1 -0
- package/dist/types/__tests__/market-time.test.d.ts +2 -0
- package/dist/types/__tests__/market-time.test.d.ts.map +1 -0
- package/dist/types/__tests__/misc-utils.test.d.ts +2 -0
- package/dist/types/__tests__/misc-utils.test.d.ts.map +1 -0
- package/dist/types/__tests__/paginator.test.d.ts +2 -0
- package/dist/types/__tests__/paginator.test.d.ts.map +1 -0
- package/dist/types/__tests__/performance-metrics.test.d.ts +2 -0
- package/dist/types/__tests__/performance-metrics.test.d.ts.map +1 -0
- package/dist/types/__tests__/polygon.test.d.ts +2 -0
- package/dist/types/__tests__/polygon.test.d.ts.map +1 -0
- package/dist/types/__tests__/price-utils.test.d.ts +2 -0
- package/dist/types/__tests__/price-utils.test.d.ts.map +1 -0
- package/dist/types/__tests__/property-based-financial.test.d.ts +2 -0
- package/dist/types/__tests__/property-based-financial.test.d.ts.map +1 -0
- package/dist/types/__tests__/rate-limiter.test.d.ts +2 -0
- package/dist/types/__tests__/rate-limiter.test.d.ts.map +1 -0
- package/dist/types/__tests__/schema-validation.test.d.ts +2 -0
- package/dist/types/__tests__/schema-validation.test.d.ts.map +1 -0
- package/dist/types/__tests__/technical-analysis.test.d.ts +2 -0
- package/dist/types/__tests__/technical-analysis.test.d.ts.map +1 -0
- package/dist/types/__tests__/time-utils.test.d.ts +2 -0
- package/dist/types/__tests__/time-utils.test.d.ts.map +1 -0
- package/dist/types/adaptic.d.ts +52 -0
- package/dist/types/adaptic.d.ts.map +1 -0
- package/dist/types/alpaca/client.d.ts +95 -0
- package/dist/types/alpaca/client.d.ts.map +1 -0
- package/dist/types/alpaca/crypto/data.d.ts +281 -0
- package/dist/types/alpaca/crypto/data.d.ts.map +1 -0
- package/dist/types/alpaca/crypto/index.d.ts +75 -0
- package/dist/types/alpaca/crypto/index.d.ts.map +1 -0
- package/dist/types/alpaca/crypto/orders.d.ts +221 -0
- package/dist/types/alpaca/crypto/orders.d.ts.map +1 -0
- package/dist/types/alpaca/index.d.ts +205 -0
- package/dist/types/alpaca/index.d.ts.map +1 -0
- package/dist/types/alpaca/legacy/account.d.ts +34 -0
- package/dist/types/alpaca/legacy/account.d.ts.map +1 -0
- package/dist/types/alpaca/legacy/assets.d.ts +13 -0
- package/dist/types/alpaca/legacy/assets.d.ts.map +1 -0
- package/dist/types/alpaca/legacy/auth.d.ts +18 -0
- package/dist/types/alpaca/legacy/auth.d.ts.map +1 -0
- package/dist/types/alpaca/legacy/index.d.ts +15 -0
- package/dist/types/alpaca/legacy/index.d.ts.map +1 -0
- package/dist/types/alpaca/legacy/market-data.d.ts +32 -0
- package/dist/types/alpaca/legacy/market-data.d.ts.map +1 -0
- package/dist/types/alpaca/legacy/orders.d.ts +84 -0
- package/dist/types/alpaca/legacy/orders.d.ts.map +1 -0
- package/dist/types/alpaca/legacy/positions.d.ts +66 -0
- package/dist/types/alpaca/legacy/positions.d.ts.map +1 -0
- package/dist/types/alpaca/legacy/utils.d.ts +18 -0
- package/dist/types/alpaca/legacy/utils.d.ts.map +1 -0
- package/dist/types/alpaca/market-data/bars.d.ts +142 -0
- package/dist/types/alpaca/market-data/bars.d.ts.map +1 -0
- package/dist/types/alpaca/market-data/index.d.ts +13 -0
- package/dist/types/alpaca/market-data/index.d.ts.map +1 -0
- package/dist/types/alpaca/market-data/news.d.ts +87 -0
- package/dist/types/alpaca/market-data/news.d.ts.map +1 -0
- package/dist/types/alpaca/market-data/quotes.d.ts +85 -0
- package/dist/types/alpaca/market-data/quotes.d.ts.map +1 -0
- package/dist/types/alpaca/market-data/trades.d.ts +98 -0
- package/dist/types/alpaca/market-data/trades.d.ts.map +1 -0
- package/dist/types/alpaca/options/contracts.d.ts +279 -0
- package/dist/types/alpaca/options/contracts.d.ts.map +1 -0
- package/dist/types/alpaca/options/data.d.ts +126 -0
- package/dist/types/alpaca/options/data.d.ts.map +1 -0
- package/dist/types/alpaca/options/index.d.ts +17 -0
- package/dist/types/alpaca/options/index.d.ts.map +1 -0
- package/dist/types/alpaca/options/orders.d.ts +366 -0
- package/dist/types/alpaca/options/orders.d.ts.map +1 -0
- package/dist/types/alpaca/options/strategies.d.ts +224 -0
- package/dist/types/alpaca/options/strategies.d.ts.map +1 -0
- package/dist/types/alpaca/streams/base-stream.d.ts +143 -0
- package/dist/types/alpaca/streams/base-stream.d.ts.map +1 -0
- package/dist/types/alpaca/streams/crypto-stream.d.ts +173 -0
- package/dist/types/alpaca/streams/crypto-stream.d.ts.map +1 -0
- package/dist/types/alpaca/streams/index.d.ts +54 -0
- package/dist/types/alpaca/streams/index.d.ts.map +1 -0
- package/dist/types/alpaca/streams/option-stream.d.ts +167 -0
- package/dist/types/alpaca/streams/option-stream.d.ts.map +1 -0
- package/dist/types/alpaca/streams/stock-stream.d.ts +176 -0
- package/dist/types/alpaca/streams/stock-stream.d.ts.map +1 -0
- package/dist/types/alpaca/streams/stream-manager.d.ts +277 -0
- package/dist/types/alpaca/streams/stream-manager.d.ts.map +1 -0
- package/dist/types/alpaca/streams/trading-stream.d.ts +186 -0
- package/dist/types/alpaca/streams/trading-stream.d.ts.map +1 -0
- package/dist/types/alpaca/streams.d.ts +88 -0
- package/dist/types/alpaca/streams.d.ts.map +1 -0
- package/dist/types/alpaca/test-imports.d.ts +7 -0
- package/dist/types/alpaca/test-imports.d.ts.map +1 -0
- package/dist/types/alpaca/trading/account.d.ts +198 -0
- package/dist/types/alpaca/trading/account.d.ts.map +1 -0
- package/dist/types/alpaca/trading/bracket-orders.d.ts +162 -0
- package/dist/types/alpaca/trading/bracket-orders.d.ts.map +1 -0
- package/dist/types/alpaca/trading/clock.d.ts +99 -0
- package/dist/types/alpaca/trading/clock.d.ts.map +1 -0
- package/dist/types/alpaca/trading/index.d.ts +15 -0
- package/dist/types/alpaca/trading/index.d.ts.map +1 -0
- package/dist/types/alpaca/trading/oco-orders.d.ts +203 -0
- package/dist/types/alpaca/trading/oco-orders.d.ts.map +1 -0
- package/dist/types/alpaca/trading/order-utils.d.ts +404 -0
- package/dist/types/alpaca/trading/order-utils.d.ts.map +1 -0
- package/dist/types/alpaca/trading/orders.d.ts +199 -0
- package/dist/types/alpaca/trading/orders.d.ts.map +1 -0
- package/dist/types/alpaca/trading/oto-orders.d.ts +282 -0
- package/dist/types/alpaca/trading/oto-orders.d.ts.map +1 -0
- package/dist/types/alpaca/trading/positions.d.ts +389 -0
- package/dist/types/alpaca/trading/positions.d.ts.map +1 -0
- package/dist/types/alpaca/trading/smart-orders.d.ts +301 -0
- package/dist/types/alpaca/trading/smart-orders.d.ts.map +1 -0
- package/dist/types/alpaca/trading/trailing-stops.d.ts +240 -0
- package/dist/types/alpaca/trading/trailing-stops.d.ts.map +1 -0
- package/dist/types/alpaca-market-data-api.d.ts +386 -0
- package/dist/types/alpaca-market-data-api.d.ts.map +1 -0
- package/dist/types/alpaca-trading-api.d.ts +315 -0
- package/dist/types/alpaca-trading-api.d.ts.map +1 -0
- package/dist/types/alphavantage.d.ts +44 -0
- package/dist/types/alphavantage.d.ts.map +1 -0
- package/dist/types/asset-allocation-algorithm.d.ts +143 -0
- package/dist/types/asset-allocation-algorithm.d.ts.map +1 -0
- package/dist/types/cache/stampede-protected-cache.d.ts +565 -0
- package/dist/types/cache/stampede-protected-cache.d.ts.map +1 -0
- package/dist/types/config/api-endpoints.d.ts +94 -0
- package/dist/types/config/api-endpoints.d.ts.map +1 -0
- package/dist/types/crypto.d.ts +76 -0
- package/dist/types/crypto.d.ts.map +1 -0
- package/dist/types/display-manager.d.ts +24 -0
- package/dist/types/display-manager.d.ts.map +1 -0
- package/dist/types/errors/index.d.ts +130 -0
- package/dist/types/errors/index.d.ts.map +1 -0
- package/dist/types/examples/asset-allocation-example.d.ts +36 -0
- package/dist/types/examples/asset-allocation-example.d.ts.map +1 -0
- package/dist/types/examples/rate-limiter-example.d.ts +7 -0
- package/dist/types/examples/rate-limiter-example.d.ts.map +1 -0
- package/dist/types/format-tools.d.ts +54 -0
- package/dist/types/format-tools.d.ts.map +1 -0
- package/dist/types/http-timeout.d.ts +37 -0
- package/dist/types/http-timeout.d.ts.map +1 -0
- package/dist/types/index.d.ts +766 -0
- package/dist/types/index.d.ts.map +1 -0
- package/dist/types/logger.d.ts +56 -0
- package/dist/types/logger.d.ts.map +1 -0
- package/dist/types/logging.d.ts +12 -0
- package/dist/types/logging.d.ts.map +1 -0
- package/dist/types/market-hours.d.ts +24 -0
- package/dist/types/market-hours.d.ts.map +1 -0
- package/dist/types/market-time.d.ts +246 -0
- package/dist/types/market-time.d.ts.map +1 -0
- package/dist/types/metrics-calcs.d.ts +6 -0
- package/dist/types/metrics-calcs.d.ts.map +1 -0
- package/dist/types/misc-utils.d.ts +52 -0
- package/dist/types/misc-utils.d.ts.map +1 -0
- package/dist/types/performance-metrics.d.ts +88 -0
- package/dist/types/performance-metrics.d.ts.map +1 -0
- package/dist/types/polygon-indices.d.ts +85 -0
- package/dist/types/polygon-indices.d.ts.map +1 -0
- package/dist/types/polygon.d.ts +126 -0
- package/dist/types/polygon.d.ts.map +1 -0
- package/dist/types/price-utils.d.ts +26 -0
- package/dist/types/price-utils.d.ts.map +1 -0
- package/dist/types/rate-limiter.d.ts +171 -0
- package/dist/types/rate-limiter.d.ts.map +1 -0
- package/dist/types/schemas/alpaca-schemas.d.ts +779 -0
- package/dist/types/schemas/alpaca-schemas.d.ts.map +1 -0
- package/dist/types/schemas/alphavantage-schemas.d.ts +255 -0
- package/dist/types/schemas/alphavantage-schemas.d.ts.map +1 -0
- package/dist/types/schemas/index.d.ts +21 -0
- package/dist/types/schemas/index.d.ts.map +1 -0
- package/dist/types/schemas/polygon-schemas.d.ts +551 -0
- package/dist/types/schemas/polygon-schemas.d.ts.map +1 -0
- package/dist/types/schemas/validate-response.d.ts +88 -0
- package/dist/types/schemas/validate-response.d.ts.map +1 -0
- package/dist/types/technical-analysis.d.ts +90 -0
- package/dist/types/technical-analysis.d.ts.map +1 -0
- package/dist/types/test.d.ts +2 -0
- package/dist/types/test.d.ts.map +1 -0
- package/dist/types/testing/options-ws.d.ts +2 -0
- package/dist/types/testing/options-ws.d.ts.map +1 -0
- package/dist/types/time-utils.d.ts +17 -0
- package/dist/types/time-utils.d.ts.map +1 -0
- package/dist/types/types/adaptic-types.d.ts +11 -0
- package/dist/types/types/adaptic-types.d.ts.map +1 -0
- package/dist/types/types/alpaca-types.d.ts +1165 -0
- package/dist/types/types/alpaca-types.d.ts.map +1 -0
- package/dist/types/types/alphavantage-types.d.ts +66 -0
- package/dist/types/types/alphavantage-types.d.ts.map +1 -0
- package/dist/types/types/asset-allocation-types.d.ts +328 -0
- package/dist/types/types/asset-allocation-types.d.ts.map +1 -0
- package/dist/types/types/index.d.ts +21 -0
- package/dist/types/types/index.d.ts.map +1 -0
- package/dist/types/types/logging-types.d.ts +10 -0
- package/dist/types/types/logging-types.d.ts.map +1 -0
- package/dist/types/types/market-time-types.d.ts +59 -0
- package/dist/types/types/market-time-types.d.ts.map +1 -0
- package/dist/types/types/metrics-types.d.ts +33 -0
- package/dist/types/types/metrics-types.d.ts.map +1 -0
- package/dist/types/types/polygon-indices-types.d.ts +190 -0
- package/dist/types/types/polygon-indices-types.d.ts.map +1 -0
- package/dist/types/types/polygon-types.d.ts +204 -0
- package/dist/types/types/polygon-types.d.ts.map +1 -0
- package/dist/types/types/ta-types.d.ts +89 -0
- package/dist/types/types/ta-types.d.ts.map +1 -0
- package/dist/types/utils/auth-validator.d.ts +32 -0
- package/dist/types/utils/auth-validator.d.ts.map +1 -0
- package/dist/types/utils/http-keep-alive.d.ts +110 -0
- package/dist/types/utils/http-keep-alive.d.ts.map +1 -0
- package/dist/types/utils/paginator.d.ts +154 -0
- package/dist/types/utils/paginator.d.ts.map +1 -0
- package/dist/types/utils/retry.d.ts +78 -0
- package/dist/types/utils/retry.d.ts.map +1 -0
- package/package.json +78 -0
|
@@ -0,0 +1,565 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Cache entry with metadata for stale-while-revalidate and probabilistic expiration
|
|
3
|
+
*
|
|
4
|
+
* @description Represents a single cached item with comprehensive metadata for cache stampede
|
|
5
|
+
* prevention, including access tracking, expiration management, and refresh state monitoring.
|
|
6
|
+
*
|
|
7
|
+
* @rationale In high-frequency trading systems, cache stampedes can cause request bursts that
|
|
8
|
+
* overwhelm market data APIs (Alpaca, Polygon) during periods of synchronized expiration,
|
|
9
|
+
* leading to rate limiting and missed trading opportunities.
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* ```typescript
|
|
13
|
+
* const entry: CacheEntry<MarketData> = {
|
|
14
|
+
* value: { price: 150.25, volume: 1000000 },
|
|
15
|
+
* createdAt: Date.now(),
|
|
16
|
+
* ttl: 60000,
|
|
17
|
+
* expiresAt: Date.now() + 60000,
|
|
18
|
+
* accessCount: 0,
|
|
19
|
+
* lastAccessedAt: Date.now(),
|
|
20
|
+
* isRefreshing: false
|
|
21
|
+
* };
|
|
22
|
+
* ```
|
|
23
|
+
*/
|
|
24
|
+
export interface CacheEntry<T> {
|
|
25
|
+
/**
|
|
26
|
+
* The cached value of type T
|
|
27
|
+
* @description Stores the actual data retrieved from the source (e.g., market prices, positions)
|
|
28
|
+
*/
|
|
29
|
+
value: T;
|
|
30
|
+
/**
|
|
31
|
+
* Timestamp when the entry was created (milliseconds since epoch)
|
|
32
|
+
* @description Used to calculate entry age and determine if stale-while-revalidate is applicable
|
|
33
|
+
*/
|
|
34
|
+
createdAt: number;
|
|
35
|
+
/**
|
|
36
|
+
* Time-to-live in milliseconds
|
|
37
|
+
* @description Duration for which the entry is considered fresh. Range: 1000ms - 3600000ms (1s - 1hr)
|
|
38
|
+
*/
|
|
39
|
+
ttl: number;
|
|
40
|
+
/**
|
|
41
|
+
* Timestamp when entry expires (createdAt + ttl)
|
|
42
|
+
* @description Pre-calculated expiration timestamp for performance optimization
|
|
43
|
+
*/
|
|
44
|
+
expiresAt: number;
|
|
45
|
+
/**
|
|
46
|
+
* Number of times this entry has been accessed
|
|
47
|
+
* @description Tracks usage frequency for LRU eviction and performance analysis
|
|
48
|
+
*/
|
|
49
|
+
accessCount: number;
|
|
50
|
+
/**
|
|
51
|
+
* Last access timestamp (milliseconds since epoch)
|
|
52
|
+
* @description Used for cache statistics and to determine recently used entries
|
|
53
|
+
*/
|
|
54
|
+
lastAccessedAt: number;
|
|
55
|
+
/**
|
|
56
|
+
* Whether this entry is currently being refreshed
|
|
57
|
+
* @description Prevents duplicate refresh requests during stale-while-revalidate scenarios
|
|
58
|
+
*/
|
|
59
|
+
isRefreshing: boolean;
|
|
60
|
+
/**
|
|
61
|
+
* Error that occurred during last refresh attempt
|
|
62
|
+
* @description Stores error context for debugging failed market data fetches or API errors
|
|
63
|
+
*/
|
|
64
|
+
lastError?: Error;
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* Options for configuring the StampedeProtectedCache
|
|
68
|
+
*
|
|
69
|
+
* @description Configuration interface for initializing a stampede-protected cache with custom
|
|
70
|
+
* TTL, size limits, jitter settings, and background refresh behavior.
|
|
71
|
+
*
|
|
72
|
+
* @rationale Trading systems require fine-tuned cache parameters to balance data freshness
|
|
73
|
+
* with API rate limits. Different data types (positions vs quotes) have different staleness
|
|
74
|
+
* tolerances and update frequencies.
|
|
75
|
+
*
|
|
76
|
+
* @example
|
|
77
|
+
* ```typescript
|
|
78
|
+
* const positionCacheOptions: StampedeProtectedCacheOptions = {
|
|
79
|
+
* maxSize: 1000,
|
|
80
|
+
* defaultTtl: 30000, // 30s for position data
|
|
81
|
+
* staleWhileRevalidateTtl: 60000, // 60s grace period
|
|
82
|
+
* minJitter: 0.9,
|
|
83
|
+
* maxJitter: 1.1,
|
|
84
|
+
* enableBackgroundRefresh: true,
|
|
85
|
+
* logger: pinoLogger
|
|
86
|
+
* };
|
|
87
|
+
* ```
|
|
88
|
+
*/
|
|
89
|
+
export interface StampedeProtectedCacheOptions {
|
|
90
|
+
/**
|
|
91
|
+
* Maximum number of entries in the cache
|
|
92
|
+
* @description LRU eviction limit. Range: 10 - 100000. Recommended: 1000 for position data, 10000 for market data
|
|
93
|
+
*/
|
|
94
|
+
maxSize: number;
|
|
95
|
+
/**
|
|
96
|
+
* Default TTL in milliseconds
|
|
97
|
+
* @description How long entries remain fresh. Range: 1000ms - 3600000ms. Recommended: 30000ms (30s) for positions, 5000ms (5s) for quotes
|
|
98
|
+
*/
|
|
99
|
+
defaultTtl: number;
|
|
100
|
+
/**
|
|
101
|
+
* Grace period for stale-while-revalidate in milliseconds
|
|
102
|
+
* @description Extended TTL for serving stale data during refresh. Default: 2x defaultTtl. Range: defaultTtl - 600000ms
|
|
103
|
+
*/
|
|
104
|
+
staleWhileRevalidateTtl?: number;
|
|
105
|
+
/**
|
|
106
|
+
* Minimum jitter percentage (e.g., 0.9 for 90%)
|
|
107
|
+
* @description Lower bound of random TTL variance to prevent synchronized expiration. Range: 0.5 - 1.0. Default: 0.9
|
|
108
|
+
*/
|
|
109
|
+
minJitter?: number;
|
|
110
|
+
/**
|
|
111
|
+
* Maximum jitter percentage (e.g., 1.1 for 110%)
|
|
112
|
+
* @description Upper bound of random TTL variance. Range: 1.0 - 1.5. Default: 1.1
|
|
113
|
+
*/
|
|
114
|
+
maxJitter?: number;
|
|
115
|
+
/**
|
|
116
|
+
* Whether to enable background refresh
|
|
117
|
+
* @description If true, stale data is served while refreshing asynchronously. Default: true. Recommended for trading systems
|
|
118
|
+
*/
|
|
119
|
+
enableBackgroundRefresh?: boolean;
|
|
120
|
+
/**
|
|
121
|
+
* Logger implementation for cache operations
|
|
122
|
+
* @description Structured logger for debugging cache hits, misses, refreshes, and errors
|
|
123
|
+
*/
|
|
124
|
+
logger?: {
|
|
125
|
+
debug: (message: string, meta?: Record<string, unknown>) => void;
|
|
126
|
+
info: (message: string, meta?: Record<string, unknown>) => void;
|
|
127
|
+
warn: (message: string, meta?: Record<string, unknown>) => void;
|
|
128
|
+
error: (message: string, meta?: Record<string, unknown>) => void;
|
|
129
|
+
};
|
|
130
|
+
}
|
|
131
|
+
/**
|
|
132
|
+
* Statistics for monitoring cache performance
|
|
133
|
+
*
|
|
134
|
+
* @description Comprehensive metrics for cache performance analysis, hit rate monitoring,
|
|
135
|
+
* and identifying potential issues like stampedes or excessive misses.
|
|
136
|
+
*
|
|
137
|
+
* @rationale Trading systems require real-time performance monitoring to detect cache
|
|
138
|
+
* degradation that could impact order execution latency or market data freshness.
|
|
139
|
+
*
|
|
140
|
+
* @example
|
|
141
|
+
* ```typescript
|
|
142
|
+
* const stats = cache.getStats();
|
|
143
|
+
* if (stats.hitRatio < 0.8) {
|
|
144
|
+
* logger.warn('Low cache hit ratio', { hitRatio: stats.hitRatio });
|
|
145
|
+
* }
|
|
146
|
+
* if (stats.refreshErrors > 0) {
|
|
147
|
+
* logger.error('Cache refresh errors detected', { errors: stats.refreshErrors });
|
|
148
|
+
* }
|
|
149
|
+
* ```
|
|
150
|
+
*/
|
|
151
|
+
export interface CacheStats {
|
|
152
|
+
/**
|
|
153
|
+
* Total number of get operations
|
|
154
|
+
* @description Cumulative count of all cache.get() calls since initialization or last reset
|
|
155
|
+
*/
|
|
156
|
+
totalGets: number;
|
|
157
|
+
/**
|
|
158
|
+
* Number of cache hits
|
|
159
|
+
* @description Count of fresh cache entries served without fetching from source
|
|
160
|
+
*/
|
|
161
|
+
hits: number;
|
|
162
|
+
/**
|
|
163
|
+
* Number of cache misses
|
|
164
|
+
* @description Count of requests requiring data fetch from source (API call)
|
|
165
|
+
*/
|
|
166
|
+
misses: number;
|
|
167
|
+
/**
|
|
168
|
+
* Number of stale hits (served while revalidating)
|
|
169
|
+
* @description Count of expired entries served during background refresh
|
|
170
|
+
*/
|
|
171
|
+
staleHits: number;
|
|
172
|
+
/**
|
|
173
|
+
* Hit ratio (hits / totalGets)
|
|
174
|
+
* @description Cache effectiveness metric. Range: 0.0 - 1.0. Target: >0.8 for optimal performance
|
|
175
|
+
*/
|
|
176
|
+
hitRatio: number;
|
|
177
|
+
/**
|
|
178
|
+
* Current cache size
|
|
179
|
+
* @description Number of entries currently stored in cache
|
|
180
|
+
*/
|
|
181
|
+
size: number;
|
|
182
|
+
/**
|
|
183
|
+
* Maximum cache size
|
|
184
|
+
* @description LRU eviction threshold from configuration
|
|
185
|
+
*/
|
|
186
|
+
maxSize: number;
|
|
187
|
+
/**
|
|
188
|
+
* Number of entries currently being refreshed
|
|
189
|
+
* @description Active background refresh operations (useful for detecting stuck refreshes)
|
|
190
|
+
*/
|
|
191
|
+
activeRefreshes: number;
|
|
192
|
+
/**
|
|
193
|
+
* Number of coalesced requests
|
|
194
|
+
* @description Count of duplicate simultaneous requests merged into single fetch (stampede prevention metric)
|
|
195
|
+
*/
|
|
196
|
+
coalescedRequests: number;
|
|
197
|
+
/**
|
|
198
|
+
* Number of background refreshes performed
|
|
199
|
+
* @description Count of successful stale-while-revalidate operations
|
|
200
|
+
*/
|
|
201
|
+
backgroundRefreshes: number;
|
|
202
|
+
/**
|
|
203
|
+
* Number of errors during refresh
|
|
204
|
+
* @description Count of failed data fetch attempts (API errors, timeouts, rate limits)
|
|
205
|
+
*/
|
|
206
|
+
refreshErrors: number;
|
|
207
|
+
}
|
|
208
|
+
/**
|
|
209
|
+
* Cache loader function type
|
|
210
|
+
*
|
|
211
|
+
* @description Function signature for loading data from source when cache miss occurs.
|
|
212
|
+
* Should implement data fetching logic (API calls, database queries, etc.).
|
|
213
|
+
*
|
|
214
|
+
* @param key - Unique identifier for the data to load
|
|
215
|
+
* @returns Promise resolving to the loaded data
|
|
216
|
+
*
|
|
217
|
+
* @throws Error if data fetch fails (network error, API error, timeout, rate limit)
|
|
218
|
+
*
|
|
219
|
+
* @example
|
|
220
|
+
* ```typescript
|
|
221
|
+
* const positionLoader: CacheLoader<AlpacaPosition[]> = async (accountId: string) => {
|
|
222
|
+
* const response = await alpacaApi.getPositions(accountId);
|
|
223
|
+
* return response.positions;
|
|
224
|
+
* };
|
|
225
|
+
* ```
|
|
226
|
+
*/
|
|
227
|
+
export type CacheLoader<T> = (key: string) => Promise<T>;
|
|
228
|
+
/**
|
|
229
|
+
* StampedeProtectedCache provides three-layer protection against cache stampedes
|
|
230
|
+
*
|
|
231
|
+
* @description High-performance caching system implementing multiple stampede prevention
|
|
232
|
+
* strategies to protect downstream services (market data APIs, position services) from
|
|
233
|
+
* request bursts during synchronized cache expiration events.
|
|
234
|
+
*
|
|
235
|
+
* @rationale In algorithmic trading, cache stampedes can:
|
|
236
|
+
* - Overwhelm market data APIs (Alpaca, Polygon) causing rate limiting (200 req/min limits)
|
|
237
|
+
* - Introduce latency spikes during critical trading windows (market open/close)
|
|
238
|
+
* - Trigger cascading failures when position data becomes unavailable
|
|
239
|
+
* - Cause missed trading opportunities due to stale or unavailable data
|
|
240
|
+
*
|
|
241
|
+
* Three-layer protection:
|
|
242
|
+
* 1. Request coalescing - Multiple concurrent requests for the same key share a single promise
|
|
243
|
+
* 2. Stale-while-revalidate - Serve stale data while refreshing in background
|
|
244
|
+
* 3. Probabilistic early expiration - Add jitter to prevent synchronized expiration
|
|
245
|
+
*
|
|
246
|
+
* @template T - Type of cached data (e.g., AlpacaPosition[], MarketQuote, AccountInfo)
|
|
247
|
+
*
|
|
248
|
+
* @example
|
|
249
|
+
* ```typescript
|
|
250
|
+
* // Initialize cache for position data
|
|
251
|
+
* const positionCache = new StampedeProtectedCache<AlpacaPosition[]>({
|
|
252
|
+
* maxSize: 1000,
|
|
253
|
+
* defaultTtl: 30000, // 30 seconds
|
|
254
|
+
* staleWhileRevalidateTtl: 60000, // 60 seconds grace
|
|
255
|
+
* minJitter: 0.9,
|
|
256
|
+
* maxJitter: 1.1,
|
|
257
|
+
* enableBackgroundRefresh: true,
|
|
258
|
+
* logger: pinoLogger
|
|
259
|
+
* });
|
|
260
|
+
*
|
|
261
|
+
* // Fetch with automatic caching and stampede protection
|
|
262
|
+
* const positions = await positionCache.get(
|
|
263
|
+
* accountId,
|
|
264
|
+
* async (key) => await alpacaApi.getPositions(key)
|
|
265
|
+
* );
|
|
266
|
+
* ```
|
|
267
|
+
*
|
|
268
|
+
* @businessLogic
|
|
269
|
+
* 1. On cache.get(), check for existing entry
|
|
270
|
+
* 2. If found and fresh (< TTL with jitter): return cached value (HIT)
|
|
271
|
+
* 3. If found but stale (< staleWhileRevalidateTtl): return stale value, trigger background refresh (STALE HIT)
|
|
272
|
+
* 4. If not found or expired beyond grace period: fetch from source (MISS)
|
|
273
|
+
* 5. During fetch, coalesce duplicate concurrent requests to single API call
|
|
274
|
+
* 6. After successful fetch, cache result with jittered TTL to prevent synchronized expiration
|
|
275
|
+
*
|
|
276
|
+
* @auditTrail
|
|
277
|
+
* - All cache operations logged with timestamps and metadata
|
|
278
|
+
* - Statistics tracked: hits, misses, stale hits, coalesced requests, refresh errors
|
|
279
|
+
* - Performance metrics exposed via getStats() for monitoring dashboards
|
|
280
|
+
*/
|
|
281
|
+
export declare class StampedeProtectedCache<T> {
|
|
282
|
+
private readonly cache;
|
|
283
|
+
private readonly options;
|
|
284
|
+
private readonly pendingRefreshes;
|
|
285
|
+
private readonly stats;
|
|
286
|
+
constructor(options: StampedeProtectedCacheOptions);
|
|
287
|
+
/**
|
|
288
|
+
* Get a value from the cache, loading it if necessary
|
|
289
|
+
*
|
|
290
|
+
* @description Primary cache access method implementing three-layer stampede protection.
|
|
291
|
+
* Returns cached data if fresh, serves stale data while refreshing if within grace period,
|
|
292
|
+
* or fetches fresh data with request coalescing if expired.
|
|
293
|
+
*
|
|
294
|
+
* @param key - Unique cache key (e.g., accountId, symbol, "positions:ACCT123")
|
|
295
|
+
* @param loader - Async function to load data on cache miss
|
|
296
|
+
* @param ttl - Optional TTL override in milliseconds. If not provided, uses defaultTtl from config
|
|
297
|
+
*
|
|
298
|
+
* @returns Promise resolving to cached or freshly loaded data
|
|
299
|
+
*
|
|
300
|
+
* @throws Error if loader function fails and no stale data is available
|
|
301
|
+
*
|
|
302
|
+
* @example
|
|
303
|
+
* ```typescript
|
|
304
|
+
* // Get positions with default TTL
|
|
305
|
+
* const positions = await cache.get(
|
|
306
|
+
* accountId,
|
|
307
|
+
* async (key) => await alpacaApi.getPositions(key)
|
|
308
|
+
* );
|
|
309
|
+
*
|
|
310
|
+
* // Get market quote with custom TTL (5 seconds for real-time data)
|
|
311
|
+
* const quote = await cache.get(
|
|
312
|
+
* `quote:${symbol}`,
|
|
313
|
+
* async (key) => await polygonApi.getQuote(symbol),
|
|
314
|
+
* 5000
|
|
315
|
+
* );
|
|
316
|
+
* ```
|
|
317
|
+
*
|
|
318
|
+
* @businessLogic
|
|
319
|
+
* 1. Increment totalGets counter for statistics
|
|
320
|
+
* 2. Calculate effective TTL (custom or default)
|
|
321
|
+
* 3. Attempt cache lookup by key
|
|
322
|
+
* 4. If entry exists:
|
|
323
|
+
* a. Increment access count and update lastAccessedAt
|
|
324
|
+
* b. Apply probabilistic jitter to expiration time
|
|
325
|
+
* c. If still fresh (now < jitteredExpiresAt): return cached value (HIT)
|
|
326
|
+
* d. If stale but within grace period (now < staleExpiresAt) and not already refreshing:
|
|
327
|
+
* - Serve stale value immediately
|
|
328
|
+
* - Trigger background refresh if enabled
|
|
329
|
+
* - Return stale value (STALE HIT)
|
|
330
|
+
* 5. If entry not found or expired beyond grace: load fresh data with coalescing (MISS)
|
|
331
|
+
*/
|
|
332
|
+
get(key: string, loader: CacheLoader<T>, ttl?: number): Promise<T>;
|
|
333
|
+
/**
|
|
334
|
+
* Set a value in the cache
|
|
335
|
+
*
|
|
336
|
+
* @description Manually store a value in the cache with optional custom TTL.
|
|
337
|
+
* Useful for pre-warming cache or storing computed results.
|
|
338
|
+
*
|
|
339
|
+
* @param key - Unique cache key
|
|
340
|
+
* @param value - Data to cache
|
|
341
|
+
* @param ttl - Optional TTL in milliseconds. If not provided, uses defaultTtl
|
|
342
|
+
*
|
|
343
|
+
* @returns void
|
|
344
|
+
*
|
|
345
|
+
* @example
|
|
346
|
+
* ```typescript
|
|
347
|
+
* // Pre-warm cache with known data
|
|
348
|
+
* cache.set('positions:ACCT123', positions, 30000);
|
|
349
|
+
*
|
|
350
|
+
* // Cache computed result
|
|
351
|
+
* const aggregatedData = computeAggregation(positions);
|
|
352
|
+
* cache.set('aggregated:ACCT123', aggregatedData, 60000);
|
|
353
|
+
* ```
|
|
354
|
+
*/
|
|
355
|
+
set(key: string, value: T, ttl?: number): void;
|
|
356
|
+
/**
|
|
357
|
+
* Check if a key exists in the cache (regardless of expiration)
|
|
358
|
+
*
|
|
359
|
+
* @description Checks for cache entry existence without considering TTL or freshness.
|
|
360
|
+
* Does not update access statistics or timestamps.
|
|
361
|
+
*
|
|
362
|
+
* @param key - Cache key to check
|
|
363
|
+
*
|
|
364
|
+
* @returns true if entry exists (fresh or stale), false otherwise
|
|
365
|
+
*
|
|
366
|
+
* @example
|
|
367
|
+
* ```typescript
|
|
368
|
+
* if (cache.has(accountId)) {
|
|
369
|
+
* // Entry exists, may be fresh or stale
|
|
370
|
+
* }
|
|
371
|
+
* ```
|
|
372
|
+
*/
|
|
373
|
+
has(key: string): boolean;
|
|
374
|
+
/**
|
|
375
|
+
* Delete a specific key from the cache
|
|
376
|
+
*
|
|
377
|
+
* @description Immediately removes cache entry and any pending refreshes for the key.
|
|
378
|
+
* Useful for cache invalidation when source data changes.
|
|
379
|
+
*
|
|
380
|
+
* @param key - Cache key to delete
|
|
381
|
+
*
|
|
382
|
+
* @returns true if entry was deleted, false if key did not exist
|
|
383
|
+
*
|
|
384
|
+
* @example
|
|
385
|
+
* ```typescript
|
|
386
|
+
* // Invalidate after position update
|
|
387
|
+
* await alpacaApi.submitOrder(order);
|
|
388
|
+
* cache.delete(`positions:${accountId}`);
|
|
389
|
+
* ```
|
|
390
|
+
*/
|
|
391
|
+
delete(key: string): boolean;
|
|
392
|
+
/**
|
|
393
|
+
* Invalidate a key (alias for delete)
|
|
394
|
+
*
|
|
395
|
+
* @description Semantic alias for delete() method. Use for clarity when invalidating
|
|
396
|
+
* cache after data mutations.
|
|
397
|
+
*
|
|
398
|
+
* @param key - Cache key to invalidate
|
|
399
|
+
*
|
|
400
|
+
* @returns true if entry was invalidated, false if key did not exist
|
|
401
|
+
*
|
|
402
|
+
* @example
|
|
403
|
+
* ```typescript
|
|
404
|
+
* // Invalidate after trade execution
|
|
405
|
+
* cache.invalidate(`positions:${accountId}`);
|
|
406
|
+
* ```
|
|
407
|
+
*/
|
|
408
|
+
invalidate(key: string): boolean;
|
|
409
|
+
/**
|
|
410
|
+
* Clear all entries from the cache
|
|
411
|
+
*
|
|
412
|
+
* @description Removes all cached entries and pending refreshes. Use during system
|
|
413
|
+
* resets or configuration changes requiring fresh data.
|
|
414
|
+
*
|
|
415
|
+
* @returns void
|
|
416
|
+
*
|
|
417
|
+
* @example
|
|
418
|
+
* ```typescript
|
|
419
|
+
* // Clear cache during market hours transition
|
|
420
|
+
* if (marketJustOpened) {
|
|
421
|
+
* cache.clear();
|
|
422
|
+
* }
|
|
423
|
+
* ```
|
|
424
|
+
*/
|
|
425
|
+
clear(): void;
|
|
426
|
+
/**
|
|
427
|
+
* Get cache statistics
|
|
428
|
+
*
|
|
429
|
+
* @description Returns comprehensive performance metrics for monitoring and analysis.
|
|
430
|
+
* Statistics include hit/miss ratios, active refreshes, coalesced requests, and errors.
|
|
431
|
+
*
|
|
432
|
+
* @returns CacheStats object with current performance metrics
|
|
433
|
+
*
|
|
434
|
+
* @example
|
|
435
|
+
* ```typescript
|
|
436
|
+
* const stats = cache.getStats();
|
|
437
|
+
* logger.info('Cache performance', {
|
|
438
|
+
* hitRatio: stats.hitRatio,
|
|
439
|
+
* size: stats.size,
|
|
440
|
+
* activeRefreshes: stats.activeRefreshes
|
|
441
|
+
* });
|
|
442
|
+
*
|
|
443
|
+
* // Alert on poor performance
|
|
444
|
+
* if (stats.hitRatio < 0.7) {
|
|
445
|
+
* alerting.send('Low cache hit ratio', stats);
|
|
446
|
+
* }
|
|
447
|
+
* ```
|
|
448
|
+
*/
|
|
449
|
+
getStats(): CacheStats;
|
|
450
|
+
/**
|
|
451
|
+
* Get all cached keys
|
|
452
|
+
*
|
|
453
|
+
* @description Returns array of all cache keys currently stored, regardless of freshness.
|
|
454
|
+
* Useful for debugging and cache inspection.
|
|
455
|
+
*
|
|
456
|
+
* @returns Array of cache keys
|
|
457
|
+
*
|
|
458
|
+
* @example
|
|
459
|
+
* ```typescript
|
|
460
|
+
* const keys = cache.keys();
|
|
461
|
+
* console.log('Cached accounts:', keys);
|
|
462
|
+
* // ['positions:ACCT123', 'positions:ACCT456', 'quote:AAPL']
|
|
463
|
+
* ```
|
|
464
|
+
*/
|
|
465
|
+
keys(): string[];
|
|
466
|
+
/**
|
|
467
|
+
* Get the size of the cache
|
|
468
|
+
*
|
|
469
|
+
* @description Returns current number of entries in cache. Compare to maxSize to
|
|
470
|
+
* monitor capacity utilization.
|
|
471
|
+
*
|
|
472
|
+
* @returns Number of cached entries
|
|
473
|
+
*
|
|
474
|
+
* @example
|
|
475
|
+
* ```typescript
|
|
476
|
+
* const utilizationPct = (cache.size / cache.getStats().maxSize) * 100;
|
|
477
|
+
* if (utilizationPct > 90) {
|
|
478
|
+
* logger.warn('Cache near capacity', { size: cache.size });
|
|
479
|
+
* }
|
|
480
|
+
* ```
|
|
481
|
+
*/
|
|
482
|
+
get size(): number;
|
|
483
|
+
/**
|
|
484
|
+
* Load data with request coalescing to prevent duplicate requests
|
|
485
|
+
*/
|
|
486
|
+
private loadWithCoalescing;
|
|
487
|
+
/**
|
|
488
|
+
* Load data and cache it
|
|
489
|
+
*/
|
|
490
|
+
private loadAndCache;
|
|
491
|
+
/**
|
|
492
|
+
* Refresh data in the background
|
|
493
|
+
*/
|
|
494
|
+
private refreshInBackground;
|
|
495
|
+
/**
|
|
496
|
+
* Apply probabilistic jitter to expiration time
|
|
497
|
+
*/
|
|
498
|
+
private applyJitter;
|
|
499
|
+
/**
|
|
500
|
+
* Reset statistics (useful for testing)
|
|
501
|
+
*
|
|
502
|
+
* @description Clears all performance counters to zero. Use for testing or when starting
|
|
503
|
+
* fresh metrics collection period.
|
|
504
|
+
*
|
|
505
|
+
* @returns void
|
|
506
|
+
*
|
|
507
|
+
* @example
|
|
508
|
+
* ```typescript
|
|
509
|
+
* // Reset stats at start of trading day
|
|
510
|
+
* cache.resetStats();
|
|
511
|
+
* ```
|
|
512
|
+
*/
|
|
513
|
+
resetStats(): void;
|
|
514
|
+
}
|
|
515
|
+
/**
|
|
516
|
+
* Factory function to create a new StampedeProtectedCache instance
|
|
517
|
+
*
|
|
518
|
+
* @description Convenience factory for creating cache instances with type inference.
|
|
519
|
+
* Alternative to using 'new StampedeProtectedCache<T>()'.
|
|
520
|
+
*
|
|
521
|
+
* @template T - Type of cached data
|
|
522
|
+
* @param options - Cache configuration options
|
|
523
|
+
*
|
|
524
|
+
* @returns New StampedeProtectedCache instance
|
|
525
|
+
*
|
|
526
|
+
* @example
|
|
527
|
+
* ```typescript
|
|
528
|
+
* // Type is automatically inferred
|
|
529
|
+
* const cache = createStampedeProtectedCache<AlpacaPosition[]>({
|
|
530
|
+
* maxSize: 1000,
|
|
531
|
+
* defaultTtl: 30000
|
|
532
|
+
* });
|
|
533
|
+
* ```
|
|
534
|
+
*/
|
|
535
|
+
export declare function createStampedeProtectedCache<T>(options: StampedeProtectedCacheOptions): StampedeProtectedCache<T>;
|
|
536
|
+
/**
|
|
537
|
+
* Default cache options for common use cases
|
|
538
|
+
*
|
|
539
|
+
* @description Production-tested default configuration suitable for most trading applications.
|
|
540
|
+
* Provides balanced performance for position and market data caching.
|
|
541
|
+
*
|
|
542
|
+
* @rationale These defaults are optimized for:
|
|
543
|
+
* - Position data refresh frequency (30-60s acceptable staleness)
|
|
544
|
+
* - API rate limit protection (Alpaca: 200 req/min)
|
|
545
|
+
* - Memory efficiency (1000 entries ≈ 10MB for typical position data)
|
|
546
|
+
* - Stampede prevention (±10% jitter prevents synchronized expiration)
|
|
547
|
+
*
|
|
548
|
+
* @example
|
|
549
|
+
* ```typescript
|
|
550
|
+
* // Use defaults for quick setup
|
|
551
|
+
* const cache = new StampedeProtectedCache({
|
|
552
|
+
* ...DEFAULT_CACHE_OPTIONS,
|
|
553
|
+
* logger: customLogger
|
|
554
|
+
* });
|
|
555
|
+
*
|
|
556
|
+
* // Override specific settings
|
|
557
|
+
* const realtimeCache = new StampedeProtectedCache({
|
|
558
|
+
* ...DEFAULT_CACHE_OPTIONS,
|
|
559
|
+
* defaultTtl: 5000, // 5s for real-time quotes
|
|
560
|
+
* maxSize: 10000
|
|
561
|
+
* });
|
|
562
|
+
* ```
|
|
563
|
+
*/
|
|
564
|
+
export declare const DEFAULT_CACHE_OPTIONS: StampedeProtectedCacheOptions;
|
|
565
|
+
//# sourceMappingURL=stampede-protected-cache.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"stampede-protected-cache.d.ts","sourceRoot":"","sources":["../../../src/cache/stampede-protected-cache.ts"],"names":[],"mappings":"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"}
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Centralized API Endpoints Configuration
|
|
3
|
+
*
|
|
4
|
+
* This file defines all Alpaca API base URLs to ensure consistency
|
|
5
|
+
* across the codebase and make updates easier.
|
|
6
|
+
*
|
|
7
|
+
* API Version Guidelines:
|
|
8
|
+
* - Trading API: v2 (stable, production-ready)
|
|
9
|
+
* - Market Data (stocks): v2 (stable)
|
|
10
|
+
* - Market Data (crypto): v1beta3 (latest beta)
|
|
11
|
+
* - Market Data (options): v1beta1 (latest beta)
|
|
12
|
+
* - News API: v1beta1 (latest beta)
|
|
13
|
+
*/
|
|
14
|
+
/**
|
|
15
|
+
* Account type for trading
|
|
16
|
+
*/
|
|
17
|
+
export type AccountType = "PAPER" | "LIVE";
|
|
18
|
+
/**
|
|
19
|
+
* Trading API base URLs (v2)
|
|
20
|
+
* Used for orders, positions, account management
|
|
21
|
+
*/
|
|
22
|
+
export declare const TRADING_API: {
|
|
23
|
+
readonly PAPER: "https://paper-api.alpaca.markets/v2";
|
|
24
|
+
readonly LIVE: "https://api.alpaca.markets/v2";
|
|
25
|
+
};
|
|
26
|
+
/**
|
|
27
|
+
* Get trading API base URL for account type
|
|
28
|
+
*/
|
|
29
|
+
export declare function getTradingApiUrl(accountType: AccountType): string;
|
|
30
|
+
/**
|
|
31
|
+
* Market Data API base URLs
|
|
32
|
+
*/
|
|
33
|
+
export declare const MARKET_DATA_API: {
|
|
34
|
+
/** Stock market data (v2) - bars, quotes, trades */
|
|
35
|
+
readonly STOCKS: "https://data.alpaca.markets/v2";
|
|
36
|
+
/** Cryptocurrency market data (v1beta3) - latest stable beta */
|
|
37
|
+
readonly CRYPTO: "https://data.alpaca.markets/v1beta3";
|
|
38
|
+
/** Options market data (v1beta1) */
|
|
39
|
+
readonly OPTIONS: "https://data.alpaca.markets/v1beta1";
|
|
40
|
+
/** News API (v1beta1) */
|
|
41
|
+
readonly NEWS: "https://data.alpaca.markets/v1beta1";
|
|
42
|
+
};
|
|
43
|
+
/**
|
|
44
|
+
* WebSocket stream URLs
|
|
45
|
+
*/
|
|
46
|
+
export declare const WEBSOCKET_STREAMS: {
|
|
47
|
+
/** Trading updates (orders, fills, etc.) */
|
|
48
|
+
readonly TRADING: {
|
|
49
|
+
readonly PAPER: "wss://paper-api.alpaca.markets/stream";
|
|
50
|
+
readonly LIVE: "wss://api.alpaca.markets/stream";
|
|
51
|
+
};
|
|
52
|
+
/** Stock market data stream (v2) */
|
|
53
|
+
readonly STOCKS: {
|
|
54
|
+
readonly PRODUCTION: "wss://stream.data.alpaca.markets/v2/sip";
|
|
55
|
+
readonly TEST: "wss://stream.data.alpaca.markets/v2/test";
|
|
56
|
+
};
|
|
57
|
+
/** Options market data stream (v1beta3) */
|
|
58
|
+
readonly OPTIONS: {
|
|
59
|
+
readonly PRODUCTION: "wss://stream.data.alpaca.markets/v1beta3/options";
|
|
60
|
+
readonly SANDBOX: "wss://stream.data.sandbox.alpaca.markets/v1beta3/options";
|
|
61
|
+
};
|
|
62
|
+
/** Crypto market data stream (v1beta3) */
|
|
63
|
+
readonly CRYPTO: {
|
|
64
|
+
readonly PRODUCTION: "wss://stream.data.alpaca.markets/v1beta3/crypto/us";
|
|
65
|
+
readonly SANDBOX: "wss://stream.data.sandbox.alpaca.markets/v1beta3/crypto/us";
|
|
66
|
+
};
|
|
67
|
+
};
|
|
68
|
+
/**
|
|
69
|
+
* Get trading WebSocket URL for account type
|
|
70
|
+
*/
|
|
71
|
+
export declare function getTradingWebSocketUrl(accountType: AccountType): string;
|
|
72
|
+
/**
|
|
73
|
+
* Get stock stream WebSocket URL
|
|
74
|
+
*/
|
|
75
|
+
export declare function getStockStreamUrl(mode?: "PRODUCTION" | "TEST"): string;
|
|
76
|
+
/**
|
|
77
|
+
* Get options stream WebSocket URL
|
|
78
|
+
*/
|
|
79
|
+
export declare function getOptionsStreamUrl(mode?: "PRODUCTION" | "SANDBOX"): string;
|
|
80
|
+
/**
|
|
81
|
+
* Get crypto stream WebSocket URL
|
|
82
|
+
*/
|
|
83
|
+
export declare function getCryptoStreamUrl(mode?: "PRODUCTION" | "SANDBOX"): string;
|
|
84
|
+
/**
|
|
85
|
+
* Legacy support - map old API version references to current
|
|
86
|
+
* @deprecated Use the constants above directly
|
|
87
|
+
*/
|
|
88
|
+
export declare const LEGACY_API_VERSIONS: {
|
|
89
|
+
/** @deprecated Use MARKET_DATA_API.OPTIONS instead */
|
|
90
|
+
readonly v1beta1: "https://data.alpaca.markets/v1beta1";
|
|
91
|
+
/** @deprecated Use MARKET_DATA_API.CRYPTO instead */
|
|
92
|
+
readonly v1beta3: "https://data.alpaca.markets/v1beta3";
|
|
93
|
+
};
|
|
94
|
+
//# sourceMappingURL=api-endpoints.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"api-endpoints.d.ts","sourceRoot":"","sources":["../../../src/config/api-endpoints.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;AAEH;;GAEG;AACH,MAAM,MAAM,WAAW,GAAG,OAAO,GAAG,MAAM,CAAC;AAE3C;;;GAGG;AACH,eAAO,MAAM,WAAW;;;CAGd,CAAC;AAEX;;GAEG;AACH,wBAAgB,gBAAgB,CAAC,WAAW,EAAE,WAAW,GAAG,MAAM,CAEjE;AAED;;GAEG;AACH,eAAO,MAAM,eAAe;IAC1B,oDAAoD;;IAEpD,gEAAgE;;IAEhE,oCAAoC;;IAEpC,yBAAyB;;CAEjB,CAAC;AAEX;;GAEG;AACH,eAAO,MAAM,iBAAiB;IAC5B,4CAA4C;;;;;IAK5C,oCAAoC;;;;;IAKpC,2CAA2C;;;;;IAK3C,0CAA0C;;;;;CAKlC,CAAC;AAEX;;GAEG;AACH,wBAAgB,sBAAsB,CAAC,WAAW,EAAE,WAAW,GAAG,MAAM,CAEvE;AAED;;GAEG;AACH,wBAAgB,iBAAiB,CAC/B,IAAI,GAAE,YAAY,GAAG,MAAqB,GACzC,MAAM,CAER;AAED;;GAEG;AACH,wBAAgB,mBAAmB,CACjC,IAAI,GAAE,YAAY,GAAG,SAAwB,GAC5C,MAAM,CAER;AAED;;GAEG;AACH,wBAAgB,kBAAkB,CAChC,IAAI,GAAE,YAAY,GAAG,SAAwB,GAC5C,MAAM,CAER;AAED;;;GAGG;AACH,eAAO,MAAM,mBAAmB;IAC9B,sDAAsD;;IAEtD,qDAAqD;;CAE7C,CAAC"}
|