2017年9月29日金曜日

Calculate my portfolio vol.3

# ticker,Date,sell/buy,quantity
# "FAS","2014-01-01","buy",13888
# "FAS","2016-12-19","sell",750

x <- vector("list",length(read.csv("~/fas.csv")[,1]))
z <- read.csv("~/fas.csv")

# ticker       Date sell.buy quantity
# 1    FAS 2014-01-01      buy    13888
# 2    FAS 2016-12-19     sell      750
#
#  > mapply(my_assign,z$ticker)
#
my_assign <- function(t){
  print("0")
  output <- as.character(paste(t,"hold",sep="_"))
  # print(c("1",output))
  if(!exists(output)){
    assign(output,c(),env=.GlobalEnv)
  }
  output <- as.character(paste(t,"xts",sep="_"))
  # print(c("1",output))
  if(!exists(output)){
    assign(output,c(),env=.GlobalEnv)
  }
}
#
# mapply(my_hist,z$ticker,z$quantity,z$Date,"2014-01-01")
#
my_hist <- function(t,q,d,s){
  l <- seq(as.Date(s),Sys.Date(),by="days")
  output <- as.character(paste(t,"hold",sep="_"))
  print(d)
  print(s)
  assign(output,c(get(output),
  rep(0,length(l)-length(seq(as.Date(as.character(d)),Sys.Date(),by="days"))),
  rep(q,length(seq(as.Date(as.character(d)),Sys.Date(),by="days")))),env=.GlobalEnv)
  # as.xts(rowSums(matrix(get(output),ncol=length(get(output))/length(l))),l) * apply.monthly(get(as.character(t)),last)[,4]

}
my_xts <- function(t){
    output <- as.character(paste(t,"hold",sep="_"))
    assign(as.character(paste(t,"xts",sep="_")),as.xts(rowSums(matrix(get(output),ncol=length(get(output))/length(l))),l) * apply.monthly(get(as.character(t)),last)[,4],env=.GlobalEnv)
}
#
# > ticker_list <- unique(z$ticker)
# [1] "FAS"  "SPXL"
# > ticker_list
# [1] "FAS"  "SPXL"
#
lapply(ticker_list,my_xts)

0 件のコメント: