-
Notifications
You must be signed in to change notification settings - Fork 0
/
apiReqOptionsData.R
55 lines (46 loc) · 1.89 KB
/
apiReqOptionsData.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
# =============================================================================.
# Get Options Data via polygon.io API
# =============================================================================.
# Using this script, options data for all tickers contained in database 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 Options Data via polygon.io API ----
# =============================================================================.
cat("\nAPI request...")
# get tickers for all options
db <- dbConnect(RSQLite::SQLite(), database)
tickers <- dbGetQuery(db, "SELECT * FROM tickers")
tickers <- tickers$ticker
dbDisconnect(db)
# since we query all option tickers, clear relevant table if already exists
# db <- dbConnect(RSQLite::SQLite(), database)
# if("options" %in% dbListTables(db)){
# dbRemoveTable(db, "options")
# }
# dbDisconnect(db)
# get all option data
extractPolygonOptContracts(apikey = apikey,
tickers = tickers,
from = "2019-01-07",
to = "2023-06-07",
database = database,
adjusted="true",
limit=50000,
limitedAPIcalls=F)
# =============================================================================.
cat("\nRuntime:")
Sys.time() - start_time