-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathmarketdata.R
84 lines (82 loc) · 2.55 KB
/
marketdata.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
81
82
83
84
#' Read and parses files delivered by B3
#'
#' B3, and previously BMF&Bovespa, used to deliver many files with a diverse
#' set of valuable data and informations that can be used to study of can
#' be called of marketdata.
#' There are files with informations about futures, option, interest
#' rates, currency rates, bonds and many other subjects.
#'
#' @param filename a string containing a path for the file.
#' @param template a string with the template name.
#' @param parse_fields a logical indicating if the fields must be parsed.
#' @param do_cache Whether to use cache or not (default = TRUE)
#'
#' The function `show_templates` can be used to view the available templates.
#'
#' @return `data.frame` of a list of `data.frame` containing data parsed from
#' files.
#'
#' @seealso show_templates display_template
#'
#' @examples
#' \dontrun{
#' path <- "Indic.txt"
#' df <- read_marketdata(path, template = "Indic")
#' path <- "PUWEB.TXT"
#' df <- read_marketdata(path, template = "PUWEB")
#' }
#' @export
read_marketdata <- function(filename, template,
parse_fields = TRUE,
do_cache = TRUE) {
if (file.size(filename) <= 2) {
msg <- str_glue("File is empty: {b}", b = filename)
stop(empty_file_error(msg))
}
template <- template_retrieve(template)
basename_ <- str_replace(basename(filename), "\\.[^\\.]+$", "") |>
str_replace("\\.", "_")
parsed_ <- if (parse_fields) "parsed" else "strict"
cache_folder <- dirname(filename)
f_cache <- file.path(
cache_folder, str_glue("{b}-{p}.rds", b = basename_, p = parsed_)
)
if (file.exists(f_cache) && do_cache) {
df_ <- read_rds(f_cache)
if (is.null(df_)) {
alert("warning", "Removed cached file {f_cache} that returns NULL.",
f_cache = f_cache
)
unlink(f_cache)
}
return(df_)
}
df <- template$read_file(template, filename, parse_fields)
if (is.null(df)) {
rb3_clear_cache <- getOption("rb3.clear.cache")
if (!is.null(rb3_clear_cache) && isTRUE(rb3_clear_cache)) {
alert(
"warning",
"Removed {filename} - It hasn't valid content.",
filename = filename
)
unlink(filename)
} else {
alert(
"warning",
"{filename} hasn't valid content, consider removing if it is cached.",
filename = filename
)
}
}
if (do_cache && !is.null(df)) {
write_rds(df, f_cache)
}
df
}
empty_file_error <- function(message) {
structure(
class = c("empty_file_error", "condition"),
list(message = message, call = sys.call(-1))
)
}