@ckirg/corelib-markets 0.1.22
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/LICENSE +21 -0
- package/README.md +313 -0
- package/dist/index.d.ts +784 -0
- package/dist/index.js +2124 -0
- package/package.json +48 -0
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,784 @@
|
|
|
1
|
+
import * as ky from 'ky';
|
|
2
|
+
import { Options } from 'ky';
|
|
3
|
+
import { Database, StrictLogger } from '@ckirg/corelib';
|
|
4
|
+
import * as luxon from 'luxon';
|
|
5
|
+
export { luxon as Luxon };
|
|
6
|
+
import { EventEmitter } from 'node:events';
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Result pattern for Nasdaq API calls.
|
|
10
|
+
* @template T The type of the value on success.
|
|
11
|
+
*/
|
|
12
|
+
type NasdaqResult<T = unknown> = {
|
|
13
|
+
/** Indicates a successful request and logic check. */
|
|
14
|
+
status: "success";
|
|
15
|
+
/** The data returned by the API (usually the contents of the 'data' field). */
|
|
16
|
+
value: T;
|
|
17
|
+
/** Optional details about the response (headers, status, etc.). */
|
|
18
|
+
details?: unknown;
|
|
19
|
+
} | {
|
|
20
|
+
/** Indicates a transport error or an API-level logic error. */
|
|
21
|
+
status: "error";
|
|
22
|
+
/** The reason for the failure. */
|
|
23
|
+
reason: {
|
|
24
|
+
message: string;
|
|
25
|
+
[key: string]: unknown;
|
|
26
|
+
};
|
|
27
|
+
};
|
|
28
|
+
/**
|
|
29
|
+
* Generates spoofed headers for Nasdaq API requests to ensure compatibility.
|
|
30
|
+
* Dynamically handles differences between standard API calls and charting-specific endpoints.
|
|
31
|
+
*
|
|
32
|
+
* @param {string} url - The URL of the request.
|
|
33
|
+
* @returns {Record<string, string>} A dictionary of headers.
|
|
34
|
+
*/
|
|
35
|
+
declare function getNasdaqHeaders(url: string): Record<string, string>;
|
|
36
|
+
/**
|
|
37
|
+
* Executes a single Nasdaq API request with automatic header spoofing and logic check.
|
|
38
|
+
*
|
|
39
|
+
* @template T The expected type of the data returned in the 'data' field.
|
|
40
|
+
* @param {string | URL | Request} url - The URL or request object.
|
|
41
|
+
* @param {Options} [options] - Additional request options (passed to ky).
|
|
42
|
+
* @returns {Promise<NasdaqResult<T>>} A promise resolving to a NasdaqResult.
|
|
43
|
+
*/
|
|
44
|
+
declare function nasdaqEndPoint<T = unknown>(url: string | URL | Request, options?: Options): Promise<NasdaqResult<T>>;
|
|
45
|
+
/**
|
|
46
|
+
* Executes multiple Nasdaq API requests in parallel.
|
|
47
|
+
*
|
|
48
|
+
* @template T The expected type of the data returned by each request.
|
|
49
|
+
* @param {(string | URL | Request)[]} urls - Array of URLs or request objects.
|
|
50
|
+
* @param {Options} [options] - Additional request options.
|
|
51
|
+
* @returns {Promise<NasdaqResult<T>[]>} A promise resolving to an array of NasdaqResults.
|
|
52
|
+
*/
|
|
53
|
+
declare function nasdaqEndPoints<T = unknown>(urls: (string | URL | Request)[], options?: Options): Promise<NasdaqResult<T>[]>;
|
|
54
|
+
/**
|
|
55
|
+
* Nasdaq API integration section.
|
|
56
|
+
*/
|
|
57
|
+
declare const ApiNasdaqUnlimited: {
|
|
58
|
+
/**
|
|
59
|
+
* Executes a single Nasdaq API request.
|
|
60
|
+
*/
|
|
61
|
+
endPoint: typeof nasdaqEndPoint;
|
|
62
|
+
/**
|
|
63
|
+
* Executes multiple Nasdaq API requests in parallel.
|
|
64
|
+
*/
|
|
65
|
+
endPoints: typeof nasdaqEndPoints;
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* Represents a single row in the Nasdaq symbols database.
|
|
70
|
+
*/
|
|
71
|
+
interface MarketSymbolRow {
|
|
72
|
+
/** Trading symbol (ticker). */
|
|
73
|
+
symbol: string;
|
|
74
|
+
/** Human-readable name of the security. */
|
|
75
|
+
name: string;
|
|
76
|
+
/** Data type: 'rt' (real-time) or 'eod' (end-of-day). */
|
|
77
|
+
type: "rt" | "eod";
|
|
78
|
+
/** Asset class (e.g., 'stocks', 'etf'). */
|
|
79
|
+
class: string;
|
|
80
|
+
/** Last updated timestamp (Unix milliseconds). */
|
|
81
|
+
ts: number;
|
|
82
|
+
/** Indicates if the symbol is currently active. */
|
|
83
|
+
active: boolean;
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* Nasdaq symbol database using ts-core SQLite (local or Turso) alongside API & Ingestor fallbacks.
|
|
87
|
+
*
|
|
88
|
+
* Automatically refreshes on first use or when data is older than today (NY time).
|
|
89
|
+
* Modifies search hierarchy (DB vs API) dynamically based on edge vs non-edge runtimes.
|
|
90
|
+
*/
|
|
91
|
+
declare class MarketSymbols {
|
|
92
|
+
private readonly ingestors;
|
|
93
|
+
private db;
|
|
94
|
+
private initialized;
|
|
95
|
+
private isDbOwner;
|
|
96
|
+
private readonly config?;
|
|
97
|
+
/**
|
|
98
|
+
* Registry mapping URL patterns to specific ingestor methods.
|
|
99
|
+
* Designed to be "open" for additional ingestors in future releases.
|
|
100
|
+
*/
|
|
101
|
+
private readonly ingestorRegistry;
|
|
102
|
+
/**
|
|
103
|
+
* @param db - Optional database configuration or existing instance:
|
|
104
|
+
* - `undefined` → uses `${getTempDir()}/NasdaqSymbols.sqlite`
|
|
105
|
+
* - `string` → local SQLite file path
|
|
106
|
+
* - `{ dbUrl: string; dbToken: string }` → Turso/LibSQL remote
|
|
107
|
+
* - `Database` → An existing instance of a Database driver
|
|
108
|
+
* @param ingestors - Array of ingestor URLs (e.g., Google App Script endpoints) to query for missing symbols.
|
|
109
|
+
*/
|
|
110
|
+
constructor(db?: string | {
|
|
111
|
+
dbUrl: string;
|
|
112
|
+
dbToken: string;
|
|
113
|
+
} | Database, ingestors?: string[]);
|
|
114
|
+
/**
|
|
115
|
+
* Force a full refresh of the symbol database.
|
|
116
|
+
* Called automatically on first use if needed.
|
|
117
|
+
*/
|
|
118
|
+
refresh(): Promise<void>;
|
|
119
|
+
/**
|
|
120
|
+
* Get symbol data.
|
|
121
|
+
* Searches Nasdaq API, external ingestors, and the DB. The sequence order is
|
|
122
|
+
* optimized dynamically based on whether it is running in an Edge environment.
|
|
123
|
+
* @returns `null` if the symbol is not found or is inactive.
|
|
124
|
+
*/
|
|
125
|
+
get(symbol: string): Promise<MarketSymbolRow | null>;
|
|
126
|
+
/**
|
|
127
|
+
* Graceful shutdown – disconnects the database driver if it was created internally.
|
|
128
|
+
*/
|
|
129
|
+
close(): Promise<void>;
|
|
130
|
+
/**
|
|
131
|
+
* Queries the official Nasdaq autocomplete API for a symbol.
|
|
132
|
+
* Filters for an exact match.
|
|
133
|
+
*/
|
|
134
|
+
private searchNasdaqApi;
|
|
135
|
+
/**
|
|
136
|
+
* Queries external ingestors defined in the constructor based on the internal registry pattern.
|
|
137
|
+
*/
|
|
138
|
+
private searchIngestors;
|
|
139
|
+
/**
|
|
140
|
+
* Specifically processes Google Apps Script (GAS) ingestor URLs.
|
|
141
|
+
*/
|
|
142
|
+
private ingestorGAS;
|
|
143
|
+
/**
|
|
144
|
+
* Searches the local or remote SQLite database.
|
|
145
|
+
*/
|
|
146
|
+
private searchDb;
|
|
147
|
+
/**
|
|
148
|
+
* Initializes the database driver if not already done.
|
|
149
|
+
* Creates the `nasdaq_symbols` table if it doesn't exist.
|
|
150
|
+
* Creates an index on the `active` column if it doesn't exist.
|
|
151
|
+
* Called automatically on first use, and before any other operations.
|
|
152
|
+
*/
|
|
153
|
+
private ensureInitialized;
|
|
154
|
+
/**
|
|
155
|
+
* Checks if the database needs to be refreshed.
|
|
156
|
+
* Returns true if the database has never been populated, or if the last refresh was not today.
|
|
157
|
+
* @returns {Promise<boolean>} true if the database needs to be refreshed
|
|
158
|
+
*/
|
|
159
|
+
private needsRefresh;
|
|
160
|
+
/**
|
|
161
|
+
* Refreshes the symbol database.
|
|
162
|
+
* Only runs if the database has never been populated, or if the last refresh was not today.
|
|
163
|
+
* Downloads the official Nasdaq symbol directories, parses them, and updates the database.
|
|
164
|
+
* @returns {Promise<void>} resolves after the database has been refreshed
|
|
165
|
+
*/
|
|
166
|
+
private performRefresh;
|
|
167
|
+
/**
|
|
168
|
+
* Downloads the official Nasdaq symbol directories with retry and circuit breaker.
|
|
169
|
+
* Retries with exponential backoff up to `markets.nasdaq.symbols.maxRetryBackoffMs` per interval.
|
|
170
|
+
* Stops after `markets.nasdaq.symbols.maxFetchRetries` consecutive failures even when existing data is present.
|
|
171
|
+
* Throws immediately on first failure when no existing data exists.
|
|
172
|
+
*/
|
|
173
|
+
private fetchSymbolFilesWithRetry;
|
|
174
|
+
/**
|
|
175
|
+
* Checks if there is existing data in the database.
|
|
176
|
+
* Returns true if there is any existing data, false otherwise.
|
|
177
|
+
* @returns {Promise<boolean>} true if there is any existing data, false otherwise
|
|
178
|
+
*/
|
|
179
|
+
private hasExistingData;
|
|
180
|
+
/**
|
|
181
|
+
* Parses the official Nasdaq symbol directory file (nasdaqlisted.txt).
|
|
182
|
+
* Skips the first two lines (header) and empty lines.
|
|
183
|
+
* Skips lines with less than 8 fields (invalid).
|
|
184
|
+
* Extracts the symbol, name, and ETF status from the line.
|
|
185
|
+
* Creates a MarketSymbolRow with the extracted data and adds it to the result array.
|
|
186
|
+
* @param {string} text - The content of the nasdaqlisted.txt file as a string
|
|
187
|
+
* @returns {MarketSymbolRow[]} - An array of MarketSymbolRow objects parsed from the file
|
|
188
|
+
*/
|
|
189
|
+
private parseNasdaqListed;
|
|
190
|
+
/**
|
|
191
|
+
* Parses the official Nasdaq symbol directory file (otherlisted.txt).
|
|
192
|
+
* Skips the first two lines (header) and empty lines.
|
|
193
|
+
* Skips lines with less than 5 fields (invalid).
|
|
194
|
+
* Extracts the symbol, name, and ETF status from the line.
|
|
195
|
+
* Creates a MarketSymbolRow with the extracted data and adds it to the result array.
|
|
196
|
+
* @param {string} text - The content of the otherlisted.txt file as a string
|
|
197
|
+
* @returns {MarketSymbolRow[]} - An array of MarketSymbolRow objects parsed from the file
|
|
198
|
+
*/
|
|
199
|
+
private parseOtherListed;
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
/**
|
|
203
|
+
* Configuration options for the ApiNasdaqQuotes module.
|
|
204
|
+
*/
|
|
205
|
+
interface ApiNasdaqQuotesOptions {
|
|
206
|
+
/** Standardized logger for error and warning reporting */
|
|
207
|
+
logger?: StrictLogger;
|
|
208
|
+
/** Optional list of proxy URLs. If provided, requests will be routed through RequestProxied */
|
|
209
|
+
proxies?: string[];
|
|
210
|
+
/** Optional instance of MarketSymbols. If not provided, a new one will be created internally */
|
|
211
|
+
marketSymbols?: MarketSymbols;
|
|
212
|
+
/** Concurrency limit for non-proxied requests to prevent Nasdaq rate limiting. Defaults to 5 */
|
|
213
|
+
concurrencyLimit?: number;
|
|
214
|
+
}
|
|
215
|
+
/**
|
|
216
|
+
* ApiNasdaqQuotes handles fetching ticker info and quotes from the unofficial Nasdaq API.
|
|
217
|
+
* It manages asset class resolution via MarketSymbols and supports both proxied and
|
|
218
|
+
* rate-limited (batched) request strategies.
|
|
219
|
+
*/
|
|
220
|
+
declare class ApiNasdaqQuotes {
|
|
221
|
+
private readonly logger?;
|
|
222
|
+
private readonly marketSymbols;
|
|
223
|
+
private readonly requestProxied?;
|
|
224
|
+
private readonly isInternalMarketSymbols;
|
|
225
|
+
private readonly concurrencyLimit;
|
|
226
|
+
/**
|
|
227
|
+
* Creates an instance of ApiNasdaqQuotes.
|
|
228
|
+
* @param options Configuration options for the module.
|
|
229
|
+
*/
|
|
230
|
+
constructor(options?: ApiNasdaqQuotesOptions);
|
|
231
|
+
/**
|
|
232
|
+
* Retrieves real-time quotes for a batch of symbols.
|
|
233
|
+
* Results are returned in an array mirroring the order of the input symbols.
|
|
234
|
+
* @param symbols An array of ticker symbols (e.g. ['AAPL', 'MSFT']).
|
|
235
|
+
* @returns A promise resolving to an array of NasdaqResult objects.
|
|
236
|
+
*/
|
|
237
|
+
getNasdaqQuote<T = unknown>(symbols: string[]): Promise<NasdaqResult<T>[]>;
|
|
238
|
+
/**
|
|
239
|
+
* Properly shuts down internal resources and database connections.
|
|
240
|
+
* Must be called if MarketSymbols was instantiated internally.
|
|
241
|
+
*/
|
|
242
|
+
close(): Promise<void>;
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
/**
|
|
246
|
+
* Result pattern for CNN API calls.
|
|
247
|
+
* @template T The type of the value on success.
|
|
248
|
+
*/
|
|
249
|
+
type CnnResult<T = unknown> = {
|
|
250
|
+
/** Indicates a successful request and schema validation. */
|
|
251
|
+
status: "success";
|
|
252
|
+
/** The filtered data from the CNN response. */
|
|
253
|
+
value: T;
|
|
254
|
+
/** Optional details about the response. */
|
|
255
|
+
details?: unknown;
|
|
256
|
+
} | {
|
|
257
|
+
/** Indicates a transport error or schema validation failure. */
|
|
258
|
+
status: "error";
|
|
259
|
+
/** The reason for the failure. */
|
|
260
|
+
reason: {
|
|
261
|
+
message: string;
|
|
262
|
+
[key: string]: unknown;
|
|
263
|
+
};
|
|
264
|
+
};
|
|
265
|
+
/**
|
|
266
|
+
* Available filters for the CNN Fear & Greed API.
|
|
267
|
+
* Each member corresponds to a key in the raw JSON response.
|
|
268
|
+
*/
|
|
269
|
+
declare enum CnnFearAndGreedFilter {
|
|
270
|
+
/** Overall Fear & Greed Index value and description. */
|
|
271
|
+
FearAndGreed = "fear_and_greed",
|
|
272
|
+
/** Historical Fear & Greed Index values. */
|
|
273
|
+
FearAndGreedHistorical = "fear_and_greed_historical",
|
|
274
|
+
/** Market Momentum based on the S&P 500. */
|
|
275
|
+
MarketMomentumSp500 = "market_momentum_sp500",
|
|
276
|
+
/** Market Momentum based on the S&P 125. */
|
|
277
|
+
MarketMomentumSp125 = "market_momentum_sp125",
|
|
278
|
+
/** Stock Price Strength index. */
|
|
279
|
+
StockPriceStrength = "stock_price_strength",
|
|
280
|
+
/** Stock Price Breadth index. */
|
|
281
|
+
StockPriceBreadth = "stock_price_breadth",
|
|
282
|
+
/** Put and Call Options ratio. */
|
|
283
|
+
PutCallOptions = "put_call_options",
|
|
284
|
+
/** Market Volatility (VIX). */
|
|
285
|
+
MarketVolatilityVix = "market_volatility_vix",
|
|
286
|
+
/** Market Volatility (VIX 50-day moving average). */
|
|
287
|
+
MarketVolatilityVix50 = "market_volatility_vix_50",
|
|
288
|
+
/** Junk Bond Demand index. */
|
|
289
|
+
JunkBondDemand = "junk_bond_demand",
|
|
290
|
+
/** Safe Haven Demand index. */
|
|
291
|
+
SafeHavenDemand = "safe_haven_demand"
|
|
292
|
+
}
|
|
293
|
+
/**
|
|
294
|
+
* Input type for filtering CNN API results.
|
|
295
|
+
* Can be a single filter, an array of filters, or "full" to return the entire response body.
|
|
296
|
+
*/
|
|
297
|
+
type CnnFilterInput = CnnFearAndGreedFilter | CnnFearAndGreedFilter[] | "full";
|
|
298
|
+
/**
|
|
299
|
+
* Fetches the CNN Fear & Greed Index data.
|
|
300
|
+
*
|
|
301
|
+
* @param {string | "Historical"} [date] - Optional date in YYYY-MM-DD format or "Historical" for 1-year data.
|
|
302
|
+
* @param {CnnFilterInput} [filter=CnnFearAndGreedFilter.FearAndGreed] - Optional filter to apply to the result.
|
|
303
|
+
* @param {Options} [options={}] - Additional request options for ky.
|
|
304
|
+
* @returns {Promise<CnnResult<unknown>>} A promise resolving to a CnnResult.
|
|
305
|
+
*/
|
|
306
|
+
declare function getFearAndGreed(date?: string | "Historical", filter?: CnnFilterInput, options?: Options): Promise<CnnResult<unknown>>;
|
|
307
|
+
/**
|
|
308
|
+
* CNN Fear & Greed Index integration section.
|
|
309
|
+
*/
|
|
310
|
+
declare const CnnFearAndGreed: {
|
|
311
|
+
/**
|
|
312
|
+
* Fetches Fear & Greed Index data.
|
|
313
|
+
*/
|
|
314
|
+
getFearAndGreed: typeof getFearAndGreed;
|
|
315
|
+
};
|
|
316
|
+
|
|
317
|
+
/**
|
|
318
|
+
* Standardized options for fetching historical data.
|
|
319
|
+
*/
|
|
320
|
+
interface HistoricalOptions {
|
|
321
|
+
/** Start date of the historical data range. Can be a Date object, ISO string, or Unix timestamp. */
|
|
322
|
+
period1: Date | string | number;
|
|
323
|
+
/** End date of the historical data range (inclusive). Defaults to current time if omitted. */
|
|
324
|
+
period2?: Date | string | number;
|
|
325
|
+
/** The resolution of the data points. */
|
|
326
|
+
interval?: "1d" | "1wk" | "1mo";
|
|
327
|
+
}
|
|
328
|
+
/**
|
|
329
|
+
* Strictly standardized JSON format for historical pricing data.
|
|
330
|
+
* Dates are guaranteed to be ISO-8601 strings.
|
|
331
|
+
*/
|
|
332
|
+
interface HistoricalQuote {
|
|
333
|
+
/** Trading symbol. */
|
|
334
|
+
symbol: string;
|
|
335
|
+
/** ISO-8601 date string for the quote. */
|
|
336
|
+
date: string;
|
|
337
|
+
/** Opening price. */
|
|
338
|
+
open: number;
|
|
339
|
+
/** Highest price during the period. */
|
|
340
|
+
high: number;
|
|
341
|
+
/** Lowest price during the period. */
|
|
342
|
+
low: number;
|
|
343
|
+
/** Closing price. */
|
|
344
|
+
close: number;
|
|
345
|
+
/** Trading volume. */
|
|
346
|
+
volume: number;
|
|
347
|
+
/** Adjusted closing price (accounting for dividends and splits). */
|
|
348
|
+
adjClose: number | null;
|
|
349
|
+
}
|
|
350
|
+
/**
|
|
351
|
+
* Standard Corelib result pattern for historical data.
|
|
352
|
+
*/
|
|
353
|
+
type HistoricalResult = {
|
|
354
|
+
/** Indicates a successful data retrieval. */
|
|
355
|
+
status: "success";
|
|
356
|
+
/** Array of historical quotes. */
|
|
357
|
+
value: HistoricalQuote[];
|
|
358
|
+
} | {
|
|
359
|
+
/** Indicates a failure in data retrieval. */
|
|
360
|
+
status: "error";
|
|
361
|
+
/** The reason for the failure. */
|
|
362
|
+
reason: {
|
|
363
|
+
/** Error message. */
|
|
364
|
+
message: string;
|
|
365
|
+
/** Optional error payload for debugging. */
|
|
366
|
+
payload?: unknown;
|
|
367
|
+
[key: string]: unknown;
|
|
368
|
+
};
|
|
369
|
+
};
|
|
370
|
+
|
|
371
|
+
/**
|
|
372
|
+
* Historical Data Module
|
|
373
|
+
* Provides standardized access to historical pricing data.
|
|
374
|
+
*/
|
|
375
|
+
declare const Historical: {
|
|
376
|
+
/**
|
|
377
|
+
* Retrieves historical data for a given symbol.
|
|
378
|
+
*/
|
|
379
|
+
getData: (symbol: string, options: HistoricalOptions) => Promise<HistoricalResult>;
|
|
380
|
+
};
|
|
381
|
+
|
|
382
|
+
/**
|
|
383
|
+
* NasdaqPolling handles periodic polling of Nasdaq stock quotes.
|
|
384
|
+
* It uses a list of symbols and a set of proxies to fetch data via ApiNasdaqQuotes.
|
|
385
|
+
* * @example
|
|
386
|
+
* const poller = new NasdaqPolling(10000, ["https://proxy-url..."]);
|
|
387
|
+
* poller.on("data", (data) => console.log(data));
|
|
388
|
+
* poller.subscribe(["AAPL", "MSFT"]);
|
|
389
|
+
* poller.start();
|
|
390
|
+
*/
|
|
391
|
+
declare class NasdaqPolling extends EventEmitter {
|
|
392
|
+
private intervalId;
|
|
393
|
+
private subscriptions;
|
|
394
|
+
private apiInterval;
|
|
395
|
+
private proxies;
|
|
396
|
+
private nasdaqQuotes;
|
|
397
|
+
/**
|
|
398
|
+
* @param apiInterval - Polling interval in milliseconds (defaults to 10000ms/10s).
|
|
399
|
+
* @param proxies - Array of proxy URLs to rotate or use for requests.
|
|
400
|
+
*/
|
|
401
|
+
constructor(apiInterval?: number, proxies?: string[]);
|
|
402
|
+
/**
|
|
403
|
+
* Updates the polling interval at runtime.
|
|
404
|
+
* If polling is active, it will restart with the new interval.
|
|
405
|
+
* @param value - New interval in milliseconds.
|
|
406
|
+
*/
|
|
407
|
+
setApiInterval(value: number): void;
|
|
408
|
+
/**
|
|
409
|
+
* Adds symbols to the internal subscription list.
|
|
410
|
+
* @param symbols - Array of stock symbols (e.g., ["AAPL", "MSFT"]).
|
|
411
|
+
*/
|
|
412
|
+
subscribe(symbols: string[]): void;
|
|
413
|
+
/**
|
|
414
|
+
* Removes symbols from the internal subscription list.
|
|
415
|
+
* @param symbols - Array of stock symbols to remove.
|
|
416
|
+
*/
|
|
417
|
+
unsubscribe(symbols: string[]): void;
|
|
418
|
+
/**
|
|
419
|
+
* Starts the polling process at the defined apiInterval.
|
|
420
|
+
* If polling is already active, this method does nothing.
|
|
421
|
+
*/
|
|
422
|
+
start(): void;
|
|
423
|
+
/**
|
|
424
|
+
* Stops the polling process.
|
|
425
|
+
* Existing subscriptions are preserved.
|
|
426
|
+
*/
|
|
427
|
+
stop(): void;
|
|
428
|
+
/**
|
|
429
|
+
* Resets the internal subscription list and halts active polling.
|
|
430
|
+
*/
|
|
431
|
+
clear(): void;
|
|
432
|
+
/**
|
|
433
|
+
* Internal logic to fetch data from ApiNasdaqQuotes and emit results.
|
|
434
|
+
*/
|
|
435
|
+
private poll;
|
|
436
|
+
}
|
|
437
|
+
|
|
438
|
+
/**
|
|
439
|
+
* AlpacaStreaming
|
|
440
|
+
* Provides a real-time data stream from Alpaca using the native Rust library via FFI.
|
|
441
|
+
* Emits events for pricing, logging, and connection status.
|
|
442
|
+
*
|
|
443
|
+
* Construction calls into the native addon and throws SYNCHRONOUSLY if the Rust side
|
|
444
|
+
* fails to initialize (e.g. redb cannot open its db path). Wrap `new AlpacaStreaming()`
|
|
445
|
+
* in try/catch. Ensure ConfigManager is initialized before constructing so config-derived
|
|
446
|
+
* db paths resolve.
|
|
447
|
+
*/
|
|
448
|
+
declare class AlpacaStreaming extends EventEmitter {
|
|
449
|
+
private rust;
|
|
450
|
+
private initialized;
|
|
451
|
+
private readonly health;
|
|
452
|
+
constructor();
|
|
453
|
+
/**
|
|
454
|
+
* Initializes the configuration for the Alpaca streaming client.
|
|
455
|
+
*
|
|
456
|
+
* @param {object} [config] - Configuration options.
|
|
457
|
+
* @param {string} [config.dbPath] - Path to the local persistence database. Defaults to system temp.
|
|
458
|
+
* @param {number} [config.silenceSeconds] - Duration of silence (in seconds) before triggering a reconnect.
|
|
459
|
+
* @param {string} [config.baseUrl] - Alpaca API base URL.
|
|
460
|
+
* @param {string} [config.keyId] - Alpaca API Key ID.
|
|
461
|
+
* @param {string} [config.secretKey] - Alpaca API Secret Key.
|
|
462
|
+
* @returns {Promise<void>}
|
|
463
|
+
*/
|
|
464
|
+
init(config?: {
|
|
465
|
+
dbPath?: string;
|
|
466
|
+
silenceSeconds?: number;
|
|
467
|
+
baseUrl?: string;
|
|
468
|
+
keyId?: string;
|
|
469
|
+
secretKey?: string;
|
|
470
|
+
}): Promise<void>;
|
|
471
|
+
/**
|
|
472
|
+
* Starts the streaming client and begins connecting to Alpaca.
|
|
473
|
+
* @returns {Promise<void>}
|
|
474
|
+
*/
|
|
475
|
+
start(): Promise<void>;
|
|
476
|
+
/**
|
|
477
|
+
* Subscribes to real-time updates for the specified symbols.
|
|
478
|
+
* @param {string[] | { trades?: string[]; quotes?: string[]; bars?: string[] }} input - Array of symbols (mapped to quotes) or subscription options object.
|
|
479
|
+
*/
|
|
480
|
+
subscribe(input: string[] | {
|
|
481
|
+
trades?: string[];
|
|
482
|
+
quotes?: string[];
|
|
483
|
+
bars?: string[];
|
|
484
|
+
}): void;
|
|
485
|
+
/**
|
|
486
|
+
* Unsubscribes from updates for the specified symbols.
|
|
487
|
+
* @param {string[] | { trades?: string[]; quotes?: string[]; bars?: string[] }} input - Array of symbols (mapped to quotes) or subscription options object.
|
|
488
|
+
*/
|
|
489
|
+
unsubscribe(input: string[] | {
|
|
490
|
+
trades?: string[];
|
|
491
|
+
quotes?: string[];
|
|
492
|
+
bars?: string[];
|
|
493
|
+
}): void;
|
|
494
|
+
/**
|
|
495
|
+
* Cleans up the local state/database.
|
|
496
|
+
*/
|
|
497
|
+
clean(): void;
|
|
498
|
+
/**
|
|
499
|
+
* Stops the streaming client and disconnects.
|
|
500
|
+
*/
|
|
501
|
+
stop(): void;
|
|
502
|
+
}
|
|
503
|
+
|
|
504
|
+
interface FinnhubPricingData {
|
|
505
|
+
symbol: string;
|
|
506
|
+
message_type: string;
|
|
507
|
+
price: number;
|
|
508
|
+
volume: number;
|
|
509
|
+
timestamp: number;
|
|
510
|
+
conditions?: string[];
|
|
511
|
+
}
|
|
512
|
+
interface FinnhubConfig {
|
|
513
|
+
token?: string;
|
|
514
|
+
name?: string;
|
|
515
|
+
baseUrl?: string;
|
|
516
|
+
}
|
|
517
|
+
/**
|
|
518
|
+
* Real-time Finnhub trade stream via the native Rust library (coreFFI.FinnhubStreaming).
|
|
519
|
+
* Emits: `pricing` (FinnhubPricingData), `log` ({level,msg,extras?}), and connection events
|
|
520
|
+
* (`connected`, `disconnected`, `reconnecting`, `error`).
|
|
521
|
+
*
|
|
522
|
+
* Construction calls into the native addon and throws SYNCHRONOUSLY if the Rust side
|
|
523
|
+
* fails to initialize (e.g. redb cannot open its db path). Wrap `new FinnhubStreaming()`
|
|
524
|
+
* in try/catch. Ensure ConfigManager is initialized before constructing so config-derived
|
|
525
|
+
* db paths resolve.
|
|
526
|
+
*/
|
|
527
|
+
declare class FinnhubStreaming extends EventEmitter {
|
|
528
|
+
private rust;
|
|
529
|
+
private initialized;
|
|
530
|
+
private readonly health;
|
|
531
|
+
constructor();
|
|
532
|
+
init(config?: FinnhubConfig): Promise<void>;
|
|
533
|
+
start(): Promise<void>;
|
|
534
|
+
subscribe(symbols: string[]): Promise<void>;
|
|
535
|
+
unsubscribe(symbols: string[]): Promise<void>;
|
|
536
|
+
stop(): Promise<void>;
|
|
537
|
+
clean(): Promise<void>;
|
|
538
|
+
}
|
|
539
|
+
|
|
540
|
+
/**
|
|
541
|
+
* YahooStreaming
|
|
542
|
+
* Provides a real-time data stream from Yahoo Finance using the native Rust library via FFI.
|
|
543
|
+
* Emits events for pricing, logging, and connection status.
|
|
544
|
+
*
|
|
545
|
+
* Construction calls into the native addon and throws SYNCHRONOUSLY if the Rust side
|
|
546
|
+
* fails to initialize (e.g. redb cannot open its db path). Wrap `new YahooStreaming()`
|
|
547
|
+
* in try/catch. Ensure ConfigManager is initialized before constructing so config-derived
|
|
548
|
+
* db paths resolve.
|
|
549
|
+
*/
|
|
550
|
+
declare class YahooStreaming extends EventEmitter {
|
|
551
|
+
private rust;
|
|
552
|
+
private initialized;
|
|
553
|
+
private readonly health;
|
|
554
|
+
constructor();
|
|
555
|
+
/**
|
|
556
|
+
* Initializes the configuration for the Yahoo streaming client.
|
|
557
|
+
*
|
|
558
|
+
* @param {object} [config] - Configuration options.
|
|
559
|
+
* @param {string} [config.dbPath] - Path to the local persistence database.
|
|
560
|
+
* @param {number} [config.silenceSeconds] - Duration of silence (in seconds) before triggering a reconnect.
|
|
561
|
+
* @returns {Promise<void>}
|
|
562
|
+
*/
|
|
563
|
+
init(config?: {
|
|
564
|
+
dbPath?: string;
|
|
565
|
+
silenceSeconds?: number;
|
|
566
|
+
}): Promise<void>;
|
|
567
|
+
/**
|
|
568
|
+
* Starts the streaming client and begins connecting.
|
|
569
|
+
* @returns {Promise<void>}
|
|
570
|
+
*/
|
|
571
|
+
start(): Promise<void>;
|
|
572
|
+
/**
|
|
573
|
+
* Subscribes to real-time updates for the specified symbols.
|
|
574
|
+
* @param {string[]} symbols - Array of trading symbols.
|
|
575
|
+
*/
|
|
576
|
+
subscribe(symbols: string[]): void;
|
|
577
|
+
/**
|
|
578
|
+
* Unsubscribes from updates for the specified symbols.
|
|
579
|
+
* @param {string[]} symbols - Array of trading symbols.
|
|
580
|
+
*/
|
|
581
|
+
unsubscribe(symbols: string[]): void;
|
|
582
|
+
/**
|
|
583
|
+
* Cleans up the local state/database.
|
|
584
|
+
*/
|
|
585
|
+
clean(): void;
|
|
586
|
+
/**
|
|
587
|
+
* Stops the streaming client and disconnects.
|
|
588
|
+
*/
|
|
589
|
+
stop(): void;
|
|
590
|
+
}
|
|
591
|
+
|
|
592
|
+
/**
|
|
593
|
+
* @module Nasdaq/Groups/Top100
|
|
594
|
+
* @description Provides access to the Nasdaq 100 constituent symbols with in-memory caching and request collapsing.
|
|
595
|
+
*/
|
|
596
|
+
/**
|
|
597
|
+
* Retrieves the list of Nasdaq 100 symbols, sorted alphabetically.
|
|
598
|
+
*
|
|
599
|
+
* @remarks
|
|
600
|
+
* - If the data is already cached, it returns the cached list immediately.
|
|
601
|
+
* - If a request is already in progress, it returns the existing promise.
|
|
602
|
+
* - In case of failure or empty data, it logs a warning via StrictLogger and returns an empty array.
|
|
603
|
+
*
|
|
604
|
+
* @returns A Promise resolving to an array of ticker symbols (e.g., ["AAPL", "AMZN", ...]).
|
|
605
|
+
*/
|
|
606
|
+
declare function getSymbolsTop100(): Promise<string[]>;
|
|
607
|
+
|
|
608
|
+
/**
|
|
609
|
+
* Interface matching the raw JSON data structure from Nasdaq API's "data" field.
|
|
610
|
+
*/
|
|
611
|
+
interface NasdaqMarketInfo {
|
|
612
|
+
/** Country associated with the market. */
|
|
613
|
+
country: string;
|
|
614
|
+
/** Human-readable market status indicator (e.g., "Market Open"). */
|
|
615
|
+
marketIndicator: string;
|
|
616
|
+
/** UI-optimized market status indicator. */
|
|
617
|
+
uiMarketIndicator: string;
|
|
618
|
+
/** Human-readable countdown to market open/close (e.g., "Market Closes in 3H 37M"). */
|
|
619
|
+
marketCountDown: string;
|
|
620
|
+
/** Simplified market status (e.g., "Open", "Closed", "Pre Market", "After Hours"). */
|
|
621
|
+
mrktStatus: string;
|
|
622
|
+
/** Numeric or UI-optimized market countdown. */
|
|
623
|
+
mrktCountDown: string;
|
|
624
|
+
/** Pre-market opening time (e.g., "Mar 9, 2026 04:00 AM ET"). */
|
|
625
|
+
preMarketOpeningTime: string;
|
|
626
|
+
/** Pre-market closing time. */
|
|
627
|
+
preMarketClosingTime: string;
|
|
628
|
+
/** Regular market opening time. */
|
|
629
|
+
marketOpeningTime: string;
|
|
630
|
+
/** Regular market closing time. */
|
|
631
|
+
marketClosingTime: string;
|
|
632
|
+
/** After-hours market opening time. */
|
|
633
|
+
afterHoursMarketOpeningTime: string;
|
|
634
|
+
/** After-hours market closing time. */
|
|
635
|
+
afterHoursMarketClosingTime: string;
|
|
636
|
+
/** Previous trading date string (e.g., "Mar 6, 2026"). */
|
|
637
|
+
previousTradeDate: string;
|
|
638
|
+
/** Next trading date string (e.g., "Mar 10, 2026"). */
|
|
639
|
+
nextTradeDate: string;
|
|
640
|
+
/** Raw pre-market opening time in ISO format (NY time, no offset). e.g. "2026-03-09T04:00:00" */
|
|
641
|
+
pmOpenRaw: string;
|
|
642
|
+
/** Raw regular market opening time in ISO format. */
|
|
643
|
+
openRaw: string;
|
|
644
|
+
/** Raw regular market closing time in ISO format. */
|
|
645
|
+
closeRaw: string;
|
|
646
|
+
/** Raw after-hours market closing time in ISO format. */
|
|
647
|
+
ahCloseRaw: string;
|
|
648
|
+
/** Indicates if the current date is a business day for the market. */
|
|
649
|
+
isBusinessDay: boolean;
|
|
650
|
+
}
|
|
651
|
+
/**
|
|
652
|
+
* Calculates how long to sleep/wait based on market status in milliseconds.
|
|
653
|
+
* Useful for polling services that need to wait for market open.
|
|
654
|
+
*
|
|
655
|
+
* @param {NasdaqMarketInfo} data - The current market information.
|
|
656
|
+
* @returns {number} The sleep duration in milliseconds.
|
|
657
|
+
*/
|
|
658
|
+
declare function getSleepDuration(data: NasdaqMarketInfo): number;
|
|
659
|
+
/**
|
|
660
|
+
* Fetches the current market status from Nasdaq API.
|
|
661
|
+
* Performs strict schema validation on the response.
|
|
662
|
+
*
|
|
663
|
+
* @returns {Promise<NasdaqResult<NasdaqMarketInfo>>} A promise resolving to a NasdaqResult.
|
|
664
|
+
*/
|
|
665
|
+
declare function getStatus(): Promise<NasdaqResult<NasdaqMarketInfo>>;
|
|
666
|
+
/**
|
|
667
|
+
* Market status utility.
|
|
668
|
+
*/
|
|
669
|
+
declare const MarketStatus: {
|
|
670
|
+
/**
|
|
671
|
+
* Fetches the current market status.
|
|
672
|
+
*/
|
|
673
|
+
getStatus: typeof getStatus;
|
|
674
|
+
/**
|
|
675
|
+
* Calculates the sleep duration until next market phase.
|
|
676
|
+
*/
|
|
677
|
+
getSleepDuration: typeof getSleepDuration;
|
|
678
|
+
};
|
|
679
|
+
|
|
680
|
+
/**
|
|
681
|
+
* Represents the current phase of the market.
|
|
682
|
+
*/
|
|
683
|
+
type MarketPhase = "open" | "pre-market" | "after-hours" | "closed";
|
|
684
|
+
/**
|
|
685
|
+
* MarketMonitor – resilient, adaptive market status poller.
|
|
686
|
+
*
|
|
687
|
+
* Long-running task that:
|
|
688
|
+
* • Polls Nasdaq market status at adaptive intervals
|
|
689
|
+
* • Emits phase changes immediately after first successful poll and on every phase change
|
|
690
|
+
* • Falls back to heuristic (time-based) phase + cached data during fetch failures
|
|
691
|
+
* • Logs warnings throttled to `warnIntervalSec`
|
|
692
|
+
* • Graceful stop with 'stopped' event
|
|
693
|
+
*
|
|
694
|
+
* @example
|
|
695
|
+
* const monitor = new MarketMonitor({ liveIntervalSec: 15 });
|
|
696
|
+
* monitor.on("status-change", (phase, data, heuristic) => {
|
|
697
|
+
* console.log(`Phase changed to ${phase} (heuristic: ${!!heuristic})`, data);
|
|
698
|
+
* });
|
|
699
|
+
* monitor.start();
|
|
700
|
+
*
|
|
701
|
+
* @event status-change
|
|
702
|
+
* @param {MarketPhase} phase - Current market phase
|
|
703
|
+
* @param {NasdaqMarketInfo & { heuristic?: true }} data - Full market info (cloned + heuristic flag during failures)
|
|
704
|
+
* @param {boolean} [heuristic] - `true` when using cached data because fetch failed
|
|
705
|
+
*
|
|
706
|
+
* @event stopped
|
|
707
|
+
*/
|
|
708
|
+
declare class MarketMonitor extends EventEmitter {
|
|
709
|
+
private liveIntervalSec;
|
|
710
|
+
private closedIntervalSec;
|
|
711
|
+
private warnIntervalSec;
|
|
712
|
+
private proxies;
|
|
713
|
+
private proxyIndex;
|
|
714
|
+
private timeoutId;
|
|
715
|
+
private isRunning;
|
|
716
|
+
private lastData;
|
|
717
|
+
private lastPhase;
|
|
718
|
+
private lastWarnTime;
|
|
719
|
+
private failureCount;
|
|
720
|
+
private hasEmitted;
|
|
721
|
+
/**
|
|
722
|
+
* @param {object} [options] - Configuration options.
|
|
723
|
+
* @param {number} [options.liveIntervalSec] - Polling interval in seconds when market is active.
|
|
724
|
+
* @param {number} [options.closedIntervalSec] - Polling interval in seconds when market is closed.
|
|
725
|
+
* @param {number} [options.warnIntervalSec] - Interval for logging fetch failure warnings.
|
|
726
|
+
* @param {string[]} [options.proxies] - Optional array of proxy URLs for status fetching.
|
|
727
|
+
*/
|
|
728
|
+
constructor(options?: {
|
|
729
|
+
liveIntervalSec?: number;
|
|
730
|
+
closedIntervalSec?: number;
|
|
731
|
+
warnIntervalSec?: number;
|
|
732
|
+
proxies?: string[];
|
|
733
|
+
});
|
|
734
|
+
/** Start the monitor. First emission happens only after the first successful poll. */
|
|
735
|
+
start(): void;
|
|
736
|
+
/** Graceful shutdown. Clears timer and emits 'stopped'. */
|
|
737
|
+
stop(): void;
|
|
738
|
+
/** Current running state */
|
|
739
|
+
get isRunningState(): boolean;
|
|
740
|
+
/** Last known phase (real or heuristic) */
|
|
741
|
+
get currentPhase(): MarketPhase;
|
|
742
|
+
/** Last known full market data (null until first success) */
|
|
743
|
+
get lastKnownData(): NasdaqMarketInfo | null;
|
|
744
|
+
/** Number of consecutive fetch failures (reset on success) */
|
|
745
|
+
get failureCountValue(): number;
|
|
746
|
+
private poll;
|
|
747
|
+
private handleSuccess;
|
|
748
|
+
private handleFailure;
|
|
749
|
+
/**
|
|
750
|
+
* Determine market phase.
|
|
751
|
+
* 1. Try to normalize the official mrktStatus field first
|
|
752
|
+
* 2. Fall back to precise time-based calculation using the four raw timestamps
|
|
753
|
+
*/
|
|
754
|
+
private determinePhase;
|
|
755
|
+
private scheduleNextPoll;
|
|
756
|
+
/**
|
|
757
|
+
* Adaptive polling interval.
|
|
758
|
+
* • No data yet → warnIntervalSec
|
|
759
|
+
* • Has data → use liveIntervalSec or closedIntervalSec based on CURRENT (real or heuristic) phase
|
|
760
|
+
*/
|
|
761
|
+
private getPollIntervalMs;
|
|
762
|
+
private maybeLogWarn;
|
|
763
|
+
}
|
|
764
|
+
|
|
765
|
+
/**
|
|
766
|
+
* Main entry point for market data integrations.
|
|
767
|
+
* Organized by provider and data source.
|
|
768
|
+
*/
|
|
769
|
+
declare const Markets: {
|
|
770
|
+
/** Nasdaq-specific integrations. */
|
|
771
|
+
nasdaq: {
|
|
772
|
+
/** High-performance Nasdaq API wrapper. */
|
|
773
|
+
ApiNasdaqUnlimited: {
|
|
774
|
+
endPoint: <T = unknown>(url: string | URL | Request, options?: ky.Options) => Promise<NasdaqResult<T>>;
|
|
775
|
+
endPoints: <T = unknown>(urls: (string | URL | Request)[], options?: ky.Options) => Promise<NasdaqResult<T>[]>;
|
|
776
|
+
};
|
|
777
|
+
/** Simple Nasdaq quote fetcher. */
|
|
778
|
+
ApiNasdaqQuotes: typeof ApiNasdaqQuotes;
|
|
779
|
+
/** Persistent Nasdaq symbol database. */
|
|
780
|
+
MarketSymbols: typeof MarketSymbols;
|
|
781
|
+
};
|
|
782
|
+
};
|
|
783
|
+
|
|
784
|
+
export { AlpacaStreaming, ApiNasdaqQuotes, type ApiNasdaqQuotesOptions, ApiNasdaqUnlimited, CnnFearAndGreed, CnnFearAndGreedFilter, type CnnFilterInput, type CnnResult, type FinnhubConfig, type FinnhubPricingData, FinnhubStreaming, Historical, type HistoricalQuote, type HistoricalResult, MarketMonitor, type MarketPhase, MarketStatus, type MarketSymbolRow, MarketSymbols, Markets, type NasdaqMarketInfo, NasdaqPolling, type NasdaqResult, YahooStreaming, getNasdaqHeaders, getSymbolsTop100 };
|