import {z} from 'zod' export type GetApiV1SecuritiesTickerOptionsContractBarsArgs = {ticker: string, symbol?: string | undefined, timeframe?: string | undefined, startDate?: string | undefined, endDate?: string | undefined, limit?: number | undefined}; export type GetApiV1SecuritiesTickerOptionsContractBarsOptions = {baseUrl?: string | undefined, ApiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const optionContractReferenceDto = z.object({occTicker: z.string().nullable().optional(), contractType: z.string().nullable().optional(), exerciseStyle: z.string().nullable().optional(), strikePrice: z.number().optional(), expirationDate: z.string().optional(), sharesPerContract: z.number().int().optional(), active: z.boolean().optional(), openInterest: z.number().int().nullable().optional(), openInterestDate: z.string().nullable().optional(), lastClose: z.number().nullable().optional(), lastCloseDate: z.string().nullable().optional()}); export const optionBarDto = z.object({time: z.string().optional(), session: z.string().optional(), open: z.number().nullable().optional(), high: z.number().nullable().optional(), low: z.number().nullable().optional(), close: z.number().nullable().optional(), volume: z.number().int().nullable().optional(), vwap: z.number().nullable().optional(), tradeCount: z.number().int().nullable().optional(), final: z.boolean().optional()}); export const optionBarsResponse = z.object({ticker: z.string().nullable().optional(), timeframe: z.string().nullable().optional(), startDate: z.string().optional(), endDate: z.string().optional(), returned: z.number().int().optional(), truncated: z.boolean().optional(), delayed: z.boolean().optional(), contract: optionContractReferenceDto.optional(), bars: z.array(optionBarDto).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 }; /** * Past bars for one option contract by its OCC symbol, active or expired, newest first: open, high, low, close, volume, VWAP and trade count. A period without trades has no bar, so a gap means no trades. `timeframe` is day (default, last 90 days), hour (last 7 days) or minute (last 4 days); `startDate` and `endDate` set another window. History begins in early 2024. Plus receives bars 15 minutes after they end; Pro receives them as they form, with `final: false` on a bar read before its period ended. The Free plan covers end-of-day data only and receives 403. Open interest, greeks and bid/ask have no history here; implied volatility history is `/options/volatility`. US listings only. * * GET /v1/securities/{ticker}/options/contract/bars */ export const getApiV1SecuritiesTickerOptionsContractBars = async (args: GetApiV1SecuritiesTickerOptionsContractBarsArgs, options?: GetApiV1SecuritiesTickerOptionsContractBarsOptions) => { 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.symbol != null) query.append("symbol", String(args.symbol)) if (args.timeframe != null) query.append("timeframe", String(args.timeframe)) if (args.startDate != null) query.append("startDate", String(args.startDate)) if (args.endDate != null) query.append("endDate", String(args.endDate)) 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))}/options/contract/bars${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(optionBarsResponse, await res.json()) };