Files

684 lines
15 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 <string>
#include <OnixS/CME/MDH/Decimal.h>
#include <OnixS/CME/MDH/DirectPriceLevel.h>
#include <OnixS/CME/MDH/MboBookTraits.h>
#include <OnixS/CME/MDH/MbpBookTraits.h>
ONIXS_CMEMDH_NAMESPACE_BEGIN
/// Raises exception on invalid best bid/offer threshold value.
inline
void
throwBadBboThreshold()
{
throw std::runtime_error
(
"Threshold for tracking best bids "
"or offers can't be a negative value. "
);
}
/// Defines tracking attributes for a particular BBO parameter.
ONIXS_CMEMDH_LTWT_CLASS BboThreshold
{
// Actual threshold value.
Decimal threshold_;
// Indicates whether threshold must be used.
bool enabled_;
public:
/// Initializes parameter tracking.
BboThreshold()
: threshold_()
, enabled_(false)
{
}
/// Indicates whether tracking is enabled.
bool enabled() const
{
return enabled_;
}
/// Indicates whether tracking is enabled.
void enabled(bool value)
{
enabled_ = value;
}
/// Defines value threshold for parameter being tracked.
const Decimal& threshold() const
{
return threshold_;
}
/// Defines value threshold for parameter being tracked.
void
threshold(
const Decimal& threshold)
{
if (0 > threshold.mantissa())
throwBadBboThreshold();
threshold_ = threshold;
}
};
/// Checks whether new value exceeds given
/// threshold in compare to the old value.
ONIXS_CMEMDH_EXPORTED
bool
thresholdExceeded(
const Decimal&,
const Decimal&,
const Decimal&);
/// Checks whether new value exceeds given
/// threshold in compare to the old value.
ONIXS_CMEMDH_EXPORTED
bool
thresholdExceeded(
const Decimal&,
Int32,
Int32);
/// Defines tracking for BBO in MBO books.
ONIXS_CMEMDH_LTWT_CLASS MboBboTracking
{
BboThreshold price_;
BboThreshold quantity_;
public:
/// Initializes tracking.
MboBboTracking()
{
}
/// Price tracking attributes.
const
BboThreshold&
price() const
{
return price_;
}
/// Price tracking attributes.
BboThreshold& price()
{
return price_;
}
/// Indicates whether tracking enabled at all.
bool enabled() const
{
return (
price_.enabled()
);
}
/// Checks whether any of thresholds is exceeded.
bool
exceeded(
const Order& previous,
const Order& current) const
{
return (
price_.enabled()
&&
thresholdExceeded(
price_.threshold(),
previous.price(),
current.price())
);
}
};
/// Serializes BBO tracking parameters into string.
ONIXS_CMEMDH_EXPORTED
void
toStr(
std::string&,
const MboBboTracking&);
/// Serializes BBO tracking parameters into string.
inline
std::string
toStr(
const MboBboTracking& tracking)
{
std::string str;
toStr(str, tracking);
return str;
}
/// Defines tracking for BBO in implied books.
ONIXS_CMEMDH_LTWT_CLASS ImpliedBboTracking
{
BboThreshold price_;
BboThreshold quantity_;
public:
/// Initializes tracking.
ImpliedBboTracking()
{
}
/// Price tracking attributes.
const
BboThreshold&
price() const
{
return price_;
}
/// Price tracking attributes.
BboThreshold& price()
{
return price_;
}
/// Quantity tracking attributes.
const
BboThreshold&
quantity() const
{
return quantity_;
}
/// Quantity tracking attributes.
BboThreshold& quantity()
{
return quantity_;
}
/// Indicates whether tracking enabled at all.
bool enabled() const
{
return (
price_.enabled()
||
quantity_.enabled()
);
}
/// Checks whether any of thresholds is exceeded.
bool
exceeded(
const ImpliedPriceLevel& previous,
const ImpliedPriceLevel& current) const
{
return (
(price_.enabled()
&&
thresholdExceeded(
price_.threshold(),
previous.price(),
current.price()))
||
(quantity_.enabled()
&&
thresholdExceeded(
quantity_.threshold(),
previous.quantity(),
current.quantity()))
);
}
};
/// Serializes BBO tracking parameters into string.
ONIXS_CMEMDH_EXPORTED
void
toStr(
std::string&,
const ImpliedBboTracking&);
/// Serializes BBO tracking parameters into string.
inline
std::string
toStr(
const ImpliedBboTracking& tracking)
{
std::string str;
toStr(str, tracking);
return str;
}
/// Defines tracking for BBO in direct books.
ONIXS_CMEMDH_LTWT_CLASS
DirectBboTracking
: public ImpliedBboTracking
{
BboThreshold numberOfOrders_;
public:
/// Initializes tracking.
DirectBboTracking()
{
}
/// Tracking for order quantity.
const
BboThreshold&
numberOfOrders() const
{
return numberOfOrders_;
}
/// Tracking for order quantity.
BboThreshold& numberOfOrders()
{
return numberOfOrders_;
}
/// Indicates whether tracking enabled at all.
bool enabled() const
{
return (
ImpliedBboTracking::enabled()
||
numberOfOrders_.enabled()
);
}
/// Checks whether any of thresholds is exceeded.
bool
exceeded(
const DirectPriceLevel& previous,
const DirectPriceLevel& current) const
{
return (
ImpliedBboTracking::exceeded(previous, current)
||
(numberOfOrders_.enabled()
&&
thresholdExceeded(
numberOfOrders_.threshold(),
previous.numberOfOrders(),
current.numberOfOrders())
)
);
}
};
/// Serializes BBO tracking parameters into string.
ONIXS_CMEMDH_EXPORTED
void
toStr(
std::string&,
const DirectBboTracking&);
/// Serializes BBO tracking parameters into string.
inline
std::string
toStr(
const DirectBboTracking& tracking)
{
std::string str;
toStr(str, tracking);
return str;
}
/// Parameters affecting book management machinery for
/// a particular book type (direct, implied, consolidated).
template
<
class BboTracking
>
class BaseBookManagement
{
// Indicates whether feature is enabled.
bool maintain_;
// Parameters for tracking.
BboTracking bboTracking_;
public:
/// Initializes instance with default values.
BaseBookManagement()
: maintain_(false)
, bboTracking_()
{
}
/// Forces to build order book of a particular kind.
///
/// Unless parameter is set to 'true', Handler doesn't
/// construct and doesn't process data blocks for the
/// books of given kind. If book isn't built, book snapshot
/// retrieval is not possible.
///
/// @note Default value is 'false'.
bool maintain() const
{
return maintain_;
}
/// Forces Handler to maintain direct order books.
void maintain(bool value)
{
maintain_ = value;
}
/// Parameters affecting BBO tracking for
/// books which given settings refer to.
const BboTracking& bboTracking() const
{
return bboTracking_;
}
/// Parameters affecting BBO tracking for
/// books which given settings refer to.
BboTracking& bboTracking()
{
return bboTracking_;
}
};
/// Parameters affecting book management machinery for
/// a particular book type (direct, implied, consolidated).
template
<
class BboTracking,
typename Depth
>
class
FixedDepthBookManagement
: public BaseBookManagement
<
BboTracking
>
{
Depth defaultDepth_;
public:
/// Initializes instance with default values.
FixedDepthBookManagement(
Depth defaultDepth = 0)
: BaseBookManagement<BboTracking>()
, defaultDepth_(defaultDepth)
{
}
/// Defines default depth of order book for
/// the security whose definition wasn't
/// received or had no corresponding data.
Depth defaultDepth() const
{
return defaultDepth_;
}
/// Defines default depth of order book for
/// the security whose definition wasn't
/// received or had no corresponding data.
void defaultDepth(Depth value)
{
defaultDepth_ = value;
}
};
/// Parameters affecting book management machinery
/// for a books having variable depth like MBO ones.
template
<
class BboTracking,
typename Depth
>
class
AnyDepthBookManagement
: public BaseBookManagement
<
BboTracking
>
{
Depth reserved_;
public:
/// Initializes instance with default values.
AnyDepthBookManagement()
: BaseBookManagement<BboTracking>()
{
}
};
/// Defines book update notification strategies.
ONIXS_CMEMDH_LTWT_STRUCT BookUpdateNotification
{
/// Integral type used as basement for constants.
typedef UInt32 Base;
enum Enum
{
/// Book update event is raised once security id is changed in
/// sequence of market data entries being processed by the Handler.
///
/// @warning Given strategy does not follow MDP policy
/// on exposing order books and thus can be used on own risk!
/// Also, books may not be in a valid state (especially consolidated),
/// as far as exposed earlier than data for entire market event
/// is processed.
OnSecurityChange,
/// Book update event is raised at the end of market event.
///
/// Direct books updates are raised at the end of real
/// quotes updates, implied books updates are raised at
/// the end of implied quotes updates and consolidated
/// books updates are raised at the end of entire event.
OnEndOfEvent
};
};
/// Serializes book update notification policy into a string.
ONIXS_CMEMDH_EXPORTED
void
toStr(
std::string&,
BookUpdateNotification::Enum);
/// Serializes book update notification policy into a string.
inline
std::string
toStr(
BookUpdateNotification::Enum strategy)
{
std::string str;
toStr(str, strategy);
return str;
}
/// Parameters affecting book management machinery.
ONIXS_CMEMDH_LTWT_CLASS BookManagement
{
public:
/// Management and tracking parameters for MBO books.
typedef
AnyDepthBookManagement
<
MboBboTracking,
SortedOrders::size_type
>
MboBookManagement;
/// Management and tracking parameters for direct books.
typedef
FixedDepthBookManagement
<
DirectBboTracking,
MbpBookDepth
>
DirectBookManagement;
/// Management and tracking parameters for implied books.
typedef
FixedDepthBookManagement
<
ImpliedBboTracking,
MbpBookDepth
>
ImpliedBookManagement;
/// Management and tracking parameters for consolidated books.
typedef
BaseBookManagement
<
ImpliedBboTracking
>
ConsolidatedBookManagement;
/// Initializes instance with default values.
BookManagement()
: mboBooks_()
, directBooks_(10)
, impliedBooks_(2)
, consolidatedBooks_()
, updateNotification_(
BookUpdateNotification::
OnEndOfEvent)
{
}
/// Management and tracking parameters for direct books.
const
MboBookManagement&
mboBooks() const
{
return mboBooks_;
}
/// Management and tracking parameters for direct books.
MboBookManagement&
mboBooks()
{
return mboBooks_;
}
/// Management and tracking parameters for direct books.
const
DirectBookManagement&
directBooks() const
{
return directBooks_;
}
/// Management and tracking parameters for direct books.
DirectBookManagement&
directBooks()
{
return directBooks_;
}
/// Management and tracking parameters for implied books.
const
ImpliedBookManagement&
impliedBooks() const
{
return impliedBooks_;
}
/// Management and tracking parameters for implied books.
ImpliedBookManagement&
impliedBooks()
{
return impliedBooks_;
}
/// Management and tracking parameters for consolidated books.
const
ConsolidatedBookManagement&
consolidatedBooks() const
{
return consolidatedBooks_;
}
/// Management and tracking parameters for consolidated books.
ConsolidatedBookManagement&
consolidatedBooks()
{
return consolidatedBooks_;
}
/// Defines the way Handler raises onBookUpdated callbacks.
///
/// @note Default is BookUpdateNotification::OnEndOfEvent.
BookUpdateNotification::Enum
updateNotification() const
{
return updateNotification_;
}
/// Defines the way Handler raises order book update event.
void
updateNotification(
BookUpdateNotification::Enum value)
{
updateNotification_ = value;
}
private:
MboBookManagement mboBooks_;
DirectBookManagement directBooks_;
ImpliedBookManagement impliedBooks_;
ConsolidatedBookManagement consolidatedBooks_;
BookUpdateNotification::Enum updateNotification_;
};
/// Serializes book management settings into string.
ONIXS_CMEMDH_EXPORTED
void
toStr(
std::string&,
const BookManagement&);
/// Serializes book management settings into string.
inline
std::string
toStr(
const BookManagement& settings)
{
std::string str;
toStr(str, settings);
return str;
}
ONIXS_CMEMDH_NAMESPACE_END