Files
spark/src/lib/md/MBOProcessor.h
T

185 lines
4.4 KiB
C++

#pragma once
#include "lib/domain_model/SecurityKey.h"
#include "lib/domain_model/Security.h"
#include "lib/utils/TimeStamp.h"
#include "IntPrice.h"
#include "ExchangeOrderKey.h"
#include "MDProducerHandle.h"
#ifndef _WIN32
#include "google/btree/btree_map.h"
#endif
#include <vector>
#include <memory>
#include <map>
#include <tuple>
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;
};
}
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() {}
MDProducerHandle handle;
utils::TimeStamp updated;
// Used for 'change' messages which update price
std::unordered_map<ExchangeOrderKey, key_t> orderid_index;
#ifdef _WIN32
std::map<key_t,Data,LessBid> bid;
std::map<key_t,Data> ask;
#else
btree::btree_map<key_t,Data,LessBid> bid;
btree::btree_map<key_t,Data> ask;
#endif
};
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 std {
std::ostream& operator<<(std::ostream& os, const bts::md::inc::Update&);
}