-
-
Save ProbablePattern/c46e4fb12bf758b99b03 to your computer and use it in GitHub Desktop.
#### VPIN calculation ######################################################### | |
#install.packages('fasttime',repos='http://www.rforge.net/') | |
require(data.table); require(fasttime); require(plyr) | |
# Assuming TAQ data is arranged in 1 year stock csv files | |
stock=fread('/TAQ_data.csv'); stock=stock[,1:3,with=FALSE] | |
setnames(stock,colnames(stock),c('DateTime','Price','Volume')); | |
stock[,DateTime:=paste(paste(substr(DateTime,1,4),substr(DateTime,5,6), | |
substr(DateTime,7,8),sep='-'),substr(DateTime,10,17))] | |
setkey(stock,DateTime); | |
stock=as.xts(stock[,2:3,with=FALSE],unique=FALSE, | |
order.by=fastPOSIXct(stock[,DateTime],tz='GMT')) | |
# Now we have an xts data frame called 'stock' with a DateTime index and... | |
# two columns: Price and Volume | |
# Vbucket=Number of volume buckets in an average volume day (Vbucket=50) | |
VPIN=function(stock,Vbucket) { | |
stock$dP1=diff(stock[,'Price'],lag=1,diff=1,na.pad=TRUE) | |
ends=endpoints(stock,'minutes') | |
timeDF=period.apply(stock[,'dP1'],INDEX=ends,FUN=sum) | |
timeDF$Volume=period.apply(stock[,'Volume'],INDEX=ends,FUN=sum) | |
Vbar=mean(period.apply(timeDF[,'Volume'],INDEX=endpoints(timeDF,'days'), | |
FUN=sum))/Vbucket | |
timeDF$Vfrac=timeDF[,'Volume']/Vbar | |
timeDF$CumVfrac=cumsum(timeDF[,'Vfrac']) | |
timeDF$Next=(timeDF[,'CumVfrac']-floor(timeDF[,'CumVfrac']))/timeDF[,'Vfrac'] | |
timeDF[timeDF[,'Next']<1,'Next']=0 | |
timeDF$Previous=lag(timeDF[,'dP1'])*lag(timeDF[,'Next']) | |
timeDF$dP2=(1-timeDF[,'Next'])*timeDF[,'dP1'] + timeDF[,'Previous'] | |
timeDF$Vtick=floor(timeDF[,'CumVfrac']) | |
timeDF[,'Vtick']=timeDF[,'Vtick']-diff(timeDF[,'Vtick']); timeDF[1,'Vtick']=0 | |
timeDF=as.data.frame(timeDF); timeDF[,'DateTime']=row.names(timeDF) | |
timeDF=ddply(as.data.frame(timeDF),.(Vtick),last) | |
timeDF=as.xts(timeDF[,c('Volume','dP2','Vtick')], | |
order.by=fastPOSIXct(timeDF$DateTime,tz='GMT')) | |
timeDF[1,'dP2']=0 | |
timeDF$sigma=rollapply(timeDF[,'dP2'],Vbucket,sd,fill=NA) | |
timeDF$sigma=na.fill(timeDF$sigma,"extend") | |
timeDF$Vbuy=Vbar*pnorm(timeDF[,'dP2']/timeDF[,'sigma']) | |
timeDF$Vsell=Vbar-timeDF[,'Vbuy'] | |
timeDF$OI=abs(timeDF[,'Vsell']-timeDF[,'Vbuy']) | |
timeDF$VPIN=rollapply(timeDF[,'OI'],Vbucket,sum)/(Vbar*Vbucket) | |
timeDF=timeDF[,c('VPIN')]; return(timeDF) | |
} | |
out=VPIN(stock,50) | |
############################################################################### |
A few things to note:
-If you don't use fasttime, you will also need to change the fasttime call in the VPIN function in addition to the first change you made before the VPIN function call.
-Without volume variation, there is no point to looking at PIN in volume time.
-The sample you sent me only has 1 minute of data. Since the VPIN algo uses 1 minute bars in addition to the volume buckets, you won't be able to get an observation with just 1 second of data. You could rewrite the endpoints to use 1 second time bars if you have a sufficient number of trades throughout the day.
-Aside from these points, the algo runs on your data up through the call to fasttime in the VPIN function. Beyond that, I would need to see data that spans more than 1 minute.
Hi, can you repeat flash crash figure in Easley, ohara's 2012 paper? It seems that your code is quite sensitive to the parameter Vbucket? VPIN doesn't increase even in the flash crash time using Emini SP500 data.
@gstar1990,ELO(2012) calculate
Would you explain what does "Next", "Previous" and "dP2" mean?
Thanks a lot!
Hi can you please suggest that can we use this methodology and code if one has data aggregated to 1 minute frequency ?
Will the quote above work on the 10 years treasury contract data? I have data I compiled with Datetime, Price and Volume (10,000 at each time). I have tried using the code several time without success. I will kindly appreciate any advice. I could share the data set I have if you don't mind. Thank you in advance.
Here is the link of the file I am using, any advice if this is not a correct set of data that can work with the code above:
https://www.dropbox.com/s/3duo99t6frk0a0q/dat.csv?dl=0
Here is how I modified the first part of the code to fit my data:
stock<-fread('dat.csv');stock=stock[,1:3,with=FALSE]
require(data.table); require(fasttime); require(plyr)
setnames(stock,colnames(stock),c('DateTime','Price','Volume'));
setkey(stock,DateTime);
stock$DateTime<-as.POSIXct(stock$DateTime) #POSIXt
library(zoo)
library(xts)
stock=as.xts(stock[,2:3,with=FALSE],unique=FALSE,
order.by=stock[,DateTime],tz='GMT')
After running the above, I ran the VPIN function above without success, and I am just curious if it has to do with my data.
Thank you very much!