import {z} from 'zod' export type GetApiV1StocksTickerCorrelatedArgs = {ticker: string, scope?: string | undefined, days?: number | undefined, direction?: string | undefined, limit?: number | undefined}; export type GetApiV1StocksTickerCorrelatedOptions = {baseUrl?: string | undefined, ApiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const correlatedStockDto = z.object({ticker: z.string().nullable().optional(), name: z.string().nullable().optional(), industry: z.string().nullable().optional(), sector: z.string().nullable().optional(), correlation: z.number().optional(), observations: z.number().int().optional(), marketCap: z.number().optional()}); export const correlatedStocksResponse = z.object({ticker: z.string().nullable().optional(), companyName: z.string().nullable().optional(), scope: z.string().nullable().optional(), direction: z.string().nullable().optional(), windowDays: z.number().int().optional(), method: z.string().nullable().optional(), universeSize: z.number().int().optional(), subjectObservations: z.number().int().optional(), note: z.string().nullable().optional(), data: z.array(correlatedStockDto).nullable().optional()}); export const validateResponse = async ( schema: { readonly '~standard': { readonly validate: (value: unknown) => ({ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }) | Promise<{ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }> } }, value: unknown ): Promise => { const result = await schema['~standard'].validate(value) if (result.issues !== undefined) { const detail = result.issues .map(({ message, path }) => path?.length ? `${path.map((segment) => String(typeof segment === 'object' ? segment.key : segment)).join('.')}: ${message}` : message ) .join('; ') throw new Error(`Response does not match the schema: ${detail}`, { cause: { issues: result.issues } }) } return result.value }; /** * Stocks whose daily price returns are most (or least) correlated with one stock — * Pearson correlation of daily log returns on raw closes inside each exact listing's * comparable post-split interval, computed over the trading days both stocks priced. * Dividends are excluded; correlations are never computed on price levels. * Candidates need a $100M market cap and enough overlapping trading days; each row * reports the observation count behind its coefficient. * * GET /v1/stocks/{ticker}/correlated */ export const getApiV1StocksTickerCorrelated = async (args: GetApiV1StocksTickerCorrelatedArgs, options?: GetApiV1StocksTickerCorrelatedOptions) => { const {baseUrl = "https://api.equibles.com", fetch: fetchFn = fetch, ApiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.scope != null) query.append("scope", String(args.scope)) if (args.days != null) query.append("days", String(args.days)) if (args.direction != null) query.append("direction", String(args.direction)) if (args.limit != null) query.append("limit", String(args.limit)) if (ApiKey !== undefined) headers.set('authorization', `Bearer ${ApiKey}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/stocks/${encodeURIComponent(String(args.ticker))}/correlated${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(correlatedStocksResponse, await res.json()) };