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

160 lines
5.4 KiB
C++

#pragma once
#include "lib/utils/bts_no_copy.h"
#include "lib/utils/bts_export.h"
#include "Shm/MDStructure.h"
#include "MDProducerChannel.h"
#include "lib/utils/datetime.h"
#include <tbb/concurrent_queue.h>
#include <vector>
namespace bts {
namespace co2 { struct Work; }
namespace md {
class BTS_EXPORT MDProducerHandle
{
public:
MDProducerHandle(ColdHeader&, HotHeader&, ColdEntry&, void* hot, MDProducerChannel channel);
~MDProducerHandle();
MDProducerHandle& operator=(MDProducerHandle const& other);
const FixedSecurity& security() const { return cdata().security; }
FixedSecurity* mutable_security() { return key() == invalid_security_key ? &cdata().security : nullptr; }
bool has_depth() const { return cheader().has_depth; }
void set_key(security_key);
security_key key() const {
security_key key = has_depth() ? hdepth().key : hdata().key;
bts_assert_d(key == cdata().security.key);
return key;
}
void update_bbo(TimeStamp time, const PriceSize bid_price, const PriceSize ask_price, uint16_t seq=0);
void update_direct(TimeStamp time, const std::array<PriceSize,COCONUT_MAX_DEPTH>& bids, const std::array<PriceSize,COCONUT_MAX_DEPTH>& asks, uint32_t seq=0) {
if (!has_depth()) {
update_bbo(time, bids[0], asks[0], seq);
}
else {
auto& book = hdepth();
book.set_time(TimeStamp::invalid());
for (size_t x = 0; x < COCONUT_MAX_DEPTH; ++x) {
book.write_direct(x, bids[x], asks[x]);
}
book.status = co2::COCONUTSECURITYSTATUS_OK;
book.sequence = seq;
book.set_time(time);
hheader().direct_count += 1;
}
}
void update_implied(TimeStamp time, const std::array<PriceSize,COCONUT_MAX_IMPLIED_DEPTH>& bids, const std::array<PriceSize,COCONUT_MAX_IMPLIED_DEPTH>& asks, uint32_t seq=0) {
if (has_depth()) {
auto& book = hdepth();
book.set_time(TimeStamp::invalid());
for (size_t x = 0; x < COCONUT_MAX_IMPLIED_DEPTH; ++x) {
book.write_implied(x, bids[x], asks[x]);
}
book.status = co2::COCONUTSECURITYSTATUS_OK;
book.sequence = seq;
book.set_time(time);
hheader().implied_count += 1;
}
}
void update_books(TimeStamp time,
const std::array<PriceSize, COCONUT_MAX_DEPTH>& bid,
const std::array<PriceSize,COCONUT_MAX_DEPTH>& ask,
const std::array<PriceSize, COCONUT_MAX_IMPLIED_DEPTH>& implied_bid,
const std::array<PriceSize,COCONUT_MAX_IMPLIED_DEPTH>& implied_ask,
uint32_t seq=0) {
if (has_depth()) {
auto& book = hdepth();
book.set_time(TimeStamp::invalid());
for (size_t x = 0; x < COCONUT_MAX_DEPTH; ++x) {
if (x < COCONUT_MAX_IMPLIED_DEPTH) {
book.write_implied(x, implied_bid[x], implied_ask[x]);
}
book.write_direct(x, bid[x], ask[x]);
}
book.status = co2::COCONUTSECURITYSTATUS_OK;
book.sequence = seq;
book.set_time(time);
hheader().direct_count += 1;
hheader().implied_count += 1;
}
}
void set_status(co2::CoconutSecurityStatus status); // hot
// Updates trade log and last trade
void add_trade(TimeStamp time, const PriceSize pricesize, datetime::LLDateTime date,
co2::AggressorSide agside, uint32_t total_volume=0, bool from_spread=false); // cold
// Update last_trade without updating trade log
void set_last_trade(TimeStamp time, const PriceSize ps, datetime::LLDateTime date, uint32_t total_volume=0,bool from_spread=false);
void set_total_volume(uint32_t total_volume);
void add_rfq(TimeStamp time, const co2::QuoteRequestMessage& rfq); // cold
void update_exchange_status(TimeStamp time, TradingStatus status); // cold
// Misc fields
void update_settlement(TimeStamp time, IntPrice price, datetime::Date date); // cold
void update_previous_settlement(TimeStamp time, IntPrice price, datetime::Date date); // cold
void update_opening(TimeStamp time, Price price, datetime::Date date); // cold
void update_closing(TimeStamp time, Price price, datetime::Date date); // cold
void update_session_high(TimeStamp time, Price price); // cold
void update_session_low(TimeStamp time, Price price); // cold
void update_session_high_bid(TimeStamp time, Price price); // cold
void update_session_low_offer(TimeStamp time, Price price); // cold
void update_fixing_price(TimeStamp time, Price price); // cold
void mark_deleted(); // cold
private:
// Private default constructor allows friend classes to store this class in a concurrent_queue
MDProducerHandle();
friend class tbb::concurrent_queue < MDProducerHandle > ;
friend struct SecurityIDRecorderState;
friend struct bts::co2::Work;
HotEntry& hdata() const {
bts_assert_d(_hdata && !has_depth());
return *static_cast<HotEntry*>(_hdata);
}
HotDepthEntry& hdepth() const {
bts_assert_d(_hdata && has_depth());
return *static_cast<HotDepthEntry*>(_hdata);
}
HotHeader& hheader() const {
bts_assert_d(_hheader);
return *_hheader;
}
ColdEntry& cdata() const {
bts_assert_d(_cdata);
return *_cdata;
}
ColdHeader& cheader() const {
bts_assert_d(_cheader);
return *_cheader;
}
// COPY OK -- BTS_NO_COPY(MDConsumerHandle);
void* _hdata;
HotHeader* _hheader;
ColdEntry* _cdata;
ColdHeader* _cheader;
MDProducerChannel _channel;
};
}}//namespace