-
Notifications
You must be signed in to change notification settings - Fork 1
/
fetch-dividends-yahoo.R
62 lines (55 loc) · 1.92 KB
/
fetch-dividends-yahoo.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
library(RPostgreSQL)
library(quantmod)
OVERLAPPING = 4
driver <- PostgreSQL()
host <- Sys.getenv('HOST', 'localhost')
dbname <- Sys.getenv('DATABASE_NAME', 'econometrika')
dbuser <- Sys.getenv('DATABASE_USER')
dbpass <- Sys.getenv('DATABASE_PASSWORD')
con <- dbConnect(driver, host=host, dbname=dbname, user=dbuser, password=dbpass)
args <- commandArgs()
sql <- "
select sy.*, ss.key
from analysis_symbol sy
join analysis_symbolsource ss on ss.symbol_id = sy.id
where ss.name = 'quantmod' and ss.type = 'quote'"
if (!is.na(args[1])) {
sql <- paste(sql, "and ticker = $1")
stmt <- dbSendQuery(con, sql, args[1])
} else stmt <- dbSendQuery(con, sql)
symbols <- fetch(stmt, -1)
dbClearResult(stmt)
if (!is.na(args[2])) {
min_ex_date <- as.Date(args[2])
} else min_ex_date <- as.Date('1839-07-08')
for (rs in rownames(symbols)) {
symbol <- symbols[rs,]
print(paste0('Fetching for ', symbol$name))
ret <- try (all_dividends <- getDividends(symbol$key))
if (inherits(ret, 'error')) {
message(ret)
next
}
stmt <- dbSendQuery(con, "select d.* from analysis_dividend d where d.symbol_id = $1 order by ex_date desc", symbol$id)
existing_dividends <- fetch(stmt, -1)
dbClearResult(stmt)
for (i in index(all_dividends)) {
i_date <- as.Date(i)
if (i_date < min_ex_date) next
for (d in rownames(existing_dividends)) {
div <- existing_dividends[d,]
isnew <- T
if (abs(i_date - div$ex_date) <= OVERLAPPING) {
isnew <- F
break
}
}
if (isnew) {
gross <- all_dividends[i_date]
stmt <- postgresqlExecStatement(con, 'insert into analysis_dividend (symbol_id, ex_date, gross, type) values ($1, $2, $3, \'\')',
c(symbol$id, as.character(i_date), gross))
dbClearResult(stmt)
}
}
}
dbDisconnect(con)