241 lines
7.0 KiB
C++
241 lines
7.0 KiB
C++
#ifndef NO_CPP0X
|
|
#include "MDProducerHandle.h"
|
|
#include "common.h"
|
|
|
|
namespace bts {
|
|
namespace md {
|
|
using namespace datetime;
|
|
using namespace co2;
|
|
|
|
MDProducerHandle::MDProducerHandle(
|
|
ColdHeader &cheader,
|
|
HotHeader &hheader,
|
|
ColdEntry ¢ry,
|
|
void *phot,
|
|
MDProducerChannel channel)
|
|
: _hdata(phot), _hheader(&hheader), _cdata(¢ry), _cheader(&cheader),
|
|
_channel(channel) {}
|
|
|
|
MDProducerHandle::MDProducerHandle()
|
|
: _hdata(nullptr), _hheader(nullptr), _cdata(nullptr), _cheader(nullptr),
|
|
_channel() {}
|
|
|
|
MDProducerHandle::~MDProducerHandle() {}
|
|
|
|
MDProducerHandle &MDProducerHandle::operator=(MDProducerHandle const &other) {
|
|
_hdata = other._hdata;
|
|
_hheader = other._hheader;
|
|
_cdata = other._cdata;
|
|
_cheader = other._cheader;
|
|
_channel = other._channel;
|
|
return *this;
|
|
}
|
|
|
|
void MDProducerHandle::set_key(security_key key) {
|
|
if (cdata().security.key == key)
|
|
return;
|
|
bts_assert(cdata().security.key == invalid_security_key);
|
|
// ! Assignment order important !
|
|
// Once the cold data has a key the security becomes visible so the hot data
|
|
// *must* be able to be found or we assert
|
|
if (has_depth()) {
|
|
hdepth().key = key;
|
|
} else {
|
|
hdata().key = key;
|
|
}
|
|
|
|
cdata().security.key = key;
|
|
|
|
// Increment public security count
|
|
cheader().security_count++;
|
|
|
|
// If this is a spread make sure the legs have valid keys
|
|
if (security().type == api::SECURITYTYPE_SPREAD) {
|
|
const auto &psecurity =
|
|
static_cast<const FixedSpreadSecurityXL &>(security());
|
|
for (size_t x = 0; x < psecurity.leg_count(); ++x) {
|
|
bts_assert(psecurity.legs[x].key != invalid_security_key);
|
|
}
|
|
}
|
|
}
|
|
|
|
void MDProducerHandle::update_bbo(
|
|
TimeStamp time, const PriceSize bid, const PriceSize ask, uint16_t seq) {
|
|
if (!has_depth()) {
|
|
auto oldtime = hdata().time();
|
|
hdata().set_time(TimeStamp::invalid());
|
|
bool updated = hdata().book.update(PriceLevel(bid, ask));
|
|
hdata().status = COCONUTSECURITYSTATUS_OK;
|
|
if (updated) {
|
|
hdata().set_time(time);
|
|
hdata().sequence = seq;
|
|
hheader().direct_count++;
|
|
} else {
|
|
hdata().set_time(oldtime);
|
|
}
|
|
} else {
|
|
auto oldtime = hdepth().time();
|
|
hdepth().set_time(TimeStamp::invalid());
|
|
bool updated = hdepth().write_direct(0, bid, ask);
|
|
hdepth().status = COCONUTSECURITYSTATUS_OK;
|
|
if (updated) {
|
|
hdepth().set_time(time);
|
|
hdepth().sequence = seq;
|
|
hheader().direct_count++;
|
|
} else {
|
|
hdepth().set_time(oldtime);
|
|
}
|
|
}
|
|
}
|
|
|
|
void MDProducerHandle::set_status(CoconutSecurityStatus status) {
|
|
if (has_depth()) {
|
|
hdepth().status = status;
|
|
} else {
|
|
hdata().status = status;
|
|
}
|
|
}
|
|
|
|
void MDProducerHandle::add_trade(
|
|
TimeStamp time,
|
|
const PriceSize price,
|
|
LLDateTime tradetime,
|
|
co2::AggressorSide agside,
|
|
uint32_t volume,
|
|
bool from_spread) {
|
|
// Add this trade to the last trade if price/side match
|
|
auto current = std::get<PriceSize>(cdata().last_trade.load());
|
|
if (current.int_price() == price.int_price()) {
|
|
cdata().last_trade.store(
|
|
PriceSize{price.int_price(), price.size() + current.size()}, time);
|
|
} else {
|
|
cdata().last_trade.store(price, time);
|
|
}
|
|
cdata().last_trade_time = tradetime;
|
|
cdata().from_spread = (from_spread ? 1 : 0);
|
|
if (volume > 0)
|
|
cdata().total_volume = volume;
|
|
|
|
if (key() != invalid_security_key) {
|
|
co2::TradeMessage trade;
|
|
trade.set_exponent(price.exponent());
|
|
trade.set_mantissa(price.mantissa());
|
|
trade.set_size(price.size());
|
|
trade.set_time(tradetime);
|
|
trade.set_aggressor_side(agside);
|
|
if (volume > 0)
|
|
trade.set_total_volume(volume);
|
|
trade.set_key(key());
|
|
_channel.add_trade(trade);
|
|
}
|
|
}
|
|
void MDProducerHandle::set_last_trade(
|
|
TimeStamp time,
|
|
const PriceSize ps,
|
|
datetime::LLDateTime tradetime,
|
|
uint32_t total_volume,
|
|
bool from_spread) {
|
|
cdata().last_trade.store(ps, time);
|
|
cdata().last_trade_time = tradetime;
|
|
cdata().from_spread = (from_spread ? 1 : 0);
|
|
if (total_volume > 0)
|
|
cdata().total_volume = total_volume;
|
|
}
|
|
|
|
void MDProducerHandle::set_total_volume(uint32_t total_volume) {
|
|
cdata().total_volume = total_volume;
|
|
}
|
|
|
|
void MDProducerHandle::add_rfq(TimeStamp, const co2::QuoteRequestMessage &rfq) {
|
|
if (rfq.key() != invalid_security_key) {
|
|
bts_assert(security().key == rfq.key());
|
|
_channel.add_rfq(rfq);
|
|
}
|
|
}
|
|
|
|
void MDProducerHandle::update_exchange_status(TimeStamp, TradingStatus status) {
|
|
if (status != TRADINGSTATUS_NO_CHANGE) {
|
|
cdata().exchange_status = status;
|
|
cheader().exchange_status_count++;
|
|
}
|
|
}
|
|
|
|
void MDProducerHandle::update_settlement(
|
|
TimeStamp time, IntPrice price, Date date) {
|
|
if (date > cdata().misc.settlement_date) {
|
|
cdata().misc.previous_settlement_price =
|
|
has_depth() ? hdepth().settlement : hdata().settlement;
|
|
cdata().misc.previous_settlement_date = cdata().misc.settlement_date;
|
|
}
|
|
if (has_depth()) {
|
|
hdepth().settlement = price;
|
|
} else {
|
|
hdata().settlement = price;
|
|
}
|
|
cdata().misc.settlement_date = date;
|
|
cdata().misc.misc_timestamp = time;
|
|
}
|
|
void MDProducerHandle::update_previous_settlement(
|
|
TimeStamp time, IntPrice price, Date date) {
|
|
cdata().misc.previous_settlement_price = price;
|
|
cdata().misc.previous_settlement_date = date;
|
|
cdata().misc.misc_timestamp = time;
|
|
}
|
|
|
|
void MDProducerHandle::update_opening(TimeStamp time, Price price, Date date) {
|
|
cdata().misc.open_price = price;
|
|
cdata().misc.open_date = date;
|
|
cdata().misc.misc_timestamp = time;
|
|
}
|
|
|
|
void MDProducerHandle::update_closing(TimeStamp time, Price price, Date date) {
|
|
cdata().misc.close_price = price;
|
|
cdata().misc.close_date = date;
|
|
cdata().misc.misc_timestamp = time;
|
|
}
|
|
|
|
void MDProducerHandle::update_session_high(TimeStamp time, Price price) {
|
|
cdata().misc.session_high = price;
|
|
cdata().misc.misc_timestamp = time;
|
|
}
|
|
|
|
void MDProducerHandle::update_session_low(TimeStamp time, Price price) {
|
|
cdata().misc.session_low = price;
|
|
cdata().misc.misc_timestamp = time;
|
|
}
|
|
|
|
void MDProducerHandle::update_session_high_bid(TimeStamp, Price) {
|
|
// cdata().misc.session_high_bid = price;
|
|
// cdata().misc_timestamp = TimeStamp::now();
|
|
}
|
|
|
|
void MDProducerHandle::update_session_low_offer(TimeStamp, Price) {
|
|
// cdata().misc.session_low_offer = price;
|
|
// cdata().misc_timestamp = TimeStamp::now();
|
|
}
|
|
|
|
void MDProducerHandle::update_fixing_price(TimeStamp, Price) {
|
|
// cdata().misc.fixing_price = price;
|
|
// cdata().misc_timestamp = TimeStamp::now();
|
|
}
|
|
|
|
void MDProducerHandle::mark_deleted() {
|
|
TimeStamp ts = TimeStamp::now();
|
|
cdata().deleted = true;
|
|
std::array<PriceSize, COCONUT_MAX_DEPTH> blank;
|
|
std::array<PriceSize, COCONUT_MAX_IMPLIED_DEPTH> iblank;
|
|
for (size_t x = 0; x < blank.size(); ++x) {
|
|
blank[x] = NO_MARKET;
|
|
if (x < COCONUT_MAX_IMPLIED_DEPTH) {
|
|
iblank[x] = NO_MARKET;
|
|
}
|
|
}
|
|
update_direct(ts, blank, blank, 0);
|
|
update_implied(ts, iblank, iblank, 0);
|
|
}
|
|
|
|
} // namespace md
|
|
} // namespace bts
|
|
|
|
#endif
|