# The package includes a preloaded dataset called 'hfdata'.
# This dataset is an artificially created high-frequency trading data
# containing 100,000 trades and five variables: 'timestamp', 'price',
# 'volume', 'bid', and 'ask'. For more information, type ?hfdata.
xdata <- hfdata
### Estimation of the VPIN model ###
# \donttest{
# Estimate the VPIN model using the following parameters:
# - timebarsize: 5 minutes (300 seconds)
# - buckets: 50 buckets per average daily volume
# - samplength: 250 for the VPIN calculation
estimate <- vpin(xdata, timebarsize = 300, buckets = 50,
samplength = 250)
# Display a description of the VPIN estimate
show(estimate)
# Display the parameters of the VPIN estimates
show(estimate@parameters)
# Display the summary statistics of the VPIN vector
summary(estimate@vpin)
# Store the computed data of the different buckets in a dataframe 'buckets'
# and display the first 10 rows of the dataframe.
buckets <- estimate@bucketdata
show(head(buckets, 10))
# Display the first 10 rows of the dataframe containing daily vpin values.
dayvpin <- estimate@dailyvpin
show(head(dayvpin, 10))
### Estimation of the IVPIN model ###
# Estimate the IVPIN model using the same parameters as above.
# The grid_size parameter is unspecified and will default to 5.
iestimate <- ivpin(xdata[1:50000,], timebarsize = 300, samplength = 50, verbose = FALSE)
# Display the summary statistics of the IVPIN vector
summary(iestimate@ivpin)
# The output of ivpin() also contains the VPIN vector in the @vpin slot.
# Plot the VPIN and IVPIN vectors in the same plot using the iestimate object.
# Define the range for the VPIN and IVPIN vectors, removing NAs.
vpin_range <- range(c(iestimate@vpin, iestimate@ivpin), na.rm = TRUE)
# Plot the VPIN vector in blue
plot(iestimate@vpin, type = "l", col = "blue", ylim = vpin_range,
ylab = "VPIN/iVPIN", xlab = "Bucket", main = "Plot of VPIN and IVPIN")
# Add the IVPIN vector in red
lines(iestimate@ivpin, type = "l", col = "red")
# Add a legend to the plot
legend("topright", legend = c("VPIN", "IVPIN"), col = c("blue", "red"),
lty = 1,
cex = 0.6, # Adjust the text size
x.intersp = 1.2, # Adjust the horizontal spacing
y.intersp = 2, # Adjust the vertical spacing
inset = c(0.05, 0.05)) # Adjust the position slightly
# }
Run the code above in your browser using DataLab