8000 Adds Futures support (alpha) · polygon-io/client-js@e46e33c · GitHub
[go: up one dir, main page]

Skip to content

Commit e46e33c

Browse files
committed
Adds Futures support (alpha)
1 parent 75aa6ed commit e46e33c

File tree

4 files changed

+305
-0
lines changed

4 files changed

+305
-0
lines changed

src/rest/futures/index.ts

Lines changed: 252 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,252 @@
1+
import { IGet, IPolygonQuery, IRequestOptions, getWithGlobals } from "../transport/request.js";
2+
3+
// ### Futures Aggregates
4+
export interface IFuturesAggsQuery extends IPolygonQuery {
5+
resolution?: string;
6+
"window_start"?: string;
7+
"window_start.gt"?: string;
8+
"window_start.gte"?: string;
9+
"window_start.lt"?: string;
10+
"window_start.lte"?: string;
11+
order?: "asc" | "desc";
12+
limit?: number;
13+
sort?: string;
14+
}
15+
16+
export interface IFuturesAgg {
17+
ticker: string;
18+
open: number;
19+
high: number;
20+
low: number;
21+
close: number;
22+
volume: number;
23+
window_start: number;
24+
window_end: number;
25+
}
26+
27+
export interface IFuturesAggs {
28+
status: string;
29+
request_id: string;
30+
results?: IFuturesAgg[];
31+
}
32+
33+
export const listFuturesAggs = async (
34+
get: IGet,
35+
ticker: string,
36+
query?: IFuturesAggsQuery,
37+
options?: IRequestOptions
38+
): Promise<IFuturesAggs> =>
39+
get(`/futures/vX/aggs/${ticker}`, query, options);
40+
41+
// ### Futures Contracts
42+
export interface IFuturesContract {
43+
ticker: string;
44+
name: string;
45+
product_code: string;
46+
expiration_date: string;
47+
}
48+
49+
export interface IFuturesContracts {
50+
status: string;
51+
request_id: string;
52+
results?: IFuturesContract[];
53+
}
54+
55+
export const listFuturesContracts = async (
56+
get: IGet,
57+
query?: IPolygonQuery,
58+
options?: IRequestOptions
59+
): Promise<IFuturesContracts> =>
60+
get(`/futures/vX/contracts`, query, options);
61+
62+
export const getFuturesContract = async (
63+
get: IGet,
64+
ticker: string,
65+
options?: IRequestOptions
66+
): Promise<IFuturesContract> =>
67+
get(`/futures/vX/contracts/${ticker}`, undefined, options);
68+
69+
// ### Futures Market Status
70+
export interface IFuturesMarketStatus {
71+
status: string;
72+
timestamp: number;
73+
}
74+
75+
export const listFuturesMarketStatuses = async (
76+
get: IGet,
77+
options?: IRequestOptions
78+
): Promise<IFuturesMarketStatus> =>
79+
get(`/futures/vX/market-status`, undefined, options);
80+
81+
// ### Futures Products
82+
export interface IFuturesProduct {
83+
product_code: string;
84+
name: string;
85+
description?: string;
86+
}
87+
88+
export interface IFuturesProducts {
89+
status: string;
90+
request_id: string;
91+
results?: IFuturesProduct[];
92+
}
93+
94+
export const listFuturesProducts = async (
95+
get: IGet,
96+
query?: IPolygonQuery,
97+
options?: IRequestOptions
98+
): Promise<IFuturesProducts> =>
99+
get(`/futures/vX/products`, query, options);
100+
101+
export const getFuturesProduct = async (
102+
get: IGet,
103+
product_code: string,
104+
options?: IRequestOptions
105+
): Promise<IFuturesProduct> =>
106+
get(`/futures/vX/products/${product_code}`, undefined, options);
107+
108+
// ### Futures Schedules
109+
export interface IFuturesSchedule {
110+
product_code?: string;
111+
start_date: string;
112+
end_date: string;
113+
open_time: string;
114+
close_time: string;
115+
}
116+
117+
export interface IFuturesSchedules {
118+
status: string;
119+
request_id: string;
120+
results?: IFuturesSchedule[];
121+
}
122+
123+
export const listFuturesSchedules = async (
124+
get: IGet,
125+
query?: IPolygonQuery,
126+
options?: IRequestOptions
127+
): Promise<IFuturesSchedules> =>
128+
get(`/futures/vX/schedules`, query, options);
129+
130+
export const listFuturesProductSchedules = async (
131+
get: IGet,
132+
product_code: string,
133+
query?: IPolygonQuery,
134+
options?: IRequestOptions
135+
): Promise<IFuturesSchedules> =>
136+
get(`/futures/vX/products/${product_code}/schedules`, query, options);
137+
138+
// ### Futures Trades
139+
export interface IFuturesTrade {
140+
price: number;
141+
size: number;
142+
timestamp: number;
143+
}
144+
145+
export interface IFuturesTrades {
146+
next_url?: string;
147+
request_id?: string;
148+
results?: IFuturesTrade[];
149+
status?: string;
150+
}
151+
152+
export const listFuturesTrades = async (
153+
get: IGet,
154+
ticker: string,
155+
query?: IPolygonQuery,
156+
options?: IRequestOptions
157+
): Promise<IFuturesTrades> =>
158+
get(`/futures/vX/trades/${ticker}`, query, options);
159+
160+
// ### Futures Quotes
161+
export interface IFuturesQuote {
162+
ask_price: number;
163+
ask_size: number;
164+
bid_price: number;
165+
bid_size: number;
166+
timestamp: number;
167+
}
168+
169+
export interface IFuturesQuotes {
170+
next_url?: string;
171+
request_id?: string;
172+
results?: IFuturesQuote[];
173+
status?: string;
174+
}
175+
176+
export const listFuturesQuotes = async (
177+
get: IGet,
178+
ticker: string,
179+
query?: IPolygonQuery,
180+
options?: IRequestOptions
181+
): Promise<IFuturesQuotes> =>
182+
get(`/futures/vX/quotes/${ticker}`, query, options);
183+
184+
// ### Futures Client Interface
185+
export interface IFuturesClient {
186+
listFuturesAggs: (
187+
ticker: string,
188+
query?: IFuturesAggsQuery,
189+
options?: IRequestOptions
190+
) => Promise<IFuturesAggs>;
191+
listFuturesContracts: (
192+
query?: IPolygonQuery,
193+
options?: IRequestOptions
194+
) => Promise<IFuturesContracts>;
195+
getFuturesContract: (
196+
ticker: string,
197+
options?: IRequestOptions
198+
) => Promise<IFuturesContract>;
199+
listFuturesMarketStatuses: (
200+
options?: IRequestOptions
201+
) => Promise<IFuturesMarketStatus>;
202+
listFuturesProducts: (
203+
query?: IPolygonQuery,
204+
options?: IRequestOptions
205+
) => Promise<IFuturesProducts>;
206+
getFuturesProduct: (
207+
product_code: string,
208+
options?: IRequestOptions
209+
) => Promise<IFuturesProduct>;
210+
listFuturesSchedules: (
211+
query?: IPolygonQuery,
212+
options?: IRequestOptions
213+
) => Promise<IFuturesSchedules>;
214+
listFuturesProductSchedules: (
215+
product_code: string,
216+
query?: IPolygonQuery,
217+
options?: IRequestOptions
218+
) => Promise<IFuturesSchedules>;
219+
listFuturesTrades: (
220+
ticker: string,
221+
query?: IPolygonQuery,
222+
options?: IRequestOptions
223+
) => Promise<IFuturesTrades>;
224+
listFuturesQuotes: (
225+
ticker: string,
226+
query?: IPolygonQuery,
227+
options?: IRequestOptions
228+
) => Promise<IFuturesQuotes>;
229+
}
230+
231+
// ### Futures Client Factory
232+
export const futuresClient = (
233+
apiKey: string,
234+
apiBase = "https://api.polygon.io",
235+
options?: IRequestOptions
236+
): IFuturesClient => {
237+
const get = getWithGlobals(apiKey, apiBase, options);
238+
return {
239+
listFuturesAggs: (...args) => listFuturesAggs(get, ...args),
240+
listFuturesContracts: (...args) => listFuturesContracts(get, ...args),
241+
getFuturesContract: (...args) => getFuturesContract(get, ...args),
242+
listFuturesMarketStatuses: (...args) => listFuturesMarketStatuses(get, ...args),
243+
listFuturesProducts: (...args) => listFuturesProducts(get, ...args),
244+
getFuturesProduct: (...args) => getFuturesProduct(get, ...args),
245+
listFuturesSchedules: (...args) => listFuturesSchedules(get, ...args),
246+
listFuturesProductSchedules: (...args) => listFuturesProductSchedules(get, ...args),
247+
listFuturesTrades: (...args) => listFuturesTrades(get, ...args),
248+
listFuturesQuotes: (...args) => listFuturesQuotes(get, ...args),
249+
};
250+
};
251+
252+
export default futuresClient;

src/rest/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,15 @@ import { referenceClient, IReferenceClient } from "./reference/index.js";
44
import { optionsClient, IOptionsClient } from "./options/index.js";
55
import { stocksClient, IStocksClient } from "./stocks/index.js";
66
import { indicesClient, IIndicesClient } from "./indices/index.js";
7+
import { futuresClient, IFuturesClient } from "./futures/index.js";
78
import { IRequestOptions } from "./transport/request.js";
89
export * from "./crypto/index.js";
910
export * from "./forex/index.js";
1011
export * from "./reference/index.js";
1112
export * from "./options/index.js";
1213
export * from "./stocks/index.js";
1314
export * from "./indices/index.js";
15+
export * from "./futures/index.js";
1416
export * from "./universal/universalSnapshot.js";
1517
export * from "./transport/request.js"; // ensure types are exported
1618

@@ -21,6 +23,7 @@ export interface IRestClient {
2123
options: IOptionsClient;
2224
stocks: IStocksClient;
2325
indices: IIndicesClient;
26+
futures: IFuturesClient;
2427
}
2528

2629
export const restClient = (apiKey, apiBase?: string, options?: IRequestOptions): IRestClient => ({
@@ -30,6 +33,7 @@ export const restClient = (apiKey, apiBase?: string, options?: IRequestOptions):
3033
options: optionsClient(apiKey, apiBase, options),
3134
stocks: stocksClient(apiKey, apiBase, options),
3235
indices: indicesClient(apiKey, apiBase, options),
36+
futures: futuresClient(apiKey, apiBase, options),
3337
});
3438

3539
export default restClient;

src/websockets/futures/index.ts

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import * as websocket from "websocket";
2+
import { getWsClient } from "../transport/index.js";
3+
4+
// Futures Trade Event
5+
export interface IFuturesTradeEvent {
6+
ev: string; // e.g., "T"
7+
sym: string; // Symbol (e.g., "F:ESZ4")
8+
p: number; // Price
9+
s: number; // Size
10+
t: number; // Timestamp (Unix MS)
11+
c?: number[]; // Conditions
12+
x?: number; // Exchange ID
13+
}
14+
15+
// Futures Quote Event
16+
export interface IFuturesQuoteEvent {
17+
ev: string; // e.g., "Q"
18+
sym: string; // Symbol
19+
bp: number; // Bid Price
20+
bs: number; // Bid Size
21+
ap: number; // Ask Price
22+
as: number; // Ask Size
23+
t: number; // Timestamp (Unix MS)
24+
x?: number; // Exchange ID
25+
}
26+
27+
// Futures Aggregate Event
28+
export interface IFuturesAggregateEvent {
29+
ev: string; // e.g., "A" (second) or "AM" (minute)
30+
sym: string; // Symbol
31+
o: number; // Open Price
32+
c: number; // Close Price
33+
h: number; // High Price
34+
l: number; // Low Price
35+
v: number; // Volume
36+
s: number; // Start Timestamp (Unix MS)
37+
e: number; // End Timestamp (Unix MS)
38+
}
39+
40+
export const getFuturesWebsocket = (
41+
apiKey: string,
42+
apiBase = "wss://socket.polygon.io"
43+
): websocket.w3cwebsocket => getWsClient(`${apiBase}/futures`, apiKey);
44+
45+
export default getFuturesWebsocket;

src/websockets/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,22 @@ import { getForexWebsocket } from "./forex/index.js";
44
import { getIndicesWebsocket } from "./indices/index.js";
55
import { getOptionsWebsocket } from "./options/index.js";
66
import { getStocksWebsocket } from "./stocks/index.js";
7+
import { getFuturesWebsocket } from "./futures/index.js";
78

89
export * from "./forex/index.js";
910
export * from "./indices/index.js";
1011
export * from "./stocks/index.js";
1112
export * from "./crypto/index.js";
1213
export * from "./options/index.js";
14+
export * from "./futures/index.js";
1315

1416
export interface IWebsocketClient {
1517
crypto: () => websocket.w3cwebsocket;
1618
forex: () => websocket.w3cwebsocket;
1719
indices: () => websocket.w3cwebsocket;
1820
options: () => websocket.w3cwebsocket;
1921
stocks: () => websocket.w3cwebsocket;
22+
futures: () => websocket.w3cwebsocket;
2023
}
2124

2225
export const websocketClient = (
@@ -28,6 +31,7 @@ export const websocketClient = (
2831
indices: () => getIndicesWebsocket(apiKey, apiBase),
2932
options: () => getOptionsWebsocket(apiKey, apiBase),
3033
stocks: () => getStocksWebsocket(apiKey, apiBase),
34+
futures: () => getFuturesWebsocket(apiKey, apiBase),
3135
});
3236

3337
export default websocketClient;

0 commit comments

Comments
 (0)
0