[Highfrequency-commits] r68 - pkg/highfrequency/R

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Mon Dec 23 14:28:11 CET 2013


Author: kboudt
Date: 2013-12-23 14:28:10 +0100 (Mon, 23 Dec 2013)
New Revision: 68

Modified:
   pkg/highfrequency/R/highfrequencyGSOC.R
   pkg/highfrequency/R/realized.R
Log:
unitaqload: make sure it continues if missing data ; 
rbeta: improved default choise

Modified: pkg/highfrequency/R/highfrequencyGSOC.R
===================================================================
--- pkg/highfrequency/R/highfrequencyGSOC.R	2013-10-27 09:36:35 UTC (rev 67)
+++ pkg/highfrequency/R/highfrequencyGSOC.R	2013-12-23 13:28:10 UTC (rev 68)
@@ -662,28 +662,21 @@
 
 ####Realized beta####
 
-rBeta = function(rdata, rindex, RCOVestimator= "rCov", RVestimator= "RV", makeReturns= FALSE,...)
+rBeta = function(rdata, rindex, RCOVestimator= "rCov", RVestimator= NULL, makeReturns= FALSE,...)
 {
   if (hasArg(data)) 
   {
     rdata = data
   }
   
-  if (makeReturns) 
-  {
-    rdata = makeReturns(rdata)
-    rindex= makeReturns(rindex)
+  if(RCOVestimator!="rRTSCov" & RCOVestimator!="rTSCov" &  makeReturns  ){
+    rdata = makeReturns(rdata);
+    rindex= makeReturns(rindex);  
   }
   
-  multixts = .multixts(rdata)
-  
-  if (multixts) 
+  if(!makeReturns)
   {
-    print("No support for multiple days")
-  }
-  if (!multixts) 
-  {
-    if(RCOVestimator=="rRTSCov" | RCOVestimator=="rTSCov"){
+    if(RCOVestimator=="rRTSCov" || RCOVestimator=="rTSCov"){
       if( min(rdata) <0 ){
         print("when using rRTSCov, rTSCov, introduce price data - transformation to price data done")
         rdata = exp(cumsum(rdata))
@@ -693,48 +686,63 @@
         rindex = exp(cumsum(rindex))
       }       
     }
+  }
+  
+  multixts = .multixts(rdata)
+  
+  if (multixts) 
+  {
+    print("No support for multiple days")
+  }
+  if (!multixts) 
+  {
+    
     rcovfun= function(rdata, rindex, RCOVestimator)
     {
       switch(RCOVestimator,
-             rCov= rCov(cbind(rdata,rindex) )[1,2],
-             rAVGCov= rAVGCov(list(rdata, rindex) )[1,2],
-             rBPCov= rBPCov(cbind(rdata, rindex) )[1,2],
-             rHYCov= rHYCov(list(rdata, rindex) )[1,2],
-             rKernelCov= rKernelCov(list(rdata, rindex) )[1,2],
-             rOWCov= rOWCov(cbind(rdata, rindex) )[1,2],
-             rRTSCov= rRTSCov(list(rdata, rindex))[1,2],
-             rThresholdCov= rThresholdCov(cbind(rdata, rindex) )[1,2],
-             rTSCov= rTSCov(list(rdata, rindex))[1,2]
+             rCov= rCov(cbind(rdata,rindex) ),
+             rAVGCov= rAVGCov(list(rdata, rindex) ),
+             rBPCov= rBPCov(cbind(rdata, rindex) ),
+             rHYCov= rHYCov(list(rdata, rindex) ),
+             rKernelCov= rKernelCov(list(rdata, rindex) ),
+             rOWCov= rOWCov(cbind(rdata, rindex) ),
+             rRTSCov= rRTSCov(list(rdata, rindex)),
+             rThresholdCov= rThresholdCov(cbind(rdata, rindex) ),
+             rTSCov= rTSCov(list(rdata, rindex))
       )
       
     }
-    rcov= rcovfun(rdata,rindex,RCOVestimator)
+    rcov= rcovfun(rdata,rindex,RCOVestimator);
     
-    if( is.null(RVestimator) ){ RVestimator = RCOVestimator }  
-    
-    rvfun= function(rindex, RVestimator)
+    if(RVestimator == RCOVestimator || is.null(RVestimator))
     {
+      rbeta = rcov[1,2]/rcov[2,2];
+    }else{
+      rvfun= function(rindex, RVestimator)
+      {
+        
+        switch(RVestimator,
+               RV= RV(rindex),
+               BV= RBPVar(rindex),
+               minRV= minRV(rindex ),
+               medRV= medRV(rindex ),
+               rCov= rCov(rindex ) ,
+               rAVGCov= rAVGCov(rindex ) ,
+               rBPCov= rBPCov(rindex ) ,
+               rHYCov= rHYCov(rindex ) ,
+               rKernelCov= rKernelCov(rindex ) ,
+               rOWCov= rOWCov(rindex ) ,
+               rRTSCov= rRTSCov(rindex) ,
+               rThresholdCov= rThresholdCov(rindex ) ,
+               rTSCov= rTSCov(rindex)
+        )             
+        
+      }
+      rv=rvfun(rindex,RVestimator)
       
-      switch(RVestimator,
-             RV= RV(rindex),
-             BV= RBPVar(rindex),
-             minRV= minRV(rindex ),
-             medRV= medRV(rindex ),
-             rCov= rCov(rindex ) ,
-             rAVGCov= rAVGCov(rindex ) ,
-             rBPCov= rBPCov(rindex ) ,
-             rHYCov= rHYCov(rindex ) ,
-             rKernelCov= rKernelCov(rindex ) ,
-             rOWCov= rOWCov(rindex ) ,
-             rRTSCov= rRTSCov(rindex) ,
-             rThresholdCov= rThresholdCov(rindex ) ,
-             rTSCov= rTSCov(rindex)
-      )             
-      
+      rbeta = rcov[1,2]/rv
     }
-    rv=rvfun(rindex,RVestimator)
     
-    rbeta = rcov/rv
     return(rbeta)
   }
 }

Modified: pkg/highfrequency/R/realized.R
===================================================================
--- pkg/highfrequency/R/realized.R	2013-10-27 09:36:35 UTC (rev 67)
+++ pkg/highfrequency/R/realized.R	2013-12-23 13:28:10 UTC (rev 68)
@@ -1237,128 +1237,149 @@
 
 ### Two time scale covariance : 
 rTSCov = function (pdata, cor = FALSE, K = 300, J = 1, K_cov = NULL, J_cov = NULL, 
-K_var = NULL, J_var = NULL, makePsd = FALSE) 
+                   K_var = NULL, J_var = NULL, makePsd = FALSE) 
 {
-    if (!is.list(pdata)) {
-        n = 1
+  if (!is.list(pdata)) {
+    n = 1
+  }
+  else {
+    n = length(pdata)
+    if (n == 1) {
+      pdata = pdata[[1]]
     }
-    else {
-        n = length(pdata)
-        if (n == 1) {
-            pdata = pdata[[1]]
-        }
+  }
+  
+  
+  if (n == 1) {
+    if ( nrow(pdata) < (10*K) ) {
+      stop("Two time scale estimator uses returns based on prices that are K ticks aways. 
+           Please provide a timeseries of at least 10*K" ) 
+    } 
+    multixts = .multixts(pdata); 
+    if(multixts){ stop("This function does not support having an xts object of multiple days as input. Please provide a timeseries of one day as input"); }
+    return(TSRV(pdata, K = K, J = J))
     }
-    if (n == 1) {
-        multixts = .multixts(pdata); 
-        if(multixts){ stop("This function does not support having an xts object of multiple days as input. Please provide a timeseries of one day as input"); }
-        return(TSRV(pdata, K = K, J = J))
+  if (n > 1) {
+    if ( nrow(pdata[[1]]) < (10*K) ) {
+      stop("Two time scale estimator uses returns based on prices that are K ticks aways. 
+           Please provide a timeseries of at least 10*K" ) 
+    } 
+    multixts = .multixts(pdata[[1]]); 
+    if(multixts){ stop("This function does not support having an xts object of multiple days as input. Please provide a timeseries of one day as input"); }
+    
+    
+    cov = matrix(rep(0, n * n), ncol = n)
+    if( is.null(K_cov)){ K_cov = K }
+    if( is.null(J_cov)){ J_cov = J }
+    if( is.null(K_var)){ K_var = rep(K,n) }
+    if( is.null(J_var)){ J_var = rep(J,n) }
+    
+    diagonal = c()
+    for (i in 1:n) {
+      diagonal[i] = TSRV(pdata[[i]], K = K_var[i], J = J_var[i])
     }
-    if (n > 1) {
-        multixts = .multixts(pdata[[1]]); 
-        if(multixts){ stop("This function does not support having an xts object of multiple days as input. Please provide a timeseries of one day as input"); }
-        
-        
-        cov = matrix(rep(0, n * n), ncol = n)
-        if( is.null(K_cov)){ K_cov = K }
-        if( is.null(J_cov)){ J_cov = J }
-        if( is.null(K_var)){ K_var = rep(K,n) }
-        if( is.null(J_var)){ J_var = rep(J,n) }
-        
-        diagonal = c()
-        for (i in 1:n) {
-            diagonal[i] = TSRV(pdata[[i]], K = K_var[i], J = J_var[i])
+    diag(cov) = diagonal
+    
+    for (i in 2:n) {
+      for (j in 1:(i - 1)) {
+        cov[i, j] = cov[j, i] = TSCov_bi(pdata[[i]], 
+                                         pdata[[j]], K = K_cov, J = J_cov)
+      }
+    }
+    if (cor == FALSE) {
+      if (makePsd == TRUE) {
+        cov = makePsd(cov)
+      }
+      return(cov)
+    }
+    if (cor == TRUE) {
+      invsdmatrix = try(solve(sqrt(diag(diag(cov)))), silent = F)
+      if (!inherits(invsdmatrix, "try-error")) {
+        rcor = invsdmatrix %*% cov %*% invsdmatrix
+        if (makePsd == TRUE) {
+          rcor = makePsd(rcor)
         }
-        diag(cov) = diagonal
-        
-        for (i in 2:n) {
-            for (j in 1:(i - 1)) {
-                cov[i, j] = cov[j, i] = TSCov_bi(pdata[[i]], 
-                pdata[[j]], K = K_cov, J = J_cov)
-            }
-        }
-        if (cor == FALSE) {
-            if (makePsd == TRUE) {
-                cov = makePsd(cov)
-            }
-            return(cov)
-        }
-        if (cor == TRUE) {
-            invsdmatrix = try(solve(sqrt(diag(diag(cov)))), silent = F)
-            if (!inherits(invsdmatrix, "try-error")) {
-                rcor = invsdmatrix %*% cov %*% invsdmatrix
-                if (makePsd == TRUE) {
-                    rcor = makePsd(rcor)
-                }
-                return(rcor)
-            }
-        }
+        return(rcor)
+      }
     }
+    }
 }
 
 ### ROBUST Two time scale covariance : 
 rRTSCov = function (pdata, cor = FALSE, startIV = NULL, noisevar = NULL, 
-K = 300, J = 1, 
-K_cov = NULL , J_cov = NULL,
-K_var = NULL , J_var = NULL , 
-eta = 9, makePsd = FALSE){
-    if (!is.list(pdata)) {
-        n = 1
+                    K = 300, J = 1, 
+                    K_cov = NULL , J_cov = NULL,
+                    K_var = NULL , J_var = NULL , 
+                    eta = 9, makePsd = FALSE){
+  if (!is.list(pdata)) {
+    n = 1
+  }
+  else {
+    n = length(pdata)
+    if (n == 1) {
+      pdata = pdata[[1]]
     }
-    else {
-        n = length(pdata)
-        if (n == 1) {
-            pdata = pdata[[1]]
-        }
+  }
+  
+  
+  
+  if (n == 1) {
+    if ( nrow(pdata) < (10*K) ) {
+      stop("Two time scale estimator uses returns based on prices that are K ticks aways. 
+           Please provide a timeseries of at least 10*K" ) 
+    } 
+    multixts = .multixts(pdata); 
+    if(multixts){ stop("This function does not support having an xts object of multiple days as input. Please provide a timeseries of one day as input"); }    
+    return(RTSRV(pdata, startIV = startIV, noisevar = noisevar, 
+                 K = K, J = J, eta = eta))
     }
-    if (n == 1) {
-        multixts = .multixts(pdata); 
-        if(multixts){ stop("This function does not support having an xts object of multiple days as input. Please provide a timeseries of one day as input"); }    
-        return(RTSRV(pdata, startIV = startIV, noisevar = noisevar, 
-        K = K, J = J, eta = eta))
+  if (n > 1) {
+    if ( nrow(pdata[[1]]) < (10*K) ) {
+      stop("Two time scale estimator uses returns based on prices that are K ticks aways. 
+           Please provide a timeseries of at least 10*K" ) 
+    } 
+    multixts = .multixts(pdata[[1]]); 
+    if(multixts){ stop("This function does not support having an xts object of multiple days as input. Please provide a timeseries of one day as input"); }
+    
+    cov = matrix(rep(0, n * n), ncol = n)
+    diagonal = c()
+    if( is.null(K_cov)){ K_cov = K }
+    if( is.null(J_cov)){ J_cov = J }  
+    if( is.null(K_var)){ K_var = rep(K,n) }
+    if( is.null(J_var)){ J_var = rep(J,n) }        
+    for (i in 1:n){ 
+      diagonal[i] = RTSRV(pdata[[i]], startIV = startIV[i], 
+                          noisevar = noisevar[i], K = K_var[i], J = J_var[i], 
+                          eta = eta)
     }
-    if (n > 1) {
-        multixts = .multixts(pdata[[1]]); 
-        if(multixts){ stop("This function does not support having an xts object of multiple days as input. Please provide a timeseries of one day as input"); }
-        
-        cov = matrix(rep(0, n * n), ncol = n)
-        diagonal = c()
-        if( is.null(K_cov)){ K_cov = K }
-        if( is.null(J_cov)){ J_cov = J }  
-        if( is.null(K_var)){ K_var = rep(K,n) }
-        if( is.null(J_var)){ J_var = rep(J,n) }        
-        for (i in 1:n){ 
-            diagonal[i] = RTSRV(pdata[[i]], startIV = startIV[i], 
-            noisevar = noisevar[i], K = K_var[i], J = J_var[i], 
-            eta = eta)
+    diag(cov) = diagonal
+    if( is.null(K_cov)){ K_cov = K }
+    if( is.null(J_cov)){ J_cov = J }                        
+    for (i in 2:n) {
+      for (j in 1:(i - 1)) {
+        cov[i, j] = cov[j, i] = RTSCov_bi(pdata[[i]], 
+                                          pdata[[j]], startIV1 = diagonal[i], startIV2 = diagonal[j], 
+                                          noisevar1 = noisevar[i], noisevar2 = noisevar[j], 
+                                          K = K_cov, J = J_cov, eta = eta)
+      }
+    }
+    if (cor == FALSE) {
+      if (makePsd == TRUE) {
+        cov = makePsd(cov)
+      }
+      return(cov)
+    }
+    if (cor == TRUE) {
+      invsdmatrix = try(solve(sqrt(diag(diag(cov)))), silent = F)
+      if (!inherits(invsdmatrix, "try-error")) {
+        rcor = invsdmatrix %*% cov %*% invsdmatrix
+        if (makePsd == TRUE) {
+          rcor = makePsd(rcor)
         }
-        diag(cov) = diagonal
-        if( is.null(K_cov)){ K_cov = K }
-        if( is.null(J_cov)){ J_cov = J }                        
-        for (i in 2:n) {
-            for (j in 1:(i - 1)) {
-                cov[i, j] = cov[j, i] = RTSCov_bi(pdata[[i]], 
-                pdata[[j]], startIV1 = diagonal[i], startIV2 = diagonal[j], 
-                noisevar1 = noisevar[i], noisevar2 = noisevar[j], 
-                K = K_cov, J = J_cov, eta = eta)
-            }
-        }
-        if (cor == FALSE) {
-            if (makePsd == TRUE) {
-                cov = makePsd(cov)
-            }
-            return(cov)
-        }
-        if (cor == TRUE) {
-            invsdmatrix = try(solve(sqrt(diag(diag(cov)))), silent = F)
-            if (!inherits(invsdmatrix, "try-error")) {
-                rcor = invsdmatrix %*% cov %*% invsdmatrix
-                if (makePsd == TRUE) {
-                    rcor = makePsd(rcor)
-                }
-                return(rcor)
-            }
-        }
+        return(rcor)
+      }
     }
+    }
 }
 
 ## Threshold covariance: 
@@ -2322,24 +2343,26 @@
   dates = dates[isBizday(dates, holidays = holidayNYSE(1960:2040))];
   
   if(trades){ tdata=NULL;
+              totaldata=NULL;
               for(i in 1:length(dates)){
                 datasourcex = paste(datasource,"/",dates[i],sep="");
                 filename = paste(datasourcex,"/",ticker,"_trades.RData",sep="");
                 
+                
                 ifmissingname = paste(datasourcex,"/missing_",ticker,".RData",sep="");  
                 
-                if(file.exists(ifmissingname)){stop(paste("No trades available on ",dates[i],sep=""))}
-                if(!file.exists(filename)){stop(paste("The file ",filename," does not exist. Please read the documentation.",sep=""))}
+                if(file.exists(ifmissingname)){warning(paste("No trades available on ",dates[i],sep="")); next;}
+                if(!file.exists(filename)){warning(paste("The file ",filename," does not exist. Please read the documentation.",sep="")); next;}
                 if(file.exists(ifmissingname)==FALSE){
                   load(filename);
-                  if(i==1)	{
+                  if(i==1)  { 
                     if( is.null(variables)){totaldata=tdata;
                     }else{
                       allnames=as.vector(colnames(tdata));
                       selection = allnames%in%variables;
                       qq=(1:length(selection))[selection];
                       totaldata=tdata[,qq];
-                    }	  
+                    }    
                   };
                   if(i>1){
                     if( is.null(variables)){totaldata=rbind(totaldata,tdata);
@@ -2352,17 +2375,18 @@
   }
   
   if(quotes){ qdata=NULL;
+              totaldataq=NULL;
               for(i in 1:length(dates)){
                 datasourcex = paste(datasource,"/",dates[i],sep="");
                 filename = paste(datasourcex,"/",ticker,"_quotes.RData",sep="");
                 ifmissingname = paste(datasourcex,"/missingquotes_",ticker,".RData",sep="");
                 
-                if(file.exists(ifmissingname)){stop(paste("no quotes available on ",dates[i],sep=""))}
-                if(!file.exists(filename)){stop(paste("The file ",filename," does not exist. Please read the documentation.",sep=""))}
+                if(file.exists(ifmissingname)){warning(paste("no quotes available on ",dates[i],sep="")); next;}
+                if(!file.exists(filename)){warning(paste("The file ",filename," does not exist. Please read the documentation.",sep="")); next;}
                 if(file.exists(ifmissingname)==FALSE){
                   load(filename);
                   
-                  if(i==1)	{
+                  if(i==1)  {
                     if( is.null(variables)){totaldataq=qdata;
                     }else{
                       allnames=as.vector(colnames(qdata));
@@ -2386,6 +2410,7 @@
   if(trades==FALSE & quotes==TRUE){return(totaldataq)}
 }
 
+
 ###### start SPOTVOL FUNCTIONS formerly in periodicityTAQ #########
 # Documented function:
 



More information about the Highfrequency-commits mailing list