-
-
Save ProbablePattern/c46e4fb12bf758b99b03 to your computer and use it in GitHub Desktop.
VPIN calculation using bulk-volume classification
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #### 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) | |
| ############################################################################### |
Author
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 ?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.