Refactored sim types to have a sim prefix.

This commit is contained in:
Pedro Pinto committed 2021-02-01 13:57:16 -05:00
1 parent 2cffaa9d73
commit aeb4bec02d
27 files changed
+189 -109

No files matched your search

@@ -31,7 +31,7 @@ struct PolyObserver::Opportunity {
struct PolyObserver::PImpl {
std::vector<std::unique_ptr<Target>> targets;
std::optional<Opportunity> current_opportunity;
std::unordered_map<SecurityID, MBPUpdate> books;
std::unordered_map<SecurityID, SimBookUpdate> books;
EventHeader last_event;
ISim *sim = nullptr;
};
@@ -60,7 +60,7 @@ void PolyObserver::finit() {}
void PolyObserver::on_replay_start(
EventHeader const &current_event,
std::vector<MBPUpdate> const &current_books) {
std::vector<SimBookUpdate> const &current_books) {
me->sim->log("replay_start", current_event.event_id.first);
me->books.clear();
for (auto const &b : current_books)
@@ -165,11 +165,11 @@ void PolyObserver::analyze() {
}
}
void PolyObserver::on_order_update(EventHeader const &, OrderUpdate const &) {
void PolyObserver::on_order_update(EventHeader const &, SimOrderUpdate const &) {
bts_assert(false);
}
void PolyObserver::on_mbp(EventHeader const &h, MBPUpdate const &mbp) {
void PolyObserver::on_mbp(EventHeader const &h, SimBookUpdate const &mbp) {
me->last_event = h;
me->books[mbp.security_id] = mbp;
eval();
@@ -15,9 +15,9 @@ class PolyObserver : public IAlgoModel {
void init(ISim &, protos::SimRunOptions const &) override;
void finit() override;
void on_replay_start(
EventHeader const &, std::vector<MBPUpdate> const &) override;
void on_order_update(EventHeader const &, OrderUpdate const &) override;
void on_mbp(EventHeader const &, MBPUpdate const &) override;
EventHeader const &, std::vector<SimBookUpdate> const &) override;
void on_order_update(EventHeader const &, SimOrderUpdate const &) override;
void on_mbp(EventHeader const &, SimBookUpdate const &) override;
void on_time() override;
private:
@@ -31,7 +31,7 @@ struct SniperModel::Target {
struct SniperModel::WorkingOrder {
SmallOrderChange order;
std::vector<OrderUpdate> fills;
std::vector<SimOrderUpdate> fills;
};
struct SniperModel::Shot {
@@ -47,7 +47,7 @@ struct SniperModel::Shot {
struct SniperModel::DependentTargets {
SecurityDefinition definition;
MBPUpdate book;
SimBookUpdate book;
std::vector<Target *> dependencies;
};
@@ -86,7 +86,7 @@ void SniperModel::init(ISim &s, protos::SimRunOptions const &options) {
}
void SniperModel::on_replay_start(
EventHeader const &h, std::vector<MBPUpdate> const &books) {
EventHeader const &h, std::vector<SimBookUpdate> const &books) {
auto parse_poly_filter = [](std::string const &pf) {
std::vector<std::string> polys;
boost::split(polys, pf, boost::is_any_of(","));
@@ -158,7 +158,7 @@ void SniperModel::on_replay_start(
}
}
void SniperModel::on_mbp(EventHeader const &h, const MBPUpdate &mbp) {
void SniperModel::on_mbp(EventHeader const &h, const SimBookUpdate &mbp) {
me->current_event = h;
auto it = me->books.find(mbp.security_id);
if (it == me->books.end())
@@ -188,7 +188,7 @@ void SniperModel::on_mbp(EventHeader const &h, const MBPUpdate &mbp) {
}
void SniperModel::on_order_update(
EventHeader const &h, const OrderUpdate &update) {
EventHeader const &h, const SimOrderUpdate &update) {
me->current_event = h;
bts_assert(me->state == State::SHOOTING);
bts_assert(me->current_shot);
@@ -202,9 +202,9 @@ void SniperModel::on_order_update(
for (auto const &p : in_flight.working) {
auto &these_fills = p.second.fills;
for (auto &f : these_fills) {
clean_fills = clean_fills && (f.fill_type == OrderUpdate::AGGRESSIVE);
if (f.status == OrderUpdate::CANCELED ||
f.status == OrderUpdate::FILLED) {
clean_fills = clean_fills && (f.fill_type == SimOrderUpdate::AGGRESSIVE);
if (f.status == SimOrderUpdate::CANCELED ||
f.status == SimOrderUpdate::FILLED) {
pending_orders--;
break;
}
@@ -212,7 +212,7 @@ void SniperModel::on_order_update(
}
if (pending_orders == 0) {
std::vector<OrderUpdate> all_fills;
std::vector<SimOrderUpdate> all_fills;
for (auto const &p : in_flight.working) {
auto &these_fills = p.second.fills;
all_fills.insert(
@@ -292,7 +292,7 @@ void SniperModel::maybe_force_fills() {
auto &these_fills = p.second.fills;
auto total_fills = 0;
for (auto &f : these_fills) {
bts_assert(f.status != OrderUpdate::CANCELED);
bts_assert(f.status != SimOrderUpdate::CANCELED);
total_fills += f.last_fills.value();
}
if (order.order.quantity.value() > total_fills) {
@@ -19,9 +19,9 @@ class SniperModel : public IAlgoModel {
void init(ISim &, protos::SimRunOptions const &) override;
protos::SimRunOptions fill_defaults(protos::SimRunOptions const &) override;
void on_replay_start(
EventHeader const &, std::vector<MBPUpdate> const &) override;
void on_mbp(EventHeader const &, MBPUpdate const &) override;
void on_order_update(EventHeader const &, OrderUpdate const &) override;
EventHeader const &, std::vector<SimBookUpdate> const &) override;
void on_mbp(EventHeader const &, SimBookUpdate const &) override;
void on_order_update(EventHeader const &, SimOrderUpdate const &) override;
void on_time() override;
private:
@@ -188,10 +188,10 @@ Quantity FifoImpliedSecurityMatcher::fill(
for (auto it = effects->begin() + leg_fills_pos; it != effects->end();
it++) {
auto &passive = *it;
bts_assert(std::holds_alternative<OrderUpdate>(passive));
bts_assert(std::holds_alternative<SimOrderUpdate>(passive));
fills.add_fill(
kv.first, key,
_to_passive_fill(std::get<OrderUpdate>(passive), true));
_to_passive_fill(std::get<SimOrderUpdate>(passive), true));
}
// the implied matchers should not produce partial leg fills (modulus
// leg_size should be 0)
@@ -210,12 +210,12 @@ Quantity FifoImpliedSecurityMatcher::fill(
for (auto it = effects->begin() + leg_fills_pos; it != effects->end();
it++) {
auto &f = *it;
bts_assert(std::holds_alternative<OrderUpdate>(f));
bts_assert(std::holds_alternative<SimOrderUpdate>(f));
MDP3Fill aggr;
aggr.key = key;
aggr.aggressor = _to_aggressor(side);
aggr.price = impl_fill_price;
aggr.quantity = std::get<OrderUpdate>(f).last_fills / abs(leg_size);
aggr.quantity = std::get<SimOrderUpdate>(f).last_fills / abs(leg_size);
fills.add_fill(security_id(), key, aggr);
fill_qty += aggr.quantity.value();
}
@@ -98,8 +98,8 @@ struct FifoMatcherContext {
}
matcher->matcher()->reset(sorted_orders.begin(), sorted_orders.end());
if (effects != nullptr) {
effects->emplace_back(MBPUpdate());
MBPUpdate &mbp = std::get<MBPUpdate>(effects->back());
effects->emplace_back(SimBookUpdate());
SimBookUpdate &mbp = std::get<SimBookUpdate>(effects->back());
mbp.security_id = sec_id;
mbp.mbp_buy = matcher->book(Side::Buy);
mbp.mbp_sell = matcher->book(Side::Sell);
@@ -369,8 +369,8 @@ Quantity FifoMergedSecurityMatcher::_fill(
for (auto it = effects->begin() + fills_pos; it != effects->end();
it++) {
auto &df = *it;
bts_assert(std::holds_alternative<OrderUpdate>(df));
auto &upd = std::get<OrderUpdate>(df);
bts_assert(std::holds_alternative<SimOrderUpdate>(df));
auto &upd = std::get<SimOrderUpdate>(df);
if (upd.key == key) {
if (aggr_fill.price.is_nan()) {
aggr_fill.price = upd.last_price;
@@ -70,29 +70,29 @@ FifoPriceLevelMatcher::_one_pass_cross_order(
auto [o_rem, cross_rem, filled] = o.cross(cross_ord_qty.value());
cross_ord_qty = Quantity(cross_rem);
if (res != nullptr) {
OrderUpdate passive_upd = {
SimOrderUpdate passive_upd = {
o.key,
o.total_qty().value() > 0
? OrderUpdate::Status::PARTIALLY_FILLED
: OrderUpdate::Status::FILLED,
? SimOrderUpdate::Status::PARTIALLY_FILLED
: SimOrderUpdate::Status::FILLED,
_sec,
_side,
Quantity(filled),
_price,
OrderUpdate::FillType::PASSIVE};
SimOrderUpdate::FillType::PASSIVE};
MatcherUtils::validate_outgoing_update(passive_upd);
res->push_back(passive_upd);
if (add_agg_side_to_match_effect) {
OrderUpdate agg_upd = {
SimOrderUpdate agg_upd = {
cross_ord_key,
cross_ord_qty.value() > 0
? OrderUpdate::Status::PARTIALLY_FILLED
: OrderUpdate::Status::FILLED,
? SimOrderUpdate::Status::PARTIALLY_FILLED
: SimOrderUpdate::Status::FILLED,
_sec,
other_side(_side),
Quantity(filled),
_price,
OrderUpdate::FillType::AGGRESSIVE};
SimOrderUpdate::FillType::AGGRESSIVE};
MatcherUtils::validate_outgoing_update(agg_upd);
res->push_back(agg_upd);
}
@@ -253,9 +253,9 @@ void FifoPriceLevelMatcher::on_order_update(
void FifoPriceLevelMatcher::on_order_cancel(
MatcherOrder &order, std::vector<MatchEffect> *res) {
_logically_delete(order);
OrderUpdate o = {
SimOrderUpdate o = {
order.key,
OrderUpdate::Status::CANCELED,
SimOrderUpdate::Status::CANCELED,
_sec,
_side,
Quantity(0),
@@ -228,12 +228,12 @@ void FifoSecurityMatcher::_on_order_not_affecting_books(
auto fill_size = level_qty - new_level_qty_ret;
new_ord_rem = new_ord_rem_ret;
if (res != nullptr)
res->push_back(OrderUpdate{
res->push_back(SimOrderUpdate{
ord.key,
new_ord_rem > 0 ? OrderUpdate::Status::PARTIALLY_FILLED
: OrderUpdate::Status::FILLED,
new_ord_rem > 0 ? SimOrderUpdate::Status::PARTIALLY_FILLED
: SimOrderUpdate::Status::FILLED,
ord.security, ord.side, Quantity(fill_size), pl.price(),
OrderUpdate::FillType::AGGRESSIVE});
SimOrderUpdate::FillType::AGGRESSIVE});
} else
break;
if (new_ord_rem == 0)
@@ -243,8 +243,8 @@ void FifoSecurityMatcher::_on_order_not_affecting_books(
}
case OrderChange::removed:
if (res != nullptr)
res->push_back(OrderUpdate{
ord.key, OrderUpdate::Status::CANCELED, ord.security, ord.side,
res->push_back(SimOrderUpdate{
ord.key, SimOrderUpdate::Status::CANCELED, ord.security, ord.side,
Quantity(0), Price(), std::nullopt});
// nothing to do since this order was never made a part of the book
break;
@@ -159,15 +159,15 @@ void MergedMatcher::match(
// log/validate order updates
if (res != nullptr) {
for (auto it = res->begin() + effects_origin; it != res->end(); it++)
_trace.log_outgoing_update(std::get<OrderUpdate>(*it));
_trace.log_outgoing_update(std::get<SimOrderUpdate>(*it));
for (auto it = res->begin() + effects_origin; it != res->end(); it++)
MatcherUtils::validate_outgoing_update(std::get<OrderUpdate>(*it));
MatcherUtils::validate_outgoing_update(std::get<SimOrderUpdate>(*it));
// add mbp updates
for (auto &ass_kv : mbps) {
for (auto &sec_kv : ass_kv.second) {
auto new_mbp = sec_kv.second.get_new_mbp();
if (new_mbp.has_value()) {
res->emplace_back(MBPUpdate{
res->emplace_back(SimBookUpdate{
sec_kv.first, new_mbp.value().bids, new_mbp.value().asks,
new_mbp.value().ibids, new_mbp.value().iasks});
}
@@ -235,13 +235,13 @@ void DirectMatcher::match(
dirty.emplace_back(c.order.security, &it->second);
}
for (const auto &u : *res)
_trace.log_outgoing_update(std::get<OrderUpdate>(u));
_trace.log_outgoing_update(std::get<SimOrderUpdate>(u));
for (const auto &u : *res)
MatcherUtils::validate_outgoing_update(std::get<OrderUpdate>(u));
MatcherUtils::validate_outgoing_update(std::get<SimOrderUpdate>(u));
// all order activity is not reflecting in the
// books
for (auto cm : dirty)
res->emplace_back(MBPUpdate{
res->emplace_back(SimBookUpdate{
cm.first, cm.second->book(Side::Buy),
cm.second->book(Side::Sell), ImpliedBookSide(), ImpliedBookSide()});
auto now = TimeUtil::GetCurrentTime();
@@ -32,7 +32,7 @@ class MergedMatcher {
MergedMatcher(MergedMatcher &&);
MergedMatcher &operator=(MergedMatcher &&) { return *this; }
using MatchEffect = std::variant<MBPUpdate, OrderUpdate>;
using MatchEffect = std::variant<SimBookUpdate, SimOrderUpdate>;
enum ChangeSrc { ALGO, SIMULATOR_INPUT };
@@ -77,7 +77,7 @@ class DirectMatcher {
DirectMatcher(DirectMatcher &&);
DirectMatcher &operator=(DirectMatcher &&) { return *this; }
using MatchEffect = std::variant<MBPUpdate, OrderUpdate>;
using MatchEffect = std::variant<SimBookUpdate, SimOrderUpdate>;
enum ChangeSrc { ALGO, SIMULATOR_INPUT };
@@ -31,7 +31,7 @@ struct Trace {
c.change, c.order.key, c.order.side, c.order.price,
c.order.quantity, c.order.iceberg_quantity);
}
void log_outgoing_update(const OrderUpdate &u) {
void log_outgoing_update(const SimOrderUpdate &u) {
if (traced_orders.find(u.key) != traced_orders.end())
LOG(OrderMan, Diag,
"Matcher outgoing update:{} ok:{} side:{} last_fp:{} last_fq:{}",
@@ -60,11 +60,11 @@ class MatcherUtils {
if (c.change == OrderChange::ChangeType::added)
bts_assert(c.order.quantity.value() > 0);
}
static void validate_outgoing_update(const OrderUpdate &u) {
if (u.status == OrderUpdate::Status::PARTIALLY_FILLED ||
u.status == OrderUpdate::Status::FILLED)
static void validate_outgoing_update(const SimOrderUpdate &u) {
if (u.status == SimOrderUpdate::Status::PARTIALLY_FILLED ||
u.status == SimOrderUpdate::Status::FILLED)
bts_assert(u.last_fills.value() > 0);
if (u.status == OrderUpdate::Status::CANCELED)
if (u.status == SimOrderUpdate::Status::CANCELED)
bts_assert(u.last_fills.value() == 0);
}
};
@@ -14,7 +14,7 @@ inline MDP3Fill::Aggressor _to_aggressor(Side s) {
}
inline MDP3Fill
_to_passive_fill(const OrderUpdate &upd, bool implied_aggressor) {
_to_passive_fill(const SimOrderUpdate&upd, bool implied_aggressor) {
MDP3Fill passive;
passive.key = upd.key;
passive.price = upd.last_price;
@@ -25,7 +25,7 @@ _to_passive_fill(const OrderUpdate &upd, bool implied_aggressor) {
}
inline MDP3Fill
_to_aggressor_fill(OrderKey k, Side side, const OrderUpdate &upd) {
_to_aggressor_fill(OrderKey k, Side side, const SimOrderUpdate &upd) {
MDP3Fill aggr;
aggr.key = k;
aggr.price = upd.last_price;
@@ -712,7 +712,7 @@ TEST_CASE("perf", "[matcher] [spartan]") {
bts_assert(exec_rep.size() == (i + 1) * 2);
auto l_it = exec_rep.end() - 1;
MatchEffect &eff = *l_it;
auto &upd = std::get<OrderUpdate>(eff);
auto &upd = std::get<SimOrderUpdate>(eff);
REQUIRE(upd.last_fills.value() == 1);
}
auto t1 = TimeUtil::GetCurrentTime();
@@ -7,8 +7,8 @@
namespace bts::spartan::mdp3 {
struct EventHeader;
struct MBPUpdate;
struct OrderUpdate;
struct SimBookUpdate;
struct SimOrderUpdate;
} // namespace bts::spartan::mdp3
namespace bts::spartan::mdp3 {
@@ -25,9 +25,9 @@ class IAlgoModel {
virtual void init(ISim &, protos::SimRunOptions const &) = 0;
virtual void finit() {}
virtual void
on_replay_start(EventHeader const &, std::vector<MBPUpdate> const &) {}
virtual void on_order_update(EventHeader const &, OrderUpdate const &) {}
virtual void on_mbp(EventHeader const &, MBPUpdate const &) {}
on_replay_start(EventHeader const &, std::vector<SimBookUpdate> const &) {}
virtual void on_order_update(EventHeader const &, SimOrderUpdate const &) {}
virtual void on_mbp(EventHeader const &, SimBookUpdate const &) {}
virtual void on_time() {}
};
@@ -65,14 +65,14 @@ struct Sim::AlgoCBFilter : IAlgoModel {
void finit() override { real_algo.finit(); }
void on_replay_start(
EventHeader const &h, std::vector<MBPUpdate> const &b) override {
EventHeader const &h, std::vector<SimBookUpdate> const &b) override {
real_algo.on_replay_start(h, b);
}
void on_order_update(EventHeader const &h, OrderUpdate const &o) override {
void on_order_update(EventHeader const &h, SimOrderUpdate const &o) override {
if (!owner.is_stopping_sim())
real_algo.on_order_update(h, o);
}
void on_mbp(EventHeader const &h, MBPUpdate const &mbp) override {
void on_mbp(EventHeader const &h, SimBookUpdate const &mbp) override {
if (!owner.is_stopping_sim())
real_algo.on_mbp(h, mbp);
}
@@ -98,7 +98,7 @@ struct Sim::PImpl {
std::optional<Simulation> simulation = {};
OrderKey last_order_key =
OrderKey{std::numeric_limits<OrderKey::Value>::max()};
std::vector<OrderUpdate> my_fills = {};
std::vector<SimOrderUpdate> my_fills = {};
PendingSimOP pending_sim_op = PendingSimOP::NONE;
TimeStamp next_time_event = TimeStamp{0};
std::queue<AlgoAction> pending_algo_actions = {};
@@ -265,7 +265,7 @@ void Sim::on_pcap_event_during_replay() {
for (auto &snap : ev.affected_securities) {
if (snap.ready_to_trade()) {
MBPUpdate mbp{
SimBookUpdate mbp{
snap.security_id(),
snap.book_side(Side::Buy),
snap.book_side(Side::Sell),
@@ -397,7 +397,7 @@ void Sim::_start_simulation() {
for (auto &ef : effects)
std::visit(
overloaded{
[&](MBPUpdate &mbp) {
[&](SimBookUpdate &mbp) {
sim_log(
"sim_book_init",
get_security(mbp.security_id).symbol(),
@@ -406,7 +406,7 @@ void Sim::_start_simulation() {
mbp.mbp_sell[0].price.to_double(),
mbp.mbp_sell[0].quantity.value());
},
[&](OrderUpdate const &) { bts_assert(false); }},
[&](SimOrderUpdate const &) { bts_assert(false); }},
ef);
}
}
@@ -425,10 +425,10 @@ void Sim::_stop_simulation() {
}
void Sim::on_algo_replay_start() {
std::vector<MBPUpdate> collected_books;
std::vector<SimBookUpdate> collected_books;
for (auto &[id, snap] : me->snapshots)
if (snap.ready_to_trade()) {
collected_books.emplace_back(MBPUpdate{
collected_books.emplace_back(SimBookUpdate{
id, snap.book_side(Side::Buy), snap.book_side(Side::Sell),
snap.implied_book_side(Side::Buy),
snap.implied_book_side(Side::Sell)});
@@ -456,10 +456,10 @@ void Sim::match_and_send_effects(
std::vector<MergedMatcher::MatchEffect> sorted_effects;
sorted_effects.reserve(effects.size());
for (auto const &e : effects)
if (std::holds_alternative<MBPUpdate>(e))
if (std::holds_alternative<SimBookUpdate>(e))
sorted_effects.push_back(e);
for (auto const &e : effects)
if (std::holds_alternative<OrderUpdate>(e))
if (std::holds_alternative<SimOrderUpdate>(e))
sorted_effects.push_back(e);
EventHeader header{me->event_id, transact_time, me->time};
@@ -467,7 +467,7 @@ void Sim::match_and_send_effects(
for (auto &ef : sorted_effects)
std::visit(
overloaded{
[&](MBPUpdate const &mbp) {
[&](SimBookUpdate const &mbp) {
if (me->options.log_all_transactions())
sim_log(
"sim_book", get_security(mbp.security_id).symbol(),
@@ -477,7 +477,7 @@ void Sim::match_and_send_effects(
mbp.mbp_sell[0].quantity.value());
me->algo.on_mbp(header, mbp);
},
[&](OrderUpdate const &order_update) {
[&](SimOrderUpdate const &order_update) {
if (order_update.key.value() >= me->last_order_key.value()) {
sim_log(
"algo_fill", order_update.status_str(),
@@ -12,7 +12,7 @@ struct PandL::PImpl {
PandL::PandL(SecurityMap const &m, Pricer const &p) : me(new PImpl{m, p}) {}
PandL::~PandL() {}
double PandL::net_cash(std::vector<OrderUpdate> const &fills) const {
double PandL::net_cash(std::vector<SimOrderUpdate> const &fills) const {
double net_cash_flow = 0;
for (auto const &f : fills) {
auto cash = price_to_cash(
@@ -27,7 +27,7 @@ double PandL::net_cash(std::vector<OrderUpdate> const &fills) const {
// Trading firm
double PandL::commission_cost(
CommissionModel m, std::vector<OrderUpdate> const &fills) const {
CommissionModel m, std::vector<SimOrderUpdate> const &fills) const {
auto outright_commission = 1.23;
auto spread_comission = 0.86;
@@ -55,8 +55,8 @@ double PandL::commission_cost(
} // namespace bts::spartan::mdp3
PandL::SymbolNetCash
PandL::symbol_net_cash(std::vector<OrderUpdate> const &fills) const {
std::unordered_map<std::string, std::vector<OrderUpdate>> fills_per_symbol;
PandL::symbol_net_cash(std::vector<SimOrderUpdate> const &fills) const {
std::unordered_map<std::string, std::vector<SimOrderUpdate>> fills_per_symbol;
for (auto const &f : fills)
fills_per_symbol[get_sec(f.security_id).asset()].push_back(f);
@@ -68,7 +68,7 @@ PandL::symbol_net_cash(std::vector<OrderUpdate> const &fills) const {
}
PandL::OutrightPosition
PandL::outright_position(std::vector<OrderUpdate> const &fills) const {
PandL::outright_position(std::vector<SimOrderUpdate> const &fills) const {
OutrightPosition result;
auto add_outright_pos = [&](auto sec_id, int64_t change) {
auto &this_pos = result[sec_id];
@@ -10,7 +10,7 @@
#include <vector>
namespace bts::spartan::mdp3 {
struct OrderUpdate;
struct SimOrderUpdate;
}
namespace bts::spartan::mdp3 {
@@ -25,12 +25,12 @@ class PandL {
Pricer const & = [](auto, auto, auto) { return os::nan(); });
~PandL();
double net_cash(std::vector<OrderUpdate> const &fills) const;
SymbolNetCash symbol_net_cash(std::vector<OrderUpdate> const &fills) const;
double net_cash(std::vector<SimOrderUpdate> const &fills) const;
SymbolNetCash symbol_net_cash(std::vector<SimOrderUpdate> const &fills) const;
OutrightPosition
outright_position(std::vector<OrderUpdate> const &fills) const;
outright_position(std::vector<SimOrderUpdate> const &fills) const;
double position_liquidation_value(OutrightPosition const &) const;
double commission_cost(CommissionModel m,std::vector<OrderUpdate> const &fills) const;
double commission_cost(CommissionModel m,std::vector<SimOrderUpdate> const &fills) const;
private:
struct PImpl;
@@ -4,6 +4,7 @@
namespace bts::spartan::mdp3 {
class IAlgoModel;
using AlgoEventID = std::pair<size_t, size_t>;
@@ -19,7 +20,7 @@ struct AlgoAction {
SmallOrderChange change;
};
struct MBPUpdate {
struct SimBookUpdate {
SecurityID security_id;
BookSide mbp_buy;
BookSide mbp_sell;
@@ -33,7 +34,7 @@ struct MBPUpdate {
}
};
struct OrderUpdate {
struct SimOrderUpdate {
OrderKey key;
enum Status { FILLED, PARTIALLY_FILLED, CANCELED };
Status status;
@@ -59,23 +60,23 @@ struct OrderUpdate {
}
};
using MatchEffect = std::variant<MBPUpdate, OrderUpdate>;
using MatchEffect = std::variant<SimBookUpdate, SimOrderUpdate>;
struct AlgoNotification {
EventHeader header;
std::variant<MBPUpdate, OrderUpdate> event;
std::variant<SimBookUpdate, SimOrderUpdate> event;
};
inline std::ostream &
operator<<(std::ostream &ostr, const OrderUpdate::Status &status) {
operator<<(std::ostream &ostr, const SimOrderUpdate::Status &status) {
switch (status) {
case OrderUpdate::Status::FILLED:
case SimOrderUpdate::Status::FILLED:
ostr << "FILLED";
break;
case OrderUpdate::Status::PARTIALLY_FILLED:
case SimOrderUpdate::Status::PARTIALLY_FILLED:
ostr << "PARTIALLY_FILLED";
break;
case OrderUpdate::Status::CANCELED:
case SimOrderUpdate::Status::CANCELED:
return ostr << "CANCELED";
default:
ostr << "UNKNOWN_" << static_cast<int>(status);
@@ -84,7 +85,7 @@ operator<<(std::ostream &ostr, const OrderUpdate::Status &status) {
return ostr;
}
inline std::ostream &operator<<(std::ostream &ostr, const OrderUpdate &upd) {
inline std::ostream &operator<<(std::ostream &ostr, const SimOrderUpdate &upd) {
ostr << "key: " << upd.key.value() << ", status: " << upd.status
<< ", last fills: " << upd.last_fills.value()
<< ", last price: " << upd.last_price.to_double();
@@ -2,8 +2,8 @@
** Templates
- [[ftp://ftp.cmegroup.com/SBEFix/][Templates and Config FTP Site]]
* iLink3
** Message [[https://www.cmegroup.com/confluence/display/EPICSANDBOX/iLink+3+Binary+Order+Entry+-+Session+Layer#iLink3BinaryOrderEntrySessionLayer-Transferring][Retransmission]]
** CME Limits
** Message [[https://www.cmegroup.com/confluence/display/EPICSANDBOX/iLink+3+Binary+Order+Entry+-+Session+Layer#iLink3BinaryOrderEntrySessionLayer-Transferring][Re-transmission]]
** Limits
**** Automated Port Closure
CME Globex will automatically terminate and close both the primary and backup ports for any iLink 3 session on the 5th invalid negotiate and establish attempts in 60 seconds.
**** Application Messaging Thresholds
@@ -46,10 +46,18 @@ CME Globex will automatically terminate and close both the primary and backup po
- configuration:
- get /MSGW/[NRCert|Cert|Production]/Configuration/MSGW_Config.xml
** [[https://www.cmegroup.com/confluence/display/EPICSANDBOX/iLink+3+Binary+Order+Entry+-+Session+Layer][Session Protocol]]
** Pre-registration of party details
** Preregistration of party details
- CME allows the registration of bundles of order properties so that orders can simply refer to a registration id rather than include every property on every order.
- This is achieved through a separate iLink3 server, the [[https://www.cmegroup.com/confluence/display/EPICSANDBOX/iLink+3+Binary+Order+Entry+-+Business+Layer#iLink3BinaryOrderEntryBusinessLayer-OrderEntryServiceGateway][Order Entry Service Gateway]]
- This gateway uses the same iLink3s session, meaning the same UUID and seq numbers as the main gateway
** [[https://www.cmegroup.com/confluence/display/EPICSANDBOX/iLink+Reject+Codes][Error codes]]
** [[https://www.cmegroup.com/confluence/display/EPICSANDBOX/iLink+Order+Cancel-Replace+and+In-Flight+Mitigation][In-Flight mitigation]]
** Messages
- [[https://www.cmegroup.com/confluence/display/EPICSANDBOX/iLink+3+New+Order+-+Single][NewOrderSingle]]
- [[https://www.cmegroup.com/confluence/display/EPICSANDBOX/iLink+3+Order+Cancel+Replace+Request][CancelReplace]]
- [[https://www.cmegroup.com/confluence/display/EPICSANDBOX/iLink+3+Order+Cancel+Request][Cancel]]
- [[https://www.cmegroup.com/confluence/display/EPICSANDBOX/iLink+3+Execution+Report+-+Trade+Outright][ExecutionReport (Outright)]]
- [[https://www.cmegroup.com/confluence/display/EPICSANDBOX/iLink+3+Execution+Report+-+Trade+Spread][ExecutionReport (Spread)]]
* SBE
** Generators
- Source be found on [[https://github.com/real-logic/simple-binary-encoding][RealLogic's GitHub]]
@@ -1,10 +1,18 @@
#pragma once
#include "execution/OrderHandle.h"
namespace bts::spartan {
class ExecutionReport {
OrderHandle h;
enum class OrderStatus {
WORKING,
CANCELED,
FILLED,
PARTIALLY_FILLED,
};
};
}
@@ -5,6 +5,8 @@
namespace bts::spartan {
const char *translate_business_reject_reason(int reason) {
switch (reason) {
case 0:
@@ -372,7 +374,25 @@ GatewayError::il3_unexpected_terminate(std::string const &reason_txt, int code)
return {
UNEXPECTED_TERMINATE,
fmt::format(
"Reason: {} Text: {}", reason_txt, translate_terminate(code), code)};
"Reason: {} Text: {} Error: {} ", reason_txt, translate_terminate(code), code)};
}
GatewayError GatewayError::il3_cancel_reject(std::string const&text, int reason) {
if (reason == 2045)
return {ATTEMPT_TO_CANCEL_UNKNOWN_ORDER,
fmt::format("Attempt to cancel unknown order. Text: {} Error: {}", text, reason)};
else
return {CANCEL_REJECT,
fmt::format("Cancel reject. Text: {} Error: {}", text, reason)};
}
GatewayError GatewayError::il3_cancel_replace_reject(std::string const&text, int reason) {
if (reason == 2045)
return {ATTEMPT_TO_CANCEL_REPLACE_UNKNOWN_ORDER,
fmt::format("Attempt to cancel replace unknown order. Text: {} Error: {}", text, reason)};
else
return {CANCEL_REPLACE_REJECT,
fmt::format("Cancel reject. Text: {} Error: {}", text, reason)};
}
std::string error_code_name(GatewayError::ErrorCode c) {
@@ -397,6 +417,14 @@ std::string error_code_name(GatewayError::ErrorCode c) {
return "RETRANSMIT_FAILURE";
case GatewayError::TRADE_CORRECTION:
return "TRADE_CORRECTION";
case GatewayError::ATTEMPT_TO_CANCEL_UNKNOWN_ORDER:
return "ATTEMPT_TO_CANCEL_UNKNOWN_ORDER";
case GatewayError::ATTEMPT_TO_CANCEL_REPLACE_UNKNOWN_ORDER:
return "ATTEMPT_TO_CANCEL_REPLACE_UNKNOWN_ORDER";
case GatewayError::CANCEL_REJECT:
return "CANCEL_REJECT";
case GatewayError::CANCEL_REPLACE_REJECT:
return "CANCEL_REPLACE_REJECT";
}
bts_assert(false);
return {};
@@ -16,9 +16,15 @@ class GatewayError {
UNEXPECTED_TERMINATE,
UNEXPECTED_DISCONNECT,
RETRANSMIT_FAILURE,
TRADE_CORRECTION
TRADE_CORRECTION,
ATTEMPT_TO_CANCEL_UNKNOWN_ORDER,
ATTEMPT_TO_CANCEL_REPLACE_UNKNOWN_ORDER,
CANCEL_REJECT,
CANCEL_REPLACE_REJECT
};
static GatewayError il3_cancel_reject(std::string const&text, int reason);
static GatewayError il3_cancel_replace_reject(std::string const&text, int reason);
static GatewayError il3_bussiness_reject(int reason);
static GatewayError il3_negotiation_reject(int reason);
static GatewayError il3_establish_reject(int reason);
@@ -0,0 +1,13 @@
#pragma once
namespace bts::spartan {
class OrderUpdate;
class IOrderObserver {
public:
virtual ~IOrderObserver() {}
virtual void on_order_update(OrderUpdate const &) = 0;
};
}
@@ -172,6 +172,7 @@ void RealTimeHandler::queue(OrderHandle h, CancelReplace const &cr) {
m.ordType(::il3::OrderTypeReq::Limit);
m.timeInForce(::il3::TimeInForce::Day);
m.manualOrderIndicator(::il3::ManualOrdIndReq::Automated);
m.oFMOverride(::il3::OFMOverrideReq::Enabled);
m.execInst().clear();
m.executionMode(::il3::ExecMode::NULL_VALUE);
m.liquidityFlag(::il3::BooleanNULL::NULL_VALUE);
@@ -237,12 +238,26 @@ void RealTimeHandler::operator()(::il3::ExecutionReportReject523) {}
void RealTimeHandler::operator()(::il3::ExecutionReportElimination524) {}
void RealTimeHandler::operator()(::il3::ExecutionReportTradeOutright525) {}
void RealTimeHandler::operator()(::il3::ExecutionReportTradeSpread526) {}
void RealTimeHandler::operator()(::il3::ExecutionReportTradeSpreadLeg527) {}
void RealTimeHandler::operator()(::il3::ExecutionReportModify531) {}
void RealTimeHandler::operator()(::il3::ExecutionReportStatus532) {}
void RealTimeHandler::operator()(::il3::ExecutionReportCancel534) {}
void RealTimeHandler::operator()(::il3::OrderCancelReject535) {}
void RealTimeHandler::operator()(::il3::OrderCancelReplaceReject536) {}
// Ignore these
void RealTimeHandler::operator()(::il3::ExecutionReportTradeSpreadLeg527) {}
// These should never be received
void RealTimeHandler::operator()(::il3::ExecutionReportStatus532) {}
void RealTimeHandler::operator()(::il3::OrderCancelReject535 m) {
auto error = GatewayError::il3_cancel_reject(m.getTextAsString(),
m.cxlRejReason());
me->obs.on_error(error);
}
void RealTimeHandler::operator()(::il3::OrderCancelReplaceReject536 m) {
auto error = GatewayError::il3_cancel_replace_reject(m.getTextAsString(),
m.cxlRejReason());
me->obs.on_error(error);
}
void RealTimeHandler::operator()(
::il3::ExecutionReportTradeAddendumOutright548) {
@@ -41,6 +41,7 @@ class RealTimeHandler {
void operator()(::il3::Sequence506);
void operator()(::il3::Terminate507);
void operator()(::il3::BusinessReject521);
void operator()(::il3::ExecutionReportNew522);
void operator()(::il3::ExecutionReportReject523);
void operator()(::il3::ExecutionReportElimination524);
@@ -93,8 +93,8 @@ inline void _process_changes(
processed_secs.insert(s->security_id());
}
for (const auto &eff : effects) {
if (std::holds_alternative<MBPUpdate>(eff)) {
const auto &up = std::get<MBPUpdate>(eff);
if (std::holds_alternative<SimBookUpdate>(eff)) {
const auto &up = std::get<SimBookUpdate>(eff);
_write_row(out, event_id, up.security_id, up.mbp_buy, up.mbp_sell);
}
}