7
\$\begingroup\$

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_) 
    {
        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<name>(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:

  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_
    
\$\endgroup\$
3
  • \$\begingroup\$ javapowered ..... time to change your nick? ;-) \$\endgroup\$
    – rolfl
    Jan 23, 2014 at 23:07
  • \$\begingroup\$ not using java for 3+ years so probably can change to c#++powered :) \$\endgroup\$ Jan 24, 2014 at 7:28
  • \$\begingroup\$ 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. \$\endgroup\$
    – Jamal
    Jul 5, 2014 at 20:45

1 Answer 1

3
\$\begingroup\$
  • 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;
    
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.