[Blotter-commits] r333 - pkg/RTAQ/R
noreply at r-forge.r-project.org
noreply at r-forge.r-project.org
Tue May 25 15:14:32 CEST 2010
Author: jonathan
Date: 2010-05-25 15:14:31 +0200 (Tue, 25 May 2010)
New Revision: 333
Modified:
pkg/RTAQ/R/convert_to_RData.R
pkg/RTAQ/R/manipulation.R
Log:
Modified: pkg/RTAQ/R/convert_to_RData.R
===================================================================
--- pkg/RTAQ/R/convert_to_RData.R 2010-04-29 20:55:57 UTC (rev 332)
+++ pkg/RTAQ/R/convert_to_RData.R 2010-05-25 13:14:31 UTC (rev 333)
@@ -1,24 +1,38 @@
##########HELPFUNCTION######
-readdata = function(path=NULL, extention="txt",header=F){
+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")}
+colnames = rep("x",dims);
#load txt
if(extention == "txt"){
fullpath = paste(path,".txt",sep="");
-data = try(read.delim(fullpath,sep="",header=header,dec=","),silent=TRUE);
+data = try(read.delim(fullpath,sep="",header=header,dec=",",col.names=colnames),silent=TRUE);
+
+ if(is.null(dim(data))){
+ data = try(read.delim(fullpath,sep="",header=header,dec=",",col.names=c(colnames,"EXTRA")),silent=TRUE);
+ if(is.null(dim(data))){data=matrix(nrow=0,ncol=9);
+ }else{data=data[,(-dim(data)[2])]}
+ }
}
+
if(extention == "csv"){
fullpath = paste(path,".csv",sep="");
-data = try(read.delim(fullpath,sep=",",header=header,dec="."),silent=TRUE);
+data = try(read.delim(fullpath,sep=",",header=header,dec=".",col.names=colnames),silent=TRUE);
+
+ if(is.null(dim(data))){
+ data = try(read.delim(fullpath,sep=",",header=header,dec=".",col.names=c(colnames,"EXTRA")),silent=TRUE);
+ if(is.null(dim(data))){data=matrix(nrow=0,ncol=9);
+ }else{data=data[,(-dim(data)[2])]}
+ }
}
-
return(data);
}
+
############################
-
convert = function(from,to,datasource,datadestination,trades=TRUE,quotes=TRUE,ticker,dir=F,extention="txt",header=F,tradecolnames=NULL,quotecolnames=NULL,format="%m/%d/%Y %H:%M:%S"){
dates = timeSequence(from,to, format = "%Y-%m-%d", FinCenter = "GMT")
dates = dates[isBizday(dates, holidays = holidayNYSE(2004:2010))];
+ missingt=missingq=matrix(ncol=2,nrow=0);
if(dir) {
dir.create(datadestination);
@@ -27,16 +41,17 @@
dir.create(dirname);
}
}
-
+
for(i in 1:length(dates)){
- datasource = paste(datasource,"\\",dates[i],sep="");
- datadestination = paste(datadestination,"\\",dates[i],sep="");
- if(trades==TRUE){convert_trades(datasource,datadestination,ticker,extention=extention,header=header,tradecolnames=tradecolnames,format=format)}
- if(quotes==TRUE){convert_quotes(datasource,datadestination,ticker,extention=extention,header=header,quotecolnames=quotecolnames,format=format)}
+ datasourcex = paste(datasource,"\\",dates[i],sep="");
+ datadestinationx = paste(datadestination,"\\",dates[i],sep="");
+ if(trades==TRUE){convert_trades(datasourcex,datadestinationx,ticker,extention=extention,header=header,tradecolnames=tradecolnames,format=format)}
+ if(quotes==TRUE){convert_quotes(datasourcex,datadestinationx,ticker,extention=extention,header=header,quotecolnames=quotecolnames,format=format)}
}
}
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,":"));
@@ -46,13 +61,13 @@
}
for(i in 1:length(ticker)){
- tfile_name = paste(ticker[i],"_trades",sep="");
- tdata = try(readdata(path=tfile_name, extention=extention,header=header),silent=TRUE);
- error = is.null(dim(tdata));
+ 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(currentdate,ticker[i]));
+ missingt = rbind(missingt,c(datasource,ticker[i]));
}
if(error==FALSE){
@@ -71,8 +86,8 @@
cr=tdata$CR[is.na(tdata$G127)];
tdata$COND[is.na(tdata$G127)]=0;
- tdata$CR[is.na(tdata$G127)]= cond;
- tdata$G127[is.na(tdata$G127)] = cr;
+ tdata$CR[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)
@@ -92,6 +107,7 @@
xts_name = paste(ticker[i],"_trades.RData",sep="");
setwd(datadestination);
save(tdata, file = xts_name);
+
}
}
@@ -106,14 +122,14 @@
}
for(i in 1:length(ticker)){
- qfile_name = paste(ticker[i],"_quotes",sep="");
+ qfile_name = paste(datasource,"\\",ticker[i],"_quotes",sep="");
- qdata=try(readdata(path=qfile_name, extention=extention,header=header),silent=TRUE);
- error = is.null(dim(qdata));
+ 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(currentdate,ticker[i]));
+ missingq=rbind(missingq,c(datasource,ticker[i]));
}
if(error==FALSE){
@@ -149,6 +165,3 @@
save(qdata, file = xts_name);
}
}
-
-
-
Modified: pkg/RTAQ/R/manipulation.R
===================================================================
--- pkg/RTAQ/R/manipulation.R 2010-04-29 20:55:57 UTC (rev 332)
+++ pkg/RTAQ/R/manipulation.R 2010-05-25 13:14:31 UTC (rev 333)
@@ -308,7 +308,6 @@
return(prop_pi_xts);
}
-
tspread = function(data){
#returns the half traded spread as xts object
bid = as.numeric(data$BID);
@@ -440,7 +439,6 @@
return(mq_return_abs_xts);
}
-
liquidity = function(data,tdata,qdata){
##Function computes many liquidity measures and returns an xts object containing them
@@ -457,52 +455,52 @@
data2 = subset(data2,as.vector(as.character(index(data2)))%in%as.vector(as.character(index(data))),select=1:(dim(data)[2]));
}
-##Variables needed for the computation of the liquidity measures
- bid = as.numeric(data$BID);
- offer = as.numeric(data$OFFER);
- 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)-1]));
- mq_return = mq_return(data);
- midpoints2 = (as.numeric(data2$BID)+as.numeric(data2$OFFER))/2;
-
-##Liquidity measures:
- es = 2*d*(price-midpoints);
- rs = 2*d*(price-midpoints2);
- value_trade = price*size;
- signed_value_trade = d*price*size;
- signed_trade_size = d*size;
- di_diff = (d*(offersize-bidsize))/(offersize+bidsize);
- di_div = (offersize/bidsize)^d;
- pes = 100*es/midpoints;
- prs = 100*rs/midpoints;
- price_impact = (es-rs)/2;
- prop_price_impact = (100*price_impact)/midpoints;
- tspread = d*(price-midpoints);
- pts = ts/midpoints;
- p_return_sqr = return^2;
- p_return_abs = abs(return);
- qs = offer-bid;
- pqs = 100*qs/midpoints;
- logqs = log(offer/bid);
- logsize = log(bidsize)+log(offersize);
- qslope = qs/logsize;
- 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,ts,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","ts","pts","p_return_sqr","p_return_abs","qs","pqs","logqs","logsize","qslope","logqslope","mq_return_sqr","mq_return_abs");
-colnames(liquid) = names;
-
-return(liquid);
+ bid = as.numeric(data$BID)
+ offer = as.numeric(data$OFFER)
+ 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) -
+ 1]))
+ mq_return = mq_return(data)
+ midpoints2 = (as.numeric(data2$BID) + as.numeric(data2$OFFER))/2
+ es = 2 * d * (price - midpoints)
+ rs = 2 * d * (price - midpoints2)
+ value_trade = price * size
+ signed_value_trade = d * price * size
+ signed_trade_size = d * size
+ di_diff = (d * (offersize - bidsize))/(offersize + bidsize)
+ di_div = (offersize/bidsize)^d
+ pes = 100 * es/midpoints
+ prs = 100 * rs/midpoints
+ price_impact = (es - rs)/2
+ prop_price_impact = (100 * price_impact)/midpoints
+ tspread = d * (price - midpoints)
+ pts = tspread/midpoints
+ p_return_sqr = return^2
+ p_return_abs = abs(return)
+ qs = offer - bid
+ pqs = 100 * qs/midpoints
+ logqs = log(offer/bid)
+ logsize = log(bidsize) + log(offersize)
+ qslope = qs/logsize
+ 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,
+ 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);
}
##help_function:
More information about the Blotter-commits
mailing list