2017年12月5日火曜日

calculate portfolio historical record after 2017-12-04



# retrieve price data from gogole
getSymbols('FAS',src="google")
getSymbols('SPXL',src="google")

# 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)))
# 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,index(fas_shares))),theme='white')

fas_shares * apply.monthly(FAS,last)[,4]
spxl_shares * apply.monthly(SPXL,last)[,4]

fas_shares * apply.monthly(FAS,last)[,4] +spxl_shares * apply.monthly(SPXL,last)[,4] #monthly historcal price

# weekly closed price chart
plot(fas_shares * apply.weekly(FAS,last)[,4] +spxl_shares * apply.weekly(SPXL,last)[,4])

# store weekly balance
weekly_pf <- fas_shares * apply.weekly(FAS,last)[,4] +spxl_shares * apply.weekly(SPXL,last)[,4]

0 件のコメント: