-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapiReqStockData.R
67 lines (53 loc) · 2.17 KB
/
apiReqStockData.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
# =============================================================================.
# Get Stock Data via polygon.io API
# =============================================================================.
# Using this script, stock 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 Stock Data via polygon.io API ----
# =============================================================================.
cat("\nAPI request...")
# get tickers for all stocks
tickers <- readRDS("data/tickers.rds")
tickers <- tickers$ticker
# unfortunately, I cannot estimate the PoD for all banks I wanted due to
# computational limitations, so I reduce the sample
tickers <- c()
# the 3 biggest banks in the US
tickers <- c(tickers, "JPM", "BAC", "C")
# banks comparable to SVB and First Republic Bank (in terms of total assets)
tickers <- c(tickers, "CFG", "HSBC", "SIVBQ", "FRCB", "FITB")
# the Swiss banks
tickers <- c(tickers, "UBS", "CS")
# banks comparable to CS (in terms of total assets)
tickers <- c(tickers, "VLY", "MFG", "WTFC")
# since we query all stocks, clear relevant table if already exists
db <- dbConnect(RSQLite::SQLite(), database)
if("stocks" %in% dbListTables(db)){
dbRemoveTable(db, "stocks")
}
dbDisconnect(db)
# get all option data
extractPolygonStocks(apikey = apikey,
tickers = tickers,
database = database,
from = "2019-06-10",
to = "2023-06-02",
limitedAPIcalls = T)
# =============================================================================.
cat("\nRuntime:")
Sys.time() - start_time