Files
spark/src/lib/md/MBOProcessor.h
T
John Colvin df5aecd102 refactor: Replace refs to google/btree w/ Kronuz/btree
The former has been replaced with the latter in externals
2021-12-05 16:24:34 -05:00

172 lines
4.3 KiB
C++

#pragma once
#include "ExchangeOrderKey.h"
#include "IntPrice.h"
#include "MDProducerHandle.h"
#include "lib/domain_model/Security.h"
#include "lib/domain_model/SecurityKey.h"
#include "lib/utils/TimeStamp.h"
#include <btree/map.h>
#include <map>
#include <memory>
#include <tuple>
#include <vector>
namespace bts {
namespace md {
struct SnapshotOrder {
ExchangeOrderKey id;
IntPrice price;
int32_t size;
};
struct MBOSnapshot {
MBOSnapshot() = default;
MBOSnapshot(const MBOSnapshot &) = delete;
MBOSnapshot(MBOSnapshot &&other) = delete;
utils::TimeStamp updated;
std::vector<SnapshotOrder> bids;
std::vector<SnapshotOrder> asks;
void copy_from(const MBOSnapshot &src) {
bids = src.bids;
asks = src.asks;
updated = src.updated;
}
void reset() {
updated = utils::TimeStamp();
constexpr int max = 256;
if (bids.size() > max) {
bids.resize(max);
}
if (asks.size() > max) {
asks.resize(max);
}
bids.clear();
asks.clear();
}
};
namespace inc {
// according to this link MBO book only has these updates types
// https://www.cmegroup.com/confluence/display/EPICSANDBOX/MDP+3.0+-+Market+Data+Snapshot+Full+Refresh#MDP3.0-MarketDataSnapshotFullRefresh-MarketbyOrder(MBO)DataBlock
enum class UpdateType {
Bid = 0,
Ask = 1,
// ImpliedBid, // 'E'
// ImpliedAsk, // 'F'
// BookReset // 'J'
};
// OrderUpdateAction=37708
enum class UpdateAction { New = 0, Change = 1, Delete = 2 };
typedef uint64_t Priority;
struct Update {
ExchangeOrderKey order_id;
Priority priority;
IntPrice price;
int32_t size;
UpdateType type;
UpdateAction action;
};
} // namespace inc
struct MBOWorkingState {
typedef std::tuple<IntPrice, inc::Priority> key_t;
struct Data {
ExchangeOrderKey id;
int32_t size;
};
struct LessBid {
bool operator()(const key_t &left, const key_t &right) const {
const auto &left_price = std::get<IntPrice>(left);
const auto &right_price = std::get<IntPrice>(right);
if (left_price == right_price) {
return std::get<inc::Priority>(left) <
std::get<inc::Priority>(right);
}
return !(left_price < right_price);
}
};
MBOWorkingState(MDProducerHandle &h) : handle(h), updated(), bid(), ask() {}
MBOWorkingState(const MBOWorkingState &other) = default;
MDProducerHandle handle;
utils::TimeStamp updated;
// Used for 'change' messages which update price
std::unordered_map<ExchangeOrderKey, key_t> orderid_index;
btree::map<key_t, Data, LessBid> bid;
btree::map<key_t, Data> ask;
};
void copy_to_snapshot(MBOWorkingState &state, MBOSnapshot *destination);
void apply_incremental(MBOWorkingState &, const inc::Update &);
template <class V>
void apply_incremental(MBOWorkingState &state, const V &vec) {
for (const auto &update : vec) {
apply_incremental(state, update);
}
}
struct MBOProcessorImpl;
class MBOProcessor;
class MBOWriter {
std::shared_ptr<MBOProcessorImpl> _me;
friend class MBOProcessor;
MBOWriter(std::shared_ptr<MBOProcessorImpl> &);
~MBOWriter();
public:
//
// Snapshot api
MBOSnapshot *allocate_snapshot(size_t bid_count = 0, size_t ask_count = 0);
void release_snapshot(MBOSnapshot **);
void publish_snapshot(SecurityKey, MBOSnapshot **);
bool validate_snapshot(uint32_t, SecurityKey, MBOSnapshot *);
//
// Incremental api
// Enqueue the update to the mbo work queue.
void enqueue_incremental(MBOWorkingState &, const inc::Update &);
};
class MBOReader {
std::shared_ptr<MBOProcessorImpl> _me;
friend class MBOProcessor;
friend class MBOWriter;
MBOReader(std::shared_ptr<MBOProcessorImpl> &);
~MBOReader();
public:
MBOSnapshot *acquire_snapshot(SecurityKey key) const;
void release_snapshot(MBOSnapshot **) const;
};
class MBOProcessor {
std::shared_ptr<MBOProcessorImpl> _me;
MBOWriter _writer;
MBOReader _reader;
public:
MBOProcessor();
MBOWriter &writer();
MBOReader &reader();
};
} // namespace md
} // namespace bts
namespace std {
std::ostream &operator<<(std::ostream &os, const bts::md::inc::Update &);
}