Merge branch 'fix' of bitbucket.org:bluetradesys/spark into vwap
This commit is contained in:
commit
cefb2b2524
70 files changed
+92400
-63
No files matched your search
@@ -18,6 +18,11 @@ target_include_directories( sparkd PUBLIC ${CMAKE_SOURCE_DIR}/src ${CMAKE_BINARY
|
||||
|
||||
set_source_files_properties(exchange_sim/ExchangeSim.cpp PROPERTIES COMPILE_FLAGS --std=c++14)
|
||||
set_source_files_properties(exchange_sim/ExchangeSimApplication.cpp PROPERTIES COMPILE_FLAGS --std=c++14)
|
||||
set_source_files_properties(fix/FixInit.cpp PROPERTIES COMPILE_FLAGS --std=c++14)
|
||||
set_source_files_properties(fix/FixRouter.cpp PROPERTIES COMPILE_FLAGS --std=c++14)
|
||||
set_source_files_properties(fix/SessionBase.cpp PROPERTIES COMPILE_FLAGS --std=c++14)
|
||||
set_source_files_properties(fix/OrderSession.cpp PROPERTIES COMPILE_FLAGS --std=c++14)
|
||||
set_source_files_properties(fix/DropCopySession.cpp PROPERTIES COMPILE_FLAGS --std=c++14)
|
||||
|
||||
# linking
|
||||
target_link_libraries( sparkd
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
#include "lib/api/public/ServiceContext.h"
|
||||
#include "lib/config/config.h"
|
||||
#include "lib/config/protos/Ports.pb.h"
|
||||
#include "lib/config/protos/fix.pb.h"
|
||||
#include "lib/config/protos/quickbus_cfg.pb.h"
|
||||
#include "lib/logging/logging.h"
|
||||
#include "lib/os/os.h"
|
||||
@@ -38,11 +39,13 @@
|
||||
#include "server/sparkd/support/ShellLatencyServer.h"
|
||||
#include "server/sparkd/user/AdminServiceServer.h"
|
||||
#include "server/sparkd/user/AuthServiceServer.h"
|
||||
#include "server/sparkd/fix/FixInit.h"
|
||||
|
||||
#include "lib/orderman/orders/algo/algo_orders/ProRataAlgo.h"
|
||||
#include "lib/orderman/orders/algo/algo_orders/ScratchAlgo.h"
|
||||
|
||||
#include <thread>
|
||||
#include <algorithm>
|
||||
|
||||
namespace bts {
|
||||
namespace cmd {
|
||||
@@ -104,6 +107,12 @@ int Run::execute(std::vector<std::string> const &) {
|
||||
position::MarginServiceServer margin_service(sc, mdclient);
|
||||
metrics::ShellLatencyServer shell_latency_service(
|
||||
sc, stats, utils::Dispatcher::background());
|
||||
|
||||
if(config::get<config::Fix>().enable_acceptor())
|
||||
{
|
||||
threading::make_thread("SparkFix", [&sc, &mdclient]() { fix::FixInit::start_acceptor(sc, mdclient); })
|
||||
.detach();
|
||||
}
|
||||
|
||||
auto bg_health =
|
||||
std::make_shared<metrics::IntCounterMetric>("spark_background_thread");
|
||||
|
||||
@@ -0,0 +1,41 @@
|
||||
#include "RunFix.h"
|
||||
#include "server/sparkd/fix/FixInit.h"
|
||||
|
||||
#include "lib/api/public/ServiceContext.h"
|
||||
#include "lib/config/config.h"
|
||||
#include "lib/config/protos/Ports.pb.h"
|
||||
#include "lib/config/protos/quickbus_cfg.pb.h"
|
||||
#include "lib/logging/logging.h"
|
||||
#include "lib/os/os.h"
|
||||
#include "lib/quickbus/quickbus.h"
|
||||
#include "lib/utils/Dispatcher.h"
|
||||
#include "lib/utils/thread_manager.h"
|
||||
|
||||
#include "lib/config/protos/toggles.pb.h"
|
||||
#include "lib/md/SyntheticSecurityService.h"
|
||||
#include "lib/md/MDClient.h"
|
||||
|
||||
|
||||
namespace bts {
|
||||
namespace cmd {
|
||||
|
||||
int RunFix::execute(std::vector<std::string> const &)
|
||||
{
|
||||
config::QuickbusNodeCfg qb_cfg;
|
||||
auto &ports = config::get<config::Ports>();
|
||||
qb_cfg.set_my_port(ports.qbus_port());
|
||||
qbus::Quickbus quickbus(qb_cfg,
|
||||
qbus::Quickbus::ThreadOptions::deferred_start);
|
||||
api::ServiceContext sc(&quickbus);
|
||||
|
||||
api::synthetic::SyntheticSecurityService synthetic_service(sc);
|
||||
md::MDClient mdclient(&utils::Dispatcher::background(), &synthetic_service);
|
||||
|
||||
threading::make_thread("SparkFix", [&sc, &mdclient]() { fix::FixInit::start_acceptor(sc, mdclient); })
|
||||
.detach();
|
||||
return 0;
|
||||
}
|
||||
|
||||
std::string RunFix::help () {return "\t run fix acceptor";}
|
||||
|
||||
}}
|
||||
@@ -0,0 +1,17 @@
|
||||
#pragma once
|
||||
|
||||
#include "ICmd.h"
|
||||
|
||||
namespace bts {
|
||||
namespace cmd {
|
||||
|
||||
class RunFix : public ICmd
|
||||
{
|
||||
public:
|
||||
int execute(std::vector<std::string> const &args);
|
||||
std::string help ();
|
||||
};
|
||||
|
||||
}}
|
||||
|
||||
|
||||
@@ -0,0 +1,38 @@
|
||||
#include "AdminModelWrapper.h"
|
||||
|
||||
#include "lib/api/service_helpers/AdminServiceUtils.h"
|
||||
|
||||
#include <string>
|
||||
|
||||
namespace bts
|
||||
{
|
||||
|
||||
AdminModelWrapper::AdminModelWrapper(const bts::api::admin::AdminData& data, const std::string& user_name) :
|
||||
data(data),
|
||||
_user_name(user_name),
|
||||
_allowed_accounts()
|
||||
{
|
||||
auto user = bts::admin::AdminServiceUtils::find_user(data, _user_name);
|
||||
if(user && user->has_user_data() && user->user_data().has_account_permissions())
|
||||
{
|
||||
for(const auto& a : user->user_data().account_permissions().account_permissions())
|
||||
{
|
||||
auto account = bts::admin::AdminServiceUtils::find_account(data, a.first);
|
||||
if(account)
|
||||
{
|
||||
if(a.second.has_trading_permission() && a.second.trading_permission().has_permissions())
|
||||
_allowed_accounts[account->name()] = 0;
|
||||
if(a.second.has_admin_permission() && a.second.admin_permission().has_permissions())
|
||||
_allowed_accounts[account->name()] = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bool AdminModelWrapper::can_view_account(const std::string& account)
|
||||
{
|
||||
auto it = _allowed_accounts.find(account);
|
||||
return it != _allowed_accounts.end() || _user_name == "bts";
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
#pragma once
|
||||
|
||||
#include "lib/api/services/AdminService.pb.h"
|
||||
#include "lib/api/services/AdminService.client.pb.h"
|
||||
|
||||
#include <string>
|
||||
#include <unordered_map>
|
||||
|
||||
namespace bts
|
||||
{
|
||||
|
||||
//see AdminModelWrapper.cs
|
||||
|
||||
namespace api { namespace admin { class AdminData; } }
|
||||
class AdminModelWrapper
|
||||
{
|
||||
public:
|
||||
AdminModelWrapper(const bts::api::admin::AdminData& data, const std::string& user_name);
|
||||
|
||||
bool can_view_account(const std::string& account);
|
||||
const bts::api::admin::AdminData data;
|
||||
|
||||
private:
|
||||
std::string _user_name;
|
||||
std::unordered_map<std::string, int> _allowed_accounts;
|
||||
};
|
||||
|
||||
}
|
||||
@@ -0,0 +1,374 @@
|
||||
#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;
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
}}
|
||||
@@ -0,0 +1,61 @@
|
||||
#pragma once
|
||||
|
||||
#include "SessionBase.h"
|
||||
#include "lib/md/SecurityPtr.h"
|
||||
|
||||
#include <quickfix/Message.h>
|
||||
#include <quickfix/SessionID.h>
|
||||
|
||||
#include <string>
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <memory>
|
||||
#include <unordered_map>
|
||||
|
||||
namespace FIX44
|
||||
{
|
||||
class OrderStatusRequest;
|
||||
class SecurityDefinitionRequest;
|
||||
}
|
||||
|
||||
namespace bts {
|
||||
|
||||
namespace fix {
|
||||
|
||||
class DropCopySession : public SessionBase
|
||||
{
|
||||
public:
|
||||
DropCopySession(bts::api::ServiceContext&, IFixRouter&, const FIX::SessionID&);
|
||||
virtual ~DropCopySession();
|
||||
|
||||
void onLogon(const FIX::SessionID&);
|
||||
void onLogout(const FIX::SessionID&);
|
||||
|
||||
void fromApp(const FIX::Message &message,const FIX::SessionID &sessionID)
|
||||
throw(FIX::FieldNotFound, FIX::IncorrectDataFormat, FIX::IncorrectTagValue, FIX::UnsupportedMessageType);
|
||||
|
||||
void onMessage(const FIX44::OrderStatusRequest& m, const FIX::SessionID& s);
|
||||
void onMessage(const FIX44::SecurityDefinitionRequest& m, const FIX::SessionID& s);
|
||||
|
||||
protected:
|
||||
virtual void subscribe_fills_and_orders(const FIX::SessionID sessionID);
|
||||
virtual void 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 = nullptr);
|
||||
|
||||
md::SecurityPtr resolve_execution_report_security(md::SecurityPtr sec, FIX44::ExecutionReport& report);
|
||||
virtual FIX44::ExecutionReport build_fill(const FIX::SessionID& sess, const bts::api::orderman::Fill& fill, bool change = false);
|
||||
virtual FIX44::ExecutionReport build_order_status(const FIX::SessionID& sess, const bts::api::orderman::OrderRecord& order, bool change = false);
|
||||
|
||||
protected:
|
||||
bts::api::orderman::OrderService _orderservice;
|
||||
|
||||
utils::LockBox< std::unordered_map<uint, std::shared_ptr<const bts::api::orderman::OrderRecord>> > _order_cache;
|
||||
utils::LockBox< std::unordered_map<uint, std::string> > _fix_clorderids;
|
||||
|
||||
private:
|
||||
struct PImpl;
|
||||
std::shared_ptr<PImpl> me;
|
||||
|
||||
};
|
||||
|
||||
}}
|
||||
@@ -0,0 +1,74 @@
|
||||
#include "FixInit.h"
|
||||
|
||||
#include "IFixSession.h"
|
||||
#include "FixRouter.h"
|
||||
#include "OrderSession.h"
|
||||
#include "DropCopySession.h"
|
||||
#include "lib/config/config.h"
|
||||
#include "lib/config/protos/fix.pb.h"
|
||||
#include "lib/os/os.h"
|
||||
#include "lib/utils/bts_assert.h"
|
||||
#include "lib/utils/bts_chrono.h"
|
||||
#include "lib/config/protos/logging.pb.h"
|
||||
#include "lib/utils/file_utils.h"
|
||||
#include "lib/utils/helper_macros.h"
|
||||
|
||||
#include <quickfix/FileStore.h>
|
||||
#include <quickfix/SocketAcceptor.h>
|
||||
#include <quickfix/SessionSettings.h>
|
||||
#include <quickfix/FileLog.h>
|
||||
#include <quickfix/Message.h>
|
||||
#include <quickfix/SessionID.h>
|
||||
|
||||
#include <boost/algorithm/string/replace.hpp>
|
||||
|
||||
#include <string>
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <memory>
|
||||
|
||||
namespace bts
|
||||
{
|
||||
namespace fix
|
||||
{
|
||||
|
||||
boost::filesystem::path fix_path ()
|
||||
{
|
||||
auto store_root = os::bts_data_path();
|
||||
std::string base_path = store_root;
|
||||
boost::filesystem::path result = utils::normalize_data_path (base_path);
|
||||
result /= "fix";
|
||||
return result;
|
||||
}
|
||||
|
||||
int FixInit::start_acceptor(bts::api::ServiceContext& sc, md::MDClient& md)
|
||||
{
|
||||
std::string settings_file = os::sparkd_wd() + "/data_files/fix/";
|
||||
settings_file += config::get<config::Fix>().fix_config_file();
|
||||
bool orders_allowed = config::get<config::Fix>().allow_orders();
|
||||
|
||||
std::ifstream str{settings_file};
|
||||
FIX::SessionSettings settings( str );
|
||||
auto sess = settings.getSessions();
|
||||
int scount = sess.size();
|
||||
UNUSED(scount);
|
||||
|
||||
FixRouter application(md, [&sc, orders_allowed](IFixRouter& router, const auto& id)
|
||||
{
|
||||
if(orders_allowed)
|
||||
return std::shared_ptr<IFixSession>(new OrderSession(sc, router, id));
|
||||
else
|
||||
return std::shared_ptr<IFixSession>(new DropCopySession(sc, router, id));
|
||||
});
|
||||
FIX::FileStoreFactory storeFactory( settings );
|
||||
FIX::FileLogFactory logFactory( settings );
|
||||
FIX::SocketAcceptor acceptor( application, storeFactory, settings, logFactory );
|
||||
|
||||
acceptor.start();
|
||||
utils::Dispatcher::current().run();
|
||||
acceptor.stop();
|
||||
return 0;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
|
||||
namespace bts {
|
||||
namespace api { class ServiceContext; }
|
||||
namespace md { class MDClient; }
|
||||
namespace fix {
|
||||
|
||||
class FixInit
|
||||
{
|
||||
public:
|
||||
static int start_acceptor(bts::api::ServiceContext& sc, md::MDClient& md);
|
||||
};
|
||||
|
||||
}}
|
||||
@@ -0,0 +1,240 @@
|
||||
#include "FixRouter.h"
|
||||
|
||||
#include "IFixSession.h"
|
||||
#include "SecurityIndex.h"
|
||||
#include "lib/md/MDConsumerHandle.h"
|
||||
#include "lib/utils/LockBox.h"
|
||||
|
||||
#include <quickfix/FileStore.h>
|
||||
#include <quickfix/SocketAcceptor.h>
|
||||
#include <quickfix/SessionSettings.h>
|
||||
#include <quickfix/FileLog.h>
|
||||
#include <quickfix/Message.h>
|
||||
#include <quickfix/SessionID.h>
|
||||
#include <quickfix/fix44/Reject.h>
|
||||
#include <quickfix/fix44/BusinessMessageReject.h>
|
||||
|
||||
#include <boost/algorithm/string/replace.hpp>
|
||||
|
||||
#include <string>
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <memory>
|
||||
|
||||
namespace bts
|
||||
{
|
||||
namespace fix
|
||||
{
|
||||
|
||||
struct FixRouter::PImpl
|
||||
{
|
||||
utils::LockBox< boost::container::flat_map< FIX::SessionID, std::shared_ptr<IFixSession> > > sessions;
|
||||
SecurityIndex index;
|
||||
std::mutex lock;
|
||||
Factory factory;
|
||||
|
||||
PImpl(md::MDClient& md, Factory factory) :
|
||||
sessions(),
|
||||
index(md),
|
||||
lock(),
|
||||
factory(factory)
|
||||
{
|
||||
}
|
||||
};
|
||||
|
||||
FixRouter::FixRouter(md::MDClient& md, Factory factory) : me(new PImpl(md, factory))
|
||||
{
|
||||
}
|
||||
|
||||
FixRouter::~FixRouter()
|
||||
{
|
||||
}
|
||||
|
||||
void FixRouter::send(FIX::Message& mess, const FIX::SessionID& sess)
|
||||
{
|
||||
try
|
||||
{
|
||||
std::lock_guard<std::mutex> guard(me->lock);
|
||||
FIX::Session::sendToTarget(mess, sess);
|
||||
}
|
||||
catch(FIX::SessionNotFound)
|
||||
{
|
||||
bts_assert_msg(false, "session not found " + sess.toString());
|
||||
}
|
||||
catch(std::exception ex)
|
||||
{
|
||||
LOG(Fix, Error, "session error {} {}", sess.toString(), std::string(ex.what()));
|
||||
}
|
||||
}
|
||||
|
||||
void FixRouter::send_session_reject(const FIX::SessionID& sess, const std::string& text, const FIX::Message* refmessage)
|
||||
{
|
||||
FIX44::Reject rej;
|
||||
if(refmessage && refmessage->getHeader().isSetField(FIX::FIELD::MsgSeqNum))
|
||||
rej.setField(FIX::FIELD::RefSeqNum, refmessage->getHeader().getField(FIX::FIELD::MsgSeqNum));
|
||||
rej.setField(FIX::FIELD::Text, text);
|
||||
send(rej, sess);
|
||||
}
|
||||
|
||||
void FixRouter::send_biz_reject(const FIX::SessionID sess, const std::string& text, const FIX::Message& refmessage, int reason_code)
|
||||
{
|
||||
FIX44::BusinessMessageReject rej;
|
||||
rej.setField(FIX::FIELD::RefMsgType, refmessage.getHeader().getField(FIX::FIELD::MsgType));
|
||||
rej.setField(FIX::FIELD::RefSeqNum, refmessage.getHeader().getField(FIX::FIELD::MsgSeqNum));
|
||||
rej.setField(FIX::FIELD::BusinessRejectReason, std::to_string(reason_code));
|
||||
rej.setField(FIX::FIELD::Text, text);
|
||||
send(rej, sess);
|
||||
}
|
||||
|
||||
void FixRouter::fromAdmin(const FIX::Message& message, const FIX::SessionID& sessionID)
|
||||
throw(FIX::FieldNotFound, FIX::IncorrectDataFormat, FIX::IncorrectTagValue, FIX::RejectLogon)
|
||||
{
|
||||
try
|
||||
{
|
||||
session(sessionID).fromAdmin(message, sessionID);
|
||||
}
|
||||
catch (FIX::FieldNotFound ex) { handle_field_not_found(ex, sessionID, message); }
|
||||
catch (FIX::UnsupportedMessageType) { throw; }
|
||||
catch (FIX::IncorrectTagValue) { throw; }
|
||||
catch (std::exception ex) { handle_session_exception(ex, sessionID, &message); }
|
||||
}
|
||||
|
||||
void FixRouter::fromApp(const FIX::Message &message,const FIX::SessionID &sessionID)
|
||||
throw(FIX::FieldNotFound, FIX::IncorrectDataFormat, FIX::IncorrectTagValue, FIX::UnsupportedMessageType)
|
||||
{
|
||||
try
|
||||
{
|
||||
session(sessionID).fromApp(message, sessionID);
|
||||
}
|
||||
catch (FIX::FieldNotFound ex) { handle_field_not_found(ex, sessionID, message); }
|
||||
catch (FIX::UnsupportedMessageType) { throw; }
|
||||
catch (FIX::IncorrectTagValue) { throw; }
|
||||
catch (std::exception ex) { handle_session_exception(ex, sessionID, &message); }
|
||||
}
|
||||
|
||||
void FixRouter::onCreate(const FIX::SessionID& sessionID)
|
||||
{
|
||||
try
|
||||
{
|
||||
session(sessionID).onCreate(sessionID);
|
||||
}
|
||||
catch(std::exception ex)
|
||||
{
|
||||
handle_session_exception(ex, sessionID);
|
||||
}
|
||||
}
|
||||
|
||||
void FixRouter::onLogon(const FIX::SessionID& sessionID)
|
||||
{
|
||||
try
|
||||
{
|
||||
session(sessionID).onLogon(sessionID);
|
||||
}
|
||||
catch (std::exception ex)
|
||||
{
|
||||
handle_session_exception(ex, sessionID);
|
||||
}
|
||||
}
|
||||
|
||||
void FixRouter::onLogout(const FIX::SessionID& sessionID)
|
||||
{
|
||||
try
|
||||
{
|
||||
//TODO stackoverflow?
|
||||
session(sessionID).onLogout(sessionID);
|
||||
}
|
||||
catch (std::exception ex)
|
||||
{
|
||||
handle_session_exception(ex, sessionID);
|
||||
}
|
||||
}
|
||||
|
||||
void FixRouter::toAdmin(FIX::Message& message, const FIX::SessionID& sessionID)
|
||||
{
|
||||
try
|
||||
{
|
||||
session(sessionID).toAdmin(message, sessionID);
|
||||
}
|
||||
catch (FIX::FieldNotFound ex) { handle_field_not_found(ex, sessionID, message); }
|
||||
catch (FIX::UnsupportedMessageType) { throw; }
|
||||
catch (FIX::IncorrectTagValue) { throw; }
|
||||
catch (std::exception ex) { handle_session_exception(ex, sessionID, &message); }
|
||||
}
|
||||
|
||||
void FixRouter::toApp(FIX::Message& message, const FIX::SessionID& sessionID) throw(FIX::DoNotSend)
|
||||
{
|
||||
try
|
||||
{
|
||||
session(sessionID).toApp(message, sessionID);
|
||||
}
|
||||
catch (FIX::FieldNotFound ex) { handle_field_not_found(ex, sessionID, message); }
|
||||
catch (FIX::UnsupportedMessageType) { throw FIX::DoNotSend() ; }
|
||||
catch (FIX::IncorrectTagValue) { throw FIX::DoNotSend() ; }
|
||||
catch (std::exception ex) { handle_session_exception(ex, sessionID, &message); }
|
||||
}
|
||||
|
||||
IFixSession& FixRouter::session(const FIX::SessionID& sid)
|
||||
{
|
||||
auto ptr = me->sessions.lock([this, sid] (auto& sessions)
|
||||
{
|
||||
auto it = sessions.find(sid);
|
||||
if(it == sessions.end())
|
||||
{
|
||||
auto ptr = me->factory(*this, sid);
|
||||
sessions[sid] = ptr;
|
||||
return ptr;
|
||||
}
|
||||
else
|
||||
return it->second;
|
||||
});
|
||||
return *ptr;
|
||||
}
|
||||
|
||||
void FixRouter::handle_session_exception(const std::exception& ex, const FIX::SessionID& sessionID, const FIX::Message* message)
|
||||
{
|
||||
std::string msg = "==ERROR in session " + sessionID.toString() + "== ";
|
||||
msg += (message ? message->getHeader().getField(FIX::FIELD::MsgType) : "");
|
||||
msg += std::string(ex.what());
|
||||
|
||||
LOG(Fix, Error, msg.c_str());
|
||||
//now shut this fucker down
|
||||
try
|
||||
{
|
||||
auto sess = FIX::Session::lookupSession(sessionID);
|
||||
if(sess != nullptr)
|
||||
sess->disconnect();
|
||||
}
|
||||
catch(...) { }
|
||||
}
|
||||
|
||||
void FixRouter::handle_field_not_found(const FIX::FieldNotFound& ex, const FIX::SessionID& sess, const FIX::Message& message)
|
||||
{
|
||||
if(ex.field > 0)
|
||||
{
|
||||
std::string text = "Field " + std::to_string(ex.field) + " was expected but not found";
|
||||
send_biz_reject(sess, text, message, 5 );
|
||||
}
|
||||
//QF does not include the tag number....
|
||||
//throw new FieldNotFoundException($"Field {ex.Field} was expected but not found", ex);
|
||||
else
|
||||
throw ex;
|
||||
}
|
||||
|
||||
std::vector<md::SecurityPtr> FixRouter::get_securities()
|
||||
{
|
||||
return me->index.get_securities();
|
||||
}
|
||||
|
||||
const md::SecurityPtr FixRouter::lookup(md::security_key key)
|
||||
{
|
||||
return me->index.lookup(key);
|
||||
}
|
||||
|
||||
const md::SecurityPtr FixRouter::lookup(const std::string& desc)
|
||||
{
|
||||
return me->index.lookup(desc);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
#pragma once
|
||||
|
||||
#include "IFixSession.h"
|
||||
#include "IFixRouter.h"
|
||||
|
||||
#include <quickfix/MessageCracker.h>
|
||||
#include <quickfix/Message.h>
|
||||
#include <quickfix/SessionID.h>
|
||||
|
||||
#include <boost/container/flat_map.hpp>
|
||||
#include <string>
|
||||
#include <memory>
|
||||
#include <mutex>
|
||||
#include <functional>
|
||||
|
||||
|
||||
namespace bts {
|
||||
namespace api { class ServiceContext; }
|
||||
namespace md { class MDClient; }
|
||||
namespace fix {
|
||||
|
||||
class FixRouter : public IFixSession, public FIX::MessageCracker, public IFixRouter
|
||||
{
|
||||
public:
|
||||
typedef std::function< std::shared_ptr<IFixSession> (IFixRouter&, const FIX::SessionID&) > Factory;
|
||||
|
||||
FixRouter(md::MDClient&, Factory factory);
|
||||
~FixRouter();
|
||||
|
||||
void send(FIX::Message&, const FIX::SessionID&) override;
|
||||
void send_session_reject(const FIX::SessionID& sess, const std::string& text, const FIX::Message* refmessage = nullptr) override;
|
||||
void send_biz_reject(const FIX::SessionID sess, const std::string& text, const FIX::Message& refmessage, int reason_code = 0) override;
|
||||
|
||||
public:
|
||||
void fromAdmin(const FIX::Message& message, const FIX::SessionID& sessionID)
|
||||
throw(FIX::FieldNotFound, FIX::IncorrectDataFormat, FIX::IncorrectTagValue, FIX::RejectLogon);
|
||||
void fromApp(const FIX::Message &message,const FIX::SessionID &sessionID)
|
||||
throw(FIX::FieldNotFound, FIX::IncorrectDataFormat, FIX::IncorrectTagValue, FIX::UnsupportedMessageType);
|
||||
void toAdmin(FIX::Message&, const FIX::SessionID&);
|
||||
void toApp(FIX::Message&, const FIX::SessionID&) throw(FIX::DoNotSend);
|
||||
|
||||
void onCreate(const FIX::SessionID&);
|
||||
void onLogon(const FIX::SessionID&);
|
||||
void onLogout(const FIX::SessionID&);
|
||||
|
||||
private:
|
||||
IFixSession& session(const FIX::SessionID&);
|
||||
void handle_session_exception(const std::exception& ex, const FIX::SessionID& sessionID, const FIX::Message* message = nullptr);
|
||||
void handle_field_not_found(const FIX::FieldNotFound& ex, const FIX::SessionID& sess, const FIX::Message& message);
|
||||
|
||||
private:
|
||||
std::vector<md::SecurityPtr> get_securities() override;
|
||||
const md::SecurityPtr lookup(md::security_key key) override;
|
||||
const md::SecurityPtr lookup(const std::string& desc) override;
|
||||
|
||||
private:
|
||||
struct PImpl;
|
||||
std::unique_ptr<PImpl> me;
|
||||
};
|
||||
|
||||
}}
|
||||
@@ -0,0 +1,37 @@
|
||||
#pragma once
|
||||
|
||||
#include "lib/md/common.h"
|
||||
#include "lib/md/SecurityPtr.h"
|
||||
|
||||
#include <vector>
|
||||
#include <memory>
|
||||
|
||||
namespace FIX
|
||||
{
|
||||
class Message;
|
||||
class SessionID;
|
||||
}
|
||||
|
||||
namespace bts
|
||||
{
|
||||
|
||||
namespace api
|
||||
{
|
||||
class Security;
|
||||
}
|
||||
|
||||
namespace fix
|
||||
{
|
||||
class IFixRouter
|
||||
{
|
||||
public:
|
||||
virtual std::vector<md::SecurityPtr> get_securities() = 0;
|
||||
virtual const md::SecurityPtr lookup(md::security_key key) = 0;
|
||||
virtual const md::SecurityPtr lookup(const std::string& desc) = 0;
|
||||
|
||||
virtual void send(FIX::Message& mess, const FIX::SessionID& sess) = 0;
|
||||
virtual void send_session_reject(const FIX::SessionID& sess, const std::string& text, const FIX::Message* refmessage = nullptr) = 0;
|
||||
virtual void send_biz_reject(const FIX::SessionID sess, const std::string& text, const FIX::Message& refmessage, int reason_code = 0) = 0;
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
#pragma once
|
||||
|
||||
#include "lib/utils/warn_ctrl.h"
|
||||
GCC_DIAG_OFF("-Woverloaded-virtual")
|
||||
GCC_DIAG_OFF("-Wdeprecated")
|
||||
GCC_DIAG_OFF("-Wignored-qualifiers")
|
||||
//GCC_DIAG_OFF("-Wunused-parameter")
|
||||
|
||||
#include <quickfix/Application.h>
|
||||
|
||||
namespace bts {
|
||||
namespace fix {
|
||||
|
||||
class IFixSession : public FIX::Application
|
||||
{
|
||||
};
|
||||
|
||||
}}
|
||||
@@ -0,0 +1,226 @@
|
||||
#include "OrderSession.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/OrderCancelRequest.h>
|
||||
#include <quickfix/fix44/OrderCancelReplaceRequest.h>
|
||||
#include <quickfix/fix44/NewOrderSingle.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>
|
||||
|
||||
/*
|
||||
order for 4 lot CLZ9
|
||||
8=FIX.4.49=15635=D49=SPARKFIX56=CLIENT134=150=3073797=Y52=20000809-20:20:5011=900010081=ACC A21=255=SIM:CLZ954=138=440=259=044=5060=20000809-18:20:32207=SIM10=045
|
||||
|
||||
order for 4 lot CLZ9-CLH0
|
||||
8=FIX.4.49=15635=D49=SPARKFIX56=CLIENT134=150=3073797=Y52=20000809-20:20:5011=900010081=ACC A21=255=SIM:CLZ9-CLH054=138=440=259=044=7460=20000809-18:20:32207=SIM10=045
|
||||
*/
|
||||
|
||||
namespace bts {
|
||||
namespace fix {
|
||||
|
||||
OrderSession::OrderSession(bts::api::ServiceContext& sc, IFixRouter& rt, const FIX::SessionID& id) :
|
||||
DropCopySession(sc, rt, id)
|
||||
{
|
||||
}
|
||||
|
||||
OrderSession::~OrderSession()
|
||||
{
|
||||
}
|
||||
|
||||
void OrderSession::fromApp(const FIX::Message &message,const FIX::SessionID &sessionID)
|
||||
throw(FIX::FieldNotFound, FIX::IncorrectDataFormat, FIX::IncorrectTagValue, FIX::UnsupportedMessageType)
|
||||
{
|
||||
bts_assert_msg(sessionID == _sess, "invalid session");
|
||||
auto mt = message.getHeader().getField(FIX::FIELD::MsgType);
|
||||
if(mt == FIX44::OrderStatusRequest::MsgType() ||
|
||||
mt == FIX44::SecurityDefinitionRequest::MsgType() ||
|
||||
mt == FIX44::NewOrderSingle::MsgType() ||
|
||||
mt == FIX44::OrderCancelReplaceRequest::MsgType() ||
|
||||
mt == FIX44::OrderCancelRequest::MsgType()
|
||||
)
|
||||
{
|
||||
LOG(Fix, Info, "IN: {}", message.toString());
|
||||
crack(message, sessionID);
|
||||
}
|
||||
else
|
||||
{
|
||||
LOG(Fix, Info, "UNSUPPORTED IN: {}", message.toString());
|
||||
throw FIX::UnsupportedMessageType();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void OrderSession::onMessage(const FIX44::OrderCancelRequest& cancel, const FIX::SessionID& s)
|
||||
{
|
||||
auto id = cancel.getField(FIX::FIELD::OrderID);
|
||||
uint key = 0;
|
||||
try
|
||||
{
|
||||
key = atoi(id.c_str());
|
||||
}
|
||||
catch(...)
|
||||
{
|
||||
throw FIX::IncorrectTagValue(FIX::FIELD::OrderID);
|
||||
}
|
||||
api::orderman::Cancel can;
|
||||
can.set_order_key(key);
|
||||
_orderservice.cancel_order(can, [this, s, cancel] (const auto& resp)
|
||||
{
|
||||
if(!resp.success() || resp.value()->error_code() != bts::api::orderman::ORDER_ERROR_OK)
|
||||
{
|
||||
send_cancel_reject(s, "Cancel Rejected: " + resp.value()->msg(), cancel);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
void OrderSession::onMessage(const FIX44::OrderCancelReplaceRequest& replace, const FIX::SessionID& s)
|
||||
{
|
||||
auto id = replace.getField(FIX::FIELD::OrderID);
|
||||
uint key = 0;
|
||||
try
|
||||
{
|
||||
key = atoi(id.c_str());
|
||||
}
|
||||
catch(...) { throw FIX::IncorrectTagValue(FIX::FIELD::OrderID); }
|
||||
double price = 0.0;
|
||||
try
|
||||
{
|
||||
price = atof(replace.getField(FIX::FIELD::Price).c_str());
|
||||
}
|
||||
catch(...) { throw FIX::IncorrectTagValue(FIX::FIELD::Price); }
|
||||
uint sz = 0;
|
||||
try
|
||||
{
|
||||
sz = (uint) abs( atoi (replace.getField(FIX::FIELD::OrderQty).c_str()) );
|
||||
}
|
||||
catch(...) { throw FIX::IncorrectTagValue(FIX::FIELD::OrderQty); }
|
||||
api::orderman::Update upd;
|
||||
upd.set_order_key(key);
|
||||
upd.set_price(price);
|
||||
upd.set_quantity(sz);
|
||||
_orderservice.update_order(upd, [this, s, replace] (const auto& resp)
|
||||
{
|
||||
if(!resp.success() || resp.value()->error_code() != bts::api::orderman::ORDER_ERROR_OK)
|
||||
{
|
||||
send_cancel_reject(s, "Cancel Rejected: " + resp.value()->msg(), replace);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
void OrderSession::onMessage(const FIX44::NewOrderSingle& create, const FIX::SessionID& s)
|
||||
{
|
||||
api::Venue ven = convert_exchange(FIX::SecurityExchange(create.getField(FIX::FIELD::SecurityExchange)));
|
||||
if (ven == bts::api::VENUE_UNSET)
|
||||
throw FIX::IncorrectTagValue(FIX::FIELD::SecurityExchange);
|
||||
|
||||
auto sec = _router.lookup(create.getField(FIX::FIELD::Symbol));
|
||||
if (!sec)
|
||||
sec = _router.lookup(create.getField(FIX::FIELD::SecurityDesc));
|
||||
if (!sec)
|
||||
throw FIX::IncorrectTagValue(FIX::FIELD::SecurityDesc);
|
||||
|
||||
api::orderman::OrderDefinition def;
|
||||
try
|
||||
{
|
||||
std::string manual = "Y";
|
||||
if(create.isSetField(FIX::FIELD::ManualOrderIndicator))
|
||||
manual = create.getField(FIX::FIELD::ManualOrderIndicator);
|
||||
def.set_account(create.getField(FIX::FIELD::Account));
|
||||
def.set_account_id(lookup_account(def.account()));
|
||||
def.set_security_key(sec->id());
|
||||
def.set_side(create.getField(FIX::FIELD::Side) == "1" ? api::SIDE_BUY : api::SIDE_SELL);
|
||||
def.set_user(_user_name);
|
||||
def.set_user_id(lookup_user(_user_name));
|
||||
def.set_manual_order_indicator(manual == "Y" ? bts::api::orderman::MANUAL_ORDER_INDICATOR_YES : bts::api::orderman::MANUAL_ORDER_INDICATOR_NO);
|
||||
def.set_time_in_force( (api::orderman::TimeInForce) int(create.getField(FIX::FIELD::TimeInForce)[0] ) );
|
||||
}
|
||||
catch(...)
|
||||
{
|
||||
throw FIX::IncorrectTagValue(FIX::FIELD::TimeInForce);
|
||||
}
|
||||
|
||||
bool price_req = false;
|
||||
set_ordertype(def, create, price_req);
|
||||
api::orderman::Create cr;
|
||||
cr.mutable_new_order()->CopyFrom(def);
|
||||
try
|
||||
{
|
||||
if (price_req)
|
||||
cr.set_price( atof( create.getField(FIX::FIELD::Price).c_str() ) );;
|
||||
}
|
||||
catch(...)
|
||||
{
|
||||
throw FIX::IncorrectTagValue(FIX::FIELD::Price);
|
||||
}
|
||||
try
|
||||
{
|
||||
cr.set_quantity( (uint) abs(atoi( create.getField(FIX::FIELD::OrderQty).c_str() )) );
|
||||
}
|
||||
catch(...)
|
||||
{
|
||||
throw FIX::IncorrectTagValue(FIX::FIELD::OrderQty);
|
||||
}
|
||||
_orderservice.create_order(cr, [this, s, create] (const auto& resp)
|
||||
{
|
||||
if(!resp.success() || resp.value()->error_code() != bts::api::orderman::ORDER_ERROR_OK)
|
||||
{
|
||||
_router.send_biz_reject(s, "Order Rejected: " + resp.value()->msg(), create);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
void OrderSession::set_ordertype(api::orderman::OrderDefinition& def, const FIX44::NewOrderSingle& req, bool& price_req)
|
||||
{
|
||||
price_req = false;
|
||||
auto ot = req.getField(FIX::FIELD::OrdType);
|
||||
if(ot == "1")
|
||||
{
|
||||
def.mutable_custom_def()->mutable_market();
|
||||
}
|
||||
else if(ot == "2")
|
||||
{
|
||||
def.mutable_custom_def()->mutable_limit();
|
||||
price_req = true;
|
||||
}
|
||||
else if(ot == "3")
|
||||
{
|
||||
auto s = def.mutable_custom_def()->mutable_stop();
|
||||
double stoppx = 0.0;
|
||||
try
|
||||
{
|
||||
stoppx = atof(req.getField(FIX::FIELD::StopPx).c_str());
|
||||
}
|
||||
catch(...) { throw FIX::IncorrectTagValue(FIX::FIELD::StopPx); }
|
||||
s->set_stop_price(stoppx);
|
||||
}
|
||||
else if(ot == "4")
|
||||
{
|
||||
price_req = true;
|
||||
auto s = def.mutable_custom_def()->mutable_stop_limit();
|
||||
double stoppx = 0.0;
|
||||
try
|
||||
{
|
||||
stoppx = atof(req.getField(FIX::FIELD::StopPx).c_str());
|
||||
}
|
||||
catch(...) { throw FIX::IncorrectTagValue(FIX::FIELD::StopPx); }
|
||||
s->set_stop_price(stoppx);
|
||||
}
|
||||
else
|
||||
throw FIX::IncorrectTagValue(FIX::FIELD::OrdType);
|
||||
}
|
||||
|
||||
|
||||
}}
|
||||
|
||||
@@ -0,0 +1,27 @@
|
||||
#pragma once
|
||||
|
||||
#include "DropCopySession.h"
|
||||
|
||||
namespace bts {
|
||||
namespace fix {
|
||||
|
||||
class OrderSession : public DropCopySession
|
||||
{
|
||||
public:
|
||||
OrderSession(bts::api::ServiceContext&, IFixRouter&, const FIX::SessionID&);
|
||||
virtual ~OrderSession();
|
||||
|
||||
void fromApp(const FIX::Message &message,const FIX::SessionID &sessionID)
|
||||
throw(FIX::FieldNotFound, FIX::IncorrectDataFormat, FIX::IncorrectTagValue, FIX::UnsupportedMessageType);
|
||||
|
||||
void onMessage(const FIX44::OrderCancelRequest& cancel, const FIX::SessionID& s);
|
||||
void onMessage(const FIX44::OrderCancelReplaceRequest& replace, const FIX::SessionID& s);
|
||||
void onMessage(const FIX44::NewOrderSingle& create, const FIX::SessionID& s);
|
||||
|
||||
protected:
|
||||
void set_ordertype(api::orderman::OrderDefinition& def, const FIX44::NewOrderSingle& req, bool& price_req);
|
||||
|
||||
};
|
||||
|
||||
}}
|
||||
|
||||
@@ -0,0 +1,61 @@
|
||||
#include "SecurityIndex.h"
|
||||
|
||||
#include "lib/md/HandleIndex.h"
|
||||
|
||||
|
||||
namespace bts {
|
||||
namespace fix {
|
||||
|
||||
struct SecurityIndex::PImpl
|
||||
{
|
||||
md::DescHandleIndex index;
|
||||
md::KeyHandleIndex key_index;
|
||||
PImpl(md::MDClient& md) : index(md), key_index(md)
|
||||
{
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
SecurityIndex::SecurityIndex(md::MDClient& md) : me(new PImpl(md))
|
||||
{
|
||||
}
|
||||
|
||||
std::vector<md::SecurityPtr> SecurityIndex::get_securities()
|
||||
{
|
||||
std::vector<md::SecurityPtr> list;
|
||||
me->index.security_index.lock([&list] (const auto& secmap)
|
||||
{
|
||||
for(const auto& s : secmap)
|
||||
list.push_back(s.second);
|
||||
});
|
||||
return list;
|
||||
}
|
||||
|
||||
|
||||
md::SecurityPtr SecurityIndex::lookup(const std::string& desc)
|
||||
{
|
||||
md::SecurityPtr ptr = nullptr;
|
||||
me->index.security_index.lock([&ptr, &desc] (const auto& secmap)
|
||||
{
|
||||
auto it = secmap.find(desc);
|
||||
if(it != secmap.end())
|
||||
ptr = it->second;
|
||||
});
|
||||
return ptr;
|
||||
}
|
||||
|
||||
md::SecurityPtr SecurityIndex::lookup(md::security_key key)
|
||||
{
|
||||
md::SecurityPtr ptr = nullptr;
|
||||
me->key_index.security_index.lock([&ptr, &key] (const auto& secmap)
|
||||
{
|
||||
auto it = secmap.find( key );
|
||||
if(it != secmap.end())
|
||||
ptr = it->second;
|
||||
});
|
||||
return ptr;
|
||||
}
|
||||
|
||||
}}
|
||||
|
||||
|
||||
@@ -0,0 +1,29 @@
|
||||
#pragma once
|
||||
|
||||
#include "lib/md/common.h"
|
||||
#include "lib/md/SecurityPtr.h"
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <memory>
|
||||
|
||||
namespace bts {
|
||||
namespace api { class Security; }
|
||||
namespace md { class MDClient; }
|
||||
namespace fix {
|
||||
|
||||
//A Wrapper for HandleIndex that compiles in c++14 (because of Fix dynamic exception stuff)
|
||||
class SecurityIndex
|
||||
{
|
||||
public:
|
||||
SecurityIndex(md::MDClient&);
|
||||
|
||||
std::vector<md::SecurityPtr> get_securities();
|
||||
md::SecurityPtr lookup(const std::string& desc);
|
||||
md::SecurityPtr lookup(md::security_key key);
|
||||
|
||||
private:
|
||||
struct PImpl;
|
||||
std::shared_ptr<PImpl> me;
|
||||
};
|
||||
|
||||
}}
|
||||
@@ -0,0 +1,335 @@
|
||||
#include "SessionBase.h"
|
||||
|
||||
#include "IFixRouter.h"
|
||||
|
||||
#include "lib/api/public/ServiceContext.h"
|
||||
#include "lib/api/service_helpers/AdminServiceUtils.h"
|
||||
#include "lib/utils/AuthToken.h"
|
||||
#include "lib/logging/logging.h"
|
||||
#include "lib/utils/bts_assert.h"
|
||||
#include "lib/utils/helper_macros.h"
|
||||
|
||||
#include <quickfix/Message.h>
|
||||
#include <quickfix/FixValues.h>
|
||||
#include <quickfix/SessionID.h>
|
||||
#include <quickfix/fix44/Logon.h>
|
||||
#include <quickfix/fix44/Heartbeat.h>
|
||||
#include <quickfix/fix44/BusinessMessageReject.h>
|
||||
#include <quickfix/fix44/OrderCancelReject.h>
|
||||
#include <quickfix/fix44/OrderCancelRequest.h>
|
||||
#include <quickfix/fix44/OrderCancelReplaceRequest.h>
|
||||
#include <boost/container/flat_map.hpp>
|
||||
|
||||
|
||||
namespace bts {
|
||||
namespace fix {
|
||||
|
||||
SessionBase::SessionBase(bts::api::ServiceContext& sc, IFixRouter& router, const FIX::SessionID& sess) :
|
||||
_authservice( sc.locate<bts::api::user::AuthService>() ),
|
||||
_adminservice( sc.locate<bts::api::admin::AdminService>() ),
|
||||
_router(router),
|
||||
_sess(sess),
|
||||
|
||||
_admin_data(),
|
||||
_user_name(),
|
||||
_token()
|
||||
{
|
||||
_adminobservable = _adminservice.subscribe_admin_data() |
|
||||
rxcpp::operators::map(
|
||||
[this](const auto& update)
|
||||
{
|
||||
bts_assert(update.all().size() == 1);
|
||||
|
||||
auto adata = _admin_data.lock( [&update] (auto& ad)
|
||||
{
|
||||
ad = *update.all()[0];
|
||||
return ad;
|
||||
} );
|
||||
return std::shared_ptr<AdminModelWrapper>(new AdminModelWrapper(adata, _user_name) ); //TODO RACE
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
SessionBase::~SessionBase()
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
void SessionBase::fromAdmin(const FIX::Message& message, const FIX::SessionID& sessionID)
|
||||
throw(FIX::FieldNotFound, FIX::IncorrectDataFormat, FIX::IncorrectTagValue, FIX::RejectLogon)
|
||||
{
|
||||
bts_assert_msg(sessionID == _sess, "invalid session");
|
||||
|
||||
FIX::MsgType type;
|
||||
message.getHeader().getField(type);
|
||||
if(type.getString() == FIX44::Logon::MsgType())
|
||||
{
|
||||
try
|
||||
{
|
||||
LOG(Fix, Info, "LOGIN: {}", message.toString() );
|
||||
FIX44::Logon msg = (FIX44::Logon)message;
|
||||
if(msg.isSetField(FIX::FIELD::Username))
|
||||
_user_name = msg.getField(FIX::FIELD::Username);
|
||||
std::string pass;
|
||||
if(msg.isSetField(FIX::FIELD::Password))
|
||||
pass = msg.getField(FIX::FIELD::Password);
|
||||
|
||||
bts::api::user::AuthenticateLoginRequest auth;
|
||||
auth.set_login(_user_name);
|
||||
auth.set_password(pass);
|
||||
_authservice.authenticate_login(auth, [this, sessionID] (auto resp)
|
||||
{
|
||||
if (resp.success() && resp.value()->is_valid())
|
||||
{
|
||||
_token = resp.value()->token();
|
||||
}
|
||||
else
|
||||
{
|
||||
//I can't just throw here cuz c++
|
||||
std::string reason = "LOGIN FAILED " + resp.value()->reason();
|
||||
LOG(Fix, Error, reason.c_str());
|
||||
try
|
||||
{
|
||||
auto sess = FIX::Session::lookupSession(sessionID);
|
||||
if(sess != nullptr)
|
||||
sess->disconnect();
|
||||
}
|
||||
catch(...) {}
|
||||
}
|
||||
});
|
||||
}
|
||||
catch (std::exception ex)
|
||||
{
|
||||
throw FIX::RejectLogon("Logon rejected " + std::string(ex.what()));
|
||||
}
|
||||
}
|
||||
else if(type.getString() == FIX44::Heartbeat::MsgType())
|
||||
{
|
||||
FIX44::Heartbeat hb = (FIX44::Heartbeat)message;
|
||||
handle_heartbeat(hb, sessionID);
|
||||
}
|
||||
else
|
||||
{
|
||||
LOG(Fix, Info, "ADMININ: {}", message.toString() );
|
||||
}
|
||||
}
|
||||
|
||||
void SessionBase::handle_heartbeat(const FIX44::Heartbeat& hb, const FIX::SessionID& sessionID)
|
||||
{
|
||||
//TODO I'd previously sent this to the latency service when it was out of process, not sure what to do now
|
||||
UNUSED(hb);
|
||||
UNUSED(sessionID);
|
||||
}
|
||||
|
||||
void SessionBase::toAdmin(FIX::Message& m, const FIX::SessionID& sessionID)
|
||||
{
|
||||
bts_assert_msg(sessionID == _sess, "invalid session");
|
||||
LOG(Fix, Info, "ADMINOUT: {}", m.toString());
|
||||
}
|
||||
|
||||
void SessionBase::toApp(FIX::Message& m, const FIX::SessionID&) throw(FIX::DoNotSend)
|
||||
{
|
||||
LOG(Fix, Info, "OUT: {}", m.toString());
|
||||
}
|
||||
|
||||
void SessionBase::onCreate(const FIX::SessionID& s)
|
||||
{
|
||||
LOG(Fix, Info, "CREATE: {}", s.toString());
|
||||
}
|
||||
|
||||
void SessionBase::onMessage(const FIX44::BusinessMessageReject& n, const FIX::SessionID& s)
|
||||
{
|
||||
LOG(Fix, Warn, "{} Business Reject: {} {}", s.toString(), n.getField(FIX::FIELD::BusinessRejectReason), n.getField(FIX::FIELD::Text) );
|
||||
}
|
||||
|
||||
void SessionBase::send_cancel_reject(const FIX::SessionID& sess, const std::string& text, const FIX::Message& refmessage)
|
||||
{
|
||||
FIX44::OrderCancelReject rej;
|
||||
if(refmessage.isSetField(FIX::FIELD::ClOrdID))
|
||||
rej.setField(FIX::FIELD::ClOrdID, refmessage.getField(FIX::FIELD::ClOrdID));
|
||||
if(refmessage.isSetField(FIX::FIELD::OrigClOrdID))
|
||||
rej.setField(FIX::FIELD::OrigClOrdID, refmessage.getField(FIX::FIELD::OrigClOrdID));
|
||||
if(refmessage.isSetField(FIX::FIELD::OrderID))
|
||||
rej.setField(FIX::FIELD::OrderID, refmessage.getField(FIX::FIELD::OrderID));
|
||||
rej.setField(FIX::FIELD::Text, text);
|
||||
|
||||
if(refmessage.getField(FIX::FIELD::MsgType) == FIX44::OrderCancelRequest::MsgType())
|
||||
{
|
||||
rej.setField(FIX::FIELD::CxlRejResponseTo, "1");
|
||||
}
|
||||
else if(refmessage.getField(FIX::FIELD::MsgType) == FIX44::OrderCancelReplaceRequest::MsgType())
|
||||
{
|
||||
rej.setField(FIX::FIELD::CxlRejResponseTo, "2");
|
||||
}
|
||||
_router.send(rej, sess);
|
||||
}
|
||||
|
||||
bool SessionBase::security_types_equal(bts::api::SecurityType btstype, FIX::SecurityType fixtype)
|
||||
{
|
||||
return btstype == convert_security_type(fixtype);
|
||||
}
|
||||
|
||||
FIX::SecurityType SessionBase::convert_security_type(bts::api::SecurityType btstype)
|
||||
{
|
||||
switch (btstype)
|
||||
{
|
||||
case bts::api::SecurityType::SECURITYTYPE_FUTURE:
|
||||
return FIX::SecurityType("FUT");
|
||||
case bts::api::SecurityType::SECURITYTYPE_OPTION:
|
||||
return FIX::SecurityType("OPT");
|
||||
case bts::api::SecurityType::SECURITYTYPE_SPREAD:
|
||||
return FIX::SecurityType("MLEG");
|
||||
case bts::api::SecurityType::SECURITYTYPE_STOCK:
|
||||
return FIX::SecurityType("CS");
|
||||
case bts::api::SecurityType::SECURITYTYPE_SYNTHETIC:
|
||||
return FIX::SecurityType("SYNTH");
|
||||
default:
|
||||
return FIX::SecurityType();
|
||||
}
|
||||
}
|
||||
|
||||
bts::api::SecurityType SessionBase::convert_security_type(FIX::SecurityType fixtype)
|
||||
{
|
||||
std::string s = std::string(fixtype.getValue());
|
||||
if(s == "FUT")
|
||||
return bts::api::SecurityType::SECURITYTYPE_FUTURE;
|
||||
else if(s == "OPT")
|
||||
return bts::api::SecurityType::SECURITYTYPE_OPTION;
|
||||
else if(s == "MLEG")
|
||||
return bts::api::SecurityType::SECURITYTYPE_SPREAD;
|
||||
else if(s == "CS")
|
||||
return bts::api::SecurityType::SECURITYTYPE_STOCK;
|
||||
else if("SYNTH" == s)
|
||||
return bts::api::SecurityType::SECURITYTYPE_SYNTHETIC;
|
||||
else
|
||||
return bts::api::SecurityType::SECURITYTYPE_UNSET;
|
||||
}
|
||||
|
||||
bool SessionBase::exchanges_equal(bts::api::Venue btstype, FIX::SecurityExchange fixtype)
|
||||
{
|
||||
return btstype == convert_exchange(fixtype);
|
||||
}
|
||||
|
||||
FIX::SecurityExchange SessionBase::convert_exchange(bts::api::Venue btstype)
|
||||
{
|
||||
switch (btstype)
|
||||
{
|
||||
case bts::api::Venue::VENUE_BTS:
|
||||
return FIX::SecurityExchange("BTS");
|
||||
case bts::api::Venue::VENUE_CBOE:
|
||||
return FIX::SecurityExchange("XCBO");
|
||||
case bts::api::Venue::VENUE_CBOECFE:
|
||||
return FIX::SecurityExchange("XCBF");
|
||||
case bts::api::Venue::VENUE_GLBX:
|
||||
return FIX::SecurityExchange("XCME");
|
||||
case bts::api::Venue::VENUE_ICE:
|
||||
return FIX::SecurityExchange("IFUS");
|
||||
case bts::api::Venue::VENUE_SIM:
|
||||
return FIX::SecurityExchange("SIM");
|
||||
default:
|
||||
return FIX::SecurityExchange();
|
||||
}
|
||||
}
|
||||
|
||||
bts::api::Venue SessionBase::convert_exchange(FIX::SecurityExchange fixtype)
|
||||
{
|
||||
std::string s = std::string(fixtype.getValue());
|
||||
if("BTS" == s)
|
||||
return bts::api::Venue::VENUE_BTS;
|
||||
else if("XCBO" == s)
|
||||
return bts::api::Venue::VENUE_CBOE;
|
||||
else if("XCBF" == s)
|
||||
return bts::api::Venue::VENUE_CBOECFE;
|
||||
else if("GLBX" == s || "XCME" == s || "XCBT" == s || "XCEC" == s || "XNYM" == s)
|
||||
return bts::api::Venue::VENUE_GLBX;
|
||||
else if("IEPA" == s || "IFUS" == s || "IFEU" == s || "IFLL" == s || "IFCA" == s)
|
||||
return bts::api::Venue::VENUE_ICE;
|
||||
else if("SIM" == s)
|
||||
return bts::api::Venue::VENUE_SIM;
|
||||
else
|
||||
return bts::api::Venue::VENUE_UNSET;
|
||||
}
|
||||
|
||||
FIX::OrdStatus SessionBase::translate_status(const bts::api::orderman::OrderState& state,
|
||||
bts::api::orderman::NativeOrderState* native)
|
||||
{
|
||||
native->Clear();
|
||||
if (state.has_market() && state.market().has_native_state() && state.market().native_state().native_status() != bts::api::orderman::NativeOrderStatus::NATIVE_ORDER_STATUS_UNACKNOWLEDGED)
|
||||
{
|
||||
*native = state.market().native_state();
|
||||
return FIX::OrdStatus((char)native->native_status());
|
||||
}
|
||||
else if (state.has_limit() && state.limit().has_native_state() && state.limit().native_state().native_status() != bts::api::orderman::NativeOrderStatus::NATIVE_ORDER_STATUS_UNACKNOWLEDGED)
|
||||
{
|
||||
*native = state.limit().native_state();
|
||||
return FIX::OrdStatus((char)native->native_status());
|
||||
}
|
||||
else if (state.has_stop() && state.stop().has_native_state() && state.stop().native_state().native_status() != bts::api::orderman::NativeOrderStatus::NATIVE_ORDER_STATUS_UNACKNOWLEDGED)
|
||||
{
|
||||
*native = state.stop().native_state();
|
||||
return FIX::OrdStatus((char)native->native_status());
|
||||
}
|
||||
else if (state.has_stop_limit() && state.stop_limit().has_native_state() && state.stop_limit().native_state().native_status() != bts::api::orderman::NativeOrderStatus::NATIVE_ORDER_STATUS_UNACKNOWLEDGED)
|
||||
{
|
||||
*native = state.stop_limit().native_state();
|
||||
return FIX::OrdStatus((char)native->native_status());
|
||||
}
|
||||
else
|
||||
{
|
||||
switch (state.status())
|
||||
{
|
||||
case bts::api::orderman::OrderStatus::ORDER_STATUS_CANCELED:
|
||||
return FIX::OrdStatus(FIX::OrdStatus_CANCELED);
|
||||
case bts::api::orderman::OrderStatus::ORDER_STATUS_COMPLETED:
|
||||
return FIX::OrdStatus(FIX::OrdStatus_FILLED);
|
||||
case bts::api::orderman::OrderStatus::ORDER_STATUS_DISCONNECTED:
|
||||
return FIX::OrdStatus(FIX::OrdStatus_STOPPED);
|
||||
case bts::api::orderman::OrderStatus::ORDER_STATUS_ERROR:
|
||||
return FIX::OrdStatus(FIX::OrdStatus_REJECTED);
|
||||
case bts::api::orderman::OrderStatus::ORDER_STATUS_INACTIVE:
|
||||
return FIX::OrdStatus(FIX::OrdStatus_PENDING_NEW);
|
||||
case bts::api::orderman::OrderStatus::ORDER_STATUS_PENDING_WORKING:
|
||||
return FIX::OrdStatus(FIX::OrdStatus_PENDING_NEW);
|
||||
case bts::api::orderman::OrderStatus::ORDER_STATUS_REJECTED_BY_SPARK:
|
||||
return FIX::OrdStatus(FIX::OrdStatus_REJECTED);
|
||||
case bts::api::orderman::OrderStatus::ORDER_STATUS_WORKING:
|
||||
{
|
||||
if (state.filled_quantity() < state.quantity())
|
||||
return FIX::OrdStatus(FIX::OrdStatus_PARTIALLY_FILLED);
|
||||
else
|
||||
return FIX::OrdStatus(FIX::OrdStatus_NEW);
|
||||
}
|
||||
default:
|
||||
return FIX::OrdStatus();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
uint SessionBase::lookup_account(const std::string& acct)
|
||||
{
|
||||
bts::api::admin::BTSAccount* a = nullptr;
|
||||
_admin_data.lock( [&] (auto& ad)
|
||||
{
|
||||
a = admin::AdminServiceUtils::find_account(ad, acct);
|
||||
});
|
||||
if(a)
|
||||
return a->id();
|
||||
else
|
||||
throw FIX::IncorrectTagValue(FIX::FIELD::Account);
|
||||
}
|
||||
|
||||
uint SessionBase::lookup_user(const std::string& user)
|
||||
{
|
||||
bts::api::admin::BTSUser* u = nullptr;
|
||||
_admin_data.lock( [&] (auto& ad)
|
||||
{
|
||||
u = admin::AdminServiceUtils::find_user(ad, user);
|
||||
});
|
||||
if(u)
|
||||
return u->id();
|
||||
else
|
||||
throw FIX::IncorrectTagValue(FIX::FIELD::Username);
|
||||
}
|
||||
|
||||
|
||||
}}
|
||||
@@ -0,0 +1,84 @@
|
||||
#pragma once
|
||||
|
||||
#include "IFixSession.h"
|
||||
#include "AdminModelWrapper.h"
|
||||
|
||||
#include "lib/api/public/ServiceContext.h"
|
||||
#include "lib/api/services/AuthService.pb.h"
|
||||
#include "lib/api/services/AuthService.client.pb.h"
|
||||
#include "lib/api/services/AdminService.pb.h"
|
||||
#include "lib/api/services/AdminService.client.pb.h"
|
||||
#include "lib/api/services/OrderService.pb.h"
|
||||
#include "lib/api/services/OrderService.client.pb.h"
|
||||
#include "lib/domain_model/Security.pb.h"
|
||||
#include "lib/utils/LockBox.h"
|
||||
|
||||
#include <quickfix/MessageCracker.h>
|
||||
|
||||
#include <string>
|
||||
#include <memory>
|
||||
#include <mutex>
|
||||
#include <functional>
|
||||
|
||||
namespace FIX
|
||||
{
|
||||
class Message;
|
||||
class SessionID;
|
||||
}
|
||||
|
||||
namespace FIX44
|
||||
{
|
||||
class Heartbeat;
|
||||
class BusinessMessageReject;
|
||||
}
|
||||
|
||||
namespace bts {
|
||||
namespace fix {
|
||||
|
||||
class IFixRouter;
|
||||
|
||||
class SessionBase : public IFixSession, public FIX::MessageCracker
|
||||
{
|
||||
public:
|
||||
SessionBase(bts::api::ServiceContext&, IFixRouter&, const FIX::SessionID&);
|
||||
virtual ~SessionBase();
|
||||
|
||||
void fromAdmin(const FIX::Message& message, const FIX::SessionID& sessionID)
|
||||
throw(FIX::FieldNotFound, FIX::IncorrectDataFormat, FIX::IncorrectTagValue, FIX::RejectLogon);
|
||||
void toAdmin(FIX::Message&, const FIX::SessionID&);
|
||||
void toApp(FIX::Message&, const FIX::SessionID&) throw(FIX::DoNotSend);
|
||||
|
||||
void onCreate(const FIX::SessionID& s);
|
||||
void onMessage(const FIX44::BusinessMessageReject& n, const FIX::SessionID& s);
|
||||
|
||||
protected:
|
||||
void handle_heartbeat(const FIX44::Heartbeat& hb, const FIX::SessionID& sessionID);
|
||||
void send_cancel_reject(const FIX::SessionID& sess, const std::string& text, const FIX::Message& refmessage);
|
||||
|
||||
bool security_types_equal(bts::api::SecurityType btstype, FIX::SecurityType fixtype);
|
||||
FIX::SecurityType convert_security_type(bts::api::SecurityType btstype);
|
||||
bts::api::SecurityType convert_security_type(FIX::SecurityType fixtype);
|
||||
|
||||
bool exchanges_equal(bts::api::Venue btstype, FIX::SecurityExchange fixtype);
|
||||
FIX::SecurityExchange convert_exchange(bts::api::Venue btstype);
|
||||
bts::api::Venue convert_exchange(FIX::SecurityExchange fixtype);
|
||||
|
||||
FIX::OrdStatus translate_status(const bts::api::orderman::OrderState& state,
|
||||
bts::api::orderman::NativeOrderState* native);
|
||||
|
||||
uint lookup_account(const std::string& acct);
|
||||
uint lookup_user(const std::string& user);
|
||||
|
||||
protected:
|
||||
bts::api::user::AuthService _authservice;
|
||||
bts::api::admin::AdminService _adminservice;
|
||||
rxcpp::observable<std::shared_ptr<AdminModelWrapper>> _adminobservable;
|
||||
IFixRouter& _router;
|
||||
FIX::SessionID _sess;
|
||||
|
||||
utils::LockBox<bts::api::admin::AdminData> _admin_data;
|
||||
std::string _user_name;
|
||||
bts::api::AuthToken _token;
|
||||
};
|
||||
|
||||
}}
|
||||
@@ -16,6 +16,7 @@
|
||||
#include "server/sparkd/cmds/QuickTest.h"
|
||||
#include "server/sparkd/cmds/Run.h"
|
||||
#include "server/sparkd/cmds/RunSim.h"
|
||||
#include "server/sparkd/cmds/RunFix.h"
|
||||
#include "server/sparkd/cmds/Span.h"
|
||||
#include "server/sparkd/cmds/Test.h"
|
||||
#include "server/sparkd/cmds/TestCeloxica.h"
|
||||
@@ -35,6 +36,7 @@ int main(int argc, char **argv) {
|
||||
REGISTER(ConfigCheck);
|
||||
REGISTER(KeyCode);
|
||||
REGISTER(RunSim);
|
||||
REGISTER(RunFix);
|
||||
REGISTER(Benchmark);
|
||||
REGISTER(QuickTest);
|
||||
REGISTER(AutoCert);
|
||||
|
||||
Reference in new issue
Block a user