-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlastDay.R
executable file
·70 lines (62 loc) · 1.71 KB
/
lastDay.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
#! /usr/bin/Rscript
#
# 1. take list 'la' (key: security code + expiration date, value: time series data.table
# 2. select from 'value' only the last line (last date) for each key
# 3. select only keys with expiration dates in the future
# 4. change data structure from
# key: `NTN-F010131`, value: date bid ask puc puv txm pum
# to
# key1: 'NTN-F' key2: 010131 value: same
#
# find date of last time info was updated in xls file
#
getLastDate <- function(){
# initialize data in the past
lastDate <- as.Date('2000-01-01')
for( name in names(la) ) {
e <- la[[ name ]]
# check last date
dt <- e[nrow(e),date]
if( dt > lastDate ) {
lastDate <- dt
}
}
return( lastDate )
}
lastDate <- getLastDate()
# select only instruments that were not expired on the last date
ld1 <- list()
for( name in names(la) ) {
e <- la[[ name ]]
dt <- e[nrow(e),date]
if( dt == lastDate ) {
ld1[[ name ]] <- e[nrow(e),]
}
}
# change data structure from
# key: `NTN-F010131`, value: date bid ask puc puv txm pum
# to
# key1: 'NTN-F' key2: 010131 value: same
ld2 <- list()
for( code_venc in names(ld1) ) {
code <- gsub( '[0-9]', '', code_venc )
venc <- gsub( '[^0-9]', '', code_venc )
name <- secdict[[ code ]]
value <- ld1[[ code_venc ]]
value[,date := as.character(date)]
# build new list ld2
venc <- as.Date(venc,"%d%m%y")
name_venc <- paste( name, venc, sep='\t' )
ld2[[ name_venc ]] <- value
}
#
# xlsLastUpdated
#
#print(lastDate)
ld2[[ 'xlsLastUpdated' ]] <- as.character(format(lastDate,'%d-%m-%Y'))
#
# save json to local file
#
jsonData <- toJSON(ld2)
write(jsonData, paste( pathp, "tsv/lastDay.json", sep='') )
write(jsonData, paste0(lastDay_dir, "lastDay.json" ))