简体   繁体   English

按名称的累积投资组合表现

[英]Cumulative Portfolio performance by Name

This is what my data.frame looks like.这就是我的 data.frame 的样子。 The rightmost column(Performance) is my desired column.最右边的列(性能)是我想要的列。

library(data.table)
    dt <- fread('
    Name      FundName     SharePrice   TotalShares   PurchaseDate   Performance
    John       A               10           500          2016-01-01       0%   
    John       A               20           1000         2016-02-01       20%     
    John       A               10           1500         2016-03-01      -25%%          
    John       B               30           500          2016-04-01      -18.18%       
    John       B               60           1000         2016-05-01       4.16%       
    Tom        A               10           500          2016-01-01       0%   
    Tom        A               20           1000         2016-02-01       20%     
    Tom        A               10           1500         2016-03-01      -25%%          
    Tom        B               30           500          2016-04-01      -18.18%       
    Tom        B               60           1000         2016-05-01       4.16%                    
      ')
  • For the first row, John bought 500 shares at the price of $10.在第一行,约翰以 10 美元的价格购买了 500 股。 So he spent 5000 bucks and he can sell his position for 5000 bucks that day.所以他花了5000块钱,那天他可以卖掉他的头寸5000块钱。 So the performance for the first row is 0%.所以第一行的性能是 0%。
  • For the second row, John spent total of (10*500)+(20*1000)=25000 and he can sell his position for (20 * 1500 shares) or 30000 bucks.对于第二行,John 总共花费了 (10*500)+(20*1000)=25000,他可以以(20 * 1500 股)或 30000 美元的价格出售他的头寸。 So his cumulative performance is (30000-25000)/25000 or 20%.所以他的累积表现是(30000-25000)/25000或20%。
  • For third row he spent total of (10*500+20*1000+10*1500)=40000 bucks and can sell for (10*3000 shares) or 30000 bucks, thereby the performance of -25%.第三排他总共花了(10*500+20*1000+10*1500)=40000块钱,可以卖出(10*3000股)或30000块钱,从而表现-25%。
  • Now for the 4th row, there are two funds(A&B);现在第四行,有两只基金(A&B); he can sell his A position for 30000 still as I am assuming the price of A is still $10 a share, and he bought his new B position for (30*500)=15000.他仍然可以以 30000 的价格卖出他的 A 头寸,因为我假设 A 的价格仍然是每股 10 美元,而他以 (30*500)=15000 的价格买入了新的 B 头寸。 So he can sell his position of both A & B for (30000+15000)=45000 but he spent(40000+15000) or 55000 so far.因此,他可以以 (30000+15000)=45000 的价格出售 A 和 B 的头寸,但到目前为止他已经花费了 (40000+15000) 或 55000。 so his his cumulative performance for the 4th row is (55000-45000)/55000 or -18.18%.所以他在第四排的累积表现是(55000-45000)/55000或-18.18%。

I hope it makes sense.我希望这是有道理的。 I am struggling with tracking the price of both funds when trying to calculate the cumulative performance.在尝试计算累积表现时,我正在努力跟踪两只基金的价格。 Thank you for your help.感谢您的帮助。

I'd expand the data to cover all Date-Fund combos for each person:我将扩展数据以涵盖每个人的所有 Date-Fund 组合:

dt_skel = dt[, do.call(CJ, c(.SD, unique=TRUE)), 
  by=Name, .SDcols=c("FundName", "PurchaseDate")]

dt_full = dt[dt_skel, on=names(dt_skel)]
dt_full[ is.na(TotalShares), TotalShares := 0L]
dt_full[ , SharePrice := SharePrice[1L], by=.(Name, FundName, cumsum(!is.na(SharePrice)))]

Then aggregate然后聚合

res = dt_full[!is.na(SharePrice), .(
  PurchaseDate,
  spent = cumsum(TotalShares*SharePrice),
  value = cumsum(TotalShares)*SharePrice
), by=.(Name, FundName)][, .(
  value = sum(value),
  spent = sum(spent)
), by=.(Name, PurchaseDate)]


    Name PurchaseDate  value  spent
 1: John   2016-01-01   5000   5000
 2: John   2016-02-01  30000  25000
 3: John   2016-03-01  30000  40000
 4: John   2016-04-01  45000  55000
 5: John   2016-05-01 120000 115000
 6:  Tom   2016-01-01   5000   5000
 7:  Tom   2016-02-01  30000  25000
 8:  Tom   2016-03-01  30000  40000
 9:  Tom   2016-04-01  45000  55000
10:  Tom   2016-05-01 120000 115000

To add the performance metric to the original transactions table:将性能指标添加到原始交易表中:

dt[res, ret := value/spent - 1, on=c("Name, PurchaseDate")]

Assuming dates are always monthly, you can make dt_skel smaller with假设日期总是每月,你可以用dt_skel变小

dt_skel = dt[, MaxDate := max(PurchaseDate), by=Name][, 
  seq(from = PurchaseDate[1L], to =MaxDate[1L], by="month"), by=.(Name, FundName)]

Of course, dates should be formatted as Date or IDate for this to work.当然,日期应该格式化为DateIDate才能正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 R:计算投资组合的累积回报 - R: Calculating cumulative return of a portfolio 即时构建吸墨纸投资组合的名称 - Construct name of blotter portfolio on the fly 如何使用 tidyquant(性能分析)来计算投资组合中的投资组合统计数据,该投资组合具有不同时期的资产 - How to use tidyquant (performance analytics) to calculate portfolio statistics in a portfolio with varying assets by period 如何在投资组合绩效图中仅选择有效的前沿点? - How to pick only efficient frontier points in a plot of portfolio performance? data.table中的扩展窗口(累积计算):如何提高性能 - Expanding window (cumulative calculation) in data.table: how to improve performance 计算由分号按名称分组的累积唯一因子 - Count cumulative unique factors separated by semicolon Grouped by Name 投资组合优化 - Portfolio optimization R 中从一个列名到另一个列名的行的累积总和,然后将单元格除以另一个值 - Cumulative sum of rows in R from one column name until another column name, then divide cells by another value 营业额限制不适用于投资组合分析的投资组合优化 - Turnover Contraint not working in Portfolio Optimization with Portfolio Analytics 长变量名是否会降低r的性能? - Does a long variable name slow performance in r?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM