ラベル portfolio の投稿を表示しています。 すべての投稿を表示
ラベル portfolio の投稿を表示しています。 すべての投稿を表示

2018年7月22日日曜日

Calculate your mortgage.


CHECK THIS AS WELL

For the case interest rate is 0.875%, months to pay is 26 full years, and the remaining principle is 50,814,556JPY.

See aDFyear for each year and aDFmonth for each month.

> mortgage(P=50814556, I=0.875, L=(26*12), amort=T, plotData=T)

The payments for this loan are:


Monthly payment: ¥182,154.3 (stored in monthPay)
Total cost: ¥56,832,131

The amortization data for each of the 312 months are stored in "aDFmonth".

The amortization data for each of the 312 years are stored in "aDFyear".

> aDFyear
   Amortization Annual_Payment Annual_Principal Annual_Interest Year
1      50814556        2185851          1748224       437627.33    1
2      49066332        2185851          1763582       422268.88    2
3      47302750        2185851          1779076       406775.49    3
4      45523674        2185851          1794705       391146.00    4
                                     <skip>
25      4332107        2185851          2156580        29270.81   25
26      2175526        2185851          2175526        10324.87   26

2018年2月8日木曜日

Calculate NIKKEI225 vol.3



k3 <-  "2007-01-01::2018-02-07"

# or

getSymbols("^GSPC")
k3 <- paste("2007-01-01", index(last(GSPC)),sep="::")
k3

# download other data
getSymbols("NIKKEI225",src="FRED") # download nikkei 225
# getSymbols("DEXJPUS", src = "FRED")
# getSymbols("YJUSDJPY",src="yahooj")
getSymbols('YJUSDJPY', src="yahooj",auto.assign=TRUE)
N225 <- NIKKEI225
# result <summary(lm(to.monthly(N225[k3])[,4] ~  to.monthly(GSPC[k3])[,4] + to.monthly(DEXJPUS[k3])[,4]) 
result_nikkei <- lm(to.monthly(N225[k3])[,4] ~  to.monthly(GSPC[k3])[,4] + to.monthly(YJUSDJPY[k3])[,4])
result_nikkei$coefficients[2]*last(GSPC)[,4]+result_nikkei$coefficients[3]*as.vector(last(YJUSDJPY)[,4])+result_nikkei$coefficients[1]
# plot(merge(as.xts(predict(result_nikkei),index(residuals(result_nikkei))),to.monthly(N225[k3])[,4],residuals(result_nikkei)))
be
ep(2)

plot(merge(as.xts(predict(result_nikkei),index(residuals(result_nikkei))),to.monthly(N225[k3])[,4],residuals(result_nikkei)),main=paste(paste("NIKKEI225 =",round(result_nikkei$coefficients[2],4),"* GSPC +",round(result_nikkei$coefficients[3],2),"*USDJPY +",round(result_nikkei$coefficients[1],2))))

tmp.legend <- paste("R Squared is ",round(summary(result_nikkei)$r.squared,4)," \n","DF is ",round(summary(result_nikkei)$df[2],0),sep=' ')

addLegend(legend.loc = "topleft", legend.names = tmp.legend,col=3)

> result

Call:
lm(formula = to.monthly(N225[k3])[, 4] ~ to.monthly(GSPC[k3])[,
    4] + to.monthly(YJUSDJPY[k3])[, 4])

Residuals:
     Min       1Q   Median       3Q      Max
-1875.41  -473.74   -12.89   507.10  1855.32

Coefficients:
                                Estimate Std. Error t value Pr(>|t|) 
(Intercept)                   -9864.4555   499.7539  -19.74   <2e-16 ***
to.monthly(GSPC[k3])[, 4]         4.8991     0.1804   27.16   <2e-16 ***
to.monthly(YJUSDJPY[k3])[, 4]   158.2244     6.1444   25.75   <2e-16 ***
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 792.4 on 131 degrees of freedom
Multiple R-squared:  0.9655, Adjusted R-squared:  0.965
F-statistic:  1832 on 2 and 131 DF,  p-value: < 2.2e-16


summary(lm(apply.weekly(N225[k3],mean) ~  apply.weekly(GSPC[k3][,4],mean) + apply.weekly(YJUSDJPY[k3][,4],mean)))   

> summary(lm(apply.weekly(N225[k3],mean) ~  apply.weekly(GSPC[k3][,4],mean) + apply.weekly(YJUSDJPY[k3][,4],mean)))   

Call:
lm(formula = apply.weekly(N225[k3], mean) ~ apply.weekly(GSPC[k3][,
    4], mean) + apply.weekly(YJUSDJPY[k3][, 4], mean))

Residuals:
     Min       1Q   Median       3Q      Max
-1992.66  -510.50    51.79   469.11  2524.40

Coefficients:
                                        Estimate Std. Error t value Pr(>|t|) 
(Intercept)                           -1.017e+04  2.593e+02  -39.24   <2e-16 ***
apply.weekly(GSPC[k3][, 4], mean)      4.800e+00  9.568e-02   50.17   <2e-16 ***
apply.weekly(YJUSDJPY[k3][, 4], mean)  1.626e+02  3.180e+00   51.11   <2e-16 ***
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 754.9 on 445 degrees of freedom
  (132 observations deleted due to missingness)
Multiple R-squared:  0.9674, Adjusted R-squared:  0.9673
F-statistic:  6612 on 2 and 445 DF,  p-value: < 2.2e-16

# plot(merge(na.omit(apply.weekly(N225[k3],mean))[2:length(na.omit(apply.weekly(N225[k3],mean)))],predict(lm(apply.weekly(N225[k3],mean) ~  apply.weekly(GSPC[k3][,4],mean) + apply.weekly(YJUSDJPY[k3][,4],mean)))))

result <- lm(apply.weekly(N225[k3],mean) ~  apply.weekly(GSPC[k3][,4],mean) + apply.weekly(YJUSDJPY[k3][,4],mean)) 
tmp.main <- paste("NIKKEI225 =",round(result$coefficients[2],4),"* GSPC +",round(result$coefficients[3],2),"*USDJPY +",round(result$coefficients[1],2))
nikkei_week <- na.omit(apply.weekly(N225[k3],mean))
predict_nikkei_week <- predict(lm(apply.weekly(N225[k3],mean) ~  apply.weekly(GSPC[k3][,4],mean) + apply.weekly(YJUSDJPY[k3][,4],mean)))
if(length(nikkei_week) > length(predict_nikkei_week)){
  plot(merge(last(nikkei_week,n=length(predict_nikkei_week),predict_nikkei_week)),main=tmp.main)
}else{
  plot(merge(nikkei_week,last(predict_nikkei_week,n=length(nikkei_week))),main=tmp.main) 
}
# add legend at topleft
#
#
addLegend(legend.loc = "topleft", legend.names = paste("R Squared",round(summary(result)$r.squared,4),sep=':'),col=2)

or

tmp.legend <- paste("R Squared is ",round(summary(result)$r.squared,4)," \n","DF is ",round(summary(result)$df[2],0),sep=' ')
addLegend(legend.loc = "topleft", legend.names = tmp.legend,col=3)



In spite of the recent steep declines, Nikkei225 is still in the premium side.




use grid.ticks.on='months' to show grid on monthly basis.


2018年1月15日月曜日

purchase history.

---------------------------------------------------------------------------------------------

Fully Executed 13 Jan 2014 Buy
P-500589 FAS 300 shares
Outstanding Qty: 0 shares
Total Executed Qty: 300 shares 91.3799 USD
--------------------------------------------------------------------------------------------
Fully Executed 09 Jan 2014 Buy
P-449838 FAS 100 shares
Outstanding Qty: 0 shares
Total Executed Qty: 100 shares 91.6699 USD
---------------------------------------------------------------------------------------------
Fully Executed 09 Jan 2014 Buy
P-449832 SPXL 300 shares
Outstanding Qty: 0 shares
Total Executed Qty: 300 shares 62.2399 USD
---------------------------------------------------------------------------------------------
Fully Executed 02 Jan 2014 Buy
P-296549 SPXL 200 shares
Outstanding Qty: 0 shares
Total Executed Qty: 200 shares 62.56 USD
----------------------------------------------------------------------------------------------
Fully Executed 30 Dec 2013 Buy
P-262561 SPXL 500 shares
Outstanding Qty: 0 shares
Total Executed Qty: 500 shares 62.93 USD
---------------------------------------------------------------------------------------------
Fully Executed 26 Dec 2013 Buy
P-878321 FAS 500 shares
Outstanding Qty: 0 shares
Total Executed Qty: 500 shares 89.81 USD
---------------------------------------------------------------------------------------------
Fully Executed 26 Dec 2013 Buy
P-878316 SPXL 1,000 shares
Outstanding Qty: 0 shares
Total Executed Qty: 1,000 shares 62.66 USD
---------------------------------------------------------------------------------------------
Fully Executed 23 Dec 2013 Buy
P-864555 SPXL 500 shares
Outstanding Qty: 0 shares
Total Executed Qty: 500 shares 61.57 USD
------------------------------------------------------------------------------------------------
Fully Executed 23 Dec 2013 Buy
P-864552 FAS 500 shares
Outstanding Qty: 0 shares
Total Executed Qty: 500 shares 88.50 USD
------------------------------------------------------------------------------------------------
Fully Executed 20 Dec 2013 Buy
P-844082 FAS 500 shares
Outstanding Qty: 0 shares
Total Executed Qty: 500 shares 86.00 USD
--------------- -------------- ---------- ----- -----------------
Fully Executed 08 Dec 2013 Buy
P-600562 SPXL 500 shares
Outstanding Qty: 0 shares
Total Executed Qty: 500 shares 59.85 USD
-------------------------------------------------------------------------------------------------
Fully Executed 06 Dec 2013 Buy
P-600562(598416001) SPXL 500 shares
Outstanding Qty: 0 shares
Total Executed Qty: 500 shares 58.80USD



89.81 * 500 + 62.66 * 1000 + 61.57 * 500 + 88.5 * 500 + 86 * 500 + 59.85 * 500 + 58.8 * 500 + 62.93 * 500 + 62.56 * 200
=328902.00

62.66 * 1000 + 61.57 * 500 + 59.85 * 500 + 58.8 * 500  + 62.93 * 500   + 62.56 * 200 + 62.2399 * 300
=215418.97
215418.97 / 3500
= 61.55

89.81 * 500 + 88.5 * 500 + 86 * 500  + 91.6699 * 100 + 91.3799 * 300
= 168735.9600
168735.9600 / 1900
=88.81

3000 * 63.8
=191400.0
1500 * 90.30
=135450.00

2018年1月12日金曜日

managing portfolio - building portfolio table. - vol3


update the script to calculate both USD and JPY total.

x <- as.data.frame(tapply(read.csv("~/f4.csv")$quantity,read.csv("~/f4.csv")$ticker,sum))
colnames(x)[1] <- "shares"
z <- as.data.frame(tapply(read.csv("~/f4.csv")$quantity,read.csv("~/f4.csv")$ticker,sum))
colnames(z)[1] <- "shares"
z2 <- transform(z,ticker=rownames(z))
my_pf <- transform(z2,currency=mapply(function(x) read.csv("~/f4.csv")[grep(x,read.csv("~/f4.csv")$ticker),5][1],my_pf$ticker))
mapply(my_getsymbols,my_pf$currency,my_pf$ticker)     
my_pf <- transform(my_pf,total=mapply(function(x,y) x*last(get(as.character(y))[,4]),z2$shares,z2$ticker))
tapply(my_pf$total,my_pf$currency,sum)                 
getFX("USD/JPY")                                   
print("JPY TOTAL")
tapply(my_pf$total,my_pf$currency,sum)["JPY"] + tapply(my_pf$total,my_pf$currency,sum)["USD"] * last(USDJPY)
print("USD TOTAL")
tapply(my_pf$total,my_pf$currency,sum)["JPY"] /  last(USDJPY) + tapply(my_pf$total,my_pf$currency,sum)["USD"]
beep(2)

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

2017年12月5日火曜日

managing portfolio - building portfolio table. - vol2

x <- as.data.frame(tapply(read.csv("~/f4.csv")$quantity,read.csv("~/f4.csv")$ticker,sum))
colnames(x)[1] <- "shares"
z <- as.data.frame(tapply(read.csv("~/f4.csv")$quantity,read.csv("~/f4.csv")$ticker,sum))
colnames(z)[1] <- "shares"
z2 <- transform(z,ticker=rownames(z))
my_pf <- transform(z2,currency=mapply(function(x) read.csv("~/f4.csv")[grep(x,read.csv("~/f4.csv")$ticker),5][1],my_pf$ticker))
mapply(my_getsymbols,my_pf$currency,my_pf$ticker)
my_pf <- transform(my_pf,total=mapply(function(x,y) x*last(get(as.character(y))[,4]),z2$shares,z2$ticker))
tapply(my_pf$total,my_pf$currency,sum)
getFX("USD/JPY")
tapply(my_pf$total,my_pf$currency,sum)["JPY"] + tapply(my_pf$total,my_pf$currency,sum)["USD"] * last(USDJPY)
beep(2)