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!
dput(head(your_data), and also do you really have an xts object? I mean do you need a time series here , look that you just need the last day, Maybe a simple data.frame(bid,size) is sufficient. - agstudy