375 lines
16 KiB
C++
375 lines
16 KiB
C++
#include "DropCopySession.h"
|
|
#include "FixRouter.h"
|
|
|
|
#include "lib/api/service_helpers/AdminServiceUtils.h"
|
|
#include "lib/domain_model/Security.h"
|
|
#include "lib/logging/logging.h"
|
|
#include "lib/utils/bts_assert.h"
|
|
#include "lib/utils/helper_macros.h"
|
|
|
|
#include <quickfix/fix44/OrderStatusRequest.h>
|
|
#include <quickfix/fix44/SecurityDefinitionRequest.h>
|
|
#include <quickfix/fix44/SecurityDefinition.h>
|
|
#include <quickfix/fix44/ExecutionReport.h>
|
|
|
|
#include <boost/date_time/posix_time/posix_time.hpp>
|
|
#include <google/protobuf/timestamp.pb.h>
|
|
#include <google/protobuf/util/time_util.h>
|
|
|
|
namespace bts {
|
|
namespace fix {
|
|
|
|
struct DropCopySession::PImpl
|
|
{
|
|
rxcpp::observable<bts::api::orderman::OrdersUpdate> filtered_orders;
|
|
rxcpp::observable<bts::api::orderman::FillsUpdate> filtered_fills;
|
|
PImpl()
|
|
{
|
|
}
|
|
};
|
|
|
|
DropCopySession::DropCopySession(bts::api::ServiceContext& sc, IFixRouter& rt, const FIX::SessionID& id) :
|
|
SessionBase(sc, rt, id),
|
|
_orderservice( sc.locate<api::orderman::OrderService>() ),
|
|
_order_cache(),
|
|
me(new PImpl())
|
|
{
|
|
}
|
|
|
|
DropCopySession::~DropCopySession()
|
|
{
|
|
}
|
|
|
|
void DropCopySession::onLogon(const FIX::SessionID& sessionID)
|
|
{
|
|
bts_assert(sessionID == _sess);
|
|
LOG(Fix, Info, "Logged on {}", sessionID.toString());
|
|
subscribe_fills_and_orders(sessionID);
|
|
}
|
|
|
|
void DropCopySession::onLogout(const FIX::SessionID& sessionID)
|
|
{
|
|
me->filtered_orders.finally([] () {} );
|
|
me->filtered_fills.finally([] () {} );
|
|
|
|
bts_assert(sessionID == _sess);
|
|
LOG(Fix, Info, "Logged out {}", sessionID.toString());
|
|
}
|
|
|
|
void DropCopySession::fromApp(const FIX::Message &message,const FIX::SessionID &sessionID)
|
|
throw(FIX::FieldNotFound, FIX::IncorrectDataFormat, FIX::IncorrectTagValue, FIX::UnsupportedMessageType)
|
|
{
|
|
bts_assert(sessionID == _sess);
|
|
auto mt = message.getHeader().getField(FIX::FIELD::MsgType);
|
|
if(mt == FIX44::OrderStatusRequest::MsgType() ||
|
|
mt == FIX44::SecurityDefinitionRequest::MsgType()
|
|
)
|
|
{
|
|
LOG(Fix, Info, "DC IN: {}", message.toString() );
|
|
crack(message, sessionID);
|
|
}
|
|
else
|
|
{
|
|
LOG(Fix, Info, "DC IN: Unsupported message in {}", mt );
|
|
throw FIX::UnsupportedMessageType(mt);
|
|
}
|
|
}
|
|
|
|
void DropCopySession::onMessage(const FIX44::OrderStatusRequest& req, const FIX::SessionID& s)
|
|
{
|
|
uint oid = atoi(req.getField(FIX::FIELD::OrderID).c_str());
|
|
_order_cache.lock([&] (auto& order_cache)
|
|
{
|
|
auto it = order_cache.find(oid);
|
|
if(it != order_cache.end())
|
|
{
|
|
auto&& ostat = build_order_status(s, *it->second);
|
|
ostat.setField(FIX::FIELD::OrdStatusReqID, req.getField(FIX::FIELD::OrdStatusReqID));
|
|
_router.send(ostat, s);
|
|
}
|
|
});
|
|
}
|
|
|
|
void DropCopySession::onMessage(const FIX44::SecurityDefinitionRequest& req, const FIX::SessionID& s)
|
|
{
|
|
if (req.getField(FIX::FIELD::SecurityRequestType) == std::to_string(FIX::SecurityRequestType_ALL_SECURITIES))
|
|
{
|
|
// auto&& secs = _router.get_securities();
|
|
// if (req.isSetField(FIX::FIELD::Symbol))
|
|
// secs = secs.Where(x => x.Symbol.ToLower() == req.Symbol.getValue().ToLower());
|
|
// if (req.IsSetSecurityType())
|
|
// secs = secs.Where(x => security_types_equal(x.security_type(), req.SecurityType));
|
|
// if (req.IsSetSecurityExchange())
|
|
// secs = secs.Where(x => exchanges_equal(x.Exchange, req.SecurityExchange));
|
|
|
|
auto def = FIX44::SecurityDefinition(FIX::SecurityReqID(req.getField(FIX::FIELD::SecurityReqID)),
|
|
FIX::SecurityResponseID(""),
|
|
FIX::SecurityResponseType(FIX::SecurityResponseType_LIST_OF_SECURITIES_RETURNED_PER_REQUEST));
|
|
_router.send(def, s);
|
|
}
|
|
else
|
|
throw FIX::IncorrectTagValue(FIX::FIELD::SecurityRequestType);
|
|
}
|
|
|
|
|
|
|
|
void DropCopySession::subscribe_fills_and_orders(const FIX::SessionID sessionID)
|
|
{
|
|
//TODO put this in our C++ pipe and smoke it
|
|
//var on_orders = _orderservice.Value.subscribei_orders().Naggle(interval, scheduler).PermissionFilter(_adminobservable).Table().Cache();
|
|
//var on_fills = _orderservice.Value.subscribei_fills().Naggle(interval, scheduler).PermissionFilter(_adminobservable).Table().Cache();
|
|
|
|
me->filtered_orders = _adminobservable.combine_latest(_orderservice.subscribe_orders())
|
|
.map( [] (auto& tuple) -> auto
|
|
{
|
|
std::shared_ptr<AdminModelWrapper> admin = std::get<0>(tuple);
|
|
auto&& update = std::get<1>(tuple);
|
|
|
|
auto all = update.all();
|
|
all.erase(std::remove_if(all.begin(), all.end(),
|
|
[admin](auto& i) -> bool { return !admin->can_view_account(i->def().account()); }), all.end());
|
|
|
|
auto added = update.added();
|
|
added.erase(std::remove_if(added.begin(), added.end(),
|
|
[admin](auto& i) -> bool { return !admin->can_view_account(i->def().account()); }), added.end());
|
|
|
|
auto changed = update.changed();
|
|
changed.erase(std::remove_if(changed.begin(), changed.end(),
|
|
[admin](auto& i) -> bool { return !admin->can_view_account(i.current()->def().account()); }), changed.end());
|
|
|
|
auto removed = update.removed();
|
|
removed.erase(std::remove_if(removed.begin(), removed.end(),
|
|
[admin](auto& i) -> bool { return !admin->can_view_account(i->def().account()); }), removed.end());
|
|
|
|
bts::api::orderman::OrdersUpdate newupdate(all, added, changed, removed);
|
|
return newupdate;
|
|
});
|
|
//.buffer_with_time( std::chrono::milliseconds(500) );
|
|
|
|
me->filtered_orders.subscribe([this, sessionID](const auto& update)
|
|
{
|
|
// for(auto& update : updatevec)
|
|
// {
|
|
for(auto& order : update.added())
|
|
{
|
|
auto&& ostat = build_order_status(sessionID, *order);
|
|
_order_cache.lock( [&order] (auto& order_cache)
|
|
{
|
|
order_cache[order->key()] = order;
|
|
});
|
|
_router.send(ostat, sessionID);
|
|
}
|
|
for(auto& order : update.changed())
|
|
{
|
|
auto&& ostat = build_order_status(sessionID, *order.current(), true);
|
|
_router.send(ostat, sessionID);
|
|
}
|
|
// }
|
|
});
|
|
|
|
me->filtered_fills = _adminobservable.combine_latest(_orderservice.subscribe_fills())
|
|
.map( [] (auto& tuple) -> auto
|
|
{
|
|
std::shared_ptr<AdminModelWrapper> admin = std::get<0>(tuple);
|
|
auto&& update = std::get<1>(tuple);
|
|
|
|
auto all = update.all();
|
|
all.erase(std::remove_if(all.begin(), all.end(),
|
|
[admin](auto& i) -> bool { return !admin->can_view_account(i->account()); }), all.end());
|
|
|
|
auto added = update.added();
|
|
added.erase(std::remove_if(added.begin(), added.end(),
|
|
[admin](auto& i) -> bool { return !admin->can_view_account(i->account()); }), added.end());
|
|
|
|
auto changed = update.changed();
|
|
changed.erase(std::remove_if(changed.begin(), changed.end(),
|
|
[admin](auto& i) -> bool { return !admin->can_view_account(i.current()->account()); }), changed.end());
|
|
|
|
auto removed = update.removed();
|
|
removed.erase(std::remove_if(removed.begin(), removed.end(),
|
|
[admin](auto& i) -> bool { return !admin->can_view_account(i->account()); }), removed.end());
|
|
|
|
bts::api::orderman::FillsUpdate newupdate(all, added, changed, removed);
|
|
return newupdate;
|
|
});
|
|
//.buffer_with_time( std::chrono::milliseconds(500) );
|
|
|
|
me->filtered_fills.subscribe([this, sessionID](const auto& update)
|
|
{
|
|
// for(auto& update : updatevec)
|
|
// {
|
|
for(auto& fill : update.added())
|
|
{
|
|
auto&& fi = build_fill(sessionID, *fill);
|
|
_router.send(fi, sessionID);
|
|
}
|
|
for(auto& fill : update.changed())
|
|
{
|
|
auto&& fi = build_fill(sessionID, *fill.current(), true);
|
|
_router.send(fi, sessionID);
|
|
}
|
|
// }
|
|
});
|
|
}
|
|
|
|
void DropCopySession::process_order_state(const bts::api::orderman::OrderState& state, uint& filled, uint& leaves, double& avgprx,
|
|
FIX::OrdStatus& fstatus, bts::api::orderman::NativeOrderState& native, const bts::api::orderman::Fill* fill)
|
|
{
|
|
UNUSED(fill);
|
|
filled = state.filled_quantity();
|
|
leaves = state.quantity() - filled;
|
|
avgprx = state.avg_price();;
|
|
fstatus = translate_status(state, &native);
|
|
}
|
|
|
|
md::SecurityPtr DropCopySession::resolve_execution_report_security(md::SecurityPtr sec, FIX44::ExecutionReport& report)
|
|
{
|
|
if(!sec)
|
|
return nullptr;
|
|
auto sectype = bts::admin::AdminServiceUtils::security_type(*sec);
|
|
report.setField(FIX::FIELD::SecurityExchange, convert_exchange(sec->exchange()).getString());
|
|
report.setField(FIX::FIELD::SecurityType, convert_security_type(sectype).getString());
|
|
if(sec->has_glbx() || sec->has_ice())
|
|
report.setField(FIX::FIELD::SecurityDesc, securities::descriptor(*sec));
|
|
|
|
if(sectype == bts::api::SecurityType::SECURITYTYPE_FUTURE)
|
|
{
|
|
auto dt = securities::expiration(*sec);
|
|
report.setField(FIX::FIELD::MaturityMonthYear, boost::gregorian::to_iso_string(dt));
|
|
}
|
|
else if(sectype == bts::api::SecurityType::SECURITYTYPE_SPREAD)
|
|
{
|
|
//TODO this is not quite right for spreads of spreads
|
|
auto dt = securities::expiration(*sec); //smallest
|
|
report.setField(FIX::FIELD::MaturityMonthYear, boost::gregorian::to_iso_string(dt));
|
|
}
|
|
else
|
|
throw FIX::UnsupportedMessageType();
|
|
return sec;
|
|
}
|
|
|
|
FIX44::ExecutionReport DropCopySession::build_fill(const FIX::SessionID& sess,
|
|
const bts::api::orderman::Fill& fill, bool change)
|
|
{
|
|
UNUSED(sess);
|
|
auto exec = FIX::ExecType(FIX::ExecType_PARTIAL_FILL);
|
|
FIX::OrdStatus status;
|
|
uint filled = 0; uint leaves = 0; double prx = 0.0;
|
|
api::orderman::NativeOrderState native;
|
|
|
|
_order_cache.lock([&] (auto& order_cache)
|
|
{
|
|
auto orderit = order_cache.find(fill.order_key());
|
|
if(orderit != order_cache.end())
|
|
{
|
|
const api::orderman::OrderRecord& order = *orderit->second;
|
|
process_order_state(order.state(), filled, leaves, prx, status, native);
|
|
if( ((double)order.state().quantity()) == fill.last_shares())
|
|
exec = FIX::ExecType(FIX::ExecType_FILL);
|
|
}
|
|
});
|
|
if (change)
|
|
exec = FIX::ExecType(FIX::ExecType_TRADE_CORRECT);
|
|
|
|
auto sec = _router.lookup(fill.security_key());
|
|
FIX44::ExecutionReport report (
|
|
FIX::OrderID(std::to_string(fill.order_key())),
|
|
FIX::ExecID(fill.fill_key()),
|
|
exec,
|
|
status,
|
|
FIX::Side(fill.side() == bts::api::SIDE_BUY ? FIX::Side_BUY : FIX::Side_SELL),
|
|
FIX::LeavesQty(leaves),
|
|
FIX::CumQty(filled),
|
|
FIX::AvgPx(prx)
|
|
);
|
|
|
|
if(sec)
|
|
report.setField(FIX::FIELD::Symbol, sec->symbol());
|
|
report.setField(FIX::FIELD::Account, FIX::Account(fill.account()).getString() );
|
|
report.setField(FIX::FIELD::LastQty, FIX::LastQty(fill.last_shares()).getString() );
|
|
report.setField(FIX::FIELD::LastPx, FIX::LastPx(fill.last_price()).getString() );
|
|
|
|
auto ts = fill.has_timestamp() ? fill.timestamp() : google::protobuf::Timestamp();
|
|
//report.setField(FIX::FIELD::TransactTime, FIX::TransactTime(ts, true).getString());
|
|
//report.setField(FIX::FIELD::TradeDate, FIX::TradeDate(ts.ToString("yyyyMMdd"));
|
|
report.setField(FIX::FIELD::TransactTime, google::protobuf::util::TimeUtil::ToString(ts));
|
|
report.setField(FIX::FIELD::TradeDate, google::protobuf::util::TimeUtil::ToString(ts));
|
|
|
|
sec = resolve_execution_report_security(sec, report);
|
|
|
|
if (native.exchange_order_id() != "")
|
|
report.setField(FIX::FIELD::SecondaryOrderID, FIX::SecondaryOrderID(native.exchange_order_id()).getString());
|
|
|
|
if (sec && (bts::admin::AdminServiceUtils::security_type(*sec) == bts::api::SECURITYTYPE_SPREAD ||
|
|
bts::admin::AdminServiceUtils::security_type(*sec) == bts::api::SECURITYTYPE_SYNTHETIC) )
|
|
{
|
|
if (fill.fill_kind() == bts::api::orderman::Fill::FILL_KIND_LEG)
|
|
report.setField(FIX::FIELD::MultiLegReportingType, FIX::MultiLegReportingType('2').getString());
|
|
else
|
|
report.setField(FIX::FIELD::MultiLegReportingType, FIX::MultiLegReportingType('3').getString());
|
|
}
|
|
return report;
|
|
|
|
}
|
|
|
|
FIX44::ExecutionReport DropCopySession::build_order_status(const FIX::SessionID& sess,
|
|
const bts::api::orderman::OrderRecord& order, bool change )
|
|
{
|
|
UNUSED(sess);
|
|
UNUSED(change);
|
|
auto exec = FIX::ExecType(FIX::ExecType_NEW);
|
|
_order_cache.lock( [&exec, &order] (auto& order_cache)
|
|
{
|
|
auto it = order_cache.find(order.key());
|
|
if(it != order_cache.end())
|
|
exec = FIX::ExecType(FIX::ExecType_ORDER_STATUS);
|
|
});
|
|
|
|
FIX::OrdStatus status;
|
|
uint filled = 0; uint leaves = 0; double prx = 0.0;
|
|
api::orderman::NativeOrderState native;
|
|
process_order_state(order.state(), filled, leaves, prx, status, native);
|
|
|
|
auto sec = _router.lookup(order.def().security_key());
|
|
FIX44::ExecutionReport report(
|
|
FIX::OrderID(std::to_string(order.key())),
|
|
FIX::ExecID("0"),
|
|
exec,
|
|
status,
|
|
FIX::Side(order.def().side() == bts::api::SIDE_BUY ? FIX::Side_BUY : FIX::Side_SELL),
|
|
FIX::LeavesQty(leaves),
|
|
FIX::CumQty(filled),
|
|
FIX::AvgPx(prx));
|
|
|
|
if(sec)
|
|
report.setField(FIX::Symbol(sec->symbol()));
|
|
report.setField(FIX::FIELD::Account, FIX::Account(order.def().account()).getString() );
|
|
report.setField(FIX::FIELD::OrderQty, FIX::OrderQty(order.state().quantity()).getString() );
|
|
report.setField(FIX::FIELD::Price, FIX::Price(order.state().price()).getString() );
|
|
report.setField(FIX::FIELD::TimeInForce, FIX::TimeInForce((char)order.def().time_in_force()).getString() );
|
|
auto ts = order.state().last_change();
|
|
report.setField(FIX::FIELD::TransactTime, google::protobuf::util::TimeUtil::ToString(ts));
|
|
|
|
if(order.state().last_msg() != "")
|
|
report.setField(FIX::FIELD::Text, FIX::Text(order.state().last_msg()));
|
|
sec = resolve_execution_report_security(sec, report);
|
|
if (native.exchange_order_id() != "")
|
|
{
|
|
report.setField(FIX::FIELD::OrdType, FIX::OrdType((char)native.native_type()).getString());;
|
|
report.setField(FIX::FIELD::SecondaryOrderID, FIX::SecondaryOrderID(native.exchange_order_id()).getString());;
|
|
}
|
|
//TODO Big race here, we need a correlation id of some sort
|
|
_fix_clorderids.lock([&order, &report] (auto& fix_clorderids)
|
|
{
|
|
auto fixit = fix_clorderids.find(order.key());
|
|
if(fixit != fix_clorderids.end())
|
|
report.setField(FIX::FIELD::ClOrdID, FIX::ClOrdID(fixit->second).getString());
|
|
});
|
|
return report;
|
|
|
|
}
|
|
|
|
|
|
|
|
}}
|