[英]How to find the monthly return of stocks using data.table in R?
I have two months of data for two stocks as follows -我有两个股票的两个月数据如下 -
dt <- structure(list(date = structure(c(18718, 18722, 18723, 18724,
18725, 18726, 18729, 18730, 18731, 18732, 18733, 18736, 18737,
18738, 18739, 18740, 18743, 18744, 18745, 18746, 18747, 18750,
18751, 18752, 18753, 18754, 18757, 18758, 18759, 18760, 18761,
18764, 18765, 18766, 18767, 18768, 18771, 18772, 18773, 18774,
18778, 18718, 18722, 18723, 18724, 18725, 18726, 18729, 18730,
18731, 18732, 18733, 18736, 18737, 18738, 18739, 18740, 18743,
18744, 18745, 18746, 18747, 18750, 18751, 18752, 18753, 18754,
18757, 18758, 18759, 18760, 18761, 18764, 18765, 18766, 18767,
18768, 18771, 18772, 18773, 18774, 18778), class = "Date"),
close = c(123,
125.9, 126.21, 127.9, 130.36, 132.995, 131.24, 134.43, 132.03,
134.5, 134.16, 134.84, 133.11, 133.5, 131.94, 134.32, 134.72,
134.39, 133.58, 133.48, 131.46, 132.54, 127.85, 128.1, 129.74,
130.21, 126.85, 125.91, 122.77, 124.97, 127.45, 126.27, 124.85,
124.69, 127.31, 125.43, 127.1, 126.9, 126.85, 125.28, 124.61,
2137.75, 2225.55, 2224.75, 2249.68, 2265.44, 2285.88, 2254.79,
2267.27, 2254.84, 2296.66, 2297.76, 2302.4, 2293.63, 2293.29,
2267.92, 2315.3, 2326.74, 2307.12, 2379.91, 2429.89, 2410.12,
2395.17, 2354.25, 2356.74, 2381.35, 2398.69, 2341.66, 2308.76,
2239.08, 2261.97, 2316.16, 2321.41, 2303.43, 2308.71, 2356.09,
2345.1, 2406.67, 2409.07, 2433.53, 2402.51, 2411.56),
ticker = c("AAPL",
"AAPL", "AAPL", "AAPL", "AAPL", "AAPL", "AAPL", "AAPL", "AAPL",
"AAPL", "AAPL", "AAPL", "AAPL", "AAPL", "AAPL", "AAPL", "AAPL",
"AAPL", "AAPL", "AAPL", "AAPL", "AAPL", "AAPL", "AAPL", "AAPL",
"AAPL", "AAPL", "AAPL", "AAPL", "AAPL", "AAPL", "AAPL", "AAPL",
"AAPL", "AAPL", "AAPL", "AAPL", "AAPL", "AAPL", "AAPL", "AAPL",
"GOOG", "GOOG", "GOOG", "GOOG", "GOOG", "GOOG", "GOOG", "GOOG",
"GOOG", "GOOG", "GOOG", "GOOG", "GOOG", "GOOG", "GOOG", "GOOG",
"GOOG", "GOOG", "GOOG", "GOOG", "GOOG", "GOOG", "GOOG", "GOOG",
"GOOG", "GOOG", "GOOG", "GOOG", "GOOG", "GOOG", "GOOG", "GOOG",
"GOOG", "GOOG", "GOOG", "GOOG", "GOOG", "GOOG", "GOOG", "GOOG",
"GOOG")), row.names = c(NA, -82L), class = c("data.table", "data.frame"
))
I want to find the monthly return of these stocks using only data.table.我想仅使用 data.table 找到这些股票的月收益。 Is there any existing function or an easy way to accomplish this?
是否有任何现有的 function 或实现此目的的简单方法?
I have tried to solve it using the following code, but it is giving error -我尝试使用以下代码解决它,但它给出了错误 -
dt[, return := rep(periodReturn(.SD, period = 'monthly', type = "arithmetic"), .N), by = .(ticker)]
Here is the error这是错误
Error in `[.data.table`(dt, , `:=`(return, rep(periodReturn(.SD, period = "monthly", :
Supplied 82 items to be assigned to group 1 of size 41 in column 'return'. The RHS length must either be 1 (single values are ok) or match the LHS length exactly. If you wish to 'recycle' the RHS please use rep() explicitly to make this intent clear to readers of your code.
Any insights will be helpful.任何见解都会有所帮助。
The expected output is预期的 output 是
ticker month return
AAPL 4 0.06878049
AAPL 5 -0.05210710
GOOG 4 0.1274096597
GOOG 5 0.0005974806
Perhaps, the error with size mismatch can be solved if we specify the length.out
in rep
如果我们在
rep
中指定length.out
,也许可以解决大小不匹配的错误
library(data.table)
library(quantmod)
dt[, return := rep(periodReturn(.SD, period = 'monthly',
type = "arithmetic"), length.out = .N), by = .(ticker)]
-output -输出
dt
date close ticker return
1: 2021-04-01 123.000 AAPL 0.0687804878
2: 2021-04-05 125.900 AAPL -0.0521071048
3: 2021-04-06 126.210 AAPL 0.0687804878
4: 2021-04-07 127.900 AAPL -0.0521071048
5: 2021-04-08 130.360 AAPL 0.0687804878
6: 2021-04-09 132.995 AAPL -0.0521071048
7: 2021-04-12 131.240 AAPL 0.0687804878
8: 2021-04-13 134.430 AAPL -0.0521071048
9: 2021-04-14 132.030 AAPL 0.0687804878
10: 2021-04-15 134.500 AAPL -0.0521071048
11: 2021-04-16 134.160 AAPL 0.0687804878
12: 2021-04-19 134.840 AAPL -0.0521071048
13: 2021-04-20 133.110 AAPL 0.0687804878
14: 2021-04-21 133.500 AAPL -0.0521071048
15: 2021-04-22 131.940 AAPL 0.0687804878
16: 2021-04-23 134.320 AAPL -0.0521071048
17: 2021-04-26 134.720 AAPL 0.0687804878
18: 2021-04-27 134.390 AAPL -0.0521071048
19: 2021-04-28 133.580 AAPL 0.0687804878
20: 2021-04-29 133.480 AAPL -0.0521071048
21: 2021-04-30 131.460 AAPL 0.0687804878
22: 2021-05-03 132.540 AAPL -0.0521071048
23: 2021-05-04 127.850 AAPL 0.0687804878
24: 2021-05-05 128.100 AAPL -0.0521071048
25: 2021-05-06 129.740 AAPL 0.0687804878
26: 2021-05-07 130.210 AAPL -0.0521071048
27: 2021-05-10 126.850 AAPL 0.0687804878
28: 2021-05-11 125.910 AAPL -0.0521071048
29: 2021-05-12 122.770 AAPL 0.0687804878
30: 2021-05-13 124.970 AAPL -0.0521071048
31: 2021-05-14 127.450 AAPL 0.0687804878
32: 2021-05-17 126.270 AAPL -0.0521071048
33: 2021-05-18 124.850 AAPL 0.0687804878
34: 2021-05-19 124.690 AAPL -0.0521071048
35: 2021-05-20 127.310 AAPL 0.0687804878
36: 2021-05-21 125.430 AAPL -0.0521071048
37: 2021-05-24 127.100 AAPL 0.0687804878
38: 2021-05-25 126.900 AAPL -0.0521071048
39: 2021-05-26 126.850 AAPL 0.0687804878
40: 2021-05-27 125.280 AAPL -0.0521071048
41: 2021-05-31 124.610 AAPL 0.0687804878
42: 2021-04-01 2137.750 GOOG 0.1274096597
43: 2021-04-05 2225.550 GOOG 0.0005974806
44: 2021-04-06 2224.750 GOOG 0.1274096597
45: 2021-04-07 2249.680 GOOG 0.0005974806
46: 2021-04-08 2265.440 GOOG 0.1274096597
47: 2021-04-09 2285.880 GOOG 0.0005974806
48: 2021-04-12 2254.790 GOOG 0.1274096597
49: 2021-04-13 2267.270 GOOG 0.0005974806
50: 2021-04-14 2254.840 GOOG 0.1274096597
51: 2021-04-15 2296.660 GOOG 0.0005974806
52: 2021-04-16 2297.760 GOOG 0.1274096597
53: 2021-04-19 2302.400 GOOG 0.0005974806
54: 2021-04-20 2293.630 GOOG 0.1274096597
55: 2021-04-21 2293.290 GOOG 0.0005974806
56: 2021-04-22 2267.920 GOOG 0.1274096597
57: 2021-04-23 2315.300 GOOG 0.0005974806
58: 2021-04-26 2326.740 GOOG 0.1274096597
59: 2021-04-27 2307.120 GOOG 0.0005974806
60: 2021-04-28 2379.910 GOOG 0.1274096597
61: 2021-04-29 2429.890 GOOG 0.0005974806
62: 2021-04-30 2410.120 GOOG 0.1274096597
63: 2021-05-03 2395.170 GOOG 0.0005974806
64: 2021-05-04 2354.250 GOOG 0.1274096597
65: 2021-05-05 2356.740 GOOG 0.0005974806
66: 2021-05-06 2381.350 GOOG 0.1274096597
67: 2021-05-07 2398.690 GOOG 0.0005974806
68: 2021-05-10 2341.660 GOOG 0.1274096597
69: 2021-05-11 2308.760 GOOG 0.0005974806
70: 2021-05-12 2239.080 GOOG 0.1274096597
71: 2021-05-13 2261.970 GOOG 0.0005974806
72: 2021-05-14 2316.160 GOOG 0.1274096597
73: 2021-05-17 2321.410 GOOG 0.0005974806
74: 2021-05-18 2303.430 GOOG 0.1274096597
75: 2021-05-19 2308.710 GOOG 0.0005974806
76: 2021-05-20 2356.090 GOOG 0.1274096597
77: 2021-05-21 2345.100 GOOG 0.0005974806
78: 2021-05-24 2406.670 GOOG 0.1274096597
79: 2021-05-25 2409.070 GOOG 0.0005974806
80: 2021-05-26 2433.530 GOOG 0.1274096597
81: 2021-05-27 2402.510 GOOG 0.0005974806
82: 2021-05-31 2411.560 GOOG 0.1274096597
If we want to summarise, wrap it in a list
as xts
attribute built on top of matrix
from periodReturn
may need it to be blocked within a list
.如果我们想总结一下,将它包装在一个
list
,因为在periodReturn
的matrix
之上构建的xts
属性可能需要它被阻止在一个list
。 When we use rep
, it strips off the xts/matrix
attribute and the resulting column is numeric
vector
当我们使用
rep
时,它会xts/matrix
属性,结果列是numeric
vector
dt[, .(return = .(periodReturn(.SD, period = 'monthly',
type = "arithmetic"))), .(ticker)]
ticker return
1: AAPL 0.06878049,-0.05210710
2: GOOG 0.1274096597,0.0005974806
Or remove the xts
attribute by converting to numeric
and it should work或者通过转换为
numeric
来删除xts
属性,它应该可以工作
library(lubridate)
dt[, .(month = unique(month(date)),
return = as.numeric(periodReturn(.SD, period = 'monthly',
type = "arithmetic"))), .(ticker)]
ticker month return
1: AAPL 4 0.0687804878
2: AAPL 5 -0.0521071048
3: GOOG 4 0.1274096597
4: GOOG 5 0.0005974806
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.