Skip to content

Commit

Permalink
remove logging
Browse files Browse the repository at this point in the history
  • Loading branch information
phil8192 committed Oct 28, 2016
1 parent 75949a5 commit 758685c
Show file tree
Hide file tree
Showing 10 changed files with 3 additions and 59 deletions.
2 changes: 0 additions & 2 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,5 @@ importFrom(stats,quantile)
importFrom(stats,time)
importFrom(utils,head)
importFrom(utils,read.csv)
importFrom(utils,setTxtProgressBar)
importFrom(utils,tail)
importFrom(utils,timestamp)
importFrom(utils,txtProgressBar)
7 changes: 0 additions & 7 deletions R/auxiliary.R
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,3 @@ intervalPriceLevelGaps <-
function(volume, breaks) intervalSumBreaks(ifelse(volume == 0, 1, 0),
breaks)

# logs to console in form of:
# calling function => msg
logger <- function(msg) {
caller <- head(sys.call(-1), 1)
cat(paste0(" ", caller, " => ", msg, "\n"))
}

7 changes: 1 addition & 6 deletions R/data.R
Original file line number Diff line number Diff line change
Expand Up @@ -75,17 +75,14 @@ processData <- function(csv.file) {
zombie.ids <- getZombieIds(events, trades)
zombies <- events[events$id %in% zombie.ids, ]
events <- events[!events$id %in% zombie.ids, ]
logger(paste("removed", length(zombie.ids), "zombies"))
created.ids <- events[events$action == "created", ]$id
duplicate.update.event.ids <- events[events$type != "pacman" & events$action
== "changed" & events$fill == 0 & events$id %in% created.ids, ]$event.id
events <- events[!events$event.id %in% duplicate.update.event.ids, ]
logger(paste("removed", length(duplicate.update.event.ids),
warning(paste("removed", length(duplicate.update.event.ids),
"duplicated updates"))
depth <- priceLevelVolume(events)
logger("calculating depth metrics (may take some time...)")
depth.summary <- depthMetrics(depth)
logger("calculating order aggressiveness...")
events <- orderAggressiveness(events, depth.summary)
# depth summary data starts 1 minute later to allow for order book population.
offset <- min(events$timestamp) + 60
Expand Down Expand Up @@ -114,7 +111,6 @@ processData <- function(csv.file) {
##' }
##' @export loadData
loadData <- function(bin.file, ...) {
logger(paste("loading binary from", bin.file))
readRDS(file=bin.file, ...)
}

Expand All @@ -135,6 +131,5 @@ loadData <- function(bin.file, ...) {
##' }
##' @export saveData
saveData <- function(lob.data, bin.file, ...) {
logger(paste("saving binary to", bin.file))
saveRDS(lob.data, file=bin.file, ...)
}
13 changes: 0 additions & 13 deletions R/depth.R
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,8 @@ priceLevelVolume <- function(events) {
side=volume.deltas$direction)
}

logger("calculating priceLevelVolume from bid event deltas...")
bids <- events[events$direction == "bid", ]
depth.bid <- directionalPriceLevelVolume(bids)
logger("calculating priceLevelVolume from ask event deltas...")
asks <- events[events$direction == "ask", ]
depth.ask <- directionalPriceLevelVolume(asks)
depth.data <- rbind(depth.bid, depth.ask)
Expand Down Expand Up @@ -137,31 +135,24 @@ priceLevelVolume <- function(events) {
filterDepth <- function(d, from, to) {

# 1. get all active price levels before start of range.
logger(paste("filterDepth between", from, "and", to))
pre <- d[d$timestamp <= from, ]
logger(paste("got", nrow(pre), "previous deltas"))
pre <- pre[order(pre$price, pre$timestamp), ]
logger(paste("ordered", nrow(pre), "previous deltas"))

# last update for each price level <= from. this becomes the starting point
# for all updates within the range.
pre <- pre[!duplicated(pre$price, fromLast=T) & pre$volume > 0, ]
logger(paste("extracted", nrow(pre), "previously updated deltas"))

# clamp range (reset timestamp to from if price level active before start of
# range.
if(nrow(pre) > 0) {
pre$timestamp <- as.POSIXct(sapply(pre$timestamp, function(r) {
max(from, r)
}), origin="1970-01-01", tz="UTC")
logger("clamped range.")
}

# 2. add all volume change within the range.
mid <- d[d$timestamp > from & d$timestamp < to, ]
logger(paste("got", nrow(mid), "in range deltas"))
range <- rbind(pre, mid)
logger(paste("appended range now contains", nrow(range), "deltas"))

# 3. at the end of the range, set all price level volume to 0.
open.ends <- data.frame(timestamp=to,
Expand All @@ -171,8 +162,6 @@ filterDepth <- function(d, from, to) {
# combine pre, mid and open.ends. ensure it is in order.
range <- rbind(range, open.ends)
range <- range[order(range$price, range$timestamp), ]
logger(paste("closed range. depth filtering resulted in",
length(unique(range$price)), "price levels."))

range
}
Expand Down Expand Up @@ -204,7 +193,6 @@ filterDepth <- function(d, from, to) {
##' @author phil
##' @keywords internal
depthMetrics <- function(depth, bps=25, bins=20) {
pb <- txtProgressBar(1, nrow(depth), 0, style=3)
pctNames <- function(name) paste0(name, seq(bps, bps*bins, bps), "bps")
ordered.depth <- depth[order(depth$timestamp), ]
ordered.depth$price <- as.integer(round(100*ordered.depth$price))
Expand Down Expand Up @@ -299,7 +287,6 @@ depthMetrics <- function(depth, bps=25, bins=20) {
if(i > 1) metrics[i, ] <- metrics[i - 1, ]
}
}
setTxtProgressBar(pb, i)
}

# back into $
Expand Down
5 changes: 1 addition & 4 deletions R/event-processing.R
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,12 @@ loadEventData <- function(file) {
deletes[which(duplicated(deletes$id)), ]$id, ]
duplicate.event.ids <- duplicate.deletes[duplicated(duplicate.deletes$id),
]$event.id
logger(paste("removed", length(duplicate.event.ids),
warning(paste("removed", length(duplicate.event.ids),
"duplicate order cancellations: ",
paste(events[duplicate.event.ids, ]$id, collapse=" ")))
events[!events$event.id %in% duplicate.event.ids, ]
}

logger(paste("loading data from", file))

events <- read.csv(file, header=T, sep=",")
events$timestamp <- as.POSIXct(events$timestamp/1000, origin="1970-01-01",
tz="UTC")
Expand Down Expand Up @@ -64,7 +62,6 @@ loadEventData <- function(file) {
### fix timestamps: most of the time the event stream is out of order.
### the events have been ordered by id, volume, then action. now re-assign
### the timestamps to match this order for each order id.
logger("realigning event timestamps...")
ts.ordered <- unlist(tapply(events$timestamp, events$id, sort), use.names=F)
events$timestamp <- as.POSIXct(ts.ordered, origin="1970-01-01", tz="UTC")

Expand Down
2 changes: 0 additions & 2 deletions R/matching-engine.R
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@
##' @keywords internal
eventMatch <- function(events, cut.off.ms=5000) {
matcher <- function() {
logger(paste("matching", nrow(events), "events..."))

res <- integer()
cols <- c("event.id", "fill", "timestamp")

Expand Down
1 change: 0 additions & 1 deletion R/ob-analytics.R
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,6 @@
##' @importFrom grDevices colorRampPalette
##' @importFrom stats aggregate quantile time
##' @importFrom utils head read.csv setTxtProgressBar tail timestamp
##' txtProgressBar
##' @author Philip Stubbings \email{phil@@parasec.net}
##' @references \url{http://parasec.net/transmission/order-book-visualisation}
NULL
Expand Down
9 changes: 1 addition & 8 deletions R/order-types.R
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,12 @@ setOrderTypes <- function(events, trades) {
})
}

logger("identifying order types...")

events$type <- "unknown"
events$type <- factor(events$type, c("unknown", "flashed-limit",
"resting-limit", "market-limit", "pacman", "market"))

# pacman orders (this needs to be determined first)
pac.men <- which(isPacman(events))
logger(paste("found", length(pac.men), "pacman orders"))
if(length(pac.men) > 0)
events[which(events$id %in% names(pac.men)), ]$type <- "pacman"

Expand All @@ -67,8 +64,6 @@ setOrderTypes <- function(events, trades) {
pacman.ids <- unique(events[events$type=="pacman", ]$id)
maker.ids <- maker.ids[!maker.ids %in% taker.ids]
maker.ids <- maker.ids[!maker.ids %in% pacman.ids]
logger(paste("found", length(flashed.ids), "flashed-limit and",
length(forever.ids)+length(maker.ids), "resting orders"))
events[events$id %in% flashed.ids, ]$type <- "flashed-limit"
events[events$id %in% forever.ids |
events$id %in% maker.ids, ]$type <- "resting-limit"
Expand All @@ -79,17 +74,15 @@ setOrderTypes <- function(events, trades) {
ml.ids <- taker.ids[taker.ids %in% unique(events[events$event.id %in%
trades$maker.event.id, ]$id)]
ml.ids <- ml.ids[!ml.ids %in% pacman.ids]
logger(paste("found", length(ml.ids), "market-limit orders"))
events[events$id %in% ml.ids, ]$type <- "market-limit"

# market orders: at least 1 taking event, no identified making events.
mo.ids <- taker.ids[!taker.ids %in% unique(events[events$event.id %in%
trades$maker.event.id, ]$id)]
mo.ids <- mo.ids[!mo.ids %in% pacman.ids]
logger(paste("found", length(mo.ids), "market orders"))
events[events$id %in% mo.ids, ]$type <- "market"

logger(paste("could not identify", length(which(events$type=="unknown")),
warning(paste("could not identify", length(which(events$type=="unknown")),
"orders"))

events
Expand Down
2 changes: 0 additions & 2 deletions R/trades.R
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,6 @@
##' @keywords internal
matchTrades <- function(events) {

logger(paste("inferring trades from", nrow(events), "events..."))

# trades with matching maker/taker.
# align them by event id.
matching.bids <- events[events$direction == "bid" &
Expand Down
14 changes: 0 additions & 14 deletions R/visualisation.R
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ plotTimeSeries <- function(timestamp, series, start.time=min(timestamp),
end.time=max(timestamp), title="time series", y.label="series") {

stopifnot(length(timestamp) == length(series))
logger(paste("plotTimeSeries between", start.time, "and", end.time))

df <- data.frame(ts=timestamp, val=series)
df <- df[df$ts >= start.time & df$ts <= end.time, ]
Expand Down Expand Up @@ -221,7 +220,6 @@ plotPriceLevels <- function(depth, spread=NULL, trades=NULL,
unchanged.prices <- unchanged.prices[unchanged]
depth.filtered <- depth.filtered[!depth.filtered$price
%in% unchanged.prices, ]
logger(paste("removed", length(unchanged.prices), "unchanged depths"))
}

depth.filtered[depth.filtered$volume==0, ]$volume <- NA
Expand Down Expand Up @@ -485,14 +483,12 @@ plotVolumeMap <- function(events,
events <- events[events$volume >= volume.from | events$volume == 0, ]
else {
lim <- quantile(events$volume, 0.0001)
logger(paste("lower volume limit =", lim))
events <- events[events$volume >= lim, ]
}
if(!is.null(volume.to))
events <- events[events$volume <= volume.to, ]
else {
lim <- quantile(events$volume, 0.9999)
logger(paste("uppper volume limit =", lim))
events <- events[events$volume <= lim, ]
}

Expand Down Expand Up @@ -568,9 +564,6 @@ plotCurrentDepth <- function(order.book,
bid.quantiles <- with(bids, price[volume >= quantile(volume, 0.99)])
ask.quantiles <- with(asks, price[volume >= quantile(volume, 0.99)])

logger(paste("bid quantiles =", paste(bid.quantiles, collapse=", "),
"ask quantiles =", paste(ask.quantiles, collapse=", ")))

p <- p + geom_vline(xintercept=bid.quantiles, colour="#222222")
p <- p + geom_vline(xintercept=ask.quantiles, colour="#222222")
}
Expand Down Expand Up @@ -630,21 +623,17 @@ plotVolumePercentiles <- function(depth.summary,
# see: http://stackoverflow.com/questions/9439256
liquidity <- NULL; percentile <- NULL

logger(paste("plot depth percentiles between", start.time, "and", end.time))

bid.names <- paste0("bid.vol", seq(from=25, to=500, by=25), "bps")
ask.names <- paste0("ask.vol", seq(from=25, to=500, by=25), "bps")

td <- difftime(end.time, start.time, units="secs")
logger(paste("time range =", td, "secs"))
td <- round(as.numeric(td))

# resolution: if(td > 15 minutes, minute ticks, else seconds.
frequency <- ifelse(td > 900, "mins", "secs")
ob.percentiles <- depth.summary[depth.summary$timestamp
>= start.time-ifelse(frequency == "mins", 60, 1) & depth.summary$timestamp
<= end.time, c("timestamp", bid.names, ask.names)]
logger(paste("aggregating to", frequency, "intervals"))

# remove duplicates (take last entry) (for zoo to work)
ob.percentiles <- ob.percentiles[!duplicated(ob.percentiles$timestamp,
Expand All @@ -655,8 +644,6 @@ plotVolumePercentiles <- function(depth.summary,

# intervals truncated to frequency
intervals <- as.POSIXct(trunc(time(zoo.obj), frequency))
logger(paste("aggregation:", min(intervals), ":", max(intervals), "by =",
frequency))

# use zoo to aggregate by intervals. take mean of each interval.
aggregated <- aggregate(zoo.obj, intervals, mean)
Expand Down Expand Up @@ -764,7 +751,6 @@ plotEventsHistogram <- function(events,
bw=NULL) {

stopifnot(val == "volume" || val == "price")
logger(paste("from =", start.time, "to =", end.time))

events <- events[events$timestamp >= start.time
& events$timestamp <= end.time, ]
Expand Down

0 comments on commit 758685c

Please sign in to comment.