You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
85 lines
2.3 KiB
TypeScript
85 lines
2.3 KiB
TypeScript
import { restClient } from "@polygon.io/client-js";
|
|
import PQueue from "p-queue";
|
|
import dotenv from "dotenv";
|
|
import { Env } from "@humanwhocodes/env";
|
|
|
|
dotenv.config();
|
|
|
|
const env = new Env();
|
|
const POLYGON_API_KEY = env.require("POLYGON_API_KEY");
|
|
|
|
const polygon = restClient(POLYGON_API_KEY, "https://api.polygon.io", {
|
|
pagination: true,
|
|
});
|
|
|
|
const queue = new PQueue({ concurrency: 6 });
|
|
|
|
function* dateRange(
|
|
startDateStr: string,
|
|
endDateStr: string
|
|
): Generator<string, void, unknown> {
|
|
// Convert the start and end date strings to Date objects
|
|
const startDate = new Date(startDateStr);
|
|
const endDate = new Date(endDateStr);
|
|
|
|
// Loop from the start date to the end date
|
|
for (
|
|
let currentDate = startDate;
|
|
currentDate <= endDate;
|
|
currentDate.setDate(currentDate.getDate() + 1)
|
|
) {
|
|
// Format the current date as YYYY-MM-DD
|
|
const formattedDate = currentDate.toISOString().split("T")[0];
|
|
|
|
// Yield the formatted date
|
|
yield formattedDate;
|
|
}
|
|
}
|
|
|
|
for (const date of dateRange("2024-05-02", "2024-05-03")) {
|
|
const contracts = await polygon.reference.optionsContracts({
|
|
underlying_ticker: "SPY",
|
|
as_of: date,
|
|
limit: 1000,
|
|
});
|
|
if (contracts.status?.toLowerCase() === "ok" && contracts.results) {
|
|
for (const contract of contracts.results) {
|
|
/** I don't know why we need this `if` again, but typescript yelled at me: */
|
|
if (contract.ticker) {
|
|
await queue.add(async () => {
|
|
if (contract.ticker) {
|
|
const quotes = await polygon.options.quotes(
|
|
contract.ticker,
|
|
{
|
|
timestamp: date,
|
|
sort: "timestamp",
|
|
order: "asc",
|
|
limit: 50000,
|
|
},
|
|
{ pagination: true }
|
|
);
|
|
if (quotes.status?.toLowerCase() === "ok" && quotes.results) {
|
|
console.log(date, contract.ticker, quotes.results?.length);
|
|
}
|
|
}
|
|
});
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// const data = await polygon.options.quotes(
|
|
// "O:SPY241220P00600000",
|
|
// {
|
|
// "timestamp.gte": "2024-12-15",
|
|
// "timestamp.lte": "2024-12-16",
|
|
// sort: "timestamp",
|
|
// order: "asc",
|
|
// limit: 50000,
|
|
// },
|
|
// { pagination: true }
|
|
// );
|
|
|
|
// console.log(data.status);
|
|
// console.log(data.results?.length);
|