rx-cpp requires concurrent use with prescription medicine

This commit is contained in:
Kevin Darby committed 2019-09-27 14:29:22 -04:00
1 parent d23a072ad6
commit 9850d6bde0
3 files changed
+181 -39

No files matched your search

+174 -35
View File
@@ -1,6 +1,8 @@
#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"
@@ -10,6 +12,10 @@
#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 {
@@ -90,63 +96,196 @@ void DropCopySession::onMessage(const FIX44::SecurityDefinitionRequest& req, con
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();
// auto filter = rxcpp::observable<>::create([] (auto& observer)
// {
// return _adminobservable.subscribe([] (auto& x)
// {
// auto filter = [&x] (auto& z)
// {
// return x.can_view_account(z.def().account());
// };
// observer.on_next(filter);
// });
// });
auto orders = _orderservice.subscribe_orders() |
rxcpp::operators::buffer_with_time( std::chrono::milliseconds(500) );
auto admin = _adminobservable.combine_latest(orders)
.filter( [] (auto& tuple)
auto&& filtered_orders = _adminobservable.combine_latest(_orderservice.subscribe_orders())
.map( [] (auto& tuple) -> auto
{
return true;
});
// rxcpp::operators::combine_latest([] (auto& admin, auto& order)
// {
// return std::make_pair(admin, order);
// }, orders);
// rxcpp::operators::filter([] (auto& pair)
// {
// return pair.first->can_view_account(pair.second.def().account());
// //return true;
// });
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) );
admin.subscribe([this](const auto& update)
filtered_orders.subscribe([this, &sessionID](const auto& updatevec)
{
std::lock_guard<std::recursive_mutex> l(_lock);
});
for(auto& update : updatevec)
{
for(auto& order : update.added())
{
auto&& ostat = build_order_status(sessionID, *order);
_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);
}
}
});
auto&& 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) );
filtered_fills.subscribe([this, &sessionID](const auto& updatevec)
{
std::lock_guard<std::recursive_mutex> l(_lock);
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,
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)
{
filled = state.filled_quantity();
leaves = state.quantity() - filled;
avgprx = state.avg_price();;
fstatus = translate_status(state, &native);
}
const bts::api::Security* DropCopySession::resolve_execution_report_security(const bts::api::Security* sec, FIX44::ExecutionReport& report)
md::SecurityPtr DropCopySession::resolve_execution_report_security(md::SecurityPtr sec, FIX44::ExecutionReport& report)
{
return nullptr;
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)
{
return FIX44::ExecutionReport();
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;
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,