重新格式化每日库存并将数据返回到每周/每月

时间:2015-11-10 20:27:25

标签: r xts

我有一些股票数据和一些回报,如下所示。

现在我想强制每日价格变化(开盘价,最高价,最低价,收盘价,交易量,收盘价)和每周或每月价值的回报。

我知道每周价格可以通过xts::to.weekly()获得,但这会降低回报。我不知道to.weekly函数背后的确切机制,但是返回需要用sum函数进行汇总(我正在考虑使用xts::apply.weekly()),但是这样会与股票价格数据不一致....

如何有效地实现向每周或每月时间尺度的转换?

Data <-
structure(c(64.5, 67, 72, 76, 75.75, 72, 75.5, 76, 76, 78, 78, 
77.5, 79.25, 80, 76.25, 84, 89.75, 90.75, 92.25, 95.75, 94.5, 
95, 92, 95.75, 100, 98, 104.25, 101.25, 100.25, 96.5, 94.75, 
89, 94, 91.25, 99.25, 100.25, 100.25, 98, 96.5, 94.75, 97.5, 
96.25, 99.25, 97, 98, 98.75, 97.25, 98.75, 100.25, 100.25, 103.25, 
105.75, 108.5, 108.25, 103.75, 101.5, 99.75, 100, 99, 94.5, 99, 
101.5, 105, 64.75, 73.25, 76.5, 76.75, 76, 75.75, 76, 76, 76, 
80.75, 79, 79.5, 83, 80.5, 76.75, 91.5, 92.75, 94.75, 100.25, 
96, 97.5, 96.75, 92, 100, 101.75, 104, 105, 103.25, 100.75, 99, 
95.5, 92.75, 94, 97.75, 103.75, 101, 100.25, 99.5, 97.75, 96.75, 
98, 99, 100, 100.75, 99.25, 98.75, 98, 102.25, 101, 103, 105, 
109, 110.5, 108.25, 105, 102.25, 100.75, 100, 99.75, 98.75, 102.5, 
103, 107.25, 60.75, 66.75, 71.25, 74.25, 72.25, 71.5, 74.25, 
76, 76, 77, 75.75, 76, 79, 75.75, 73.25, 82, 88.75, 89.5, 91.75, 
92.25, 92.75, 92, 92, 94, 97.75, 96.5, 98.5, 99.25, 93.5, 94, 
88.25, 87.5, 91, 91, 98.75, 98.5, 100.25, 95.75, 95.5, 90.75, 
96.75, 96, 95.25, 97, 95.75, 95.75, 96.75, 98.5, 97.25, 100.25, 
102.5, 105.5, 107.5, 103.5, 102.75, 97.5, 98.25, 100, 94, 94.25, 
98, 100, 103.75, 64.25, 72.75, 75.75, 75, 73.75, 75.5, 76, 76, 
76, 79, 76, 79.25, 81.25, 76.75, 75.75, 88, 90.25, 93.75, 97.5, 
95, 95, 92, 92, 100, 97.75, 102.75, 99.75, 100.25, 97.25, 94.5, 
89, 91.75, 92.5, 97, 99.5, 100.25, 100.25, 96.75, 96, 96, 97.5, 
98, 95.5, 100.75, 98, 96.75, 98, 100.25, 99.5, 102.25, 103.5, 
108, 109.25, 104, 102.75, 99, 100, 100, 94.5, 97.5, 102, 103, 
104.5, 6808900, 8180500, 5628500, 3238900, 3765800, 3177100, 
887600, 0, 0, 3923200, 2425700, 3331200, 4058600, 3682800, 3293500, 
10525000, 5664200, 3982600, 4702300, 6479800, 2565300, 2480500, 
0, 3653000, 3400, 4010500, 5145800, 2782200, 3925100, 2770700, 
4618500, 2712300, 1675400, 3331500, 5343000, 1169700, 0, 2095400, 
1016600, 3642600, 3729400, 3575300, 3396900, 1963400, 3547300, 
1865100, 2496300, 3226800, 2333200, 4285900, 3933000, 7400700, 
3325200, 1848400, 21000, 4273700, 1910400, 0, 3168000, 2356000, 
2184800, 1950300, 2649900, 51.5865, 58.4112, 60.8199, 60.2178, 
59.2141, 60.6192, 61.0207, 61.0207, 61.0207, 63.4294, 61.0207, 
63.6301, 65.2359, 61.6228, 60.8199, 70.6555, 72.462, 75.2722, 
78.2831, 76.2758, 76.2758, 73.8671, 73.8671, 80.2903, 78.4838, 
82.4983, 80.0896, 80.4911, 78.0824, 75.8744, 71.4584, 73.6664, 
74.2686, 77.8816, 79.8889, 80.4911, 80.4911, 77.6809, 77.0787, 
77.0787, 78.2831, 78.6845, 76.6773, 80.8925, 78.6845, 77.6809, 
78.6845, 80.4911, 79.8889, 82.0969, 83.1005, 86.7136, 87.7172, 
83.5019, 82.4983, 79.4874, 80.2903, 80.2903, 75.8744, 78.2831, 
81.8961, 82.699, 83.9034, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.086956169, -0.022499605, 0.051150683, 
-0.029196965, 0.005013135, -0.029925048, -0.028277819, -0.058201449, 
0.030899097, 0.00817469, 0.048647746, 0.025773739, 0.007537968, 
0, -0.034913177, -0.007752227, 0, 0.015625588, 0.005127543, -0.025509471, 
0.054973245, -0.027295485, -0.012754736, 0.012919521, 0.022960049, 
-0.007481572, 0.027638383, 0.012224579, 0.043478679, 0.011573732, 
-0.048055569, -0.012018888, -0.03649651, 0.010100972, 0, 0, 0, 
0.046153001, 0.009803886, 0.014563659), .Dim = c(63L, 7L), .Dimnames = list(
    NULL, c("Open", "High", "Low", "Close", "Volume", "Adj.Close", 
    "Return")), index = structure(c(1238544000, 1238630400, 1238716800, 
1238976000, 1239062400, 1239148800, 1239235200, 1239321600, 1239580800, 
1239667200, 1239753600, 1239840000, 1239926400, 1240185600, 1240272000, 
1240358400, 1240444800, 1240531200, 1240790400, 1240876800, 1240963200, 
1241049600, 1241136000, 1241395200, 1241481600, 1241568000, 1241654400, 
1241740800, 1.242e+09, 1242086400, 1242172800, 1242259200, 1242345600, 
1242604800, 1242691200, 1242777600, 1242864000, 1242950400, 1243209600, 
1243296000, 1243382400, 1243468800, 1243555200, 1243814400, 1243900800, 
1243987200, 1244073600, 1244160000, 1244419200, 1244505600, 1244592000, 
1244678400, 1244764800, 1245024000, 1245110400, 1245196800, 1245283200, 
1245369600, 1245628800, 1245715200, 1245801600, 1245888000, 1245974400),
tzone = "UTC", tclass = "Date"), class = c("xts", "zoo"), .indexCLASS = "Date",
tclass = "Date", .indexTZ = "UTC", tzone = "UTC")

1 个答案:

答案 0 :(得分:3)

period.apply(或apply.dailyapply.weekly等)与您自己的自定义功能一起使用。类似的东西:

library(quantmod)  # for Op, Hi, Lo, Cl, and Vo functions
myFun <- function(x) {
  # need coredata, so c.xts will not be dispatched
  cx <- coredata(x)
  c(Open = first(Op(cx)),
    Hi = max(Hi(cx)),
    Low = min(Lo(cx)),
    Close = last(cx[,"Close"]),
    Volume = sum(Vo(cx)),
    Adj.Close = last(cx[,"Adj.Close"]),
    Return = sum(cx[,"Return"]))
}
out <- period.apply(Data, endpoints(Data, "months"), myFun)
#           Open     Hi   Low Close   Volume Adj.Close    Return
# 2009-04-30 64.5 100.25 60.75  92.0 88802000   73.8671 0.0000000
# 2009-05-29 92.0 105.00 87.50  95.5 58597300   76.6773 0.0486306
# 2009-06-26 97.0 110.50 94.00 104.5 54739400   83.9034 0.1222869