Advertisement
karstenw

Intrinsic time

Jan 20th, 2019
1,289
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
R 4.52 KB | None | 0 0
  1. dc_intervals <- function(dat, threshold=0.02/100, price="price", time="time", vol="amount") {
  2.   # candidates for confirmed directional change
  3.   dcc_up_cand <- c(FALSE, c(dat[2:nrow(dat), price]<dat[1:(nrow(dat)-1), price]*(1-threshold)))
  4.   dcc_down_cand <- c(FALSE, c(dat[2:nrow(dat), price]>dat[1:(nrow(dat)-1), price]*(1+threshold)))
  5.  
  6.   # initialize new columns
  7.   dat[,"interval"] <- NA  # 4 types of intervals: down, down_os, up, up_os (os=overshoot, NA means unknown)
  8.   dat[,"point"] <- NA     # 3 types of points: dcc_up, dcc_down,  ep (NA means ignore)
  9.  
  10.   # threshold too large?
  11.   if(all(!dcc_up_cand)) stop("threshold=",threshold, " too large, no price moves above this threshold")
  12.   if(all(!dcc_down_cand)) stop("threshold=",threshold, " too large, no price moves above this threshold")
  13.  
  14.   # figure out if the first change is up or down
  15.   first_up <- which.max(dcc_up_cand)
  16.   first_down <- which.max(dcc_down_cand)
  17.  
  18.   if(first_up<first_down) {
  19.     dat[first_up,"point"] <- "dcc_up"
  20.     mode <- "dcc_up"
  21.     i <- first_up
  22.   } else {
  23.     dat[first_down, "point"] <- "dcc_down"
  24.     mode <- "dcc_down"
  25.     i <- first_down
  26.   }
  27.  
  28.   # iterate till the end of the data
  29.   while (i<nrow(dat)) {
  30.     if(mode=="dcc_up") {
  31.       j <- which.max(dcc_down_cand[i+1:length(dcc_down_cand)])+i
  32.       if(!dcc_down_cand[j]) break # end of dataset
  33.       dat[i,"point"] <- "dcc_up"
  34.       dat[i,"interval"] <- "uoi"
  35.       if((j-1)-(i+1)>0) { # if not changing direction immediately
  36.         ep_idx <- which.min(dat[(i+1):(j-1), price])+i
  37.         dat[ep_idx, "point"] <- "ep"
  38.         dat[i:ep_idx-1, "interval"] <- "uoi"
  39.         dat[ep_idx:j,"interval"] <- "dei"
  40.       }
  41.       i <- j+1
  42.       mode <- "dcc_down"
  43.     } else { # mode=="dcc_down"
  44.       j <- which.max(dcc_up_cand[i+1:length(dcc_up_cand)])+i
  45.       if(!dcc_up_cand[j]) break # end of dataset
  46.       dat[i,"point"] <- "dcc_down"
  47.       dat[i,"interval"] <- "doi"
  48.       if((j-1)-(i+1)>0) { # if not changing direction immediately
  49.         ep_idx <- which.max(dat[(i+1):(j-1), price])+i
  50.         dat[ep_idx, "point"] <- "ep"
  51.         dat[i:ep_idx-1, "interval"] <- "doi"
  52.         dat[ep_idx:j,"interval"] <- "uei"
  53.       }
  54.       i <- j+1
  55.       mode <- "dcc_up"      
  56.     }
  57.   }
  58.  
  59.   dat <- dat[!is.na(dat[,"point"]),] # drop all points that are not dcc points
  60.   return(dat)
  61. }
  62.  
  63. ohlc_summary <- function(dat, browse=TRUE, verbose=TRUE) {
  64.   dat$hour <- strftime(dat$time, format="%Y-%m-%d %H")
  65.   one_hour <- function(dset) {
  66.     i1 <- which.min(dset$time)
  67.     i2 <- which.max(dset$time)
  68.     ans <- data.frame(
  69.       time=strptime(dset[1,"hour"], format="%Y-%m-%d %H"),
  70.       open=dset[i1, "price"],
  71.       hi=max(dset$price),
  72.       lo=min(dset$price),
  73.       close=dset[i2,"price"],
  74.       amount=sum(dset$amount)
  75.     )
  76.     return(ans)
  77.   }
  78.   ret <- do.call(rbind, lapply(split(dat, dat$hour), FUN=one_hour))
  79.   if(verbose) message("ohlc summary built.")
  80.   if(browse) browser()
  81.   return(ret)
  82. }
  83.  
  84. # begin, end ... "YYYY-MM-DD"
  85. plot_dc <- function(dc_dat=NULL, ohlc=NULL, ticks=NULL, begin=NULL, end=NULL, lwd=3, price="price", time="time", vol="amount", ...) {
  86.   if(is.null(ohlc) & !is.null(ticks)) ohlc <- ohlc_summary(ticks)
  87.   if(is.null(dc_dat)  & !is.null(ticks)) dc_dat <- dc_intervals(dat=ticks, time=time, price=price, vol=vol)
  88.   if(is.null(dc_dat)) stop("missing dc data.")
  89.  
  90.   if(is.null(end)) end <- max(dc_dat[,time])
  91.   if(is.character(end)) end <- as.POSIXct(strptime(end, "%Y-%m-%d"))
  92.   if(is.null(begin)) begin <- min(dc_dat[,time])
  93.   if(is.character(begin)) begin <- as.POSIXct(strptime(begin, "%Y-%m-%d"))
  94.  
  95.   plot(
  96.     dat=1, col="white", dc_dat=dc_dat, xlab="",
  97.     xlim=c(min(ohlc$time), max(ohlc$time)), ylim=c(min(ticks$price), max(ticks$price)),
  98.     main=sprintf("%.2f%% Direction Changes for BTC", threshold*100),
  99.     cex.main=1
  100.   )
  101.  
  102.   if(!is.null(ohlc)) {
  103.     for (i in seq(1, nrow(ohlc))) {
  104.       segments(
  105.         x0=ohlc[i,"time"], y0=ohlc[i,"lo"],
  106.         x1=ohlc[i,"time"], y0=ohlc[i, "hi"],
  107.         col="darkgrey", lwd=0.5
  108.       )
  109.     }
  110.   }
  111.  
  112.   dcc_idx <- which(dc_dat$point=="dcc_up"|dc_dat$point=="dcc_down")
  113.   for (i in setdiff(dcc_idx,1)) {
  114.     segments(
  115.       x0=dc_dat[i-1,time], y0=dc_dat[i-1,price],
  116.       x1=dc_dat[i,time], y1=dc_dat[i,price],
  117.       col="red", lwd=lwd
  118.     )
  119.   }
  120.   for (i in setdiff(dcc_idx,nrow(dc_dat))) {
  121.     segments(
  122.       x0=dc_dat[i,time], y0=dc_dat[i,price],
  123.       x1=dc_dat[i+1,time], y1=dc_dat[i+1,price],
  124.       col="darkgreen", lwd=lwd
  125.     )
  126.   }
  127.   axis(1,at=dc_dat[dcc_idx, time],labels=FALSE)
  128. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement