# There is a preloaded dataset called 'hfdata' contained in the package.
# It is an artificially created high-frequency trading data. The dataset
# contains 100 000 trades and five variables 'timestamp', 'price',
# 'volume', 'bid', and 'ask'. For more information, type ?hfdata.
xdata <- hfdata
xdata$volume <- NULL
# \donttest{
# Use the LR algorithm with a timelag of 0.5 seconds i.e. 500000
# microseconds to classify high-frequency trades in the dataset 'xdata'
lgtrades <- classify_trades(xdata, "LR", timelag = 500000, verbose = FALSE)
# LR algorithm with a 0.5-second lead (-500000 microseconds)
ldtrades <- classify_trades(xdata, "LR", timelag = -500000, verbose = FALSE)
# Compare the number of buyer- and seller-initiated trades between the
# lagged and leading LR classifications.
comparison_tbl <- rbind(
transform(lgtrades, version = "lag of 0.5s"),
transform(ldtrades, version = "lead of 0.5s")
)
comparison_tbl <- with(comparison_tbl,
aggregate(list(Buys = as.logical(isbuy), Sells = !as.logical(isbuy)),
by = list(version = version),
FUN = sum, na.rm = TRUE)
)
show(comparison_tbl)
# Use the EMO algorithm with a timelag of 1 second, i.e. 1000000 microseconds
# to aggregate intraday data in 'xdata' at a frequency of 15 minutes.
emotrades <- aggregate_trades(xdata, algorithm = "EMO", timelag = 1000000,
frequency = "min", unit = 15, verbose = FALSE)
# Use the Quote algorithm with a timelag of 1 second to aggregate intraday
# data in the dataset 'xdata' at a daily frequency.
qtrades <- aggregate_trades(xdata, algorithm = "Quote", timelag = 1000000,
frequency = "day", unit = 1, verbose = FALSE)
# Since the argument 'fullreport' is set to FALSE by default, then the
# output 'qtrades' can be used directly for the estimation of the PIN
# model, namely using pin_ea().
estimate <- pin_ea(qtrades, verbose = FALSE)
# Show the estimate
show(estimate)
# }
Run the code above in your browser using DataLab