Merge branch 'master' of bitbucket.org:bluetradesys/spark

This commit is contained in:
Kevin Darby committed 2017-09-20 15:41:16 -04:00
commit 14c18e459c
61 files changed
+2322 -2216

No files matched your search

-1
View File
@@ -9,7 +9,6 @@
#include "fabric/Domain.h"
#include "testing/catch_test_runner.h"
int main( int argc, char*argv [] )
{
using namespace bts;
+14 -183
View File
@@ -1,189 +1,20 @@
// #include "OrderMan.h"
#include "OrderMan.h"
#include "server/sparkd/grid/Node.h"
#include "events/NewSecurities.h"
// namespace bts {
// namespace orderman {
namespace bts {
namespace orderman {
// OrderMan::OrderMan(IOrderManObserver &o,
// IFIXGateway &gw,
// OrderKeyGenerator const &key_gen) :
// state_(State::IN_RECOVERY),
// observer_(o),
// fix_gw_(gw),
// orders_(),
// key_gen_(key_gen),
// keys_left_(0),
// next_key_(),
// outgoing_fix_msgs_(),
// time_observers_()
// {
// gen_key();
// }
// OrderMan::~OrderMan() {}
// void OrderMan::on_end_recovery()
// {
// bts_assert(state_ == State::IN_RECOVERY);
// transition(State::DISCONNECTED);
// }
// void OrderMan::send_pending_messages()
// {
// if (outgoing_fix_msgs_->first_stream().n_msgs()) {
// if (state_ == State::READY) {
// outgoing_fix_msgs_->first_stream().close();
// fix_gw_.send(outgoing_fix_msgs_);
// }
// outgoing_fix_msgs_ = fix_gw_.make_outfix_buffer();
// }
// }
// void OrderMan::on_venue_ready() {
// bts_assert(state_ == State::DISCONNECTED);
// transition(State::READY);
// }
// void OrderMan::on_venue_disconnect() {
// bts_assert(state_ == State::READY);
// transition(State::DISCONNECTED);
// }
// void OrderMan::on_begin_replay()
// {
// bts_assert(state_ == State::DISCONNECTED);
// transition(State::REPLAYING);
// }
// void OrderMan::on_end_replay()
// {
// bts_assert(state_ == State::REPLAYING);
// transition(State::DISCONNECTED);
// }
// void OrderMan::on_exe_report(api::Security const &sec,
// OrderKey order_key,
// jetfix::fix::ExecutionReportMsg const&exe_report)
// {
// using namespace jetfix;
// LOG(OrderMan,Info,
// "Received execution report {} for order {} on {}",
// exe_report.get<fix::ExecID>(),
// order_key.value(),
// fix_gw_.name());
// auto it =orders_.find(order_key);
// bool is_new_order = it == orders_.end();
// // is this an order we know about?
// if (is_new_order) {
// // we have never seen this order so we must have crashed
// // before we could persist it. see if we can recover the
// // order from the execution report
// if (exe_report.get<fix::OrdType>() == api::orderman::OT_LIMIT &&
// (exe_report.get<fix::ExecType>() == api::orderman::ET_NEW ||
// exe_report.get<fix::ExecType>() == api::orderman::ET_REJECTED)) {
// // recoverable
// LOG(OrderMan,Error,
// "Received execution report for unknown limit order {} on {}. "
// "This should be investigated."
// " Will try to recreate the order from the execution report",
// order_key.value(),fix_gw_.name());
// it = orders_.emplace(order_key,
// std::unique_ptr<Order>
// (new LimitOrder(order_key,*this,exe_report))).first;
// }
// else {
// // unrecoverable
// LOG(OrderMan,Error,
// "Received execution report for unknown order {} on {}. "
// "Will not be able to recover "
// "order state. This should never happen. Please investigate",
// order_key.value(),fix_gw_.name());
// return;
// }
// }
// it->second->on_exe_report(exe_report);
// observer_.on_order_change(*(it->second));
// if (exe_report.get<fix::ExecType> () == api::orderman::ET_FILL ||
// exe_report.get<fix::ExecType> () == api::orderman::ET_PARTIAL_FILL) {
// OrderFill fill;
// extract_fill(sec,exe_report,&fill);
// observer_.on_fill(*(it->second),fill);
// }
// }
// void OrderMan::on_cancel_reject(api::Security const&,
// OrderKey order_key,
// jetfix::fix::CancelRejectMsg const&msg)
// {
// using namespace jetfix;
// LOG(OrderMan,Error, "Received cancel reject {} for order {} on {}",
// msg.get<fix::ExecID>(),
// order_key.value(),fix_gw_.name());
// auto it = orders_.find(order_key);
// if (it == orders_.end()) {
// LOG(OrderMan,Error,
// "Received cancel rejection for unknown order {} on {}. "
// " This should never happen. Please investigate",
// order_key.value(),fix_gw_.name());
// return;
// }
// else {
// it->second->on_cancel_reject(msg);
// observer_.on_order_change(*(it->second));
// }
// }
struct OrderMan::PImpl
{
PImpl(OnOrderState const&s,OnFill const&f) : on_state(s), on_fills(f) {}
OnOrderState on_state;
OnFill on_fills;
// void OrderMan::on_time( const OrderManTime&t)
// {
// for (auto &o : time_observers_)
// o(t);
// }
};
// OrderKey OrderMan::gen_key()
// {
// const auto KEY_BATCH_SIZE = 1000;
OrderMan::OrderMan(OnOrderState const&s,OnFill const&f) : me (new PImpl(s,f)) {}
OrderMan::~OrderMan () {}
// if (keys_left_ == 0) {
// keys_left_ = KEY_BATCH_SIZE - 1;
// next_key_ = key_gen_(KEY_BATCH_SIZE);
// }
// auto result = next_key_;
// keys_left_--;
// next_key_ = OrderKey(next_key_.value()+1);
// return result;
// }
// const char* OrderMan::state_name(State state)
// {
// switch (state) {
// case State::IN_RECOVERY:
// return "IN_RECOVERY";
// case State::DISCONNECTED:
// return "DISCONNECTED";
// case State::REPLAYING:
// return "REPLAYING";
// case State::READY:
// return "READY";
// }
// }
// void OrderMan::transition(State new_state) {
// LOG(OrderMan,Info, "Order man for {} transitioning from {} to {}",
// fix_gw_.name(),state_name(state_),state_name(new_state));
// state_ = new_state;
// }
// }}
}}
+19 -168
View File
@@ -1,186 +1,37 @@
#pragma once
#include <memory>
#include "lib/api/services/OrderService.pb.h"
#include "lib/logging/logging.h"
#include "lib/utils/bts_assert.h"
#include "server/sparkd/jetfix/codec/MessageDefinitions.h"
#include "server/sparkd/jetfix/common/OutFIX.h"
#include "order_man_common.h"
#include "IFIXGateway.h"
#include "fix_order_codec.h"
#include <boost/container/flat_map.hpp>
#include "server/sparkd/order_man/order_man_utils.h"
namespace bts {
namespace api {
namespace orderman {
class OrderState;
class Fill;
class CreateOrderRequest;
class UpdateOrderRequest;
class CancelOrderRequest;
}}
class OrderManTime;
class IMDSource;
class OrderFill {};
using TimeObserver = std::function<void(OrderManTime const&)>;
class Order {
public:
virtual void on_exe_report(jetfix::fix::ExecutionReportMsg const&) {}
virtual void on_cancel_reject(jetfix::fix::CancelRejectMsg const&) {}
};
class IOrderManObserver {
public:
virtual void on_order_change(Order const &order) = 0;
virtual void on_fill(Order const&order, OrderFill const &rf) = 0;
};
class IFIXGateway;
class CreateOrder;
class UpdateOrder;
class CancelOrder;
class OrderMan;
class LimitOrder : public Order{
public:
LimitOrder(OrderKey, OrderMan &,jetfix::fix::ExecutionReportMsg const&);
};
namespace orderman {
class OrderMan
{
public:
struct Result {api::orderman::OrderError error;OrderKey key;};
OrderMan(IOrderManObserver &o,
IFIXGateway &gw, OrderKeyGenerator const &key_gen) ;
using OnOrderState = std::function<void(api::orderman::OrderState const&)>;
using OnFill = std::function<void(api::orderman::Fill const&f)>;
OrderMan(OnOrderState const&,OnFill const&);
~OrderMan();
template <class OrderType>
Result create(typename OrderType::Create const &create_arg)
{
if (state_!= State::READY) {
LOG(OrderMan,Info, "Cannot create order because venue {} is not ready", fix_gw_.name());
return Result {api::orderman::OE_VENUE_NOT_READY,OrderKey(0)};
}
auto k = gen_key();
auto order_ptr = new OrderType (k,*this,create_arg);
auto &order_slot = orders_[k];
bts_assert(!order_slot);
order_slot = std::unique_ptr<Order>(order_ptr);
return {api::orderman::OE_OK,k};
}
template <class OrderType>
Result update(OrderKey k, typename OrderType::Update const& update_arg)
{
if (state_!= State::READY) {
LOG(OrderMan,Info, "Cannot update order {} because venue {} is not ready", k.value(),fix_gw_.name());
return Result {api::orderman::OE_VENUE_NOT_READY,k};
}
auto it = orders_.find(k);
if (it == orders_.end()) {
LOG(OrderMan,Error,
"Cannot update order: {} on venue {} because no such order exists",
k.value(),fix_gw_.name());
return Result {api::orderman::OE_UNKNOWN_ORDER,k};
}
auto abstract_order_ptr = it->second.get();
auto concrete_order_ptr = dynamic_cast<OrderType*> (abstract_order_ptr);
if (!concrete_order_ptr) {
LOG(OrderMan,Error,
"Cannot update order: {} on venue {} because its type does not match the requested type. "
"This is a serious problem. Contact development immediately",
k.value(),fix_gw_.name());
return Result {api::orderman::OE_ORDER_TYPE_MISMATCH,k};
}
concrete_order_ptr->update(update_arg);
return Result {api::orderman::OE_OK,k};
}
template <class OrderType>
Result cancel(OrderKey k, typename OrderType::Cancel const& cancel_arg)
{
if (state_!= State::READY) {
LOG(OrderMan,Info, "Cannot cancel order {} because venue {} is not ready", k.value(),fix_gw_.name());
return Result {api::orderman::OE_VENUE_NOT_READY,k};
}
auto it = orders_.find(k);
if (it == orders_.end()) {
LOG(OrderMan,Error,
"Cannot cancel order: {} on venue {} because no such order exists",
k.value(),fix_gw_.name());
return Result {api::orderman::OE_UNKNOWN_ORDER,k};
}
auto abstract_order_ptr = it->second.get();
auto concrete_order_ptr = dynamic_cast<OrderType*> (abstract_order_ptr);
if (!concrete_order_ptr) {
LOG(OrderMan,Error,
"Cannot cancel order: {} on venue {} because its type does not match the requested type. "
"This is a serious problem. Contact development immediately",
k.value(),fix_gw_.name());
return Result {api::orderman::OE_ORDER_TYPE_MISMATCH,k};
}
concrete_order_ptr->cancel(cancel_arg);
return Result {api::orderman::OE_OK,k};
}
template <class OrderType>
Result recover(OrderKey k,typename OrderType::State const&recovered_state)
{
bts_assert(state_ == State::IN_RECOVERY);
auto order_ptr = new OrderType (k,*this,recovered_state);
auto &order_slot = orders_[k];
bts_assert(!order_slot);
order_slot = std::unique_ptr<Order>(order_ptr);
return {api::orderman::OE_OK,k};
}
void on_end_recovery();
void send_pending_messages();
void on_venue_ready();
void on_venue_disconnect();
void on_begin_replay();
void on_end_replay();
void on_exe_report(api::Security const &sec, OrderKey order_key,
jetfix::fix::ExecutionReportMsg const&exe_report);
void on_cancel_reject(api::Security const&, OrderKey order_key,
jetfix::fix::CancelRejectMsg const&msg);
void on_time( const OrderManTime&t);
void start();
void create_order(OrderKey key,api::orderman::CreateOrderRequest const&r);
void update_order(api::orderman::UpdateOrderRequest const&r);
void cancel_order(api::orderman::CancelOrderRequest const&r);
private:
enum class State {IN_RECOVERY,DISCONNECTED, REPLAYING, READY };
using OrderMap = boost::container::flat_map<OrderKey,std::unique_ptr<Order>> ;
OrderKey gen_key();
const char* state_name(State state);
void transition(State new_state) ;
State state_;
// IMDSource &md_src_;
IOrderManObserver &observer_;
IFIXGateway &fix_gw_;
OrderMap orders_;
OrderKeyGenerator key_gen_;
size_t keys_left_;
OrderKey next_key_;
jetfix::OutFIXPtr outgoing_fix_msgs_;
std::vector<TimeObserver> time_observers_;
struct PImpl;
std::unique_ptr<PImpl> me;
};
}}
@@ -0,0 +1,18 @@
#pragma once
#include <memory>
namespace bts {
namespace orderman {
class OrderManSetup
{
public:
OrderManSetup();
~OrderManSetup();
private:
struct PImpl;
std::unique_ptr<PImpl> me;
};
}}
+6 -6
View File
@@ -13,8 +13,8 @@ std::string OrderState::to_string () const
os.set_order_key(order_key.value());
auto &proto = *os.mutable_native();
proto.set_account_name(account_name.value());
proto.set_operator_id(operator_id.value());
// proto.set_account_name(account_name.value());
// proto.set_operator_id(operator_id.value());
proto.set_security_key(security_key.value());
proto.set_side(side);
proto.set_time_in_force(time_in_force);
@@ -22,7 +22,7 @@ std::string OrderState::to_string () const
proto.set_customer_or_firm(customer_or_firm);
proto.set_cti_code(cti_code);
proto.set_user_login(user_login.value());
proto.set_exchange_order_id(exchange_order_id.value());
// proto.set_exchange_order_id(exchange_order_id.value());
proto.set_manual_order_indicator(manual_order_indicator);
proto.set_price(price.value());
proto.set_quantity(quantity.value());
@@ -48,8 +48,8 @@ void OrderState::from_string(std::string const&str)
bts_assert(proto.transact_time().size() == ts.size());
std::copy(proto.transact_time().begin(),proto.transact_time().end(), &ts[0]);
*this = OrderState {
AccountName(proto.account_name()),
OperatorID(proto.operator_id()),
AccountName(),
OperatorID(),
OrderKey(os.order_key()),
SecurityKey(proto.security_key()),
proto.side(),
@@ -58,7 +58,7 @@ void OrderState::from_string(std::string const&str)
proto.customer_or_firm(),
proto.cti_code(),
UserLogin(proto.user_login()),
ExchangeOrderID(proto.exchange_order_id()),
ExchangeOrderID(),
proto.manual_order_indicator(),
Price(proto.price()),
Quantity(proto.quantity()),
-1
View File
@@ -2,7 +2,6 @@
#include <memory>
#include "order_man_utils.h"
#include "order_man_utils.h"
#include "lib/security_model/SecurityHelper.h"
#include "lib/domain_model/Security.pb.h"
+11 -11
View File
@@ -133,8 +133,8 @@ WorkingOrder::WorkingOrder(fix::ExecutionReportMsg const&msg)
decode_clorid(msg.get<fix::ClOrdID>(),&ok, &sk);
me->order_data = OrderState {
AccountName(msg.get<fix::Account>()),
OperatorID(msg.get<fix::TargetSubID>()),
to<orderman::AccountName>(msg.get<fix::Account>()),
to<orderman::OperatorID>(msg.get<fix::TargetSubID>()),
ok,
sk,
api::BuySell(msg.get<fix::Side>()),
@@ -143,7 +143,7 @@ WorkingOrder::WorkingOrder(fix::ExecutionReportMsg const&msg)
COF_UNDEFINED,
CTI_UNDEFINED,
UserLogin(),
ExchangeOrderID(msg.get<fix::OrderID>()),
to<ExchangeOrderID>(msg.get<fix::OrderID>()),
ManualOrderIndicator(msg.get<fix::ManualOrderIndicator>()),
Price (msg.get<fix::Price>()),
Quantity (msg.get<fix::OrderQty>()),
@@ -180,8 +180,8 @@ OrderError WorkingOrder::encode_create(OrderCreate const&create,jetfix::FIXEncod
encoding.add<fix::Price> (scale_price(*create.security,create.price).value(), PriceFieldSz);
encoding.add<fix::OrderQty> (create.quantity.value(),SizeFieldSz);
encoding.add<fix::TransactTimeStr>(&create.transact_time[0],create.transact_time.size());
encoding.add<fix::Account> (create.account_name.value());
encoding.add<fix::SenderSubID> (create.operator_id.value());
encoding.add<fix::Account> (to_str(create.account_name));
encoding.add<fix::SenderSubID> (to_str(create.operator_id));
std::string clordid(clordid_size(),' ');
encode_clorid(create.order_key,create.security_key,&clordid);
@@ -264,8 +264,8 @@ OrderError WorkingOrder::encode_cancel(OrderCancel const&cancel,jetfix::FIXEncod
encoding.add<fix::ClOrdID> (clordid);
encoding.add<fix::OrigClOrdID> (clordid);
encoding.add<fix::CorrelationClOrdID> (clordid);
encoding.add<fix::SenderSubID> (state.operator_id.value());
encoding.add<fix::OrderID> (state.exchange_order_id.value());
encoding.add<fix::SenderSubID> (to_str(state.operator_id));
encoding.add<fix::OrderID> (to_str(state.exchange_order_id));
encoding.add<fix::Side> (state.side);
encoding.add<fix::TransactTimeStr>(&state.transact_time[0],state.transact_time.size());
encoding.add<fix::ManualOrderIndicator> (state.manual_order_indicator);
@@ -294,8 +294,8 @@ void WorkingOrder::init_encoding()
me->transact_time_bm =
encoding.add<fix::TransactTimeStr> (datetime::FIX_timestamp(datetime::now()));
encoding.add<fix::Account> (state.account_name.value());
encoding.add<fix::SenderSubID> (state.operator_id.value());
encoding.add<fix::Account> (to_str(state.account_name));
encoding.add<fix::SenderSubID> (to_str(state.operator_id));
std::string clordid(clordid_size(),' ');
encode_clorid(state.order_key,state.security_key,&clordid);
@@ -308,7 +308,7 @@ void WorkingOrder::init_encoding()
encoding.add<fix::OrdType> (state.order_type);
encoding.add<fix::CustomerOrFirm> (state.customer_or_firm);
encoding.add<fix::CtiCode> (state.cti_code);
encoding.add<fix::OrderID> (state.exchange_order_id.value());
encoding.add<fix::OrderID> (to_str(state.exchange_order_id));
encoding.add<fix::SecurityDesc> (me->security->glbx().security_desc());
}
@@ -374,7 +374,7 @@ bool WorkingOrder::on_exe_report (jetfix::fix::ExecutionReportMsg const&msg)
auto const &ts_str = msg.get<fix::TransactTimeStr>();
std::copy(ts_str.begin(),ts_str.end(),&(me->order_data.transact_time[0]));
me->order_data.exchange_order_id = ExchangeOrderID(msg.get<fix::OrderID>());
me->order_data.exchange_order_id = to<ExchangeOrderID>(msg.get<fix::OrderID>());
switch (me->internal_state)
{
case InternalState::UNACKNOWLEDGED:
@@ -0,0 +1,29 @@
#pragma once
#include <memory>
#include "server/sparkd/jetfix/jetfix.h"
namespace bts {
namespace orderman {
class Gateway
{
public:
using On
Gateway(config::JetFIX::Connection const&c,
) : _gw(c) {}
void on(OutGatewayMsg const&m) {_gw.send(m.msg);}
~Gateway();
private:
using FIXGw = jetfix::JetFIXGateway<fix::ExecutionReportMsg,
fix::BusinessMessageRejectMsg,
fix::RejectMsg,
fix::CancelRejectMsg>;
FIXGw _gw;
};
}}
@@ -0,0 +1 @@
#include "MDSource.h"
@@ -0,0 +1,47 @@
#pragma once
#include "server/sparkd/order_man/events/NewSecurities.h"
#include "server/sparkd/order_man/events/BookUpdate.h"
#include "lib/utils/TimeStamp.h"
#include "lib/md/MDConsumerHandle.h"
namespace bts {
namespace orderman {
class MDSource
{
public:
using OnBook = std::function<void(event::BookUpdate const&)>;
MDSource(OnBook const&cb) : _on_book(cb), _handles() {}
void on(event::NewSecurities const&secs)
{
for (auto const &s : secs.value)
_handles.push_back({utils::TimeStamp(),s.handle});
}
void poll_md() {
event::BookUpdate book;
for (auto &h : _handles)
if (h.handle->get_depth(h.last,
std::tuple_size<event::BookSide>(),
&book.bid()[0],
&book.ask()[0]) == md2::HandleReadResult::SUCCESS) {
book.security_key = SecurityKey(h.handle->key());
_on_book(book);
}
}
private:
OnBook _on_book;
struct HandleRecord {
utils::TimeStamp last;
md2::MDConsumerHandle* handle;
};
std::vector<HandleRecord> _handles;
};
}}
@@ -0,0 +1,52 @@
#include "SecuritySource.h"
#include "lib/domain_model/common.h"
#include "lib/md/MDRecoverableConsumer.h"
#include "lib/utils/bts_assert.h"
#include "server/sparkd/order_man/events/NewSecurities.h"
#include "server/sparkd/order_man/order_man_utils.h"
#include <map>
namespace bts {
namespace orderman {
struct SecuritySource::PImpl
{
PImpl(OnSecurities const&cb)
: on_securities(cb),md_consumer(),known_securities(),pending_event(),
keep_alive()
{}
OnSecurities on_securities;
md::MDRecoverableConsumer md_consumer;
std::map<SecurityKey,std::unique_ptr<api::Security>> known_securities;
event::NewSecurities pending_event;
std::vector<std::shared_ptr<md2::MDConsumerHandle>> keep_alive;
};
SecuritySource::SecuritySource (OnSecurities const&cb) : me (new PImpl(cb))
{
me->md_consumer.on_security ([this] (api::Security const &sec, md2::MDConsumerHandle ) {
auto &ptr = me->known_securities[SecurityKey(sec.id())];
bts_assert(!ptr);
ptr = std::make_unique<api::Security>(sec);
auto handle = me->md_consumer.get_managed_handle(sec.id());
me->keep_alive.push_back(handle);
me->pending_event.value.push_back({ptr.get(),handle.get()});
});
}
void SecuritySource::discover()
{
me->md_consumer.scan_securities ();
if (me->pending_event.value.size()) {
me->on_securities(me->pending_event);
me->pending_event.value.clear();
}
}
SecuritySource::~SecuritySource () {}
}}
@@ -0,0 +1,23 @@
#pragma once
#include <memory>
namespace bts {
namespace orderman {
namespace event {class NewSecurities;}
class SecuritySource
{
public:
using OnSecurities = std::function<void(event::NewSecurities const&)>;
SecuritySource(OnSecurities const&cb);
void discover();
~SecuritySource();
private:
struct PImpl;
std::unique_ptr<PImpl> me;
};
}}
@@ -0,0 +1,36 @@
#pragma once
#include "server/sparkd/order_man/events/NewSecurities.h"
#include "server/sparkd/order_man/events/BookUpdate.h"
#include "lib/utils/TimeStamp.h"
#include "lib/md/MDConsumerHandle.h"
namespace bts {
namespace orderman {
class TimeSource
{
public:
using OnTime = std::function<void(event::TimeChange const&)>;
MDSource(OnTime const&cb) : _on_time(cb),_last() {}
void check_time() {
auto now = Timer::now();
if (ms_diff(now,_last) > 10) {
_last = now;
event::TimeChange change;
auto ts_str = datetime::FIX_timestamp(datetime::now());
bts_assert(ts_str.size() == std::tuple_size<OrderManTime>());
std::copy(ts_str.begin(),ts_str.end(),&change.ts_str[0]);
_on_time(change);
}
}
private:
OnTime _on_time;
TimeStamp _last;
};
}}
@@ -34,7 +34,6 @@
| remaining_quantity | N | N | N | Y | Y | ~const,null |
| reject_msg | N | N | N | Y | Y | ~const,null |
* Order Chain trace
** NewOrder
=-=-=-=-=-=-==-=-=-=-=-=-==-=-=-=-=-=-=
@@ -0,0 +1,30 @@
#pragma once
#include <memory>
#include "lib/domain_model/common.h"
#include "lib/config/protos/MarketData.pb.h"
#include "lib/md/Structure/PriceSize.h"
namespace bts {
namespace orderman {
namespace event {
using BookSide = std::array<md2::PriceSize,5> ;
class BookUpdate {
public:
SecurityKey security_key;
BookSide &bid () {return _book[0];}
BookSide &ask () {return _book[1];}
BookSide& get_side(api::BuySell bs)
{
bts_assert_d(bs != api::UNDEFINED);
return _book[bs - api::BUY];
}
private:
std::array<BookSide,2> _book;
};
}}}
@@ -0,0 +1,32 @@
#pragma once
#include <memory>
#include "lib/api/services/OrderService.pb.h"
namespace bts {
namespace orderman {
namespace event {
class ExecutionReport
{
public:
AccountName account_name;
OperatorID operator_id;
OrderKey order_key;
SecurityKey security_key;
api::BuySell side;
TimeInForce::Type time_in_force;
api::orderman::OrderType order_type;
api::orderman::ExecType exec_type;
api::orderman::OrderStatus order_status;
ExchangeOrderID exchange_order_id;
OrderPrice price;
Quantity order_quantity;
Quantity filled_quantity;
Quantity last_shares;
OrderPrice last_px;
std::string reject_msg;
};
}}}
@@ -0,0 +1,16 @@
#pragma once
#include <memory>
#include "server/sparkd/order_man/order_man_utils.h"
namespace bts {
namespace orderman {
namespace event {
class GatewayStatusChange {
public:
GatewayName name;
bool is_connected;
};
}}}
@@ -0,0 +1,23 @@
#pragma once
#include <vector>
namespace bts {
namespace api {class Security;}
namespace md2 {class MDConsumerHandle;}
namespace orderman {
namespace event {
class NewSecurities
{
public:
struct SecRec {
api::Security const* sec;
md2::MDConsumerHandle* handle;
};
std::vector<SecRec> value;
};
}}}
@@ -0,0 +1,13 @@
#include "OutGatewayMsg.h"
#include "server/sparkd/jetfix/common/OutFIXPtr.h"
namespace bts {
namespace orderman {
namespace event {
class OutGatewayMsg {
public:
jetfix::OutFIXPtr msg;
};
}}}
@@ -0,0 +1 @@
#include "TimeChange.h"
@@ -0,0 +1,18 @@
#pragma once
#include <memory>
#include "lib/utils/bts_chrono.h"
#include "server/sparkd/order_man/order_man_utils.h"
namespace bts {
namespace orderman {
namespace event {
class TimeChange
{
public:
TimeStamp ts;
OrderTimeStamp ts_str;
};
}}}
+38 -38
View File
@@ -1,52 +1,52 @@
#pragma once
// #pragma once
#include <memory>
#include <cstring>
// #include <memory>
// #include <cstring>
#include "order_man_common.h"
#include "server/sparkd/jetfix/codec/i32_toa.h"
#include "server/sparkd/jetfix/codec/MessageDefinitions.h"
#include "lib/utils/bts_assert.h"
#include "order_man_common.h"
// #include "order_man_common.h"
// #include "server/sparkd/jetfix/codec/i32_toa.h"
// #include "server/sparkd/jetfix/codec/MessageDefinitions.h"
// #include "lib/utils/bts_assert.h"
// #include "order_man_common.h"
namespace bts {
namespace api {class Security;}
// namespace bts {
// namespace api {class Security;}
namespace orderman {
// namespace orderman {
class OrderFill;
// class OrderFill;
void extract_fill(api::Security const &sec,jetfix::fix::ExecutionReportMsg const&, OrderFill *);
// void extract_fill(api::Security const &sec,jetfix::fix::ExecutionReportMsg const&, OrderFill *);
constexpr size_t clordid_size() {return 20;}
// constexpr size_t clordid_size() {return 20;}
inline void encode_clorid(OrderKey ok, SecurityKey sk,std::string *out) {
u32_toa(sk.value(),&((*out)[0]),clordid_size()/2);
u32_toa(ok.value(),&((*out)[clordid_size()/2]),clordid_size()/2);
}
// inline void encode_clorid(OrderKey ok, SecurityKey sk,std::string *out) {
// u32_toa(sk.value(),&((*out)[0]),clordid_size()/2);
// u32_toa(ok.value(),&((*out)[clordid_size()/2]),clordid_size()/2);
// }
inline void decode_clorid(std::string const&in, OrderKey *ok, SecurityKey *sk)
{
bts_assert(in.size() == clordid_size());
char buffer[clordid_size()/2+1];
buffer[clordid_size()/2] = 0;
// inline void decode_clorid(std::string const&in, OrderKey *ok, SecurityKey *sk)
// {
// bts_assert(in.size() == clordid_size());
// char buffer[clordid_size()/2+1];
// buffer[clordid_size()/2] = 0;
std::memcpy(buffer,&in[0],clordid_size()/2);
*sk = SecurityKey(std::atoi(buffer));
// std::memcpy(buffer,&in[0],clordid_size()/2);
// *sk = SecurityKey(std::atoi(buffer));
std::memcpy(buffer,&in[clordid_size()/2],clordid_size()/2);
*ok = OrderKey(std::atoi(buffer));
}
// std::memcpy(buffer,&in[clordid_size()/2],clordid_size()/2);
// *ok = OrderKey(std::atoi(buffer));
// }
template <class MSG>
OrderKey get_order_key(MSG const &msg)
{
// template <class MSG>
// OrderKey get_order_key(MSG const &msg)
// {
bts_assert(msg.template has<jetfix::fix::ClOrdID> ());
OrderKey result;
SecurityKey dummy;
decode_clorid(msg.template get<jetfix::fix::ClOrdID>(),&result,&dummy);
return result;
}
// bts_assert(msg.template has<jetfix::fix::ClOrdID> ());
// OrderKey result;
// SecurityKey dummy;
// decode_clorid(msg.template get<jetfix::fix::ClOrdID>(),&result,&dummy);
// return result;
// }
}}
// }}
@@ -1 +0,0 @@
#include "order_man_common.h"
@@ -1,23 +0,0 @@
#pragma once
#include <cstdint>
#include <string>
#include <array>
#include <functional>
#include "lib/utils/ValueWrapper.h"
#include "lib/utils/bts_assert.h"
#include "lib/domain_model/common.h"
namespace bts {
namespace orderman {
using OrderKey = utils::ValueWrapper<struct OrderKeyTag,uint32_t>;
using FIXSessionName = utils::ValueWrapper<struct FIXSessionNameTag,std::string>;
using OperatorID = utils::ValueWrapper<struct OperatorIDTag,std::string>;
using ExchangeOrderID = utils::ValueWrapper<struct ExchangeOrderIDTag,std::string>;
using OrderTimeStamp = std::array<char, sizeof("20121204-14:44:02.000")-1> ;
using FillID = utils::ValueWrapper<struct FillIDTag,std::string>;
using OrderKeyGenerator = std::function<OrderKey (size_t how_many)>;
}}
+39 -2
View File
@@ -3,17 +3,54 @@
#include <cstdint>
#include <string>
#include <array>
#include <cstring>
#include "lib/utils/ValueWrapper.h"
#include "lib/domain_model/common.h"
#include "lib/utils/bts_assert.h"
namespace bts {
namespace api {class Security;}
namespace orderman {
using OrderKey = utils::ValueWrapper<struct OrderKeyTag,uint32_t>;
using FIXSessionName = utils::ValueWrapper<struct FIXSessionNameTag,std::string>;
using OperatorID = utils::ValueWrapper<struct OperatorIDTag,std::string>;
using ExchangeOrderID = utils::ValueWrapper<struct ExchangeOrderIDTag,std::string>;
using GatewayName = utils::ValueWrapper<struct GatewayNameTag,std::string>;
using OperatorID = utils::ValueWrapper<struct OperatorIDTag,std::array<char,18>>;
using ExchangeOrderID = utils::ValueWrapper<struct ExchangeOrderIDTag,std::array<char,17>>;
using OrderTimeStamp = std::array<char, sizeof("20121204-14:44:02.000")-1> ;
using SecurityPtr = const api::Security *;
using AccountName = utils::ValueWrapper<struct AccountNameTag,std::array<char,12>>;
template <size_t N> void to_array(std::string const&str,std::array<char,N>*out )
{
auto &result = *out;
bts_assert(str.size() <= N);
memset(&result[0],' ', N);
memcpy(&result[0],str.data(),str.size());
}
template <size_t N> void to_str(std::array<char,N> const&in ,std::string *out)
{
auto &result = *out;
result.resize(N);
memcpy(&result[0],in.data(),N);
}
template <class T>
T to(std::string const&str)
{
T result;
to_array(str,&result.value());
return result;
}
template <class T>
inline std::string to_str (T const&t)
{
std::string result;
to_str(t.value(),&result);
return result;
}
}}
@@ -1,88 +1,88 @@
#include "RiskLimitManager.h"
#include "SharedRiskLimiter.h"
#include "RiskLimitState.h"
#include "IRiskLimitObserver.h"
#include "lib/config/config.h"
#include "lib/config/protos/Account.pb.h"
#include "lib/config/protos/User.pb.h"
#include "lib/domain_model/Security.pb.h"
#include "lib/utils/bts_assert.h"
#include "lib/logging/logging.h"
#include <boost/container/flat_map.hpp>
#include <tuple>
using namespace bts::orderman;
namespace bts {
namespace risklimits {
using OperatorMap = boost::container::flat_map<std::string, config::Operator>;
using SymbolKey = std::tuple<std::string, api::Security::ContractCase>;
using KeyType = std::tuple<AccountName, OperatorID, SymbolKey>;
using SharedLimiterMap = boost::container::flat_map<KeyType, std::shared_ptr<SharedRiskLimiter>>;
using StateKeyType = std::tuple<AccountName, OperatorID>;
using PBLimitsMap = const google::protobuf::Map<std::__cxx11::string, bts::config::RiskLimit>;
struct RiskLimitManager::PImpl
{
RiskLimitManager& this_ptr;
OperatorMap operator_map;
SharedLimiterMap limiter_map;
std::vector<IRiskLimitObserver*> observers;
boost::container::flat_map<StateKeyType, RiskLimitState> aggregate_state;
PImpl(RiskLimitManager& rm) :
this_ptr(rm),
operator_map(), limiter_map(), observers(), aggregate_state()
{
auto users = config::get<config::UserCfg>();
for(int i = 0; i < users.user_size(); i++)
{
auto user = users.user(i);
for(int j = 0; i < user.operators_size(); j++)
operator_map[user.operators(j).operator_id().id()] = user.operators(j);
}
auto accts = config::get<config::AccountCfg>();
for(int i = 0; i < accts.accounts_with_limits_size(); i++)
{
const auto& acct = accts.accounts_with_limits(i).account();
const auto& future_limit = accts.accounts_with_limits(i).future_limits();
update_limits(future_limit, acct, api::Security::kFuture);
const auto& spread_limit = accts.accounts_with_limits(i).spread_limits();
update_limits(spread_limit, acct, api::Security::kSpread);
}
#include "RiskLimitManager.h"
#include "SharedRiskLimiter.h"
#include "RiskLimitState.h"
#include "IRiskLimitObserver.h"
#include "lib/config/config.h"
#include "lib/config/protos/Account.pb.h"
#include "lib/config/protos/User.pb.h"
#include "lib/domain_model/Security.pb.h"
#include "lib/utils/bts_assert.h"
#include "lib/logging/logging.h"
#include <boost/container/flat_map.hpp>
#include <tuple>
using namespace bts::orderman;
namespace bts {
namespace risklimits {
using OperatorMap = boost::container::flat_map<std::string, config::Operator>;
using SymbolKey = std::tuple<std::string, api::Security::ContractCase>;
using KeyType = std::tuple<AccountName, OperatorID, SymbolKey>;
using SharedLimiterMap = boost::container::flat_map<KeyType, std::shared_ptr<SharedRiskLimiter>>;
using StateKeyType = std::tuple<AccountName, OperatorID>;
using PBLimitsMap = const google::protobuf::Map<std::__cxx11::string, bts::config::RiskLimit>;
struct RiskLimitManager::PImpl
{
RiskLimitManager& this_ptr;
OperatorMap operator_map;
SharedLimiterMap limiter_map;
std::vector<IRiskLimitObserver*> observers;
boost::container::flat_map<StateKeyType, RiskLimitState> aggregate_state;
PImpl(RiskLimitManager& rm) :
this_ptr(rm),
operator_map(), limiter_map(), observers(), aggregate_state()
{
auto users = config::get<config::UserCfg>();
for(int i = 0; i < users.user_size(); i++)
{
auto user = users.user(i);
for(int j = 0; i < user.operators_size(); j++)
operator_map[user.operators(j).operator_id().id()] = user.operators(j);
}
auto accts = config::get<config::AccountCfg>();
for(int i = 0; i < accts.accounts_with_limits_size(); i++)
{
const auto& acct = accts.accounts_with_limits(i).account();
const auto& future_limit = accts.accounts_with_limits(i).future_limits();
update_limits(future_limit, acct, api::Security::kFuture);
const auto& spread_limit = accts.accounts_with_limits(i).spread_limits();
update_limits(spread_limit, acct, api::Security::kSpread);
}
}
void update_limits(PBLimitsMap & map, const bts::config::Account & acct, api::Security::ContractCase contract_type)
{
for(auto j = map.begin(); j != map.end(); j++)
{
for(int k = 0; k < acct.allowed_operators_size(); k++)
{
auto allowed = operator_map.find(acct.allowed_operators(k).id());
if(allowed != operator_map.end())
{
OperatorID opid(allowed->second.operator_id().id());
SharedRiskLimiter& lim = find_limiter(to_accountname(acct), opid, to_symbolkey(j->first, contract_type));
lim.on_account_update(acct);
lim.on_operator_update(allowed->second);
lim.on_risk_limit_update(j->second);
}
}
for(auto j = map.begin(); j != map.end(); j++)
{
for(int k = 0; k < acct.allowed_operators_size(); k++)
{
auto allowed = operator_map.find(acct.allowed_operators(k).id());
if(allowed != operator_map.end())
{
OperatorID opid(allowed->second.operator_id().id());
SharedRiskLimiter& lim = find_limiter(to_accountname(acct), opid, to_symbolkey(j->first, contract_type));
lim.on_account_update(acct);
lim.on_operator_update(allowed->second);
lim.on_risk_limit_update(j->second);
}
}
}
}
SymbolKey to_symbolkey(const api::Security& security)
{
return std::make_tuple("ES", security.Contract_case());
}
}
SymbolKey to_symbolkey(const api::Security& security)
{
return std::make_tuple("ES", security.Contract_case());
}
SymbolKey to_symbolkey(const std::string& symbol, api::Security::ContractCase type)
{
return std::make_tuple(symbol, type);
@@ -90,8 +90,8 @@ struct RiskLimitManager::PImpl
AccountName to_accountname(const config::Account& key)
{
return AccountName(key.account_id());
}
}
SharedRiskLimiter& find_limiter(AccountName acctname, OperatorID opid, SymbolKey symkey)
{
KeyType key(acctname, opid, symkey);
@@ -105,10 +105,10 @@ struct RiskLimitManager::PImpl
limiter_map[key] = ptr;
return *ptr;
}
}
};
}
};
RiskLimitManager::RiskLimitManager() : me(new PImpl(*this)) {}
RiskLimitManager::~RiskLimitManager() {}
+37 -37
View File
@@ -94,42 +94,42 @@ struct RiskLimiter::PImpl
};
RiskLimiter::RiskLimiter(SharedRiskLimiter& sh, orderman::OrderKey key)
: me(new PImpl(sh, key))
{
}
RiskLimiter::~RiskLimiter() {}
RiskLimiter::RiskLimitResult RiskLimiter::check(Quantity qty, api::BuySell side) const
{
RiskLimiter::RiskLimitResult res;
res.success = me->check_can_trade(res.message);
if(!res.success)
return res;
res.success = me->check_clip_size(qty, res.message);
if(!res.success)
return res;
res.success = me->check_position_limit(qty, side, res.message);
return res;
}
void RiskLimiter::on_order_state(const orderman::OrderState& state)
{
int working_diff = state.remaining_quantity.value() - me->remaining_quantity.value();
if(state.side == api::BUY)
me->shared.position_info.long_working += working_diff;
else
me->shared.position_info.short_working += working_diff;
int filled_diff = state.filled_quantity.value() - me->filled_quantity.value();
if(state.side == api::BUY)
me->shared.position_info.long_position += filled_diff;
else
me->shared.position_info.short_position += filled_diff;
}
RiskLimiter::RiskLimiter(SharedRiskLimiter& sh, orderman::OrderKey key)
: me(new PImpl(sh, key))
{
}
RiskLimiter::~RiskLimiter() {}
RiskLimiter::RiskLimitResult RiskLimiter::check(Quantity qty, api::BuySell side) const
{
RiskLimiter::RiskLimitResult res;
res.success = me->check_can_trade(res.message);
if(!res.success)
return res;
res.success = me->check_clip_size(qty, res.message);
if(!res.success)
return res;
res.success = me->check_position_limit(qty, side, res.message);
return res;
}
void RiskLimiter::on_order_state(const orderman::OrderState& state)
{
int working_diff = state.remaining_quantity.value() - me->remaining_quantity.value();
if(state.side == api::BUY)
me->shared.position_info.long_working += working_diff;
else
me->shared.position_info.short_working += working_diff;
int filled_diff = state.filled_quantity.value() - me->filled_quantity.value();
if(state.side == api::BUY)
me->shared.position_info.long_position += filled_diff;
else
me->shared.position_info.short_position += filled_diff;
}
}
}
@@ -27,7 +27,7 @@ namespace spreader
class SimpleSpreaderCreate : public SpreaderCreate
{
public:
AccountName account_name;
orderman::AccountName account_name;
orderman::OperatorID operator_id;
api::BuySell side;
+4 -3
View File
@@ -2,7 +2,8 @@
#include <tuple>
#include "lib/domain_model/Security.pb.h"
#include "server/sparkd/order_man/order_man_common.h"
#include "server/sparkd/order_man/order_man_utils.h"
#include "lib/api/services/OrderService.pb.h"
#include "lib/utils/bts_chrono.h"
@@ -49,7 +50,7 @@ class SpreaderUpdate {};
struct SpreaderSubscriptions
{
std::vector<api::Security const*> securities_for_md;
AccountName account_for_limit_check;
orderman::AccountName account_for_limit_check;
orderman::OperatorID operator_for_limit_check;
};
@@ -115,7 +116,7 @@ protected:
// use this to accelarate the creation of new orders
OrderTemplate const& make_order_template(api::Security const&,
AccountName const &,
orderman::AccountName const &,
orderman::OperatorID const &,
api::BuySell ,
api::orderman::CustomerOrFirm ,