2017年9月26日火曜日

Calculate Portfolio market value. and draw the graph.


getSymbols('FAS',src="google")
getSymbols('SPXL',src="google")

l <- seq(as.Date("2014-01-01"),Sys.Date(),by="days")
fas_shares <- (as.xts(rep(3472*4,length(l)),l))

l2 <- seq(as.Date("2016-12-19"),Sys.Date(),by="days")
s2 <- c(rep(0,length(l)-length(l2)),rep(750,length(l2)))

fas_shares <- fas_shares - s2
spxl_shares <- as.xts(rep(21000,length(l)),l)


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])
# weekly candle chart
candleChart(to.weekly(fas_shares * FAS[,4] +spxl_shares * SPXL[,4]),theme='white')

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

0 件のコメント: