-
Notifications
You must be signed in to change notification settings - Fork 43
/
data.R
56 lines (48 loc) · 1.61 KB
/
data.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
# Data adquisition and management functions
library(quantmod)
invisible(Sys.setlocale("LC_MESSAGES", "C")) # Google vs date format fix
invisible(Sys.setlocale("LC_TIME", "C")) # https://stackoverflow.com/a/20855453/2860744
get_prices <- function(symbol, from, to, src, ...) {
if (
src == "google"
&& as.Date(from, origin = '1970-01-01') < as.Date("2003-12-28", origin = '1970-01-01')
&& as.Date(to, origin = '1970-01-01') > as.Date("2003-12-30", origin = '1970-01-01')) {
return(rbind(
getSymbols(symbol, env = NULL,
from = from,
to = as.Date("2003-12-27", origin = '1970-01-01'),
src = src, ...),
getSymbols(symbol, env = NULL,
from = as.Date("2003-12-31", origin = '1970-01-01'),
to = to,
src = src, ...)
))
}
getSymbols(symbol, env = NULL, from = from, to = to, src = src, ...)
}
make_dataset <- function(prices, scale = TRUE) {
T.length = nrow(prices)
x = as.vector(Cl(prices))[2:T.length]
u = as.matrix(prices)[1:(T.length - 1), 1:4]
dataset <- list(
x = as.vector(x),
u = as.matrix(u),
x.unscaled = as.vector(x),
u.unscaled = as.matrix(u),
x.center = 0,
x.scale = 1,
u.center = 0,
u.scale = 1
)
if (scale) {
x <- scale(x, TRUE, TRUE)
u <- scale(u, TRUE, TRUE)
dataset$x <- as.vector(x)
dataset$u <- as.matrix(u)
dataset$x.center <- attr(x, "scaled:center")
dataset$x.scale <- attr(x, "scaled:scale")
dataset$u.center <- attr(u, "scaled:center")
dataset$u.scale <- attr(u, "scaled:scale")
}
dataset
}