Merged in fix-fill-accounting (pull request #251)

RTM. Fix fill accounting
This commit is contained in:
Pedro Pinto committed 2019-06-07 19:52:02 +00:00
commit 46a340d493
19 files changed
+354 -169

No files matched your search

+4 -1
View File
@@ -9,8 +9,11 @@ insert_anchor_links = "right"
- Made our orders easier to spot in MBO display
- Changed middle-click on center column to recenter ladder and introduced Alt middle-click on center to bring up the MBO popup
- Fixed Net/Absolute radio button in risk admin UI
- Fixed double counting of spread legs in limits
- Removed expired fills from the system
- Improved handling of orders with undiscovered securities
- Added feedback to the ladder when the user attempts to middle click on bid/ask columns using an order that does not have parameters
- Fixed display of algo studio algo types in order book
## 2.1.4
### User Visible Changes
- Fixed handling of cancel button in security picker
+3 -1
View File
@@ -27,7 +27,9 @@ message ToastNotifications {
message SoundNotifications {
optional bool enabled = 1 [default = true];
}
message FilterExpiredFills {
optional bool enabled = 1 [default = true];
}
message VolumeAtPrice{
optional bool enabled = 1 [default = true];
}
+54 -11
View File
@@ -3,31 +3,74 @@
#include <google/protobuf/text_format.h>
#include "lib/utils/datetime.h"
#include <boost/date_time/posix_time/posix_time.hpp>
namespace bts {
namespace securities {
std::string to_str (api::Security const&sec)
{
std::string to_str(api::Security const &sec) {
std::string serialized;
google::protobuf::TextFormat::PrintToString (sec, &serialized);
google::protobuf::TextFormat::PrintToString(sec, &serialized);
return serialized;
}
std::pair<bool, api::Security> from_str(std::string const&str)
{
std::pair<bool, api::Security> from_str(std::string const &str) {
api::Security result;
auto success = google::protobuf::TextFormat::ParseFromString (str,&result);
return std::make_pair(success,result);
auto success = google::protobuf::TextFormat::ParseFromString(str, &result);
return std::make_pair(success, result);
}
std::string descriptor(api::Security const&sec)
{
std::string descriptor(api::Security const &sec) {
if (sec.has_glbx())
return sec.glbx().security_desc();
else if (sec.has_ice())
return sec.ice().security_desc();
return sec.ice().security_desc();
else
return "No descriptor for non glbx exchange";
}
}}
boost::gregorian::date smallest_leg(
::google::protobuf::RepeatedPtrField<::bts::api::ContractLeg> const &l) {
bts_assert(l.size());
auto smallest = expiration(l[0].security());
for (const auto &leg : l) {
auto this_date = expiration(leg.security());
if (this_date < smallest) {
smallest = this_date;
}
}
return smallest;
}
boost::gregorian::date expiration(api::Security const &security) {
switch (security.Contract_case()) {
case api::Security::kFuture:
return {
boost::gregorian::greg_year(security.future().expiration_year()),
boost::gregorian::greg_month(security.future().expiration_month()),
boost::gregorian::greg_day(security.future().expiration_day())};
case api::Security::kOption:
return {
boost::gregorian::greg_year(security.option().expiration_year()),
boost::gregorian::greg_month(security.option().expiration_month()),
boost::gregorian::greg_day(security.option().expiration_day())};
case api::Security::kSpread:
return smallest_leg(security.spread().legs());
case api::Security::kSynthetic:
return smallest_leg(security.synthetic().legs());
default:
return {};
}
}
bool is_expired(api::Security const &security) {
auto e = expiration(security);
if (e == boost::gregorian::date())
return false;
else
return e < boost::posix_time::microsec_clock::universal_time().date();
}
} // namespace securities
} // namespace bts
+5
View File
@@ -7,6 +7,8 @@
#include "lib/domain_model/common.h"
#include "lib/utils/bts_assert.h"
namespace boost {namespace gregorian {class date;}}
namespace bts {
namespace api {
@@ -149,6 +151,9 @@ inline Price round_to_quote_price(api::Security const &sec, Price in,
raw_round_to_quote_price(p, sec.contract_spec().low_tick(), side));
}
boost::gregorian::date expiration(api::Security const&sec);
bool is_expired(api::Security const &security) ;
inline Price tick_size(api::Security const &sec) {
return Price(sec.contract_spec().low_tick());
}
+123 -32
View File
@@ -1,6 +1,10 @@
#include "catch/catch.hpp"
#include "../Security.h"
#include "catch/catch.hpp"
#include "lib/domain_model/Security.h"
#include "lib/utils/PBUtils.h"
#include <boost/date_time/gregorian/gregorian.hpp>
namespace bts {
namespace test {
@@ -8,33 +12,30 @@ namespace test {
TEST_CASE("rounding_tests", "[md]") {
SECTION("test_price_size") {
const double tick_sizes[] {
// Dan's production system
// ./sparkd configcheck Symbols | grep future | sed -e 's/.*future: \"\(.*\)\"/\1/' | xargs -I{} sh -c "./sparkd md dump {} future glbx" | grep -o low_tick': [0-9.]\+
0.0078125,
0.015625,
0.03125,
0.1,
0.25,
1,
// From Kevin's ramblings
// 2.5/320,
// 31./32,
// 10./32,
// 10.25/32,
// 10.375/32,
// 10.125/32,
// 10.25/32,
// .0025,
// .0000001,
// 1./32,
// 0.5/32,
// 0.25/32,
// 0.125/32,
// 0.01,
// 0.005,
// 1./100000,
// 1./16,
const double tick_sizes[]{
// Dan's production system
// ./sparkd configcheck Symbols | grep future | sed -e 's/.*future:
// \"\(.*\)\"/\1/' | xargs -I{} sh -c "./sparkd md dump {} future
// glbx" | grep -o low_tick': [0-9.]\+
0.0078125, 0.015625, 0.03125, 0.1, 0.25, 1,
// From Kevin's ramblings
// 2.5/320,
// 31./32,
// 10./32,
// 10.25/32,
// 10.375/32,
// 10.125/32,
// 10.25/32,
// .0025,
// .0000001,
// 1./32,
// 0.5/32,
// 0.25/32,
// 0.125/32,
// 0.01,
// 0.005,
// 1./100000,
// 1./16,
};
for (double tick : tick_sizes) {
@@ -43,15 +44,105 @@ TEST_CASE("rounding_tests", "[md]") {
for (double price = 0; price < 101; price += tick) {
// Since all prices are valid ticks rounding should not change them
const double epsilon = 1e-9;
for (api::Side side : { api::Side::SIDE_BUY, api::Side::SIDE_SELL }) {
double rounded = securities::raw_round_to_quote_price(price, tick, side);
REQUIRE(std::abs(price-rounded) <= epsilon);
for (api::Side side : {api::Side::SIDE_BUY, api::Side::SIDE_SELL}) {
double rounded =
securities::raw_round_to_quote_price(price, tick, side);
REQUIRE(std::abs(price - rounded) <= epsilon);
}
}
}
}
}
auto spread_txt = "id: 17\n"
"symbol: \"ES\"\n"
"exchange: VENUE_GLBX\n"
"contract_spec {\n"
" low_tick: 0.05\n"
" high_tick: 0.05\n"
" price_multiplier: 0.01\n"
"}\n"
"spread {\n"
" legs {\n"
" security {\n"
" id: 6\n"
" symbol: \"ES\"\n"
" exchange: VENUE_GLBX\n"
" contract_spec {\n"
" low_tick: 0.25\n"
" high_tick: 0.25\n"
" cash_per_point: 50\n"
" price_multiplier: 0.01\n"
" }\n"
" future {\n"
" expiration_year: 2019\n"
" expiration_month: 1\n"
" expiration_day: 20\n"
" }\n"
" glbx {\n"
" exchange_id: 16145\n"
" security_desc: \"ESZ9\"\n"
" cfi_code: \"FFIXSX\"\n"
" cme_symbol: \"ES\"\n"
" market_segment_id: 64\n"
" }\n"
" }\n"
" quantity: 1\n"
" side: SIDE_SELL\n"
" leg_delta: 1\n"
" }\n"
" legs {\n"
" security {\n"
" id: 2\n"
" symbol: \"ES\"\n"
" exchange: VENUE_GLBX\n"
" contract_spec {\n"
" low_tick: 0.25\n"
" high_tick: 0.25\n"
" cash_per_point: 50\n"
" price_multiplier: 0.01\n"
" }\n"
" future {\n"
" expiration_year: 3020\n"
" expiration_month: 6\n"
" expiration_day: 19\n"
" }\n"
" glbx {\n"
" exchange_id: 21336\n"
" security_desc: \"ESM0\"\n"
" cfi_code: \"FFIXSX\"\n"
" cme_symbol: \"ES\"\n"
" market_segment_id: 64\n"
" }\n"
" }\n"
" quantity: 1\n"
" side: SIDE_BUY\n"
" leg_delta: 1\n"
" }\n"
"}\n"
"glbx {\n"
" exchange_id: 35451\n"
" security_desc: \"ESZ9-ESM0\"\n"
" cfi_code: \"FMIXSX\"\n"
" cme_symbol: \"ES\"\n"
" market_segment_id: 64\n"
"}";
TEST_CASE("expiration_tests", "[security]") {
SECTION("test_expiration_retrieval") {
}}
using namespace boost::gregorian;
auto spread = utils::pb::from_string<api::Security>(spread_txt);
REQUIRE(securities::expiration(spread) ==
date(greg_year(2019), greg_month(1), greg_day(20)));
auto &leg = spread.spread().legs(1).security();
REQUIRE(securities::expiration(leg) ==
date(greg_year(3020), greg_month(6), greg_day(19)));
REQUIRE(securities::is_expired(spread));
REQUIRE(!securities::is_expired(leg));
}
}
} // namespace test
} // namespace bts
+10 -30
View File
@@ -8,6 +8,7 @@
#include "lib/config/protos/system.pb.h"
#include "lib/md/Shm/FixedSecurity.h"
#include "lib/config/protos/MarketData.pb.h"
#include "lib/domain_model/Security.h"
#include <boost/container/flat_set.hpp>
#include <boost/filesystem.hpp>
@@ -141,7 +142,15 @@ Result insert_or_replace(const std::vector<SecurityPtr>& securities) {
insert.bind(3, psecurity->exchange());
insert.bind(4, securitytype_of(*psecurity));
insert.bind(5, buffer);
insert.bind(6, expiration(*psecurity).to_short());
auto greg_date = securities::expiration(*psecurity);
if (greg_date == boost::gregorian::date()) {
insert.bind(6, datetime::Date(9999,1,1).to_short());
} else {
ushort year = greg_date.year();
ushort month = greg_date.month();
ushort day = greg_date.day();
insert.bind(6, datetime::Date(year,month,day).to_short());
}
insert.exec();
insert.reset();
}
@@ -204,33 +213,4 @@ SecurityPtr find_security(SecurityKey) { return nullptr; }
#endif
template <class L>
datetime::Date smallest_leg(L l) {
// Iterate though all legs and choose the smallest
datetime::Date date(std::numeric_limits<uint16_t>::max(),0,0);
for (const auto& leg : l) {
auto legDate = expiration(leg.security());
if (legDate < date) {
date = legDate;
}
}
return date;
}
datetime::Date expiration(const api::Security& security) {
switch(security.Contract_case()) {
case api::Security::kFuture:
return datetime::Date(security.future().expiration_year(), security.future().expiration_month(), security.future().expiration_day());
case api::Security::kOption:
return datetime::Date(security.option().expiration_year(), security.option().expiration_month(), security.option().expiration_day());
case api::Security::kSpread:
return smallest_leg(security.spread().legs());
case api::Security::kSynthetic:
return smallest_leg(security.synthetic().legs());
default:
// return a 0 date which will always be considered expired.
return datetime::Date();
}
}
}}}//namespace
+1 -2
View File
@@ -36,6 +36,5 @@ Result insert_or_replace(const std::vector<SecurityPtr>&);
std::vector<api::synthetic::Metadata> load_metadata();
bool store_metadata(const api::synthetic::Metadata&);
datetime::Date expiration(const api::Security& security);
}}}//namespace
}}}//namespace
+18 -8
View File
@@ -3,6 +3,7 @@
#include "lib/jetfix/common/OutFIX.h"
#include "lib/jetfix/gateway/JetFIXGateway.h"
#include "lib/logging/logging.h"
#include "lib/md/SecurityOracle.h"
#include "lib/orderman/events/CancelRejectEvent.h"
#include "lib/orderman/events/GatewayStatusChangeEvent.h"
#include "lib/orderman/events/NewExecutionReportEvent.h"
@@ -24,10 +25,11 @@ using JFGw =
fix::RejectMsg, fix::CancelRejectMsg>;
struct ExecutionGateway::PImpl {
PImpl(config::JetFIX::Connection const &cfg, trace::PerfLog &l)
PImpl(config::JetFIX::Connection const &cfg, trace::PerfLog &l,
md::SecurityOracle o)
: gw(cfg, l), exe_report_pool(), state(State::WAITING_FOR_SECURITIES),
securities(), securities_by_descriptor(), gw_name(cfg.name()),
cfg(cfg), perf_log(l) {}
cfg(cfg), perf_log(l), sec_oracle(o) {}
JFGw gw;
boost::object_pool<ExecutionReport> exe_report_pool;
@@ -41,11 +43,12 @@ struct ExecutionGateway::PImpl {
OnGwStatusChange on_gw_status_change;
OnCancelReject on_cancel_reject;
trace::PerfLog &perf_log;
md::SecurityOracle sec_oracle;
};
ExecutionGateway::ExecutionGateway(config::JetFIX::Connection const &cfg,
trace::PerfLog &log)
: me(new PImpl(cfg, log)) {
trace::PerfLog &log, md::SecurityOracle o)
: me(new PImpl(cfg, log, o)) {
me->state = State::WAITING_FOR_SECURITIES;
}
ExecutionGateway::~ExecutionGateway() {}
@@ -111,11 +114,17 @@ void ExecutionGateway::on_exe_report(
SecurityKey sk;
decode_clorid(msg.get<fix::ClOrdID>(), &ok, &sk);
auto scoped_log = trace::log_scope(me->perf_log, msg.trace(), "ExecutionGateway::on_exe_report", ok.value());
auto scoped_log =
trace::log_scope(me->perf_log, msg.trace(),
"ExecutionGateway::on_exe_report", ok.value());
LOG(Latency, Info, "Started handling of execution report for order {}",
ok.value());
auto sec = me->securities[sk.value()];
auto &sec = me->securities[sk.value()];
if (!sec) {
sec = me->sec_oracle.get_security(SecurityKey(sk.value()));
me->securities_by_descriptor[securities::descriptor(*sec)] = sec;
}
if (!sec)
LOG(OrderMan, Error,
@@ -303,8 +312,9 @@ void ExecutionGateway::dispatch_incoming_fix_messages() {
void ExecutionGateway::send_fix(jetfix::OutFIXPtr const &fix,
trace::CorID trace) {
auto scoped_log = trace::log_scope(me->perf_log,trace,"ExecutionGateway::send_fix");
me->gw.send(fix,trace);
auto scoped_log =
trace::log_scope(me->perf_log, trace, "ExecutionGateway::send_fix");
me->gw.send(fix, trace);
me->gw.dispatch_events();
}
+3 -1
View File
@@ -9,6 +9,7 @@
#include <memory>
namespace bts {
namespace md { class SecurityOracle;}
namespace trace {struct CorID;class PerfLog;}
namespace orderman {
class NewSecuritiesEvent;
@@ -22,7 +23,8 @@ using OnCancelReject = std::function<void(OrderKey, std::string const &)>;
// fix execution gateway for a single FIX session
class ExecutionGateway {
public:
ExecutionGateway(config::JetFIX::Connection const &, trace::PerfLog &);
ExecutionGateway(config::JetFIX::Connection const &, trace::PerfLog &,
md::SecurityOracle);
~ExecutionGateway();
void start();
+13 -5
View File
@@ -1,5 +1,8 @@
#include "FillStore.h"
#include "lib/api/services/OrderService.pb.h"
#include "lib/config/config.h"
#include "lib/config/protos/toggles.pb.h"
#include "lib/domain_model/Security.h"
#include "lib/orderman/actors/SODPosition.h"
#include "lib/orderman/events/NewExecutionReportEvent.h"
#include "lib/orderman/events/OrderDefinitionUpdateEvent.h"
@@ -53,10 +56,15 @@ void FillStore::init() {
auto sec = me->oracle.get_security(SecurityKey(fill.security_key()));
if (sec) {
auto fk = kvp.first;
if (!me->sod_pos_map.add_fill_to_sod_pos(fill))
stored_batch.emplace_back(fill, *sec);
me->known_fills.insert(fk);
if (config::get<config::toggles::FilterExpiredFills>().enabled() &&
securities::is_expired(*sec))
continue;
else {
auto fk = kvp.first;
if (!me->sod_pos_map.add_fill_to_sod_pos(fill))
stored_batch.emplace_back(fill, *sec);
me->known_fills.insert(fk);
}
} else
LOG(persist, Error,
"Stored fill {} contains unresolved security {}. ", kvp.first,
@@ -78,7 +86,7 @@ void FillStore::init() {
// account ids for future execution reports
subscribe<OrderDefinitionUpdateEvent>(
[this](auto const &ev) { this->on_order_definition_update(ev); });
}
} // namespace orderman
FillStore::~FillStore() {}
@@ -118,7 +118,7 @@ void OrderManager::on_fill_recovery(
boost::algorithm::trim_copy(ev.fill.account());
me->services_data.limiter.add(*ev.security, trimmed_account_name,
ev.fill.side(),
Quantity(ev.fill.last_shares()));
Quantity(ev.fill.last_shares()),false);
}
me->stored_order_sub = subscribe<StoredOrderEvent>(
@@ -16,7 +16,7 @@ OrderServicesData::OrderServicesData(OrderManager &owner,
auto &all_connections = config::get<config::JetFIX>().connection();
for (auto const &c : all_connections)
sessions.push_back(std::make_unique<SessionServicesData>(c, l));
sessions.push_back(std::make_unique<SessionServicesData>(c, l,o));
}
} // namespace orderman
} // namespace bts
@@ -48,10 +48,10 @@ struct ParentKey {
struct SessionServicesData {
SessionServicesData(config::JetFIX::Connection const &cfg,
trace::PerfLog &log)
trace::PerfLog &log, md::SecurityOracle o)
: session(cfg.name()), gw_connected(false), cfg(cfg),
governor(cfg.max_order_msgs_per_window(), cfg.window_duration_msecs()),
gw(cfg, log) {}
gw(cfg, log,o) {}
std::string session;
std::vector<jetfix::OutFIXPtr> free_buffers;
+17 -17
View File
@@ -62,29 +62,29 @@ public:
void copy_positions_from(LimitStore& source);
void add_position(const api::Security& security,
AccountID account_id, api::Side side,
Quantity quantity){
Quantity quantity, bool recurse){
SecurityLimitTree* sec_tree = _find_security(security.id());
bts_assert(sec_tree != nullptr);
sec_tree->add_position(account_id, side, quantity);
if (security.has_spread()){
if (recurse && security.has_spread()){
for (const api::ContractLeg& leg : security.spread().legs()){
api::Side effective_leg_side = side == api::Side::SIDE_BUY ? leg.side() : LimiterUtils::invert_side(leg.side());
Quantity effective_leg_qty = Quantity (quantity.value() * std::abs(leg.quantity()));
add_position (leg.security(), account_id, effective_leg_side, effective_leg_qty);
add_position (leg.security(), account_id, effective_leg_side, effective_leg_qty, recurse);
}
}
}
void remove_position(const api::Security& security,
AccountID account_id,
api::Side side, Quantity quantity){
api::Side side, Quantity quantity, bool recurse){
SecurityLimitTree* sec_tree = _find_security(security.id());
bts_assert(sec_tree != nullptr);
sec_tree->remove_position(account_id, side, quantity);
if (security.has_spread()){
if (recurse && security.has_spread()){
for (const api::ContractLeg& leg : security.spread().legs()){
api::Side effective_leg_side = side == api::Side::SIDE_BUY ? leg.side() : LimiterUtils::invert_side(leg.side());
Quantity effective_leg_qty = Quantity (quantity.value() * std::abs(leg.quantity()));
remove_position (leg.security(), account_id, effective_leg_side, effective_leg_qty);
remove_position (leg.security(), account_id, effective_leg_side, effective_leg_qty,recurse);
}
}
}
@@ -98,7 +98,7 @@ public:
//iv. account should be activated
Limiter::LimiterQueryResult can_trade(UserID user_id,
AccountID account_id) const{
Limiter::LimiterQueryResult result; result.ok = true;
Limiter::LimiterQueryResult result; result.result = Limiter::OK;
auto user_it = _user_data_map.find(user_id.value());
if (user_it != _user_data_map.end()){
const UserData& user_data = user_it->second;
@@ -106,23 +106,23 @@ public:
const api::admin::BTSAccount* account_ptr = user_data.find_account(account_id);
if (account_ptr != nullptr){
if (!account_ptr->is_active()){
result.ok=false;
result.result=Limiter::ACCOUNT_DEACTIVATED;
result.error_msg = "Account is de-activated";
}
}
else{
result.ok=false;
result.result=Limiter::NO_SUCH_ACCOUNT;
result.error_msg = "Can not find account";
}
}
else
{
result.ok=false;
result.result=Limiter::USER_DEACTIVATED;
result.error_msg = "User is de-activated";
}
}
else{
result.ok=false;
result.result=Limiter::NO_SUCH_USER;
result.error_msg = "Can not find user";
}
return result;
@@ -131,11 +131,11 @@ public:
const api::Security& security,
api::Side side,
Quantity quantity) const{
Limiter::LimiterQueryResult rv; rv.ok = true;
Limiter::LimiterQueryResult rv; rv.result = Limiter::OK;
const SecurityLimitTree* sec_tree = _find_security(security.id());
bts_assert(sec_tree != nullptr);
rv = sec_tree->can_send_new_order(account_id, side, quantity);
if (rv.ok && security.has_spread()){
if (rv.ok() && security.has_spread()){
// if its rejected we dont need to validate further,
// however if its ok and its a spread we need to check
// the legs
@@ -143,7 +143,7 @@ public:
api::Side effective_leg_side = side == api::Side::SIDE_BUY ? leg.side() : LimiterUtils::invert_side(leg.side());
Quantity effective_leg_qty = Quantity (quantity.value() * std::abs(leg.quantity()));
rv = can_send_new_order (account_id, leg.security(), effective_leg_side, effective_leg_qty);
if (!rv.ok)
if (!rv.ok())
return rv;
}
}
@@ -153,18 +153,18 @@ public:
const api::Security& security,
api::Side side,
Quantity from, Quantity to) const{
Limiter::LimiterQueryResult rv; rv.ok = true;
Limiter::LimiterQueryResult rv; rv.result = Limiter::OK;
const SecurityLimitTree* sec_tree = _find_security(security.id());
bts_assert(sec_tree != nullptr);
rv = sec_tree->can_update_order(account_id, side, from, to);
// same logic for the spread legs as above
if (rv.ok && security.has_spread()){
if (rv.ok() && security.has_spread()){
for (const api::ContractLeg& leg : security.spread().legs()){
api::Side effective_leg_side = side == api::Side::SIDE_BUY ? leg.side() : LimiterUtils::invert_side(leg.side());
Quantity effective_leg_from_qty = Quantity (from.value() * std::abs(leg.quantity()));
Quantity effective_leg_to_qty = Quantity (to.value() * std::abs(leg.quantity()));
rv = can_update_order (account_id, leg.security(), effective_leg_side, effective_leg_from_qty, effective_leg_to_qty);
if (!rv.ok)
if (!rv.ok())
return rv;
}
}
+25 -14
View File
@@ -33,45 +33,45 @@ void Limiter::add_security(api::Security const &security) {
_reset(me->limit_store->admin_data());
}
void Limiter::add(api::Security const &security, AccountID account_id,
api::Side side, Quantity quantity) {
api::Side side, Quantity quantity, bool recurse) {
if (!_contains_security(&security))
add_security(security);
if (!me->limit_store->has_account(account_id)) {
// add a temporary account, so trades can go in there
_add_account_and_reset(account_id);
}
me->limit_store->add_position(security, account_id, side, quantity);
me->limit_store->add_position(security, account_id, side, quantity, recurse);
}
void Limiter::remove(api::Security const &security, AccountID account_id,
api::Side side, Quantity quantity) {
api::Side side, Quantity quantity, bool recurse) {
if (!_contains_security(&security))
add_security(security);
if (!me->limit_store->has_account(account_id)) {
_add_account_and_reset(account_id);
}
me->limit_store->remove_position(security, account_id, side, quantity);
me->limit_store->remove_position(security, account_id, side, quantity, recurse);
}
void Limiter::add(const api::Security &security,
const std::string &account_name, api::Side side,
Quantity quantity) {
Quantity quantity, bool recurse) {
const auto *acct_ptr = bts::admin::AdminServiceUtils::find_account(
me->limit_store->admin_data(), account_name);
if (acct_ptr == nullptr)
LOG(OrderMan, Error, "Limiter::add can not find account:{}",
account_name);
else
add(security, AccountID(acct_ptr->id()), side, quantity);
add(security, AccountID(acct_ptr->id()), side, quantity, recurse);
}
void Limiter::remove(const api::Security &security,
const std::string &account_name, api::Side side,
Quantity quantity) {
Quantity quantity, bool recurse) {
const auto *acct_ptr = bts::admin::AdminServiceUtils::find_account(
me->limit_store->admin_data(), account_name);
if (acct_ptr == nullptr)
LOG(OrderMan, Error, "Limiter::remove can not find account:{}",
account_name);
else
remove(security, AccountID(acct_ptr->id()), side, quantity);
remove(security, AccountID(acct_ptr->id()), side, quantity, recurse);
}
Limiter::LimiterQueryResult Limiter::can_trade(UserID user_id,
AccountID account_id) const {
@@ -80,21 +80,32 @@ Limiter::LimiterQueryResult Limiter::can_trade(UserID user_id,
}
Limiter::LimiterQueryResult
Limiter::can_send_new_order(AccountID account_id, api::Security const &security,
api::Side side, Quantity quantity) const {
LimiterQueryResult rv = me->limit_store->can_send_new_order(
api::Side side, Quantity quantity) {
LimiterQueryResult rv =
me->limit_store->can_send_new_order(
account_id, security, side, quantity);
// rv.ok = true;
if (rv.result == Limiter::NO_SUCH_SECURITY) {
add_security(security);
rv = me->limit_store->can_send_new_order(
account_id, security, side, quantity);
}
return rv;
}
Limiter::LimiterQueryResult
Limiter::can_update_order(AccountID account_id, api::Security const &security,
api::Side side, Quantity from, Quantity to) const {
api::Side side, Quantity from, Quantity to) {
LimiterQueryResult rv;
if (from == to)
rv.ok = true;
else
rv.result = Limiter::OK;
else {
rv = me->limit_store->can_update_order(account_id, security, side, from,
to);
if (rv.result == Limiter::NO_SUCH_SECURITY) {
add_security(security);
rv = me->limit_store->can_update_order(account_id, security, side, from,
to);
}
}
return rv;
}
void Limiter::_add_account_and_reset(AccountID account_id) {
+26 -7
View File
@@ -24,26 +24,45 @@ class Limiter {
void add_securities(std::vector<api::Security const *> const &secs);
void add_security(api::Security const &new_sec);
void add(api::Security const &, AccountID, api::Side, Quantity);
void remove(api::Security const &, AccountID, api::Side, Quantity);
void add(api::Security const &, AccountID, api::Side, Quantity,
bool recurse);
void remove(api::Security const &, AccountID, api::Side, Quantity,
bool recurse);
void add(api::Security const &, std::string const & /* account_name */,
api::Side, Quantity);
api::Side, Quantity, bool recurse);
void remove(api::Security const &, std::string const & /* account_name */,
api::Side, Quantity);
api::Side, Quantity, bool recurse);
enum LimiterErrorCode {
OK,
NO_SUCH_SECURITY,
SEC_LONG_POS_BREACHED,
PROD_LONG_POS_BREACHED,
SEC_SHORT_POS_BREACHED,
PROD_SHORT_POS_BREACHED,
CLIP_SIZE_BREACHED,
ACCOUNT_DEACTIVATED,
NO_SUCH_ACCOUNT,
USER_DEACTIVATED,
NO_SUCH_USER
};
struct LimiterQueryResult {
bool ok;
std::string error_msg;
LimiterErrorCode result;
bool ok() const { return result == OK; }
bool error() const { return result != OK; }
};
LimiterQueryResult can_trade(UserID user, AccountID into_account) const;
LimiterQueryResult can_send_new_order(AccountID into_account,
api::Security const &sec,
api::Side side,
Quantity quantity) const;
Quantity quantity) ;
LimiterQueryResult can_update_order(AccountID account,
api::Security const &sec, api::Side side,
Quantity from, Quantity to) const;
Quantity from, Quantity to) ;
~Limiter();
// for testing purposes only
LimitStore &limit_store();
+32 -20
View File
@@ -16,7 +16,7 @@ public:
int short_pos_limit,
int net_pos,
Limiter::LimiterQueryResult& out_res){
out_res.ok = true;
out_res.result = Limiter::OK;
bts_assert(long_pos_limit >= 0);
bts_assert(short_pos_limit <= 0);
int net_pos_new = 0;
@@ -25,18 +25,24 @@ public:
else if(side == api::Side::SIDE_SELL)
net_pos_new = net_pos - quantity.value();
if (net_pos_new > long_pos_limit && side==api::Side::SIDE_BUY){
out_res.ok = false;
if (is_security_level)
if (is_security_level) {
out_res.result = Limiter::SEC_LONG_POS_BREACHED;
out_res.error_msg = fmt::format("Security Long Position (Max {} Current {})", long_pos_limit, net_pos);
else
}
else {
out_res.result = Limiter::PROD_LONG_POS_BREACHED;
out_res.error_msg = fmt::format("Product Long Position (Max {} Current {})", long_pos_limit, net_pos);
}
}
else if (net_pos_new < short_pos_limit && side==api::Side::SIDE_SELL){
out_res.ok = false;
if (is_security_level)
if (is_security_level) {
out_res.result = Limiter::SEC_SHORT_POS_BREACHED;
out_res.error_msg = fmt::format("Security Short Position (Max {} Current {})", short_pos_limit, net_pos);
else
}
else {
out_res.result = Limiter::PROD_SHORT_POS_BREACHED;
out_res.error_msg = fmt::format("Product Short Position (Max {} Current {})", short_pos_limit, net_pos);
}
}
}
static void can_update_order_net (api::Side side,
@@ -47,7 +53,7 @@ public:
int short_pos_limit,
int net_pos,
Limiter::LimiterQueryResult& out_res){
out_res.ok = true;
out_res.result = Limiter::OK;
if (to.value() > from.value()){
int net_pos_new = 0;
uint32_t diff = to.value() - from.value();
@@ -56,18 +62,24 @@ public:
else if(side == api::Side::SIDE_SELL)
net_pos_new = net_pos - diff;
if (net_pos_new > long_pos_limit){
out_res.ok = false;
if (is_security_level)
if (is_security_level) {
out_res.result = Limiter::SEC_LONG_POS_BREACHED;
out_res.error_msg = fmt::format("Security Long Position (Max {} Current {})", long_pos_limit, net_pos);
else
}
else {
out_res.result = Limiter::PROD_LONG_POS_BREACHED;
out_res.error_msg = fmt::format("Product Long Position (Max {} Current {})", long_pos_limit, net_pos);
}
}
else if (net_pos_new < short_pos_limit){
out_res.ok = false;
if (is_security_level)
if (is_security_level) {
out_res.result = Limiter::SEC_SHORT_POS_BREACHED;
out_res.error_msg = fmt::format("Security Short Position (Max {} Current {})", short_pos_limit, net_pos);
else
}
else {
out_res.result = Limiter::SEC_SHORT_POS_BREACHED;
out_res.error_msg = fmt::format("Product Short Position (Max {} Current {})", short_pos_limit, net_pos);
}
}
}
}
@@ -78,13 +90,13 @@ public:
uint32_t long_pos,
uint32_t short_pos,
Limiter::LimiterQueryResult& out_res){
out_res.ok = true;
out_res.result = Limiter::OK;
if(side == api::Side::SIDE_BUY){
if(long_pos + quantity.value() > long_pos_limit){
out_res.error_msg = fmt::format("Long Position (Abs) "
"(Max {} Current {})",
long_pos_limit,long_pos);
out_res.ok = false;
out_res.result = Limiter::SEC_LONG_POS_BREACHED;
}
}
else{
@@ -92,7 +104,7 @@ public:
out_res.error_msg = fmt::format("Short Position (Abs) "
"(Max {} Current {})",
short_pos_limit,short_pos);
out_res.ok = false;
out_res.result = Limiter::SEC_SHORT_POS_BREACHED;
}
}
}
@@ -104,7 +116,7 @@ public:
uint32_t long_pos,
uint32_t short_pos,
Limiter::LimiterQueryResult& out_res){
out_res.ok = true;
out_res.result = Limiter::OK;
if (to.value() > from.value()){
uint32_t diff = to.value() - from.value();
if(side == api::Side::SIDE_BUY){
@@ -112,7 +124,7 @@ public:
out_res.error_msg = fmt::format("Long Position (Abs) "
"(Max {} Current {})",
long_pos_limit,long_pos);
out_res.ok = false;
out_res.result = Limiter::SEC_LONG_POS_BREACHED;
}
}
else{
@@ -120,7 +132,7 @@ public:
out_res.error_msg = fmt::format("Short Position (Abs) "
"(Max {} Current {})",
short_pos_limit,short_pos);
out_res.ok = false;
out_res.result = Limiter::SEC_SHORT_POS_BREACHED;
}
}
}
+6 -6
View File
@@ -64,9 +64,9 @@ public:
// risk limit checks
////////////////////
Limiter::LimiterQueryResult can_send_new_order(api::Side side, Quantity quantity) const{
Limiter::LimiterQueryResult result;result.ok=true;
Limiter::LimiterQueryResult result;result.result= Limiter::OK;
if (quantity.value() > _clip_size){
result.ok = false;
result.result = Limiter::CLIP_SIZE_BREACHED;
result.error_msg = fmt::format("Security Clip Size (Max {} Qty {})", _clip_size, quantity.value());
return result;
}
@@ -74,14 +74,14 @@ public:
LimiterUtils::can_send_new_order_net (side, quantity, true,
_long_pos_limit, _short_pos_limit,
_net_pos, result);
if (result.ok)
if (result.ok())
result = _product_node.can_send_new_order(side, quantity);
return result;
}
Limiter::LimiterQueryResult can_update_order(api::Side side, Quantity from, Quantity to) const{
Limiter::LimiterQueryResult result;result.ok=true;
Limiter::LimiterQueryResult result;result.result=Limiter::OK;
if (to.value() > _clip_size){
result.ok = false;
result.result = Limiter::CLIP_SIZE_BREACHED;
result.error_msg = fmt::format("Security Clip Size (Max {} Qty {})", _clip_size, to.value());
return result;
}
@@ -89,7 +89,7 @@ public:
LimiterUtils::can_update_order_net (side, from, to, true,
_long_pos_limit, _short_pos_limit,
_net_pos, result);
if (result.ok)
if (result.ok())
result = _product_node.can_update_order(side, from, to);
return result;
}
+10 -10
View File
@@ -270,8 +270,7 @@ NativeOrder::on_init_from_exe_report(ExecutionReport const &er,
if (er.max_show.value() == 0) {
def.mutable_custom_def()->mutable_limit();
state.mutable_limit();
}
else {
} else {
def.mutable_custom_def()->mutable_glbx_iceberg();
state.mutable_glbx_iceberg();
}
@@ -370,7 +369,7 @@ NativeOrder::_on_activate(log::Level reject_level, log::Level transition_level,
auto lqr = me->services->limiter().can_trade(
UserID(me->definition.user_id()),
AccountID(me->definition.account_id()));
if (!lqr.ok) {
if (!lqr.ok()) {
LOG(OrderMan, reject_level,
"Unauthorized order attempted. User: {} on "
"Account: {}. Msg: {} Order: {}. Dropping order",
@@ -387,7 +386,7 @@ NativeOrder::_on_activate(log::Level reject_level, log::Level transition_level,
AccountID(me->definition.account_id()), *me->sec, me->definition.side(),
q);
if (!lqr.ok) {
if (!lqr.ok()) {
LOG(OrderMan, reject_level,
"New order would breach limit. User: {} Account: "
"{} Msg: {} Order: {}. Dropping order",
@@ -587,7 +586,7 @@ OrderOpResult NativeOrder::on_update(api::orderman::Update const &update) {
Quantity(current_working_qty));
// are we under the position limits?
if (!lqr.ok) {
if (!lqr.ok()) {
auto msg = fmt::format("Cannot update order due to limit breach: {}",
lqr.error_msg);
LOG(OrderMan, Error,
@@ -1047,12 +1046,14 @@ void NativeOrder::update_working_position() {
me->services->limiter().add(
*me->sec, AccountID(me->definition.account_id()),
me->definition.side(),
Quantity(current_position.value() - previous_position.value()));
Quantity(current_position.value() - previous_position.value()),
true);
else if (previous_position.value() > current_position.value())
me->services->limiter().remove(
*me->sec, AccountID(me->definition.account_id()),
me->definition.side(),
Quantity(previous_position.value() - current_position.value()));
Quantity(previous_position.value() - current_position.value()),
true);
} else
LOG(OrderMan, Error,
"Order {} ignored for purposes of limit enforcing "
@@ -1194,9 +1195,8 @@ void NativeOrder::init_fix() {
securities::encode_exchange_price(*me->sec, Price(stop_px)).value();
create_encoding.add<fix::StopPx>(encoded_stop_px,
me->price_encoding_precision, -1);
me->update_stop_px_bm=
update_encoding.add<fix::StopPx>(encoded_stop_px,
me->price_encoding_precision, -1);
me->update_stop_px_bm = update_encoding.add<fix::StopPx>(
encoded_stop_px, me->price_encoding_precision, -1);
// price
me->create_price_bm = create_encoding.add<fix::Price>(