使用自定义开始时间创建每日 OHLC
Creating daily OHLC with custom starting time
我有 15 分钟的 OHLC 数据,想转换为每日 OHLC
但一天的开始时间是 17:00:00。
这样,生成的日柱应该跨越 17:00:00 到 17:00:00,而不是从 00:00:00 到 00:00:00
_
x <- zoo(runif(25), order.by=seq(
as.POSIXct("2010-05-03 17:00:00"),
as.POSIXct("2010-05-06 17:00:00"),
by="15 min"
)
)
_
head(x)
2010-05-03 17:00:00 0.9788685
2010-05-03 17:15:00 0.5414294
2010-05-03 17:30:00 0.8435366
2010-05-03 17:45:00 0.3064713
2010-05-03 18:00:00 0.1395849
2010-05-03 18:15:00 0.9916730
使用 xts:将周期从 15m 更改为 60m 效果很好:
x_agg <- to.minutes(x,k=60, indexAt="startof")
head(x_agg)
x.Open x.High x.Low x.Close
2010-05-03 17:00:00 0.9788685 0.9788685 0.30647133 0.3064713
2010-05-03 18:00:00 0.1395849 0.9916730 0.09497550 0.5301038
2010-05-03 19:00:00 0.3580554 0.4264711 0.11728640 0.1172864
2010-05-03 20:00:00 0.9791394 0.9791394 0.01904849 0.1643573
2010-05-03 21:00:00 0.3096280 0.9193756 0.30962797 0.8896507
2010-05-03 22:00:00 0.8125618 0.8976714 0.74335042 0.7433504
使用 xts:将周期从 15m 更改为 1440m = 1Day 不起作用:
x_agg <- to.minutes(x,k=1440, indexAt="startof")
head(x_agg)
_
x.Open x.High x.Low x.Close
2010-05-03 17:00:00 0.9788685 0.991673 0.01904849 0.38669801
2010-05-04 02:00:00 0.1172864 0.991673 0.01904849 0.09497550
2010-05-05 02:00:00 0.5301038 0.991673 0.01904849 0.84353659
2010-05-06 02:00:00 0.3064713 0.991673 0.01904849 0.01904849
我不知道为什么索引会更改为 02:00:00 - 它应该是 17:00:00 所有天。
如何做到这一点 - 使用 xts?
谢谢你的努力。
dput(x_agg)
structure(c(0.97886852407828, 0.117286398308352, 0.530103818513453,
0.306471328716725, 0.991673005977646, 0.991673005977646, 0.991673005977646,
0.991673005977646, 0.0190484928898513, 0.0190484928898513, 0.0190484928898513,
0.0190484928898513, 0.386698011076078, 0.0949754973407835, 0.843536590691656,
0.0190484928898513), .Dim = c(4L, 4L), .Dimnames = list(NULL,
c("x.Open", "x.High", "x.Low", "x.Close")), index = structure(c(1272898800,
1272931200, 1273017600, 1273104000), class = c("POSIXct", "POSIXt"
), tzone = ""), class = "zoo")
大多数(全部?)xts 函数使用 endpoints()
函数定义时间间隔,该函数根据距纪元的偏移量创建间隔。这通常是可取的,因为它 finds/creates 在时间单位(在一分钟、一小时等的变化时)中断。
如果我没理解错的话,您希望间隔由数据中的第一个观察值定义。我可以看到您如何期望 17:00:00 处的端点,因为那是您第一次观察的时间和分钟。如果您的第一次观察是在 17:01:32.741.
这样的不规则时间,我不确定您是否会有同样的期望
所以,这是背景故事,因此我想不出 easy/straight-forward 方法来获得您想要的结果。但这是一种尝试,将 period.apply()
与自定义端点和函数结合使用。
# custom aggregation function to convert a chunk of data to one OHLC observation
toOHLC <- function(x) {
op <- as.vector(first(x))
hl <- range(x, na.rm = TRUE)
cl <- as.vector(last(x))
xts(cbind(Open = op, High = hl[2], Low = hl[1], Close = cl), end(x))
}
现在可以获取 17:00 定义的天数的端点。
# Convert to xts
y <- as.xts(x)
# Convert index to UTC
indexTZ(y) <- "UTC"
# Set first observation to epoch (zero)
.index(y) <- .index(y) - .index(y)[1]
# Get endpoints for y by day
ep <- endpoints(y, "days")
现在您可以在对 period.apply()
的调用中使用 ep
来按天获取 OHLC,其中 17:00:00 是断点。
period.apply(x, ep, toOHLC)
# Open High Low Close
# 2010-05-04 16:45:00 0.6415173 0.8440296 0.01497329 0.84402960
# 2010-05-05 16:45:00 0.8411363 0.8440296 0.01497329 0.08874158
# 2010-05-06 16:45:00 0.7843095 0.8440296 0.01497329 0.40912559
# 2010-05-06 17:00:00 0.5669512 0.5669512 0.56695121 0.56695121
我有 15 分钟的 OHLC 数据,想转换为每日 OHLC
但一天的开始时间是 17:00:00。
这样,生成的日柱应该跨越 17:00:00 到 17:00:00,而不是从 00:00:00 到 00:00:00
_
x <- zoo(runif(25), order.by=seq(
as.POSIXct("2010-05-03 17:00:00"),
as.POSIXct("2010-05-06 17:00:00"),
by="15 min"
)
)
_
head(x)
2010-05-03 17:00:00 0.9788685
2010-05-03 17:15:00 0.5414294
2010-05-03 17:30:00 0.8435366
2010-05-03 17:45:00 0.3064713
2010-05-03 18:00:00 0.1395849
2010-05-03 18:15:00 0.9916730
使用 xts:将周期从 15m 更改为 60m 效果很好:
x_agg <- to.minutes(x,k=60, indexAt="startof")
head(x_agg)
x.Open x.High x.Low x.Close
2010-05-03 17:00:00 0.9788685 0.9788685 0.30647133 0.3064713
2010-05-03 18:00:00 0.1395849 0.9916730 0.09497550 0.5301038
2010-05-03 19:00:00 0.3580554 0.4264711 0.11728640 0.1172864
2010-05-03 20:00:00 0.9791394 0.9791394 0.01904849 0.1643573
2010-05-03 21:00:00 0.3096280 0.9193756 0.30962797 0.8896507
2010-05-03 22:00:00 0.8125618 0.8976714 0.74335042 0.7433504
使用 xts:将周期从 15m 更改为 1440m = 1Day 不起作用:
x_agg <- to.minutes(x,k=1440, indexAt="startof")
head(x_agg)
_
x.Open x.High x.Low x.Close
2010-05-03 17:00:00 0.9788685 0.991673 0.01904849 0.38669801
2010-05-04 02:00:00 0.1172864 0.991673 0.01904849 0.09497550
2010-05-05 02:00:00 0.5301038 0.991673 0.01904849 0.84353659
2010-05-06 02:00:00 0.3064713 0.991673 0.01904849 0.01904849
我不知道为什么索引会更改为 02:00:00 - 它应该是 17:00:00 所有天。
如何做到这一点 - 使用 xts? 谢谢你的努力。
dput(x_agg)
structure(c(0.97886852407828, 0.117286398308352, 0.530103818513453,
0.306471328716725, 0.991673005977646, 0.991673005977646, 0.991673005977646,
0.991673005977646, 0.0190484928898513, 0.0190484928898513, 0.0190484928898513,
0.0190484928898513, 0.386698011076078, 0.0949754973407835, 0.843536590691656,
0.0190484928898513), .Dim = c(4L, 4L), .Dimnames = list(NULL,
c("x.Open", "x.High", "x.Low", "x.Close")), index = structure(c(1272898800,
1272931200, 1273017600, 1273104000), class = c("POSIXct", "POSIXt"
), tzone = ""), class = "zoo")
大多数(全部?)xts 函数使用 endpoints()
函数定义时间间隔,该函数根据距纪元的偏移量创建间隔。这通常是可取的,因为它 finds/creates 在时间单位(在一分钟、一小时等的变化时)中断。
如果我没理解错的话,您希望间隔由数据中的第一个观察值定义。我可以看到您如何期望 17:00:00 处的端点,因为那是您第一次观察的时间和分钟。如果您的第一次观察是在 17:01:32.741.
这样的不规则时间,我不确定您是否会有同样的期望所以,这是背景故事,因此我想不出 easy/straight-forward 方法来获得您想要的结果。但这是一种尝试,将 period.apply()
与自定义端点和函数结合使用。
# custom aggregation function to convert a chunk of data to one OHLC observation
toOHLC <- function(x) {
op <- as.vector(first(x))
hl <- range(x, na.rm = TRUE)
cl <- as.vector(last(x))
xts(cbind(Open = op, High = hl[2], Low = hl[1], Close = cl), end(x))
}
现在可以获取 17:00 定义的天数的端点。
# Convert to xts
y <- as.xts(x)
# Convert index to UTC
indexTZ(y) <- "UTC"
# Set first observation to epoch (zero)
.index(y) <- .index(y) - .index(y)[1]
# Get endpoints for y by day
ep <- endpoints(y, "days")
现在您可以在对 period.apply()
的调用中使用 ep
来按天获取 OHLC,其中 17:00:00 是断点。
period.apply(x, ep, toOHLC)
# Open High Low Close
# 2010-05-04 16:45:00 0.6415173 0.8440296 0.01497329 0.84402960
# 2010-05-05 16:45:00 0.8411363 0.8440296 0.01497329 0.08874158
# 2010-05-06 16:45:00 0.7843095 0.8440296 0.01497329 0.40912559
# 2010-05-06 17:00:00 0.5669512 0.5669512 0.56695121 0.56695121