2017年12月22日金曜日

calculate portfolio historical record after 2017-12-15


# omit getsymbols when data are up-to-dated.
getSymbols('FAS',src="yahoo")
getSymbols('SPXL',src="yahoo")

# built time series for FAS share holding.
l <- seq(as.Date("2014-01-01"),Sys.Date(),by="days")
fas_shares <- (as.xts(rep(3472*4,length(l)),l))

# sold 750 shares at 2016-12-19
l2 <- seq(as.Date("2016-12-19"),Sys.Date(),by="days")
s2 <- c(rep(0,length(l)-length(l2)),rep(750,length(l2)))
# build time series record for materialized cash after 2016-12-19
#
# 41.44 was the price at the end of 2016-12-09
c2 <- c(rep(0,length(l)-length(l2)),rep(750*41.44,length(l2)))

# sold 638 shares at 2017-12-04
l3 <- seq(as.Date("2017-12-04"),Sys.Date(),by="days")
s3 <- c(rep(0,length(l)-length(l3)),rep(638,length(l3)))
# record for materialized cash after 2017-12-04
# 69.3 was the price to be soldl at  2017-12-04.
c3 <- c(rep(0,length(l)-length(l3)),rep(638*69.3,length(l3)))
# special capital gain divident for SPXL $1.6. 10% US income tax for Firstrade
#
l4 <- seq(as.Date("2017-12-15"),Sys.Date(),by="days")
c4 <- c(rep(0,length(l)-length(l4)),rep(23412+9326,length(l4)))
# calculate time series for FAS
#
# take changes of ownership by 2016-12-19 and 2017-12-04 trade
#
fas_shares <- fas_shares - s2
fas_shares <- fas_shares - s3

# calculate time series for SPXL
spxl_shares <- as.xts(rep(21000,length(l)),l)

# candleChart(to.weekly(fas_shares * FAS[,4] +spxl_shares * SPXL[,4]),theme='white')

candleChart(to.weekly(fas_shares * FAS[,4] +spxl_shares * SPXL[,4]+as.xts(c2+c3+c4,index(fas_shares))),theme='white')

# store xts data into weekly_pf

weekly_pf <- to.weekly(fas_shares * FAS[,4] +spxl_shares * SPXL[,4]+as.xts(c2+c3+c4,index(fas_shares)))
colnames(weekly_pf)[1] <- 'open'
colnames(weekly_pf)[2] <- 'high'
colnames(weekly_pf)[3] <- 'low'
colnames(weekly_pf)[4] <- 'close'

Add the horizontal line if you like.

# add the horizontal line
> tmp <- as.xts(rep(2200000,length(index(weekly_pf))),index(weekly_pf))
> addTA(tmp,on=1,legend="tmp")
# add slope.
> length(index(weekly_pf))
[1] 243
> addTA(as.xts(approx(seq(1,2,1),c(600000,1100000),n=243,method="linear")$y,index(weekly_pf)),on=1,name='2100000')
# add the vertical line at "2018-01-05"
> length(weekly_pf[,1][paste(index(weekly_pf[1]),index(weekly_pf["2018"][1]),sep="::")])
> index(weekly_pf)[210]
[1] "2018-01-05"
> plot(addLines(v=210))
# calculate sequence # of the first week of each year.
>  for(i in seq(2014,2018,1)){cat(i);cat(" ");cat(length(weekly_pf[,1][paste(head(index(weekly_pf),n=1),head(index(weekly_pf[as.character(i)]),n=1),sep="::")]));cat("\n")}
2014 1
2015 53
2016 106
2017 158
2018 210 
> plot(addLines(v=c(106,158,210)))
> weekly_pf[,4][c(106,158,210)]
               close
2016-01-08  684234.9
2017-01-06 1189421.8
2018-01-05 1991231.4

0 件のコメント: