# Boost multi-index based orderbook

As suggested here, I'm using Boost multi-index to implement orderbook. So far my code looks like this:

"CommonsNative.h" contains typedef int64_t myDecimal; and enum Side { Buy, Sell };

#pragma once

#include "CommonsNative.h"
#include <boost/multi_index_container.hpp>
#include <boost/multi_index/member.hpp>
#include <boost/multi_index/ordered_index.hpp>
#include <boost/multi_index/hashed_index.hpp>

using boost::multi_index_container;
using namespace boost::multi_index;

struct OrderBookItem {
myDecimal price;
int32_t lots;
OrderBookItem(myDecimal price_) :price(price_), lots(0) {

}
};

typedef multi_index_container
<OrderBookItem, indexed_by<

hashed_unique<
BOOST_MULTI_INDEX_MEMBER(OrderBookItem,myDecimal,price)
>,

ordered_unique<
BOOST_MULTI_INDEX_MEMBER(OrderBookItem,myDecimal,price),
std::greater<myDecimal> /* sorted beginning with most frequent */
>

>> OrderBookContainer;

class OrderBook
{
public:
OrderBook(void);
~OrderBook(void);
void Add(Side side_, myDecimal price_, int lots_)
{
// inserts or returns existent
// how to add lots i.e. OrderBookItem.lots += lots_?
// curLevel.lots += Lots;
} else if (side_ == Sell) {
// inserts or returns existent
OrderBookContainer::iterator it = sellContainer.insert(price_).first;
}
}
private:
OrderBookContainer sellContainer;
};


I have the following questions:

1. For here:

OrderBookContainer::iterator it = buyContainer.insert(price_).first;


If I already have an item with price = price_ I receive it in $O(1)$ (from hashtable). If such a level doesn't exist, then is it added in $O(log n)$? If I'm wrong, then what should I write to get an item if it exists (in $O(1)$) otherwise add it (in $O(log n)$)?

2. Once I have an item, how can I modify lots to something like this?

OrderBookItem.lots += lots_

• javapowered ..... time to change your nick? ;-) – rolfl Jan 23 '14 at 23:07
• not using java for 3+ years so probably can change to c#++powered :) – javapowered Jan 24 '14 at 7:28
• The second question is off-topic, as well as the second sub-question of the first question. We cannot assist with adding new implementation, but we can review everything else. I've already given some advice of my own below. – Jamal Jul 5 '14 at 20:45

• myDecimal is an absolutely unhelpful name overall. I don't see any need to give int64_t a typedef here, but even if there is one, then it should still use a better name.

• Empty void parameters are not needed in C++:

OrderBook(void);
~OrderBook(void);


You also don't need these defaults as the compiler will provide them for you. They would only be necessary if you need to overload them and will still need default versions.

• It's a little less-readable not having whitespace after commas:

BOOST_MULTI_INDEX_MEMBER(OrderBookItem,myDecimal,price)


Not only that, but you already add this whitespace in other places. Keep it consistent.

BOOST_MULTI_INDEX_MEMBER(OrderBookItem, myDecimal, price)

• You use different styles of curly braces. C++ doesn't restrict you to a particular one, so choose one you prefer and stick with it.

• Although this line has been commented out, are you still using C++11?

// auto curLevel = buyContainer.get<name>(price);


If so, then you can replace OrderBookContainer::iterator with auto:

auto it = buyContainer.insert(price_).first;


auto it = sellContainer.insert(price_).first;