Fixed SIM handling of updates to size of partially filled orders.
This commit is contained in:
1 parent
41a96b990d
commit
47887b67d2
3 files changed
+83
-68
No files matched your search
@@ -6,25 +6,25 @@
|
||||
#include "lib/config/protos/ExchangeSimCfg.pb.h"
|
||||
#include "lib/domain_model/Security.h"
|
||||
#include "lib/logging/logging.h"
|
||||
#include "lib/md/MBOProcessor.h"
|
||||
#include "lib/md/MDProducer.h"
|
||||
#include "lib/md/Shm/FixedSecurity.h"
|
||||
#include "lib/md/Shm/MDStructure.h"
|
||||
#include "lib/md/get_symbols.h"
|
||||
#include "lib/md/MBOProcessor.h"
|
||||
#include "lib/orderman/orderman_utils/fix_order_codec.h"
|
||||
#include "lib/persistence/SqliteStore.h"
|
||||
#include "lib/utils/Dispatcher.h"
|
||||
#include "lib/utils/datetime.h"
|
||||
#include "server/sparkd/exchange_sim/book/Order.h"
|
||||
#include "server/sparkd/exchange_sim/book/OrderMatcher.h"
|
||||
#include "server/sparkd/exchange_sim/book/book_transform.h"
|
||||
#include "lib/persistence/SqliteStore.h"
|
||||
#include "lib/orderman/orderman_utils/fix_order_codec.h"
|
||||
|
||||
#include <boost/date_time/posix_time/posix_time.hpp>
|
||||
#include <fmt/format.h>
|
||||
#include <mutex>
|
||||
#include <quickfix/fix42/ExecutionReport.h>
|
||||
#include <quickfix/fix42/OrderCancelReplaceRequest.h>
|
||||
#include <quickfix/fix42/OrderCancelReject.h>
|
||||
#include <quickfix/fix42/OrderCancelReplaceRequest.h>
|
||||
|
||||
#include <sstream>
|
||||
|
||||
@@ -101,19 +101,17 @@ struct ExchangeSimApplication::MDConsumerRec {
|
||||
utils::TimeStamp last_read;
|
||||
};
|
||||
|
||||
inline FIX::OrderID exchange_key(const std::string& clOrdID) {
|
||||
inline FIX::OrderID exchange_key(const std::string &clOrdID) {
|
||||
return FIX::OrderID(clOrdID.substr(3));
|
||||
}
|
||||
|
||||
struct ExchangeSimApplication::PImpl {
|
||||
PImpl(md::MBOWriter* mbo)
|
||||
: producer(co2::PLATFORM_SIM),
|
||||
md_sn(1),
|
||||
PImpl(md::MBOWriter *mbo)
|
||||
: producer(co2::PLATFORM_SIM), md_sn(1),
|
||||
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")),
|
||||
lock(),
|
||||
store(new persistence::SqliteStore("ex_sim_orders")), lock(),
|
||||
mbo(mbo) {}
|
||||
|
||||
IDGenerator m_generator;
|
||||
@@ -126,10 +124,11 @@ struct ExchangeSimApplication::PImpl {
|
||||
bool do_merge_with_md;
|
||||
std::unique_ptr<persistence::IStore> store;
|
||||
std::mutex lock;
|
||||
md::MBOWriter* mbo;
|
||||
md::MBOWriter *mbo;
|
||||
};
|
||||
|
||||
ExchangeSimApplication::ExchangeSimApplication(md::MBOWriter* mbo) : me(new PImpl(mbo)) {
|
||||
ExchangeSimApplication::ExchangeSimApplication(md::MBOWriter *mbo)
|
||||
: me(new PImpl(mbo)) {
|
||||
auto previous_book = me->store->retrieve_all();
|
||||
|
||||
auto all = me->store->retrieve_all()["books"];
|
||||
@@ -204,7 +203,8 @@ void ExchangeSimApplication::onMessage(const FIX42::NewOrderSingle &message,
|
||||
LOG(ExSim, Info, "Got new order: {}", clOrdID.getString());
|
||||
|
||||
try {
|
||||
if (timeInForce != FIX::TimeInForce_DAY && timeInForce != FIX::TimeInForce_IMMEDIATE_OR_CANCEL)
|
||||
if (timeInForce != FIX::TimeInForce_DAY &&
|
||||
timeInForce != FIX::TimeInForce_IMMEDIATE_OR_CANCEL)
|
||||
throw std::logic_error("Unsupported TIF, use Day or IOC");
|
||||
|
||||
auto &matcher = me->matchers[sec_desc];
|
||||
@@ -252,18 +252,19 @@ void ExchangeSimApplication::onMessage(const FIX42::NewOrderSingle &message,
|
||||
*order.mutable_create_time() = utils::pb::pb_now();
|
||||
*order.mutable_last_update_time() = utils::pb::pb_now();
|
||||
|
||||
updateOrder(order, FIX::OrdStatus_NEW);
|
||||
send_execution_report(order, FIX::OrdStatus_NEW);
|
||||
std::vector<Order> updates;
|
||||
matcher.add_new(order, &updates);
|
||||
for (auto const &o : updates)
|
||||
fillOrder(o);
|
||||
if (updates.empty() && timeInForce == FIX::TimeInForce_IMMEDIATE_OR_CANCEL) {
|
||||
if (updates.empty() &&
|
||||
timeInForce == FIX::TimeInForce_IMMEDIATE_OR_CANCEL) {
|
||||
auto order_cpy = order;
|
||||
order_cpy.set_open_quantity(0);
|
||||
updateOrder(order_cpy, FIX::OrdStatus_CANCELED);
|
||||
send_execution_report(order_cpy, FIX::OrdStatus_CANCELED);
|
||||
matcher.cancel(clOrdID);
|
||||
}
|
||||
|
||||
|
||||
update_md(sec_desc);
|
||||
} catch (std::exception &e) {
|
||||
rejectOrder(senderCompID, senderSubID, targetCompID, clOrdID, sec_desc,
|
||||
@@ -303,16 +304,23 @@ void ExchangeSimApplication::onMessage(
|
||||
clOrdID);
|
||||
throw std::logic_error("Unknown order");
|
||||
} else {
|
||||
auto order_cpy = *order;
|
||||
order_cpy.set_price(price);
|
||||
order_cpy.set_quantity(orderQty);
|
||||
*order_cpy.mutable_last_update_time() = utils::pb::pb_now();
|
||||
updateOrder(order_cpy, FIX::OrdStatus_REPLACED);
|
||||
if (order->executed_quantity() >= orderQty) {
|
||||
auto order_cpy = *order;
|
||||
order_cpy.set_open_quantity(0);
|
||||
send_execution_report(order_cpy, FIX::OrdStatus_CANCELED);
|
||||
matcher.cancel(clOrdID);
|
||||
} else {
|
||||
auto order_cpy = *order;
|
||||
order_cpy.set_price(price);
|
||||
order_cpy.set_quantity(orderQty);
|
||||
*order_cpy.mutable_last_update_time() = utils::pb::pb_now();
|
||||
send_execution_report(order_cpy, FIX::OrdStatus_REPLACED);
|
||||
|
||||
std::vector<Order> updates;
|
||||
matcher.update(clOrdID, Price(price), Quantity(orderQty), &updates);
|
||||
for (auto const &o : updates)
|
||||
fillOrder(o);
|
||||
std::vector<Order> updates;
|
||||
matcher.update(clOrdID, Price(price), Quantity(orderQty), &updates);
|
||||
for (auto const &o : updates)
|
||||
fillOrder(o);
|
||||
}
|
||||
update_md(sec_desc);
|
||||
}
|
||||
} catch (std::exception &e) {
|
||||
@@ -345,19 +353,20 @@ void ExchangeSimApplication::onMessage(const FIX42::OrderCancelRequest &message,
|
||||
LOG(ExSim, Info, "Receive cancel for unknown order {}",
|
||||
origClOrdID.getString());
|
||||
|
||||
|
||||
FIX42::OrderCancelReject rej;
|
||||
rej.set(FIX::ClOrdID(origClOrdID.getString()));
|
||||
rej.set(FIX::Text("Unknown order"));
|
||||
rej.getHeader().set(FIX::TargetSubID(senderSubID.getValue()));
|
||||
try {
|
||||
FIX::Session::sendToTarget(rej, targetCompID, senderCompID);
|
||||
LOG(ExSim, Info, "Rejected cancel for order {}", origClOrdID.getString());
|
||||
} catch (FIX::SessionNotFound &) {}
|
||||
LOG(ExSim, Info, "Rejected cancel for order {}",
|
||||
origClOrdID.getString());
|
||||
} catch (FIX::SessionNotFound &) {
|
||||
}
|
||||
} else {
|
||||
auto order_cpy = *order;
|
||||
order_cpy.set_open_quantity(0);
|
||||
updateOrder(order_cpy, FIX::OrdStatus_CANCELED);
|
||||
send_execution_report(order_cpy, FIX::OrdStatus_CANCELED);
|
||||
matcher.cancel(origClOrdID);
|
||||
update_md(sec_desc);
|
||||
}
|
||||
@@ -366,16 +375,16 @@ void ExchangeSimApplication::onMessage(const FIX42::OrderCancelRequest &message,
|
||||
}
|
||||
|
||||
void ExchangeSimApplication::rejectOrder(const Order &order) {
|
||||
updateOrder(order, FIX::OrdStatus_REJECTED);
|
||||
send_execution_report(order, FIX::OrdStatus_REJECTED);
|
||||
}
|
||||
|
||||
void ExchangeSimApplication::fillOrder(const Order &order) {
|
||||
updateOrder(order, order.open_quantity() == 0
|
||||
? FIX::OrdStatus_FILLED
|
||||
: FIX::OrdStatus_PARTIALLY_FILLED);
|
||||
send_execution_report(order, order.open_quantity() == 0
|
||||
? FIX::OrdStatus_FILLED
|
||||
: FIX::OrdStatus_PARTIALLY_FILLED);
|
||||
}
|
||||
|
||||
void ExchangeSimApplication::updateOrder(const Order &order, char status) {
|
||||
void ExchangeSimApplication::send_execution_report(const Order &order, char status) {
|
||||
FIX::TargetCompID targetCompID(order.owner());
|
||||
FIX::SenderCompID senderCompID(order.target());
|
||||
FIX::TargetSubID(order.sender_sub_id());
|
||||
@@ -481,8 +490,8 @@ void ExchangeSimApplication::update_md(std::string const &sec_desc) {
|
||||
|
||||
auto book = matcher.get_book();
|
||||
|
||||
LOG(ExSim, Diag, "Current book {}:\n {}", sec_desc,book);
|
||||
|
||||
LOG(ExSim, Diag, "Current book {}:\n {}", sec_desc, book);
|
||||
|
||||
auto price_factor = pow(10, handle.security().display_factor);
|
||||
|
||||
const auto cmax = md::COCONUT_MAX_DEPTH;
|
||||
@@ -503,17 +512,19 @@ void ExchangeSimApplication::update_md(std::string const &sec_desc) {
|
||||
return md::PriceSize_from_double<precision>(
|
||||
bs.first.value() * price_factor, bs.second.value());
|
||||
});
|
||||
handle.update_direct(utils::TimeStamp::now(), bids_temp, asks_temp, me->md_sn++);
|
||||
handle.update_direct(utils::TimeStamp::now(), bids_temp, asks_temp,
|
||||
me->md_sn++);
|
||||
|
||||
auto new_trades = matcher.get_new_trades();
|
||||
for (auto &t : new_trades) {
|
||||
handle.add_trade(
|
||||
utils::TimeStamp::now(),
|
||||
md::PriceSize_from_double<ex_sim::precision>(t.price.value() * price_factor,t.quantity.value()),
|
||||
md::PriceSize_from_double<ex_sim::precision>(
|
||||
t.price.value() * price_factor, t.quantity.value()),
|
||||
datetime::ll_now(),
|
||||
t.bid_aggressor ? co2::AGGRESSOR_SIDE_BUY
|
||||
: ( t.ask_aggressor ? co2::AGGRESSOR_SIDE_SELL
|
||||
: co2::AGGRESSOR_SIDE_NONE) );
|
||||
t.bid_aggressor ? co2::AGGRESSOR_SIDE_BUY
|
||||
: (t.ask_aggressor ? co2::AGGRESSOR_SIDE_SELL
|
||||
: co2::AGGRESSOR_SIDE_NONE));
|
||||
}
|
||||
|
||||
// publish mbo
|
||||
@@ -522,16 +533,16 @@ void ExchangeSimApplication::update_md(std::string const &sec_desc) {
|
||||
// get the list of trades
|
||||
auto ob = matcher.get_order_book(nullptr);
|
||||
auto snap = mbo->allocate_snapshot();
|
||||
for (const auto& order : ob.order()) {
|
||||
for (const auto &order : ob.order()) {
|
||||
auto ek = exchange_key(order.clordid());
|
||||
md::SnapshotOrder sno {
|
||||
md::ExchangeOrderKey(std::atoll(ek.getString().c_str())),
|
||||
md::IntPrice_from_double<precision>(order.price() * price_factor),
|
||||
(int)order.open_quantity() };
|
||||
md::SnapshotOrder sno{
|
||||
md::ExchangeOrderKey(std::atoll(ek.getString().c_str())),
|
||||
md::IntPrice_from_double<precision>(order.price() *
|
||||
price_factor),
|
||||
(int)order.open_quantity()};
|
||||
if (order.side() == api::SIDE_BUY) {
|
||||
snap->bids.push_back(sno);
|
||||
}
|
||||
else if (order.side() == api::SIDE_SELL) {
|
||||
} else if (order.side() == api::SIDE_SELL) {
|
||||
snap->asks.push_back(sno);
|
||||
}
|
||||
}
|
||||
@@ -581,16 +592,18 @@ void ExchangeSimApplication::on_new_security(api::Security const &s) {
|
||||
}
|
||||
}
|
||||
|
||||
void ExchangeSimApplication::on_settlement(md::Security const& sec, md::IntPrice price) {
|
||||
void ExchangeSimApplication::on_settlement(md::Security const &sec,
|
||||
md::IntPrice price) {
|
||||
auto sec_desc = get_sim_sec_desc(sec);
|
||||
auto iter = me->md_producer_handles.find(sec_desc);
|
||||
if (iter != me->md_producer_handles.end()) {
|
||||
auto &handle = iter->second;
|
||||
handle.update_settlement(utils::TimeStamp::now(), price, datetime::today());
|
||||
handle.update_settlement(utils::TimeStamp::now(), price,
|
||||
datetime::today());
|
||||
}
|
||||
}
|
||||
|
||||
void ExchangeSimApplication::on_high(md::Security const& sec, md::Price price) {
|
||||
void ExchangeSimApplication::on_high(md::Security const &sec, md::Price price) {
|
||||
auto sec_desc = get_sim_sec_desc(sec);
|
||||
auto iter = me->md_producer_handles.find(sec_desc);
|
||||
if (iter != me->md_producer_handles.end()) {
|
||||
@@ -599,7 +612,7 @@ void ExchangeSimApplication::on_high(md::Security const& sec, md::Price price) {
|
||||
}
|
||||
}
|
||||
|
||||
void ExchangeSimApplication::on_low(md::Security const& sec, md::Price price) {
|
||||
void ExchangeSimApplication::on_low(md::Security const &sec, md::Price price) {
|
||||
auto sec_desc = get_sim_sec_desc(sec);
|
||||
auto iter = me->md_producer_handles.find(sec_desc);
|
||||
if (iter != me->md_producer_handles.end()) {
|
||||
@@ -608,7 +621,7 @@ void ExchangeSimApplication::on_low(md::Security const& sec, md::Price price) {
|
||||
}
|
||||
}
|
||||
|
||||
void ExchangeSimApplication::on_open(md::Security const& sec, md::Price price) {
|
||||
void ExchangeSimApplication::on_open(md::Security const &sec, md::Price price) {
|
||||
auto sec_desc = get_sim_sec_desc(sec);
|
||||
auto iter = me->md_producer_handles.find(sec_desc);
|
||||
if (iter != me->md_producer_handles.end()) {
|
||||
@@ -617,7 +630,8 @@ void ExchangeSimApplication::on_open(md::Security const& sec, md::Price price) {
|
||||
}
|
||||
}
|
||||
|
||||
void ExchangeSimApplication::on_close(md::Security const& sec, md::Price price) {
|
||||
void ExchangeSimApplication::on_close(md::Security const &sec,
|
||||
md::Price price) {
|
||||
auto sec_desc = get_sim_sec_desc(sec);
|
||||
auto iter = me->md_producer_handles.find(sec_desc);
|
||||
if (iter != me->md_producer_handles.end()) {
|
||||
@@ -627,20 +641,19 @@ void ExchangeSimApplication::on_close(md::Security const& sec, md::Price price)
|
||||
}
|
||||
|
||||
void ExchangeSimApplication::on_depth(
|
||||
md::Security const &sec,
|
||||
md::PriceSize bids[md::COCONUT_MAX_DEPTH],
|
||||
md::Security const &sec, md::PriceSize bids[md::COCONUT_MAX_DEPTH],
|
||||
md::PriceSize asks[md::COCONUT_MAX_DEPTH],
|
||||
std::array<md::PriceSize, md::COCONUT_MAX_IMPLIED_DEPTH>* implied_bids,
|
||||
std::array<md::PriceSize, md::COCONUT_MAX_IMPLIED_DEPTH>* implied_asks) {
|
||||
std::array<md::PriceSize, md::COCONUT_MAX_IMPLIED_DEPTH> *implied_bids,
|
||||
std::array<md::PriceSize, md::COCONUT_MAX_IMPLIED_DEPTH> *implied_asks) {
|
||||
|
||||
std::lock_guard<std::mutex> l(me->lock);
|
||||
|
||||
if (sec.has_synthetic())
|
||||
return ;
|
||||
return;
|
||||
|
||||
if (!me->do_merge_with_md)
|
||||
return;
|
||||
|
||||
return;
|
||||
|
||||
auto it = me->md_consumer_recs.find(sec.glbx().security_desc());
|
||||
bts_assert(it != me->md_consumer_recs.end());
|
||||
|
||||
@@ -664,13 +677,14 @@ void ExchangeSimApplication::on_depth(
|
||||
// we generate a set of orders that we apply to our order book
|
||||
merge_with_order_book(it->second.security.glbx().security_desc(),
|
||||
previous_md_book, current_md_book);
|
||||
|
||||
|
||||
// pass on implied market unchanged
|
||||
if (implied_bids || implied_asks) {
|
||||
auto iter = me->md_producer_handles.find(get_sim_sec_desc(sec));
|
||||
if (iter != me->md_producer_handles.end()) {
|
||||
auto &handle = iter->second;
|
||||
handle.update_implied(utils::TimeStamp::now(), *implied_bids, *implied_asks);
|
||||
handle.update_implied(utils::TimeStamp::now(), *implied_bids,
|
||||
*implied_asks);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -682,9 +696,9 @@ void ExchangeSimApplication::merge_with_order_book(std::string const &sec_desc,
|
||||
auto src_book = matcher.get_book();
|
||||
auto transform = compute_book_transform(src, dest);
|
||||
|
||||
LOG(ExSim, Diag, "Book transformation for {}\n source:\n {} dest:\n{}",
|
||||
sec_desc, src, dest);
|
||||
|
||||
LOG(ExSim, Diag, "Book transformation for {}\n source:\n {} dest:\n{}", sec_desc,src,dest);
|
||||
|
||||
for (auto const &op : transform) {
|
||||
|
||||
LOG(ExSim, Diag, "Adding synthetic book operation for {}. {} {} {}@{}",
|
||||
|
||||
@@ -80,7 +80,7 @@ private:
|
||||
// Order functionality
|
||||
void processOrder( const Order& );
|
||||
// void processCancel( const std::string& id, const std::string& symbol, Order::Side );
|
||||
void updateOrder( const Order&, char status );
|
||||
void send_execution_report( const Order&, char status );
|
||||
void rejectOrder( const Order& order );
|
||||
// void acceptOrder( const Order& order );
|
||||
void fillOrder( const Order& order );
|
||||
|
||||
@@ -109,7 +109,8 @@ void OrderMatcher::update(std::string const &clordid, Price price, Quantity qty,
|
||||
|
||||
if (order_cpy.quantity() != qty.value()) {
|
||||
order_cpy.set_quantity(qty.value());
|
||||
order_cpy.set_open_quantity(qty.value());
|
||||
bts_assert(qty.value() > order_cpy.executed_quantity());
|
||||
order_cpy.set_open_quantity(qty.value()-order_cpy.executed_quantity());
|
||||
}
|
||||
|
||||
if (order->side() == api::SIDE_BUY) {
|
||||
|
||||
Reference in new issue
Block a user