201 lines
7.7 KiB
C++
201 lines
7.7 KiB
C++
#include "ExecutionGateway.h"
|
|
#include "server/sparkd/jetfix/codec/MessageDefinitions.h"
|
|
#include "server/sparkd/jetfix/gateway/JetFIXGateway.h"
|
|
#include "server/sparkd/orderman2/events/NewExecutionReportEvent.h"
|
|
#include "server/sparkd/orderman2/orderman2_utils/fix_order_codec.h"
|
|
#include "server/sparkd/orderman2/events/NewSecuritiesEvent.h"
|
|
#include "server/sparkd/orderman2/events/GatewayStatusChangeEvent.h"
|
|
#include "server/sparkd/mercury/events/PollEvent.h"
|
|
#include "server/sparkd/orderman2/events/CancelRejectEvent.h"
|
|
#include "lib/utils/TimeStamp.h"
|
|
|
|
#include "lib/logging/logging.h"
|
|
|
|
#include <boost/pool/object_pool.hpp>
|
|
|
|
namespace bts {
|
|
namespace orderman2 {
|
|
|
|
using namespace jetfix;
|
|
|
|
using JFGw = JetFIXGateway<fix::ExecutionReportMsg,
|
|
fix::BusinessMessageRejectMsg,
|
|
fix::RejectMsg,
|
|
fix::CancelRejectMsg>;
|
|
|
|
struct ExecutionGateway::PImpl
|
|
{
|
|
PImpl(config::JetFIX::Connection const&cfg) :
|
|
gw(cfg),
|
|
exe_report_pool (),
|
|
state(State::WAITNG_FOR_SECURITIES),
|
|
securities(),
|
|
exe_report_stream(),
|
|
gw_status_change_stream(),
|
|
cancel_reject_stream(),
|
|
gw_name(cfg.name())
|
|
{}
|
|
|
|
JFGw gw;
|
|
boost::object_pool<NewExecutionReportEvent::Data> exe_report_pool;
|
|
State state;
|
|
std::unordered_map<SecurityKey::Value,api::Security const*> securities;
|
|
mercury::Stream<NewExecutionReportEvent> exe_report_stream;
|
|
mercury::Stream<GatewayStatusChangeEvent> gw_status_change_stream;
|
|
mercury::Stream<CancelRejectEvent> cancel_reject_stream;
|
|
GatewayName gw_name;
|
|
};
|
|
|
|
ExecutionGateway::ExecutionGateway (config::JetFIX::Connection const&cfg) : me (new PImpl(cfg)) {}
|
|
ExecutionGateway::~ExecutionGateway () {}
|
|
|
|
void ExecutionGateway::init()
|
|
{
|
|
me->state = State::WAITNG_FOR_SECURITIES;
|
|
subscribe<NewSecuritiesEvent>([this] (auto const&sec_update) {this->on_securities(sec_update);});
|
|
}
|
|
|
|
void ExecutionGateway::on_securities(const mercury::Update<NewSecuritiesEvent> &update)
|
|
{
|
|
if (update.event) {
|
|
|
|
for (auto sec_ptr : update.event->securities)
|
|
me->securities[sec_ptr->id()] = sec_ptr;
|
|
|
|
if (me->state == State::WAITNG_FOR_SECURITIES) {
|
|
LOG(OrderMan,Warn,"Received first set of {} securities. Proceeding with initialization",
|
|
update.event->securities.size());
|
|
|
|
// got our security synopsis, ready to start the GW
|
|
me->state = State::READY;
|
|
|
|
// subscribe to gateway events
|
|
me->gw.on<fix::ExecutionReportMsg>() = [this] (auto const&msg) {this->on_exe_report(msg);};
|
|
me->gw.on<fix::CancelRejectMsg>() = [this] (auto const&msg) {this->on_cancel_reject(msg);};
|
|
me->gw.on_ready() = [this] () {this->on_connection_status_change(true);};
|
|
me->gw.on_disconnect() = [this] () {this->on_connection_status_change(false);};
|
|
me->gw.on<fix::BusinessMessageRejectMsg>() = [this] (auto const&m) {this->on_business_reject(m);};
|
|
me->gw.on<fix::RejectMsg>() = [this] (auto const&m) {this->on_session_reject(m);};
|
|
|
|
// stream of execution reports
|
|
me->exe_report_stream = make_stream<NewExecutionReportEvent>(me->gw_name);
|
|
|
|
// stream for gateway status
|
|
me->gw_status_change_stream = make_stream<GatewayStatusChangeEvent>(me->gw_name);
|
|
|
|
// stream for cancel rejects
|
|
me->cancel_reject_stream = make_stream<CancelRejectEvent> (me->gw_name);
|
|
|
|
// start the gateway
|
|
me->gw.start();
|
|
|
|
// start polling
|
|
subscribe<mercury::PollEvent> (broker_id(), [this] (auto const&) {this->on_poll();});
|
|
}
|
|
}
|
|
}
|
|
|
|
void ExecutionGateway::on_exe_report(const jetfix::fix::ExecutionReportMsg &msg)
|
|
{
|
|
OrderKey ok;
|
|
SecurityKey sk;
|
|
decode_clorid(msg.get<fix::ClOrdID>(),&ok, &sk);
|
|
|
|
auto sec = me->securities[sk.value()];
|
|
|
|
if (!sec)
|
|
LOG(OrderMan,Error,
|
|
"Received execution report for an unknown security {}. Will not be "
|
|
" able to process this report. This is likely a bug that could lead to"
|
|
" lost fills. Please investigate.",sk.value());
|
|
else {
|
|
// ignore leg fills, we care only about the actual security fill
|
|
if (msg.has<fix::MultiLegReportingType>() && msg.get<fix::MultiLegReportingType>() == 2)
|
|
return;
|
|
|
|
std::string reject_msg;
|
|
if (msg.has<fix::Text>())
|
|
reject_msg = msg.get<fix::Text>();
|
|
|
|
// allocate from our pool
|
|
auto data = me->exe_report_pool.construct();
|
|
|
|
// copy data from fix msg to our structure
|
|
str_to(msg.get<fix::Account>(),&data->account_name);
|
|
str_to(msg.get<fix::TargetSubID>(),&data->operator_id);
|
|
data->order_key = ok;
|
|
data->security = sec;
|
|
data->side = api::Side(msg.get<fix::Side>());
|
|
data->time_in_force = api::orderman2::TimeInForce(msg.get<fix::TimeInForce>());
|
|
data->order_type = api::orderman2::NativeOrderType(msg.get<fix::OrdType>());
|
|
data->exec_type = api::orderman2::ExecType(msg.get<fix::ExecType>());
|
|
data->order_status = api::orderman2::NativeOrderStatus(msg.get<fix::OrdStatus>());
|
|
data->manual_order_indicator = msg.get<fix::ManualOrderIndicator>() ?
|
|
api::orderman2::MANUAL_ORDER_INDICATOR_YES :
|
|
api::orderman2::MANUAL_ORDER_INDICATOR_YES ;
|
|
str_to(msg.get<fix::OrderID>(),&data->exchange_order_id);
|
|
data->price = securities::decode_exchange_price(*sec,Price (msg.get<fix::Price>()));
|
|
data->order_quantity = Quantity(msg.get<fix::OrderQty>());
|
|
data->filled_quantity = Quantity(msg.get<fix::CumQty>());
|
|
data->last_shares = msg.has<fix::LastShares>()? Quantity(msg.get<fix::LastShares>()):Quantity(0);
|
|
data->last_price = msg.has<fix::LastPx>()?
|
|
securities::decode_exchange_price(*sec,Price(msg.get<fix::LastPx>())) : Price(0);
|
|
data->avg_price = msg.has<fix::AvgPx>()?
|
|
securities::decode_exchange_price(*sec,Price(msg.get<fix::AvgPx>())) : Price(0);
|
|
str_to(msg.get<fix::ExecID>(),&data->exchange_fill_id);
|
|
str_to(msg.get<fix::TransactTimeStr>(),&data->transact_time);
|
|
data->reject_msg = reject_msg;
|
|
|
|
publish(me->exe_report_stream,data);
|
|
}
|
|
}
|
|
|
|
void ExecutionGateway::on_cancel_reject(const jetfix::fix::CancelRejectMsg &msg)
|
|
{
|
|
OrderKey ok;
|
|
SecurityKey sk;
|
|
decode_clorid(msg.get<fix::ClOrdID>(),&ok, &sk);
|
|
|
|
std::string reject_msg;
|
|
if (msg.has<fix::Text>())
|
|
reject_msg = msg.get<fix::Text>();
|
|
else if (msg.has<fix::CXlRejReason>())
|
|
reject_msg = fmt::format ("Order cancel was rejected with error code {}", msg.get<fix::CXlRejReason>());
|
|
publish(me->cancel_reject_stream,ok,reject_msg);
|
|
}
|
|
|
|
void ExecutionGateway::on_connection_status_change(bool is_connected)
|
|
{
|
|
LOG(OrderMan,Info,"Connection state of {} changed to {}",
|
|
me->gw_name.value(), is_connected? std::string("connected") : std::string("disconnected"));
|
|
|
|
publish(me->gw_status_change_stream,is_connected);
|
|
}
|
|
|
|
void ExecutionGateway::on_business_reject(jetfix::fix::BusinessMessageRejectMsg const&m)
|
|
{
|
|
LOG(OrderMan,Error,"Unexpected business reject in {} gateway . Reason: {}. This should be investigated",
|
|
me->gw_name.value(), m.has<fix::Text>() ? m.get<fix::Text>() : std::string ("Unknown"));
|
|
}
|
|
|
|
void ExecutionGateway::on_session_reject(jetfix::fix::RejectMsg const&m)
|
|
{
|
|
LOG(OrderMan,Error,"Unexpected reject message in {} gateway . Reason: {}. This should be investigated",
|
|
me->gw_name.value(), m.has<fix::Text>() ? m.get<fix::Text>() : std::string ("Unknown"));
|
|
}
|
|
|
|
void ExecutionGateway::on_poll()
|
|
{me->gw.dispatch_events();}
|
|
|
|
void ExecutionGateway::on_outgoing_fix( mercury::Update<OutFIXMsgEvent> const&msg)
|
|
{
|
|
if (msg.event) {
|
|
auto const &m = *msg.event;
|
|
LOG(Latency,Info, "Gateway {} {}", m.cid,utils::TimeStamp::now().to_uint64());
|
|
me->gw.send(m.cid, m.msg);
|
|
}
|
|
}
|
|
|
|
}}
|
|
|