[Blotter-commits] r357 - pkg/RTAQ/R

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Mon Jul 19 19:04:07 CEST 2010


Author: jonathan
Date: 2010-07-19 19:04:07 +0200 (Mon, 19 Jul 2010)
New Revision: 357

Modified:
   pkg/RTAQ/R/aggregate.R
   pkg/RTAQ/R/convert_to_RData.R
   pkg/RTAQ/R/manipulation.R
Log:
bugfix thx to reporting of Ruey-wan Liou

Modified: pkg/RTAQ/R/aggregate.R
===================================================================
--- pkg/RTAQ/R/aggregate.R	2010-07-19 13:53:53 UTC (rev 356)
+++ pkg/RTAQ/R/aggregate.R	2010-07-19 17:04:07 UTC (rev 357)
@@ -101,22 +101,31 @@
 }
 
 #VOLUME: (specificity: always sum)
-agg_volume = function(ts,FUN = sumN,on="minutes",k=5, includeopen=FALSE){
-ts = dataformatc(ts);
-  if(!includeopen){ts3 = aggregatets(ts, FUN=sumN, on, k)}
-
-  if(includeopen){
-  ts2 = aggregatets(ts, FUN=sumN, on, k);
-  date = strsplit(as.character(index(ts))," ")[[1]][1]
-  realopen = "09:30:00";
-  a = as.timeDate(paste(date,realopen));
-  b = xts(as.numeric(ts[1]),a);
-  ts3 = c(b,ts2);
-  }
-return(ts3)
+agg_volume = function (ts, FUN = sumN, on = "minutes", k = 5, includeopen = FALSE) 
+{
+    ts = dataformatc(ts)
+    if (!includeopen) {
+        ts3 = aggregatets(ts, FUN = sumN, on, k)
+    }
+    if (includeopen) {
+        ts2 = aggregatets(ts, FUN = sumN, on, k)
+        date = strsplit(as.character(index(ts)), " ")[[1]][1]
+        realopen = "09:30:00"
+        a = as.timeDate(paste(date, realopen))
+        b = xts(as.numeric(ts[1]), a)
+        ts3 = c(b, ts2)
+	  }
+	#new
+	    realclose = "16:00:00";
+	    aa = as.timeDate(paste(date, realclose));
+	    condition = index(ts3) < aa;
+	    ts4 = ts3[condition];
+	    lastinterval = sum(as.numeric(ts3[!condition]));
+	    bb = xts(lastinterval, aa)
+	    ts4 = c(ts4, bb)
+    return(ts4)
 }
 
-
 ###TRADES AGGREGATION:
 agg_trades = function(tdata,on="minutes",k=5){
 tdata = dataformatc(tdata);

Modified: pkg/RTAQ/R/convert_to_RData.R
===================================================================
--- pkg/RTAQ/R/convert_to_RData.R	2010-07-19 13:53:53 UTC (rev 356)
+++ pkg/RTAQ/R/convert_to_RData.R	2010-07-19 17:04:07 UTC (rev 357)
@@ -50,119 +50,111 @@
   }
 }
 
-convert_trades = function(datasource,datadestination,ticker,extention="txt",header=F,tradecolnames=NULL,format="%m/%d/%Y %H:%M:%S"){
+convert_trades = function (datasource, datadestination, ticker, extention = "txt", 
+    header = F, tradecolnames = NULL, format = "%m/%d/%Y %H:%M:%S") 
+{
+    setwd(datasource)
+    adjtime = function(z) {
+        zz = unlist(strsplit(z, ":"))
+        if (nchar(zz[1]) != 2) {
+            return(paste(paste(0, zz[1], sep = ""), zz[2], zz[3], 
+                sep = ":"))
+        }
+        return(z)
+    }
+    for (i in 1:length(ticker)) {
+        tfile_name = paste(datasource, "\\", ticker[i], "_trades", 
+            sep = "")
+        tdata = try(readdata(path = tfile_name, extention = extention, 
+            header = header, dims = 9), silent = TRUE)
+        error = dim(tdata)[1] == 0
+        if (error) {
+            print(paste("no trades for stock", ticker[i]))
+            missingt = rbind(missingt, c(datasource, ticker[i]))
+        }
+        if (error == FALSE) {
+                if (is.null(tradecolnames)) {
+                  tradecolnames = c("SYMBOL", "DATE", "EX", "TIME", 
+                    "PRICE", "SIZE", "COND", "CORR", "G127")
+                  colnames(tdata) = tradecolnames
+                }else {
+                  colnames(tdata) = tradecolnames
+                }
+            cond = tdata$COND[is.na(tdata$G127)];
+            cr = tdata$CORR[is.na(tdata$G127)];
 
-  setwd(datasource);
-  adjtime = function(z){ 
-  zz = unlist(strsplit(z,":")); 
-  if(nchar(zz[1])!=2){
-  return(paste(paste(0,zz[1],sep=""),zz[2],zz[3],sep=":"))}
-  return(z);
-  }
+            tdata$COND[is.na(tdata$G127)] = 0
+            tdata$CORR[is.na(tdata$G127)] = as.character(cond)
+            tdata$G127[is.na(tdata$G127)] = as.character(cr)
+            rm(cond, cr)
+            oldtime = as.matrix(as.vector(tdata$TIME))
+            newtime = apply(oldtime, 1, adjtime)
+            tdata$TIME = newtime
+            rm(oldtime, newtime)
+            tdobject = timeDate(paste(as.vector(tdata$DATE), 
+                as.vector(tdata$TIME)), format = format, FinCenter = "GMT", 
+                zone = "GMT")
+            tdata = xts(tdata, order.by = tdobject)
+            tdata = tdata[, c("SYMBOL", "EX", "PRICE", "SIZE", 
+                "COND", "CORR", "G127")]
+            rm(tdobject)
+        }
+        xts_name = paste(ticker[i], "_trades.RData", sep = "")
+        setwd(datadestination)
+        save(tdata, file = xts_name)
+    }
+}
 
-  for(i in 1:length(ticker)){
-  tfile_name = paste(datasource,"\\",ticker[i],"_trades",sep="");
-  tdata = try(readdata(path=tfile_name, extention=extention,header=header,dims=9),silent=TRUE);
-  error = dim(tdata)[1]==0; 
 
-  if(error)
-  {print(paste("no trades for stock",ticker[i]));
-  missingt = rbind(missingt,c(datasource,ticker[i]));
-  }
-  if(error==FALSE){
+convert_quotes = function (datasource, datadestination, ticker, extention = "txt", 
+    header = F, quotecolnames = NULL, format = "%m/%d/%Y %H:%M:%S") 
+{
+    setwd(datasource)
+    adjtime = function(z) {
+        zz = unlist(strsplit(z, ":"))
+        if (nchar(zz[1]) != 2) {
+            return(paste(paste(0, zz[1], sep = ""), zz[2], zz[3], 
+                sep = ":"))
+        }
+        return(z)
+    }
+    for (i in 1:length(ticker)) {
+        qfile_name = paste(datasource, "\\", ticker[i], "_quotes", 
+            sep = "")
+        qdata = try(readdata(path = qfile_name, extention = extention, 
+            header = header, dims = 9), silent = TRUE)
+        error = dim(qdata)[1] == 0
+        if (error) {
+            print(paste("no quotes for stock", ticker[i]))
+            missingq = rbind(missingq, c(datasource, ticker[i]))
+        }
+        if (error == FALSE) {
+                if (is.null(quotecolnames)) {
+                  quotecolnames = c("SYMBOL", "DATE", "EX", "TIME", 
+                    "BID", "BIDSIZ", "OFR", "OFRSIZ", "MODE")
+                  colnames(qdata) = quotecolnames
+                }
+                else {
+                  colnames(qdata) = quotecolnames
+                }
+            qdata = qdata[qdata$SYMBOL == ticker[i], ]
+            oldtime = as.matrix(as.vector(qdata$TIME))
+            newtime = apply(oldtime, 1, adjtime)
+            qdata$TIME = newtime
+            rm(oldtime, newtime)
+            test = paste(as.vector(qdata$DATE), as.vector(qdata$TIME))
+            tdobject = timeDate(test, format = format, FinCenter = "GMT", 
+                zone = "GMT")
+            tdobject = timeDate(test, format = format, FinCenter = "GMT", 
+                zone = "GMT")
+            qdata = xts(qdata, order.by = tdobject)
+            qdata = qdata[, c("SYMBOL", "EX", "BID", "BIDSIZ", 
+                "OFR", "OFRSIZ", "MODE")]
+        }
+        xts_name = paste(ticker[i], "_quotes.RData", sep = "")
+        setwd(datadestination)
+        save(qdata, file = xts_name)
+    }
+}
 
-  #assign column names
-  if(header==FALSE){
-  if(is.null(tradecolnames)){
-  tradecolnames=c("SYMBOL","DATE","EX","TIME","PRICE","SIZE","COND","CORR","G127");
-  colnames(tdata)= tradecolnames;
-  }else{
-  colnames(tdata)= tradecolnames;
-  }
-  }
 
-  ### solve issue when there is no COND ###
-  cond=tdata$COND[is.na(tdata$G127)];
-  cr=tdata$CORR[is.na(tdata$G127)];
-
-  tdata$COND[is.na(tdata$G127)]=0;
-  tdata$CORR[is.na(tdata$G127)]= as.character(cond);
-  tdata$G127[is.na(tdata$G127)] = as.character(cr);
-  rm(cond,cr);
-
-  ## solve issue that time notation is inconsequent (no 09h but 9h)
-  oldtime = as.matrix(as.vector(tdata$TIME));
-  newtime = apply(oldtime,1,adjtime);
-  tdata$TIME = newtime;
-  rm(oldtime,newtime);
-
-  ##make xts object ##
-  tdobject=timeDate(paste(as.vector(tdata$DATE), as.vector(tdata$TIME)),format = format,FinCenter = "GMT",zone="GMT");
-  tdata = xts(tdata,order.by=tdobject);
-  tdata=tdata[,c("SYMBOL","EX","PRICE","SIZE","COND","CORR","G127")];
-
-  rm(tdobject);
-  }
-
-  xts_name = paste(ticker[i],"_trades.RData",sep="");
-  setwd(datadestination);
-  save(tdata, file = xts_name);
-
-  }
-  }
-
-
-convert_quotes = function(datasource,datadestination,ticker,extention="txt",header=F,quotecolnames=NULL,format="%m/%d/%Y %H:%M:%S"){
-  setwd(datasource);
-  adjtime = function(z){ 
-  zz = unlist(strsplit(z,":")); 
-  if(nchar(zz[1])!=2){
-  return(paste(paste(0,zz[1],sep=""),zz[2],zz[3],sep=":"))}
-  return(z);
-  }
-
-  for(i in 1:length(ticker)){
-  qfile_name = paste(datasource,"\\",ticker[i],"_quotes",sep="");
-
-  qdata=try(readdata(path=qfile_name, extention=extention,header=header,dims=9),silent=TRUE);
-  error = dim(qdata)[1]==0; 
-
-  if(error)
-  {print(paste("no quotes for stock",ticker[i])); 
-  missingq=rbind(missingq,c(datasource,ticker[i]));
-  }
-  if(error==FALSE){
-
-  #assign column names
-  if(header==FALSE){
-  if(is.null(quotecolnames)){
-  quotecolnames = c("SYMBOL","DATE","EX","TIME","BID","BIDSIZ","OFR","OFRSIZ","MODE");  
-  colnames(qdata)= quotecolnames;
-  }else{
-  colnames(qdata)= quotecolnames;
-  }
-  }
-
-  ####important because of data mistakes,must become something like "ticker"
-  qdata = qdata[qdata$SYMBOL==ticker[i],];
-
-  ## solve issue that time notation is inconsequent (no 09h but 9h):
-  oldtime = as.matrix(as.vector(qdata$TIME));
-  newtime = apply(oldtime,1,adjtime); #check if function in this file
-  qdata$TIME = newtime;
-  rm(oldtime,newtime);
-
-  ##make xts object 
-  test = paste(as.vector(qdata$DATE), as.vector(qdata$TIME));
-  tdobject=timeDate(test,format = format,FinCenter = "GMT",zone="GMT");
-  tdobject=timeDate(test,format = format,FinCenter = "GMT",zone="GMT");
-  qdata = xts(qdata,order.by=tdobject);
-  qdata = qdata[,c("SYMBOL","EX","BID","BIDSIZ","OFR","OFRSIZ","MODE")];
-  }
-
-  xts_name = paste(ticker[i],"_quotes.RData",sep="");
-  setwd(datadestination);
-  save(qdata, file = xts_name);
-  }
-  }
-

Modified: pkg/RTAQ/R/manipulation.R
===================================================================
--- pkg/RTAQ/R/manipulation.R	2010-07-19 13:53:53 UTC (rev 356)
+++ pkg/RTAQ/R/manipulation.R	2010-07-19 17:04:07 UTC (rev 357)
@@ -106,7 +106,11 @@
   merged = merged[index(tdata)];
 
   #return useful parts:
-  merged = merged[,c((1:tt),((tt+3):(dim(merged)[2])))];
+	#remove duplicated SYMBOL & EX (new)
+	eff =  colnames(merged);
+	realnames = c("SYMBOL","EX","PRICE","SIZE","COND","CORR","G127","BID","BIDSIZ","OFR","OFRSIZ","MODE");
+	condition = (1:length(eff))[eff%in%realnames];
+	merged = merged[,condition];
 
   ##a bit rough but otherwise opening price disappears...
   merged = as.xts(na.locf(as.zoo(merged),fromLast=TRUE));



More information about the Blotter-commits mailing list