From e5ef624528ba96baaedc9b9e8ce3c8c1bfe3fd4c Mon Sep 17 00:00:00 2001 From: 117 <16513382+117@users.noreply.github.com> Date: Sun, 24 Mar 2024 19:29:55 -0600 Subject: [PATCH] Refactor marketData.ts and remove unused code --- api/marketData.ts | 52 +-- api/temp.ts | 52 --- api/trade.ts | 611 ++++++++++++-------------- api/trading.ts | 1066 --------------------------------------------- 4 files changed, 306 insertions(+), 1475 deletions(-) delete mode 100644 api/temp.ts delete mode 100644 api/trading.ts diff --git a/api/marketData.ts b/api/marketData.ts index 2f85033..1f99c3b 100644 --- a/api/marketData.ts +++ b/api/marketData.ts @@ -1,4 +1,4 @@ -import { BaseAPI, ClientContext } from "../factory/createClient.ts"; +import { ClientContext } from "../factory/createClient.ts"; export interface CorporateActionsQueryParams { symbols: string; @@ -319,30 +319,30 @@ export interface OptionExchangeMapping { } 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 - }; - }; + // 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 - }; - }; + // 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 { @@ -485,9 +485,9 @@ export interface LatestQuotesParams { } export interface LatestQuotesResponse { - quotes: { - [symbol: string]: LatestQuote; - }; + // quotes: { + // [symbol: string]: LatestQuote; + // }; } export interface LatestQuote { @@ -622,7 +622,7 @@ export interface HistoricalAuctionsParams { export type MarketDataEventMap = any; -export default ({ request }: ClientContext): BaseAPI => ({ +export default ({ request }: ClientContext) => ({ rest: { v1beta1: { corporateActions: (queryParams: CorporateActionsQueryParams) => diff --git a/api/temp.ts b/api/temp.ts deleted file mode 100644 index f81e2ef..0000000 --- a/api/temp.ts +++ /dev/null @@ -1,52 +0,0 @@ -import { TradingAPI } from "./trading.ts"; - -type ClientOptions = { - apiKey: string; - apiSecret: string; - baseURL: string; -}; - -type Endpoint = keyof TradingAPI; -type Method = TradingAPI[Endpoint]["method"]; -type Params = TradingAPI[E]["params"]; -type Response = TradingAPI[E]["response"]; - -function createClient(options: ClientOptions) { - const call = async ( - method: Method, - endpoint: E, - params?: Params - ): Promise> => { - const url = new URL(`${options.baseURL}${endpoint}`); - if (params && method === "GET") { - Object.entries(params as Record).forEach( - ([key, value]) => { - url.searchParams.append(key, value); - } - ); - } - const response = await fetch(url.toString(), { - method, - headers: { - "APCA-API-KEY-ID": options.apiKey, - "APCA-API-SECRET-KEY": options.apiSecret, - "Content-Type": "application/json", - }, - body: method !== "GET" ? JSON.stringify(params) : undefined, - }); - - if (!response.ok) { - throw new Error(`HTTP error! status: ${response.status}`); - } - - return (await response.json()) as Response; - }; - - return { call }; -} - -const client = createClient({} as any); - -client.call("GET", "/v2/account/activities", {}).then((response) => { - console.log(response); -}); diff --git a/api/trade.ts b/api/trade.ts index b16a737..c7eddcc 100644 --- a/api/trade.ts +++ b/api/trade.ts @@ -1,4 +1,4 @@ -import { ClientContext, EventMap, WebSocket } from "../factory/createClient.ts"; +import { ClientContext } from "../factory/createClient.ts"; export type Account = { id: string; @@ -679,17 +679,6 @@ type StockDataMessage = | ErrorMessage | SubscriptionMessage; -interface StockChannelEventMap extends EventMap { - trades: { T: "trades"; data: Trade }; - quotes: { T: "quotes"; data: Quote }; - bars: { T: "bars"; data: Bar }; - dailyBars: { T: "dailyBars"; data: Bar }; - updatedBars: { T: "updatedBars"; data: Bar }; - statuses: { T: "statuses"; data: TradingStatus }; - lulds: { T: "lulds"; data: LULD }; -} - -export type StockDataWebSocket = WebSocket; interface BaseEvent { T: string; S: string; @@ -903,348 +892,308 @@ export type OptionsWebSocket = { type TradeEventMap = any; export default ({ request }: ClientContext) => ({ - rest: { - v2: { - account: { - get: () => - request({ - path: "/v2/account", - }), - }, - orders: { - post: (options: CreateOrderOptions) => - request({ - path: "/v2/orders", - method: "POST", - data: options, - }), - get: (options?: PatchOrderOptions | string, nested?: boolean) => { - let path = "/v2/orders"; - let params = {}; - - if (typeof options === "string") { - path += `/${options}`; - - if (nested !== undefined) { - params = { nested }; - } - } else if (typeof options === "object") { - params = options; - } - - return request({ - path, - method: "GET", - params, - }); - }, - delete: (orderId?: string) => { - let path = "/v2/orders"; + account: { + get: () => + request({ + path: "/v2/account", + }), + }, + orders: { + create: (options: CreateOrderOptions) => + request({ + path: "/v2/orders", + method: "POST", + data: options, + }), + update: (options?: PatchOrderOptions | string, nested?: boolean) => { + let path = "/v2/orders"; + let params = {}; + + if (typeof options === "string") { + path += `/${options}`; + + if (nested !== undefined) { + params = { nested }; + } + } else if (typeof options === "object") { + params = options; + } + + return request({ + path, + method: "PATCH", + params, + }); + }, + cancel: (orderId?: string) => { + let path = "/v2/orders"; - if (orderId) { - path += `/${orderId}`; - } + if (orderId) { + path += `/${orderId}`; + } - return request<{ id?: string; status?: string }[] | void>({ - path, - method: "DELETE", - }); + return request<{ id?: string; status?: string }[] | void>({ + path, + method: "DELETE", + }); + }, + }, + positions: { + get: (symbol_or_asset_id?: string) => + request({ + path: symbol_or_asset_id + ? `/v2/positions/${symbol_or_asset_id}` + : "/v2/positions", + method: "GET", + }), + close: (params: ClosePositionOptions) => + request({ + path: params.symbol_or_asset_id + ? `/v2/positions/${params.symbol_or_asset_id}` + : "/v2/positions", + method: "DELETE", + params: params.symbol_or_asset_id + ? { qty: params.qty, percentage: params.percentage } + : { cancel_orders: params.cancel_orders }, + }), + exercise: (symbol_or_contract_id: string) => + request({ + path: `/v2/positions/${symbol_or_contract_id}/exercise`, + method: "POST", + }), + }, + portfolioHistory: { + get: (params?: PortfolioHistoryParams) => + request({ + path: "/v2/account/portfolio/history", + method: "GET", + params, + }), + }, + watchlists: { + get: (watchlist_id?: string) => + request({ + path: watchlist_id + ? `/v2/watchlists/${watchlist_id}` + : "/v2/watchlists", + method: "GET", + }), + create: ({ name, symbols }: CreateWatchlistParams) => + request({ + path: "/v2/watchlists", + method: "POST", + data: { name, symbols }, + }), + update: ({ + watchlist_id, + name, + symbols, + }: { watchlist_id: string } & UpdateWatchlistParams) => + request({ + path: `/v2/watchlists/${watchlist_id}`, + method: "PUT", + data: { name, symbols }, + }), + delete: ({ watchlist_id }: { watchlist_id: string }) => + request({ + path: `/v2/watchlists/${watchlist_id}`, + method: "DELETE", + }), + addSymbol: ({ + watchlist_id, + symbol, + }: { + watchlist_id: string; + symbol: string; + }) => + request({ + path: `/v2/watchlists/${watchlist_id}`, + method: "POST", + data: { symbol }, + }), + removeSymbol: ({ watchlistId, symbol }: DeleteSymbolFromWatchlistParams) => + request({ + path: `/v2/watchlists/${watchlistId}/${symbol}`, + method: "DELETE", + }), + }, + configurations: { + get: () => + request({ + path: "/v2/account/configurations", + method: "GET", + }), + update: (updatedConfig: UpdatedAccountConfigurations) => + request({ + path: "/v2/account/configurations", + method: "PATCH", + data: updatedConfig, + }), + }, + activities: { + get: ( + activityType?: string, + options?: { + date?: string; + until?: string; + after?: string; + direction?: string; + pageSize?: number; + pageToken?: string; + category?: string; + } + ) => + request({ + path: `/v2/account/activities${activityType ? `/${activityType}` : ""}`, + method: "GET", + params: { + date: options?.date, + until: options?.until, + after: options?.after, + direction: options?.direction, + page_size: options?.pageSize, + page_token: options?.pageToken, + category: options?.category, }, - patch: (orderId: string, options: PatchOrderOptions) => - request({ - path: `/v2/orders/${orderId}`, - method: "PATCH", - data: options, - }), - }, - positions: { - get: (symbol_or_asset_id?: string) => - request({ - path: symbol_or_asset_id - ? `/v2/positions/${symbol_or_asset_id}` - : "/v2/positions", - method: "GET", - }), - delete: (params: ClosePositionOptions) => - request({ - path: params.symbol_or_asset_id - ? `/v2/positions/${params.symbol_or_asset_id}` - : "/v2/positions", - method: "DELETE", - params: params.symbol_or_asset_id - ? { qty: params.qty, percentage: params.percentage } - : { cancel_orders: params.cancel_orders }, - }), - post: (symbol_or_contract_id: string) => - request({ - path: `/v2/positions/${symbol_or_contract_id}/exercise`, - method: "POST", - }), - }, - portfolioHistory: { - get: (params?: PortfolioHistoryParams) => - request({ - path: "/v2/account/portfolio/history", - method: "GET", - params, - }), - }, - watchlists: { - get: (watchlist_id?: string) => - request({ - path: watchlist_id - ? `/v2/watchlists/${watchlist_id}` - : "/v2/watchlists", - method: "GET", - }), - post: ({ name, symbols }: CreateWatchlistParams) => - request({ - path: "/v2/watchlists", - method: "POST", - data: { name, symbols }, - }), - put: ({ - watchlist_id, - name, - symbols, - }: { watchlist_id: string } & UpdateWatchlistParams) => - request({ - path: `/v2/watchlists/${watchlist_id}`, - method: "PUT", - data: { name, symbols }, - }), - deleteById: ({ watchlist_id }: { watchlist_id: string }) => - request({ - path: `/v2/watchlists/${watchlist_id}`, - method: "DELETE", - }), - addAsset: ({ - watchlist_id, - symbol, - }: { - watchlist_id: string; - symbol: string; - }) => - request({ - path: `/v2/watchlists/${watchlist_id}`, - method: "POST", - data: { symbol }, - }), - getByName: ({ name }: GetWatchlistByNameParams) => - request({ - path: "/v2/watchlists:by_name", - method: "GET", - params: { name }, - }), - updateByName: ({ - name, - newName, - symbols, - }: UpdateWatchlistByNameParams) => - request({ - path: "/v2/watchlists:by_name", - method: "PUT", - params: { name }, - data: { name: newName, symbols }, - }), - addAssetByName: ({ name, symbol }: AddAssetToWatchlistParams) => - request({ - path: "/v2/watchlists:by_name", - method: "POST", - params: { name }, - data: { symbol }, - }), - deleteByName: ({ name }: DeleteWatchlistByNameParams) => - request({ - path: "/v2/watchlists:by_name", - method: "DELETE", - params: { name }, - }), - deleteSymbol: ({ - watchlistId, - symbol, - }: DeleteSymbolFromWatchlistParams) => - request({ - path: `/v2/watchlists/${watchlistId}/${symbol}`, - method: "DELETE", - }), - }, - accountConfigurations: { - get: () => - request({ - path: "/v2/account/configurations", - method: "GET", - }), - patch: (updatedConfig: UpdatedAccountConfigurations) => - request({ - path: "/v2/account/configurations", - method: "PATCH", - data: updatedConfig, - }), - }, - activities: { - get: ( - activityType?: string, - options?: { - date?: string; - until?: string; - after?: string; - direction?: string; - pageSize?: number; - pageToken?: string; - category?: string; - } - ) => - request({ - path: `/v2/account/activities${ - activityType ? `/${activityType}` : "" - }`, - method: "GET", - params: { - date: options?.date, - until: options?.until, - after: options?.after, - direction: options?.direction, - page_size: options?.pageSize, - page_token: options?.pageToken, - category: options?.category, - }, - }), - }, - calendar: { - get: (options?: { - start?: string; - end?: string; - dateType?: "TRADING" | "SETTLEMENT"; - }) => - request({ - path: "/v2/calendar", - method: "GET", - params: { - start: options?.start, - end: options?.end, - date_type: options?.dateType, - }, - }), - }, - clock: { - get: () => - request({ - path: "/v2/clock", - method: "GET", - }), - }, - assets: { - getAll: (options?: { - status?: string; - asset_class?: string; - exchange?: string; - attributes?: string[]; - }) => - request({ - path: "/v2/assets", - method: "GET", - params: { - status: options?.status, - asset_class: options?.asset_class, - exchange: options?.exchange, - attributes: options?.attributes?.join(","), - }, - }), - getAsset: (symbolOrAssetId: string) => - request({ - path: `/v2/assets/${symbolOrAssetId}`, + }), + }, + calendar: { + get: (options?: { + start?: string; + end?: string; + dateType?: "TRADING" | "SETTLEMENT"; + }) => + request({ + path: "/v2/calendar", + method: "GET", + params: { + start: options?.start, + end: options?.end, + date_type: options?.dateType, + }, + }), + }, + clock: { + get: () => + request({ + path: "/v2/clock", + method: "GET", + }), + }, + assets: { + get: (options?: { + status?: string; + asset_class?: string; + exchange?: string; + attributes?: string[]; + }) => + request({ + path: "/v2/assets", + method: "GET", + params: { + status: options?.status, + asset_class: options?.asset_class, + exchange: options?.exchange, + attributes: options?.attributes?.join(","), + }, + }), + // getAssetBySymbolOrId: (symbolOrAssetId: string) => + // request({ + // path: `/v2/assets/${symbolOrAssetId}`, + // method: "GET", + // }), + }, + options: { + get: (queryParams: OptionContractsQueryParams, symbolOrId?: string) => { + let path = "/v2/options/contracts"; + if (symbolOrId) { + path += `/${symbolOrId}`; + } + return request({ + path, + method: "GET", + params: queryParams, + }); + }, + }, + corporate: { + announcements: { + get: ( + queryParams?: AnnouncementsQueryParams, + announcementId?: string + ) => { + if (announcementId) { + return request({ + path: `/v2/corporate_actions/announcements/${announcementId}`, method: "GET", - }), - }, - options: { - get: (queryParams: OptionContractsQueryParams, symbolOrId?: string) => { - let path = "/v2/options/contracts"; - if (symbolOrId) { - path += `/${symbolOrId}`; - } - return request({ - path, + }); + } else { + return request({ + path: "/v2/corporate_actions/announcements", method: "GET", params: queryParams, }); - }, + } }, - corporateActions: { - announcements: { - get: ( - queryParams?: AnnouncementsQueryParams, - announcementId?: string - ) => { - if (announcementId) { - return request({ - path: `/v2/corporate_actions/announcements/${announcementId}`, - method: "GET", - }); - } else { - return request({ - path: "/v2/corporate_actions/announcements", - method: "GET", - params: queryParams, - }); - } - }, - }, - }, - cryptoFunding: { - getWallets: (asset?: string) => - request({ - path: "/v2/wallets", - method: "GET", - params: { asset }, - }), - - requestWithdrawal: (withdrawalParams: WithdrawalParams) => - request({ - path: "/v2/wallets/transfers", - method: "POST", - data: withdrawalParams, - }), - getTransfers: (transferId?: string) => { - if (transferId) { - return request({ - path: `/v2/wallets/transfers/${transferId}`, - method: "GET", - }); - } else { - return request({ - path: "/v2/wallets/transfers", - method: "GET", - }); - } - }, - getWhitelistedAddresses: () => + }, + }, + crypto: { + wallets: { + list: (asset?: string) => + request({ + path: "/v2/wallets", + method: "GET", + params: { asset }, + }), + whitelists: { + list: () => request({ path: "/v2/wallets/whitelists", method: "GET", }), - requestWhitelistedAddress: ( - whitelistedAddressParams: WhitelistedAddressParams - ) => + request: (whitelistedAddressParams: WhitelistedAddressParams) => request({ path: "/v2/wallets/whitelists", method: "POST", data: whitelistedAddressParams, }), - deleteWhitelistedAddress: (whitelistedAddressId: string) => + delete: (whitelistedAddressId: string) => request({ path: `/v2/wallets/whitelists/${whitelistedAddressId}`, method: "DELETE", }), - estimateTransactionFee: (transactionParams: TransactionParams) => - request({ - path: "/v2/wallets/fees/estimate", + }, + fees: { + estimate: { + get: (transactionParams: TransactionParams) => + request({ + path: "/v2/wallets/fees/estimate", + method: "GET", + params: transactionParams, + }), + }, + }, + }, + transfers: { + list: (transferId?: string) => { + if (transferId) { + return request({ + path: `/v2/wallets/transfers/${transferId}`, method: "GET", - params: transactionParams, - }), + }); + } else { + return request({ + path: "/v2/wallets/transfers", + method: "GET", + }); + } }, + withdraw: (withdrawalParams: WithdrawalParams) => + request({ + path: "/v2/wallets/transfers", + method: "POST", + data: withdrawalParams, + }), }, }, - // websocket: ({ options, request }: ClientContext) => null, - websocket: {} as any, }); diff --git a/api/trading.ts b/api/trading.ts deleted file mode 100644 index b288abc..0000000 --- a/api/trading.ts +++ /dev/null @@ -1,1066 +0,0 @@ -export type Account = { - id: string; - account_number: string; - status: - | "ONBOARDING" - | "SUBMISSION_FAILED" - | "SUBMITTED" - | "ACCOUNT_UPDATED" - | "APPROVAL_PENDING" - | "ACTIVE" - | "REJECTED"; - 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: 0 | 1 | 2; - options_trading_level: 0 | 1 | 2; -}; - -export interface UpdatedAccountConfigurations { - dtbp_check?: "both" | "entry" | "exit"; - trade_confirm_email?: "all" | "none"; - suspend_trade?: boolean; - no_shorting?: boolean; - fractional_trading?: boolean; - max_margin_multiplier?: "1" | "2"; - max_options_trading_level?: 0 | 1 | 2; - pdt_check?: "both" | "entry" | "exit"; - ptp_no_exception_entry?: boolean; -} - -export interface AccountConfigurations { - dtbpCheck: "both" | "entry" | "exit"; - tradeConfirmEmail: "all" | "none"; - suspendTrade: boolean; - noShorting: boolean; - fractionalTrading: boolean; - maxMarginMultiplier: "1" | "2"; - maxOptionsTradingLevel: 0 | 1 | 2; - pdtCheck: "both" | "entry" | "exit"; - ptpNoExceptionEntry: boolean; -} - -export interface AccountTradingActivity { - activity_type: string; - id: string; - cum_qty: string; - leaves_qty: string; - price: string; - qty: string; - side: string; - symbol: string; - transaction_time: string; - order_id: string; - type: "fill" | "partial_fill"; - order_status: string; -} - -export interface AccountNonTradeActivity { - activity_type: string; - id: string; - date: string; - net_amount: string; - symbol?: string; - qty?: string; - per_share_amount?: string; -} - -export type AccountActivity = AccountTradingActivity | AccountNonTradeActivity; -export type Asset = { - id: string; - class: string; - exchange: string; - symbol: string; - name: string; - status: string; - tradable: boolean; - marginable: boolean; - shortable: boolean; - easy_to_borrow: boolean; - fractionable: boolean; - maintenance_margin_requirement: string; - attributes: string[]; -}; -export interface MarketCalendar { - date: string; - open: string; - close: string; - settlement_date: string; -} -export interface MarketClock { - timestamp: string; - is_open: boolean; - next_open: string; - next_close: string; -} -export interface CorporateActionAnnouncement { - id: string; - corporate_actions_id: string; - ca_type: string; - ca_sub_type: string; - initiating_symbol: string; - initiating_original_cusip: string; - target_symbol: string; - target_original_cusip: string; - declaration_date: string; - expiration_date: string; - record_date: string; - payable_date: string; - cash: string; - old_rate: string; - new_rate: string; -} - -export interface AnnouncementsQueryParams { - ca_types?: string; - since?: string; - until?: string; - symbol?: string; - cusip?: string; - date_type?: string; -} -export interface CryptoFundingWallet { - chain: string; - address: string; - created_at: string; -} - -export interface WhitelistedAddressParams { - address: string; - asset: string; -} -export interface WhitelistedAddress { - id: string; - chain: string; - asset: string; - address: string; - status: "ACTIVE" | "PENDING"; - created_at: string; // Timestamp (RFC3339) of account creation -} - -export interface CryptoFundingTransfer { - id: string; - tx_hash: string; - direction: "INCOMING" | "OUTGOING"; - status: "PROCESSING" | "FAILED" | "COMPLETE"; - amount: string; - usd_value: string; - network_fee: string; - fees: string; - chain: string; - asset: string; - from_address: string; - to_address: string; - created_at: string; // Timestamp (RFC3339) of transfer creation -} - -export interface WithdrawalParams { - amount: string; - address: string; - asset: string; -} - -export interface CryptoFundingTransfer { - id: string; - tx_hash: string; - direction: "INCOMING" | "OUTGOING"; - status: "PROCESSING" | "FAILED" | "COMPLETE"; - amount: string; - usd_value: string; - network_fee: string; - fees: string; - chain: string; - asset: string; - from_address: string; - to_address: string; - created_at: string; // Timestamp (RFC3339) of transfer creation -} - -export interface CryptoFundingQueryParams { - asset?: string; -} - -export interface CryptoFundingResponse { - wallets?: CryptoFundingWallet | CryptoFundingWallet[]; - transfers?: CryptoFundingTransfer[]; -} - -export interface CryptoFundingWallet { - chain: string; - address: string; - created_at: string; // Timestamp (RFC3339) of account creation -} - -export interface WithdrawalParams { - amount: string; - address: string; - asset: string; -} - -export interface CryptoFundingTransfer { - id: string; - tx_hash: string; - direction: "INCOMING" | "OUTGOING"; - status: "PROCESSING" | "FAILED" | "COMPLETE"; - amount: string; - usd_value: string; - network_fee: string; - fees: string; - chain: string; - asset: string; - from_address: string; - to_address: string; - created_at: string; // Timestamp (RFC3339) of transfer creation -} - -export interface TransactionParams { - asset: string; - from_address: string; - to_address: string; - amount: string; -} - -export interface TransactionFeeResponse { - fee: string; -} -export interface OptionContractsQueryParams { - underlying_symbols?: string; - status?: string; - active?: boolean; - expiration_date?: string; - expiration_date_gte?: string; - expiration_date_lte?: string; - root_symbol?: string; - type?: string; - style?: string; - strike_price_gte?: number; - strike_price_lte?: number; - page_token?: string; - limit?: number; -} - -export interface OptionContract { - id: string; - symbol: string; - name: string; - status: string; - tradable: boolean; - expiration_date: string; - root_symbol?: string; - underlying_symbol: string; - underlying_asset_id: string; - type: string; - style: string; - strike_price: string; - size: string; - open_interest?: string; - open_interest_date?: string; - close_price?: string; - close_price_date?: string; - next_page_token: string | null; -} -export interface PatchOrderOptions { - qty?: string; - time_in_force?: "day" | "gtc" | "opg" | "cls" | "ioc" | "fok"; - limit_price?: string; - stop_price?: string; - trail?: string; - client_order_id?: string; -} - -export interface GetOrdersOptions { - status?: "open" | "closed" | "all"; - limit?: number; - after?: string; - until?: string; - direction?: "asc" | "desc"; - nested?: boolean; - symbols?: string; - side?: "buy" | "sell"; -} - -export interface CreateOrderOptions { - symbol: string; - qty?: string; - notional?: string; - side: "buy" | "sell"; - type: "market" | "limit" | "stop" | "stop_limit" | "trailing_stop"; - time_in_force: "day" | "gtc" | "opg" | "cls" | "ioc" | "fok"; - limit_price?: string; - stop_price?: string; - trail_price?: string; - trail_percent?: string; - extended_hours: boolean; - client_order_id?: string; - order_class?: "simple" | "oco" | "oto" | "bracket"; - take_profit?: { - limit_price: string; - }; - stop_loss?: { - stop_price: string; - limit_price?: string; - }; -} - -export type BaseOrder = { - id: string; - client_order_id: string; - created_at: string; - updated_at: string; - submitted_at: string; - filled_at: string | null; - expired_at: string | null; - canceled_at: string | null; - failed_at: string | null; - replaced_at: string | null; - replaced_by: string | null; - replaces: string | null; - asset_id: string; - notional: string | null; - qty: string; - filled_qty: string; - filled_avg_price: string | null; - order_class: string; - order_type: string; - type: string; - side: string; - time_in_force: string; - limit_price: string | null; - stop_price: string | null; - status: string; - extended_hours: boolean; - legs: any | null; - trail_percent: string | null; - trail_price: string | null; - hwm: string | null; - subtag: string | null; - source: string | null; -}; - -export type EquityOrder = BaseOrder & { - asset_class: "us_equity"; - symbol: string; -}; - -export type OptionsOrder = BaseOrder & { - asset_class: "us_option"; - symbol: string; - order_class: "simple"; -}; - -export type CryptoOrder = BaseOrder & { - asset_class: "crypto"; - symbol: string; -}; - -export type Order = EquityOrder | OptionsOrder | CryptoOrder; -export interface PortfolioHistoryParams { - period?: string; - timeframe?: string; - intraday_reporting?: string; - start?: string; - end?: string; - pnl_reset?: string; -} - -export interface PortfolioHistoryResponse { - timestamp: number[]; - equity: number[]; - profit_loss: number[]; - profit_loss_pct: number[]; - base_value: number; - base_value_asof: string; - timeframe: string; -} -export interface ClosePositionOptions { - symbol_or_asset_id?: string; - cancel_orders?: boolean; - qty?: number; - percentage?: number; -} - -export type PositionClosedResponse = unknown[]; - -export interface Position { - asset_id: string; - symbol: string; - exchange: string; - asset_class: string; - avg_entry_price: string; - qty: string; - qty_available: string; - side: string; - market_value: string; - cost_basis: string; - unrealized_pl: string; - unrealized_plpc: string; - unrealized_intraday_pl: string; - unrealized_intraday_plpc: string; - current_price: string; - lastday_price: string; - change_today: string; - asset_marginable: boolean; -} -export interface CreateWatchlistParams { - name: string; - symbols: string[] | null; -} - -export interface AddAssetToWatchlistParams { - name: string; - symbol: string; -} - -export interface DeleteSymbolFromWatchlistParams { - watchlistId: string; - symbol: string; -} - -export interface DeleteWatchlistByNameParams { - name: string; -} - -export interface GetWatchlistByNameParams { - name: string; -} - -export interface UpdateWatchlistParams { - name: string; - symbols: string[] | null; -} - -export interface UpdateWatchlistByNameParams { - name: string; - newName: string; - symbols: string[] | null; -} - -export interface Watchlist { - id: string; - account_id: string; - created_at: string; - updated_at: string; - name: string; - assets: Asset[]; -} -interface BaseEvent { - timestamp: string; -} - -interface NewEvent extends BaseEvent { - type: "new"; -} - -interface FillEvent extends BaseEvent { - type: "fill"; - price: number; - position_qty: number; -} - -interface PartialFillEvent extends BaseEvent { - type: "partial_fill"; - price: number; - position_qty: number; -} - -interface CanceledEvent extends BaseEvent { - type: "canceled"; -} - -interface ExpiredEvent extends BaseEvent { - type: "expired"; -} - -interface DoneForDayEvent extends BaseEvent { - type: "done_for_day"; -} - -interface ReplacedEvent extends BaseEvent { - type: "replaced"; -} - -interface RejectedEvent extends BaseEvent { - type: "rejected"; -} - -interface PendingNewEvent extends BaseEvent { - type: "pending_new"; -} - -interface StoppedEvent extends BaseEvent { - type: "stopped"; -} - -interface PendingCancelEvent extends BaseEvent { - type: "pending_cancel"; -} - -interface PendingReplaceEvent extends BaseEvent { - type: "pending_replace"; -} - -interface CalculatedEvent extends BaseEvent { - type: "calculated"; -} - -interface SuspendedEvent extends BaseEvent { - type: "suspended"; -} - -interface OrderReplaceRejectedEvent extends BaseEvent { - type: "order_replace_rejected"; -} - -interface OrderCancelRejectedEvent extends BaseEvent { - type: "order_cancel_rejected"; -} - -type TradeUpdateEvent = { - type: - | "new" - | "fill" - | "partial_fill" - | "canceled" - | "expired" - | "replaced" - | "rejected" - | "order_cancel_rejected" - | "order_replace_rejected"; - timestamp: string; -}; - -interface ChannelEventMap { - trade_updates: TradeUpdateEvent; -} - -type TradeSubscriptionRequest = { - channel: keyof ChannelEventMap; - symbols?: string[]; -}; - -export type TradeWebSocket = { - on: ( - channel: E, - event: E extends keyof ChannelEventMap ? ChannelEventMap[E]["type"] : never, - handler: ( - data: Extract - ) => void - ) => void; - subscribe: ( - requests: TradeSubscriptionRequest[] - ) => Promise; - unsubscribe: ( - requests: TradeSubscriptionRequest[] - ) => Promise; -}; - -type Trade = { - T: "t"; - i: number; - S: string; - x: string; - p: number; - s: number; - c: string[]; - t: string; - z: string; -}; - -type Quote = { - T: "q"; - S: string; - bx: string; - bp: number; - bs: number; - ax: string; - ap: number; - as: number; - c: string[]; - t: string; - z: string; -}; - -type Bar = { - T: "b" | "d" | "u"; - S: string; - o: number; - h: number; - l: number; - c: number; - v: number; - t: string; -}; - -type Correction = { - T: "c"; - S: string; - x: string; - oi: number; - op: number; - os: number; - oc: string[]; - ci: number; - cp: number; - cs: number; - cc: string[]; - t: string; - z: string; -}; - -type CancelError = { - T: "x"; - S: string; - i: number; - x: string; - p: number; - s: number; - a: string; - t: string; - z: string; -}; - -type LULD = { - T: "l"; - S: string; - u: number; - d: number; - i: string; - t: string; - z: string; -}; - -type TradingStatus = { - T: "s"; - S: string; - sc: string; - sm: string; - rc: string; - rm: string; - t: string; - z: string; -}; - -type StockDataMessage = - | Trade - | Quote - | Bar - | Correction - | CancelError - | LULD - | TradingStatus - | SuccessMessage - | ErrorMessage - | SubscriptionMessage; - -// interface StockChannelEventMap extends EventMap { -// trades: { T: "trades"; data: Trade }; -// quotes: { T: "quotes"; data: Quote }; -// bars: { T: "bars"; data: Bar }; -// dailyBars: { T: "dailyBars"; data: Bar }; -// updatedBars: { T: "updatedBars"; data: Bar }; -// statuses: { T: "statuses"; data: TradingStatus }; -// lulds: { T: "lulds"; data: LULD }; -// } - -// export type StockDataWebSocket = WebSocket; -interface BaseEvent { - T: string; - S: string; - t: string; -} - -interface TradeEvent extends BaseEvent { - T: "t"; - p: number; - s: number; - i: number; - tks: "B" | "S"; -} - -interface QuoteEvent extends BaseEvent { - T: "q"; - bp: number; - bs: number; - ap: number; - as: number; -} - -interface BarEvent extends BaseEvent { - T: "b" | "d" | "u"; - o: number; - h: number; - l: number; - c: number; - v: number; - n?: number; - vw?: number; -} - -interface OrderbookEvent extends BaseEvent { - T: "o"; - b: { p: number; s: number }[]; - a: { p: number; s: number }[]; - r?: boolean; -} - -interface SuccessMessage { - T: "success"; - msg: string; -} - -interface ErrorMessage { - T: "error"; - code: number; - msg: string; -} - -interface SubscriptionMessage { - T: "subscription"; - trades: string[]; - quotes: string[]; - bars: string[]; - orderbooks: string[]; -} - -type CryptoMessage = - | TradeEvent - | QuoteEvent - | BarEvent - | OrderbookEvent - | SuccessMessage - | ErrorMessage - | SubscriptionMessage; - -interface CryptoChannelEventMap { - trades: TradeEvent; - quotes: QuoteEvent; - bars: BarEvent; - orderbooks: OrderbookEvent; -} - -type CryptoSubscriptionRequest = { - channel: keyof CryptoChannelEventMap; - symbols?: string[]; -}; - -export type CryptoWebSocket = { - on: ( - channel: E, - event: E extends keyof CryptoChannelEventMap - ? CryptoChannelEventMap[E]["T"] - : never, - handler: ( - data: Extract< - CryptoChannelEventMap[E], - { T: CryptoChannelEventMap[E]["T"] } - > - ) => void - ) => void; - subscribe: ( - requests: CryptoSubscriptionRequest[] - ) => Promise; - unsubscribe: ( - requests: CryptoSubscriptionRequest[] - ) => Promise; -}; -interface NewsEvent { - T: "n"; - id: number; - headline: string; - summary: string; - author: string; - created_at: string; - updated_at: string; - url: string; - content: string; - symbols: string[]; - source: string; -} - -interface NewsSuccessMessage { - T: "success"; - msg: string; -} - -interface NewsErrorMessage { - T: "error"; - code: number; - msg: string; -} - -interface NewsSubscriptionMessage { - T: "subscription"; - news: string[]; -} - -type NewsMessage = - | NewsEvent - | NewsSuccessMessage - | NewsErrorMessage - | NewsSubscriptionMessage; - -type NewsAction = "subscribe" | "unsubscribe"; - -interface NewsSubscriptionRequest { - action: NewsAction; - news: string[]; -} - -export type NewsWebSocket = { - on: (event: "message", handler: (data: NewsMessage[]) => void) => void; - send: (data: NewsSubscriptionRequest) => void; -}; -interface BaseOptionEvent { - T: string; - S: string; - t: string; -} - -interface OptionTradeEvent extends BaseOptionEvent { - T: "t"; - p: number; - s: number; - x: string; - c: string; -} - -interface OptionQuoteEvent extends BaseOptionEvent { - T: "q"; - bx: string; - bp: number; - bs: number; - ax: string; - ap: number; - as: number; - c: string; -} - -interface OptionSuccessMessage { - T: "success"; - msg: string; -} - -interface OptionErrorMessage { - T: "error"; - code: number; - msg: string; -} - -interface OptionSubscriptionMessage { - T: "subscription"; - trades: string[]; - quotes: string[]; -} - -type OptionMessage = - | OptionTradeEvent - | OptionQuoteEvent - | OptionSuccessMessage - | OptionErrorMessage - | OptionSubscriptionMessage; - -type OptionAction = "subscribe" | "unsubscribe"; -type OptionChannel = "trades" | "quotes"; - -interface OptionSubscriptionRequest { - action: OptionAction; - trades?: string[]; - quotes?: string[]; -} - -export type OptionsWebSocket = { - on: (event: "message", handler: (data: OptionMessage[]) => void) => void; - send: (data: OptionSubscriptionRequest) => void; -}; - -type TradeEventMap = any; - -export type TradingAPI = { - "/v2/account": { - method: "GET"; - params: {}; - response: Account; - }; - "/v2/orders": { - method: "POST"; - params: CreateOrderOptions; - response: Order; - } & { - method: "GET"; - params: PatchOrderOptions | string; - response: Order | Order[]; - } & { - method: "DELETE"; - params: { orderId?: string }; - response: { id?: string; status?: string }[] | void; - } & { - method: "PATCH"; - params: { orderId: string; options: PatchOrderOptions }; - response: Order; - }; - "/v2/positions": { - method: "GET"; - params: { symbol_or_asset_id?: string }; - response: Position | Position[]; - } & { - method: "DELETE"; - params: ClosePositionOptions; - response: Order[]; - } & { - method: "POST"; - params: { symbol_or_contract_id: string }; - response: void; - }; - "/v2/account/portfolio/history": { - method: "GET"; - params: PortfolioHistoryParams; - response: PortfolioHistoryResponse; - }; - "/v2/watchlists": { - method: "GET"; - params: { watchlist_id?: string }; - response: Watchlist | Watchlist[]; - } & { - method: "POST"; - params: CreateWatchlistParams; - response: Watchlist; - } & { - method: "PUT"; - params: { watchlist_id: string; name: string; symbols: string[] }; - response: Watchlist; - } & { - method: "DELETE"; - params: { watchlist_id: string }; - response: void; - } & { - method: "POST"; - params: { watchlist_id: string; symbol: string }; - response: Watchlist; - } & { - method: "GET"; - params: GetWatchlistByNameParams; - response: Watchlist; - } & { - method: "PUT"; - params: UpdateWatchlistByNameParams; - response: Watchlist; - } & { - method: "POST"; - params: AddAssetToWatchlistParams; - response: void; - } & { - method: "DELETE"; - params: DeleteWatchlistByNameParams; - response: void; - } & { - method: "DELETE"; - params: DeleteSymbolFromWatchlistParams; - response: Watchlist; - }; - "/v2/account/configurations": { - method: "GET"; - params: {}; - response: AccountConfigurations; - } & { - method: "PATCH"; - params: UpdatedAccountConfigurations; - response: void; - }; - "/v2/account/activities": { - method: "GET"; - params: { activityType?: string; options?: any }; - response: AccountActivity[]; - }; - "/v2/calendar": { - method: "GET"; - params: { - start?: string; - end?: string; - dateType?: "TRADING" | "SETTLEMENT"; - }; - response: MarketCalendar[]; - }; - "/v2/clock": { - method: "GET"; - params: {}; - response: MarketClock; - }; - "/v2/assets": { - method: "GET"; - params: { - status?: string; - asset_class?: string; - exchange?: string; - attributes?: string[]; - }; - response: Asset[]; - } & { - method: "GET"; - params: { symbolOrAssetId: string }; - response: Asset; - }; - "/v2/options/contracts": { - method: "GET"; - params: OptionContractsQueryParams; - response: OptionContract | OptionContract[]; - }; - "/v2/corporate_actions/announcements": { - method: "GET"; - params: { queryParams?: AnnouncementsQueryParams; announcementId?: string }; - response: CorporateActionAnnouncement | CorporateActionAnnouncement[]; - }; - "/v2/wallets": { - method: "GET"; - params: { asset?: string }; - response: CryptoFundingWallet | CryptoFundingWallet[]; - } & { - method: "POST"; - params: WithdrawalParams; - response: CryptoFundingTransfer; - } & { - method: "GET"; - params: { transferId?: string }; - response: CryptoFundingTransfer | CryptoFundingTransfer[]; - } & { - method: "GET"; - params: {}; - response: WhitelistedAddress[]; - } & { - method: "POST"; - params: WhitelistedAddressParams; - response: WhitelistedAddress; - } & { - method: "DELETE"; - params: { whitelistedAddressId: string }; - response: void; - } & { - method: "GET"; - params: TransactionParams; - response: TransactionFeeResponse; - }; -};