import {z} from 'zod' export type GetApiV1SecuritiesTickerTechnicalsStochasticArgs = {ticker: string, startDate?: string | undefined, endDate?: string | undefined, kPeriod?: number | undefined, dPeriod?: number | undefined, limit?: number | undefined}; export type GetApiV1SecuritiesTickerTechnicalsStochasticOptions = {baseUrl?: string | undefined, ApiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const stochasticPointDto = z.object({date: z.string().optional(), close: z.number().optional(), percentK: z.number().nullable().optional(), percentD: z.number().nullable().optional()}); export const technicalsMeta = z.object({returned: z.number().int().optional(), windowPoints: z.number().int().optional(), truncated: z.boolean().optional()}); export const stochasticOscillatorResponse = z.object({ticker: z.string().nullable().optional(), companyName: z.string().nullable().optional(), exchange: z.string().nullable().optional(), currency: z.string().nullable().optional(), kPeriod: z.number().int().optional(), dPeriod: z.number().int().optional(), data: z.array(stochasticPointDto).nullable().optional(), meta: technicalsMeta.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 }; /** * Stochastic oscillator (%K and its %D smoothing) over a stock's daily bars, newest first. Extra look-back bars are fetched before the window so in-range points are fully computed; values are null only when the stock's history itself is shorter than the lookback. On the Free plan a session's bars are released 4 hours after the market close. * * GET /v1/securities/{ticker}/technicals/stochastic */ export const getApiV1SecuritiesTickerTechnicalsStochastic = async (args: GetApiV1SecuritiesTickerTechnicalsStochasticArgs, options?: GetApiV1SecuritiesTickerTechnicalsStochasticOptions) => { 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.startDate != null) query.append("startDate", String(args.startDate)) if (args.endDate != null) query.append("endDate", String(args.endDate)) if (args.kPeriod != null) query.append("kPeriod", String(args.kPeriod)) if (args.dPeriod != null) query.append("dPeriod", String(args.dPeriod)) if (args.limit != null) query.append("limit", String(args.limit)) if (ApiKey !== undefined) headers.set('authorization', `Bearer ${ApiKey}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/securities/${encodeURIComponent(String(args.ticker))}/technicals/stochastic${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(stochasticOscillatorResponse, await res.json()) };