Orderman2

This commit is contained in:
Pedro Pinto committed 2017-12-19 11:26:02 -05:00
1 parent 1f9ccafa16
commit 20c4cb912d
8 files changed
+125 -116

No files matched your search

@@ -1,110 +1,111 @@
// #include "OrderManager.h"
// #include "server/sparkd/orderman2/order_factory/OrderFactory.h"
// #include "server/sparkd/securities/ISecurityDB.h"
// #include "server/sparkd/securities/MapSecurityDB.h"
// #include "server/sparkd/orderman2/events/NewSecuritiesEvent.h"
// #include "server/sparkd/orderman2/events/StoredOrderEvent.h"
// #include "server/sparkd/orderman2/events/ActivateOrderEvent.h"
// #include "server/sparkd/orderman2/events/UpdateOrderEvent.h"
// #include "server/sparkd/orderman2/events/NewExecutionReportEvent.h"
#include "OrderManager.h"
#include "server/sparkd/orderman2/order_factory/OrderFactory.h"
#include "server/sparkd/securities/ISecurityDB.h"
#include "server/sparkd/securities/MapSecurityDB.h"
#include "server/sparkd/orderman2/events/NewSecuritiesEvent.h"
#include "server/sparkd/orderman2/events/StoredOrderEvent.h"
#include "server/sparkd/orderman2/events/ActivateOrderEvent.h"
#include "server/sparkd/orderman2/events/UpdateOrderEvent.h"
#include "server/sparkd/orderman2/events/NewExecutionReportEvent.h"
#include "server/sparkd/orderman2/orders/Order.h"
// #include <unordered_map>
#include <unordered_map>
// namespace bts {
// namespace orderman2 {
namespace bts {
namespace orderman2 {
// enum
// class OrderManager::State {WAIT_SEC, WAIT_ORDER_RECOVERY, READY } ;
enum
class OrderManager::State {WAIT_SEC, WAIT_ORDERS, READY } ;
// struct OrderManager::PImpl
// {
// PImpl(config::JetFIX::Connection const &cfg) :
// state (State::WAIT_SEC),
// sec_db(),
// orders(),
// order_factory(sec_db),
// stored_order_sub(),
// cfg(cfg),
// gw_name(GatewayName(cfg.name()))
// {}
struct OrderManager::PImpl
{
PImpl(config::JetFIX::Connection const &cfg) :
state (State::WAIT_SEC),
sec_db(),
orders(),
order_factory(sec_db),
stored_order_sub(),
cfg(cfg),
gw_name(GatewayName(cfg.name()))
{}
// State state;
// securities::MapSecurityDB sec_db;
// std::unordered_map<OrderKey::Value,Order *> orders;
// OrderFactory order_factory;
// mercury::Actor::SubHandle stored_order_sub;
// const config::JetFIX::Connection cfg;
// const GatewayName gw_name;
// };
State state;
securities::MapSecurityDB sec_db;
std::unordered_map<OrderKey,Order *> orders;
OrderFactory order_factory;
mercury::Actor::SubHandle stored_order_sub;
const config::JetFIX::Connection cfg;
const GatewayName gw_name;
};
// OrderManager::OrderManager (config::JetFIX::Connection const&cfg) : me (new PImpl(cfg)) {}
// OrderManager::~OrderManager () {}
OrderManager::OrderManager (config::JetFIX::Connection const&cfg) : me (new PImpl(cfg)) {}
OrderManager::~OrderManager () {}
// void OrderManager::init()
// {
// me->state = State::WAIT_SEC;
// subscribe<NewSecuritiesEvent>([this] (auto const&sec_update) {this->on_securities(sec_update);});
// }
void OrderManager::init()
{
me->state = State::WAIT_SEC;
subscribe<NewSecuritiesEvent>([this] (auto const&sec_update) {this->on_securities(sec_update);});
}
// void OrderManager::on_securities(const mercury::Update<NewSecuritiesEvent> &update)
// {
// if (update.event) {
void OrderManager::on_securities(const mercury::Update<NewSecuritiesEvent> &update)
{
if (update.events) {
// // update our security db
// for (auto sec_ptr : update.event->securities)
// me->sec_db.securities[SecurityKey(sec_ptr->id())] = sec_ptr;
// update our security db
for (auto ev : *(update.events)) {
auto sec_ptr = ev.security;
me->sec_db.securities[SecurityKey(sec_ptr->id())] = sec_ptr;
}
// if (me->state == State::WAIT_SEC) {
// // obtain recovered orders
// me->state = State::WAIT_ORDER_RECOVERY;
// me->stored_order_sub =
// subscribe<StoredOrderEvent> (me->gw_name,
// [this] (auto const&orders)
// {this->on_order_recovery(orders);});
// }
// }
// }
if (me->state == State::WAIT_SEC) {
// obtain recovered orders
me->state = State::WAIT_ORDERS;
me->stored_order_sub =
subscribe<StoredOrderEvent> (me->gw_name,
[this] (auto const&orders)
{this->on_order_recovery(orders);});
}
}
}
// void OrderManager::on_order_recovery(const mercury::Update<StoredOrderEvent> &update)
// {
// if (update.event)
// if (me->state == State::WAIT_ORDER_RECOVERY) {
void OrderManager::on_order_recovery(const mercury::Update<StoredOrderEvent> &update)
{
if (update.events)
if (me->state == State::WAIT_ORDERS) {
// // Got the first batch of persisted orders which has to match
// // the orders stored on disk
// for (auto &order_rec : update.event->orders) {
// auto order = me->order_factory.recover(order_rec);
// me->orders[order.key()] = order;
// order.pre_activate(me->sec_db, me->order_factory);
// order.activate();
// }
// Got the first batch of persisted orders which has to match
// the orders stored on disk (since we are the only ones
// producing new orders)
for (auto const &ev : *(update.events)) {
auto &order = me->order_factory.recover(*ev.order_record);
me->orders[order.key()] = &order;
order.pre_activate(me->sec_db, me->order_factory);
order.activate();
}
// // don't need to monitor persisted orders anymore, all the
// // orders we care about will be pass through this class
// unsubscribe(me->stored_order_sub);
// Don't need to monitor persisted orders anymore, all the
// orders we care about will be generated by us.
unsubscribe(me->stored_order_sub);
// // Now we are ready to subscribe to the execution gateway
// // events. Since we know all the orders that were stored in
// // disk we will be able to match any execution reports we
// // might get to persisted orders. Had we subscribed to
// // gatewat events earlier we wouldn't be able to tell wheter
// // an order was already stored in disk or not when we
// // received an execution report for it. Given that we know
// // for sure that we have all the persisted orders, anything
// // we can't match will signify that we crashed after we sent
// // an order to the gateway but before we were able to persist
// // it. In that case we will do our best to recover the order
// // from the execution report.
// subscribe<NewExecutionReportEvent> (me->gw_name,
// [this] (auto const&ev) {
// on_execution_report(ev);
// });
// }
// }
// Now we are ready to subscribe to the execution gateway
// events. Since we know all the orders that were stored in
// disk we will be able to match any execution reports we
// might get to persisted orders. Had we subscribed to
// gatewat events earlier we would not be able to tell wheter
// an order was already stored in disk or not when we
// received an execution report for it. Given that we know
// for sure that we have all the persisted orders, anything
// we can't match will signify that we crashed after we sent
// an order to the gateway but before we were able to persist
// it. In that case we will do our best to recover the order
// from the execution report.
subscribe<NewExecutionReportEvent> (me->gw_name,
[this] (auto const&ev) {on_execution_report(ev);});
}
}
// void OrderManager::on_execution_report(const mercury::Update<NewExecutionReportEvent> &update)
// {
// }
void OrderManager::on_execution_report(const mercury::Update<NewExecutionReportEvent> &)
{
}
// }}
}}
@@ -18,8 +18,12 @@ namespace orderman2 {
// - a pool of fix buffers (OutFIXPtr)
// - an order factory (OrderFactory)
// - a security database (SecurityDB)
// - an actor base class, which allows orders to publish and subscribe events
// - subscriptions to interesting events such as:
// - execution gateway messages (execution reports, rejects, etc)
// - market data book updates
// - security creation events
// - timer updates
// - order state changes
class OrderManager : public mercury::Actor
{
public:
@@ -296,6 +296,11 @@ ExpireDate = 20161102 432=20161102
CheckSum = 151 10=151
* Open Issues
- We need to decouple OM from fix gateway, so that we can have a slow
OM and a fast OM going against the same gateway
- Once we have multiple OM's we will need to guarantee that they can
see each others order, which means we need to use some sort of
synopsis or other mechanism to ensure that they are synchronized
- Self match protection
- fill mitigation
- should cancel on disconnect be on
@@ -8,11 +8,13 @@
namespace bts {
namespace orderman2 {
class ActivateOrderEvent : public mercury::Event
class ActivateOrderEvent : public mercury::Event<ActivateOrderEvent>
{
public:
using Tag = GatewayName;
ActivateOrderEvent (Order &o) : order (&i(o)) {}
using SynopsysPolicy = mercury::NoSynopsis;
ActivateOrderEvent (Order &o) : order (&o) {}
Order* order;
};
@@ -11,6 +11,7 @@ class GatewayStatusChangeEvent : public mercury::Event<GatewayStatusChangeEvent>
public:
using Tag = GatewayName;
using SynopsisPolicy = mercury::CacheLast;
GatewayStatusChangeEvent(bool c) : is_connected(c) {}
bool is_connected;
};
@@ -2,20 +2,21 @@
#include <memory>
#include <boost/container/pmr/small_vector.hpp>
#include "server/sparkd/mercury/synopsys/AccumulateVector.h"
#include "lib/api/services/OrderService2.pb.h"
#include "server/sparkd/mercury/synopsys/MergeByKey.h"
#include "lib/api/services/OrderService2.pb.h"
namespace bts {
namespace orderman2 {
class StoredOrderEvent : public mercury::Event
class StoredOrderEvent : public mercury::Event <StoredOrderEvent>
{
public:
using Tag = GatewayName;
using SynopsysBuilder = mercury::AccumulateVector<StoredOrderEvent>;
boost::container::small_vector<api::orderman2::OrderRecord,1> orders;
boost::container::small_vector_base<const Data*> &vector() {return orders;}
using SynopsysPolicy = mercury::MergeByKey<StoredOrderEvent>;
auto key() {return order_record->key();}
api::orderman2::OrderRecord *order_record;
};
}}
@@ -8,15 +8,14 @@
namespace bts {
namespace orderman2 {
class UpdateOrderEvent : public mercury::Event
class UpdateOrderEvent : public mercury::Event<UpdateOrderEvent>
{
public:
using Tag = GatewayName;
using SynopsysPolicy = mercury::NoSynopsis;
UpdateOrderEvent (Order &o, Price p, Quantity q) :
order (&i(o)),
price(p),
quantity(q)
order (&o), price(p), quantity(q)
{}
Order* order;
+3 -7
View File
@@ -6,7 +6,7 @@
// fwd
namespace bts {
namespace securities {class ISecurityDB;}
namespace orderman2 {class ISecurityDB; class IOrderFactory;}
namespace orderman2 {class IOrderFactory;}
namespace api {
namespace orderman2 {
class OrderDefinition;
@@ -17,8 +17,6 @@ class OrderState;
namespace bts {
namespace orderman2 {
class ISecurityDB;
class Order
{
public:
@@ -39,10 +37,8 @@ public:
private:
friend class OrderFactory;
friend class OrderManager;
void pre_init(OrderKey , OrderKey ,
securities::ISecurityDB &,
IOrderFactory &) {};
void pre_activate(ISecurityDB &, IOrderFactory &) {};
void pre_init(OrderKey , OrderKey , securities::ISecurityDB &, IOrderFactory &) {};
void pre_activate(securities::ISecurityDB &, IOrderFactory &) {};
struct PImpl;
std::unique_ptr<PImpl> me;