Compare commits
2 Commits
fc2526a4aa
...
71f72eb474
| Author | SHA1 | Date | |
|---|---|---|---|
| 71f72eb474 | |||
| f8279d4932 |
@@ -38,11 +38,15 @@ export async function* makeGetOptionContractsIterator(
|
|||||||
symbol: string,
|
symbol: string,
|
||||||
date: string
|
date: string
|
||||||
) {
|
) {
|
||||||
let latestBatchResponse = (await (
|
let latestBatchResponse = await pRetry(
|
||||||
|
async () =>
|
||||||
|
(await (
|
||||||
await fetch(
|
await fetch(
|
||||||
`https://api.polygon.io/v3/reference/options/contracts?underlying_ticker=${symbol}&as_of=${date}&sort=ticker&limit=1000&apiKey=${await getApiKey()}`
|
`https://api.polygon.io/v3/reference/options/contracts?underlying_ticker=${symbol}&as_of=${date}&sort=ticker&limit=1000&apiKey=${await getApiKey()}`
|
||||||
)
|
)
|
||||||
).json()) as PolygonOptionContractsResponse;
|
).json()) as PolygonOptionContractsResponse,
|
||||||
|
{ forever: true, factor: 2, maxTimeout: 120000 }
|
||||||
|
);
|
||||||
yield latestBatchResponse.results.map((result) => ({
|
yield latestBatchResponse.results.map((result) => ({
|
||||||
asOfDate: date,
|
asOfDate: date,
|
||||||
symbol,
|
symbol,
|
||||||
@@ -53,9 +57,15 @@ export async function* makeGetOptionContractsIterator(
|
|||||||
|
|
||||||
// as long as there's a `next_url`, call that:
|
// as long as there's a `next_url`, call that:
|
||||||
while (latestBatchResponse.hasOwnProperty("next_url")) {
|
while (latestBatchResponse.hasOwnProperty("next_url")) {
|
||||||
latestBatchResponse = (await (
|
latestBatchResponse = await pRetry(
|
||||||
await fetch(`${latestBatchResponse.next_url}&apiKey=${await getApiKey()}`)
|
async () =>
|
||||||
).json()) as PolygonOptionContractsResponse;
|
(await (
|
||||||
|
await fetch(
|
||||||
|
`${latestBatchResponse.next_url}&apiKey=${await getApiKey()}`
|
||||||
|
)
|
||||||
|
).json()) as PolygonOptionContractsResponse,
|
||||||
|
{ forever: true, factor: 2, maxTimeout: 120000 }
|
||||||
|
);
|
||||||
yield latestBatchResponse.results?.map((result) => ({
|
yield latestBatchResponse.results?.map((result) => ({
|
||||||
asOfDate: date,
|
asOfDate: date,
|
||||||
symbol,
|
symbol,
|
||||||
@@ -106,9 +116,13 @@ export async function* makeGetOptionContractAggregatesIterator({
|
|||||||
const asOfDate = currentDateAsDateObject.toISOString().substring(0, 10);
|
const asOfDate = currentDateAsDateObject.toISOString().substring(0, 10);
|
||||||
const url = `https://api.polygon.io/v2/aggs/ticker/${optionContractTicker}/range/1/minute/${asOfDate}/${asOfDate}?adjusted=false&sort=asc&limit=50000&apiKey=${await getApiKey()}`;
|
const url = `https://api.polygon.io/v2/aggs/ticker/${optionContractTicker}/range/1/minute/${asOfDate}/${asOfDate}?adjusted=false&sort=asc&limit=50000&apiKey=${await getApiKey()}`;
|
||||||
let latestBatchResponse;
|
let latestBatchResponse;
|
||||||
latestBatchResponse = (await (
|
latestBatchResponse = await pRetry(
|
||||||
|
async () =>
|
||||||
|
(await (
|
||||||
await fetch(url)
|
await fetch(url)
|
||||||
).json()) as PolygonOptionContractAggregatesResponse;
|
).json()) as PolygonOptionContractAggregatesResponse,
|
||||||
|
{ forever: true, factor: 2, maxTimeout: 120000 }
|
||||||
|
);
|
||||||
if (latestBatchResponse.status.toLowerCase() === "ok") {
|
if (latestBatchResponse.status.toLowerCase() === "ok") {
|
||||||
yield latestBatchResponse.results?.map((result) => ({
|
yield latestBatchResponse.results?.map((result) => ({
|
||||||
symbol,
|
symbol,
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ import sqlite3 from "sqlite3";
|
|||||||
import { open } from "sqlite";
|
import { open } from "sqlite";
|
||||||
import { clickhouse, query } from "./clickhouse.js";
|
import { clickhouse, query } from "./clickhouse.js";
|
||||||
import { OptionContract } from "./polygon.js";
|
import { OptionContract } from "./polygon.js";
|
||||||
|
import pRetry from "p-retry";
|
||||||
|
|
||||||
const sqliteDb = await open({
|
const sqliteDb = await open({
|
||||||
filename: "/tmp/sync-state.db",
|
filename: "/tmp/sync-state.db",
|
||||||
@@ -119,11 +120,16 @@ export async function pullOptionContracts(symbol: string, date: string) {
|
|||||||
date
|
date
|
||||||
)) {
|
)) {
|
||||||
console.log(batch.length);
|
console.log(batch.length);
|
||||||
|
await pRetry(
|
||||||
|
async () => {
|
||||||
await clickhouse.insert({
|
await clickhouse.insert({
|
||||||
table: "option_contract_existences",
|
table: "option_contract_existences",
|
||||||
values: batch,
|
values: batch,
|
||||||
format: "JSONEachRow",
|
format: "JSONEachRow",
|
||||||
});
|
});
|
||||||
|
},
|
||||||
|
{ forever: true, factor: 2, maxTimeout: 120000 }
|
||||||
|
);
|
||||||
}
|
}
|
||||||
await setPullOptionContractsState(symbol, date, {
|
await setPullOptionContractsState(symbol, date, {
|
||||||
status: OptionContractSyncStatus.COMPLETED,
|
status: OptionContractSyncStatus.COMPLETED,
|
||||||
@@ -157,11 +163,16 @@ export async function pullOptionContractAggregates(
|
|||||||
new Date(batch[0].tsStart * 1000),
|
new Date(batch[0].tsStart * 1000),
|
||||||
new Date(batch[batch.length - 1].tsStart * 1000)
|
new Date(batch[batch.length - 1].tsStart * 1000)
|
||||||
);
|
);
|
||||||
|
await pRetry(
|
||||||
|
async () => {
|
||||||
await clickhouse.insert({
|
await clickhouse.insert({
|
||||||
table: "option_contract_aggregates",
|
table: "option_contract_aggregates",
|
||||||
values: batch,
|
values: batch,
|
||||||
format: "JSONEachRow",
|
format: "JSONEachRow",
|
||||||
});
|
});
|
||||||
|
},
|
||||||
|
{ forever: true, factor: 2, maxTimeout: 120000 }
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
await setPullOptionContractAggregatesState(ticker, {
|
await setPullOptionContractAggregatesState(ticker, {
|
||||||
|
|||||||
Reference in New Issue
Block a user