import {z} from 'zod' export type GetApiV1SecuritiesTickerOptionsContractTradesArgs = {ticker: string, symbol?: string | undefined, startDate?: string | undefined, endDate?: string | undefined, endTime?: string | undefined, limit?: number | undefined}; export type GetApiV1SecuritiesTickerOptionsContractTradesOptions = {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 optionTradeDto = z.object({time: z.string().optional(), price: z.number().nullable().optional(), size: z.number().int().nullable().optional(), exchange: z.string().nullable().optional(), condition: z.string().nullable().optional(), cancelled: z.boolean().nullable().optional()}); export const optionTradesResponse = z.object({ticker: 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(), trades: z.array(optionTradeDto).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 }; /** * The trade tape for one option contract by its OCC symbol, active or expired, newest first: time, price, size, exchange and condition. A cancelled print stays in the tape with `cancelled: true`, so read volume from the bars rather than a sum of prints. Defaults to the last 4 days; `startDate` and `endDate` set another window, and history begins in early 2024. When `truncated` is true, pass the oldest row's `time` as `endTime` to continue. Plus receives trades 15 minutes after they print. The Free plan covers end-of-day data only and receives 403. US listings only. * * GET /v1/securities/{ticker}/options/contract/trades */ export const getApiV1SecuritiesTickerOptionsContractTrades = async (args: GetApiV1SecuritiesTickerOptionsContractTradesArgs, options?: GetApiV1SecuritiesTickerOptionsContractTradesOptions) => { 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.startDate != null) query.append("startDate", String(args.startDate)) if (args.endDate != null) query.append("endDate", String(args.endDate)) if (args.endTime != null) query.append("endTime", String(args.endTime)) 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/trades${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(optionTradesResponse, await res.json()) };