-
Notifications
You must be signed in to change notification settings - Fork 0
/
apiReqOptionsTickers.R
81 lines (65 loc) · 2.66 KB
/
apiReqOptionsTickers.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# =============================================================================.
# Get Option Tickers via polygon.io API
# =============================================================================.
# Using this script, option tickers for all underlyings is requested from
# polygon.io API
# =============================================================================.
# Initialization ----
# -----------------------------------------------------------------------------.
rm(list = ls()); gc()
start_time <- Sys.time()
# database
database <- "data/polygonDB.rsqlite"
# API key
apikey <- readLines("data/apikey.txt")
# package management ----
# -----------------------------------------------------------------------------.
library(devtools); load_all()
library(DBI)
library(jsonlite)
# =============================================================================.
# 1. Get option tickers via API Requests ----
# =============================================================================.
cat("\nAPI request...")
# get tickers for all underlying stocks we want options data
tickers <- readRDS("data/tickers.rds")
tickers <- tickers$ticker
# create time frames for which we make API request to avoid reaching limit
years <- c("2020", "2021", "2022", "2023")
dateStart <- c("-01-01", "-02-01", "-03-01", "-04-01", "-05-01", "-06-01",
"-07-01", "-08-01", "-09-01", "-10-01", "-11-01", "-12-01")
dateEnd <- c("-01-31", "-02-28", "-03-31", "-04-30", "-05-31", "-06-30",
"-07-31", "-08-31", "-09-30", "-10-31", "-11-30", "-12-31")
dateStart <- expand.grid(years, dateStart)
dateStart <- data.frame(
startdate = paste0(dateStart$Var1, dateStart$Var2)
)
dateStart$id <- substr(dateStart$startdate, 1, 7)
dateEnd <- expand.grid(years, dateEnd)
dateEnd <- data.frame(
enddate = paste0(dateEnd$Var1, dateEnd$Var2)
)
dateEnd$id = substr(dateEnd$enddate, 1, 7)
dates <- merge(
x = dateStart, y = dateEnd,
by = "id",
all = T
)
rm(years, dateStart, dateEnd)
# since we query all option tickers, clear relevant table if already exists
db <- dbConnect(RSQLite::SQLite(), database)
if("tickers" %in% dbListTables(db)){
dbRemoveTable(db, "tickers")
}
dbDisconnect(db)
# get all option tickers per month
for(i in 1:nrow(dates)){
cat("\nAPI requests for month", dates[i,]$id)
extractPolygonOptTickers(apikey, tickers = tickers, limitedAPIcalls = F,
expiration_date.gte = dates[i,]$startdate,
expiration_date.lte = dates[i,]$enddate,
database="data/polygonDB.rsqlite")
}
# =============================================================================.
cat("\nRuntime:")
Sys.time() - start_time