10000 [WIP] Adds Futures support (alpha) by justinpolygon · Pull Request #212 · polygon-io/client-js · GitHub
[go: up one dir, main page]

Skip to content

[WIP] Adds Futures support (alpha) #212

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
10000 Jump to file
Failed to load files.
Loading
Diff view
Diff view
Adds Futures support (alpha)
  • Loading branch information
justinpolygon committed Feb 28, 2025
commit e46e33c75086ae0b0fa7b9282e515d6005f72e56
252 changes: 252 additions & 0 deletions src/rest/futures/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,252 @@
import { IGet, IPolygonQuery, IRequestOptions, getWithGlobals } from "../transport/request.js";

// ### Futures Aggregates
export interface IFuturesAggsQuery extends IPolygonQuery {
resolution?: string;
"window_start"?: string;
"window_start.gt"?: string;
"window_start.gte"?: string;
"window_start.lt"?: string;
"window_start.lte"?: string;
order?: "asc" | "desc";
limit?: number;
sort?: string;
}

export interface IFuturesAgg {
ticker: string;
open: number;
high: number;
low: number;
close: number;
volume: number;
window_start: number;
window_end: number;
}

export interface IFuturesAggs {
status: string;
request_id: string;
results?: IFuturesAgg[];
}

export const listFuturesAggs = async (
get: IGet,
ticker: string,
query?: IFuturesAggsQuery,
options?: IRequestOptions
): Promise<IFuturesAggs> =>
get(`/futures/vX/aggs/${ticker}`, query, options);

// ### Futures Contracts
export interface IFuturesContract {
ticker: string;
name: string;
product_code: string;
expiration_date: string;
}

export interface IFuturesContracts {
status: string;
request_id: string;
results?: IFuturesContract[];
}

export const listFuturesContracts = async (
get: IGet,
query?: IPolygonQuery,
options?: IRequestOptions
): Promise<IFuturesContracts> =>
get(`/futures/vX/contracts`, query, options);

export const getFuturesContract = async (
get: IGet,
ticker: string,
options?: IRequestOptions
): Promise<IFuturesContract> =>
get(`/futures/vX/contracts/${ticker}`, undefined, options);

// ### Futures Market Status
export interface IFuturesMarketStatus {
status: string;
timestamp: number;
}

export const listFuturesMarketStatuses = async (
get: IGet,
options?: IRequestOptions
): Promise<IFuturesMarketStatus> =>
get(`/futures/vX/market-status`, undefined, options);

// ### Futures Products
export interface IFuturesProduct {
product_code: string;
name: string;
description?: string;
}

export interface IFuturesProducts {
status: string;
request_id: string;
results?: IFuturesProduct[];
}

export const listFuturesProducts = async (
get: IGet,
query?: IPolygonQuery,
options?: IRequestOptions
): Promise<IFuturesProducts> =>
get(`/futures/vX/products`, query, options);

export const getFuturesProduct = async (
get: IGet,
product_code: string,
options?: IRequestOptions
): Promise<IFuturesProduct> =>
get(`/futures/vX/products/${product_code}`, undefined, options);

// ### Futures Schedules
export interface IFuturesSchedule {
product_code?: string;
start_date: string;
end_date: string;
open_time: string;
close_time: string;
}

export interface IFuturesSchedules {
status: string;
request_id: string;
results?: IFuturesSchedule[];
}

export const listFuturesSchedules = async (
get: IGet,
query?: IPolygonQuery,
options?: IRequestOptions
): Promise<IFuturesSchedules> =>
get(`/futures/vX/schedules`, query, options);

export const listFuturesProductSchedules = async (
get: IGet,
product_code: string,
query?: IPolygonQuery,
options?: IRequestOptions
): Promise<IFuturesSchedules> =>
get(`/futures/vX/products/${product_code}/schedules`, query, options);

// ### Futures Trades
export interface IFuturesTrade {
price: number;
size: number;
timestamp: number;
}

export interface IFuturesTrades {
next_url?: string;
request_id?: string;
results?: IFuturesTrade[];
status?: string;
}

export const listFuturesTrades = async (
get: IGet,
ticker: string,
query?: IPolygonQuery,
options?: IRequestOptions
): Promise<IFuturesTrades> =>
get(`/futures/vX/trades/${ticker}`, query, options);

// ### Futures Quotes
export interface IFuturesQuote {
ask_price: number;
ask_size: number;
bid_price: number;
bid_size: number;
timestamp: number;
}

export interface IFuturesQuotes {
next_url?: string;
request_id?: string;
results?: IFuturesQuote[];
status?: string;
}

export const listFuturesQuotes = async (
get: IGet,
ticker: string,
query?: IPolygonQuery,
options?: IRequestOptions
): Promise<IFuturesQuotes> =>
get(`/futures/vX/quotes/${ticker}`, query, options);

// ### Futures Client Interface
export interface IFuturesClient {
listFuturesAggs: (
ticker: string,
query?: IFuturesAggsQuery,
options?: IRequestOptions
) => Promise<IFuturesAggs>;
listFuturesContracts: (
query?: IPolygonQuery,
options?: IRequestOptions
) => Promise<IFuturesContracts>;
getFuturesContract: (
ticker: string,
options?: IRequestOptions
) => Promise<IFuturesContract>;
listFuturesMarketStatuses: (
options?: IRequestOptions
) => Promise<IFuturesMarketStatus>;
listFuturesProducts: (
query?: IPolygonQuery,
options?: IRequestOptions
) => Promise<IFuturesProducts>;
getFuturesProduct: (
product_code: string,
options?: IRequestOptions
) => Promise<IFuturesProduct>;
listFuturesSchedules: (
query?: IPolygonQuery,
options?: IRequestOptions
) => Promise<IFuturesSchedules>;
listFuturesProductSchedules: (
product_code: string,
query?: IPolygonQuery,
options?: IRequestOptions
) => Promise<IFuturesSchedules>;
listFuturesTrades: (
ticker: string,
query?: IPolygonQuery,
options?: IRequestOptions
) => Promise<IFuturesTrades>;
listFuturesQuotes: (
ticker: string,
query?: IPolygonQuery,
options?: IRequestOptions
) => Promise<IFuturesQuotes>;
}

// ### Futures Client Factory
export const futuresClient = (
apiKey: string,
apiBase = "https://api.polygon.io",
options?: IRequestOptions
): IFuturesClient => {
const get = getWithGlobals(apiKey, apiBase, options);
return {
listFuturesAggs: (...args) => listFuturesAggs(get, ...args),
listFuturesContracts: (...args) => listFuturesContracts(get, ...args),
getFuturesContract: (...args) => getFuturesContract(get, ...args),
listFuturesMarketStatuses: (...args) => listFuturesMarketStatuses(get, ...args),
listFuturesProducts: (...args) => listFuturesProducts(get, ...args),
getFuturesProduct: (...args) => getFuturesProduct(get, ...args),
listFuturesSchedules: (...args) => listFuturesSchedules(get, ...args),
listFuturesProductSchedules: (...args) => listFuturesProductSchedules(get, ...args),
listFuturesTrades: (...args) => listFuturesTrades(get, ...args),
listFuturesQuotes: (...args) => listFuturesQuotes(get, ...args),
};
};

export default futuresClient;
4 changes: 4 additions & 0 deletions src/rest/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@ import { referenceClient, IReferenceClient } from "./reference/index.js";
import { optionsClient, IOptionsClient } from "./options/index.js";
import { stocksClient, IStocksClient } from "./stocks/index.js";
import { indicesClient, IIndicesClient } from "./indices/index.js";
import { futuresClient, IFuturesClient } from "./futures/index.js";
import { IRequestOptions } from "./transport/request.js";
export * from "./crypto/index.js";
export * from "./forex/index.js";
export * from "./reference/index.js";
export * from "./options/index.js";
export * from "./stocks/index.js";
export * from "./indices/index.js";
export * from "./futures/index.js";
export * from "./universal/universalSnapshot.js";
export * from "./transport/request.js"; // ensure types are exported

Expand All @@ -21,6 +23,7 @@ export interface IRestClient {
options: IOptionsClient;
stocks: IStocksClient;
indices: IIndicesClient;
futures: IFuturesClient;
}

export const restClient = (apiKey, apiBase?: string, options?: IRequestOptions): IRestClient => ({
Expand All @@ -30,6 +33,7 @@ export const restClient = (apiKey, apiBase?: string, options?: IRequestOptions):
options: optionsClient(apiKey, apiBase, options),
stocks: stocksClient(apiKey, apiBase, options),
indices: indicesClient(apiKey, apiBase, options),
futures: futuresClient(apiKey, apiBase, options),
});

export default restClient;
45 changes: 45 additions & 0 deletions src/websockets/futures/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import * as websocket from "websocket";
import { getWsClient } from "../transport/index.js";

// Futures Trade Event
export interface IFuturesTradeEvent {
ev: string; // e.g., "T"
sym: string; // Symbol (e.g., "F:ESZ4")
p: number; // Price
s: number; // Size
t: number; // Timestamp (Unix MS)
c?: number[]; // Conditions
x?: number; // Exchange ID
}

// Futures Quote Event
export interface IFuturesQuoteEvent {
ev: string; // e.g., "Q"
sym: string; // Symbol
bp: number; // Bid Price
bs: number; // Bid Size
ap: number; // Ask Price
as: number; // Ask Size
t: number; // Timestamp (Unix MS)
x?: number; // Exchange ID
}

// Futures Aggregate Event
export interface IFuturesAggregateEvent {
ev: string; // e.g., "A" (second) or "AM" (minute)
sym: string; // Symbol
o: number; // Open Price
c: number; // Close Price
h: number; // High Price
l: number; // Low Price
v: number; // Volume
s: number; // Start Timestamp (Unix MS)
e: number; // End Timestamp (Unix MS)
}

export const getFuturesWebsocket = (
apiKey: string,
apiBase = "wss://socket.polygon.io"
): websocket.w3cwebsocket => getWsClient(`${apiBase}/futures`, apiKey);

export default getFuturesWebsocket;
4 changes: 4 additions & 0 deletions src/websockets/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,22 @@ import { getForexWebsocket } from "./forex/index.js";
import { getIndicesWebsocket } from "./indices/index.js";
import { getOptionsWebsocket } from "./options/index.js";
import { getStocksWebsocket } from "./stocks/index.js";
import { getFuturesWebsocket } from "./futures/index.js";

export * from "./forex/index.js";
export * from "./indices/index.js";
export * from "./stocks/index.js";
export * from "./crypto/index.js";
export * from "./options/index.js";
export * from "./futures/index.js";

export interface IWebsocketClient {
crypto: () => websocket.w3cwebsocket;
forex: () => websocket.w3cwebsocket;
indices: () => websocket.w3cwebsocket;
options: () => websocket.w3cwebsocket;
stocks: () => websocket.w3cwebsocket;
futures: () => websocket.w3cwebsocket;
}

export const websocketClient = (
Expand All @@ -28,6 +31,7 @@ export const websocketClient = (
indices: () => getIndicesWebsocket(apiKey, apiBase),
options: () => getOptionsWebsocket(apiKey, apiBase),
stocks: () => getStocksWebsocket(apiKey, apiBase),
futures: () => getFuturesWebsocket(apiKey, apiBase),
});

export default websocketClient;
0