2019年5月15日水曜日

2019MAY15



間違い
eps_year_xts[128] <- 167.98
eps_year_xts[127] <- 163.53
eps_year_xts[126] <- 163.53
eps_year_xts[126] <- 159.04
eps_year_xts[125] <- 154.73
eps_year_xts[124] <- 151.44
eps_year_xts[123] <- 140
eps_year_xts[122] <- 137.38
eps_year_xts[121] <- 134.74

正解
eps_year_xts[129] <- 167.98
eps_year_xts[128] <- 163.53
eps_year_xts[127] <- 159.04
eps_year_xts[126] <- 154.73
eps_year_xts[125] <- 151.44
eps_year_xts[124] <- 140
eps_year_xts[123] <- 137.38
eps_year_xts[122] <- 134.74

eps_year_xts[121] <- 132.39

作業前
> eps_year_xts["2018::"]
             [,1]
2018-01-01 115.44
2018-04-01 122.48
2018-07-01 130.39
2018-10-01 132.39
2019-01-01 132.47
2019-04-01 135.11
2019-07-01 137.93
2019-10-01 149.61
2020-01-01 154.77
2020-04-01 159.43
2020-07-01 163.95
2020-10-01 168.40

作業後
> eps_year_xts["2018::"]
             [,1]
2018-01-01 115.44
2018-04-01 122.48
2018-07-01 130.39
2018-10-01 132.39
2019-01-01 134.74
2019-04-01 137.38
2019-07-01 140.00
2019-10-01 151.44
2020-01-01 154.73
2020-04-01 159.04
2020-07-01 163.53
2020-10-01 167.98

2019年5月9日木曜日

.Last <- function()






restore working image in the parent directory with the name "YYYY-MM-DD-HH:MM:SS". the smaple above is 17:00:50 of May 09, 2019.

$pwd
/Users/honomoto/R_proj
$ls
2019-05-09-17:00:50  startup.R           tmp1                work
R                   startup.R~          tmp2

.Last <- function() {
  # save.image(file=paste(getwd(),Sys.time(),sep="/"))
  #
  # save working image in the parent directory. expected the length of the directory name is fixed.
  #
  save.image(file=gsub(" ","-",paste(substr(getwd(),1,22),Sys.time(),sep="/")))
  # 
  # OR store working image at the working directory
  #
  # save.image(file=gsub(" ","-",paste(getwd(),Sys.time(),sep="/")))
  cat("bye bye...\n")
}

2019年5月8日水曜日

cli_5mon.r : draw spiral graph of cli 5 months delta vs. its reading.



do "> func("2011-01-01::",9)" after execute all codes below.

#
# plese refer the latter half of
# https://00819.blogspot.com/2019/03/vix-cli-6-month-delta-and-s.html
# lag is set to  5 months.
#
#   s = start date of the spiral like "1992-01-01::" DON'T FORGET DOUBLE COLON!!
#       Don't set before "1964-01-01"
#   l = length of years like 9. recommend to use equal or less than 9. Don't exceed the current end of data.
#
#   use like  > func("2001-01-01::",5)
#
func <- function(s="2011-01-01::",l=9){

  head_of_record <- "1964-01-01::"
  print(head_of_record)
  offset <- length(seq(as.Date(head_of_record),as.Date(s),by='months'))
  max_length <- length(seq(as.Date(head_of_record),as.Date(index(last(cli_xts))),by='months'))
  len_mon <- l*12-1
  lag_month <- 5
  # when the end period exceeds the current end, adjust # of months and years to avoid the counters go beyond the limit.
  if(offset + len_mon > max_length){
    len_mon <- max_length - offset
    l <- ceiling(len_mon/12) # ceiling is to round up
  }
  # len_mon <- l
  # print(offset)
  # print(len_mon)
  # print(seq(as.Date(head_of_record),as.Date(s),by='months')[offset])
  # print(seq(as.Date(head_of_record),as.Date("2100-01-01"),by='months')[offset+len_mon])

  # print(offset)
  plot.default(na.trim(diff(cli_xts$oecd,lag=lag_month))[head_of_record][offset:(offset+len_mon)],cli_xts$oecd[head_of_record][offset:(offset+len_mon)],type='b')
  # print(offset)
  tmp <- par('usr')
  # par(new=T)
  plot.default(na.trim(diff(cli_xts$oecd,lag=lag_month))[head_of_record][offset:(offset+len_mon)],cli_xts$oecd[head_of_record][offset:(offset+len_mon)],type='b',xlim=c( tmp[1],tmp[2]), ylim=c(tmp[3], tmp[4]),lwd=1,main=paste("from",substr(s,1,10),"for",len_mon+1,"months",sep=" "),ylab="",xlab="")
  par(new=T)
  for(i in seq(0,l-1,1)){
    print(i)
    # print(offset)
    # print(offset+i*12)
    # print(offset+i*12+11)
    par(new=T)
      # when the end period exceeds the current end, adjust # of months to avoid OOB
      # otherwise months to go in each iteration is always 11.
    if(offset+i*12+11 < max_length){
      m <- 11
    }else{
      #adjust months to go as go within max_length.
      m <- max_length - (offset+i*12)
    }
    # print(m)
    # print(max_length)
    # print(len_mon)
    # print(offset)
    plot.default(na.trim(diff(cli_xts$oecd,lag=lag_month))[head_of_record][(offset+i*12):(offset+i*12+m)],cli_xts$oecd[head_of_record][(offset+i*12):(offset+i*12+m)],type='b',xlim=c( tmp[1],tmp[2]), ylim=c(tmp[3], tmp[4]),col=i+1,lwd=2,ylab="",xlab="",axes = F)
    if(i == 6){
      # when i ==6 "yellow" is used for dots, but offers poor visibility. plot 'x' upon them to improve.
      par(new=T)
      plot.default(na.trim(diff(cli_xts$oecd,lag=lag_month))[head_of_record][(offset+i*12):(offset+i*12+m)],cli_xts$oecd[head_of_record][(offset+i*12):(offset+i*12+m)],type='p',xlim=c( tmp[1],tmp[2]), ylim=c(tmp[3], tmp[4]),pch='x',ylab="",xlab="",axes = F)
    }
    par(new=T)
  }
  abline(v=0)
  abline(h=100)
  # abline(v=seq(0.5,-1,-0.1),col=6,lty=3)
  # automatically calculate the upper and lower limit of vline.
  # might be able to use 'floor(tmp[2]*10)/10' instead of round(x,digit=1)
  abline(v=seq(ceiling((tmp[1]*10))/10,floor(tmp[2]*10)/10,0.1),col=6,lty=3)

}
func("2001-01-01::",9)


2019年5月6日月曜日

CLI 5 month delta and 1 month delta vs. SPX price move.


months will be categorized into 4 groups. 1)both 5 month and 1 month delta are positive -> bp 2) both negative -> bm 3) 5month negative and 1 month positive -> mp, and 4) 5 month positive and 1 month negative -> pm.
count # of months when index moves more than given parameter. when the prameter is (0.9,"c"), close is more than 10% lower than open. (0.85,"h"), low is more than 15% lower than high.

the sample below is to calculate # of months when price moved down more than 10% on close base. the index moves down more than 10% 8 time when both delta are minus, whereas never when both are positive.

> func(0.9,"c")
bp [1] 289
bm [1] 287
mp [1] 55
pm [1] 55
pm+bm+mp [1] 397
correction sp5 vs. bm[1] "1973-11-01" "1974-09-01" "1980-03-01" "1998-08-01" "2002-09-01" "2008-10-01" "2009-02-01" "2018-12-01"
correction sp5 vs. bpDate of length 0
correction sp5 vs. pm[1] "1987-10-01"
correction sp5 vs. mpDate of length 0




# delta is parameter relation against month close vs. open, when delta is 0.9.
# close price is 10% down from open
# when m is "c", its open vs. close. in the case of "h", it is high versus low
#
func <- function(delta=0.9,m="c"){

  ind_bp <- index(na.omit(diff(cli_xts$oecd,lag=5))["1962::"])[na.omit(diff(cli_xts$oecd,lag=5))["1962::"] > 0 & na.omit(diff(cli_xts$oecd,lag=1))["1962::"] > 0]

  ind_bm <- index(na.omit(diff(cli_xts$oecd,lag=5))["1962::"])[na.omit(diff(cli_xts$oecd,lag=5))["1962::"] < 0 & na.omit(diff(cli_xts$oecd,lag=1))["1962::"] < 0]

  ind_mp <- index(na.omit(diff(cli_xts$oecd,lag=5))["1962::"])[na.omit(diff(cli_xts$oecd,lag=5))["1962::"] < 0 & na.omit(diff(cli_xts$oecd,lag=1))["1962::"] > 0]

  ind_pm <- index(na.omit(diff(cli_xts$oecd,lag=5))["1962::"])[na.omit(diff(cli_xts$oecd,lag=5))["1962::"] > 0 & na.omit(diff(cli_xts$oecd,lag=1))["1962::"] < 0]

# sp_correction_ind <- index(SP5["1962::"][SP5["1962::"][,4] / SP5["1962::"][,1] < delta])
# sp_correction_ind <- index(SP5["1962::"][SP5["1962::"][,3] / SP5["1962::"][,2] < delta])

  # if(c == "h"){ print("T")}
  # if(c == "o"){ print("S")}else{print("F")}
  sp_correction_ind <- c()
  a <- m
  # switch(a,               # switch(文字列,
  #   "h" = append(sp_correction_ind,index(SP5["1962::"][SP5["1962::"][,3] / SP5["1962::"][,2] < delta])),
  #   "c" = append(sp_correction_ind,index(SP5["1962::"][SP5["1962::"][,4] / SP5["1962::"][,1] < delta])),
  #   print("?")            #  一致するものが
  # )
  if(a == "h"){
    sp_correction_ind <- index(SP5["1962::"][SP5["1962::"][,3] / SP5["1962::"][,2] < delta])
  }
  else if(a == "c"){
    sp_correction_ind <- index(SP5["1962::"][SP5["1962::"][,4] / SP5["1962::"][,1] < delta])
  }
  else{
    print("?")
  }

# cat("sp_corr ");print(sp_correction_ind)
  cat("bp ");print(length(ind_bp))
  cat("bm ");print(length(ind_bm))
  cat("mp ");print(length(ind_mp))
  cat("pm ");print(length(ind_pm))
  # cat("pm+bm+mp ");print(length(append(append(ind_pm,ind_bm),ind_mp)))
  cat("pm+bm+mp ");print(length(c(ind_pm,ind_bm,ind_mp)))


  cat("correction sp5 vs. bm");print(sp_correction_ind[is.element(sp_correction_ind,ind_bm)])

  cat("correction sp5 vs. bp");print(sp_correction_ind[is.element(sp_correction_ind,ind_bp)])
  cat("correction sp5 vs. pm");print(sp_correction_ind[is.element(sp_correction_ind,ind_pm)])
  cat("correction sp5 vs. mp");print(sp_correction_ind[is.element(sp_correction_ind,ind_mp)])


# t.test(as.vector(VIX[,4][ind_bp]),as.vector(VIX[,4][ind_bm]))
# print("######################################")
# t.test(as.vector(VIX[,4][ind_bp]),as.vector(VIX[,4][append(append(ind_pm,ind_bm),ind_mp)]))
# # t.test(as.vector(VIX[,4][ind_bp]),as.vector(VIX[,4][ind_bm]))

}
func(0.9,"c")


CLI 5 month delta vs. SPX decline.






#
# pick up months whose monthlyreturn is less than 0.05 and put them to events
#
# argument 's' could be either "2000-01-01::" or "2000-01-01::2018-12-31"
#
func <- function(s="2000-01-01::",c=0.95){
  start_date <- s
  change_rate <- c
  # events <- xts(round(monthlyReturn(GSPC[start_date])[monthlyReturn(GSPC[start_date]) < -0.05],digits = 3),as.Date(mondate(index(monthlyReturn(GSPC[start_date])[monthlyReturn(GSPC[start_date]) < -0.05]))))

  events <- xts(round(SP5[start_date][,4]/SP5[start_date][,1][SP5[start_date][,4]/SP5[start_date][,1] < change_rate],digits =4),as.Date(mondate(index(SP5[start_date][,4]/SP5[start_date][,1][SP5[start_date][,4]/SP5[start_date][,1] < change_rate]))))

  #
  # draw graph of cli 5 months delta of oecd all, usa and china.
  #
  plot(diff(cli_xts$oecd,lag=5)[start_date],type='h')
  # addEventLines(events, srt=90, pos=2,col=10)  # this causes misalinghment
  # addEventLines(events[c(6,7)], srt=90, pos=2,col=10)
  #
  # somehow "addEventLines(events, srt=90, pos=2,col=10)" works well
  # vertical lines are put at wrong places. instead of it, put loop.
  #
  for(i in seq(1,length(events),1)){
    addEventLines(events[i], srt=90, pos=2,col=4)
  }
  events
  #
  # and only the first entry to place.
  #
  addEventLines(events[1], srt=90, pos=2,col=4)
}
func("2000-01-01::",0.95)