[Blotter-commits] r345 - in pkg/RTAQ: R man

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Thu Jun 24 12:25:12 CEST 2010


Author: jonathan
Date: 2010-06-24 12:25:11 +0200 (Thu, 24 Jun 2010)
New Revision: 345

Modified:
   pkg/RTAQ/R/aggregate.R
   pkg/RTAQ/R/cleanupfunctions.R
   pkg/RTAQ/R/convert_to_RData.R
   pkg/RTAQ/R/manipulation.R
   pkg/RTAQ/R/totalcleanup.R
   pkg/RTAQ/man/prs.Rd
Log:
second bunch of EZ comments incoporated (WRDS standard)

Modified: pkg/RTAQ/R/aggregate.R
===================================================================
--- pkg/RTAQ/R/aggregate.R	2010-06-23 11:39:51 UTC (rev 344)
+++ pkg/RTAQ/R/aggregate.R	2010-06-24 10:25:11 UTC (rev 345)
@@ -11,7 +11,6 @@
 return(c)
 }
 
-
 ##AGGREGATION;
 aggregatets = function (ts, FUN = previoustick, on = "minutes", k = 1, weights = NULL,dropna=F)
 {
@@ -55,7 +54,6 @@
         ts3 = .xts(ts2, a)
     }
 
-    index(ts3) = as.timeDate(index(ts3));
 	if(!dropna){
 	if(on !="weeks"|on!="days"){
 	if(on=="secs"|on=="seconds"){tby = "s"}
@@ -63,10 +61,12 @@
       if (on == "hours"){tby = "h"}
 	by = paste(k,tby,sep=" ");
 	allindex = as.timeDate(seq(start(ts3),end(ts3),by=by));
-	xx = xts(rep(1,length(allindex)),order.by=allindex);
+	xx = xts(rep("1",length(allindex)),order.by=allindex);
 	ts3 = merge(ts3,xx)[,1];
+
 	}#currently for weeks and days, na are still dropped
 	}#end dropna if
+    index(ts3) = as.timeDate(index(ts3));
 
     return(ts3)
 }
@@ -74,6 +74,7 @@
 #PRICE (specificity: opening price and previoustick)
 
 agg_price = function(ts,FUN = previoustick,on="minutes",k=1){
+ts = dataformatc(ts);
 ##Return new timeseries as xts object where
 ##first observation is always the opening price
 ##subsequent observations are the closing prices over the interval with endpoint the timestamp of the result
@@ -101,7 +102,7 @@
 
 #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){
@@ -118,9 +119,10 @@
 
 ###TRADES AGGREGATION:
 agg_trades = function(tdata,on="minutes",k=5){
+tdata = dataformatc(tdata);
   ## Aggregates an entire trades xts object (tdata) over a "k"-minute interval.
   ## Returned xts-object contains: SYMBOL,EX,PRICE,SIZE.
-  ## Variables COND, CR, G127 are dropped because aggregating them makes no sense.
+  ## Variables COND, CORR, G127 are dropped because aggregating them makes no sense.
   ## NOTE: first observation (opening price) always included.
 
   PRICE = agg_price(tdata$PRICE,on=on,k=k);
@@ -137,24 +139,25 @@
 
 ###QUOTES AGGREGATION:
 agg_quotes = function(qdata,on="minutes",k=5){
+qdata = dataformatc(qdata);
   ## Aggregates an entire quotes xts object (qdata) object over a "k"-minute interval.
-  ## Returned xts-object contains: SYMBOL,EX,BID,BIDSIZE,OFFER,OFFERSIZE.
+  ## Returned xts-object contains: SYMBOL,EX,BID,BIDSIZ,OFR,OFRSIZ.
   ## Variable MODE is dropped because aggregation makes no sense.
   ## "includeopen" determines whether to include the exact opening quotes.
   
   BID = agg_price(qdata$BID,on=on,k=k);
-  OFFER = agg_price(qdata$OFFER,on=on,k=k);
+  OFR = agg_price(qdata$OFR,on=on,k=k);
 
-  BIDSIZE = agg_volume(qdata$BIDSIZE,on=on,k=k,includeopen=TRUE);
-  OFFERSIZE = agg_volume(qdata$OFFERSIZE,on=on,k=k,includeopen=TRUE);
+  BIDSIZ = agg_volume(qdata$BIDSIZ,on=on,k=k,includeopen=TRUE);
+  OFRSIZ = agg_volume(qdata$OFRSIZ,on=on,k=k,includeopen=TRUE);
 
   EX = agg_price(qdata$EX,on=on,k=k)
-  SYMBOL = rep(qdata$SYMBOL[1],length(BIDSIZE));
+  SYMBOL = rep(qdata$SYMBOL[1],length(BIDSIZ));
 
-  all = data.frame(SYMBOL,EX,BID,BIDSIZE,OFFER,OFFERSIZE);
-  colnames(all) =c("SYMBOL","EX","BID","BIDSIZE","OFFER","OFFERSIZE");
+  all = data.frame(SYMBOL,EX,BID,BIDSIZ,OFR,OFRSIZ);
+  colnames(all) =c("SYMBOL","EX","BID","BIDSIZ","OFR","OFRSIZ");
 
-  ts = xts(all,index(BIDSIZE));
+  ts = xts(all,index(BIDSIZ));
 
   return(ts);
 }

Modified: pkg/RTAQ/R/cleanupfunctions.R
===================================================================
--- pkg/RTAQ/R/cleanupfunctions.R	2010-06-23 11:39:51 UTC (rev 344)
+++ pkg/RTAQ/R/cleanupfunctions.R	2010-06-24 10:25:11 UTC (rev 345)
@@ -12,6 +12,7 @@
 ####FUNCTION TO FILTER EXCHANGE HOURS ONLY: ExchangeHoursOnly
 ExchangeHoursOnly = function(data, daybegin = "09:30:00",dayend="16:00:00")
 {
+data = dataformatc(data);
     # a function to excerpt data within exchange trading hours
     # daybegin and dayend: two characters in the format of "HH:MM:SS",
     #                specifying the starting hour and minute and sec of an exhange
@@ -37,6 +38,7 @@
 
 
 nozeroprices = function(tdata){
+tdata = dataformatc(tdata);
 ####FUNCTION TO DELETE ZERO PRICES: nozeroprices
 filteredts = tdata[as.numeric(tdata$PRICE)!= 0];
 return(filteredts);
@@ -44,12 +46,14 @@
 
 
 selectexchange = function(data,exch="N"){ 
+data = dataformatc(data);
 ###FUNCTION TO SELECT THE OBSERVATIONS OF A SINGLE EXCHANGE: selectexchange
 filteredts = data[data$EX==exch];
 return(filteredts);
 }
 
 autoselectexchange = function(tdata){
+tdata = dataformatc(tdata);
 ## AUTOSELECT EXCHANGE WITH HIGHEST NUMBER OF SHARES TRADED (for trades) ON:
 #function returns ts with obs of only 1 exchange
 #searches exchange with a maximum on the variable "SIZE"
@@ -82,12 +86,22 @@
 
 
 ##### TRADE DATA SPECIFIC FUNCTIONS: ###################################
-salescond = function(tdata){ 
+#salescond = function(tdata){ 
+#tdata = dataformatc(tdata);
 ###DELETE ENTRIES WITH AN ABONORMAL SALES CONDITION
-filteredts = tdata[tdata$COND == "0"|tdata$COND == "E"|tdata$COND == "F"];
-return(filteredts);
+#filteredts = tdata[tdata$COND == "0"|tdata$COND == "E"|tdata$COND == "F"];
+#return(filteredts);
+#}
+
+#zivot
+salescond <- function (tdata)
+{
+    filteredts = tdata[tdata$COND == "0" | tdata$COND == "E" |
+        tdata$COND == "F" | tdata$COND == "" | tdata$COND == "@F"]
+    return(filteredts)
 }
 
+
 ##Merge same timestamp:
 sumN = function(a){
   a = sum(as.numeric(a));
@@ -116,6 +130,7 @@
 }
 
 mergesametimestamp = function(tdata,selection="median"){
+tdata = dataformatc(tdata);
   #find end points:
   ep = endpoints(tdata,"secs");
 
@@ -137,13 +152,16 @@
 }
 
 rmtradeoutliers = function(tdata,qdata){
+tdata = dataformatc(tdata);
+qdata = dataformatc(qdata);
+
 ##Function to delete entries with prices that are above the ask plus the bid-ask
 ##spread. Similar for entries with prices below the bid minus the bid-ask
 ##spread.
   data = matchtq(tdata,qdata);
   price = as.numeric(data$PRICE);
   bid = as.numeric(data$BID);
-  offer = as.numeric(data$OFFER);
+  offer = as.numeric(data$OFR);
   spread = offer - bid;
 
   upper = offer+spread;
@@ -157,13 +175,15 @@
 #################       QUOTE SPECIFIC FUNCTIONS:       #################
 
 nozeroquotes = function(qdata){
+qdata = dataformatc(qdata);
 ####FUNCTION TO DELETE ZERO QUOTES: nozeroquotes
-filteredts = qdata[as.numeric(qdata$BID)!= 0& as.numeric(qdata$OFFER)!= 0];
+filteredts = qdata[as.numeric(qdata$BID)!= 0& as.numeric(qdata$OFR)!= 0];
 return(filteredts);
 }
 
 
 autoselectexchangeq = function(qdata){
+qdata = dataformatc(qdata);
 ####Autoselect exchange with highest value for (bidsize+offersize)
   nobs=c();
   exchanges = c("Q","A","P","B","C","N","D","X","I","M","W","Z");
@@ -171,15 +191,15 @@
 
   selected1 = selectexchange(qdata,"Q");
   selected2 = selectexchange(qdata,"T");
-  z1 = sum(as.numeric(selected1$BIDSIZE)+as.numeric(selected1$OFFERSIZE));
-  z2 = sum(as.numeric(selected2$BIDSIZE)+as.numeric(selected2$OFFERSIZE));
+  z1 = sum(as.numeric(selected1$BIDSIZ)+as.numeric(selected1$OFRSIZ));
+  z2 = sum(as.numeric(selected2$BIDSIZ)+as.numeric(selected2$OFRSIZ));
   z = max(z1,z2);
   watchout = z == z2;
   nobs = cbind(nobs,z);
 
   for(i in 2:length(exchanges)) {
   selected = selectexchange(qdata,exchanges[i]);
-  z = sum(as.numeric(selected$BIDSIZE)+as.numeric(selected$OFFERSIZE));
+  z = sum(as.numeric(selected$BIDSIZ)+as.numeric(selected$OFRSIZ));
   nobs = cbind(nobs,z); 
                         }
 
@@ -196,6 +216,7 @@
 
 
 mergequotessametimestamp = function(qdata,selection="median"){  ##FAST
+qdata = dataformatc(qdata);
   condition=selection=="median"|selection=="maxvolume"|selection=="weightedaverage";
   if(!condition){print(paste("WARNING:The result will be corrupted. Check whether",selection,"is an existing option for the attribute selection."))}
 
@@ -203,42 +224,43 @@
   ep = endpoints(qdata,"secs");
 
   #size per second:
-  bidsize = period.apply(qdata$BIDSIZE,ep,sumN);
-  offersize =  period.apply(qdata$OFFERSIZE,ep,sumN);
+  bidsize = period.apply(qdata$BIDSIZ,ep,sumN);
+  offersize =  period.apply(qdata$OFRSIZ,ep,sumN);
 
   #median per second:
   if(selection=="median"){
   bid = period.apply(qdata$BID,ep,medianN);
-  offer = period.apply(qdata$OFFER,ep,medianN);
+  offer = period.apply(qdata$OFR,ep,medianN);
   }
 
   #maxvolume per second:
   if(selection=="maxvolume"){
-  bid = period.apply(cbind(qdata$BID,qdata$BIDSIZE),ep,maxvol);
-  offer = period.apply(cbind(qdata$OFFER,qdata$OFFERSIZE),ep,maxvol);
+  bid = period.apply(cbind(qdata$BID,qdata$BIDSIZ),ep,maxvol);
+  offer = period.apply(cbind(qdata$OFR,qdata$OFRSIZ),ep,maxvol);
   }
 
   if(selection=="weightedaverage"){
-  bid = period.apply(cbind(qdata$BID,qdata$BIDSIZE),ep,waverage);
-  offer = period.apply(cbind(qdata$OFFER,qdata$OFFERSIZE),ep,waverage);
+  bid = period.apply(cbind(qdata$BID,qdata$BIDSIZ),ep,waverage);
+  offer = period.apply(cbind(qdata$OFR,qdata$OFRSIZ),ep,waverage);
   }
 
   ##merge everything:
   selection = ep[2:length(ep)];
   ts2 = qdata[selection];
   ts2$BID = bid;
-  ts2$OFFER = offer;
+  ts2$OFR = offer;
 
-  ts2$BIDSIZE = bidsize;
-  ts2$OFFERSIZE = offersize;
+  ts2$BIDSIZ = bidsize;
+  ts2$OFRSIZ = offersize;
 
 return(ts2)
 }
 
 
 rmnegspread = function(qdata){
+qdata = dataformatc(qdata);
 ##function to remove observations with negative spread
-  condition = as.numeric(qdata$OFFER)>as.numeric(qdata$BID);
+  condition = as.numeric(qdata$OFR)>as.numeric(qdata$BID);
   qdata[condition];
 }
 
@@ -246,13 +268,14 @@
 rmlargespread = function(qdata,maxi=50){
 ##function to remove observations with a spread larger than 50 times the median spread that day
 ###WATCH OUT: works only correct if supplied input data consists of 1 day...
-  spread = as.numeric(qdata$OFFER)-as.numeric(qdata$BID);
+  spread = as.numeric(qdata$OFR)-as.numeric(qdata$BID);
   condition = ((maxi*median(spread))>spread);
   return(qdata[condition])
 }
 
 rmoutliers = function (qdata, maxi = 10, window = 50, type = "advanced")
 {
+qdata = dataformatc(qdata);
 ##function to remove entries for which the mid-quote deviated by more than 10 median absolute deviations 
 ##from a rolling centered median (excluding the observation under consideration) of 50 observations if type = "standard".
 
@@ -269,7 +292,7 @@
     window = floor(window/2) * 2
     condition = c();
     halfwindow = window/2;
-    midquote = as.vector(as.numeric(qdata$BID) + as.numeric(qdata$OFFER))/2;
+    midquote = as.vector(as.numeric(qdata$BID) + as.numeric(qdata$OFR))/2;
     mad_all = mad(midquote);
 
     midquote = xts(midquote,order.by = index(qdata))
@@ -331,6 +354,12 @@
     qdata[condition];
 }
 
+###zivot
+correctedTrades <- function (tdata)
+{
+    filteredts = tdata[tdata$CR == " 0"]
+    return(filteredts)
+}
 
 ##########################  JUNK  #############################################################
 #conv =function(z){ 

Modified: pkg/RTAQ/R/convert_to_RData.R
===================================================================
--- pkg/RTAQ/R/convert_to_RData.R	2010-06-23 11:39:51 UTC (rev 344)
+++ pkg/RTAQ/R/convert_to_RData.R	2010-06-24 10:25:11 UTC (rev 345)
@@ -1,4 +1,4 @@
-##########HELPFUNCTION######
+##########HELPFUNCTION####
 readdata = function(path=NULL, extention="txt",header=F,dims=0){
 #extention should either be "txt" or "csv"
 if(!(extention=="txt"|extention=="csv")){print("Please select a supported extention")}
@@ -74,7 +74,7 @@
   #assign column names
   if(header==FALSE){
   if(is.null(tradecolnames)){
-  tradecolnames=c("SYMBOL","DATE","EX","TIME","PRICE","SIZE","COND","CR","G127");
+  tradecolnames=c("SYMBOL","DATE","EX","TIME","PRICE","SIZE","COND","CORR","G127");
   colnames(tdata)= tradecolnames;
   }else{
   colnames(tdata)= tradecolnames;
@@ -83,10 +83,10 @@
 
   ### solve issue when there is no COND ###
   cond=tdata$COND[is.na(tdata$G127)];
-  cr=tdata$CR[is.na(tdata$G127)];
+  cr=tdata$CORR[is.na(tdata$G127)];
 
   tdata$COND[is.na(tdata$G127)]=0;
-  tdata$CR[is.na(tdata$G127)]= as.character(cond);
+  tdata$CORR[is.na(tdata$G127)]= as.character(cond);
   tdata$G127[is.na(tdata$G127)] = as.character(cr);
   rm(cond,cr);
 
@@ -99,7 +99,7 @@
   ##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","CR","G127")];
+  tdata=tdata[,c("SYMBOL","EX","PRICE","SIZE","COND","CORR","G127")];
 
   rm(tdobject);
   }
@@ -136,7 +136,7 @@
   #assign column names
   if(header==FALSE){
   if(is.null(quotecolnames)){
-  quotecolnames = c("SYMBOL","DATE","EX","TIME","BID","BIDSIZE","OFFER","OFFERSIZE","MODE");  
+  quotecolnames = c("SYMBOL","DATE","EX","TIME","BID","BIDSIZ","OFR","OFRSIZ","MODE");  
   colnames(qdata)= quotecolnames;
   }else{
   colnames(qdata)= quotecolnames;
@@ -157,7 +157,7 @@
   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","BIDSIZE","OFFER","OFFERSIZE","MODE")];
+  qdata = qdata[,c("SYMBOL","EX","BID","BIDSIZ","OFR","OFRSIZ","MODE")];
   }
 
   xts_name = paste(ticker[i],"_quotes.RData",sep="");
@@ -165,3 +165,4 @@
   save(qdata, file = xts_name);
   }
   }
+

Modified: pkg/RTAQ/R/manipulation.R
===================================================================
--- pkg/RTAQ/R/manipulation.R	2010-06-23 11:39:51 UTC (rev 344)
+++ pkg/RTAQ/R/manipulation.R	2010-06-24 10:25:11 UTC (rev 345)
@@ -89,6 +89,8 @@
 
 
 matchtq = function(tdata,qdata,adjustment=2){ ##FAST VERSION
+tdata = dataformatc(tdata);
+qdata = dataformatc(qdata);
   tt = dim(tdata)[2];  
   index(qdata) = index(qdata) + adjustment;
 
@@ -114,6 +116,9 @@
 }
 
 matchtq_old = function(tdata,qdata,adjustment=2){ ##FAST VERSION
+qdata = dataformatc(qdata);
+tdata = dataformatc(tdata);
+
   tt = dim(tdata)[2];  
   index(qdata) = index(qdata) + adjustment;
   
@@ -140,10 +145,11 @@
 
 
 gettradedir = function(data){
+data = dataformatc(data);
 ##Function returns a vector with the inferred trade direction:
 ##NOTE: the value of the first (and second) observation should be ignored if price=midpoint for the first (second) observation.
   bid = as.numeric(data$BID);
-  offer = as.numeric(data$OFFER);
+  offer = as.numeric(data$OFR);
   midpoints = (bid + offer)/2;
   price = as.numeric(data$PRICE);
  
@@ -163,9 +169,10 @@
 
 
 es = function(data){
+data = dataformatc(data);
 #returns the effective spread as xts object
   bid = as.numeric(data$BID);
-  offer = as.numeric(data$OFFER);
+  offer = as.numeric(data$OFR);
   midpoints = (bid + offer)/2;
   price = as.numeric(data$PRICE);
   d = gettradedir(data);
@@ -176,6 +183,10 @@
 
 
 rs = function(data,tdata,qdata){
+data = dataformatc(data);
+qdata = dataformatc(qdata);
+tdata = dataformatc(tdata);
+
 ###Function returns the realized spread as an xts object
 #Please note that the returned object can contain less observations that the original "data"
 #because of the need to find quotes that match the trades 5 min ahead
@@ -200,7 +211,7 @@
 
 
   bid = as.numeric(data2$BID);
-  offer = as.numeric(data2$OFFER);
+  offer = as.numeric(data2$OFR);
   midpoints = (bid + offer)/2;
   price = as.numeric(data$PRICE);
   d = gettradedir(data);
@@ -211,6 +222,7 @@
 }
 
 value_trade = function(data){
+data = dataformatc(data);
 #returns the trade value as xts object
   price = as.numeric(data$PRICE);
   size = as.numeric(data$SIZE);
@@ -220,6 +232,7 @@
 }
 
 signed_value_trade = function(data){
+data = dataformatc(data);
 #returns the signed trade value as xts object
   price = as.numeric(data$PRICE);
   size = as.numeric(data$SIZE);
@@ -231,6 +244,7 @@
 
 
 signed_trade_size = function(data){
+data = dataformatc(data);
 #returns the signed size of the trade as xts object
   size = as.numeric(data$SIZE);
   d = gettradedir(data);
@@ -240,9 +254,10 @@
 }
 
 di_diff = function(data){
+data = dataformatc(data);
 #returns the depth imbalance (as a difference) as xts object
-  bidsize = as.numeric(data$BIDSIZE);
-  offersize = as.numeric(data$OFFERSIZE);
+  bidsize = as.numeric(data$BIDSIZ);
+  offersize = as.numeric(data$OFRSIZ);
 
   d = gettradedir(data);
   di = (d*(offersize-bidsize))/(offersize+bidsize);
@@ -251,9 +266,10 @@
 }
 
 di_div = function(data){
+data = dataformatc(data);
 #returns the depth imbalance (as a ratio) as xts object
-  bidsize = as.numeric(data$BIDSIZE);
-  offersize = as.numeric(data$OFFERSIZE);
+  bidsize = as.numeric(data$BIDSIZ);
+  offersize = as.numeric(data$OFRSIZ);
   d = gettradedir(data);
 
   di = (offersize/bidsize)^d;
@@ -262,10 +278,11 @@
 }
 
 pes = function(data){
+data = dataformatc(data);
 #returns the Proportional Effective Spread as xts object
   es = es(data);
   bid = as.numeric(data$BID);
-  offer = as.numeric(data$OFFER);
+  offer = as.numeric(data$OFR);
   midpoints = (bid + offer)/2;
 
   pes = 100*es/midpoints
@@ -273,19 +290,20 @@
   return(pes_xts);
 }
 
-prs = function(data){
+prs = function(data,tdata,qdata){
+data = dataformatc(data);
 #returns the Proportional Realized Spread as xts object
-  rs = rs(data);
+  rs = rs(data,tdata,qdata);
   bid = as.numeric(data$BID);
-  offer = as.numeric(data$OFFER);
+  offer = as.numeric(data$OFR);
   midpoints = (bid + offer)/2;
-
   prs = 100*rs/midpoints
   prs_xts = xts(prs,order.by=index(data));
   return(prs_xts);
 }
 
 price_impact = function(data){
+data = dataformatc(data);
 #returns the Price impact as xts object
   rs = rs(data);
   es = es(data);
@@ -296,11 +314,12 @@
 }
 
 prop_price_impact = function(data){
+data = dataformatc(data);
 #returns the Proportional Price impact as xts object
   rs = rs(data);
   es = es(data);
   bid = as.numeric(data$BID);
-  offer = as.numeric(data$OFFER);
+  offer = as.numeric(data$OFR);
   midpoints = (bid + offer)/2;
 
   prop_pi = (100*(es-rs)/2)/midpoints;
@@ -309,9 +328,10 @@
 }
 
 tspread = function(data){
+data = dataformatc(data);
 #returns the half traded spread as xts object
   bid = as.numeric(data$BID);
-  offer = as.numeric(data$OFFER);
+  offer = as.numeric(data$OFR);
   midpoints = (bid + offer)/2;
   price = as.numeric(data$PRICE);
   d = gettradedir(data);
@@ -321,9 +341,10 @@
 }
 
 pts = function(data){
+data = dataformatc(data);
 #returns the proportional half traded spread as xts object
   bid = as.numeric(data$BID);
-  offer = as.numeric(data$OFFER);
+  offer = as.numeric(data$OFR);
   midpoints = (bid + offer)/2;
   price = as.numeric(data$PRICE);
   d = gettradedir(data);
@@ -334,6 +355,7 @@
 }
 
 p_return_sqr = function(data){
+data = dataformatc(data);
 #returns the squared log return on Trade prices as xts object
   price = as.numeric(data$PRICE);
   return = c(0,log(price[2:length(price)])-log(price[1:length(price)-1]));
@@ -343,20 +365,11 @@
   return(sqr_return_xts);
 }
 
-p_return_abs = function(data){
-#returns the absolute log return on Trade prices as xts object
-  price = as.numeric(data$PRICE);
-  return = c(0,log(price[2:length(price)])-log(price[1:length(price)-1]));
-  abs_return = abs(return);
-
-  abs_return_xts = xts(abs_return,order.by=index(data));
-  return(sqr_return_xts);
-}
-
 qs = function(data){
+data = dataformatc(data);
 #returns the quoted spread as xts object
   bid = as.numeric(data$BID);
-  offer = as.numeric(data$OFFER);
+  offer = as.numeric(data$OFR);
   qs = offer-bid;
 
   qs_xts = xts(qs,order.by=index(data));
@@ -364,9 +377,10 @@
 }
 
 pqs = function(data){
+data = dataformatc(data);
 #returns the proportional quoted spread as xts object
   bid = as.numeric(data$BID);
-  offer = as.numeric(data$OFFER);
+  offer = as.numeric(data$OFR);
   midpoints = (bid + offer)/2;
   qs = offer-bid;
   pqs = 100*qs/midpoints;
@@ -376,9 +390,10 @@
 }
 
 logqs = function(data){
+data = dataformatc(data);
 #returns the logarithm of the quoted spread as xts object
   bid = as.numeric(data$BID);
-  offer = as.numeric(data$OFFER);
+  offer = as.numeric(data$OFR);
   logqs = log(offer/bid);
 
   logqs_xts = xts(logqs,order.by=index(data));
@@ -386,9 +401,10 @@
 }
 
 logsize = function(data){
+data = dataformatc(data);
 #returns the log quoted size as xts object
-  bidsize = as.numeric(data$BIDSIZE);
-  offersize = as.numeric(data$OFFERSIZE);
+  bidsize = as.numeric(data$BIDSIZ);
+  offersize = as.numeric(data$OFRSIZ);
   logsize = log(bidsize)+log(offersize);
 
   logsize_xts = xts(logsize,order.by=index(data));
@@ -396,6 +412,7 @@
 }
 
 qslope = function(data){
+data = dataformatc(data);
 #returns the quoted slope as xts object
   logsize = logsize(data);
   qs = qs(data);
@@ -407,6 +424,7 @@
 }
 
 logqslope = function(data){
+data = dataformatc(data);
 #returns the log quoted slope as xts object
   logqs = logqs(data);
   logsize = logsize(data);
@@ -420,6 +438,7 @@
 
 
 mq_return_sqr = function(data){
+data = dataformatc(data);
 #returns midquote squared returns slope as xts object
   mq_return = mq_return(data);
   
@@ -430,6 +449,7 @@
 }
 
 mq_return_abs = function(data){
+data = dataformatc(data);
 #returns absolute midquote returns slope as xts object
   mq_return = mq_return(data);
   
@@ -439,34 +459,42 @@
   return(mq_return_abs_xts);
 }
 
-liquidity = function(data,tdata,qdata){
+liquidity <- function (data, tdata, qdata)
+{
+data = dataformatc(data);
+qdata = dataformatc(qdata);
+tdata = dataformatc(tdata);
 ##Function computes many liquidity measures and returns an xts object containing them
 
 ##First part solves the problem that unequal number of obs (in data and data2) is possible when computing the RS
-  data2 = matchtq(tdata,qdata,adjustment =300);
-  if(dim(data2)[1]>dim(data)[1]){
-  condition = as.vector(as.character(index(data2)))%in%as.vector(as.character(index(data)));
-  data2 = subset(data2,condition,select=1:(dim(data)[2]));
-  data = subset(data,as.vector(as.character(index(data)))%in%as.vector(as.character(index(data2))),select=1:(dim(data2)[2]));
-  }
-  if(dim(data2)[1]<dim(data)[1]){
-  condition = as.vector(as.character(index(data)))%in%as.vector(as.character(index(data2)));
-  data = subset(data,condition,select=1:(dim(data2)[2]));
-  data2 = subset(data2,as.vector(as.character(index(data2)))%in%as.vector(as.character(index(data))),select=1:(dim(data)[2]));
-  }
 
+    data2 = matchtq(tdata, qdata, adjustment = 300)
+    if (dim(data2)[1] > dim(data)[1]) {
+        condition = as.vector(as.character(index(data2))) %in%
+            as.vector(as.character(index(data)))
+        data2 = subset(data2, condition, select = 1:(dim(data)[2]))
+        data = subset(data, as.vector(as.character(index(data))) %in%
+            as.vector(as.character(index(data2))), select = 1:(dim(data2)[2]))
+    }
+    if (dim(data2)[1] < dim(data)[1]) {
+        condition = as.vector(as.character(index(data))) %in%
+            as.vector(as.character(index(data2)))
+        data = subset(data, condition, select = 1:(dim(data2)[2]))
+        data2 = subset(data2, as.vector(as.character(index(data2))) %in%
+            as.vector(as.character(index(data))), select = 1:(dim(data)[2]))
+    }
     bid = as.numeric(data$BID)
-    offer = as.numeric(data$OFFER)
+    offer = as.numeric(data$OFR)
     midpoints = (bid + offer)/2
     price = as.numeric(data$PRICE)
     size = as.numeric(data$SIZE)
     d = gettradedir(data)
-    bidsize = as.numeric(data$BIDSIZE)
-    offersize = as.numeric(data$OFFERSIZE)
-    return = c(0, log(price[2:length(price)]) - log(price[1:length(price) - 
+    bidsize = as.numeric(data$BIDSIZ)
+    offersize = as.numeric(data$OFRSIZ)
+    return = c(0, log(price[2:length(price)]) - log(price[1:length(price) -
         1]))
     mq_return = mq_return(data)
-    midpoints2 = (as.numeric(data2$BID) + as.numeric(data2$OFFER))/2
+    midpoints2 = (as.numeric(data2$BID) + as.numeric(data2$OFR))/2
     es = 2 * d * (price - midpoints)
     rs = 2 * d * (price - midpoints2)
     value_trade = price * size
@@ -490,27 +518,51 @@
     logqslope = logqs/logsize
     mq_return_sqr = mq_return^2
     mq_return_abs = abs(mq_return)
-    liquid = cbind(es, rs, value_trade, signed_value_trade, di_diff, 
-        di_div, pes, prs, price_impact, prop_price_impact, tspread, 
-        pts, p_return_sqr, p_return_abs, qs, pqs, logqs, logsize, 
+    liquid = cbind(es, rs, value_trade, signed_value_trade, di_diff,
+        di_div, pes, prs, price_impact, prop_price_impact, tspread,
+        pts, p_return_sqr, p_return_abs, qs, pqs, logqs, logsize,
         qslope, logqslope, mq_return_sqr, mq_return_abs)
-    names = c("es", "rs", "value_trade", "signed_value_trade", 
-        "di_diff", "di_div", "pes", "prs", "price_impact", "prop_price_impact", 
-        "tspread", "pts", "p_return_sqr", "p_return_abs", "qs", "pqs", 
-        "logqs", "logsize", "qslope", "logqslope", "mq_return_sqr", 
-        "mq_return_abs");
-    colnames(liquid) = names;
-    return(liquid);
+    names = c("es", "rs", "value_trade", "signed_value_trade",
+        "di_diff", "di_div", "pes", "prs", "price_impact", "prop_price_impact",
+        "tspread", "pts", "p_return_sqr", "p_return_abs", "qs", "pqs",
+        "logqs", "logsize", "qslope", "logqslope", "mq_return_sqr",
+        "mq_return_abs")
+    colnames(liquid) = names
+    return(liquid)
 }
 
 ##help_function:
 mq_return = function(data){
+data = dataformatc(data);
 #function returns the midquote logreturns as xts object
   bid = as.numeric(data$BID);
-  offer = as.numeric(data$OFFER);
+  offer = as.numeric(data$OFR);
   midpoints = (bid + offer)/2;
   mq_return = c(0,log(midpoints[2:length(midpoints)])-log(midpoints[1:length(midpoints)-1]));
 
   mq_return_xts = xts(mq_return,order.by=index(data));
   return(mq_return_xts);
-}
\ No newline at end of file
+}
+
+
+###Zivot:
+p_return <- function (data)
+{
+    price = as.numeric(data$PRICE)
+    log.return = c(0, log(price[2:length(price)]) - log(price[1:length(price) -
+        1]))
+    return_xts = xts(log.return, order.by = index(data))
+    return(return_xts)
+}
+
+p_return_abs <- function (data)
+{
+    price = as.numeric(data$PRICE)
+    return = c(0, log(price[2:length(price)]) - log(price[1:length(price) -
+        1]))
+    abs_return = abs(return)
+    abs_return_xts = xts(abs_return, order.by = index(data))
+    return(abs_return_xts)
+}
+
+

Modified: pkg/RTAQ/R/totalcleanup.R
===================================================================
--- pkg/RTAQ/R/totalcleanup.R	2010-06-23 11:39:51 UTC (rev 344)
+++ pkg/RTAQ/R/totalcleanup.R	2010-06-24 10:25:11 UTC (rev 345)
@@ -1,3 +1,11 @@
+dataformatc = function(a){
+if(any(colnames(a)=="CR")){colnames(a)[colnames(a)=="CR"]="CORR"}
+if(any(colnames(a)=="OFFER")){colnames(a)[colnames(a)=="OFFER"]="OFR"}
+if(any(colnames(a)=="BIDSIZE")){colnames(a)[colnames(a)=="BIDSIZE"]="BIDSIZ"}
+if(any(colnames(a)=="OFFERSIZE")){colnames(a)[colnames(a)=="OFFERSIZE"]="OFRSIZ"}
+return(a)
+}
+
 #TRADES CLEANUP WRAPPER
 tradescleanup = function(from="2008-01-03",to="2008-01-03",datasource,datadestination,ticker,exchange){
   dates = timeSequence(from,to, format = "%Y-%m-%d", FinCenter = "GMT");
@@ -16,6 +24,8 @@
   if(class(tdata)!="try-error"){
   exchange = exchanges[exchanges==ticker[i]];  
 
+  tdata = dataformatc(tdata);
+
   ##actual clean-up: 
   ##general:
   tdata = try(nozeroprices(tdata));
@@ -60,10 +70,12 @@
   load(paste(datasourcex,"\\",dataname,sep=""));
   load(paste(datasourcex,"\\",dataname2,sep=""));
 
+  tdata = dataformatc(tdata);
+  qdata = dataformatc(qdata);
+
   #1 cleaning procedure that needs cleaned trades and quotes
   tdata = try(rmtradeoutliers(tdata,qdata));
 
-
   #save
   save(tdata, file = paste(datadestinationx,"\\",dataname,sep=""));
   a=TRUE;
@@ -98,6 +110,7 @@
   exchange = exchanges[exchanges==ticker[i]];  
   if(exchange=="Q"){exchange="T"}
 
+  qdata = dataformatc(qdata);
   ##actual clean-up:
   ##general:
   qdata = try(nozeroquotes(qdata));

Modified: pkg/RTAQ/man/prs.Rd
===================================================================
--- pkg/RTAQ/man/prs.Rd	2010-06-23 11:39:51 UTC (rev 344)
+++ pkg/RTAQ/man/prs.Rd	2010-06-24 10:25:11 UTC (rev 345)
@@ -18,13 +18,15 @@
 }
 
 \usage{
-prs(data);
+prs(data,tdata,qdata);
 }
 
 \arguments{
   \item{data}{ 
 xts object, containing joined trades and quotes (e.g. using \code{\link{matchtq}})
 }
+  \item{tdata}{xts object, containing the trade data. See pdf documentation for more details.}
+  \item{qdata}{xts object, containing the quote data. See pdf documentation for more details.}
 }
 
 \section{Details}{



More information about the Blotter-commits mailing list