我想在我的dataframe
中计算收益
Type Date Open Qty Price Amount
7 Bought 2018-06-28 20:47:39 UTC 0.01 667272.13336 6672.7213336
8 Sold 2018-06-28 20:47:48 UTC 0.0078 667514.96 5206.616688
9 Sold 2018-06-28 20:47:48 UTC 0.0022 667514.96 1468.532912
10 Bought 2018-06-28 20:48:17 UTC 0.01 668624.18413 6686.2418413
11 Sold 2018-06-28 20:48:33 UTC 0.005 668424.97 3342.12485
12 Sold 2018-06-28 20:48:35 UTC 0.005 668435.82 3342.1791
13 Bought 2018-06-28 20:48:50 UTC 0.01 667898.67 6678.9867
14 Sold 2018-06-28 20:48:58 UTC 0.01 667881.57 6678.8157
对于第7行,第8行和第9行,第7行返回第8行和第9行的平均价格,第11行,第12行和第13行相同。
有时候我可以有3个bought
和1个Sold
或4个sold
和1个bought
,但是数量总是在bought
和sold
之间匹配
这里是更长的数据帧。
9 Sold 2018-06-28 20:47:48 UTC 0.0022 667514.96 1468.532912
10 Bought 2018-06-28 20:48:17 UTC 0.01 668624.18413 6686.2418413
11 Sold 2018-06-28 20:48:33 UTC 0.005 668424.97 3342.12485
12 Sold 2018-06-28 20:48:35 UTC 0.005 668435.82 3342.1791
13 Bought 2018-06-28 20:48:50 UTC 0.01 667898.67 6678.9867
14 Sold 2018-06-28 20:48:58 UTC 0.01 667881.57 6678.8157
15 Bought 2018-06-28 20:49:54 UTC 0.01 668941.24 6689.4124
16 Sold 2018-06-28 20:50:01 UTC 0.01 668895.62 6688.9562
17 Bought 2018-06-28 20:50:09 UTC 0.01 668363.00995 6683.6300995
18 Sold 2018-06-28 20:50:16 UTC 0.01 668598.95 6685.9895
19 Bought 2018-06-28 20:50:26 UTC 0.01 668302.02 6683.0202
20 Sold 2018-06-28 20:51:13 UTC 0.01 668048.75 6680.4875
21 Bought 2018-06-28 20:51:21 UTC 0.01 667738.97 6677.3897
22 Sold 2018-06-28 20:51:26 UTC 0.01 667407.38 6674.0738
23 Bought 2018-06-28 20:51:45 UTC 0.01 667202.04 6672.0204
24 Sold 2018-06-28 20:51:51 UTC 0.01 667691.88 6676.9188
25 Bought 2018-06-28 20:51:59 UTC 0.01 667555.03 6675.5503
26 Sold 2018-06-28 20:52:02 UTC 0.01 668399.88746 6683.9988746
27 Bought 2018-06-28 20:53:19 UTC 0.01 669015.02 6690.1502
28 Sold 2018-06-28 20:53:28 UTC 0.01 669273.76 6692.7376
29 Bought 2018-06-28 20:53:42 UTC 0.01 668927.79587 6689.2779587
30 Sold 2018-06-28 20:53:47 UTC 0.01 669060.9 6690.609
31 Bought 2018-06-28 20:53:52 UTC 0.01 668489.0 6684.89
32 Sold 2018-06-28 20:56:04 UTC 0.01 668645.62 6686.4562
33 Bought 2018-06-28 20:56:11 UTC 0.01 668350.04001 6683.5004001
34 Sold 2018-06-28 20:56:15 UTC 0.01 669108.98 6691.0898
35 Bought 2018-06-28 20:57:07 UTC 0.01 669105.02 6691.0502
36 Sold 2018-06-28 20:57:10 UTC 0.01 669413.97 6694.1397
37 Bought 2018-06-28 20:57:13 UTC 0.01 669028.23458 6690.2823458
38 Sold 2018-06-28 20:57:32 UTC 0.01 668769.98 6687.6998
39 Bought 2018-06-28 20:57:58 UTC 0.001 668442.05 668.44205
40 Bought 2018-06-28 20:58:31 UTC 0.009 668602.02 6017.41818
41 Sold 2018-06-28 20:58:35 UTC 0.01 668293.98 6682.9398
42 Bought 2018-06-28 20:59:04 UTC 0.01 668626.02 6686.2602
43 Sold 2018-06-28 20:59:43 UTC 0.01 668892.83 6688.9283
44 Bought 2018-06-28 20:59:54 UTC 0.01 669230.64 6692.3064
45 Sold 2018-06-28 21:02:40 UTC 0.01 668746.96 6687.4696
46 Bought 2018-06-28 21:02:49 UTC 0.01 668019.58 6680.1958
47 Sold 2018-06-28 21:03:43 UTC 0.01 667884.01 6678.8401
50 Bought 2018-06-28 21:08:27 UTC 0.004 666489.20149 2665.95680596
51 Sold 2018-06-28 21:08:33 UTC 0.004 667159.97 2668.63988
52 Bought 2018-06-28 21:09:02 UTC 0.004 666584.0 2666.336
53 Sold 2018-06-28 21:09:08 UTC 0.004 666740.27 2666.96108
54 Bought 2018-06-28 21:09:11 UTC 0.004 666243.0 2664.972
55 Sold 2018-06-28 21:09:35 UTC 0.004 664868.94 2659.47576
56 Bought 2018-06-28 21:09:38 UTC 0.004 664281.02 2657.12408
57 Sold 2018-06-28 21:09:40 UTC 0.004 663341.23067 2653.36492268
58 Bought 2018-06-28 21:09:46 UTC 0.004 663304.22439 2653.21689756
59 Sold 2018-06-28 21:09:51 UTC 0.004 663513.95 2654.0558
60 Bought 2018-06-28 21:10:00 UTC 0.004 662645.73213 2650.58292852
61 Sold 2018-06-28 21:10:03 UTC 0.004 663052.46877 2652.20987508
62 Bought 2018-06-28 21:10:17 UTC 0.004 663231.30528 2652.92522112
63 Sold 2018-06-28 21:10:22 UTC 0.004 662929.97241 2651.71988964
64 Bought 2018-06-28 21:10:25 UTC 0.004 662654.21 2650.61684
65 Sold 2018-06-28 21:10:29 UTC 0.004 662415.95 2649.6638
66 Bought 2018-06-28 21:10:32 UTC 0.00273645 662007.02 1811.549109879
67 Bought 2018-06-28 21:10:34 UTC 0.00126355 661876.02 836.313445071
68 Sold 2018-06-28 21:10:41 UTC 0.004 661164.97 2644.65988
69 Bought 2018-06-28 21:10:45 UTC 0.004 660556.03 2642.22412
70 Sold 2018-06-28 21:10:48 UTC 0.004 660872.98 2643.49192
71 Bought 2018-06-28 21:11:30 UTC 0.004 661167.27 2644.66908
如果有人有主意,谢谢
答案 0 :(得分:0)
一个人可以将Bought
的每次出现的数据分组,然后为每个组计算Sold
和“购买”之间的差之和。
使用dplyr
的解决方案可以是:
library(dplyr)
df %>% group_by(Grp = cumsum(Type=="Bought")) %>%
summarise(Date = first(Date),
ReturnVal = sum(Amount[Type=="Sold"]) / sum(Amount[Type=="Bought"])) %>%
as.data.frame()
# Grp Date ReturnVal
# 1 1 2018-06-28 20:47:39 UTC 1.0003639
# 2 2 2018-06-28 20:48:17 UTC 0.9997102
# 3 3 2018-06-28 20:48:50 UTC 0.9999744
注意:我选择显示1st日期以及摘要数据。可以选择包括其他列。
数据:
df <-read.table(text="
Type Date Qty Price Amount
Bought '2018-06-28 20:47:39 UTC' 0.01 667272.13336 6672.7213336
Sold '2018-06-28 20:47:48 UTC' 0.0078 667514.96 5206.616688
Sold '2018-06-28 20:47:48 UTC' 0.0022 667514.96 1468.532912
Bought '2018-06-28 20:48:17 UTC' 0.01 668624.18413 6686.2418413
Sold '2018-06-28 20:48:33 UTC' 0.005 668424.97 3342.12485
Sold '2018-06-28 20:48:35 UTC' 0.005 668435.82 3342.1791
Bought '2018-06-28 20:48:50 UTC' 0.01 667898.67 6678.9867
Sold '2018-06-28 20:48:58 UTC' 0.01 667881.57 6678.8157",
header = TRUE, stringsAsFactors = FALSE)