SPARK-994, We are more economical in how we represent the sim book.
This commit is contained in:
1 parent
16bf0a7236
commit
fe7bf108f3
4 files changed
+134
-81
No files matched your search
@@ -97,13 +97,12 @@ struct ExchangeSimApplication::MDConsumerRec {
|
||||
|
||||
struct ExchangeSimApplication::PImpl {
|
||||
PImpl()
|
||||
: producer(co2::PLATFORM_SIM),
|
||||
consumer(api::VENUE_GLBX, co2::PLATFORM_MDP3ONIXS),
|
||||
timer(utils::Dispatcher::background(), 200,
|
||||
[=]() { producer.heartbeat(); }),
|
||||
do_merge_with_md(config::get<config::ExchangeSim>().merge_with_md()),
|
||||
store(new persistence::SqliteStore("ex_sim_orders"))
|
||||
{}
|
||||
: producer(co2::PLATFORM_SIM),
|
||||
consumer(api::VENUE_GLBX, co2::PLATFORM_MDP3ONIXS),
|
||||
timer(utils::Dispatcher::background(), 200,
|
||||
[=]() { producer.heartbeat(); }),
|
||||
do_merge_with_md(config::get<config::ExchangeSim>().merge_with_md()),
|
||||
store(new persistence::SqliteStore("ex_sim_orders")) {}
|
||||
|
||||
IDGenerator m_generator;
|
||||
md::MDProducer producer;
|
||||
@@ -120,25 +119,25 @@ struct ExchangeSimApplication::PImpl {
|
||||
|
||||
ExchangeSimApplication::ExchangeSimApplication() : me(new PImpl()) {
|
||||
auto previous_book = me->store->retrieve_all();
|
||||
|
||||
auto all = me->store->retrieve_all()["books"];
|
||||
|
||||
auto all = me->store->retrieve_all()["books"];
|
||||
for (auto const &kvp : all) {
|
||||
OrderBook book;
|
||||
std::vector<Order> out;
|
||||
|
||||
if (utils::pb::from_string<OrderBook>(kvp.second, &book)) {
|
||||
for (const auto & o : book.order())
|
||||
me->matchers[o.sec_desc()].add(o,&out);
|
||||
for (const auto &o : book.order())
|
||||
me->matchers[o.sec_desc()].add_new(o, &out);
|
||||
} else
|
||||
LOG(ExSim, Info,
|
||||
"Could not recover simulator orders for {}", kvp.first);
|
||||
LOG(ExSim, Info, "Could not recover simulator orders for {}",
|
||||
kvp.first);
|
||||
}
|
||||
|
||||
me->timer.start();
|
||||
me->consumer.on_security(
|
||||
[this](api::Security const &s, md::MDConsumerHandle h) {
|
||||
on_new_security(s, h);
|
||||
});
|
||||
[this](api::Security const &s, md::MDConsumerHandle h) {
|
||||
on_new_security(s, h);
|
||||
});
|
||||
}
|
||||
|
||||
ExchangeSimApplication::~ExchangeSimApplication() {}
|
||||
@@ -148,18 +147,18 @@ void ExchangeSimApplication::onLogon(const FIX::SessionID &) {}
|
||||
void ExchangeSimApplication::onLogout(const FIX::SessionID &) {}
|
||||
void ExchangeSimApplication::toAdmin(FIX::Message &, const FIX::SessionID &) {}
|
||||
void ExchangeSimApplication::toApp(
|
||||
FIX::Message &, const FIX::SessionID &) throw(FIX::DoNotSend) {}
|
||||
FIX::Message &, const FIX::SessionID &) throw(FIX::DoNotSend) {}
|
||||
void ExchangeSimApplication::fromAdmin(
|
||||
const FIX::Message &,
|
||||
const FIX::SessionID &) throw(FIX::FieldNotFound, FIX::IncorrectDataFormat,
|
||||
FIX::IncorrectTagValue, FIX::RejectLogon) {}
|
||||
const FIX::Message &,
|
||||
const FIX::SessionID &) throw(FIX::FieldNotFound, FIX::IncorrectDataFormat,
|
||||
FIX::IncorrectTagValue, FIX::RejectLogon) {}
|
||||
|
||||
void ExchangeSimApplication::fromApp(
|
||||
const FIX::Message &message,
|
||||
const FIX::SessionID &sessionID) throw(FIX::FieldNotFound,
|
||||
FIX::IncorrectDataFormat,
|
||||
FIX::IncorrectTagValue,
|
||||
FIX::UnsupportedMessageType) {
|
||||
const FIX::Message &message,
|
||||
const FIX::SessionID &sessionID) throw(FIX::FieldNotFound,
|
||||
FIX::IncorrectDataFormat,
|
||||
FIX::IncorrectTagValue,
|
||||
FIX::UnsupportedMessageType) {
|
||||
std::lock_guard<std::mutex> l(me->lock);
|
||||
crack(message, sessionID);
|
||||
}
|
||||
@@ -204,7 +203,7 @@ void ExchangeSimApplication::onMessage(const FIX42::NewOrderSingle &message,
|
||||
throw std::logic_error("Duplicate order");
|
||||
|
||||
ex_sim::Order order;
|
||||
order.set_clordid (clOrdID);
|
||||
order.set_clordid(clOrdID);
|
||||
order.set_sec_desc(sec_desc);
|
||||
order.set_account(account);
|
||||
order.set_owner(senderCompID);
|
||||
@@ -224,7 +223,7 @@ void ExchangeSimApplication::onMessage(const FIX42::NewOrderSingle &message,
|
||||
|
||||
updateOrder(order, FIX::OrdStatus_NEW);
|
||||
std::vector<Order> updates;
|
||||
matcher.add(order, &updates);
|
||||
matcher.add_new(order, &updates);
|
||||
for (auto const &o : updates)
|
||||
fillOrder(o);
|
||||
update_md(sec_desc);
|
||||
@@ -235,7 +234,7 @@ void ExchangeSimApplication::onMessage(const FIX42::NewOrderSingle &message,
|
||||
}
|
||||
|
||||
void ExchangeSimApplication::onMessage(
|
||||
const FIX42::OrderCancelReplaceRequest &message, const FIX::SessionID &) {
|
||||
const FIX42::OrderCancelReplaceRequest &message, const FIX::SessionID &) {
|
||||
FIX::Price price;
|
||||
FIX::OrderQty orderQty;
|
||||
FIX::ClOrdID clOrdID;
|
||||
@@ -265,7 +264,7 @@ void ExchangeSimApplication::onMessage(
|
||||
throw std::logic_error("Unknown order");
|
||||
else {
|
||||
auto order_cpy = *order;
|
||||
order_cpy.set_price (price);
|
||||
order_cpy.set_price(price);
|
||||
order_cpy.set_quantity(orderQty);
|
||||
updateOrder(order_cpy, FIX::OrdStatus_REPLACED);
|
||||
|
||||
@@ -314,9 +313,8 @@ void ExchangeSimApplication::rejectOrder(const Order &order) {
|
||||
|
||||
void ExchangeSimApplication::fillOrder(const Order &order) {
|
||||
updateOrder(order,
|
||||
order.open_quantity() == 0
|
||||
? FIX::OrdStatus_FILLED
|
||||
: FIX::OrdStatus_PARTIALLY_FILLED);
|
||||
order.open_quantity() == 0 ? FIX::OrdStatus_FILLED
|
||||
: FIX::OrdStatus_PARTIALLY_FILLED);
|
||||
}
|
||||
|
||||
void ExchangeSimApplication::updateOrder(const Order &order, char status) {
|
||||
@@ -346,12 +344,12 @@ void ExchangeSimApplication::updateOrder(const Order &order, char status) {
|
||||
fixOrder.setField(convert(order.moi()));
|
||||
|
||||
boost::posix_time::ptime now =
|
||||
boost::posix_time::second_clock::universal_time();
|
||||
boost::posix_time::second_clock::universal_time();
|
||||
fixOrder.set(FIX::TransactTime(
|
||||
FIX::UtcTimeStamp(now.time_of_day().hours(), now.time_of_day().minutes(),
|
||||
now.time_of_day().seconds(), now.date().day(),
|
||||
now.date().month(), now.date().year()),
|
||||
true));
|
||||
FIX::UtcTimeStamp(now.time_of_day().hours(), now.time_of_day().minutes(),
|
||||
now.time_of_day().seconds(), now.date().day(),
|
||||
now.date().month(), now.date().year()),
|
||||
true));
|
||||
if (status == FIX::OrdStatus_FILLED ||
|
||||
status == FIX::OrdStatus_PARTIALLY_FILLED) {
|
||||
fixOrder.set(FIX::LastShares(order.last_executed_quantity()));
|
||||
@@ -366,10 +364,10 @@ void ExchangeSimApplication::updateOrder(const Order &order, char status) {
|
||||
}
|
||||
|
||||
void ExchangeSimApplication::rejectOrder(
|
||||
const FIX::SenderCompID &sender, const FIX::SenderSubID &sender_sub_id,
|
||||
const FIX::TargetCompID &target, const FIX::ClOrdID &clOrdID,
|
||||
const FIX::SecurityDesc &symbol, const FIX::Side &side,
|
||||
const std::string &account, const std::string &message) {
|
||||
const FIX::SenderCompID &sender, const FIX::SenderSubID &sender_sub_id,
|
||||
const FIX::TargetCompID &target, const FIX::ClOrdID &clOrdID,
|
||||
const FIX::SecurityDesc &symbol, const FIX::Side &side,
|
||||
const std::string &account, const std::string &message) {
|
||||
|
||||
FIX::TargetCompID targetCompID(sender.getValue());
|
||||
FIX::SenderCompID senderCompID(target.getValue());
|
||||
@@ -399,12 +397,12 @@ void ExchangeSimApplication::rejectOrder(
|
||||
fixOrder.getHeader().set(FIX::TargetSubID(sender_sub_id.getValue()));
|
||||
|
||||
boost::posix_time::ptime now =
|
||||
boost::posix_time::second_clock::universal_time();
|
||||
boost::posix_time::second_clock::universal_time();
|
||||
fixOrder.set(FIX::TransactTime(
|
||||
FIX::UtcTimeStamp(now.time_of_day().hours(), now.time_of_day().minutes(),
|
||||
now.time_of_day().seconds(), now.date().day(),
|
||||
now.date().month(), now.date().year()),
|
||||
true));
|
||||
FIX::UtcTimeStamp(now.time_of_day().hours(), now.time_of_day().minutes(),
|
||||
now.time_of_day().seconds(), now.date().day(),
|
||||
now.date().month(), now.date().year()),
|
||||
true));
|
||||
try {
|
||||
FIX::Session::sendToTarget(fixOrder, senderCompID, targetCompID);
|
||||
} catch (FIX::SessionNotFound &) {
|
||||
@@ -454,24 +452,26 @@ void ExchangeSimApplication::update_md(std::string const &sec_desc) {
|
||||
}
|
||||
|
||||
auto new_trades = matcher.get_new_trades();
|
||||
for (auto &t : new_trades)
|
||||
handle.add_trade(utils::TimeStamp::now(),
|
||||
md::PriceSize::from_double<7>(t.price.value() * price_factor,
|
||||
t.quantity.value()), datetime::ll_now());
|
||||
for (auto &t : new_trades)
|
||||
handle.add_trade(
|
||||
utils::TimeStamp::now(),
|
||||
md::PriceSize::from_double<7>(t.price.value() * price_factor,
|
||||
t.quantity.value()),
|
||||
datetime::ll_now());
|
||||
}
|
||||
}
|
||||
|
||||
void ExchangeSimApplication::on_new_security(
|
||||
api::Security const &s, md::MDConsumerHandle consumer_handle) {
|
||||
api::Security const &s, md::MDConsumerHandle consumer_handle) {
|
||||
md::with_security_like(s, [this, s, consumer_handle](auto &fs) {
|
||||
md::convert_security(s, fs, true);
|
||||
md::MDProducerHandle handle = me->producer.add_security(fs);
|
||||
auto sec_desc = securities::descriptor(s);
|
||||
me->md_producer_handles.insert(make_pair(sec_desc, handle));
|
||||
me->md_consumer_recs.insert(
|
||||
make_pair(sec_desc, MDConsumerRec{consumer_handle, s,
|
||||
utils::TimeStamp::now(), Book()}));
|
||||
});
|
||||
md::convert_security(s, fs, true);
|
||||
md::MDProducerHandle handle = me->producer.add_security(fs);
|
||||
auto sec_desc = securities::descriptor(s);
|
||||
me->md_producer_handles.insert(make_pair(sec_desc, handle));
|
||||
me->md_consumer_recs.insert(
|
||||
make_pair(sec_desc, MDConsumerRec{consumer_handle, s,
|
||||
utils::TimeStamp::now(), Book()}));
|
||||
});
|
||||
}
|
||||
|
||||
void ExchangeSimApplication::merge_with_md_book() {
|
||||
@@ -501,13 +501,13 @@ void ExchangeSimApplication::merge_with_md_book() {
|
||||
for (size_t i = 0; i < md::COCONUT_MAX_DEPTH; i++) {
|
||||
|
||||
auto b = securities::encode_exchange_price(
|
||||
sec, Price(bids[i].price()));
|
||||
if (!os::is_nan(b.value()))
|
||||
sec, Price(bids[i].price()));
|
||||
if (!os::is_nan(b.value()))
|
||||
current_md_book.bids[b] = Quantity(bids[i].size());
|
||||
|
||||
auto a = securities::encode_exchange_price(
|
||||
sec, Price(asks[i].price()));
|
||||
if (!os::is_nan(a.value()))
|
||||
sec, Price(asks[i].price()));
|
||||
if (!os::is_nan(a.value()))
|
||||
current_md_book.asks[a] = Quantity(asks[i].size());
|
||||
}
|
||||
|
||||
@@ -529,7 +529,8 @@ void ExchangeSimApplication::merge_with_md_book(std::string const &sec_desc,
|
||||
|
||||
for (auto const &op : transform) {
|
||||
|
||||
LOG(ExSim, Diag, "Adding synthetic book operation for {}. {} {} {}@{}", sec_desc,
|
||||
LOG(ExSim, Diag, "Adding synthetic book operation for {}. {} {} {}@{}",
|
||||
sec_desc,
|
||||
op.op == BookOp::ORDER ? std::string("Order") : std::string("Cancel"),
|
||||
op.side == api::SIDE_BUY ? std::string("Buy") : std::string("Sell"),
|
||||
op.quantity.value(), op.price.value());
|
||||
@@ -537,7 +538,8 @@ void ExchangeSimApplication::merge_with_md_book(std::string const &sec_desc,
|
||||
switch (op.op) {
|
||||
case BookOp::ORDER: {
|
||||
ex_sim::Order order;
|
||||
order.set_clordid (fmt::format("synth-{}", me->m_generator.genOrderID()));
|
||||
order.set_clordid(
|
||||
fmt::format("synth-{}", me->m_generator.genOrderID()));
|
||||
order.set_sec_desc(sec_desc);
|
||||
order.set_account("SYNTH");
|
||||
order.set_owner("SYNTH");
|
||||
@@ -556,21 +558,22 @@ void ExchangeSimApplication::merge_with_md_book(std::string const &sec_desc,
|
||||
*order.mutable_create_time() = utils::pb::pb_now();
|
||||
|
||||
std::vector<Order> updates;
|
||||
matcher.add(order, &updates);
|
||||
matcher.add_or_merge(order, &updates);
|
||||
for (auto const &o : updates)
|
||||
if (o.account() != "SYNTH") {
|
||||
LOG(ExSim, Diag,
|
||||
"Generating fill in response to md change for {}: {} {}@{}",
|
||||
sec_desc,
|
||||
o.side() == api::SIDE_BUY ? std::string("Buy")
|
||||
: std::string("Sell"),
|
||||
: std::string("Sell"),
|
||||
o.open_quantity(), o.price());
|
||||
fillOrder(o);
|
||||
}
|
||||
break;
|
||||
}
|
||||
case BookOp::CANCEL:
|
||||
matcher.try_reduce_quantity_at_level(op.side,op.price,op.quantity,"SYNTH");
|
||||
matcher.try_reduce_quantity_at_level(op.side, op.price, op.quantity,
|
||||
"SYNTH");
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -579,20 +582,20 @@ void ExchangeSimApplication::merge_with_md_book(std::string const &sec_desc,
|
||||
auto order_book = matcher.get_order_book("SYNTH");
|
||||
order_book.set_sec_desc(sec_desc);
|
||||
persist_book(order_book);
|
||||
auto sz = matcher.size();
|
||||
LOG(ExSim,Info,"Book size: {} {} {} {}",sec_desc,std::get<0>(sz),std::get<1>(sz),std::get<2>(sz));
|
||||
}
|
||||
|
||||
void ExchangeSimApplication::persist_book(OrderBook const &b)
|
||||
{
|
||||
auto existing = me->store->retrieve_one("books",b.sec_desc());
|
||||
void ExchangeSimApplication::persist_book(OrderBook const &b) {
|
||||
auto existing = me->store->retrieve_one("books", b.sec_desc());
|
||||
auto val = utils::pb::to_string(b);
|
||||
|
||||
if (existing.first)
|
||||
if (existing.first)
|
||||
me->store->update("books", b.sec_desc(), val);
|
||||
else
|
||||
me->store->insert("books", b.sec_desc(), val);
|
||||
}
|
||||
|
||||
|
||||
GCC_DIAG_RESTORE()
|
||||
GCC_DIAG_RESTORE()
|
||||
}
|
||||
|
||||
@@ -36,3 +36,5 @@ struct OrderCmp {
|
||||
|
||||
|
||||
}}
|
||||
|
||||
|
||||
@@ -39,7 +39,12 @@ Order const *OrderMatcher::get_order(std::string const &clordid) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
void OrderMatcher::add(Order const &o, std::vector<Order> *out) {
|
||||
std::tuple<size_t,size_t,size_t> OrderMatcher::size() const
|
||||
{
|
||||
return std::make_tuple (me->bids.size(), me->bids.size(), me->pending_trades.size());
|
||||
}
|
||||
|
||||
void OrderMatcher::add_new(Order const &o, std::vector<Order> *out) {
|
||||
bts_assert(!get_order(o.clordid()));
|
||||
if (o.side() == api::SIDE_BUY)
|
||||
me->bids.insert(o);
|
||||
@@ -48,6 +53,46 @@ void OrderMatcher::add(Order const &o, std::vector<Order> *out) {
|
||||
match(out);
|
||||
}
|
||||
|
||||
std::pair<OrderMatcher::OrdersSide::iterator, OrderMatcher::OrdersSide::iterator>
|
||||
OrderMatcher::find_level(OrdersSide &side, double level)
|
||||
{
|
||||
std::pair<OrdersSide::iterator, OrdersSide::iterator> result {side.end(), side.end()};
|
||||
|
||||
for (auto it = side.begin() ; it != side.end(); it++) {
|
||||
|
||||
if (it->price() == level) {
|
||||
result.first = it;
|
||||
for (;it != side.end(); it++) {
|
||||
if (it->price() != level)
|
||||
break;
|
||||
}
|
||||
result.second = it;
|
||||
break;
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
void OrderMatcher::add_or_merge(Order const &o, std::vector<Order> *out) {
|
||||
bts_assert(!get_order(o.clordid()));
|
||||
OrdersSide *side = nullptr;
|
||||
if (o.side() == api::SIDE_BUY)
|
||||
side = &me->bids;
|
||||
else
|
||||
side = &me->asks;
|
||||
|
||||
auto range = find_level(*side,o.price());
|
||||
|
||||
for (auto it = range.first; it != range.second; it++)
|
||||
if (it->owner() == o.owner()) {
|
||||
auto no_const = const_cast<Order *>(&(*it));
|
||||
no_const->set_open_quantity(it->open_quantity() + o.quantity());
|
||||
match(out);
|
||||
return;
|
||||
}
|
||||
add_new(o,out);
|
||||
}
|
||||
|
||||
void OrderMatcher::update(std::string const &clordid, Price price, Quantity qty,
|
||||
std::vector<Order> *out) {
|
||||
auto order = get_order(clordid);
|
||||
@@ -87,13 +132,11 @@ void OrderMatcher::try_reduce_quantity_at_level(
|
||||
api::Side side, Price level, Quantity by_quantity,
|
||||
std::string const &order_owner) {
|
||||
std::pair<OrdersSide::iterator, OrdersSide::iterator> range;
|
||||
Order k;
|
||||
k.set_price(level.value());
|
||||
if (side == api::SIDE_BUY)
|
||||
range = me->bids.equal_range(k);
|
||||
range = find_level(me->bids,level.value());
|
||||
else
|
||||
range = me->asks.equal_range(k);
|
||||
|
||||
range = find_level(me->asks,level.value());
|
||||
|
||||
std::vector<std::string> orders_to_cancel;
|
||||
for (auto it = range.first; it != range.second && by_quantity.value();
|
||||
it++) {
|
||||
@@ -190,3 +233,4 @@ OrderBook OrderMatcher::get_order_book(std::string const&exclude_owner) const
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -18,7 +18,9 @@ class OrderMatcher {
|
||||
OrderMatcher();
|
||||
~OrderMatcher();
|
||||
|
||||
void add(Order const &, std::vector<Order> *out);
|
||||
void add_new(Order const &, std::vector<Order> *out);
|
||||
void add_or_merge(Order const &, std::vector<Order> *out);
|
||||
|
||||
void update(std::string const &clorid, Price, Quantity,
|
||||
std::vector<Order> *out);
|
||||
void cancel(std::string const &clorid);
|
||||
@@ -39,12 +41,14 @@ class OrderMatcher {
|
||||
Quantity quantity;
|
||||
};
|
||||
std::vector<Trade> get_new_trades();
|
||||
|
||||
std::tuple<size_t,size_t,size_t> size() const;
|
||||
private:
|
||||
struct PImpl;
|
||||
std::unique_ptr<PImpl> me;
|
||||
void match(std::vector<Order> *out);
|
||||
void match(Order *bid, Order *ask);
|
||||
std::pair<OrderMatcher::OrdersSide::iterator, OrderMatcher::OrdersSide::iterator>
|
||||
find_level(OrdersSide &side, double level);
|
||||
};
|
||||
|
||||
|
||||
|
||||
Reference in new issue
Block a user