425 lines
8.4 KiB
C++
425 lines
8.4 KiB
C++
// Copyright Onix Solutions Limited [OnixS]. All rights reserved.
|
|
//
|
|
// This software owned by Onix Solutions Limited [OnixS] and is
|
|
// protected by copyright law and international copyright treaties.
|
|
//
|
|
// Access to and use of the software is governed by the terms of the applicable
|
|
// OnixS Software Services Agreement (the Agreement) and Customer end user license
|
|
// agreements granting a non-assignable, non-transferable and non-exclusive license
|
|
// to use the software for it's own data processing purposes under the terms defined
|
|
// in the Agreement.
|
|
//
|
|
// Except as otherwise granted within the terms of the Agreement, copying or
|
|
// reproduction of any part of this source code or associated reference material
|
|
// to any other location for further reproduction or redistribution, and any
|
|
// amendments to this copyright notice, are expressly prohibited.
|
|
//
|
|
// Any reproduction or redistribution for sale or hiring of the Software not in
|
|
// accordance with the terms of the Agreement is a violation of copyright law.
|
|
//
|
|
|
|
#pragma once
|
|
|
|
#include <cassert>
|
|
#include <memory>
|
|
#include <string>
|
|
#include <stdexcept>
|
|
|
|
#include <OnixS/CME/MDH/Memory.h>
|
|
#include <OnixS/CME/MDH/VectorOverArray.h>
|
|
#include <OnixS/CME/MDH/BookAttributes.h>
|
|
|
|
ONIXS_CMEMDH_NAMESPACE_BEGIN
|
|
|
|
/// Raises exception on book re-depth failure.
|
|
template
|
|
<
|
|
typename Depth
|
|
>
|
|
inline
|
|
void
|
|
throwMbpBookRedepthFailure(
|
|
Depth newDepth, Depth maxDepth)
|
|
{
|
|
std::string error;
|
|
|
|
error +=
|
|
"Cannot re-depth order book "
|
|
"due to lack of capacity [newDepth=";
|
|
|
|
toStr(error, newDepth);
|
|
|
|
error += ",maxDepth=";
|
|
|
|
toStr(error, maxDepth);
|
|
|
|
error += "]. ";
|
|
|
|
throw std::runtime_error(error);
|
|
}
|
|
|
|
/// Base attributes of order book.
|
|
///
|
|
/// A set of bids and offers data for the given security
|
|
/// (bids are descending and offers are ascending).
|
|
template
|
|
<
|
|
class EntryType,
|
|
class DepthType
|
|
>
|
|
class
|
|
MbpBook
|
|
{
|
|
public:
|
|
/// Indicates price level data type.
|
|
typedef
|
|
EntryType
|
|
Entry;
|
|
|
|
/// Aliases book dimension type.
|
|
typedef
|
|
DepthType
|
|
Depth;
|
|
|
|
/// Collection of price levels allowing modifications.
|
|
typedef
|
|
VectorOverArray
|
|
<
|
|
Entry,
|
|
Depth
|
|
>
|
|
Entries;
|
|
|
|
/// Iterator over bids and offers.
|
|
typedef
|
|
typename
|
|
Entries::ConstIterator
|
|
EntryIterator;
|
|
|
|
/// Initializes instance with given attributes.
|
|
///
|
|
/// Bids and offers are stored in given segments of
|
|
/// plain arrays. Book constructs vectors over input
|
|
/// arrays of fixed size. That provides more flexibility
|
|
/// on memory layout for internally used data.
|
|
MbpBook(
|
|
Depth capacity,
|
|
Entry* bids,
|
|
Entry* offers)
|
|
: depth_(0)
|
|
, state_(
|
|
BookState::Constructed)
|
|
, bids_(
|
|
bids,
|
|
capacity)
|
|
, offers_(
|
|
offers,
|
|
capacity)
|
|
{
|
|
}
|
|
|
|
// Cleans everything up.
|
|
~MbpBook()
|
|
{
|
|
}
|
|
|
|
/// Returns the maximal number of price levels.
|
|
Depth depth() const
|
|
{
|
|
return depth_;
|
|
}
|
|
|
|
/// Indicates book capacity which is
|
|
/// maximal value of book depth allowed.
|
|
Depth capacity() const
|
|
{
|
|
return bids_.capacity();
|
|
}
|
|
|
|
/// Updates the maximal number of price levels.
|
|
void
|
|
depth(
|
|
Depth depth)
|
|
{
|
|
if (depth > capacity())
|
|
{
|
|
throwMbpBookRedepthFailure(
|
|
depth,
|
|
capacity());
|
|
}
|
|
|
|
if (bids_.size() > depth)
|
|
bids_.resize(depth);
|
|
|
|
if (offers_.size() > depth)
|
|
offers_.resize(depth);
|
|
|
|
depth_ = depth;
|
|
}
|
|
|
|
/// List of descending bid prices.
|
|
Entries& bids()
|
|
{
|
|
return bids_;
|
|
}
|
|
|
|
/// List of descending bid prices.
|
|
Entries&
|
|
operator [](
|
|
const Bids&)
|
|
{
|
|
return bids_;
|
|
}
|
|
|
|
/// List of descending bid prices.
|
|
const
|
|
Entries&
|
|
bids() const
|
|
{
|
|
return bids_;
|
|
}
|
|
|
|
/// List of descending bid prices.
|
|
const
|
|
Entries&
|
|
operator [](
|
|
const Bids&) const
|
|
{
|
|
return bids_;
|
|
}
|
|
|
|
/// List of ascending offer prices.
|
|
Entries& offers()
|
|
{
|
|
return offers_;
|
|
}
|
|
|
|
/// List of ascending offer prices.
|
|
Entries&
|
|
operator [](
|
|
const Offers&)
|
|
{
|
|
return offers_;
|
|
}
|
|
|
|
/// List of ascending offer prices.
|
|
const
|
|
Entries&
|
|
offers() const
|
|
{
|
|
return offers_;
|
|
}
|
|
|
|
/// List of ascending offer prices.
|
|
const
|
|
Entries&
|
|
operator [](
|
|
const Offers&) const
|
|
{
|
|
return offers_;
|
|
}
|
|
|
|
/// Indicates current state of book.
|
|
BookState::Enum state() const
|
|
{
|
|
return state_;
|
|
}
|
|
|
|
/// Updates state of book.
|
|
void
|
|
state(
|
|
BookState::Enum state)
|
|
{
|
|
state_ = state;
|
|
}
|
|
|
|
/// Wipes out all bids and offers and other
|
|
/// data associated with book (like instance
|
|
/// of last applied FIX message).
|
|
void
|
|
reset(
|
|
BookState::Enum bookState)
|
|
{
|
|
state(bookState);
|
|
|
|
bids_.resize(0);
|
|
offers_.resize(0);
|
|
}
|
|
|
|
private:
|
|
// Book depth.
|
|
Depth depth_;
|
|
|
|
// Book state.
|
|
BookState::Enum state_;
|
|
|
|
// Bids.
|
|
Entries bids_;
|
|
|
|
// Asks.
|
|
Entries offers_;
|
|
|
|
// Copying is done the other way.
|
|
MbpBook(const MbpBook&);
|
|
|
|
// Copying is done the other way.
|
|
MbpBook& operator=(const MbpBook&);
|
|
};
|
|
|
|
/// Copies content of one book to another.
|
|
///
|
|
/// Source and destination books are not required
|
|
/// to be of same type, just compatible by price levels.
|
|
/// For example, content of direct book can be copied to implied book,
|
|
/// or content of consolidated book can be copied to implied book,
|
|
/// however, implied book can't be copied to direct book as far as
|
|
/// direct price level has more attributes rather than implied one.
|
|
template
|
|
<
|
|
class TargetPriceLevel,
|
|
class SourcePriceLevel,
|
|
class Depth
|
|
>
|
|
void
|
|
copy(
|
|
MbpBook<TargetPriceLevel, Depth>& target,
|
|
const MbpBook<SourcePriceLevel, Depth>& source,
|
|
Depth maxDepth = static_cast<Depth>(-1))
|
|
{
|
|
if (maxDepth > source.depth())
|
|
maxDepth = source.depth();
|
|
|
|
target.depth(maxDepth);
|
|
|
|
copy(
|
|
target.bids(),
|
|
source.bids(),
|
|
maxDepth);
|
|
|
|
copy(
|
|
target.offers(),
|
|
source.offers(),
|
|
maxDepth);
|
|
|
|
target.state(source.state());
|
|
}
|
|
|
|
/// Serves as a factory for book of given type.
|
|
template
|
|
<
|
|
class PriceLevelType,
|
|
class DepthType
|
|
>
|
|
class
|
|
MbpBookFactory
|
|
{
|
|
public:
|
|
/// Type of book constructed by given factory.
|
|
typedef
|
|
MbpBook
|
|
<
|
|
PriceLevelType,
|
|
DepthType
|
|
>
|
|
Book;
|
|
|
|
/// Book entry type.
|
|
typedef
|
|
typename Book::Entry
|
|
Entry;
|
|
|
|
/// Book depth type.
|
|
typedef
|
|
typename Book::Depth
|
|
Depth;
|
|
|
|
/// Calculates size of memory required
|
|
/// to keep in memory book of given depth.
|
|
static
|
|
size_t
|
|
calcSize(
|
|
Depth maxDepth)
|
|
{
|
|
return (
|
|
sizeof(Book) +
|
|
2 *
|
|
maxDepth *
|
|
sizeof(Entry)
|
|
);
|
|
}
|
|
|
|
/// Constructs book instance of given capacity.
|
|
///
|
|
/// Book depth can be changed in bounds of specified capacity.
|
|
static
|
|
Book*
|
|
construct(
|
|
Depth maxDepth)
|
|
{
|
|
const
|
|
size_t
|
|
bookSize =
|
|
calcSize(maxDepth);
|
|
|
|
return
|
|
inplaceConstruct(
|
|
maxDepth,
|
|
new Byte [bookSize]);
|
|
}
|
|
|
|
/// Destructs previously constructed book.
|
|
static
|
|
void
|
|
destruct(
|
|
Book* book)
|
|
{
|
|
if (book)
|
|
{
|
|
book->~Book();
|
|
|
|
delete [] reinterpret_cast<Byte*>(book);
|
|
}
|
|
}
|
|
|
|
/// Constructs book on a given memory block.
|
|
static
|
|
Book*
|
|
inplaceConstruct(
|
|
Depth maxDepth,
|
|
void* block)
|
|
{
|
|
Entry*
|
|
const
|
|
entries =
|
|
reinterpret_cast<Entry*>
|
|
(
|
|
reinterpret_cast<Byte*>(block) +
|
|
sizeof(Book)
|
|
);
|
|
|
|
inplaceConstruct(
|
|
entries,
|
|
entries + 2 * maxDepth);
|
|
|
|
return
|
|
new (block)
|
|
Book(
|
|
maxDepth,
|
|
entries,
|
|
entries + maxDepth);
|
|
}
|
|
|
|
private:
|
|
static
|
|
void
|
|
inplaceConstruct(
|
|
Entry* first,
|
|
Entry* last)
|
|
{
|
|
while (first != last)
|
|
new (first++) Entry();
|
|
}
|
|
};
|
|
|
|
ONIXS_CMEMDH_NAMESPACE_END
|