XTS:按交易日拆分外汇日内柱数据 [英] XTS: split FX intraday bar data by trading days

查看:21
本文介绍了XTS:按交易日拆分外汇日内柱数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将一个函数应用于价值 20 个交易日的每小时外汇数据(作为众多示例中的一个).

我从 rollapply(data,width=20*24,FUN=FUN,by=24) 开始.这似乎运行良好,我什至可以断言我总是传入 480 条……直到我意识到这不是我想要的.由于夏令时和市场假期的变化,这 480 个柱的开始和结束时间多年来一直在漂移.

所以,我想要的是一个函数,它将一天视为我们有数据的每一天的 22:00 到 22:00.(纽约夏令时 21:00 到 21:00 - 我的数据时区是 UTC,而 daystart 定义在美国东部时间下午 5 点)

因此,我以它为核心制作了自己的 rollapply 函数:

 ep=endpoints(data,on=on,k=k)sp=ep[1:(length(ep)-width)]+1ep=ep[(宽度+1):长度(ep)]xx <- lapply(1:length(ep), function(ix) FUN(.subset_xts(data,sp[ix]:ep[ix]),...))

然后我用 on="days"、k=1 和 width=20 调用它.

这有两个问题:

  1. 天是以天为单位,而不是交易日!因此,我获得了不到 3 周的数据,而不是通常 4 周的数据.
  2. 截止时间为 UTC 午夜.我不知道如何将其更改为使用 22:00(或 21:00)截止时间.

<块引用>

UPDATE:上面的问题 1 是错误的!XTS endpoints 功能确实在交易日内起作用,而不是在日历日内起作用.我不这么认为的原因是时区问题使它看起来像 6 天交易周:周日至周五.一旦时区问题得到解决(请参阅我的自我回答),使用 width=20on="days" 确实给了我 4数周的数据.

(通常很重要:当这 4 周有交易假期时,我希望收到 4 周 1 天的数据,即总是恰好 20 个交易日.)

我开始研究一个将数据切成数周的函数,以为我可以将它们切成 5 个 24 小时的块,但这感觉是错误的方法,肯定有人在我之前发明了这个轮子?

解决方案

以下是正确处理黎明的方法:

x2=x索引(x2)=索引(x2)+(7*3600)indexTZ(x2)='America/New_York'

即只需设置时区即可将黎明置于 17:00;我们希望它是在 24:00,所以先加上 7 小时.

在以下方面的帮助下:POSIXct 和 xts 中的时区,从 GMT 转换在 R

这里是完整的功能:

rollapply_chunks.FX.xts=function(data,width,FUN,...,on="days",k=1){数据 <- try.xts(data)x2 <- 数据索引(x2) <- 索引(x2)+(7*3600)indexTZ(x2) <- '美国/纽约'ep <- endpoints(x2,on=on,k=k) #每个日历日的结束点(当on="days"时).#每个入口指向当天的最后一根柱线.ep[1]==0.如果(长度(ep)<2){stop("无法分割数据")}else if(length(ep)==2){ #只能放入一个块.sp <- 1;ep <- ep[-1]}别的{sp <- ep[1:(length(ep)-width)]+1ep <- ep[(宽度+1):长度(ep)]}xx <- lapply(1:length(ep), function(ix) FUN(.subset_xts(data,sp[ix]:ep[ix]),...))xx <- do.call(rbind,xx) #将它们合并为一个大矩阵/data.frame.tt <- index(data)[ep] #Implicit align="right".使用 sp 表示 align="left"res <- xts(xx, tt)返回 (res)}

您可以看到我们使用修改的索引来拆分原始数据.(如果 R 在幕后使用写时复制,那么唯一的额外内存要求应该是索引的副本,而不是数据的副本.)

(法律位:请认为它已获得 MIT 许可,但如果需要,则明确允许在 GPL-2 XTS 包中使用.)

I want to apply a function to 20 trading days worth of hourly FX data (as one example amongst many).

I started off with rollapply(data,width=20*24,FUN=FUN,by=24). That seemed to be working well, I could even assert I always got 480 bars passed in... until I realized that wasn't what I wanted. The start and end time of those 480 bars was drifting over the years, due to changes in daylight savings, and market holidays.

So, what I want is a function that treats a day as from 22:00 to 22:00 of each day we have data for. (21:00 to 21:00 in N.Y. summertime - my data timezone is UTC, and daystart is defined at 5pm ET)

So, I made my own rollapply function with this at its core:

 ep=endpoints(data,on=on,k=k) 
 sp=ep[1:(length(ep)-width)]+1
 ep=ep[(width+1):length(ep)]
 xx <- lapply(1:length(ep), function(ix) FUN(.subset_xts(data,sp[ix]:ep[ix]),...) )

I then called this with on="days", k=1 and width=20.

This has two problems:

  1. Days is in days, not trading days! So, instead of typically 4 weeks of data, I get just under 3 weeks of data.
  2. The cutoff is midnight UTC. I cannot work out how to change it to use the 22:00 (or 21:00) cutoff.

UPDATE: Problem 1 above is wrong! The XTS endpoints function does work in trading days, not calendar days. The reason I thought otherwise is the timezone issue made it look like a 6-day trading week: Sun to Fri. Once the timezone problem was fixed (see my self-answer), using width=20 and on="days" does indeed give me 4 weeks of data.

(The typically there is important: when there is a trading holiday during those 4 weeks I expect to receive 4 weeks 1 day's worth of data, i.e. always exactly 20 trading days.)

I started working on a function to cut the data into weeks, thinking I could then cut them into five 24hr chunks, but this feels like the wrong approach, and surely someone has invented this wheel before me?

解决方案

Here is how to get the daybreak right:

x2=x
index(x2)=index(x2)+(7*3600)
indexTZ(x2)='America/New_York'

I.e. just setting the timezone puts the daybreak at 17:00; we want it to be at 24:00, so add 7 hours on first.

With help from: time zones in POSIXct and xts, converting from GMT in R

Here is the full function:

rollapply_chunks.FX.xts=function(data,width,FUN,...,on="days",k=1){
data <- try.xts(data)

x2 <- data
index(x2) <- index(x2)+(7*3600)
indexTZ(x2) <- 'America/New_York'

ep <- endpoints(x2,on=on,k=k)    #The end point of each calendar day (when on="days").
    #Each entry points to the final bar of the day. ep[1]==0.

if(length(ep)<2){
    stop("Cannot divide data up")
}else if(length(ep)==2){  #Can only fit one chunk in.
    sp <- 1;ep <- ep[-1]
}else{
    sp <- ep[1:(length(ep)-width)]+1
    ep <- ep[(width+1):length(ep)]
}

xx <- lapply(1:length(ep), function(ix) FUN(.subset_xts(data,sp[ix]:ep[ix]),...) )
xx <- do.call(rbind,xx)   #Join them up as one big matrix/data.frame.

tt <- index(data)[ep]  #Implicit align="right". Use sp for align="left"
res <- xts(xx, tt)
return (res)
}

You can see we use the modified index to split up the original data. (If R uses copy-on-write under the covers, then the only extra memory requirement should be for a copy of the index, not of the data.)

(Legal bit: please consider it licensed under MIT, but explicit permission given to use in the GPL-2 XTS package if that is desired.)

这篇关于XTS:按交易日拆分外汇日内柱数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆