TraderCG wrote: ↑Sat Nov 03, 2018 6:52 am
My question: on line-14 of OrderBookBase class: should sizePrevious = book.put(price,size) ?
It seems to me that to get the
previous TreeMap value, line-14 should be: Integer sizePrevious = book.get(price);
Hello,
It works correctly, I just tried to make the code shorter and more elegant. A straight forward implementation as you expected would look like this:

- refactoring.png (100.34 KiB) Viewed 34021 times
Notice that both Map#put() and Map#remove() return the previous value associated with the key (in our case price is the key and size is the value).

- put.png (72.24 KiB) Viewed 34019 times
Also, since they both return a value, it allows usage of
ternary operation Code: Select all
value = <condition> ? <this if true> : <this if false>
, i.e.
Code: Select all
Integer sizePrevious = (size == 0) ? book.remove(price) : book.put(price, size);
and from the indicator:
Code: Select all
int prevSize = book.onDepth(isBid, price, size);
So, in one line we update the order book, and also get the previous value, which in some cases like this can be useful.
Remark: Here prevSize can be 'int' instead of 'Integer' because it was already checked for null.
Code: Select all
Integer prevSize = book.onDepth(isBid, price, size);
I already updated this.
Best,
S