From ce3a431248841f724536562307ae82331ba7891d Mon Sep 17 00:00:00 2001 From: 117 <16513382+117@users.noreply.github.com> Date: Tue, 26 Mar 2024 20:03:23 -0600 Subject: [PATCH] Re-structure --- src/createClient.ts | 26 +- src/rest.ts | 1238 ++++++++++++++++++++++++++++++++++++++----- src/websocket.ts | 2 +- 3 files changed, 1129 insertions(+), 137 deletions(-) diff --git a/src/createClient.ts b/src/createClient.ts index dc471d1..97c83a7 100644 --- a/src/createClient.ts +++ b/src/createClient.ts @@ -1,10 +1,6 @@ -import marketData from "../api/marketData.ts"; -import trade from "../api/trading.ts"; +import { data, trading } from "./rest.ts"; -import { - TokenBucketOptions, - createTokenBucket, -} from "../factory/createTokenBucket.ts"; +import { TokenBucketOptions, createTokenBucket } from "./createTokenBucket.ts"; export type RequestOptions = { method?: string; @@ -16,8 +12,8 @@ export type RequestOptions = { }; export type CreateClientOptions = { - keyId?: string; - secretKey?: string; + key?: string; + secret?: string; baseURL?: string; accessToken?: string; tokenBucket?: TokenBucketOptions; @@ -35,8 +31,8 @@ export type ClientWithContext = _context: ClientContext; }; -export type Trade = ReturnType; -export type MarketData = ReturnType; +export type Trade = ReturnType; +export type MarketData = ReturnType; // Infer the client type based on the base URL export type ClientFactoryMap = { @@ -80,9 +76,9 @@ export function createClient( // Construct the headers const headers = new Headers({ - "APCA-API-KEY-ID": options.keyId || Deno.env.get("APCA_KEY_ID") || "", + "APCA-API-KEY-ID": options.key || Deno.env.get("APCA_KEY_ID") || "", "APCA-API-SECRET-KEY": - options.secretKey || Deno.env.get("APCA_KEY_SECRET") || "", + options.secret || Deno.env.get("APCA_KEY_SECRET") || "", "Content-Type": "application/json", }); @@ -127,9 +123,9 @@ export function createClient( let client: ClientFactoryMap[T]; if (options.baseURL === "https://paper-api.alpaca.markets") { - client = trade(context) as ClientFactoryMap[T]; + client = trading(context) as ClientFactoryMap[T]; } else if (options.baseURL === "https://data.alpaca.markets") { - client = marketData(context) as ClientFactoryMap[T]; + client = data(context) as ClientFactoryMap[T]; } else { throw new Error("invalid base URL"); } @@ -139,3 +135,5 @@ export function createClient( return factory(context); } + +// client..(options) diff --git a/src/rest.ts b/src/rest.ts index 82e8bdf..e101ec8 100644 --- a/src/rest.ts +++ b/src/rest.ts @@ -1,4 +1,4 @@ -import { ClientContext } from "../factory/createClient.ts"; +import { ClientContext } from "./createClient.ts"; export interface CorporateActionAnnouncement { id: string; @@ -25,6 +25,7 @@ export interface AnnouncementsQueryParams { symbol?: string; cusip?: string; date_type?: string; + announcement_id?: string; } export interface CryptoFundingWallet { @@ -43,7 +44,7 @@ export interface WhitelistedAddress { asset: string; address: string; status: "ACTIVE" | "PENDING"; - created_at: string; // Timestamp (RFC3339) of account creation + created_at: string; } export interface CryptoFundingTransfer { @@ -145,6 +146,7 @@ export interface OptionContractsQueryParams { strike_price_lte?: number; page_token?: string; limit?: number; + symbol_or_id?: string; } export interface OptionContract { @@ -244,42 +246,6 @@ export type Asset = { attributes: string[]; }; -export type Account = { - id: string; - account_number: string; - status: string; - currency: string; - cash: string; - portfolio_value: string; - non_marginable_buying_power: string; - accrued_fees: string; - pending_transfer_in: string; - pending_transfer_out: string; - pattern_day_trader: boolean; - trade_suspended_by_user: boolean; - trading_blocked: boolean; - transfers_blocked: boolean; - account_blocked: boolean; - created_at: string; - shorting_enabled: boolean; - long_market_value: string; - short_market_value: string; - equity: string; - last_equity: string; - multiplier: string; - buying_power: string; - initial_margin: string; - maintenance_margin: string; - sma: string; - daytrade_count: number; - last_maintenance_margin: string; - daytrading_buying_power: string; - regt_buying_power: string; - options_buying_power: string; - options_approved_level: number; - options_trading_level: number; -}; - export interface PortfolioHistoryOptions { period?: string; timeframe?: string; @@ -507,7 +473,7 @@ export interface Watchlist { } export type GetWatchlistOptions = { - watchlist_id: string; + watchlist_id?: string; }; export type DeleteWatchlistOptions = { @@ -527,16 +493,655 @@ export interface MarketCalendar { settlement_date: string; } -export default ({ request }: ClientContext) => ({ - v2: () => ({ +export type GetAccountActivitiesByTypeOptions = { + type: string; +}; + +type GetAssetOptions = { + symbol_or_asset_id?: string; +}; + +type GetAssetsOptions = { + status?: string; + asset_class?: string; + exchange?: string; + attributes?: string[]; +}; + +export interface CorporateActionsQueryParams { + symbols: string; + types?: string; + start?: string; + end?: string; + limit?: number; + page_token?: string; + sort?: "asc" | "desc"; +} + +export interface CorporateActionsResponse { + corporate_actions: { + reverse_splits?: ReverseSplit[]; + forward_splits?: ForwardSplit[]; + unit_splits?: UnitSplit[]; + stock_dividends?: StockDividend[]; + cash_dividends?: CashDividend[]; + spin_offs?: SpinOff[]; + cash_mergers?: CashMerger[]; + stock_mergers?: StockMerger[]; + stock_and_cash_mergers?: StockAndCashMerger[]; + redemptions?: Redemption[]; + name_changes?: NameChange[]; + worthless_removals?: WorthlessRemoval[]; + next_page_token: string | null; + }; +} + +export interface ReverseSplit { + symbol: string; + new_rate: number; + old_rate: number; + process_date: string; + ex_date: string; + record_date: string; + payable_date?: string; +} + +export interface ForwardSplit { + symbol: string; + new_rate: number; + old_rate: number; + process_date: string; + ex_date: string; + record_date: string; + payable_date?: string; + due_bill_redemption_date?: string; +} + +export interface UnitSplit { + old_symbol: string; + old_rate: number; + new_symbol: string; + new_rate: number; + alternate_symbol: string; + alternate_rate: number; + process_date: string; + effective_date: string; + payable_date?: string; +} + +export interface StockDividend { + symbol: string; + rate: number; + process_date: string; + ex_date: string; + record_date: string; + payable_date?: string; +} + +export interface CashDividend { + symbol: string; + rate: number; + special: boolean; + foreign: boolean; + process_date: string; + ex_date: string; + record_date: string; + payable_date?: string; + due_bill_on_date?: string; + due_bill_off_date?: string; +} + +export interface SpinOff { + source_symbol: string; + source_rate: number; + new_symbol: string; + new_rate: number; + process_date: string; + ex_date: string; + record_date: string; + payable_date?: string; + due_bill_redemption_date?: string; +} + +export interface CashMerger { + acquirer_symbol: string; + acquiree_symbol: string; + rate: number; + process_date: string; + effective_date: string; + payable_date?: string; +} + +export interface StockMerger { + acquirer_symbol: string; + acquirer_rate: number; + acquiree_symbol: string; + acquiree_rate: number; + process_date: string; + effective_date: string; + payable_date?: string; +} + +export interface StockAndCashMerger { + acquirer_symbol: string; + acquirer_rate: number; + acquiree_symbol: string; + acquiree_rate: number; + cash_rate: number; + process_date: string; + effective_date: string; + payable_date?: string; +} + +export interface Redemption { + symbol: string; + rate: number; + process_date: string; + payable_date?: string; +} + +export interface NameChange { + old_symbol: string; + new_symbol: string; + process_date: string; +} + +export interface WorthlessRemoval { + symbol: string; + process_date: string; +} + +export interface LatestForexRatesResponse { + rates: { + [currencyPair: string]: ForexRate; + }; +} + +export interface ForexRate { + bp: number; // Bid price + mp: number; // Mid price + ap: number; // Ask price + t: string; // Timestamp +} + +export interface HistoricalForexRatesResponse { + next_page_token: string; + rates: { + [currencyPair: string]: HistoricalForexRate[]; + }; +} + +export interface HistoricalForexRate { + bp: number; // Bid price + mp: number; // Mid price + ap: number; // Ask price + t: string; // Timestamp +} +export interface HistoricalBarsResponse { + bars: { [symbol: string]: CryptoBar[] }; + next_page_token: string | null; +} + +export interface CryptoBar { + t: string; // Timestamp + o: number; // Open price + h: number; // High price + l: number; // Low price + c: number; // Close price + v: number; // Volume + n: number; // Trade count + vw: number; // Volume weighted average price +} + +export interface LatestBarsResponse { + bars: { [symbol: string]: CryptoBar }; +} + +export interface LatestOrderbooksResponse { + orderbooks: { [symbol: string]: CryptoOrderbook }; +} + +export interface CryptoOrderbook { + t: string; // Timestamp + b: OrderbookEntry[]; // Bid orderbook entries + a: OrderbookEntry[]; // Ask orderbook entries +} + +export interface OrderbookEntry { + p: number; // Price + s: number; // Size +} + +export interface LatestQuotesResponse { + quotes: { [symbol: string]: CryptoQuote }; +} + +export interface CryptoQuote { + t: string; // Timestamp + bp: number; // Bid price + bs: number; // Bid size + ap: number; // Ask price + as: number; // Ask size +} + +export interface LatestTradesResponse { + trades: { [symbol: string]: CryptoTrade }; +} + +export interface CryptoTrade { + t: string; // Timestamp + p: number; // Price + s: number; // Size + tks: string; // Trade side (S: Sell, B: Buy) + i: number; // Trade ID +} + +export interface HistoricalQuotesResponse { + quotes: { [symbol: string]: CryptoQuote[] }; + next_page_token: string | null; +} + +export interface CryptoSnapshotsResponse { + snapshots: { [symbol: string]: CryptoSnapshot }; +} + +interface CryptoSnapshot { + dailyBar: BarData; + latestQuote: QuoteData; + latestTrade: TradeData; + minuteBar: BarData; + prevDailyBar: BarData; +} + +interface BarData { + t: string; // Timestamp + o: number; // Open price + h: number; // High price + l: number; // Low price + c: number; // Close price + v: number; // Volume + vw: number; // Volume weighted average price + n: number; // Number of trades +} + +interface QuoteData { + t: string; // Timestamp + bp: number; // Bid price + bs: number; // Bid size + ap: number; // Ask price + as: number; // Ask size +} + +interface TradeData { + t: string; // Timestamp + p: number; // Trade price + s: number; // Trade size + tks: string; // Trade side (Buy or Sell) +} + +export interface CryptoHistoricalTradesResponse { + trades: { [symbol: string]: CryptoTrade[] }; + next_page_token: string | null; +} +export interface NewsResponse { + news: NewsArticle[]; + next_page_token: string | null; +} + +export interface NewsArticle { + id: number; + headline: string; + author: string; + created_at: string; + updated_at: string; + summary: string; + content: string; + url: string | null; + images: NewsImage[]; + symbols: string[]; + source: string; +} + +export interface NewsImage { + size: "thumb" | "small" | "large"; + url: string; +} +export interface HistoricalOptionBarsResponse { + bars: OptionBar[]; + next_page_token: string | null; +} + +export interface OptionBar { + t: string; // Timestamp in RFC-3339 format + o: number; // Opening price + h: number; // High price + l: number; // Low price + c: number; // Closing price + v: number; // Bar volume + n: number; // Trade count in the bar + vw: number; // Volume weighted average price +} + +export interface OptionExchangeMapping { + [exchangeCode: string]: string; +} + +export interface LatestQuotesResponse { + // quotes: { + // [symbol: string]: { + // t: string; // Timestamp in RFC-3339 format + // ax: string; // Ask exchange + // ap: number; // Ask price + // as: number; // Ask size + // bx: string; // Bid exchange + // bp: number; // Bid price + // bs: number; // Bid size + // c: string; // Condition + // }; + // }; +} + +export interface LatestTradesResponse { + // trades: { + // [symbol: string]: { + // t: string; // Timestamp in RFC-3339 format + // x: string; // Exchange code + // p: number; // Trade price + // s: number; // Trade size + // c: string; // Condition + // }; + // }; +} + +export interface HistoricalTradesResponse { + trades: { + [symbol: string]: { + t: string; // Timestamp in RFC-3339 format + x: string; // Exchange code + p: number; // Trade price + s: number; // Trade size + c: string; // Condition + }[]; + }; + next_page_token: string | null; +} + +export interface SnapshotsResponse { + snapshots: { + [symbol: string]: { + latest_trade: TradeSnapshot; + latest_quote: QuoteSnapshot; + }; + }; +} + +interface TradeSnapshot { + t: string; // Timestamp in RFC-3339 format + x: string; // Exchange code + p: number; // Trade price + s: number; // Trade size + c: string; // Condition +} + +interface QuoteSnapshot { + t: string; // Timestamp in RFC-3339 format + ax: string; // Ask exchange code + ap: number; // Ask price + as: number; // Ask size + bx: string; // Bid exchange code + bp: number; // Bid price + bs: number; // Bid size + c: string; // Condition +} +export interface MostActivesResponse { + most_actives: MostActiveStock[]; + last_updated: string; +} + +export interface MostActiveStock { + symbol: string; + volume: number; + trade_count: number; +} + +export interface MarketMoversResponse { + gainers: MarketMover[]; + losers: MarketMover[]; + market_type: string; + last_updated: string; +} + +export interface MarketMover { + symbol: string; + percent_change: number; + change: number; + price: number; +} +export interface HistoricalAuctionsResponse { + auctions: { [symbol: string]: AuctionData[] }; + next_page_token: string | null; +} + +interface AuctionData { + d: string; // Date + o: AuctionPrice[]; // Opening auction prices + c: AuctionPrice[]; // Closing auction prices +} + +interface AuctionPrice { + c: string; // Condition + p: number; // Price + t: string; // Timestamp + x: string; // Auction type +} + +export interface HistoricalBarsResponse { + bars: { [symbol: string]: Bar[] }; + next_page_token: string | null; +} + +interface Bar { + t: string; // Timestamp + o: number; // Open + h: number; // High + l: number; // Low + c: number; // Close + v: number; // Volume + n: number; // Number of trades + vw: number; // Volume weighted average price +} + +export interface LatestBarsResponse { + bars: { [symbol: string]: Bar }; +} + +interface Bar { + t: string; // Timestamp + o: number; // Open + h: number; // High + l: number; // Low + c: number; // Close + v: number; // Volume + n: number; // Number of trades + vw: number; // Volume weighted average price +} + +export interface ConditionCodesResponse { + [code: string]: string; +} + +export interface ExchangeCodesResponse { + [code: string]: string; +} + +export interface HistoricalQuotesParams { + symbols: string; + start?: string; + end?: string; + limit?: number; + asof?: string; + feed?: "sip" | "iex" | "otc"; + sip?: string; + page_token?: string; + sort?: "asc" | "desc"; +} + +export interface LatestQuotesParams { + symbols: string; + feed?: "sip" | "iex" | "otc"; + sip?: string; +} + +export interface LatestQuotesResponse { + // quotes: { + // [symbol: string]: LatestQuote; + // }; +} + +export interface LatestQuote { + t: string; // Timestamp + ax: string; // Ask Exchange + ap: number; // Ask Price + as: number; // Ask Size + bx: string; // Bid Exchange + bp: number; // Bid Price + bs: number; // Bid Size + c: string[]; // Conditions + z: string; // Tape +} + +export interface SnapshotParams { + symbols: string; + feed?: "sip" | "iex" | "otc"; + sip?: string; +} + +export interface SnapshotResponse { + snapshots: { + [symbol: string]: SnapshotData; + }; +} + +export interface SnapshotData { + latestTrade: { + t: string; // Timestamp + x: string; // Exchange + p: number; // Price + s: number; // Size + c: string[]; // Conditions + i: number; // Trade ID + z: string; // Tape + }; + latestQuote: { + t: string; // Timestamp + ax: string; // Ask Exchange + ap: number; // Ask Price + as: number; // Ask Size + bx: string; // Bid Exchange + bp: number; // Bid Price + bs: number; // Bid Size + c: string[]; // Conditions + z: string; // Tape + }; + minuteBar: { + t: string; // Timestamp + o: number; // Open + h: number; // High + l: number; // Low + c: number; // Close + v: number; // Volume + n: number; // Number of Trades + vw: number; // Volume Weighted Average Price + }; + dailyBar: { + t: string; // Timestamp + o: number; // Open + h: number; // High + l: number; // Low + c: number; // Close + v: number; // Volume + n: number; // Number of Trades + vw: number; // Volume Weighted Average Price + }; + prevDailyBar: { + t: string; // Timestamp + o: number; // Open + h: number; // High + l: number; // Low + c: number; // Close + v: number; // Volume + n: number; // Number of Trades + vw: number; // Volume Weighted Average Price + }; +} + +export interface TradeParams { + symbols: string; + start?: string; // RFC-3339 or YYYY-MM-DD format + end?: string; // RFC-3339 or YYYY-MM-DD format + limit?: number; + asof?: string; // YYYY-MM-DD format + feed?: "sip" | "iex" | "otc"; + sip?: string; // ISO 4217 format + page_token?: string; + sort?: "asc" | "desc"; +} + +export interface TradeResponse { + trades: { + [symbol: string]: Trade[]; + }; + next_page_token?: string; +} + +export interface Trade { + t: string; // Timestamp + x: string; // Exchange + p: number; // Price + s: number; // Size + c: string[]; // Conditions + i: number; // Trade ID + z: string; // Tape +} + +export interface LatestTradeParams { + symbols: string; + feed?: "sip" | "iex" | "otc"; + sip?: string; // ISO 4217 format +} + +export interface LatestTradeResponse { + trades: { + [symbol: string]: Trade; + }; +} + +export interface HistoricalAuctionsParams { + symbol?: string; + symbols?: string; + start?: string; + end?: string; + limit?: number; + asof?: string; + feed?: string; + page_token?: string; + sort?: string; +} + +export type MarketDataEventMap = any; + +// client..(options) + +export const trading = ({ request }: ClientContext) => ({ + v2: { account: { get: () => - request({ + request({ path: "/v2/account", }), portfolio: { history: { - getPortfolioHistory: (options: PortfolioHistoryOptions = {}) => + get: (options: PortfolioHistoryOptions = {}) => request({ path: "/v2/account/portfolio/history", method: "GET", @@ -568,11 +1173,9 @@ export default ({ request }: ClientContext) => ({ }, }, orders: { - get: (options: T = {} as T) => - request>({ - path: options?.order_id - ? `/v2/orders/${options.order_id}` - : "/v2/orders", + get: ({ order_id, ...options }: GetOrdersOptions = {}) => + request>({ + path: order_id ? `/v2/orders/${order_id}` : "/v2/orders", method: "GET", params: options, }), @@ -588,37 +1191,28 @@ export default ({ request }: ClientContext) => ({ method: "PATCH", data: options, }), - delete: (options: CancelOrdersOptions = {}) => + delete: ({ order_id }: CancelOrdersOptions) => request({ - path: options?.order_id - ? `/v2/orders/${options.order_id}` - : "/v2/orders", + path: order_id ? `/v2/orders/${order_id}` : "/v2/orders", method: "DELETE", }), }, positions: { - get: (options: T = {} as T) => + get: ({ symbol, asset_id }: T) => request>({ path: - options?.symbol || options?.asset_id - ? `/v2/positions/${options.symbol || options.asset_id}` + symbol || asset_id + ? `/v2/positions/${symbol || asset_id}` : "/v2/positions", method: "GET", }), - delete: (params?: T) => - request< - T extends { symbol?: string; asset_id?: string } ? Order : Order[] - >({ - path: - params?.symbol || params?.asset_id - ? `/v2/positions/${params.symbol || params.asset_id}` - : "/v2/positions", + delete: ({ symbol_or_asset_id, ...options }: DeletePositionOptions) => + request({ + path: symbol_or_asset_id + ? `/v2/positions/${symbol_or_asset_id}` + : "/v2/positions", method: "DELETE", - params: { - qty: params?.qty, - percentage: params?.percentage, - cancel_orders: params?.cancel_orders, - }, + params: options, }), exercise: { post: (options: PostExerciseOptions) => @@ -631,10 +1225,10 @@ export default ({ request }: ClientContext) => ({ }, }, watchlists: { - get: (options = {} as GetWatchlistOptions) => + get: ({ watchlist_id }: GetWatchlistOptions = {}) => request({ - path: options?.watchlist_id - ? `/v2/watchlists/${options.watchlist_id}` + path: watchlist_id + ? `/v2/watchlists/${watchlist_id}` : "/v2/watchlists", }), post: (options: CreateWatchlistParams) => @@ -667,77 +1261,48 @@ export default ({ request }: ClientContext) => ({ get: () => request({ path: "/v2/clock", - method: "GET", }), }, - // --------------- OK ABOVE THIS LINE --------------- // assets: { - get: (symbolOrAssetId: string) => + get: ({ symbol_or_asset_id, ...options }: GetAssetOptions = {}) => request({ - path: `/v2/assets/${symbolOrAssetId}`, + path: symbol_or_asset_id + ? `/v2/assets/${symbol_or_asset_id}` + : "/v2/assets", method: "GET", + params: options, }), - // getAssets: (options?: { - // status?: string; - // asset_class?: string; - // exchange?: string; - // attributes?: string[]; - // }) => - // request({ - // path: "/v2/assets", - // method: "GET", - // params: Object.fromEntries( - // Object.entries({ - // status: options?.status, - // asset_class: options?.asset_class, - // exchange: options?.exchange, - // attributes: options?.attributes?.join(","), - // }).filter(([_, value]) => value !== undefined) - // ), - // }), }, options: { contracts: { - get: (queryParams: OptionContractsQueryParams) => + get: ({ symbol_or_id, ...options }: OptionContractsQueryParams) => request({ - path: "/v2/options/contracts", + path: symbol_or_id + ? `/v2/options/contracts/${symbol_or_id}` + : "/v2/options/contracts", method: "GET", - params: queryParams, + params: options, }), - // getOptionsContract: ( - // symbolOrId: string, - // queryParams: OptionContractsQueryParams - // ) => - // request({ - // path: `/v2/options/contracts/${symbolOrId}`, - // method: "GET", - // params: queryParams, - // }), }, }, corporate_actions: { announcements: { - get: (announcementId?: string) => - request({ - path: `/v2/corporate_actions/announcements/${announcementId}`, + get: ({ announcement_id, ...queryParams }: AnnouncementsQueryParams) => + request({ + path: announcement_id + ? `/v2/corporate_actions/announcements/${announcement_id}` + : "/v2/corporate_actions/announcements", method: "GET", + params: queryParams, }), - // getCorporateActionAnnouncements: ( - // queryParams: AnnouncementsQueryParams - // ) => - // request({ - // path: "/v2/corporate_actions/announcements", - // method: "GET", - // params: queryParams, - // }), }, }, wallets: { - get: (asset?: string) => + get: (options: { asset: string }) => request({ path: "/v2/wallets", method: "GET", - params: { asset }, + params: options, }), whitelists: { get: () => @@ -768,10 +1333,13 @@ export default ({ request }: ClientContext) => ({ }, }, transfers: { - get: () => - request({ - path: "/v2/wallets/transfers", + get: (options: { asset?: string; transfer_id?: string }) => + request({ + path: options.transfer_id + ? `/v2/wallets/transfers/${options.transfer_id}` + : "/v2/wallets/transfers", method: "GET", + params: options, }), post: (withdrawalParams: WithdrawalParams) => request({ @@ -781,5 +1349,431 @@ export default ({ request }: ClientContext) => ({ }), }, }, - }), + }, +}); + +export const data = ({ request }: ClientContext) => ({ + getCorporateActions: (queryParams: CorporateActionsQueryParams) => + request({ + path: "/v1beta1/corporate-actions", + method: "GET", + params: queryParams, + }), + getLatestForexRates: (currencyPairs: string) => + request({ + path: "/v1beta1/forex/latest/rates", + method: "GET", + params: { currency_pairs: currencyPairs }, + }), + getHistoricalForexRates: ( + currencyPairs: string, + timeframe: string, + start: string, + end: string, + limit: number, + pageToken: string + ) => + request({ + path: "/v1beta1/forex/rates", + method: "GET", + params: { + currency_pairs: currencyPairs, + timeframe: timeframe, + start: start, + end: end, + limit: limit, + page_token: pageToken, + }, + }), + getLogos: (symbols: string, placeholder?: boolean) => + request({ + path: "/v1beta1/logos", + method: "GET", + params: { symbols, placeholder }, + }), +}); + +export const meow = ({ request }: ClientContext) => ({ + v1beta1: { + news: { + get: ( + start?: string, + end?: string, + sort?: string, + symbols?: string, + limit?: number, + includeContent?: boolean, + excludeContentless?: boolean, + pageToken?: string + ) => + request({ + path: "/v1beta1/news", + method: "GET", + params: { + start: start, + end: end, + sort: sort, + symbols: symbols, + limit: limit, + include_content: includeContent, + exclude_contentless: excludeContentless, + page_token: pageToken, + }, + }), + }, + options: { + bars: { + get: ( + symbols: string, + timeframe: string, + start?: string, + end?: string, + limit?: number, + pageToken?: string, + sort?: string + ) => + request({ + path: "/v1beta1/options/bars", + method: "GET", + params: { + symbols: symbols, + timeframe: timeframe, + start: start, + end: end, + limit: limit, + page_token: pageToken, + sort: sort, + }, + }), + }, + meta: { + exchanges: { + get: () => + request({ + path: "/v1beta1/options/meta/exchanges", + method: "GET", + }), + }, + }, + quotes: { + latest: { + get: (symbols: string, feed?: string) => + request({ + path: "/v1beta1/options/quotes/latest", + method: "GET", + params: { symbols, feed }, + }), + }, + }, + trades: { + latest: { + get: (symbols: string, feed?: string) => + request({ + path: "/v1beta1/options/trades/latest", + method: "GET", + params: { symbols, feed }, + }), + }, + get: ( + symbols: string, + start?: string, + end?: string, + limit?: number, + pageToken?: string, + sort?: string + ) => + request({ + path: "/v1beta1/options/trades", + method: "GET", + params: { symbols, start, end, limit, pageToken, sort }, + }), + }, + snapshots: { + get: (symbols: string, feed?: string) => + request({ + path: "/v1beta1/options/snapshots", + method: "GET", + params: { symbols, feed }, + }), + // snapshotsByUnderlying: (underlyingSymbol: string, feed?: string) => + // // uhh, not sure yet how to handle this + // request({ + // path: `/v1beta1/options/snapshots/${underlyingSymbol}`, + // method: "GET", + // params: { feed }, + // }), + }, + }, + screener: { + most_actives: { + get: (by = "volume", top = 10) => + request({ + path: "/v1beta1/screener/stocks/most-actives", + method: "GET", + params: { by, top }, + }), + }, + movers: { + get: (marketType: string, top: number = 10) => + request({ + path: `/v1beta1/screener/${marketType}/movers`, + method: "GET", + params: { top }, + }), + }, + }, + }, + v1beta3: { + crypto: { + bars: { + get: ( + loc: string, + symbols: string, + timeframe: string, + start?: string, + end?: string, + limit: number = 1000, + page_token?: string, + sort?: string + ) => + request({ + path: `/v1beta3/crypto/${loc}/bars`, + method: "GET", + params: { + symbols, + timeframe, + start, + end, + limit, + page_token, + sort, + }, + }), + }, + latest: { + bars: { + get: (loc: string, symbols: string) => + request({ + path: `/v1beta3/crypto/${loc}/latest/bars`, + method: "GET", + params: { + symbols, + }, + }), + }, + orderbooks: { + get: (loc: string, symbols: string) => + request({ + path: `/v1beta3/crypto/${loc}/latest/orderbooks`, + method: "GET", + params: { + symbols, + }, + }), + }, + quotes: { + get: (loc: string, symbols: string) => + request({ + path: `/v1beta3/crypto/${loc}/latest/quotes`, + method: "GET", + params: { + symbols, + }, + }), + }, + trades: { + get: (loc: string, symbols: string) => + request({ + path: `/v1beta3/crypto/${loc}/latest/trades`, + method: "GET", + params: { + symbols, + }, + }), + }, + }, + quotes: { + get: ( + loc: string, + symbols: string, + start?: string, + end?: string, + limit?: number, + pageToken?: string, + sort?: string + ) => + request({ + path: `/v1beta3/crypto/${loc}/quotes`, + method: "GET", + params: { + symbols, + start, + end, + limit, + page_token: pageToken, + sort, + }, + }), + }, + + snapshots: { + get: (loc: string, symbols: string) => + request({ + path: `/v1beta3/crypto/${loc}/snapshots`, + method: "GET", + params: { + symbols, + }, + }), + }, + trades: { + get: ( + loc: string, + symbols: string, + start?: string, + end?: string, + limit?: number, + page_token?: string, + sort?: string + ) => + request({ + path: `/v1beta3/crypto/${loc}/trades`, + method: "GET", + params: { + symbols, + start, + end, + limit, + page_token, + sort, + }, + }), + }, + }, + }, + v2: { + stocks: { + auctions: { + get: ( + symbols: string, + start?: string, + end?: string, + limit?: number, + asof?: string, + feed?: string, + page_token?: string, + sort?: string + ) => + request({ + path: `/v2/stocks/auctions`, + method: "GET", + params: { + symbols, + start, + end, + limit, + asof, + feed, + page_token, + sort, + }, + }), + }, + bars: { + get: ( + symbols: string, + timeframe: string, + start?: string, + end?: string, + limit?: number, + adjustment?: string, + asof?: string, + feed?: string, + page_token?: string, + sort?: string + ) => + request({ + path: `/v2/stocks/bars`, + method: "GET", + params: { + symbols, + timeframe, + start, + end, + limit, + adjustment, + asof, + feed, + page_token, + sort, + }, + }), + latest: { + get: (symbols: string, feed?: string, currency?: string) => + request({ + path: `/v2/stocks/bars/latest`, + method: "GET", + params: { symbols, feed, currency }, + }), + }, + }, + meta: { + conditions: { + get: (tickType: string, tape: string) => + request({ + path: `/v2/stocks/meta/conditions/${tickType}`, + method: "GET", + params: { tape }, + }), + }, + exchanges: { + get: () => + request({ + path: "/v2/stocks/meta/exchanges", + method: "GET", + }), + }, + quotes: { + get: (params: HistoricalQuotesParams) => + request({ + path: "/v2/stocks/quotes", + method: "GET", + params, + }), + latest: { + get: (params: LatestQuotesParams) => + request({ + path: "/v2/stocks/quotes/latest", + method: "GET", + params, + }), + }, + }, + }, + snapshots: { + get: (params: SnapshotParams) => + request({ + path: "/v2/stocks/snapshots", + method: "GET", + params, + }), + }, + trades: { + get: (params: TradeParams) => + request({ + path: "/v2/stocks/trades", + method: "GET", + params, + }), + latest: { + get: (params: LatestTradeParams) => + request({ + path: "/v2/stocks/trades/latest", + method: "GET", + params, + }), + }, + }, + }, + }, }); diff --git a/src/websocket.ts b/src/websocket.ts index 26b8119..7c1cf58 100644 --- a/src/websocket.ts +++ b/src/websocket.ts @@ -1,4 +1,4 @@ -import { ClientContext } from "../createClient.ts"; +import { ClientContext } from "./createClient.ts"; export interface CorporateActionsQueryParams { symbols: string;