-
Notifications
You must be signed in to change notification settings - Fork 22
/
currency_helpers.R
73 lines (65 loc) · 1.42 KB
/
currency_helpers.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
#' Helpers to access time series columns
#'
#' Extraxt bid and ask data from time series objects.
#'
#' @param x time series objects containing data to be extracted
#' @param ... additional arguments
#'
#' @return tibble with time series
#'
#' @name helpers
#' @examples
#' \dontrun{
#' x <- get_currency("EUR", "2018-06-22", "2018-06-28")
#' Bid(x)
#' Ask(x)
#' }
NULL
#' @rdname helpers
#' @export
Bid <- function(x, ...) UseMethod("Bid")
#' @rdname helpers
#' @export
Bid.data.frame <- function(x, ...) {
x_ <- subset(x, select = c("date", "bid"))
colnames(x_) <- c("date", attr(x, "symbol"))
x_
}
#' @rdname helpers
#' @export
Bid.xts <- function(x, ...) {
x_ <- x[, "bid"]
colnames(x_) <- c(attr(x, "symbol"))
x_
}
#' @rdname helpers
#' @export
Bid.olinda_df <- function(x, ...) {
x_ <- subset(x, select = c("datetime", "bid"))
colnames(x_) <- c("datetime", attr(x, "symbol"))
x_
}
#' @rdname helpers
#' @export
Ask <- function(x, ...) UseMethod("Ask", x)
#' @rdname helpers
#' @export
Ask.data.frame <- function(x, ...) {
x_ <- subset(x, select = c("date", "ask"))
colnames(x_) <- c("date", attr(x, "symbol"))
x_
}
#' @rdname helpers
#' @export
Ask.xts <- function(x, ...) {
x_ <- x[, "ask"]
colnames(x_) <- c(attr(x, "symbol"))
x_
}
#' @rdname helpers
#' @export
Ask.olinda_df <- function(x, ...) {
x_ <- subset(x, select = c("datetime", "ask"))
colnames(x_) <- c("datetime", attr(x, "symbol"))
x_
}