Question

I'm modelling the development of the orderbook over time. I have an initial orderbook shape in xts and then the subsequent depth updates also in xts:

The initial orderbook shape looks as follows (all entries have the same time):

                            BID.price       size
2014-02-11 23:59:42.494426  508.1000        10.0000000
2014-02-11 23:59:42.494426  509.1200         8.0000000
2014-02-11 23:59:42.494426  509.1000        10.0000000

and the subsequent depth udpates look as follows:

                           BID. price       size
2014-02-12 04:57:51.191514 508.1000        -10.00000000
2014-02-12 04:57:51.640302 514.0000         10.00000000

What I need to to is:

1) for each row in updates, compare the price with the orderbook:

1a) If the updates price level is in orderbook already, adjust the size accordingly, so the example above would look as follows:

                            BID.price       size
2014-02-12 04:57:51.191514  509.1200         8.0000000
2014-02-12 04:57:51.291514  509.1000        10.0000000

(the price level 508.10000 was deleted, and time was updated)

1b) If the depth updates is not in orderbook yet, add new prise level with given size, so the example would looks like:

                            BID.price       size
2014-02-12 04:57:51.640302  509.1200         8.0000000
2014-02-12 04:57:51.640302  509.1000         10.0000000
2014-02-12 04:57:51.640302  514.0000         10.00000000

(new price level of 514 was added and time was adjusted).

Is there any convenient and fast way how to do such thing avoiding the for loop over depth updates xts?

Thanks!

Was it helpful?

Solution

I think no need to use an xts object here since the index is the same for all observations and the right id here is the bid variable. So I explain my solution using 1 simple data.frame as shown above:

DT                  ## the day before
  day    bid size
1   1 508.10   10
2   1 509.12    8
3   1 509.10   10
DT1                ## the current or last day
  day   bid size
1   2 508.1  -10
2   2 514.0   10

Now using merge we have nearly the solution:

dtm
     bid day.x size.x day.y size.y
1 508.10     1     10     2    -10
2 509.10     1     10    NA     NA
3 509.12     1      8    NA     NA
4 514.00    NA     NA     2     10

Now we should just adjust size and remove bids with a null positions. I cerate here an intermediate %+% function to deal with missing values.

## compute size
"%+%" <- function(x,y) 
     ifelse(is.na(x),
            ifelse(is.na(y),NA,y),
            ifelse(is.na(y),x,NA))
## remove  numm poistion(size==0)
subset(transform(dtm,size=size.x%+%size.y,day=max(day.y,na.rm=T)),
       size !=0,select=c(day,bid,size))

 day    bid size
2   2 509.10   10
3   2 509.12    8
4   2 514.00   10

I think you can get more succicent and syntax sugar solution here using data.table.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top