patterncppMinor
Boost multi-index based orderbook
Viewed 0 times
multiorderbookbasedindexboost
Problem
As suggested here, I'm using Boost multi-index to implement orderbook. So far my code looks like this:
I have the following questions:
-
For here:
If I already have an item with
-
Once I have an item, how can I modify
"CommonsNative.h" contains typedef int64_t myDecimal; and enum Side { Buy, Sell };#pragma once
#include "CommonsNative.h"
#include
#include
#include
#include
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
,
ordered_unique /* sorted beginning with most frequent */
>
>> OrderBookContainer;
class OrderBook
{
public:
OrderBook(void);
~OrderBook(void);
void Add(Side side_, myDecimal price_, int lots_)
{
if (side_ == Buy) {
// inserts or returns existent
OrderBookContainer::iterator it = buyContainer.insert(price_).first;
// how to add lots i.e. OrderBookItem.lots += lots_?
// auto curLevel = buyContainer.get(price);
// curLevel.lots += Lots;
} else if (side_ == Sell) {
// inserts or returns existent
OrderBookContainer::iterator it = sellContainer.insert(price_).first;
}
}
private:
OrderBookContainer buyContainer;
OrderBookContainer sellContainer;
};I have the following questions:
-
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)\$)?-
Once I have an item, how can I modify
lots to something like this?OrderBookItem.lots += lots_Solution
-
-
Empty
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:
Not only that, but you already add this whitespace in other places. Keep it consistent.
-
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?
If so, then you can replace
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(price);If so, then you can replace
OrderBookContainer::iterator with auto:auto it = buyContainer.insert(price_).first;auto it = sellContainer.insert(price_).first;Code Snippets
OrderBook(void);
~OrderBook(void);BOOST_MULTI_INDEX_MEMBER(OrderBookItem,myDecimal,price)BOOST_MULTI_INDEX_MEMBER(OrderBookItem, myDecimal, price)// auto curLevel = buyContainer.get<name>(price);auto it = buyContainer.insert(price_).first;Context
StackExchange Code Review Q#39870, answer score: 3
Revisions (0)
No revisions yet.