From 0cc30a7934d2b6a1448c42c413d53d69850da1ac Mon Sep 17 00:00:00 2001 From: Pedro Pinto Date: Mon, 6 May 2019 17:35:20 -0400 Subject: [PATCH 001/121] Added server side support for GLBX Iceberg. --- src/lib/api/services/OrderService.proto | 12 ++- src/lib/jetfix/codec/FieldDefinitions.h | 1 + .../orderman/order_factory/OrderFactory.cpp | 1 + .../orderman/orderman_utils/fix_order_codec.h | 1 + src/lib/orderman/orders/NativeOrder.cpp | 73 +++++++++++++------ 5 files changed, 62 insertions(+), 26 deletions(-) diff --git a/src/lib/api/services/OrderService.proto b/src/lib/api/services/OrderService.proto index b16c4d584..798d8f174 100644 --- a/src/lib/api/services/OrderService.proto +++ b/src/lib/api/services/OrderService.proto @@ -81,10 +81,11 @@ message CustomOrderDefinition oneof custom { // native order types // https://www.cmegroup.com/confluence/display/EPICSANDBOX/New+Order - MarketOrderDefinition market = 11; // OrdType = 1 - LimitOrderDefinition limit = 12; // OrdType = 2 - StopOrderDefinition stop = 13; // OrdType = 3 - StopLimitOrderDefinition stop_limit = 14; // OrdType = 4 + MarketOrderDefinition market = 11; // OrdType = 1 + LimitOrderDefinition limit = 12; // OrdType = 2 + StopOrderDefinition stop = 13; // OrdType = 3 + StopLimitOrderDefinition stop_limit = 14; // OrdType = 4 + GLBXIcebergOrderDefinition glbx_iceberg = 28; // OrdType = 4, with max_show_quantity !=0 IcebergOrderDefinition iceberg = 15; SpreaderOrderDefinition spreader = 16; @@ -114,6 +115,9 @@ message StopOrderDefinition { message StopLimitOrderDefinition { double stop_price = 1; } +message GLBXIcebergOrderDefinition { + uint32 max_show_quantity = 1; +} message IcebergOrderDefinition { double show_quantity = 1; uint32 pause_between_orders_ms = 3; diff --git a/src/lib/jetfix/codec/FieldDefinitions.h b/src/lib/jetfix/codec/FieldDefinitions.h index 3bcfd393f..f1bacd704 100644 --- a/src/lib/jetfix/codec/FieldDefinitions.h +++ b/src/lib/jetfix/codec/FieldDefinitions.h @@ -127,6 +127,7 @@ typedef Group OrderQty; +typedef IntField<210> MaxShow; typedef FloatField<44> Price; typedef FloatField<99> StopPx; typedef IntField<9003> UDFSupportIndicator; diff --git a/src/lib/orderman/order_factory/OrderFactory.cpp b/src/lib/orderman/order_factory/OrderFactory.cpp index 2cbac4d4b..461ea669c 100644 --- a/src/lib/orderman/order_factory/OrderFactory.cpp +++ b/src/lib/orderman/order_factory/OrderFactory.cpp @@ -147,6 +147,7 @@ Order &OrderFactory::allocate(OrderKey k, OrderKey pk, case CustomOrderDefinition::kLimit: case CustomOrderDefinition::kStop: case CustomOrderDefinition::kStopLimit: + case CustomOrderDefinition::kGlbxIceberg: order_ptr.reset(new NativeOrder()); break; case CustomOrderDefinition::kIceberg: diff --git a/src/lib/orderman/orderman_utils/fix_order_codec.h b/src/lib/orderman/orderman_utils/fix_order_codec.h index 9c14d5687..5b69c7d1b 100644 --- a/src/lib/orderman/orderman_utils/fix_order_codec.h +++ b/src/lib/orderman/orderman_utils/fix_order_codec.h @@ -16,6 +16,7 @@ namespace orderman { constexpr int clordid_sz() {return 20;} constexpr int price_sz() {return 17;} constexpr int quantity_sz() {return 6;} +constexpr int max_show_sz() {return 6;} inline void encode_clorid(OrderKey ok, SecurityKey sk,std::string *out) { u32_toa(sk.value(),&((*out)[0]),clordid_sz()/2); diff --git a/src/lib/orderman/orders/NativeOrder.cpp b/src/lib/orderman/orders/NativeOrder.cpp index 2103fd02f..ffd6ccdc9 100644 --- a/src/lib/orderman/orders/NativeOrder.cpp +++ b/src/lib/orderman/orders/NativeOrder.cpp @@ -48,6 +48,7 @@ struct NativeOrder::PImpl { jetfix::FIXFragment update_encoding; jetfix::FIXFragment::Bookmark update_price_bm; jetfix::FIXFragment::Bookmark update_quantity_bm; + jetfix::FIXFragment::Bookmark update_max_show_bm; jetfix::FIXFragment::Bookmark update_transact_time_bm; @@ -92,6 +93,17 @@ void NativeOrder::on_init_new(OrderDefinition const &def, case CustomOrderDefinition::kStopLimit: me->state.mutable_stop_limit(); break; + case CustomOrderDefinition::kGlbxIceberg: { + auto &ib = def.custom_def().glbx_iceberg(); + if (ib.max_show_quantity() == 0 || + ib.max_show_quantity() > pow(10, max_show_sz()) - 1) { + auto msg = fmt::format("Invalid max show size: {}", ib.max_show_quantity()); + me->state.set_last_msg(msg); + transition(ORDER_STATUS_REJECTED_BY_SPARK, msg); + return; + } + break; + } default: bts_assert(false); break; @@ -103,26 +115,30 @@ void NativeOrder::on_init_new(OrderDefinition const &def, auto msg = fmt::format("Unknown security: {}", def.security_key()); me->state.set_last_msg(msg); transition(ORDER_STATUS_REJECTED_BY_SPARK, msg); - } else { - auto rq = serv.order_router.get_routing_record( - UserID(def.user_id()), AccountID(def.account_id()), *me->sec); - if (rq.has_route) { - transition(ORDER_STATUS_INACTIVE); - auto &native_state = get_native_state(me->state); - *native_state.mutable_routing_record() = rq.routing_record; - - init_fix(); - } else { - auto msg = fmt::format("Could not route order: {}", rq.error_msg); - LOG(OrderMan, Error, "{}", msg); - transition(ORDER_STATUS_REJECTED_BY_SPARK, msg); - } + return; } + + auto rq = serv.order_router.get_routing_record( + UserID(def.user_id()), AccountID(def.account_id()), *me->sec); + + if (!rq.has_route) { + auto msg = fmt::format("Could not route order: {}", rq.error_msg); + LOG(OrderMan, Error, "{}", msg); + transition(ORDER_STATUS_REJECTED_BY_SPARK, msg); + return; + } + + transition(ORDER_STATUS_INACTIVE); + auto &native_state = get_native_state(me->state); + *native_state.mutable_routing_record() = rq.routing_record; + + init_fix(); } void NativeOrder::on_init_recovered(OrderRecord const &rec, const PreActivationOrderServices &serv) { - // order was recovered from our orders database or from an execution report + // order was recovered from our orders database or from an execution + // report auto const &def = rec.def(); @@ -733,7 +749,8 @@ OrderOpResult NativeOrder::on_cancel() { case ORDER_STATUS_DISCONNECTED: { me->buffered_cancel = true; LOG(OrderMan, Warn, - "Cannot cancel order {} while in the disconnected state. Buffering " + "Cannot cancel order {} while in the disconnected state. " + "Buffering " "cancel", key().value()); auto msg = "Order will be canceled when re-connected"; @@ -863,15 +880,13 @@ void NativeOrder::on_exe_report(const NewExecutionReportEvent &er) { "Applying buffered cancel to order {}, on first ack", key().value()); on_cancel(); - } - else if (me->buffered_update.has_value()) { + } else if (me->buffered_update.has_value()) { auto [p, q] = me->buffered_update.value(); me->buffered_update.reset(); LOG(OrderMan, Warn, "Applying buffered update ({}@{}) to order {}, on " "first ack", - q.value(),p.value(), - key().value()); + q.value(), p.value(), key().value()); on_update(p, q); } } @@ -951,8 +966,7 @@ void NativeOrder::on_gateway_status(const GatewayStatusChangeEvent &ev) { LOG(OrderMan, Warn, "Applying buffered update ({}@{}) to order {}, on " "re-connection", - q.value(),p.value(), - key().value()); + q.value(), p.value(), key().value()); on_update(p, q); } } @@ -1156,6 +1170,21 @@ void NativeOrder::init_fix() { 0, me->price_encoding_precision, price_sz()); break; } + case CustomOrderDefinition::kGlbxIceberg: { + + // type, still a limit order but we use a show quantity + create_encoding.add(NATIVE_ORDER_TYPE_STOP_LIMIT); + update_encoding.add(NATIVE_ORDER_TYPE_STOP_LIMIT); + cancel_encoding.add(NATIVE_ORDER_TYPE_STOP_LIMIT); + + // max show qty + auto max_show_qty = def.custom_def().glbx_iceberg().max_show_quantity(); + create_encoding.add(max_show_qty); + me->update_max_show_bm = + update_encoding.add(max_show_qty, max_show_sz()); + break; + } + default: bts_assert(false); break; From f60afe119672a7979c062a88dea76110cd32ea46 Mon Sep 17 00:00:00 2001 From: Pedro Pinto Date: Tue, 7 May 2019 13:57:06 -0400 Subject: [PATCH 002/121] Woark in progress to add support for update to stop limit and glbx iceberg. --- src/lib/orderman/orders/NativeOrder.cpp | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/src/lib/orderman/orders/NativeOrder.cpp b/src/lib/orderman/orders/NativeOrder.cpp index ffd6ccdc9..a41e3470d 100644 --- a/src/lib/orderman/orders/NativeOrder.cpp +++ b/src/lib/orderman/orders/NativeOrder.cpp @@ -49,6 +49,7 @@ struct NativeOrder::PImpl { jetfix::FIXFragment::Bookmark update_price_bm; jetfix::FIXFragment::Bookmark update_quantity_bm; jetfix::FIXFragment::Bookmark update_max_show_bm; + jetfix::FIXFragment::Bookmark update_stop_px_bm; jetfix::FIXFragment::Bookmark update_transact_time_bm; @@ -537,6 +538,16 @@ OrderOpResult NativeOrder::on_activate(Price p, Quantity q, } OrderOpResult NativeOrder::on_update(Price p, Quantity q) { + api::orderman::Update update; + update.set_price(p.value()); + update.set_quantity(q.value()); + return on_update(update); +} + +OrderOpResult NativeOrder::on_update(api::orderman::Update const &update) { + auto p = Price(update.price()); + auto q = Quantity(update.quantity()); + auto scoped_log = me->services->log_scope("NativeOrder::on_update", key().value()); auto result = OrderOpResult{}; @@ -648,10 +659,7 @@ OrderOpResult NativeOrder::on_update(Price p, Quantity q) { } report_state(); return result; -} -OrderOpResult NativeOrder::on_update(api::orderman::Update const &update) { - return on_update(Price(update.price()), Quantity(update.quantity())); } OrderOpResult NativeOrder::on_detach() { return OrderOpResult{}; } @@ -1143,8 +1151,9 @@ void NativeOrder::init_fix() { securities::encode_exchange_price(*me->sec, Price(stop_px)).value(); create_encoding.add(encoded_stop_px, me->price_encoding_precision, -1); - update_encoding.add(encoded_stop_px, - me->price_encoding_precision, -1); + me->update_stop_px_bm = + update_encoding.add(encoded_stop_px, + me->price_encoding_precision, price_sz()); break; } From 8c9522e22b9b2c40dc2332de98c81794a9d57b35 Mon Sep 17 00:00:00 2001 From: Pedro Pinto Date: Tue, 7 May 2019 13:57:06 -0400 Subject: [PATCH 003/121] Work in progress to add support for update to stop limit and glbx iceberg. --- src/lib/api/services/OrderService.proto | 1 + src/lib/orderman/orders/NativeOrder.cpp | 93 +++++++++++++++++-------- src/lib/orderman/orders/NativeOrder.h | 3 + 3 files changed, 69 insertions(+), 28 deletions(-) diff --git a/src/lib/api/services/OrderService.proto b/src/lib/api/services/OrderService.proto index 798d8f174..71a266aed 100644 --- a/src/lib/api/services/OrderService.proto +++ b/src/lib/api/services/OrderService.proto @@ -471,6 +471,7 @@ enum OrderError ORDER_ERROR_UNAUTHORIZED = 14; ORDER_ERROR_INVALID_ORDER_DEFINITON = 15; ORDER_ERROR_UNEXPECTED_CHILD_STATE = 16; + ORDER_ERROR_INVALID_PARAMETER = 17; } // Trigger Condition ///////////////////////////////// diff --git a/src/lib/orderman/orders/NativeOrder.cpp b/src/lib/orderman/orders/NativeOrder.cpp index ffd6ccdc9..199b6bc5e 100644 --- a/src/lib/orderman/orders/NativeOrder.cpp +++ b/src/lib/orderman/orders/NativeOrder.cpp @@ -49,6 +49,7 @@ struct NativeOrder::PImpl { jetfix::FIXFragment::Bookmark update_price_bm; jetfix::FIXFragment::Bookmark update_quantity_bm; jetfix::FIXFragment::Bookmark update_max_show_bm; + jetfix::FIXFragment::Bookmark update_stop_px_bm; jetfix::FIXFragment::Bookmark update_transact_time_bm; @@ -97,7 +98,8 @@ void NativeOrder::on_init_new(OrderDefinition const &def, auto &ib = def.custom_def().glbx_iceberg(); if (ib.max_show_quantity() == 0 || ib.max_show_quantity() > pow(10, max_show_sz()) - 1) { - auto msg = fmt::format("Invalid max show size: {}", ib.max_show_quantity()); + auto msg = + fmt::format("Invalid max show size: {}", ib.max_show_quantity()); me->state.set_last_msg(msg); transition(ORDER_STATUS_REJECTED_BY_SPARK, msg); return; @@ -537,6 +539,16 @@ OrderOpResult NativeOrder::on_activate(Price p, Quantity q, } OrderOpResult NativeOrder::on_update(Price p, Quantity q) { + api::orderman::Update update; + update.set_price(p.value()); + update.set_quantity(q.value()); + return on_update(update); +} + +OrderOpResult NativeOrder::on_update(api::orderman::Update const &update) { + auto p = Price(update.price()); + auto q = Quantity(update.quantity()); + auto scoped_log = me->services->log_scope("NativeOrder::on_update", key().value()); auto result = OrderOpResult{}; @@ -567,16 +579,7 @@ OrderOpResult NativeOrder::on_update(Price p, Quantity q) { "Order update failed due to limit breach. Order: {} Msg: {}", key().value(), lqr.error_msg); - // if we are not being driven by an algo we just signal the - // error but keep running. otherwise we stop. - if (parent_key() == null_order_key()) - transition(me->state.status(), msg); - else { - me->state.set_last_msg(msg); - on_cancel(); - } - result.error = api::orderman::ORDER_ERROR_LIMIT_BREACH; - result.error_msg = lqr.error_msg; + ignore_or_cancel(msg, ORDER_ERROR_LIMIT_BREACH, &result); break; } @@ -587,21 +590,46 @@ OrderOpResult NativeOrder::on_update(Price p, Quantity q) { key().value()); auto msg = "Messaging threshold breached"; - // if we are not being driven by an algo we just signal the - // error but keep running. otherwise we stop. - if (parent_key() == null_order_key()) - transition(me->state.status(), msg); - else { - me->state.set_last_msg(msg); - on_cancel(); - } - result.error = api::orderman::ORDER_ERROR_MESSAGING_LIMIT_BREACH; - result.error_msg = msg; + ignore_or_cancel(msg, ORDER_ERROR_MESSAGING_LIMIT_BREACH, &result); break; } - // all good auto &encoding = me->update_encoding; + + if (update.has_custom()) { + bts_assert(update.custom().custom_case() == + me->definition.custom_def().custom_case()); + + if (update.custom().custom_case() == + CustomOrderDefinition::kStopLimit) { + auto const &stop_limit = update.custom().stop_limit(); + auto stop_px = Price(stop_limit.stop_price()); + me->update_stop_px_bm = encoding.overwrite( + me->update_stop_px_bm, + securities::encode_exchange_price(*me->sec, stop_px).value(), + me->price_encoding_precision, price_sz()); + break; + } + + if (update.custom().custom_case() == + CustomOrderDefinition::kGlbxIceberg) { + + // GLBX Iceberg + auto const &ib = update.custom().glbx_iceberg(); + if (ib.max_show_quantity() == 0 || + ib.max_show_quantity() > pow(10, max_show_sz()) - 1) { + auto msg = fmt::format("Invalid max show size: {}", + ib.max_show_quantity()); + + ignore_or_cancel(msg, ORDER_ERROR_INVALID_PARAMETER, &result); + break; + } + me->update_max_show_bm = encoding.overwrite( + me->update_max_show_bm, ib.max_show_quantity(), quantity_sz()); + } + } + + // All good me->update_price_bm = encoding.overwrite( me->update_price_bm, securities::encode_exchange_price(*me->sec, p).value(), @@ -650,10 +678,6 @@ OrderOpResult NativeOrder::on_update(Price p, Quantity q) { return result; } -OrderOpResult NativeOrder::on_update(api::orderman::Update const &update) { - return on_update(Price(update.price()), Quantity(update.quantity())); -} - OrderOpResult NativeOrder::on_detach() { return OrderOpResult{}; } void NativeOrder::on_attached( @@ -1143,8 +1167,8 @@ void NativeOrder::init_fix() { securities::encode_exchange_price(*me->sec, Price(stop_px)).value(); create_encoding.add(encoded_stop_px, me->price_encoding_precision, -1); - update_encoding.add(encoded_stop_px, - me->price_encoding_precision, -1); + me->update_stop_px_bm = update_encoding.add( + encoded_stop_px, me->price_encoding_precision, price_sz()); break; } @@ -1345,5 +1369,18 @@ NativeOrder::get_native_state(api::orderman::OrderState &st) { bts_assert(false); return *st.mutable_stop_limit()->mutable_native_state(); } + +void NativeOrder::ignore_or_cancel(std::string const &msg, + api::orderman::OrderError error, + OrderOpResult *out) { + if (parent_key() == null_order_key()) + transition(me->state.status(), msg); + else { + me->state.set_last_msg(msg); + on_cancel(); + } + *out = {error, msg}; +} + } // namespace orderman } // namespace bts diff --git a/src/lib/orderman/orders/NativeOrder.h b/src/lib/orderman/orders/NativeOrder.h index 8f6f0fbc0..472c1627a 100644 --- a/src/lib/orderman/orders/NativeOrder.h +++ b/src/lib/orderman/orders/NativeOrder.h @@ -68,6 +68,9 @@ class NativeOrder : public Order { bool terminal_state(api::orderman::OrderStatus status) const; api::orderman::NativeOrderState & get_native_state(api::orderman::OrderState &); + void ignore_or_cancel(std::string const &, + api::orderman::OrderError, + OrderOpResult *out) ; }; } // namespace orderman From 910c04544bb589572e1b7a8960f81c1b56751381 Mon Sep 17 00:00:00 2001 From: Pedro Pinto Date: Wed, 8 May 2019 16:27:23 -0400 Subject: [PATCH 004/121] Code complete on update of stop limit and iceberg qty. --- src/lib/orderman/orders/NativeOrder.cpp | 37 +++++++++++++------------ 1 file changed, 19 insertions(+), 18 deletions(-) diff --git a/src/lib/orderman/orders/NativeOrder.cpp b/src/lib/orderman/orders/NativeOrder.cpp index 199b6bc5e..e8063aab5 100644 --- a/src/lib/orderman/orders/NativeOrder.cpp +++ b/src/lib/orderman/orders/NativeOrder.cpp @@ -81,6 +81,14 @@ void NativeOrder::on_init_new(OrderDefinition const &def, // order was just created. me->definition = def; + me->sec = serv.sec_db.resolve(SecurityKey(def.security_key())); + if (!me->sec) { + auto msg = fmt::format("Unknown security: {}", def.security_key()); + me->state.set_last_msg(msg); + transition(ORDER_STATUS_REJECTED_BY_SPARK, msg); + return; + } + switch (def.custom_def().custom_case()) { case CustomOrderDefinition::kMarket: me->state.mutable_market(); @@ -104,6 +112,12 @@ void NativeOrder::on_init_new(OrderDefinition const &def, transition(ORDER_STATUS_REJECTED_BY_SPARK, msg); return; } + if (me->sec->exchange() != api::VENUE_GLBX) { + auto msg = "Order type supported in this venue"; + me->state.set_last_msg(msg); + transition(ORDER_STATUS_REJECTED_BY_SPARK, msg); + return; + } break; } default: @@ -111,15 +125,6 @@ void NativeOrder::on_init_new(OrderDefinition const &def, break; } - me->sec = serv.sec_db.resolve(SecurityKey(def.security_key())); - - if (!me->sec) { - auto msg = fmt::format("Unknown security: {}", def.security_key()); - me->state.set_last_msg(msg); - transition(ORDER_STATUS_REJECTED_BY_SPARK, msg); - return; - } - auto rq = serv.order_router.get_routing_record( UserID(def.user_id()), AccountID(def.account_id()), *me->sec); @@ -608,11 +613,8 @@ OrderOpResult NativeOrder::on_update(api::orderman::Update const &update) { me->update_stop_px_bm, securities::encode_exchange_price(*me->sec, stop_px).value(), me->price_encoding_precision, price_sz()); - break; - } - - if (update.custom().custom_case() == - CustomOrderDefinition::kGlbxIceberg) { + } else if (update.custom().custom_case() == + CustomOrderDefinition::kGlbxIceberg) { // GLBX Iceberg auto const &ib = update.custom().glbx_iceberg(); @@ -1167,8 +1169,6 @@ void NativeOrder::init_fix() { securities::encode_exchange_price(*me->sec, Price(stop_px)).value(); create_encoding.add(encoded_stop_px, me->price_encoding_precision, -1); - me->update_stop_px_bm = update_encoding.add( - encoded_stop_px, me->price_encoding_precision, price_sz()); break; } @@ -1184,8 +1184,9 @@ void NativeOrder::init_fix() { securities::encode_exchange_price(*me->sec, Price(stop_px)).value(); create_encoding.add(encoded_stop_px, me->price_encoding_precision, -1); - update_encoding.add(encoded_stop_px, - me->price_encoding_precision, -1); + me->update_stop_px_bm= + update_encoding.add(encoded_stop_px, + me->price_encoding_precision, -1); // price me->create_price_bm = create_encoding.add( From 3be2aedfe3fe463e9ef7b72b5323ec11595cff04 Mon Sep 17 00:00:00 2001 From: Pedro Pinto Date: Thu, 9 May 2019 08:59:59 -0400 Subject: [PATCH 005/121] Work in progress for manual fills --- src/lib/api/services/PositionService.proto | 37 +++++++++- .../sparkd/position/PositionServiceServer.cpp | 18 ++++- .../sparkd/position/PositionServiceServer.h | 69 ++++++++++++------- 3 files changed, 94 insertions(+), 30 deletions(-) diff --git a/src/lib/api/services/PositionService.proto b/src/lib/api/services/PositionService.proto index 68263734c..9265c2da6 100644 --- a/src/lib/api/services/PositionService.proto +++ b/src/lib/api/services/PositionService.proto @@ -3,6 +3,7 @@ package bts.api.position; option csharp_namespace="bts.api.position"; import "lib/api/apioptions.proto"; import "lib/api/apicore.proto"; +import "lib/domain_model/Security.proto"; message PositionUnit{ int32 record_id = 1 [(bts.api.key)=true]; @@ -18,8 +19,38 @@ message PositionUnit{ double day_short_avg_price = 100; } +message ManualFill { + int32 record_id = 1 [(bts.api.key)=true]; + uint32 security_key = 2; + uint32 account_id = 3; + uint32 user_id = 4; + api.Side side = 5; + uint32 quantity = 6; + double price = 7; +} + +message ManualFillBatch { + repeated ManualFill manual_fill = 1; +} + +message PositionServiceOpResponse { + bool ok = 1; + string msg = 2; +} + +message PositionServiceResponse { + repeated PositionServiceOpResponse response = 1; +} + service PositionService { - option (bts.api.service_description) = "Provides Position and PNL, per security, per account"; - // subscription - rpc position_data(bts.api.VoidMessage) returns (stream PositionUnit); + option (bts.api.service_description) = "Provides Position and PNL, per security, per account"; + + rpc create_manual_fill (ManualFillBatch) returns (PositionServiceResponse); + rpc update_manual_fill (ManualFillBatch) returns (PositionServiceResponse); + rpc remove_manual_fill (ManualFillBatch) returns (PositionServiceResponse); + + // subscription + rpc manual_fills(bts.api.VoidMessage) returns (stream ManualFill); + rpc position_data(bts.api.VoidMessage) returns (stream PositionUnit); + } \ No newline at end of file diff --git a/src/server/sparkd/position/PositionServiceServer.cpp b/src/server/sparkd/position/PositionServiceServer.cpp index 49706a39c..94131b71b 100644 --- a/src/server/sparkd/position/PositionServiceServer.cpp +++ b/src/server/sparkd/position/PositionServiceServer.cpp @@ -160,4 +160,20 @@ std::string PositionServiceServer::_fill_account(const Fill& fill){ } PositionServiceServer::~PositionServiceServer(){ } -}} \ No newline at end of file + +void PositionServiceServer::create_manual_fill( + const ::bts::api::position::ManualFillBatch &, + const api::Response<::bts::api::position::PositionServiceResponse> &) +{} + +void PositionServiceServer::update_manual_fill( + const ::bts::api::position::ManualFillBatch &, + const api::Response<::bts::api::position::PositionServiceResponse> &) +{} + +void PositionServiceServer::remove_manual_fill( + const ::bts::api::position::ManualFillBatch &, + const api::Response<::bts::api::position::PositionServiceResponse> &) +{} + +}} diff --git a/src/server/sparkd/position/PositionServiceServer.h b/src/server/sparkd/position/PositionServiceServer.h index 67f7b1384..e1d4f3242 100644 --- a/src/server/sparkd/position/PositionServiceServer.h +++ b/src/server/sparkd/position/PositionServiceServer.h @@ -1,42 +1,59 @@ #pragma once -#include #include "lib/api/services/PositionService.server.pb.h" #include "lib/utils/bts_no_copy.h" - +#include namespace bts { -namespace api {class ServiceContext;} -namespace api {namespace orderman {class Fill;}} -namespace position{ +namespace api { +class ServiceContext; +} +namespace api { +namespace orderman { +class Fill; +} +} // namespace api +namespace position { -class PositionServiceServer : public bts::api::position::PositionServiceBase -{ -private: +class PositionServiceServer : public bts::api::position::PositionServiceBase { + private: struct PImpl; std::unique_ptr me; -private: + + private: // this is the function that aggrgates the // position. is_add is to indicate if a row // is added or removed - const bts::api::position::PositionUnit& _on_fill ( - const bts::api::orderman::Fill& fill, - bool is_add); + const bts::api::position::PositionUnit & + _on_fill(const bts::api::orderman::Fill &fill, bool is_add); // given an account and key, finds (or adds if absent) // the PositionUnit entry - bts::api::position::PositionUnit& _get_or_add (const std::string& acct, - uint32_t sec_id); - static void _avg_price_qty(double base_p, - double base_q, - double fill_p, - double fill_q, - bool is_add, - const std::string& fill_id, - double& avg_price, - double& tot_qty); - static std::string _fill_account(const bts::api::orderman::Fill& fill); -public: - PositionServiceServer(bts::api::ServiceContext& context); + bts::api::position::PositionUnit &_get_or_add(const std::string &acct, + uint32_t sec_id); + static void _avg_price_qty(double base_p, double base_q, double fill_p, + double fill_q, bool is_add, + const std::string &fill_id, double &avg_price, + double &tot_qty); + static std::string _fill_account(const bts::api::orderman::Fill &fill); + + public: + PositionServiceServer(bts::api::ServiceContext &context); + + void create_manual_fill( + const ::bts::api::position::ManualFillBatch &, + const api::Response<::bts::api::position::PositionServiceResponse> &) + override; + + void update_manual_fill( + const ::bts::api::position::ManualFillBatch &, + const api::Response<::bts::api::position::PositionServiceResponse> &) + override; + + void remove_manual_fill( + const ::bts::api::position::ManualFillBatch &, + const api::Response<::bts::api::position::PositionServiceResponse> &) + override; ~PositionServiceServer(); }; -}} +} // namespace position +} // namespace bts From 2f23292836f60785999ab12b84241448cb1748d4 Mon Sep 17 00:00:00 2001 From: Pedro Pinto Date: Thu, 9 May 2019 09:12:37 -0400 Subject: [PATCH 006/121] Pos server --- src/server/sparkd/position/PositionServiceServer.cpp | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/server/sparkd/position/PositionServiceServer.cpp b/src/server/sparkd/position/PositionServiceServer.cpp index 94131b71b..82b88f403 100644 --- a/src/server/sparkd/position/PositionServiceServer.cpp +++ b/src/server/sparkd/position/PositionServiceServer.cpp @@ -4,6 +4,7 @@ #include "lib/api/public/ServiceContext.h" #include "lib/logging/logging.h" #include "lib/utils/PBUtils.h" +#include "lib/persistence/SqliteStore.h" #include #include @@ -23,7 +24,9 @@ struct PositionServiceServer::PImpl { bts::api::orderman::OrderService order_service; std::map> state; int id; - + std::unique_ptr manual_fill_store = + std::make_unique ("manual_fills"); + PImpl(bts::api::ServiceContext& context, const PositionServiceBase::Position_dataStatefulPublisher& publisher) : From 698612bb3d99f62c95f6fa03f0302a365eb6fde5 Mon Sep 17 00:00:00 2001 From: Pedro Pinto Date: Thu, 9 May 2019 10:29:54 -0400 Subject: [PATCH 007/121] Storage component of manual fills code complete. --- .../sparkd/position/PositionServiceServer.cpp | 101 +++++++++++++++--- 1 file changed, 87 insertions(+), 14 deletions(-) diff --git a/src/server/sparkd/position/PositionServiceServer.cpp b/src/server/sparkd/position/PositionServiceServer.cpp index 82b88f403..37473eab8 100644 --- a/src/server/sparkd/position/PositionServiceServer.cpp +++ b/src/server/sparkd/position/PositionServiceServer.cpp @@ -5,6 +5,7 @@ #include "lib/logging/logging.h" #include "lib/utils/PBUtils.h" #include "lib/persistence/SqliteStore.h" +#include "lib/utils/Puid.h" #include #include @@ -19,21 +20,28 @@ namespace position{ using namespace bts::api::position; using namespace bts::api::orderman; +static auto const manual_fills_ns = "manual_fills"; + struct PositionServiceServer::PImpl { PositionServiceBase::Position_dataStatefulPublisher publisher; bts::api::orderman::OrderService order_service; std::map> state; int id; - std::unique_ptr manual_fill_store = - std::make_unique ("manual_fills"); + + // manual fills management + std::unique_ptr manual_fills_store ; + PositionServiceBase::Manual_fillsStatefulPublisher manual_fills_publisher; PImpl(bts::api::ServiceContext& context, - const PositionServiceBase::Position_dataStatefulPublisher& publisher) + const PositionServiceBase::Position_dataStatefulPublisher& publisher, + const PositionServiceBase::Manual_fillsStatefulPublisher &manual_fills_publisher) : publisher(publisher), order_service(context.locate()), state(), - id(0) + id(0), + manual_fills_store(std::make_unique ("manual_fills")), + manual_fills_publisher(manual_fills_publisher) { } }; @@ -42,7 +50,9 @@ PositionServiceServer::PositionServiceServer(bts::api::ServiceContext& context) : PositionServiceBase (context), me(new PImpl(context, - make_position_data_stateful_publisher())){ + make_position_data_stateful_publisher(), + make_manual_fills_stateful_publisher())) +{ me->order_service.subscribe_fills() | rxcpp::operators::subscribe( [this](auto const& update) { @@ -57,6 +67,20 @@ me(new PImpl(context, } me->publisher.flush(); }); + + auto all = me->manual_fills_store->retrieve_all()[manual_fills_ns]; + LOG(persist, Info, "Finished loading all {} manual fills", all.size()); + ManualFill rec; + for (auto const &kvp : all) + if (utils::pb::from_string(kvp.second, &rec)) + me->manual_fills_publisher.add_or_update(rec); + else { + LOG(OrderMan, Error, + "Could not recover manual fill {} from storage. " + "This should be investigated", + kvp.first); + } + me->manual_fills_publisher.flush(); } const PositionUnit& PositionServiceServer::_on_fill (const Fill& f, @@ -165,18 +189,67 @@ PositionServiceServer::~PositionServiceServer(){ } void PositionServiceServer::create_manual_fill( - const ::bts::api::position::ManualFillBatch &, - const api::Response<::bts::api::position::PositionServiceResponse> &) -{} + const ManualFillBatch &batch, + const api::Response &r) +{ + PositionServiceResponse resp; + for (auto mf : batch.manual_fill()) { + auto id = utils::gen_id ("manual_fills"); + mf.set_record_id(id); + me->manual_fills_store->insert(manual_fills_ns, + std::to_string(id), + utils::pb::to_string(mf)); + resp.add_response()->set_ok(true); + } + r(resp); +} void PositionServiceServer::update_manual_fill( - const ::bts::api::position::ManualFillBatch &, - const api::Response<::bts::api::position::PositionServiceResponse> &) -{} + const ManualFillBatch &batch, + const api::Response &r) +{ + PositionServiceResponse resp; + for (auto mf : batch.manual_fill()) { + auto *this_resp = resp.add_response(); + auto id = std::to_string(mf.record_id()); + + auto [found,v] = me->manual_fills_store->retrieve_one(manual_fills_ns,id); + if (!found) { + this_resp->set_ok(false); + this_resp->set_msg("Could not find record"); + } + else { + me->manual_fills_store->update(manual_fills_ns, + id, + utils::pb::to_string(mf)); + this_resp->set_ok(true); + } + } + r(resp); +} void PositionServiceServer::remove_manual_fill( - const ::bts::api::position::ManualFillBatch &, - const api::Response<::bts::api::position::PositionServiceResponse> &) -{} + const ManualFillBatch &batch, + const api::Response &r) +{ + PositionServiceResponse resp; + for (auto mf : batch.manual_fill()) { + auto *this_resp = resp.add_response(); + auto id = std::to_string(mf.record_id()); + + auto [found,v] = me->manual_fills_store->retrieve_one(manual_fills_ns,id); + if (!found) { + this_resp->set_ok(false); + this_resp->set_msg("Could not find record"); + } + else { + std::set to_r ; + to_r.insert(id); + me->manual_fills_store->remove(manual_fills_ns,to_r); + this_resp->set_ok(true); + } + } + r(resp); +} }} From 0ea72b017eafd25b16aa4bafccd6f0bd861d00e2 Mon Sep 17 00:00:00 2001 From: Pedro Pinto Date: Thu, 9 May 2019 10:29:54 -0400 Subject: [PATCH 008/121] Storage component of manual fills code complete. --- .../sparkd/position/PositionServiceServer.cpp | 107 +++++++++++++++--- 1 file changed, 93 insertions(+), 14 deletions(-) diff --git a/src/server/sparkd/position/PositionServiceServer.cpp b/src/server/sparkd/position/PositionServiceServer.cpp index 82b88f403..8f8ea758c 100644 --- a/src/server/sparkd/position/PositionServiceServer.cpp +++ b/src/server/sparkd/position/PositionServiceServer.cpp @@ -5,6 +5,7 @@ #include "lib/logging/logging.h" #include "lib/utils/PBUtils.h" #include "lib/persistence/SqliteStore.h" +#include "lib/utils/Puid.h" #include #include @@ -19,21 +20,28 @@ namespace position{ using namespace bts::api::position; using namespace bts::api::orderman; +static auto const manual_fills_ns = "manual_fills"; + struct PositionServiceServer::PImpl { PositionServiceBase::Position_dataStatefulPublisher publisher; bts::api::orderman::OrderService order_service; std::map> state; int id; - std::unique_ptr manual_fill_store = - std::make_unique ("manual_fills"); + + // manual fills management + std::unique_ptr manual_fills_store ; + PositionServiceBase::Manual_fillsStatefulPublisher manual_fills_publisher; PImpl(bts::api::ServiceContext& context, - const PositionServiceBase::Position_dataStatefulPublisher& publisher) + const PositionServiceBase::Position_dataStatefulPublisher& publisher, + const PositionServiceBase::Manual_fillsStatefulPublisher &manual_fills_publisher) : publisher(publisher), order_service(context.locate()), state(), - id(0) + id(0), + manual_fills_store(std::make_unique ("manual_fills")), + manual_fills_publisher(manual_fills_publisher) { } }; @@ -42,7 +50,9 @@ PositionServiceServer::PositionServiceServer(bts::api::ServiceContext& context) : PositionServiceBase (context), me(new PImpl(context, - make_position_data_stateful_publisher())){ + make_position_data_stateful_publisher(), + make_manual_fills_stateful_publisher())) +{ me->order_service.subscribe_fills() | rxcpp::operators::subscribe( [this](auto const& update) { @@ -57,6 +67,20 @@ me(new PImpl(context, } me->publisher.flush(); }); + + auto all = me->manual_fills_store->retrieve_all()[manual_fills_ns]; + LOG(persist, Info, "Finished loading all {} manual fills", all.size()); + ManualFill rec; + for (auto const &kvp : all) + if (utils::pb::from_string(kvp.second, &rec)) + me->manual_fills_publisher.add_or_update(rec); + else { + LOG(OrderMan, Error, + "Could not recover manual fill {} from storage. " + "This should be investigated", + kvp.first); + } + me->manual_fills_publisher.flush(); } const PositionUnit& PositionServiceServer::_on_fill (const Fill& f, @@ -165,18 +189,73 @@ PositionServiceServer::~PositionServiceServer(){ } void PositionServiceServer::create_manual_fill( - const ::bts::api::position::ManualFillBatch &, - const api::Response<::bts::api::position::PositionServiceResponse> &) -{} + const ManualFillBatch &batch, + const api::Response &r) +{ + PositionServiceResponse resp; + for (auto mf : batch.manual_fill()) { + auto id = utils::gen_id ("manual_fills"); + mf.set_record_id(id); + me->manual_fills_store->insert(manual_fills_ns, + std::to_string(id), + utils::pb::to_string(mf)); + resp.add_response()->set_ok(true); + me->manual_fills_publisher.add_or_update(mf); + } + r(resp); + me->manual_fills_publisher.flush(); +} void PositionServiceServer::update_manual_fill( - const ::bts::api::position::ManualFillBatch &, - const api::Response<::bts::api::position::PositionServiceResponse> &) -{} + const ManualFillBatch &batch, + const api::Response &r) +{ + PositionServiceResponse resp; + for (auto mf : batch.manual_fill()) { + auto *this_resp = resp.add_response(); + auto id = std::to_string(mf.record_id()); + + auto [found,v] = me->manual_fills_store->retrieve_one(manual_fills_ns,id); + if (!found) { + this_resp->set_ok(false); + this_resp->set_msg("Could not find record"); + } + else { + me->manual_fills_store->update(manual_fills_ns, + id, + utils::pb::to_string(mf)); + this_resp->set_ok(true); + me->manual_fills_publisher.add_or_update(mf); + } + } + r(resp); + me->manual_fills_publisher.flush(); +} void PositionServiceServer::remove_manual_fill( - const ::bts::api::position::ManualFillBatch &, - const api::Response<::bts::api::position::PositionServiceResponse> &) -{} + const ManualFillBatch &batch, + const api::Response &r) +{ + PositionServiceResponse resp; + for (auto mf : batch.manual_fill()) { + auto *this_resp = resp.add_response(); + auto id = std::to_string(mf.record_id()); + + auto [found,v] = me->manual_fills_store->retrieve_one(manual_fills_ns,id); + if (!found) { + this_resp->set_ok(false); + this_resp->set_msg("Could not find record"); + } + else { + std::set to_r ; + to_r.insert(id); + me->manual_fills_store->remove(manual_fills_ns,to_r); + this_resp->set_ok(true); + me->manual_fills_publisher.erase(mf.record_id()); + } + } + r(resp); + me->manual_fills_publisher.flush(); +} }} From cc26d0bd8b1cd5944c9bfd410f62fd45f3b87862 Mon Sep 17 00:00:00 2001 From: Jake Norbut Date: Mon, 13 May 2019 11:03:57 -0500 Subject: [PATCH 009/121] Adding show_ltq to VolumeColumnSettings --- .../spark/Modules/Trading/Ladder/LadderSettings.cs | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/src/client/spark/Modules/Trading/Ladder/LadderSettings.cs b/src/client/spark/Modules/Trading/Ladder/LadderSettings.cs index c919ce390..2a76493cb 100644 --- a/src/client/spark/Modules/Trading/Ladder/LadderSettings.cs +++ b/src/client/spark/Modules/Trading/Ladder/LadderSettings.cs @@ -119,7 +119,8 @@ namespace mbts.spark { buy = new HistogramBarAnimation { bars = new ColorShowProto((uint)BTSColors.MarketData.bid, false) }, sell = new HistogramBarAnimation { bars = new ColorShowProto((uint)BTSColors.MarketData.ask, false) }, passive = new HistogramBarAnimation { bars = new ColorShowProto((uint)BTSColors.LadderSpecial.passive_volume_bar, false) }, - total = new HistogramBarAnimation { bars = new ColorShowProto((uint)BTSColors.LadderSpecial.total_volume_bar), show_numbers = true } + total = new HistogramBarAnimation { bars = new ColorShowProto((uint)BTSColors.LadderSpecial.total_volume_bar), show_numbers = true }, + show_ltq = false }; } } @@ -134,6 +135,8 @@ namespace mbts.spark { public HistogramBarAnimation passive { get; set; } [ProtoMember(5)] public HistogramBarAnimation total{ get; set; } + [ProtoMember(6)] + public bool show_ltq { get; set; } public bool Equals(VolumeColumnSettings other) => other != null && @@ -141,7 +144,8 @@ namespace mbts.spark { buy.safe_equals(other.buy) && sell.safe_equals(other.sell) && passive.safe_equals(other.passive) && - total.safe_equals(other.total); + total.safe_equals(other.total) && + show_ltq.Equals(other.show_ltq); public override bool Equals(object obj) => Equals(obj as VolumeColumnSettings); @@ -151,7 +155,8 @@ namespace mbts.spark { buy.GetHashCode(), sell.GetHashCode(), passive.GetHashCode(), - total.GetHashCode()); + total.GetHashCode(), + show_ltq.GetHashCode()); } [ProtoContract] From 24f23adf24bfc1f3cbfa2f910801d19c6332333e Mon Sep 17 00:00:00 2001 From: Jake Norbut Date: Mon, 13 May 2019 11:08:36 -0500 Subject: [PATCH 010/121] extending show last trade to VolumnColumnVM --- .../spark/Modules/Trading/Ladder/VM/LadderSettingsVM.cs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/client/spark/Modules/Trading/Ladder/VM/LadderSettingsVM.cs b/src/client/spark/Modules/Trading/Ladder/VM/LadderSettingsVM.cs index 3d6db39c3..8076f3121 100644 --- a/src/client/spark/Modules/Trading/Ladder/VM/LadderSettingsVM.cs +++ b/src/client/spark/Modules/Trading/Ladder/VM/LadderSettingsVM.cs @@ -449,6 +449,7 @@ namespace mbts.spark { passive = new VolumeAtPriceAnimationVM("Passive", new VolumeAtPriceSampleData(this, 72, 127, 198)); total = new VolumeAtPriceAnimationVM("Total", new VolumeAtPriceSampleData(this, 81, 100, 144)); _any_visible = backing(nameof(any_visible), () => volume_components.Any(vc => vc.show_numbers || vc.bars.enabled)); + _show_last_trade = backing(nameof(show_last_trade), false); foreach (var vc in volume_components) { vc.show_numbers_changed += _any_visible.refresh; vc.bars.enabled_changed += _any_visible.refresh; @@ -463,6 +464,9 @@ namespace mbts.spark { public VolumeAtPriceAnimationVM sell { get; } public VolumeAtPriceAnimationVM passive { get; } public VolumeAtPriceAnimationVM total { get; } + + readonly Property _show_last_trade; + public bool show_last_trade { get => _show_last_trade.Value; set => _show_last_trade.Value = value; } public bool any_visible => volume_components.Any(vc => vc.show_numbers || vc.bars.enabled); public IEnumerable volume_components { @@ -481,6 +485,7 @@ namespace mbts.spark { sell.assign_from(settings.sell); passive.assign_from(settings.passive); total.assign_from(settings.total); + show_last_trade = settings.show_ltq; } public VolumeColumnSettings get_settings() => @@ -490,6 +495,7 @@ namespace mbts.spark { sell = sell.get_settings(), passive = passive.get_settings(), total = total.get_settings(), + show_ltq = show_last_trade }; } From 9a5b5595c31f8e917718702351b7c58db78e1a78 Mon Sep 17 00:00:00 2001 From: Jake Norbut Date: Mon, 13 May 2019 11:09:22 -0500 Subject: [PATCH 011/121] adding show last setting to Volumn tab --- .../Trading/Ladder/View/LadderSettingsView.xaml | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/src/client/spark/Modules/Trading/Ladder/View/LadderSettingsView.xaml b/src/client/spark/Modules/Trading/Ladder/View/LadderSettingsView.xaml index 05f6e165b..ca4691070 100644 --- a/src/client/spark/Modules/Trading/Ladder/View/LadderSettingsView.xaml +++ b/src/client/spark/Modules/Trading/Ladder/View/LadderSettingsView.xaml @@ -478,6 +478,7 @@ + @@ -586,6 +587,22 @@ + + + + + + + + + + + + + + From 9b497e323fde28074c2e636fe20bb9f270b6b9c8 Mon Sep 17 00:00:00 2001 From: Jake Norbut Date: Mon, 13 May 2019 11:17:15 -0500 Subject: [PATCH 012/121] adding last on volume column to ladder grid host, tooltips --- .../Trading/Ladder/View/LadderGridHost.cs | 95 ++++++++++++++++++- 1 file changed, 94 insertions(+), 1 deletion(-) diff --git a/src/client/spark/Modules/Trading/Ladder/View/LadderGridHost.cs b/src/client/spark/Modules/Trading/Ladder/View/LadderGridHost.cs index ed727da04..b1520e9f2 100644 --- a/src/client/spark/Modules/Trading/Ladder/View/LadderGridHost.cs +++ b/src/client/spark/Modules/Trading/Ladder/View/LadderGridHost.cs @@ -63,6 +63,11 @@ namespace mbts.spark { MarketBreakLines _mkt_break_lines; NativePriceSize _last_trade; Func _fmt_size = MDDisplayHelper.get_formatter_nps(MarketSizeDisplay.get_default()); + int _old_tooltip_row = int.MaxValue; + double _old_last_trade_price = 0.0; + bool _tooltips_are_hooked_up = false; + ToolTip tooltip_view = new ToolTip(); + public LadderGridHost() { AllowDrop = true; @@ -368,6 +373,18 @@ namespace mbts.spark { uint? _ask_size_from_row(int row) => _size_from_row(row, _price_list, _asks, _vm.security.ContractSpec); + uint _volume_from_row(int row) { + if (_price_list != null) { + var price = _price_list[row]; + var vapls = _volume_container.read_new_value(); + if (vapls != null) { + return vapls.SelectFirstOrDefault(v => MathUtils.epsilon_equals(v.Price, price), v => v.Volume); + } + } + return 0u; + } + + int _row_for_price(double price) => LadderRender.index_of(_price_list, price, _price_comparer); #endregion @@ -492,6 +509,70 @@ namespace mbts.spark { } } + #region tooltip handling + void _toggle_tooltips() { + if (_tooltips_are_hooked_up && !_settings.volume_column.show_ltq) { + this.MouseMove -= _mouse_move_handler; + _tooltips_are_hooked_up = false; + _hide_tool_tip(); + // somehow even after hiding an unhooking the event handler, after toggling the setting off and + // then hovering over the Volume column, the tooltip will still display, so we explicitly set it + // to null here. + this.ToolTip = null; + } + + if (_settings.volume_column.show_ltq && !_tooltips_are_hooked_up) { + this.MouseMove += _mouse_move_handler; + _tooltips_are_hooked_up = true; + } + } + + void _mouse_move_handler(object sender, MouseEventArgs e) { + var pt = e.GetPosition(this); + if (_column_for_point(pt)?.column_type == ColumnType.Volume) { + var row = _row_for_point(pt); + // mark dirty so that we'll always get a value on our read + _volume_container.mark_dirty(); + var volume = _volume_from_row(row); + if (volume > uint.MinValue && row != _old_tooltip_row) { + if (tooltip_view.IsOpen) { + _old_tooltip_row = row; + // we've changed rows, so hide the old tooltip + //System.Diagnostics.Debug.WriteLine(" Tooltip - Previous tooltip is visible, but we've changed rows"); + _hide_tool_tip(); + } + + tooltip_view.Content = $"{volume}"; + tooltip_view.IsOpen = true; + this.ToolTip = tooltip_view; + } else { + // got 0 volume or we're over the same row + if (volume == uint.MinValue) { + // The latest volume query resulted in a 0 volume + // Perhaps we read it too quickly, or perhaps we are on a row where there is no volume + // At any rate, we need this part hiding the tooltip so that we don't leave random tooltips around + // and we have to reset to old row so that when we go back to what could be our preivous row, the tooltip gets drawn again + //System.Diagnostics.Debug.WriteLine(" Tooltip - Volume is 0"); + _hide_tool_tip(); + //_old_tooltip_row = Int32.MaxValue; + } + } + } else { + //System.Diagnostics.Debug.WriteLine(" Tooltip - not over Volume column"); + _hide_tool_tip(); + } + } + void _hide_tool_tip() { + // I am leaving this here for posterity + // Apparently even when setting IsOpen to false first and then setting Content to null afterward, + // the tooltip will *still* display as empty for a brief moment, which looks odd. So, we just hide it now. + tooltip_view.IsOpen = false; + //tooltip_view.Content = null; + } + + #endregion + + void _on_vm_disposed(bool obj) => _dispose_resources(); @@ -642,7 +723,18 @@ namespace mbts.spark { var vapls = _volume_container.read_new_value(); if (vapls != null) { Log.assert_d(_price_list != null); - LadderRender.render_volumes(_volume_vis_tup.visual, _volume_vis_tup.glyph_context, _price_list, vapls, _settings.row_height, security.ContractSpec, _settings.volume_column); + var ltp = _last_trade.price; + var last_index = LadderRender.index_of(_price_list, ltp, _price_comparer); + string direction_arrow = string.Empty; + if (MathUtils.epsilon_equals(ltp, _old_last_trade_price)) { + // do nothing if in here b/c direction_arrow already string.empty + } else if (ltp > _old_last_trade_price) { + direction_arrow = "▲"; + } else { + direction_arrow = "▼"; + } + LadderRender.render_volumes(_volume_vis_tup.visual, _volume_vis_tup.glyph_context, _price_list, vapls, _settings.row_height, security.ContractSpec, _settings.volume_column, last_index, _last_trade, direction_arrow); + _old_last_trade_price = ltp; } } @@ -825,6 +917,7 @@ namespace mbts.spark { case ColumnType.PIQ: SetCurrentValue(PIQColumnWidthProperty, col.settings.width); break; case ColumnType.Volume: SetCurrentValue(VolumeColumnWidthProperty, col.settings.width); + _toggle_tooltips(); _volume_container.mark_dirty(); break; } From a3f5de099f33e101a16af23b45170790ad24612a Mon Sep 17 00:00:00 2001 From: Jake Norbut Date: Mon, 13 May 2019 11:18:56 -0500 Subject: [PATCH 013/121] swapping out bar brushes and glyph values for using last --- .../Trading/Ladder/View/LadderRender.cs | 32 ++++++++++++++++--- 1 file changed, 28 insertions(+), 4 deletions(-) diff --git a/src/client/spark/Modules/Trading/Ladder/View/LadderRender.cs b/src/client/spark/Modules/Trading/Ladder/View/LadderRender.cs index 6873c3bd4..557dfb43c 100644 --- a/src/client/spark/Modules/Trading/Ladder/View/LadderRender.cs +++ b/src/client/spark/Modules/Trading/Ladder/View/LadderRender.cs @@ -194,7 +194,10 @@ namespace mbts.spark { IEnumerable data, double row_height, ContractSpecificationImmutable spec, - VolumeColumnSettings settings + VolumeColumnSettings settings, + int last_index, + NativePriceSize last_trade, + string direction_arrow ) { if (!data.Any()) { _clear_visual(dv); @@ -217,28 +220,47 @@ namespace mbts.spark { _clear_visual(dv); return; } - double max = data.Max(v => v.Volume); + // inflate the max so that the graph never fills the available space + double max = data.Max(v => v.Volume) * 1.1; Brush text_brush = settings.general.foreground.rgb_brush(); _render_cells(dv, row_height, data.Select(d => (d.Price, d)), settings.general, prices, spec, render_cell); /// local methods void render_cell(DrawingContext dc, int r, VolumeAtPriceLevelImmutable vap) { double y = row_height * r; + var last_trade_here = r == last_index; + // render the total first; each component (buy/sell/passive) will be rendered on top of the total if (total_brush != null) { + // if we have to change the brush to indicate this is the last, save brush, draw, then restore brush + var temp = total_brush; + if (settings.show_ltq && last_trade_here) { + total_brush = BTSColors.yellow_brush; + } render_bar(dc, 0, y, vap.Volume, total_brush, out double _); + total_brush = temp; } List numbers_text = new List(); double x_bar = 0; foreach ((var get_volume, Brush bars, bool show_numbers) in components) { uint v = get_volume(vap); - render_bar(dc, x_bar, y, v, bars, out double width); + double width; + if (settings.show_ltq && last_trade_here) { + render_bar(dc, x_bar, y, v, BTSColors.yellow_brush, out width); + } else { + render_bar(dc, x_bar, y, v, bars, out width); + } x_bar += width; if (show_numbers) numbers_text.Add(v.ToString()); } + if (settings.total.show_numbers) { numbers_text.Add(vap.Volume.ToString()); } + if (settings.show_ltq && last_trade_here) { + numbers_text.Clear(); + numbers_text.Add($"{direction_arrow} {last_trade.size} "); + } if (numbers_text.Count > 0) { string str = string.Join("/", numbers_text); GlyphRun gr = glyph_context.get_glyph_run(str, settings.general.font_size, settings.general.width, row_height, HorizontalAlignment.Center, 0, VerticalAlignment.Center, y, true); @@ -253,7 +275,9 @@ namespace mbts.spark { } double pct = v / max; width = pct * settings.general.width; - dc.DrawRectangle(brush, null, new Rect(x, y, width, row_height - 1)); + // 4 seems like a good number to move the y pos down by and similarly + // to shorten the total y coponent by (-4 for the shift of +4, and another -4 to give it some space); + dc.DrawRectangle(brush, null, new Rect(x, y + 4, width, row_height - 2 * 4)); } } From d04f9f4e96b61f0df202f17d6b29d77f55d9fea2 Mon Sep 17 00:00:00 2001 From: John Colvin Date: Mon, 13 May 2019 13:27:20 -0400 Subject: [PATCH 014/121] fix server build --- src/server/sparkd/position/PositionServiceServer.cpp | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/server/sparkd/position/PositionServiceServer.cpp b/src/server/sparkd/position/PositionServiceServer.cpp index 8f8ea758c..3684c0fdb 100644 --- a/src/server/sparkd/position/PositionServiceServer.cpp +++ b/src/server/sparkd/position/PositionServiceServer.cpp @@ -215,7 +215,8 @@ void PositionServiceServer::update_manual_fill( auto *this_resp = resp.add_response(); auto id = std::to_string(mf.record_id()); - auto [found,v] = me->manual_fills_store->retrieve_one(manual_fills_ns,id); + auto p = me->manual_fills_store->retrieve_one(manual_fills_ns,id); + auto found = p.first; if (!found) { this_resp->set_ok(false); this_resp->set_msg("Could not find record"); @@ -241,7 +242,8 @@ void PositionServiceServer::remove_manual_fill( auto *this_resp = resp.add_response(); auto id = std::to_string(mf.record_id()); - auto [found,v] = me->manual_fills_store->retrieve_one(manual_fills_ns,id); + auto p = me->manual_fills_store->retrieve_one(manual_fills_ns,id); + auto found = p.first; if (!found) { this_resp->set_ok(false); this_resp->set_msg("Could not find record"); From bd70e7c09e341e9441ca09296e9ac076c39bf9f2 Mon Sep 17 00:00:00 2001 From: John Colvin Date: Mon, 13 May 2019 13:32:15 -0500 Subject: [PATCH 015/121] OrderDataHelper - fix typeo (Offset order metadata was assigning the tick order color) --- src/client/spark/Orders/OrderDataHelper.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/client/spark/Orders/OrderDataHelper.cs b/src/client/spark/Orders/OrderDataHelper.cs index e31fface7..63027a576 100644 --- a/src/client/spark/Orders/OrderDataHelper.cs +++ b/src/client/spark/Orders/OrderDataHelper.cs @@ -266,7 +266,7 @@ namespace mbts.spark.utilities { case CustomOrderDefinition.CustomOneofCase.Cover: return new OrderMetadata { KeyBinding = "C", Color = BTSColors.Orders.cover_order }; case CustomOrderDefinition.CustomOneofCase.Oco: return new OrderMetadata { KeyBinding = "C", Color = BTSColors.Orders.oco_order }; case CustomOrderDefinition.CustomOneofCase.Pricer: return new OrderMetadata { KeyBinding = "C", Color = BTSColors.Orders.pricer_order }; - case CustomOrderDefinition.CustomOneofCase.Offset: return new OrderMetadata { KeyBinding = "O", Color = BTSColors.Orders.tick_order }; + case CustomOrderDefinition.CustomOneofCase.Offset: return new OrderMetadata { KeyBinding = "O", Color = BTSColors.Orders.off_order }; case CustomOrderDefinition.CustomOneofCase.Trigger: return new OrderMetadata { KeyBinding = "T", Color = BTSColors.Orders.trigger_order }; case CustomOrderDefinition.CustomOneofCase.Spreader: return new OrderMetadata { KeyBinding = "", Color = BTSColors.Orders.spreader_order }; case CustomOrderDefinition.CustomOneofCase.Dynamic: return new OrderMetadata { KeyBinding = "", Color = BTSColors.Orders.top_order }; @@ -427,7 +427,7 @@ namespace mbts.spark.utilities { case CustomOrderDefinition.CustomOneofCase.Cover: return new OrderMetadata { KeyBinding = "C", Color = BTSColors.Orders.cover_order }; case CustomOrderDefinition.CustomOneofCase.Oco: return new OrderMetadata { KeyBinding = "C", Color = BTSColors.Orders.oco_order }; case CustomOrderDefinition.CustomOneofCase.Pricer: return new OrderMetadata { KeyBinding = "C", Color = BTSColors.Orders.pricer_order }; - case CustomOrderDefinition.CustomOneofCase.Offset: return new OrderMetadata { KeyBinding = "O", Color = BTSColors.Orders.tick_order }; + case CustomOrderDefinition.CustomOneofCase.Offset: return new OrderMetadata { KeyBinding = "O", Color = BTSColors.Orders.off_order }; case CustomOrderDefinition.CustomOneofCase.Trigger: return new OrderMetadata { KeyBinding = "T", Color = BTSColors.Orders.trigger_order }; case CustomOrderDefinition.CustomOneofCase.Spreader: return new OrderMetadata { KeyBinding = "", Color = BTSColors.Orders.spreader_order }; case CustomOrderDefinition.CustomOneofCase.Dynamic: return new OrderMetadata { KeyBinding = "", Color = BTSColors.Orders.top_order }; From 52305d69f2a4958124f256cdf2dc28b861b3e1a8 Mon Sep 17 00:00:00 2001 From: John Colvin Date: Mon, 13 May 2019 13:33:27 -0500 Subject: [PATCH 016/121] normalize spacing in OrderDataHelper.cs --- src/client/spark/Orders/OrderDataHelper.cs | 26 +++++++++++----------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/src/client/spark/Orders/OrderDataHelper.cs b/src/client/spark/Orders/OrderDataHelper.cs index 63027a576..a7f80f80a 100644 --- a/src/client/spark/Orders/OrderDataHelper.cs +++ b/src/client/spark/Orders/OrderDataHelper.cs @@ -49,7 +49,7 @@ namespace mbts.spark.utilities { public OrderFactory(AdminModel admin_model, DynamicOrderHelper dynamic_helper) { _admin_model = admin_model; _dynamic_helper = dynamic_helper; - var algo_name_index = new HashSet>(OneOf.ValueEqualityComparer); + var algo_name_index = new HashSet>(OneOf.ValueEqualityComparer); // add standard types algo_name_index.AddRange( EnumHelper.get_values_except(CustomOrderDefinition.CustomOneofCase.None) @@ -58,7 +58,7 @@ namespace mbts.spark.utilities { algo_name_index.AddRange(dynamic_helper.dynamic_types .Select(dt => new OneOf(dt))); // build abbreviation index - _algo_abbreviation_index = DisplayNameHelper.get_word_tree_abbreviations(algo_name_index, oneof => oneof.value.ToString(), OneOf.ValueEqualityComparer); + _algo_abbreviation_index = DisplayNameHelper.get_word_tree_abbreviations(algo_name_index, oneof => oneof.value.ToString(), OneOf.ValueEqualityComparer); } public IObservable> subscribe_accounts() => _admin_model.on_accounts; @@ -70,7 +70,7 @@ namespace mbts.spark.utilities { public CustomOrderDefinition.CustomOneofCase default_order_type() => CustomOrderDefinition.CustomOneofCase.Limit; - public CustomOrderDefinition.CustomOneofCase default_order_type(SecurityImmutable security, bool is_child=false) { + public CustomOrderDefinition.CustomOneofCase default_order_type(SecurityImmutable security, bool is_child = false) { if (is_child) return CustomOrderDefinition.CustomOneofCase.Limit; switch (security.ContractCase) { case Security.ContractOneofCase.Synthetic: return CustomOrderDefinition.CustomOneofCase.Spreader; @@ -78,7 +78,7 @@ namespace mbts.spark.utilities { } } - public CustomOrderDefinition.CustomOneofCase default_order_type(SecurityImmutable? security, bool is_child=false) => + public CustomOrderDefinition.CustomOneofCase default_order_type(SecurityImmutable? security, bool is_child = false) => security.HasValue ? default_order_type(security.Value, is_child) : default_order_type(); public TimeInForce default_time_in_force() => @@ -103,13 +103,13 @@ namespace mbts.spark.utilities { } } - public IEnumerable default_order_types(SecurityImmutable? security, bool is_child=false) => + public IEnumerable default_order_types(SecurityImmutable? security, bool is_child = false) => security.HasValue ? default_order_types(security.Value, is_child) : default_order_types(); public IEnumerable default_time_in_forces() => EnumHelper.get_values_except(TimeInForce.Undefined); - public IEnumerable default_time_in_forces(SecurityImmutable security, bool is_child=false) => + public IEnumerable default_time_in_forces(SecurityImmutable security, bool is_child = false) => default_time_in_forces(); public IEnumerable default_time_in_forces(SecurityImmutable security, CustomOrderDefinition.CustomOneofCase order_type, bool is_child = false) { @@ -120,7 +120,7 @@ namespace mbts.spark.utilities { return tifs; } - public IEnumerable default_time_in_forces(SecurityImmutable? security, bool is_child=false) => + public IEnumerable default_time_in_forces(SecurityImmutable? security, bool is_child = false) => security.HasValue ? default_time_in_forces(security.Value, is_child) : default_time_in_forces(); public LadderSettings default_ladder_settings(SecurityImmutable security) { @@ -226,14 +226,14 @@ namespace mbts.spark.utilities { public CustomOrderDefinition custom_order(CustomOrderDefinition.CustomOneofCase order_type) { var rv = new CustomOrderDefinition(); - switch(order_type) { + switch (order_type) { case CustomOrderDefinition.CustomOneofCase.Market: rv.Market = market_order(); break; case CustomOrderDefinition.CustomOneofCase.Limit: rv.Limit = limit_order(); break; case CustomOrderDefinition.CustomOneofCase.Stop: rv.Stop = stop_order(); break; case CustomOrderDefinition.CustomOneofCase.StopLimit: rv.StopLimit = stop_limit_order(); break; case CustomOrderDefinition.CustomOneofCase.Iceberg: rv.Iceberg = iceberg_order(); break; case CustomOrderDefinition.CustomOneofCase.TimeBox: rv.TimeBox = time_box_order(); break; - case CustomOrderDefinition.CustomOneofCase.Tick: rv.Tick = tick_order(); break; + case CustomOrderDefinition.CustomOneofCase.Tick: rv.Tick = tick_order(); break; case CustomOrderDefinition.CustomOneofCase.Lean: rv.Lean = lean_order(); break; case CustomOrderDefinition.CustomOneofCase.Join: rv.Join = join_order(); break; case CustomOrderDefinition.CustomOneofCase.Top: rv.Top = top_order(); break; @@ -244,7 +244,7 @@ namespace mbts.spark.utilities { case CustomOrderDefinition.CustomOneofCase.Trigger: rv.Trigger = trigger_order(); break; case CustomOrderDefinition.CustomOneofCase.Spreader: rv.Spreader = spreader_order(); break; case CustomOrderDefinition.CustomOneofCase.Dynamic: rv.Dynamic = dynamic_order(); break; - //case CustomOrderDefinition.CustomOneofCase.QueueHolder: rv.QueueHolder = queue_holder_order(); break; + //case CustomOrderDefinition.CustomOneofCase.QueueHolder: rv.QueueHolder = queue_holder_order(); break; } return rv; } @@ -636,12 +636,12 @@ namespace mbts.spark.utilities { var tgt_child = cad.get_child_from_impl(tgt_type_def); copy_values(src_child, ref tgt_child); } - } + } public static CustomOrderDefinition.CustomOneofCase get_order_type(IMessage order_type_def) => order_type_def != null && _algo_type_case_index.TryGetValue(order_type_def.Descriptor.ClrType, out CustomOrderDefinition.CustomOneofCase algo_case) ? algo_case : CustomOrderDefinition.CustomOneofCase.None; - + #region Private Members // get child static CustomOrderDefinitionImmutable? _get_iceberg_child(IcebergOrderDefinitionImmutable iceberg) => iceberg.HasChild ? iceberg.Child : (CustomOrderDefinitionImmutable?)null; @@ -857,7 +857,7 @@ namespace mbts.spark.utilities { int phase_mult = 1; switch (def.CustomCase) { case CustomOrderDefinition.CustomOneofCase.Cover: - add_component(def.Cover.HedgeTicks.GetHashCode()); + add_component(def.Cover.HedgeTicks.GetHashCode()); add_component(_get_hash(_get_cover_child(def.Cover))); add_component(_get_hash(_get_cover_hedge(def.Cover))); break; From 1bcb7ac87f6bd7ba7a417b9b252888bf981208b3 Mon Sep 17 00:00:00 2001 From: John Colvin Date: Mon, 13 May 2019 13:59:54 -0500 Subject: [PATCH 017/121] SPARK-2411 / SPARK-2414 Add UI for glbx icerberg order --- src/client/spark/Orders/OrderDataHelper.cs | 8 ++++ .../Orders/OrderTypeVMs/GLBXIcebergOrderVM.cs | 16 ++++++++ .../Orders/OrderTypeVMs/OrderTypeVMFactory.cs | 2 + .../spark/core/FontsAndColors/BTSColors.cs | 1 + .../Order/AlgoContentTemplateSelector.xaml | 39 +++++++++++++++++++ .../Order/AlgoContentTemplateSelector.xaml.cs | 2 + 6 files changed, 68 insertions(+) create mode 100644 src/client/spark/Orders/OrderTypeVMs/GLBXIcebergOrderVM.cs diff --git a/src/client/spark/Orders/OrderDataHelper.cs b/src/client/spark/Orders/OrderDataHelper.cs index a7f80f80a..96acc2caa 100644 --- a/src/client/spark/Orders/OrderDataHelper.cs +++ b/src/client/spark/Orders/OrderDataHelper.cs @@ -201,6 +201,7 @@ namespace mbts.spark.utilities { public LimitOrderDefinition limit_order() => OrderDataHelper.Defaults.LimitOrderDefinition(); public StopOrderDefinition stop_order() => OrderDataHelper.Defaults.StopOrderDefinition(); public StopLimitOrderDefinition stop_limit_order() => OrderDataHelper.Defaults.StopLimitOrderDefinition(); + public GLBXIcebergOrderDefinition glbx_iceberg_order() => OrderDataHelper.Defaults.GLBXIcebergOrderDefinition(); public IcebergOrderDefinition iceberg_order() => OrderDataHelper.Defaults.IcebergOrderDefinition(); public TimeBoxOrderDefinition time_box_order() => OrderDataHelper.Defaults.TimeBoxOrderDefinition(); public TickOrderDefinition tick_order() => OrderDataHelper.Defaults.TickOrderDefinition(); @@ -231,6 +232,7 @@ namespace mbts.spark.utilities { case CustomOrderDefinition.CustomOneofCase.Limit: rv.Limit = limit_order(); break; case CustomOrderDefinition.CustomOneofCase.Stop: rv.Stop = stop_order(); break; case CustomOrderDefinition.CustomOneofCase.StopLimit: rv.StopLimit = stop_limit_order(); break; + case CustomOrderDefinition.CustomOneofCase.GlbxIceberg: rv.GlbxIceberg = glbx_iceberg_order(); break; case CustomOrderDefinition.CustomOneofCase.Iceberg: rv.Iceberg = iceberg_order(); break; case CustomOrderDefinition.CustomOneofCase.TimeBox: rv.TimeBox = time_box_order(); break; case CustomOrderDefinition.CustomOneofCase.Tick: rv.Tick = tick_order(); break; @@ -257,6 +259,7 @@ namespace mbts.spark.utilities { case CustomOrderDefinition.CustomOneofCase.Limit: return new OrderMetadata { KeyBinding = "L", Color = BTSColors.green.rgb_color(), NoProps = true }; case CustomOrderDefinition.CustomOneofCase.Stop: return new OrderMetadata { KeyBinding = "S", Color = BTSColors.red_orange.rgb_color(), NoProps = true }; case CustomOrderDefinition.CustomOneofCase.StopLimit: return new OrderMetadata { KeyBinding = "SL", Color = BTSColors.pale_green.rgb_color() }; + case CustomOrderDefinition.CustomOneofCase.GlbxIceberg: return new OrderMetadata { KeyBinding = "GB", Color = BTSColors.Orders.glbx_iceberg_order }; case CustomOrderDefinition.CustomOneofCase.Iceberg: return new OrderMetadata { KeyBinding = "I", Color = BTSColors.pale_blue.rgb_color() }; case CustomOrderDefinition.CustomOneofCase.TimeBox: return new OrderMetadata { KeyBinding = "TB", Color = BTSColors.purple.rgb_color() }; case CustomOrderDefinition.CustomOneofCase.Tick: return new OrderMetadata { KeyBinding = "T", Color = BTSColors.Orders.tick_order }; @@ -418,6 +421,7 @@ namespace mbts.spark.utilities { case CustomOrderDefinition.CustomOneofCase.Limit: return new OrderMetadata { KeyBinding = "L", Color = BTSColors.green.rgb_color(), NoProps = true }; case CustomOrderDefinition.CustomOneofCase.Stop: return new OrderMetadata { KeyBinding = "S", Color = BTSColors.red_orange.rgb_color(), NoProps = true }; case CustomOrderDefinition.CustomOneofCase.StopLimit: return new OrderMetadata { KeyBinding = "SL", Color = BTSColors.pale_green.rgb_color() }; + case CustomOrderDefinition.CustomOneofCase.GlbxIceberg: return new OrderMetadata { KeyBinding = "GB", Color = BTSColors.Orders.glbx_iceberg_order }; case CustomOrderDefinition.CustomOneofCase.Iceberg: return new OrderMetadata { KeyBinding = "I", Color = BTSColors.pale_blue.rgb_color() }; case CustomOrderDefinition.CustomOneofCase.TimeBox: return new OrderMetadata { KeyBinding = "TB", Color = BTSColors.purple.rgb_color() }; case CustomOrderDefinition.CustomOneofCase.Tick: return new OrderMetadata { KeyBinding = "T", Color = BTSColors.Orders.tick_order }; @@ -440,6 +444,7 @@ namespace mbts.spark.utilities { switch (algo_type) { case CustomOrderDefinition.CustomOneofCase.Limit: return "LM"; case CustomOrderDefinition.CustomOneofCase.Cover: return "CV"; + case CustomOrderDefinition.CustomOneofCase.GlbxIceberg: return "GB"; case CustomOrderDefinition.CustomOneofCase.Iceberg: return "IB"; case CustomOrderDefinition.CustomOneofCase.Lean: return "LN"; case CustomOrderDefinition.CustomOneofCase.Market: return "MA"; @@ -594,6 +599,7 @@ namespace mbts.spark.utilities { CustomOrderDefinition.CustomOneofCase.Limit, CustomOrderDefinition.CustomOneofCase.Stop, CustomOrderDefinition.CustomOneofCase.StopLimit, + CustomOrderDefinition.CustomOneofCase.GlbxIceberg, CustomOrderDefinition.CustomOneofCase.Iceberg, CustomOrderDefinition.CustomOneofCase.Lean, CustomOrderDefinition.CustomOneofCase.Join, @@ -981,6 +987,7 @@ namespace mbts.spark.utilities { case bts.api.orderman.CustomOrderDefinition.CustomOneofCase.Stop: rv.Stop = StopOrderDefinition(); break; case bts.api.orderman.CustomOrderDefinition.CustomOneofCase.StopLimit: rv.StopLimit = StopLimitOrderDefinition(); break; case bts.api.orderman.CustomOrderDefinition.CustomOneofCase.Iceberg: rv.Iceberg = IcebergOrderDefinition(); break; + case bts.api.orderman.CustomOrderDefinition.CustomOneofCase.GlbxIceberg: rv.GlbxIceberg = GLBXIcebergOrderDefinition(); break; case bts.api.orderman.CustomOrderDefinition.CustomOneofCase.TimeBox: rv.TimeBox = TimeBoxOrderDefinition(); break; case bts.api.orderman.CustomOrderDefinition.CustomOneofCase.Tick: rv.Tick = TickOrderDefinition(); break; case bts.api.orderman.CustomOrderDefinition.CustomOneofCase.Lean: rv.Lean = LeanOrderDefinition(); break; @@ -1004,6 +1011,7 @@ namespace mbts.spark.utilities { public static LimitOrderDefinition LimitOrderDefinition() => new LimitOrderDefinition { }; public static StopOrderDefinition StopOrderDefinition() => new StopOrderDefinition { }; public static StopLimitOrderDefinition StopLimitOrderDefinition() => new StopLimitOrderDefinition { }; + public static GLBXIcebergOrderDefinition GLBXIcebergOrderDefinition() => new GLBXIcebergOrderDefinition { MaxShowQuantity = 1 }; public static IcebergOrderDefinition IcebergOrderDefinition() => new IcebergOrderDefinition { Child = CustomOrderDefinition(), ShowQuantity = 1 }; public static TimeBoxOrderDefinition TimeBoxOrderDefinition() => new TimeBoxOrderDefinition { Child = CustomOrderDefinition(), Start = Timestamp.FromDateTime(DateTime.UtcNow), Stop = Timestamp.FromDateTime(DateTime.UtcNow) }; public static TickOrderDefinition TickOrderDefinition() => new TickOrderDefinition { Child = CustomOrderDefinition(), CrossThreshold = QuantityMeasure }; diff --git a/src/client/spark/Orders/OrderTypeVMs/GLBXIcebergOrderVM.cs b/src/client/spark/Orders/OrderTypeVMs/GLBXIcebergOrderVM.cs new file mode 100644 index 000000000..b93f24632 --- /dev/null +++ b/src/client/spark/Orders/OrderTypeVMs/GLBXIcebergOrderVM.cs @@ -0,0 +1,16 @@ +using bts.api.orderman; + +namespace mbts.spark { + static partial class OrderTypeVMFactory { + class GLBXIcebergOrderVM : OrderTypeVMBase { + readonly Property _max_show_quantity; + + public GLBXIcebergOrderVM(AlgoKey key, string display_name, AlgoEditingPermissions editing_permissions) + : base(key, display_name, editing_permissions) { + _max_show_quantity = add_nullable_property(nameof(max_show_quantity), GLBXIcebergOrderDefinition.MaxShowQuantityFieldNumber); + } + + public uint? max_show_quantity { get => _max_show_quantity.Value; set => _max_show_quantity.set(value); } + } + } +} \ No newline at end of file diff --git a/src/client/spark/Orders/OrderTypeVMs/OrderTypeVMFactory.cs b/src/client/spark/Orders/OrderTypeVMs/OrderTypeVMFactory.cs index 693dd9e25..5a4ae67d8 100644 --- a/src/client/spark/Orders/OrderTypeVMs/OrderTypeVMFactory.cs +++ b/src/client/spark/Orders/OrderTypeVMs/OrderTypeVMFactory.cs @@ -53,6 +53,7 @@ namespace mbts.spark { public static IAlgoVM pricer(AlgoKey key, string display_name, AlgoEditingPermissions editing_permissions) => new PricerOrderVM(key, display_name, editing_permissions); public static IAlgoVM offset(AlgoKey key, string display_name, AlgoEditingPermissions editing_permissions) => new OffsetOrderVM(key, display_name, editing_permissions); public static IAlgoVM trigger(AlgoKey key, string display_name, AlgoEditingPermissions editing_permissions) => new TriggerOrderVM(key, display_name, editing_permissions); + public static IAlgoVM glbx_iceberg(AlgoKey key, string display_name, AlgoEditingPermissions editing_permissions) => new GLBXIcebergOrderVM(key, display_name, editing_permissions); public static IAlgoVM iceberg(AlgoKey key, string display_name, AlgoEditingPermissions editing_permissions) => new IcebergOrderVM(key, display_name, editing_permissions); public static IAlgoVM spreader(AlgoKey key, string display_name, AlgoEditingPermissions editing_permissions) => new SpreaderOrderVM(key, display_name, editing_permissions); public static IAlgoVM dynamic(AlgoKey key, string display_name, string dynamic_type, AlgoEditingPermissions editing_permissions) => new DynamicOrderVM(key, dynamic_type, display_name, editing_permissions); @@ -83,6 +84,7 @@ namespace mbts.spark { case AlgoType.Pricer: return pricer(key, name, editing_permissions); case AlgoType.Offset: vm = offset(key, name, editing_permissions); break; case AlgoType.Trigger: vm = trigger(key, name, editing_permissions); break; + case AlgoType.GlbxIceberg: vm = glbx_iceberg(key, name, editing_permissions); break; case AlgoType.Iceberg: vm = iceberg(key, name, editing_permissions); break; case AlgoType.Spreader: vm = spreader(key, name, editing_permissions); break; case AlgoType.Dynamic: vm = dynamic(key, name, name, editing_permissions); break; diff --git a/src/client/spark/core/FontsAndColors/BTSColors.cs b/src/client/spark/core/FontsAndColors/BTSColors.cs index 14009cbf7..e58fb36c0 100644 --- a/src/client/spark/core/FontsAndColors/BTSColors.cs +++ b/src/client/spark/core/FontsAndColors/BTSColors.cs @@ -222,6 +222,7 @@ namespace mbts.ui { public static BTSColor straddle_theovsmarket_positive { get { return BTSCoreColors.green_light; } } // Order Types + public static BTSColor glbx_iceberg_order => new BTSColor(85, 85, 196); public static BTSColor tick_order { get { return BTSColors.purple.rgb_color(); } } public static BTSColor lean_order { get { return new BTSColor(255, 170, 210); } } public static BTSColor join_order { get { return new BTSColor(200, 171, 232); } } diff --git a/src/client/spark/view/Controls/Order/AlgoContentTemplateSelector.xaml b/src/client/spark/view/Controls/Order/AlgoContentTemplateSelector.xaml index 6706baf48..c64d29f52 100644 --- a/src/client/spark/view/Controls/Order/AlgoContentTemplateSelector.xaml +++ b/src/client/spark/view/Controls/Order/AlgoContentTemplateSelector.xaml @@ -872,6 +872,45 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Max Show Quantity + + + + diff --git a/src/client/spark/view/Controls/Order/AlgoContentTemplateSelector.xaml.cs b/src/client/spark/view/Controls/Order/AlgoContentTemplateSelector.xaml.cs index 9e1110cd8..a7af0cd77 100644 --- a/src/client/spark/view/Controls/Order/AlgoContentTemplateSelector.xaml.cs +++ b/src/client/spark/view/Controls/Order/AlgoContentTemplateSelector.xaml.cs @@ -40,6 +40,7 @@ namespace mbts.ui.spark { public DataTemplate PricerOrderTemplate { get; set; } public DataTemplate OffsetOrderTemplate { get; set; } public DataTemplate TriggerOrderTemplate { get; set; } + public DataTemplate GLBXIcebergOrderTemplate { get; set; } public DataTemplate IcebergOrderTemplate { get; set; } public DataTemplate SpreaderTemplate { get; set; } public DataTemplate DynamicOrderTemplate { get; set; } @@ -62,6 +63,7 @@ namespace mbts.ui.spark { case CustomOrderDefinition.CustomOneofCase.Pricer: return PricerOrderTemplate; case CustomOrderDefinition.CustomOneofCase.Offset: return OffsetOrderTemplate; case CustomOrderDefinition.CustomOneofCase.Trigger: return TriggerOrderTemplate; + case CustomOrderDefinition.CustomOneofCase.GlbxIceberg: return GLBXIcebergOrderTemplate; case CustomOrderDefinition.CustomOneofCase.Iceberg: return IcebergOrderTemplate; case CustomOrderDefinition.CustomOneofCase.Spreader: return SpreaderTemplate; case CustomOrderDefinition.CustomOneofCase.Dynamic: return DynamicOrderTemplate; From 7b61b4bd5be75b1732008925990a9784b1cb374e Mon Sep 17 00:00:00 2001 From: Pedro Pinto Date: Mon, 13 May 2019 17:18:32 -0400 Subject: [PATCH 018/121] Fixed error message --- docker/linux | 2 +- src/lib/orderman/orders/NativeOrder.cpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/docker/linux b/docker/linux index ce08a5129..e13bb5d8d 160000 --- a/docker/linux +++ b/docker/linux @@ -1 +1 @@ -Subproject commit ce08a5129f14af0d1bb9fa569d1e721b694964fe +Subproject commit e13bb5d8df14171a9a5abfdb8b6b4ecbc519032a diff --git a/src/lib/orderman/orders/NativeOrder.cpp b/src/lib/orderman/orders/NativeOrder.cpp index e8063aab5..23fb76165 100644 --- a/src/lib/orderman/orders/NativeOrder.cpp +++ b/src/lib/orderman/orders/NativeOrder.cpp @@ -113,7 +113,7 @@ void NativeOrder::on_init_new(OrderDefinition const &def, return; } if (me->sec->exchange() != api::VENUE_GLBX) { - auto msg = "Order type supported in this venue"; + auto msg = "Order type not supported in this venue"; me->state.set_last_msg(msg); transition(ORDER_STATUS_REJECTED_BY_SPARK, msg); return; From 86467b2be1edc747aacaa659d9b877e56ed6f6d2 Mon Sep 17 00:00:00 2001 From: John Colvin Date: Mon, 13 May 2019 21:24:36 -0500 Subject: [PATCH 019/121] SPARK-2411 / SPARK-2415 Add param change popup to favorites buttons --- .../AlgoTemplateFavoriteButtonsView.xaml | 97 ++++++++++++++++++- .../AlgoTemplateFavoriteButtonsView.xaml.cs | 26 +++-- .../Controls/Order/AlgoTemplateToolBar.xaml | 48 ++++++++- .../Order/OrderDetailsPopupControl.xaml | 29 ------ .../spark/view/Utility/VisualChildLookup.cs | 19 ++++ 5 files changed, 181 insertions(+), 38 deletions(-) create mode 100644 src/client/spark/view/Utility/VisualChildLookup.cs diff --git a/src/client/spark/view/Controls/Order/AlgoTemplateFavoriteButtonsView.xaml b/src/client/spark/view/Controls/Order/AlgoTemplateFavoriteButtonsView.xaml index b7131fb8d..b1f1a5bd9 100644 --- a/src/client/spark/view/Controls/Order/AlgoTemplateFavoriteButtonsView.xaml +++ b/src/client/spark/view/Controls/Order/AlgoTemplateFavoriteButtonsView.xaml @@ -1,9 +1,17 @@  + + + + + + + + - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/client/spark/Modules/Trading/ManualFills/ManualFillsView.xaml.cs b/src/client/spark/Modules/Trading/ManualFills/ManualFillsView.xaml.cs new file mode 100644 index 000000000..678bcb132 --- /dev/null +++ b/src/client/spark/Modules/Trading/ManualFills/ManualFillsView.xaml.cs @@ -0,0 +1,36 @@ +using bts.shell.manual_fills; +using mbts.ui.converters; +using System.Windows; +using System.Windows.Controls; +using System.Windows.Data; +using System.Windows.Media; + +namespace mbts.ui.spark { + /// + /// Interaction logic for ManualFillsView.xaml + /// + partial class ManualFillsView : UserControl { + public static readonly IMultiValueConverter + RowHeaderVisualStateConverter = ValueConverter.create( + (edit_state, is_valid) => is_valid ? edit_state.ToString() : edit_state == EditableItemState.Uncommitted ? "ErrorUncommitted" : "Error"); + + public enum Columns { + RecordID, + Security, + Account, + User, + Side, + Size, + Price + } + + public ManualFillsView() => + InitializeComponent(); + + public static readonly DependencyProperty ImageBrushProperty = + DependencyProperty.RegisterAttached("ImageBrush", typeof(Brush), typeof(ManualFillsView)); + + public static Brush GetImageBrush(DependencyObject d) => d.GetValue(ImageBrushProperty) as Brush; + public static void SetImageBrush(DependencyObject d, Brush value) => d.SetValue(ImageBrushProperty, value); + } +} \ No newline at end of file diff --git a/src/client/spark/Modules/Trading/ManualFills/ManualFillsWindowFactory.cs b/src/client/spark/Modules/Trading/ManualFills/ManualFillsWindowFactory.cs new file mode 100644 index 000000000..f6d7a73e9 --- /dev/null +++ b/src/client/spark/Modules/Trading/ManualFills/ManualFillsWindowFactory.cs @@ -0,0 +1,105 @@ +using bts.api; +using bts.shell.manual_fills; +using bts.spark; +using mbts; +using mbts.spark; +using mbts.ui; +using mbts.ui.controls; +using mbts.ui.spark; +using System; +using System.Collections.Generic; +using System.Collections.Specialized; +using System.ComponentModel; +using System.Linq; +using System.Threading.Tasks; +using System.Windows; +using System.Windows.Controls; + +namespace bts.shell { + class ManualFillsWindowFactory : ShellWindowFactory { + const string + _window_type = "ManualFills", + _window_name = "Manual Fills"; + + readonly IBus _bus; + readonly OrdersModel _orders_model; + readonly string _thread_id; + + public ManualFillsWindowFactory(IShellModule owner, IBus bus, OrdersModel orders_model) : base(owner, _window_name) { + _bus = bus; + _orders_model = orders_model; + _thread_id = mbts.config.Configuration.get().manual_fills ? _window_type : null; + _dispose = bus.subscribe(SparkTags.ShowManualFillsTag).Subscribe(open_window); + // + async void open_window(ManualFillsSettings state) => + _bus.post(SparkTags.ShowWindow, await load(state)); + } + + public override Task load(ManualFillsSettings settings) { + return Task.FromResult(SparkWindow.create( + _window_type, _window_name, _bus, this, Icons.DataEdit, _thread_id, thread_init)); + // + (UIElement, IEnumerable, Func, IDisposable) thread_init() { + var vm = ManualFillsVM.create(_bus, _orders_model); + var view = new ManualFillsView { DataContext = vm }; + view.data_grid.Sorting += on_sorting; + using (apply_sort()) { + var col_sort_tuples = + from sc in settings.sorting_criterion + join col in view.data_grid.Columns on sc.col_name equals _col_id(col) + select (col, sc); + foreach (var (col, sc) in col_sort_tuples) { + col.SortDirection = sc.direction(); + vm.rows_view.SortDescriptions.Add(new SortDescription(col.SortMemberPath, sc.direction())); + } + } + var cmds = new[] { + new SparkCommandCommand("Add New", Icons.AddBoxes, vm.add_fill_cmd), + new SparkCommandCommand("Revert Selected", Icons.UndoCircular,vm.revert_selected_fills_cmd) { show_in_toolbar = false }, + new SparkCommandCommand("Revert All", Icons.UndoCircular, vm.revert_all_cmd), + new SparkCommandCommand("Save All", Icons.Disk, vm.commit_all_fills_cmd), + new SparkCommandCommand("Delete Selected", Icons.Trash, vm.remove_selected_fills_cmd) + }; + return (view, cmds, get_settings, vm); + // + void on_sorting(object sender, DataGridSortingEventArgs e) { + e.Handled = true; + using (apply_sort()) { + var new_sort_desc = new SortDescription(e.Column.SortMemberPath, e.Column.SortDirection == ListSortDirection.Ascending ? ListSortDirection.Descending : ListSortDirection.Ascending); + e.Column.SortDirection = new_sort_desc.Direction; + if (KeyboardExtensions.IsShiftDown()) { + if (vm.rows_view.SortDescriptions.TryGetFirst(sd => sd.PropertyName == e.Column.SortMemberPath, out var match, out int index)) { + vm.rows_view.SortDescriptions[index] = new_sort_desc; + } else { + vm.rows_view.SortDescriptions.Add(new_sort_desc); + } + } else { + vm.rows_view.SortDescriptions.Clear(); + foreach (var col in view.data_grid.Columns.Except(new[] { e.Column })) + col.SortDirection = null; + vm.rows_view.SortDescriptions.Add(new_sort_desc); + } + } + } + /// Ensures that the primary sort description property (i.e., at index 0) is the placeholder_key. + /// This behavior has the effect of grouping placeholder rows at the bottom, without using (which is incompatible with row virtualization). + IDisposable apply_sort() => + Disposable.Create(() => { + var pk_sort_desc = new SortDescription(nameof(ManualFillRow.placeholder_key), ListSortDirection.Ascending); + if (vm.rows_view.SortDescriptions.Count > 0) { + if (vm.rows_view.SortDescriptions[0].Equals(pk_sort_desc)) + return; + vm.rows_view.SortDescriptions.removeFirst(sd => sd.PropertyName == pk_sort_desc.PropertyName); + } + vm.rows_view.SortDescriptions.Insert(0, pk_sort_desc); + }); + + ManualFillsSettings get_settings() => + new ManualFillsSettings(vm.rows_view.get_sorting_criteria(view.data_grid.Columns).ToList()); + } + } + + static string _col_id(DataGridColumn col) => + DataGridEx.GetCellKey(col)?.ToString(); + } +} \ No newline at end of file diff --git a/src/client/spark/Modules/Trading/TradingShellModule.cs b/src/client/spark/Modules/Trading/TradingShellModule.cs index c5cf0da00..b8e4fa1cf 100644 --- a/src/client/spark/Modules/Trading/TradingShellModule.cs +++ b/src/client/spark/Modules/Trading/TradingShellModule.cs @@ -1,4 +1,5 @@ using bts.api; +using bts.shell.manual_fills; using bts.spark; using mbts.persist; using mbts.spark; @@ -19,6 +20,7 @@ namespace bts.shell { FillsWindowFactory _fwf; PAndLWindowFactory _plwf; AlgoStudioWindowFactory _aswf; + ManualFillsWindowFactory _mfwf; [ImportingConstructor] public TradingShellModule(IBus bus, IWindowManager wm, INotificationService notification_service, INotificationServiceDataCache notification_service_data_cache, @@ -28,6 +30,7 @@ namespace bts.shell { _fwf = new FillsWindowFactory(this, bus, settings_hub, orders_model); _plwf = new PAndLWindowFactory(this, bus, settings_hub, pandl_model); _aswf = new AlgoStudioWindowFactory(this, bus, orders_model, error_reporter); + _mfwf = new ManualFillsWindowFactory(this, bus, orders_model); } public ImageSource icon => Icons.Trades; @@ -42,7 +45,8 @@ namespace bts.shell { SparkCommands.ShowPAndL.complete(() => new PAndLWindowFactory.PAndLStartupData()), SparkCommands.PickLadder, SparkCommands.PickOrderTicket, - SparkCommands.ShowAlgoStudio.complete(() => new AlgoStudioWindowFactory.StartupData()) + SparkCommands.ShowAlgoStudio.complete(() => new AlgoStudioWindowFactory.StartupData()), + SparkCommands.ShowManualFills().complete(() => ManualFillsSettings.Default) }; IEnumerable window_factories() { @@ -51,6 +55,7 @@ namespace bts.shell { yield return _fwf; yield return _plwf; yield return _aswf; + yield return _mfwf; } public Task create(Stream load_stream) => diff --git a/src/client/spark/core/Bus/SparkCommands.cs b/src/client/spark/core/Bus/SparkCommands.cs index 88291a993..26d459533 100644 --- a/src/client/spark/core/Bus/SparkCommands.cs +++ b/src/client/spark/core/Bus/SparkCommands.cs @@ -1,15 +1,15 @@ using bts.api; using bts.api.orderman; using bts.shell; +using bts.shell.manual_fills; +using mbts; using mbts.spark; using mbts.ui; -using mbts; using System; using System.Collections.Generic; using System.Windows.Input; -namespace bts.spark -{ +namespace bts.spark { static class SparkCommands { // Market public static PartialCommand ShowMarketView => @@ -30,6 +30,9 @@ namespace bts.spark public static PartialCommand ShowFills(KeyGesture key_gesture = null) => SparkCommand.create("Fills", Icons.Payment, SparkTags.ShowFillsTag, key_gesture); + public static PartialCommand ShowManualFills(KeyGesture key_gesture = null) => + SparkCommand.create("Manual Fills", Icons.Math, SparkTags.ShowManualFillsTag, key_gesture); + public static PartialCommand ShowPAndL => SparkCommand.create("P&L", Icons.Accounting, SparkTags.ShowPAndLTag); diff --git a/src/client/spark/core/Bus/SparkTags.cs b/src/client/spark/core/Bus/SparkTags.cs index d32380fe1..c1c683f1c 100644 --- a/src/client/spark/core/Bus/SparkTags.cs +++ b/src/client/spark/core/Bus/SparkTags.cs @@ -1,6 +1,7 @@ using bts.api; using bts.api.orderman; using bts.shell; +using bts.shell.manual_fills; using bts.spark; using mbts.ui; using System; @@ -52,6 +53,7 @@ namespace mbts.spark #region Spark Window public static ITag ShowFillsTag { get; } = new SparkTag(nameof(ShowFillsTag)); + public static ITag ShowManualFillsTag { get; } = new SparkTag(nameof(ShowManualFillsTag)); // TODO: maybe get rid of the singlular ShowLadderTag, and just use the plural ShowLaddersTag. // The owners of the various SparkWindows may remove dependencies on the former at their leasure. // For now, both tags go through the same logic in the LadderWindowFactory anyways. diff --git a/src/client/spark/core/Utility/Delegates.cs b/src/client/spark/core/Utility/Delegates.cs index 28a606793..90b03ac5a 100644 --- a/src/client/spark/core/Utility/Delegates.cs +++ b/src/client/spark/core/Utility/Delegates.cs @@ -1,4 +1,14 @@ -namespace mbts.utils { +using System; + +namespace mbts.utils { delegate bool TryGetValue(out T value); delegate bool TryGetValue(TKey key, out TValue value); + + static class LogicalDelegates { + public static readonly Func + And = (a, b) => a && b, + Or = (a, b) => a || b, + XOr = (a, b) => a ? !b : b, + None = (a, b) => !a && !b; + } } \ No newline at end of file diff --git a/src/client/spark/core/Utility/ObjectSnapshot.cs b/src/client/spark/core/Utility/ObjectSnapshot.cs new file mode 100644 index 000000000..ef2a594ff --- /dev/null +++ b/src/client/spark/core/Utility/ObjectSnapshot.cs @@ -0,0 +1,103 @@ +using bts.api.core; +using mbts; +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Linq; +using System.Reactive.Linq; +using System.Reflection; + +namespace bts.utils { + static class ObjectSnapshot { + public static IObservable> property_table(T instance, params string[] prop_names) where T : INotifyPropertyChanged => + property_table(instance, Array.ConvertAll(prop_names, Impl.type.GetProperty)); + + public static IObservable> property_table(T instance, params PropertyInfo[] props) where T : INotifyPropertyChanged => + Observable.Create>(o => { + var cache = props.ToDictionary(p => p.Name, p => new PropNode(p, p.GetValue(instance))); + var table = TableUpdate<(string name, object current, object original), string>.mk_empty(t => t.name).with_added(cache.Values.Select(pn => (pn.prop.Name, pn.current, pn.original))); + instance.PropertyChanged += on_prop_changed; + return Disposable.Create(() => instance.PropertyChanged -= on_prop_changed); + void on_prop_changed(object sender, PropertyChangedEventArgs e) { + if (cache.TryGetValue(e.PropertyName, out PropNode pn)) { + object oldval = pn.current; + pn.current = pn.prop.GetValue(instance); + table = table.with_changes(new[] { new Change<(string, object, object)>((pn.prop.Name, oldval, pn.original), (pn.prop.Name, pn.current, pn.original)) }); + o.OnNext(table); + } + } + }); + + public static IObservable dirty_monitor(T instance, params string[] prop_names) where T : INotifyPropertyChanged => + dirty_monitor(instance, Array.ConvertAll(prop_names, Impl.type.GetProperty)); + + public static IObservable dirty_monitor(T instance, out IDisposable snapshot, params string[] prop_names) where T : INotifyPropertyChanged => + dirty_monitor(instance, out snapshot, Array.ConvertAll(prop_names, Impl.type.GetProperty)); + + public static IObservable dirty_monitor(T instance, out IDisposable snapshot, params PropertyInfo[] props) where T : INotifyPropertyChanged { + var prop_map = props.ToDictionary(p => p.Name, p => new PropNode(p, p.GetValue(instance))); + snapshot = Disposable.Create(() => { + foreach (var pn in prop_map.Values) + pn.prop.SetValue(instance, pn.original); + }); + return _dirty_monitor(instance, prop_map); + } + + public static IObservable dirty_monitor(T instance, PropertyInfo[] props) where T : INotifyPropertyChanged => + _dirty_monitor(instance, props.ToDictionary(p => p.Name, p => new PropNode(p, p.GetValue(instance)))); + + static IObservable _dirty_monitor(T instance, Dictionary prop_map) where T : INotifyPropertyChanged => + Observable.Create(o => { + var dirty_props = new HashSet(); + instance.PropertyChanged += on_prop_changed; + return Disposable.Create(() => instance.PropertyChanged -= on_prop_changed); + void on_prop_changed(object sender, PropertyChangedEventArgs e) { + if (prop_map.TryGetValue(e.PropertyName, out var p)) { + p.current = p.prop.GetValue(instance); + if (Equals(p.current, p.original) == p.is_dirty) { + p.is_dirty = !p.is_dirty; + if (p.is_dirty) { + if (dirty_props.Add(p.prop.Name) && dirty_props.Count == 1) + o.OnNext(true); + } else if (dirty_props.Remove(p.prop.Name) && dirty_props.Count == 0) + o.OnNext(false); + } + } + } + }); + + public static IDisposable create(T instance) => + create(instance, Impl.mutable_props); + + public static IDisposable create(T instance, PropertyInfo[] props) { + var snapshot = Array.ConvertAll(props, p => p.GetValue(instance)); + return Disposable.Create(() => { + for (int i = 0; i < props.Length; i++) + props[i].SetValue(instance, snapshot[i]); + }); + } + + static class Impl { + public static readonly Type type = typeof(T); + + public static readonly PropertyInfo[] mutable_props = + type.GetProperties(BindingFlags.Public | BindingFlags.Instance) + .Where(p => p.GetSetMethod() != null) + .ToArray(); + } + + class PropNode { + public readonly PropertyInfo prop; + + public PropNode(PropertyInfo prop, object init) { + this.prop = prop; + original = init; + current = init; + } + + public bool is_dirty { get; set; } + public object original { get; set; } + public object current { get; set; } + } + } +} \ No newline at end of file diff --git a/src/client/spark/core/Utility/ThresholdMonitor.cs b/src/client/spark/core/Utility/ThresholdMonitor.cs index f8c15ef85..35d9cb2b8 100644 --- a/src/client/spark/core/Utility/ThresholdMonitor.cs +++ b/src/client/spark/core/Utility/ThresholdMonitor.cs @@ -61,6 +61,7 @@ namespace mbts.utils { _threshold = value; } } + public IEnumerable filtered_items => _filtered_items; public IDisposable join(T item) => _subscribe_item == null ? throw new InvalidOperationException("No 'subscribe-item' delegate was specified in the constructor.") : join(item, _subscribe_item(item)); diff --git a/src/client/spark/view/Controls/Dialogs/BTSMessageBox.xaml.cs b/src/client/spark/view/Controls/Dialogs/BTSMessageBox.xaml.cs index 97f4032d7..1a68edb7d 100644 --- a/src/client/spark/view/Controls/Dialogs/BTSMessageBox.xaml.cs +++ b/src/client/spark/view/Controls/Dialogs/BTSMessageBox.xaml.cs @@ -36,6 +36,9 @@ namespace mbts.ui { #endregion + public static bool Confirm(string message, string title) => + Show(message, title, BTSMessageBoxButtons.Yes | BTSMessageBoxButtons.No, MessageBoxImage.Question, true, true, true) == BTSMessageBoxButtons.Yes; + public static Task ShowAsync(string message, string title) { return ShowAsync(message, title, BTSMessageBoxButtons.OK, MessageBoxImage.None, false, false); } diff --git a/src/client/spark/view/Controls/PriceEntryControl.xaml.cs b/src/client/spark/view/Controls/PriceEntryControl.xaml.cs index d2fe6ccbf..523557c3b 100644 --- a/src/client/spark/view/Controls/PriceEntryControl.xaml.cs +++ b/src/client/spark/view/Controls/PriceEntryControl.xaml.cs @@ -90,13 +90,20 @@ namespace mbts.ui.spark { nameof(ContractSpec), (o, v) => { o._spec = v; - if (o._raw_value.HasValue) + if (o._raw_value.HasValue) { o.SetCurrentValue(ValueProperty, o._raw_value.Value); + if (o.AlwaysReformatOnSpecChange) + o._update_text(); + } }, SecurityDataHelper.Defaults.ContractSpecificationImmutable); public ContractSpecificationImmutable? ContractSpec { get => GetValue(ContractSpecProperty) as ContractSpecificationImmutable?; set => SetValue(ContractSpecProperty, value); } #endregion + /// TODO: verify that this control behaves correctly when is true (ideally this prop will be removed, and this control will always behave is if the prop is true). + /// Because this control is ubiquitious in our application, such verification will require careful, meticulous analysis. + /// Until such verification is complete, this boolean property will be used to trigger the behavior, so unsuspecting instances of this control will not be affected. + public bool AlwaysReformatOnSpecChange { get; set; } // explicit props public bool EnableArrowKeyTick { get => _enable_arrow_key_tick.value; set => _enable_arrow_key_tick.value = value; } public ModifierKeys ArrowKeyTickModifiers { get => _arrow_key_tick_modifiers.value; set => _arrow_key_tick_modifiers.value = value; } diff --git a/src/client/spark/view/Extensions/WPFExtensions.cs b/src/client/spark/view/Extensions/WPFExtensions.cs index 4c0c82877..b23ae7cbb 100644 --- a/src/client/spark/view/Extensions/WPFExtensions.cs +++ b/src/client/spark/view/Extensions/WPFExtensions.cs @@ -352,15 +352,23 @@ namespace mbts.ui { .Where(sc => sc.col_name != null) .ToList(); - /// - /// Converts .SortDescriptions into a List<>. - /// - public static List get_sorting_criteria(this ICollectionView collection_view, IEnumerable<(string id, string path)> columns) => - collection_view.SortDescriptions - .Select(sd => columns.TryGetFirst(c => c.path == sd.PropertyName, out var col) ? - new SortingCriteria(col.id, sd.Direction == ListSortDirection.Ascending) : null) - .Where(sc => sc?.col_name != null) - .ToList(); + public static IEnumerable get_sorting_criteria(this ICollectionView collection_view, IEnumerable columns) => + get_sorting_criteria(collection_view.SortDescriptions, columns.Select(c => (controls.DataGridEx.GetCellKey(c)?.ToString(), c.SortMemberPath))); + + public static IEnumerable get_sorting_criteria(IEnumerable sort_descriptions, IEnumerable<(string id, string path)> columns) => + from sd in sort_descriptions + join col in columns + on sd.PropertyName equals col.path + select new SortingCriteria(col.id, sd.Direction == ListSortDirection.Ascending); + + public static IEnumerable get_sort_descriptions(this IEnumerable sorting_criterion, IEnumerable columns) => + get_sort_descriptions(sorting_criterion, columns.Select(c => (controls.DataGridEx.GetCellKey(c)?.ToString(), c.SortMemberPath))); + + public static IEnumerable get_sort_descriptions(this IEnumerable sorting_criterion, IEnumerable<(string id, string path)> columns) => + from sc in sorting_criterion + join col in columns + on sc.col_name equals col.id + select new SortDescription(col.path, sc.direction()); /// /// Converts each into a and assigns the result to .SortDescriptions. diff --git a/src/client/spark/view/Utility/RXCollection.cs b/src/client/spark/view/Utility/RXCollection.cs index f5d8f76b1..177fd4a13 100644 --- a/src/client/spark/view/Utility/RXCollection.cs +++ b/src/client/spark/view/Utility/RXCollection.cs @@ -1,11 +1,14 @@ -using bts.collections; +using bts; +using bts.api.core; +using bts.collections; +using bts.utils; using mbts.utils; using System; -using System.Collections; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Collections.Specialized; using System.Linq; +using System.Reactive; using System.Reactive.Linq; namespace mbts.ui.utility { @@ -16,6 +19,44 @@ namespace mbts.ui.utility { } static class RXCollection { + // Note: comparer must be 100% deterministic (i.e., if a comprison returns 0, items must be equal) + public static IDisposable connect(ObservableCollection target, IObservable> source, IComparer comparer) where TKey : IEquatable => + source.Subscribe(table => { + foreach (var v in table.removed.Concat(table.changes.Old())) { + int i = CollectionHelper.binary_search(target, v, comparer); + if (i < 0) + Log.assert(false, TableUpdateMessges.sorted_target_error("Comparer failed to find value by binary search (comparer should be 100% deterministic, i.e., equality should be assumed if comparison returns 0)")); + target.RemoveAt(i); + } + foreach (var v in table.added.Concat(table.changed)) { + int i = CollectionHelper.binary_search(target, v, comparer); + if (i > 0) + Log.assert(false, TableUpdateMessges.sorted_target_error("Binary search for a new value (to be added) yielded a non-negative index (comparer should be 100% deterministic, i.e., equality should be assumed if comparison returns 0)")); + target.Insert(i, v); + } + }); + + // Note: comparer must be 100% deterministic (i.e., if a comprison returns 0, items must be equal) + public static IObserver> sort_target_observer(IList target, Func convert, Func convert_back, IComparer comparer) where TKey : IEquatable { + var comp_wrapper = comparer.wrap(convert_back); + return new AnonymousObserver>(table => { + foreach (var v in table.removed.Concat(table.changes.Old())) { + var result = convert(v); + int i = CollectionHelper.binary_search(target, result, comp_wrapper); + if (i < 0) + Log.assert(false, TableUpdateMessges.sorted_target_error("Comparer failed to find value by binary search (comparer should be 100% deterministic, i.e., equality is a tautology of a comparison that returns 0)")); + target.RemoveAt(i); + } + foreach (var v in table.added.Concat(table.changed)) { + var result = convert(v); + int i = CollectionHelper.binary_search(target, result, comp_wrapper); + if (i > 0) + Log.assert(false, TableUpdateMessges.sorted_target_error("Binary search for a new value (to be added) yielded a non-negative index (comparer should be 100% deterministic, i.e., equality should be assumed if comparison returns 0)")); + target.Insert(~i, result); + } + }); + } + public static IObservableCollection from_enumerable(IEnumerable source) => new RXCollectionWrapper(source); @@ -51,9 +92,12 @@ namespace mbts.ui.utility { } }); + static class TableUpdateMessges { + static readonly string _sorted_target_error_header = $"Error in incremental update of sorted collection in response to TableUpdate notification (value type '{typeof(T)}' key type '{typeof(TKey)}': "; - public static IRXCollection CreateIncrementalUpdate(IObservable> source) where TKey : IEquatable => - new IncrementalUpdateCollection(source); + public static string sorted_target_error(string error) => + $"{_sorted_target_error_header}: {error}."; + } class RXCollectionWrapper : ObservableCollection, IRXCollection { public RXCollectionWrapper(IEnumerable source) => @@ -88,39 +132,5 @@ namespace mbts.ui.utility { public bool TryGetValue(TKey key, out T result) => _raw_state.TryGetValue(key, out result); public void Dispose() => Disposable.dispose(ref _sub); } - - class IncrementalUpdateCollection : ObservableCollection, IRXCollection where TKey : IEquatable { - IDisposable _sub; - - internal IncrementalUpdateCollection(IObservable> source) { - _sub = source.OnDispatcher().Subscribe(u => { - foreach (T val in u.removed.Values) - Remove(val); - foreach (T val in u.added.Values) - Add(val); - }); - } - - public void Dispose() => Disposable.dispose(ref _sub); - } - - public static class ReadOnly { - public static IReadOnlyObservableCollection Empty() => Impl.instance; - - class Impl : IReadOnlyObservableCollection { - public static readonly IReadOnlyObservableCollection instance = new Impl(); - public T this[int index] => throw new IndexOutOfRangeException(); - public int Count => 0; - public event NotifyCollectionChangedEventHandler CollectionChanged; - public IEnumerator GetEnumerator() { yield break; } - IEnumerator IEnumerable.GetEnumerator() { yield break; } - void _raise_collection_changed() { - if (CollectionChanged == null) { - // shutup compiler - } - } - //CollectionChanged?.Invoke(this, new NotifyCollectionChangedEventArgs()); - } - } } } \ No newline at end of file diff --git a/src/client/spark/view/Utility/SortingCriteria.cs b/src/client/spark/view/Utility/SortingCriteria.cs index 6845f9470..29116e640 100644 --- a/src/client/spark/view/Utility/SortingCriteria.cs +++ b/src/client/spark/view/Utility/SortingCriteria.cs @@ -2,12 +2,10 @@ using System; using System.ComponentModel; -namespace mbts.ui -{ +namespace mbts.ui { [Serializable] [ProtoContract] - public class SortingCriteria - { + class SortingCriteria { public SortingCriteria() { } public SortingCriteria(SortDescription sort_description) : this(sort_description.PropertyName, sort_description.Direction == ListSortDirection.Ascending) { } public SortingCriteria(string col_name, bool is_ascending) { @@ -20,8 +18,11 @@ namespace mbts.ui [ProtoMember(2)] public bool is_ascending; - public SortDescription to_sort_description() { return new SortDescription(col_name, is_ascending ? ListSortDirection.Ascending : ListSortDirection.Descending); } - public override string ToString() => $"{col_name}:{(is_ascending ? "Ascending" : "Descending")}"; } + + static class SortingCriteriaExtensions { + public static ListSortDirection direction(this SortingCriteria sorting_criteria) => + sorting_criteria.is_ascending ? ListSortDirection.Ascending : ListSortDirection.Descending; + } } \ No newline at end of file diff --git a/src/client/spark_core/Collections/Extensions/EnumerableExtensions.cs b/src/client/spark_core/Collections/Extensions/EnumerableExtensions.cs index d2f85f273..2d57fe0d7 100644 --- a/src/client/spark_core/Collections/Extensions/EnumerableExtensions.cs +++ b/src/client/spark_core/Collections/Extensions/EnumerableExtensions.cs @@ -150,6 +150,20 @@ namespace System.Collections.Generic { return false; } + public static bool TryGetFirst(this IEnumerable source, Func predicate, out T result, out int index) { + if (source == null) throw new ArgumentNullException(nameof(source)); + index = 0; + foreach (T element in source) { + if (predicate(element)) { + result = element; + return true; + } + index++; + } + result = default; + return false; + } + public static bool TryGetFirst(this IEnumerable source, Func predicate, Func select, out TResult result) { if (source == null) throw new ArgumentNullException(nameof(source)); foreach (T element in source) diff --git a/src/client/spark_core/Collections/Extensions/ListExtensions.cs b/src/client/spark_core/Collections/Extensions/ListExtensions.cs index 00ab07626..814548a71 100644 --- a/src/client/spark_core/Collections/Extensions/ListExtensions.cs +++ b/src/client/spark_core/Collections/Extensions/ListExtensions.cs @@ -86,6 +86,9 @@ namespace bts.collections { public static IList WrapType(this IList list) => new TypeWrapper(list); + public static IList WrapSubRange(this IList list, int start) => + new SubRangeWrapper(list, start, list.Count - start); + public static IList WrapSubRange(this IList list, int start, int length) => new SubRangeWrapper(list, start, length); diff --git a/src/client/spark_core/utils/UpdateExtensions.cs b/src/client/spark_core/utils/UpdateExtensions.cs index 675a2a5cc..bf653b55f 100644 --- a/src/client/spark_core/utils/UpdateExtensions.cs +++ b/src/client/spark_core/utils/UpdateExtensions.cs @@ -7,6 +7,18 @@ using System.Reactive.Linq; namespace bts.utils { public static class UpdateExtensions { + public static TableUpdate with_added(this TableUpdate table, IEnumerable added) where TKey : IEquatable => + TableUpdate.update(table, added, Enumerable.Empty>(), Enumerable.Empty(), table.key_extractor); + + public static TableUpdate with_removed(this TableUpdate table, IEnumerable removed) where TKey : IEquatable => + TableUpdate.update(table, Enumerable.Empty(), Enumerable.Empty>(), removed, table.key_extractor); + + public static TableUpdate with_changed(this TableUpdate table, IEnumerable changed) where TKey : IEquatable => + TableUpdate.update(table, Enumerable.Empty(), changed.Select(v => new Change(table.state[table.key_extractor(v)], v)), Enumerable.Empty(), table.key_extractor); + + public static TableUpdate with_changes(this TableUpdate table, IEnumerable> changes) where TKey : IEquatable => + TableUpdate.update(table, Enumerable.Empty(), changes, Enumerable.Empty(), table.key_extractor); + public static IObservable> RootDependent( this IObservable> source, Func key, diff --git a/src/lib/config/protos/UI.proto b/src/lib/config/protos/UI.proto index ceaea14db..9bcc09076 100644 --- a/src/lib/config/protos/UI.proto +++ b/src/lib/config/protos/UI.proto @@ -53,6 +53,7 @@ message MultiThreadedWindows { optional bool time_and_sales = 2 [default = true]; optional bool order_book = 3 [default = true]; optional bool fills = 4 [default = true]; + optional bool manual_fills = 9 [default = true]; optional bool pnl = 5 [default = true]; optional bool algo_studio = 6 [default = true]; optional bool ladder = 7 [default = true]; From 893c6702a2eb7a0d3709560dffef0b685cee6513 Mon Sep 17 00:00:00 2001 From: John Colvin Date: Sat, 18 May 2019 11:53:57 -0500 Subject: [PATCH 049/121] make DictionaryExtensions.Join more efficient --- .../Collections/Extensions/DictionaryExtensions.cs | 8 -------- 1 file changed, 8 deletions(-) diff --git a/src/client/spark_core/Collections/Extensions/DictionaryExtensions.cs b/src/client/spark_core/Collections/Extensions/DictionaryExtensions.cs index 296d85640..e1f450d91 100644 --- a/src/client/spark_core/Collections/Extensions/DictionaryExtensions.cs +++ b/src/client/spark_core/Collections/Extensions/DictionaryExtensions.cs @@ -64,14 +64,6 @@ namespace System.Collections.Generic { dictionary.TryGetValue(key, out TValue result) ? result : default; #endregion - public static IReadOnlyDictionary Join(this IReadOnlyDictionary dictionary, IReadOnlyDictionary merge_target) => - Join(dictionary, merge_target, (v1, v2) => (v1, v2)); - - public static IReadOnlyDictionary Join(this IReadOnlyDictionary dictionary, IReadOnlyDictionary merge_target, Func select) { - var iks = dictionary.Keys.Intersect(merge_target.Keys); - return new ReadOnlyDictionary(iks.ToDictionary(k => k, k => select(dictionary[k], merge_target[k]))); - } - public static IReadOnlyDictionary LeftJoin(this IReadOnlyDictionary dictionary, IReadOnlyDictionary merge_target, Func select, Func select_left_only) => dictionary.ToDictionary(kv => kv.Key, kv => { From bded6d334ce4c3adce3b382ed28ab398efbe43a7 Mon Sep 17 00:00:00 2001 From: John Colvin Date: Sat, 18 May 2019 23:37:28 -0500 Subject: [PATCH 050/121] ContractPickerPopup 'CloseOnOK' should just close the popup (not the owning window) --- .../view/Spark/ContractPicker/ContractPickerPopup.xaml | 2 -- .../view/Spark/ContractPicker/ContractPickerView.xaml.cs | 9 ++++++--- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/src/client/spark/view/Spark/ContractPicker/ContractPickerPopup.xaml b/src/client/spark/view/Spark/ContractPicker/ContractPickerPopup.xaml index 2e5578b71..6bc4b493e 100644 --- a/src/client/spark/view/Spark/ContractPicker/ContractPickerPopup.xaml +++ b/src/client/spark/view/Spark/ContractPicker/ContractPickerPopup.xaml @@ -11,8 +11,6 @@ > \ No newline at end of file diff --git a/src/client/spark/view/Spark/ContractPicker/ContractPickerView.xaml.cs b/src/client/spark/view/Spark/ContractPicker/ContractPickerView.xaml.cs index 054ef54b3..1a6492117 100644 --- a/src/client/spark/view/Spark/ContractPicker/ContractPickerView.xaml.cs +++ b/src/client/spark/view/Spark/ContractPicker/ContractPickerView.xaml.cs @@ -124,14 +124,14 @@ namespace mbts.spark #region CloseOnOK public static readonly DependencyProperty CloseOnOKProperty = - DialogSkirtOKCancel.CloseOnOKProperty.AddOwner(typeof(ContractPickerView)); + DialogSkirtOKCancel.CloseOnOKProperty.AddOwner(typeof(ContractPickerView), new PropertyMetadata(false)); public bool CloseOnOK { get => (bool)GetValue(CloseOnOKProperty); set => SetValue(CloseOnOKProperty, value); } #endregion #region CloseOnCancel public static readonly DependencyProperty CloseOnCancelProperty = - DialogSkirtOKCancel.CloseOnCancelProperty.AddOwner(typeof(ContractPickerView)); + DialogSkirtOKCancel.CloseOnCancelProperty.AddOwner(typeof(ContractPickerView), new PropertyMetadata(false)); public bool CloseOnCancel { get => (bool)GetValue(CloseOnCancelProperty); set => SetValue(CloseOnCancelProperty, value); } #endregion @@ -179,7 +179,10 @@ namespace mbts.spark //return ShellWindow.create(this, "test", view, vm.get_state, Icons.Abacus); var x = new Window { - Content = new ContractPickerView(new ContractPickerModel(_bus, false, null)) + Content = new ContractPickerView(new ContractPickerModel(_bus, false, null)) { + CloseOnOK = true, + CloseOnCancel = true + } }; //x.SizeToContent = SizeToContent.WidthAndHeight; x.Show(); From 0e5833b2758d8ee10ab3c60118bcf081d4a2989c Mon Sep 17 00:00:00 2001 From: John Colvin Date: Sun, 19 May 2019 13:06:11 -0500 Subject: [PATCH 051/121] SetStack - 'SoftCount' logic was incorrect --- src/client/spark_core/Collections/SetStack.cs | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/client/spark_core/Collections/SetStack.cs b/src/client/spark_core/Collections/SetStack.cs index dcf892895..52850b462 100644 --- a/src/client/spark_core/Collections/SetStack.cs +++ b/src/client/spark_core/Collections/SetStack.cs @@ -89,15 +89,13 @@ namespace bts.collections { } public bool IsProperSubsetOf(IEnumerable other) { - if (other.SoftCount() is int other_count && other_count <= Count) - return false; + // TODO: optimize for cases where other is a more compatible type. _mark_common(other, out int num_marked, out int num_unmarked); return num_marked == Count && num_unmarked > 0; } public bool IsProperSupersetOf(IEnumerable other) { - if (other.SoftCount() is int other_count && other_count < Count) - return true; + // TODO: optimize for cases where other is a more compatible type. _mark_common(other, out int num_marked, out int num_unmarked); return num_marked < Count && num_unmarked > 0; } From 44f2a9daf7f2ac1e072c982f286763c6d67149ba Mon Sep 17 00:00:00 2001 From: John Colvin Date: Sun, 19 May 2019 14:11:49 -0500 Subject: [PATCH 052/121] RefCountMap implements IReadOnlyCollection --- src/client/spark/core/Collections/RefCountMap.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/client/spark/core/Collections/RefCountMap.cs b/src/client/spark/core/Collections/RefCountMap.cs index 7a98672ef..76755ddc8 100644 --- a/src/client/spark/core/Collections/RefCountMap.cs +++ b/src/client/spark/core/Collections/RefCountMap.cs @@ -9,7 +9,7 @@ namespace mbts.collections { int count { get; } } - class RefCountMap : IEnumerable>, ICollection { + class RefCountMap : IReadOnlyCollection>, ICollection { readonly IEqualityComparer _key_comparer; readonly Dictionary _dictionary; @@ -241,4 +241,4 @@ namespace mbts.collections { return ref_count_map.Count > count_before; } } -} \ No newline at end of file +} From a1b89ccb346ed29d70e0a3e52da4f15e7bb2c900 Mon Sep 17 00:00:00 2001 From: John Colvin Date: Sun, 19 May 2019 14:12:22 -0500 Subject: [PATCH 053/121] change RefCountMap.keys from IEnumerable to IReadOnlyCollection --- src/client/spark/core/Collections/RefCountMap.cs | 2 +- src/client/spark/core/Utility/CommonValueMonitor.cs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/client/spark/core/Collections/RefCountMap.cs b/src/client/spark/core/Collections/RefCountMap.cs index 76755ddc8..be9fb8678 100644 --- a/src/client/spark/core/Collections/RefCountMap.cs +++ b/src/client/spark/core/Collections/RefCountMap.cs @@ -28,7 +28,7 @@ namespace mbts.collections { public object SyncRoot => null; public bool IsSynchronized => false; public int this[TKey key] => _dictionary[key].count; - public ICollection keys => _dictionary.Keys; + public IReadOnlyCollection keys => _dictionary.Keys; public bool contains_key(TKey key) => _dictionary.ContainsKey(key); public void CopyTo(Array array, int index) => diff --git a/src/client/spark/core/Utility/CommonValueMonitor.cs b/src/client/spark/core/Utility/CommonValueMonitor.cs index cdb99d9b5..c38a65e48 100644 --- a/src/client/spark/core/Utility/CommonValueMonitor.cs +++ b/src/client/spark/core/Utility/CommonValueMonitor.cs @@ -40,7 +40,7 @@ namespace bts.utils { public event Action common_value_changed; public event Action plurality_changed; - public ICollection distinct_values => _values.keys; + public IReadOnlyCollection distinct_values => _values.keys; public T common_value => _values.keys.SafeSingleOrDefault(_default_value); public Plurality plurality { get { From af64592f522cb5605f30e58c29702c7d80629dd6 Mon Sep 17 00:00:00 2001 From: John Colvin Date: Sun, 19 May 2019 14:16:16 -0500 Subject: [PATCH 054/121] ReactiveThresholdMonitor use RefCountMap to support duplicate adds --- src/client/spark/core/Utility/ThresholdMonitor.cs | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/src/client/spark/core/Utility/ThresholdMonitor.cs b/src/client/spark/core/Utility/ThresholdMonitor.cs index 35d9cb2b8..13af09ac7 100644 --- a/src/client/spark/core/Utility/ThresholdMonitor.cs +++ b/src/client/spark/core/Utility/ThresholdMonitor.cs @@ -1,4 +1,5 @@ -using System; +using mbts.collections; +using System; using System.Collections.Generic; using System.Reactive.Disposables; using System.Threading; @@ -37,7 +38,7 @@ namespace mbts.utils { // Warning: This class is not thread-safe. Use the .OnDispatcher extension in the subscribe_item delegate if necessary to ensure all updates occur on a single thread. class ReactiveThresholdMonitor : IObservable { - readonly HashSet _filtered_items; + readonly RefCountMap _filtered_items; readonly List> _observers = new List>(); readonly Func> _subscribe_item; uint _threshold; @@ -50,7 +51,7 @@ namespace mbts.utils { public ReactiveThresholdMonitor(Func> subscribe_item, IEqualityComparer comparer) { _subscribe_item = subscribe_item; - _filtered_items = new HashSet(comparer); + _filtered_items = new RefCountMap(comparer); } public bool is_above_threshold => _filtered_items.Count > _threshold; @@ -61,7 +62,7 @@ namespace mbts.utils { _threshold = value; } } - public IEnumerable filtered_items => _filtered_items; + public IReadOnlyCollection filtered_items => _filtered_items.keys; public IDisposable join(T item) => _subscribe_item == null ? throw new InvalidOperationException("No 'subscribe-item' delegate was specified in the constructor.") : join(item, _subscribe_item(item)); @@ -69,12 +70,12 @@ namespace mbts.utils { public IDisposable join(T item, IObservable source) => new CompositeDisposable(source.Subscribe(val => { using (_begin_update()) { - if (val) _filtered_items.Add(item); - else _filtered_items.Remove(item); + if (val) _filtered_items.increment(item); + else _filtered_items.decrement(item); } }), Disposable.Create(() => { using (_begin_update()) - _filtered_items.Remove(item); + _filtered_items.decrement(item); })); public IDisposable Subscribe(IObserver observer) { From 57b70684ea0f0cef981efac43faba54db63e49f5 Mon Sep 17 00:00:00 2001 From: John Colvin Date: Sun, 19 May 2019 14:24:19 -0500 Subject: [PATCH 055/121] EnumHelper.get_values returns a cached strongly typed IReadOnlyList (simple optimization) --- src/client/spark/view/Utility/EnumHelper.cs | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/client/spark/view/Utility/EnumHelper.cs b/src/client/spark/view/Utility/EnumHelper.cs index b2043831b..3fc0c8926 100644 --- a/src/client/spark/view/Utility/EnumHelper.cs +++ b/src/client/spark/view/Utility/EnumHelper.cs @@ -10,9 +10,11 @@ using System.Reflection; namespace mbts.ui.utility { static class EnumHelper { - public static IEnumerable get_values() { return Enum.GetValues(typeof(T)).Cast(); } + public static IReadOnlyList get_values() => + EnumDescriptorStore.values; - public static IEnumerable get_values_except(params T[] except) { return get_values().Except(except); } + public static IEnumerable get_values_except(params T[] except) => + get_values().Except(except); public static IEnumerable get_values(Type t) { if (!t.IsEnum) { @@ -115,6 +117,8 @@ namespace mbts.ui.utility { static class EnumDescriptorStore { public static readonly EnumDescriptor descriptor; + public static readonly IReadOnlyList values = Enum.GetValues(typeof(T)).Cast().ToReadOnlyList(); + static EnumDescriptorStore() { Func has_flags; if (!typeof(T).IsEnum) { From 4768c1cd87e95c472983b9c8c89c3c046889708c Mon Sep 17 00:00:00 2001 From: John Colvin Date: Sun, 19 May 2019 14:42:10 -0500 Subject: [PATCH 056/121] SPARK-2447 / SPARK-2458 import dialog --- .../ManualFillsImportConflictsDialog.xaml | 39 +++++ .../ManualFillsImportConflictsDialog.xaml.cs | 11 ++ .../ManualFillsImportConflictsVM.cs | 106 ++++++++++++ .../ManualFillsImportConflictsView.xaml | 144 ++++++++++++++++ .../ManualFillsImportConflictsView.xaml.cs | 30 ++++ .../Trading/ManualFills/ManualFillsModel.cs | 9 +- .../Trading/ManualFills/ManualFillsParser.cs | 160 ++++++++++++++++++ .../Trading/ManualFills/ManualFillsVM.cs | 74 +++++++- .../Trading/ManualFills/ManualFillsView.xaml | 2 +- .../ManualFills/ManualFillsWindowFactory.cs | 15 +- .../Controls/Dialogs/BTSMessageBox.xaml.cs | 3 + .../Extensions/Command/FileDialogCommand.cs | 3 + .../Extensions/EnumerableExtensions.cs | 7 + src/client/spark_core/utils/ConvertUtils.cs | 3 + src/client/spark_core/utils/RowParser.cs | 53 ++++++ 15 files changed, 647 insertions(+), 12 deletions(-) create mode 100644 src/client/spark/Modules/Trading/ManualFills/ManualFillsImportConflictsDialog.xaml create mode 100644 src/client/spark/Modules/Trading/ManualFills/ManualFillsImportConflictsDialog.xaml.cs create mode 100644 src/client/spark/Modules/Trading/ManualFills/ManualFillsImportConflictsVM.cs create mode 100644 src/client/spark/Modules/Trading/ManualFills/ManualFillsImportConflictsView.xaml create mode 100644 src/client/spark/Modules/Trading/ManualFills/ManualFillsImportConflictsView.xaml.cs create mode 100644 src/client/spark/Modules/Trading/ManualFills/ManualFillsParser.cs create mode 100644 src/client/spark_core/utils/RowParser.cs diff --git a/src/client/spark/Modules/Trading/ManualFills/ManualFillsImportConflictsDialog.xaml b/src/client/spark/Modules/Trading/ManualFills/ManualFillsImportConflictsDialog.xaml new file mode 100644 index 000000000..58c33c1dd --- /dev/null +++ b/src/client/spark/Modules/Trading/ManualFills/ManualFillsImportConflictsDialog.xaml @@ -0,0 +1,39 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/client/spark/Modules/Trading/ManualFills/ManualFillsImportConflictsDialog.xaml.cs b/src/client/spark/Modules/Trading/ManualFills/ManualFillsImportConflictsDialog.xaml.cs new file mode 100644 index 000000000..e3a0e8cde --- /dev/null +++ b/src/client/spark/Modules/Trading/ManualFills/ManualFillsImportConflictsDialog.xaml.cs @@ -0,0 +1,11 @@ +using System.Windows; + +namespace mbts.ui.spark { + /// + /// Interaction logic for ManualFillsImportConflictsDialog.xaml + /// + partial class ManualFillsImportConflictsDialog : Window { + public ManualFillsImportConflictsDialog() => + InitializeComponent(); + } +} \ No newline at end of file diff --git a/src/client/spark/Modules/Trading/ManualFills/ManualFillsImportConflictsVM.cs b/src/client/spark/Modules/Trading/ManualFills/ManualFillsImportConflictsVM.cs new file mode 100644 index 000000000..c2125db20 --- /dev/null +++ b/src/client/spark/Modules/Trading/ManualFills/ManualFillsImportConflictsVM.cs @@ -0,0 +1,106 @@ +using bts.api; +using bts.collections; +using mbts; +using mbts.collections; +using mbts.ui; +using mbts.ui.shell; +using mbts.ui.utility; +using System; +using System.Collections.Generic; +using System.Collections.ObjectModel; +using System.ComponentModel; +using System.Linq; +using System.Reactive.Linq; +using System.Windows.Data; +using System.Windows.Input; +using Component = mbts.ui.spark.ManualFillsImportConflictsView.ManualFillComponent; + +namespace bts.shell.manual_fills { + class ManualFillsImportVM : DisposableViewModelBase, IRequestViewClose { + readonly RefCountMap _conflicts = new RefCountMap(); + IReadOnlyDictionary _security_map = DictionaryHelper.empty(); + + public ManualFillsImportVM(int num_fills) { + rows_view = CollectionViewSource.GetDefaultView(rows); + ok_cmd = new DelegateCommand(() => import_request?.Invoke()); + cancel_cmd = new DelegateCommand(() => RequestClose?.Invoke(this, new EventArgs())); + this.num_fills = num_fills; + _dispose = ObservableAutoWrapper.connect_item_hooks(rows, r => + r.subscribe_has_error().Subscribe(has_error => { + if (has_error) { + if (r.affected_fills.Count(_conflicts.increment_adds_key) == 0) + return; + } else if (r.affected_fills.Count(_conflicts.decrement_removes_key) == 0) + return; + RaisePropertyChanged(nameof(num_successfully_parsed_fills)); + })); + } + + public event Action import_request; + public event EventHandler RequestClose; + + public ObservableCollection rows { get; } = new ObservableCollection(); + public ICollectionView rows_view { get; } + public int num_fills { get; } + public int num_successfully_parsed_fills => num_fills - _conflicts.Count; + // misc items sources + public IEnumerable sides => new[] { Side.Buy, Side.Sell }; + public ObservableCollection accounts { get; } = new ObservableCollection(); + public ObservableCollection users { get; } = new ObservableCollection(); + public IReadOnlyDictionary security_map { + get => _security_map; + set { + if (!ReferenceEquals(_security_map, value)) { + _security_map = value; + _resolve_securities(); + RaisePropertyChanged(); + } + } + } + // commands + public ICommand ok_cmd { get; } + public ICommand cancel_cmd { get; } + + void _resolve_securities() { + if (_security_map == null) + return; + var reverse_lookup = ManualFillsParser.reverse_lookup(MappedDictionary.create(_security_map, v => v.security.ToString())); + foreach (var r in rows) { + if (r.component == Component.Security && r.has_error) { + var id = reverse_lookup(r.text); + if (id != 0 && _security_map.TryGetValue(id, out var smd)) + r.value = smd.security; + } + } + } + + public class Row : ViewModelBase, ITemplateSelectorKey { + readonly DelegateProperty _has_error; + object _value; + string _text; + + public Row(Component component, string text, IReadOnlyCollection affected_fills, object error_value, object value) { + this.component = component; + this.text = text; + this.affected_fills = affected_fills; + _value = value; + _has_error = backing(nameof(has_error), () => Equals(this.value, error_value)); + } + + public Component component { get; } + public string text { get => _text; set => Set(ref _text, value); } + public IReadOnlyCollection affected_fills { get; } + public object value { + get => _value; + set { + if (Set(ref _value, value)) + _has_error.refresh(); + } + } + public bool has_error => _has_error.Value; + string ITemplateSelectorKey.key => component.ToString(); + + public IObservable subscribe_has_error() => _has_error.observe_current(true); + } + } +} \ No newline at end of file diff --git a/src/client/spark/Modules/Trading/ManualFills/ManualFillsImportConflictsView.xaml b/src/client/spark/Modules/Trading/ManualFills/ManualFillsImportConflictsView.xaml new file mode 100644 index 000000000..04d77bb66 --- /dev/null +++ b/src/client/spark/Modules/Trading/ManualFills/ManualFillsImportConflictsView.xaml @@ -0,0 +1,144 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/client/spark/Modules/Trading/ManualFills/ManualFillsImportConflictsView.xaml.cs b/src/client/spark/Modules/Trading/ManualFills/ManualFillsImportConflictsView.xaml.cs new file mode 100644 index 000000000..54e5dc78d --- /dev/null +++ b/src/client/spark/Modules/Trading/ManualFills/ManualFillsImportConflictsView.xaml.cs @@ -0,0 +1,30 @@ +using System.Windows.Controls; + +namespace mbts.ui.spark { + /// + /// Interaction logic for ManualFillsImportConflictsView.xaml + /// + partial class ManualFillsImportConflictsView : UserControl { + public ManualFillsImportConflictsView() => + InitializeComponent(); + + public enum ManualFillComponent { + Security, + Account, + User, + Side, + Size, + Price + } + + public static class ManualFillComponentNames { + public static readonly string + Security = ManualFillComponent.Security.ToString(), + Account = ManualFillComponent.Account.ToString(), + User = ManualFillComponent.User.ToString(), + Side = ManualFillComponent.Side.ToString(), + Size = ManualFillComponent.Size.ToString(), + Price = ManualFillComponent.Price.ToString(); + } + } +} \ No newline at end of file diff --git a/src/client/spark/Modules/Trading/ManualFills/ManualFillsModel.cs b/src/client/spark/Modules/Trading/ManualFills/ManualFillsModel.cs index 3f0c9d873..3d2a78959 100644 --- a/src/client/spark/Modules/Trading/ManualFills/ManualFillsModel.cs +++ b/src/client/spark/Modules/Trading/ManualFills/ManualFillsModel.cs @@ -136,8 +136,11 @@ namespace bts.shell.manual_fills { public async Task update_fills(IEnumerable fills) => await _model.update_manual_fills(fills); - public void add_new_placeholder(ManualFillTuple value) { - _placeholder_table = _placeholder_table.with_added(new[] { new ManualFillData(new ManualFillKey(Guid.NewGuid()), value) }); + public void add_new_placeholder(ManualFillTuple value) => + add_new_placeholder(new[] { value }); + + public void add_new_placeholder(IEnumerable values) { + _placeholder_table = _placeholder_table.with_added(values.Select(v => new ManualFillData(new ManualFillKey(Guid.NewGuid()), v)).ToList()); if (_placeholder_table.has_changes()) _placeholder_table_changed?.Invoke(_placeholder_table); } @@ -154,4 +157,4 @@ namespace bts.shell.manual_fills { } } } -} +} \ No newline at end of file diff --git a/src/client/spark/Modules/Trading/ManualFills/ManualFillsParser.cs b/src/client/spark/Modules/Trading/ManualFills/ManualFillsParser.cs new file mode 100644 index 000000000..cad137c35 --- /dev/null +++ b/src/client/spark/Modules/Trading/ManualFills/ManualFillsParser.cs @@ -0,0 +1,160 @@ +using bts.api; +using bts.api.position; +using bts.collections; +using bts.utils; +using mbts.ui.utility; +using System; +using System.Collections.Generic; +using System.Linq; +using ManualFillComponent = mbts.ui.spark.ManualFillsImportConflictsView.ManualFillComponent; + +namespace bts.shell.manual_fills { + class ParsedManualFill { + public readonly Guid unpersisted_id = Guid.NewGuid(); + public readonly ManualFill result = new ManualFill(); + + public (string text, bool success) security { get; set; } + public (string text, bool success) account { get; set; } + public (string text, bool success) user { get; set; } + public (string text, bool success) side { get; set; } + public (string text, bool success) quantity { get; set; } + public (string text, bool success) price { get; set; } + + public bool Equals(ParsedManualFill other) => other != null && unpersisted_id == other.unpersisted_id; + public override bool Equals(object obj) => Equals(obj as ParsedManualFill); + public override int GetHashCode() => unpersisted_id.GetHashCode(); + } + + static class ManualFillsParser { + public static Action assign_component(ManualFillComponent component) { + switch (component) { + case ManualFillComponent.Security: return (mf, val) => mf.SecurityKey = ConvertUtils.try_convert(val)?.Id ?? 0; + case ManualFillComponent.Account: return (mf, val) => mf.AccountId = ConvertUtils.try_convert(val)?.id ?? 0; + case ManualFillComponent.User: return (mf, val) => mf.UserId = ConvertUtils.try_convert(val)?.id ?? 0; + case ManualFillComponent.Side: return (mf, val) => mf.Side = ConvertUtils.try_convert(val, Side.Undefined); + case ManualFillComponent.Size: return (mf, val) => mf.Quantity = ConvertUtils.try_convert(val); + case ManualFillComponent.Price: return (mf, val) => mf.Price = ConvertUtils.try_convert(val, double.NaN); + default: throw new NotImplementedException(); + } + } + + /// Note: the returned dictionary will have entries for each , regardless of whether or not that component has any conflicts. + public static Dictionary>> get_conflicts(IEnumerable parsed_fills) { + var missing_components = EnumHelper.get_values().ToDictionary(c => c, c => new Dictionary>()); + var mk_list = new Func>(_ => new List()); + foreach (var pmf in parsed_fills) { + if (!pmf.security.success) { + add_missing(ManualFillComponent.Security, pmf.security.text, pmf); + } + if (!pmf.account.success) { + add_missing(ManualFillComponent.Account, pmf.account.text, pmf); + } + if (!pmf.user.success) { + add_missing(ManualFillComponent.User, pmf.user.text, pmf); + } + if (!pmf.quantity.success) { + add_missing(ManualFillComponent.Size, pmf.quantity.text, pmf); + } + if (!pmf.side.success) { + add_missing(ManualFillComponent.Side, pmf.side.text, pmf); + } + if (!pmf.price.success) { + add_missing(ManualFillComponent.Price, pmf.price.text, pmf); + } + } + return missing_components; + void add_missing(ManualFillComponent component, string text, ParsedManualFill result) => + missing_components[component].GetOrAdd(text, mk_list).Add(result); + } + + // This implementation prioritizes simplicity over speed. + // The security desc lookup could be better optimized, but the speed at which fills are imported is probably not a paramount concern. + public static IEnumerable parse_rows(IEnumerable rows, IReadOnlyDictionary security_map, IReadOnlyDictionary account_map, IReadOnlyDictionary user_map) => + parse_rows(rows, + reverse_lookup(MappedDictionary.create(security_map, kv => kv.Value.ToString())), + reverse_lookup(account_map), + reverse_lookup(user_map)); + + public static IEnumerable parse_rows(IEnumerable rows, Func security_id_lookup, Func account_id_lookup, Func user_id_lookup) { + var columns = new[] { + (Headers.Security, Cells.security(security_id_lookup)), + (Headers.Account, Cells.account(account_id_lookup)), + (Headers.User, Cells.user(user_id_lookup)), + (Headers.Side, Cells.side), + (Headers.Quantity, Cells.quantity), + (Headers.Price, Cells.price) + }.ToDictionary(col => col.Item1, col => col.Item2.with_null_check()); + return RowParser.parse_rows(rows, columns, Cells.delimiter); + } + + public static Func reverse_lookup(IReadOnlyDictionary map) { + var lookup = map.ToLookup(kv => kv.Value, kv => kv.Key); + return cell_text => lookup.Contains(cell_text) ? lookup[cell_text].First() : default; + } + + static class Headers { + public const string + Security = "Security", + Account = "Account", + User = "User", + Side = "Side", + Quantity = "Quantity", + Price = "Price"; + } + + static class Cells { + public const char delimiter = ','; + + public static RowParser.AssignColumnValue security(Func reverse_id_lookup) { + return impl; + void impl(ref ParsedManualFill pmf, string text) { + pmf.result.SecurityKey = reverse_id_lookup(text); + pmf.security = (text, pmf.result.SecurityKey != 0); + } + } + + public static void side(ref ParsedManualFill pmf, string cell_text) { + switch (cell_text[0]) { + case 'B': pmf.result.Side = Side.Buy; break; + case 'S': pmf.result.Side = Side.Sell; break; + default: pmf.side = (cell_text, false); break; + } + pmf.side = (cell_text, true); + } + + public static void quantity(ref ParsedManualFill pmf, string cell_text) { + if (uint.TryParse(cell_text, out uint v)) { + pmf.result.Quantity = v; + pmf.quantity = (cell_text, true); + } else { + pmf.quantity = (cell_text, false); + } + } + + public static void price(ref ParsedManualFill pmf, string cell_text) { + if (double.TryParse(cell_text, out var v)) { + pmf.result.Price = v; + pmf.price = (cell_text, true); + } else { + pmf.price = (cell_text, false); + } + } + + public static RowParser.AssignColumnValue account(Func reverse_id_lookup) { + return impl; + void impl(ref ParsedManualFill pmf, string text) { + pmf.result.AccountId = reverse_id_lookup(text); + pmf.account = (text, pmf.result.AccountId != 0); + } + } + + public static RowParser.AssignColumnValue user(Func reverse_id_lookup) { + return impl; + void impl(ref ParsedManualFill pmf, string text) { + pmf.result.UserId = reverse_id_lookup(text); + pmf.user = (text, pmf.result.UserId != 0); + } + } + } + } +} \ No newline at end of file diff --git a/src/client/spark/Modules/Trading/ManualFills/ManualFillsVM.cs b/src/client/spark/Modules/Trading/ManualFills/ManualFillsVM.cs index f14e04749..e422b5c62 100644 --- a/src/client/spark/Modules/Trading/ManualFills/ManualFillsVM.cs +++ b/src/client/spark/Modules/Trading/ManualFills/ManualFillsVM.cs @@ -14,6 +14,7 @@ using System.Collections.Generic; using System.Collections.ObjectModel; using System.Collections.Specialized; using System.ComponentModel; +using System.IO; using System.Linq; using System.Reactive.Disposables; using System.Reactive.Linq; @@ -21,6 +22,7 @@ using System.Threading.Tasks; using System.Windows.Data; using System.Windows.Input; using Disposable = mbts.Disposable; +using ManualFillComponent = mbts.ui.spark.ManualFillsImportConflictsView.ManualFillComponent; namespace bts.shell.manual_fills { enum EditableItemState { @@ -35,6 +37,7 @@ namespace bts.shell.manual_fills { readonly ManualFillsModel _model; readonly OperableCollection _rows; readonly ObservableCollection _placeholder_rows = new ObservableCollection(); + IDisposable _import_fills_hooks; public static ManualFillsVM create(IBus bus, OrdersModel model) => new ManualFillsVM(bus, new ManualFillsModel(model)); @@ -74,6 +77,7 @@ namespace bts.shell.manual_fills { _model.delete_rows(selection_mon.filtered_items).ignore(); }, out var remove_selected_fills_observer); add_fill_cmd = new DelegateCommand(_add_fill); + import_fills_cmd = FileDialogCommand.open_file(_import_fills); // var dirty_or_new_src = dirty_mon.CombineLatest(placeholder_mon, LogicalDelegates.Or); _dispose = Disposable.Create( @@ -96,7 +100,8 @@ namespace bts.shell.manual_fills { selection_mon.CombineLatest(dirty_or_new_src, LogicalDelegates.And).OnDispatcher().Subscribe(commit_selected_fills_observer), // accounts/users subs model.account_table.OnDispatcher().Subscribe(RXCollection.sort_target_observer(accounts, a => new AccountTuple?(a), a => a.Value, Comparer.Default)), - model.user_table.OnDispatcher().Subscribe(RXCollection.sort_target_observer(users, u => new UserTuple?(u), u => u.Value, Comparer.Default)) + model.user_table.OnDispatcher().Subscribe(RXCollection.sort_target_observer(users, u => new UserTuple?(u), u => u.Value, Comparer.Default)), + Disposable.Create(() => Disposable.dispose(ref _import_fills_hooks)) ); } @@ -140,5 +145,72 @@ namespace bts.shell.manual_fills { await _model.create_fills(adds.WrapSelector(t => t.manual_fill, null)); } } + + void _import_fills(string file_name) { + try { + var lines = File.ReadAllLines(file_name); + var parsed_fills = ManualFillsParser.parse_rows( + lines, + MappedDictionary.create(security_map, kv => kv.Value.security), + accounts.ToDictionary(a => a.Value.id, a => a.Value.name), + users.ToDictionary(u => u.Value.id, u => u.Value.name) + ).ToList(); + var conflicts = ManualFillsParser.get_conflicts(parsed_fills); + if (conflicts.Values.Any(c => c.Count > 0)) { + // init vm + var vm = new ManualFillsImportVM(parsed_fills.Count) { + security_map = security_map + }; + // add rows + foreach (var kv in conflicts) { + var assign_del = ManualFillsParser.assign_component(kv.Key); + foreach (var text_groups in kv.Value) { + var eval = get_error_value(kv.Key); + var row = new ManualFillsImportVM.Row(kv.Key, text_groups.Key, text_groups.Value, eval, eval); + vm.rows.Add(row); + vm.import_request += () => { + foreach (var pmf in text_groups.Value) + assign_del(pmf.result, row.value); + }; + } + } + var win = new mbts.ui.spark.ManualFillsImportConflictsDialog { + DataContext = vm, + Title = ManualFillsWindowFactory.window_name, + Topmost = true + }; + vm.RequestClose += (s, e) => Disposable.dispose(ref _import_fills_hooks); + vm.import_request += () => { + add_imported_fills(); + Disposable.dispose(ref _import_fills_hooks); + }; + Disposable.replace(ref _import_fills_hooks, Disposable.Create( + ObservableAutoWrapper.connect(accounts, vm.accounts), + ObservableAutoWrapper.connect(users, vm.users), + vm, + Disposable.Create(close) + )); + win.move_to_mouse(-200, -75); + win.Show(); + void close() => WindowUtilities.force_close(win); + object get_error_value(ManualFillComponent component) { + switch (component) { + case ManualFillComponent.Price: return double.NaN; + case ManualFillComponent.Size: return 0; + default: return null; + } + } + } else { + add_imported_fills(); + } + void add_imported_fills() => + _model.add_new_placeholder(parsed_fills.Select(pmf => new ManualFillTuple(new ManualFillImmutable(pmf.result), + pmf.result.SecurityKey != 0 && security_map.TryGetValue(pmf.result.SecurityKey, out var smd) ? smd.security : (SecurityImmutable?)null, + accounts.FirstOrDefault(a => a?.id == pmf.result.AccountId), + users.FirstOrDefault(a => a?.id == pmf.result.UserId)))); + } catch (Exception ex) { + BTSMessageBox.Error($"Error importing fills: {ex}.", ManualFillsWindowFactory.window_name); + } + } } } diff --git a/src/client/spark/Modules/Trading/ManualFills/ManualFillsView.xaml b/src/client/spark/Modules/Trading/ManualFills/ManualFillsView.xaml index a8d4b5746..4da2a01bc 100644 --- a/src/client/spark/Modules/Trading/ManualFills/ManualFillsView.xaml +++ b/src/client/spark/Modules/Trading/ManualFills/ManualFillsView.xaml @@ -191,7 +191,7 @@ - + diff --git a/src/client/spark/Modules/Trading/ManualFills/ManualFillsWindowFactory.cs b/src/client/spark/Modules/Trading/ManualFills/ManualFillsWindowFactory.cs index f6d7a73e9..82e214824 100644 --- a/src/client/spark/Modules/Trading/ManualFills/ManualFillsWindowFactory.cs +++ b/src/client/spark/Modules/Trading/ManualFills/ManualFillsWindowFactory.cs @@ -17,18 +17,18 @@ using System.Windows.Controls; namespace bts.shell { class ManualFillsWindowFactory : ShellWindowFactory { - const string - _window_type = "ManualFills", - _window_name = "Manual Fills"; + public const string + window_type = "ManualFills", + window_name = "Manual Fills"; readonly IBus _bus; readonly OrdersModel _orders_model; readonly string _thread_id; - public ManualFillsWindowFactory(IShellModule owner, IBus bus, OrdersModel orders_model) : base(owner, _window_name) { + public ManualFillsWindowFactory(IShellModule owner, IBus bus, OrdersModel orders_model) : base(owner, window_name) { _bus = bus; _orders_model = orders_model; - _thread_id = mbts.config.Configuration.get().manual_fills ? _window_type : null; + _thread_id = mbts.config.Configuration.get().manual_fills ? window_type : null; _dispose = bus.subscribe(SparkTags.ShowManualFillsTag).Subscribe(open_window); // async void open_window(ManualFillsSettings state) => @@ -37,7 +37,7 @@ namespace bts.shell { public override Task load(ManualFillsSettings settings) { return Task.FromResult(SparkWindow.create( - _window_type, _window_name, _bus, this, Icons.DataEdit, _thread_id, thread_init)); + window_type, window_name, _bus, this, Icons.DataEdit, _thread_id, thread_init)); // (UIElement, IEnumerable, Func, IDisposable) thread_init() { var vm = ManualFillsVM.create(_bus, _orders_model); @@ -58,7 +58,8 @@ namespace bts.shell { new SparkCommandCommand("Revert Selected", Icons.UndoCircular,vm.revert_selected_fills_cmd) { show_in_toolbar = false }, new SparkCommandCommand("Revert All", Icons.UndoCircular, vm.revert_all_cmd), new SparkCommandCommand("Save All", Icons.Disk, vm.commit_all_fills_cmd), - new SparkCommandCommand("Delete Selected", Icons.Trash, vm.remove_selected_fills_cmd) + new SparkCommandCommand("Delete Selected", Icons.Trash, vm.remove_selected_fills_cmd), + new SparkCommandCommand("Import Fills", Icons.Import, vm.import_fills_cmd) }; return (view, cmds, get_settings, vm); // diff --git a/src/client/spark/view/Controls/Dialogs/BTSMessageBox.xaml.cs b/src/client/spark/view/Controls/Dialogs/BTSMessageBox.xaml.cs index 1a68edb7d..d6a36101f 100644 --- a/src/client/spark/view/Controls/Dialogs/BTSMessageBox.xaml.cs +++ b/src/client/spark/view/Controls/Dialogs/BTSMessageBox.xaml.cs @@ -39,6 +39,9 @@ namespace mbts.ui { public static bool Confirm(string message, string title) => Show(message, title, BTSMessageBoxButtons.Yes | BTSMessageBoxButtons.No, MessageBoxImage.Question, true, true, true) == BTSMessageBoxButtons.Yes; + public static void Error(string message, string title) => + Show(message, title, BTSMessageBoxButtons.OK, MessageBoxImage.Error, true, true, true); + public static Task ShowAsync(string message, string title) { return ShowAsync(message, title, BTSMessageBoxButtons.OK, MessageBoxImage.None, false, false); } diff --git a/src/client/spark/view/Extensions/Command/FileDialogCommand.cs b/src/client/spark/view/Extensions/Command/FileDialogCommand.cs index e6b4c911b..419fdbed7 100644 --- a/src/client/spark/view/Extensions/Command/FileDialogCommand.cs +++ b/src/client/spark/view/Extensions/Command/FileDialogCommand.cs @@ -7,6 +7,9 @@ using System.Windows.Forms; namespace mbts.ui { public static class FileDialogCommand { + public static IDialogCommand open_file(Action execute) => + new AnonymousFileDialogCommand(_ => _create_open_file_dialog(), (_, file) => execute(file), _ => true); + public static IDialogCommand export_proto( Func get_proto, Func can_execute = null, diff --git a/src/client/spark_core/Collections/Extensions/EnumerableExtensions.cs b/src/client/spark_core/Collections/Extensions/EnumerableExtensions.cs index 2d57fe0d7..3b0c6d69d 100644 --- a/src/client/spark_core/Collections/Extensions/EnumerableExtensions.cs +++ b/src/client/spark_core/Collections/Extensions/EnumerableExtensions.cs @@ -81,6 +81,13 @@ namespace System.Collections.Generic { } } + public static IEnumerable KeyJoin(this IEnumerable outer_keys, IReadOnlyDictionary inner) { + foreach (var oval in outer_keys) { + if (inner.TryGetValue(oval, out var ival)) + yield return ival; + } + } + /// /// Checks if implements or . If so, the resultant Count is returned, otherwise null (will not enumerate through any items). /// diff --git a/src/client/spark_core/utils/ConvertUtils.cs b/src/client/spark_core/utils/ConvertUtils.cs index d8d9b259b..1d22c35b7 100644 --- a/src/client/spark_core/utils/ConvertUtils.cs +++ b/src/client/spark_core/utils/ConvertUtils.cs @@ -29,6 +29,9 @@ public static T try_convert(object obj) => (T)try_convert(typeof(T), obj); + public static T try_convert(object obj, T default_value) => + try_convert(obj, out T result) ? result : default_value; + public static bool try_convert(object obj, out T result) { bool rv = try_convert(typeof(T), obj, out object r_obj); result = (T)r_obj; diff --git a/src/client/spark_core/utils/RowParser.cs b/src/client/spark_core/utils/RowParser.cs new file mode 100644 index 000000000..ed4c0f34a --- /dev/null +++ b/src/client/spark_core/utils/RowParser.cs @@ -0,0 +1,53 @@ +using System; +using System.Collections.Generic; +using System.Linq; + +namespace bts.utils { + static class RowParser { + public delegate void AssignColumnValue(ref T manual_fill, string cell_text); + + public static AssignColumnValue with_null_check(this AssignColumnValue assignment_del) { + return impl; + void impl(ref T target, string cell_text) { + if (!string.IsNullOrEmpty(cell_text)) + assignment_del(ref target, cell_text); + } + } + + public static AssignColumnValue wrap(this AssignColumnValue assignment_del, Func select_inner, AssignColumnValue outer_assignment_del) { + return impl; + void impl(ref TOuter result, string cell_text) { + outer_assignment_del(ref result, cell_text); + var inner = select_inner(result); + assignment_del(ref inner, cell_text); + } + } + + // Assumes that the first row is column headers + public static IEnumerable parse_rows(IEnumerable rows, IReadOnlyDictionary> column_map, char delimiter) where T : new() { + var rows_en = rows.GetEnumerator(); + return rows_en.MoveNext() ? _parse_rows(rows_en.Current, rows_en, column_map, delimiter) : Enumerable.Empty(); + } + + public static IEnumerable parse_rows(string header_line, IEnumerable rows, IReadOnlyDictionary> column_map, char delimiter) where T : new() => + _parse_rows(header_line, rows.GetEnumerator(), column_map, delimiter); + + static IEnumerable _parse_rows(string header_line, IEnumerator rows_en, IReadOnlyDictionary> column_map, char delimiter) where T : new() { + var assignment_dels = header_line + .Split(new[] { delimiter }, StringSplitOptions.RemoveEmptyEntries) + .KeyJoin(column_map) + .ToArray(); + using (rows_en) { + while (rows_en.MoveNext()) { + var cells = rows_en.Current.Split(delimiter); + var result = new T(); + // assumes cells.Length will never exceed columns.Length (this can be changed if necessary) + for (int i = 0; i < cells.Length; i++) { + assignment_dels[i](ref result, cells[i]); + } + yield return result; + } + } + } + } +} \ No newline at end of file From 0057f8f080e0de9c500870221ac162429171e73c Mon Sep 17 00:00:00 2001 From: Jake Norbut Date: Mon, 20 May 2019 12:29:34 -0500 Subject: [PATCH 057/121] when determining average price, position service should use the fill's last_price() and not avg_price; using avg_price from partials leads to wildly inaccurate pnl --- src/server/sparkd/position/PositionServiceServer.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/server/sparkd/position/PositionServiceServer.cpp b/src/server/sparkd/position/PositionServiceServer.cpp index 3684c0fdb..2fb78fd4c 100644 --- a/src/server/sparkd/position/PositionServiceServer.cpp +++ b/src/server/sparkd/position/PositionServiceServer.cpp @@ -91,7 +91,7 @@ const PositionUnit& PositionServiceServer::_on_fill (const Fill& f, if(f.fill_kind()==bts::api::orderman::Fill::FILL_KIND_SOD){ if(f.side()==api::Side::SIDE_BUY){ _avg_price_qty(pu.sod_long_avg_price(),pu.sod_long_pos(), - f.avg_price(),f.last_shares(), + f.last_price(),f.last_shares(), is_add,f.fill_key(), avg_price, tot_qty); pu.set_sod_long_avg_price(avg_price); @@ -99,7 +99,7 @@ const PositionUnit& PositionServiceServer::_on_fill (const Fill& f, } else{ _avg_price_qty(pu.sod_short_avg_price(),pu.sod_short_pos(), - f.avg_price(),f.last_shares(), + f.last_price(),f.last_shares(), is_add,f.fill_key(), avg_price, tot_qty); pu.set_sod_short_avg_price(avg_price); @@ -109,7 +109,7 @@ const PositionUnit& PositionServiceServer::_on_fill (const Fill& f, else{ if(f.side()==api::Side::SIDE_BUY){ _avg_price_qty(pu.day_long_avg_price(),pu.day_long_pos(), - f.avg_price(),f.last_shares(), + f.last_price(),f.last_shares(), is_add,f.fill_key(), avg_price, tot_qty); pu.set_day_long_avg_price(avg_price); @@ -117,7 +117,7 @@ const PositionUnit& PositionServiceServer::_on_fill (const Fill& f, } else{ _avg_price_qty(pu.day_short_avg_price(),pu.day_short_pos(), - f.avg_price(),f.last_shares(), + f.last_price(),f.last_shares(), is_add,f.fill_key(), avg_price, tot_qty); pu.set_day_short_avg_price(avg_price); From 082a241bb9bff07b6ba97063177002d00fae8ca5 Mon Sep 17 00:00:00 2001 From: Jake Norbut Date: Mon, 20 May 2019 12:48:14 -0500 Subject: [PATCH 058/121] passing the OrdersModel, from where the PositionUnit will be published from, down the line --- src/client/spark/Modules/Trading/PAndLWindowFactory.cs | 6 ++++-- src/client/spark/Modules/Trading/TradingShellModule.cs | 2 +- src/client/spark/view/Spark/PAndL/PAndLWindow.cs | 6 ++++-- 3 files changed, 9 insertions(+), 5 deletions(-) diff --git a/src/client/spark/Modules/Trading/PAndLWindowFactory.cs b/src/client/spark/Modules/Trading/PAndLWindowFactory.cs index 486e261a9..a0d936236 100644 --- a/src/client/spark/Modules/Trading/PAndLWindowFactory.cs +++ b/src/client/spark/Modules/Trading/PAndLWindowFactory.cs @@ -20,11 +20,13 @@ namespace bts.shell { internal class PAndLWindowFactory : ShellWindowFactory { readonly IBus _bus; readonly PAndLModel _model; + readonly OrdersModel _orders_model; readonly UserSettingsHub _settings_hub; - public PAndLWindowFactory(IShellModule owner, IBus bus, UserSettingsHub settings_hub, PAndLModel model) : base(owner, "SparkPAndL") { + public PAndLWindowFactory(IShellModule owner, IBus bus, UserSettingsHub settings_hub, PAndLModel model, OrdersModel orders_model) : base(owner, "SparkPAndL") { _bus = bus; _model = model; + _orders_model = orders_model; _settings_hub = settings_hub; _dispose = bus.subscribe(SparkTags.ShowPAndLTag).OnDispatcher().Subscribe(show); } @@ -45,7 +47,7 @@ namespace bts.shell { state.grid, get: us => us.pnl_grid, set: (us, gs) => us.with(provided_pnl_gridsettings: gs)); - return new PAndLWindow(_model, this, _bus, state, SparkCommands.ShowPAndL.icon, settings_manager); + return new PAndLWindow(_model, _orders_model, this, _bus, state, SparkCommands.ShowPAndL.icon, settings_manager); } [ProtoContract] diff --git a/src/client/spark/Modules/Trading/TradingShellModule.cs b/src/client/spark/Modules/Trading/TradingShellModule.cs index b8e4fa1cf..f0ffaffe7 100644 --- a/src/client/spark/Modules/Trading/TradingShellModule.cs +++ b/src/client/spark/Modules/Trading/TradingShellModule.cs @@ -28,7 +28,7 @@ namespace bts.shell { _lwf = new LadderWindowFactory(this, bus, wm, settings_hub, orders_model); _obwf = new OrderBookWindowFactory(this, bus, notification_service, notification_service_data_cache, orders_model, settings_hub); _fwf = new FillsWindowFactory(this, bus, settings_hub, orders_model); - _plwf = new PAndLWindowFactory(this, bus, settings_hub, pandl_model); + _plwf = new PAndLWindowFactory(this, bus, settings_hub, pandl_model, orders_model); _aswf = new AlgoStudioWindowFactory(this, bus, orders_model, error_reporter); _mfwf = new ManualFillsWindowFactory(this, bus, orders_model); } diff --git a/src/client/spark/view/Spark/PAndL/PAndLWindow.cs b/src/client/spark/view/Spark/PAndL/PAndLWindow.cs index e2aa7e95b..e82ebebf6 100644 --- a/src/client/spark/view/Spark/PAndL/PAndLWindow.cs +++ b/src/client/spark/view/Spark/PAndL/PAndLWindow.cs @@ -18,13 +18,15 @@ namespace mbts.spark { readonly IWindowSettingsManager _settings_manager; PAndLWindowFactory.PAndLStartupData _startup_data; PAndLModel _model; + OrdersModel _orders_model; PAndLView _view; PAndLVM _vm; PAndLWindowFactory _factory; IReadOnlyList _commands; - public PAndLWindow(PAndLModel model, PAndLWindowFactory factory, IBus bus, PAndLWindowFactory.PAndLStartupData startup_data, ImageSource Icon, WindowSettingsManager settings_manager) : base("P&L", bus, factory, SparkCommands.ShowPAndL.icon) { + public PAndLWindow(PAndLModel model, OrdersModel orders_model, PAndLWindowFactory factory, IBus bus, PAndLWindowFactory.PAndLStartupData startup_data, ImageSource Icon, WindowSettingsManager settings_manager) : base("P&L", bus, factory, SparkCommands.ShowPAndL.icon) { _model = model; + _orders_model = orders_model; _factory = factory; _startup_data = startup_data; _settings_manager = settings_manager; @@ -52,7 +54,7 @@ namespace mbts.spark { public string thread_id { get; } public void thread_init() { - _vm = new PAndLVM(_bus, _model, _startup_data, _settings_manager); + _vm = new PAndLVM(_bus, _model, _orders_model, _startup_data, _settings_manager); _view = new PAndLView(_vm); _view.DataContext = _vm; _commands = new List { From 2137d37cbd2f4e85fe2e0d8195dbf1429b22cddb Mon Sep 17 00:00:00 2001 From: Jake Norbut Date: Mon, 20 May 2019 12:51:24 -0500 Subject: [PATCH 059/121] adding SecurityImmutable? to on_positions and updating security_positions_demuxer for additional argument type --- src/client/spark/Orders/OrdersModel.cs | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/client/spark/Orders/OrdersModel.cs b/src/client/spark/Orders/OrdersModel.cs index 76a3c7241..eb4669156 100644 --- a/src/client/spark/Orders/OrdersModel.cs +++ b/src/client/spark/Orders/OrdersModel.cs @@ -38,7 +38,7 @@ namespace mbts.spark { readonly OrderActivitySummarizer _activity; readonly TableUpdateDemuxer _security_orders_demuxer; readonly TableUpdateDemuxer _security_working_orders_with_piq_demuxer; - readonly TableUpdateDemuxer _security_positions_demuxer; + readonly TableUpdateDemuxer<(SecurityImmutable?, PositionUnitImmutable), int, uint> _security_positions_demuxer; IDisposable _dispose; static readonly bool _piq_enabled = Configuration.get().enabled; @@ -67,7 +67,7 @@ namespace mbts.spark { .Table() .Cache(); - on_positions = _position_service.subscribei_position_data().Naggle(interval, scheduler).PermissionFilter(admin_model.on_admin_data).Table().Cache(); + on_positions = _position_service.subscribei_position_data().Naggle(interval, scheduler).PermissionFilter(admin_model.on_admin_data).join(sstore, pos => pos.SecurityId).Table().Cache(); working_orders = on_orders.Scan( ImmutableDictionary.Empty, @@ -83,7 +83,7 @@ namespace mbts.spark { _activity = new OrderActivitySummarizer(bus, on_orders); _security_orders_demuxer = new TableUpdateDemuxer(o => o.Key, o => o.security_key()); _security_working_orders_with_piq_demuxer = new TableUpdateDemuxer(o => o.order.Key, o => o.order.security_key()); - _security_positions_demuxer = new TableUpdateDemuxer(o => o.RecordId, p => p.SecurityId); + _security_positions_demuxer = new TableUpdateDemuxer<(SecurityImmutable?, PositionUnitImmutable pos), int, uint>(o => o.pos.RecordId, p => p.pos.SecurityId); studio_algos_with_meta = algo_model.definitions.LeftJoin( algo_model.metadata_service.subscribe_metadata().Table(m => m.id), d => d.key, @@ -200,7 +200,7 @@ namespace mbts.spark { public IObservable> on_order_securities { get; } public IObservable> on_fill_tuples { get; } public IObservable> working_orders { get; } - public IObservable> on_positions { get; } + public IObservable> on_positions { get; } public IObservable> studio_algos_with_meta { get; } public IObservable> on_manual_fills_securities { get; } public IObservable> on_manual_fill_tuples { get; } @@ -214,7 +214,7 @@ namespace mbts.spark { public IObservable> on_security_orders(uint security) => _security_orders_demuxer.subscribe(security); - public IObservable> on_security_positions(uint security) => + public IObservable> on_security_positions(uint security) => _security_positions_demuxer.subscribe(security); #endregion @@ -418,10 +418,10 @@ namespace mbts.spark { public static IObservable security_total_position(this OrdersModel orders_model, uint security_key, string account) => orders_model.on_security_positions(security_key) - .Select(t => { + .Select((t,k) => { foreach (var kv in t.state) { - if (kv.Value.Account == account) - return kv.Value.total_pos(); + if (kv.Value.Item2.Account == account) + return kv.Value.Item2.total_pos(); } return double.NaN; }).DistinctUntilChanged() From 22f89490a36b21f51b2ce71b9d1d622ff363cb0f Mon Sep 17 00:00:00 2001 From: Jake Norbut Date: Mon, 20 May 2019 12:55:09 -0500 Subject: [PATCH 060/121] adding SecurityMD lookup facility to model; this is ultimately for compatability with the PnLSummaryRow, of which a SecurityMD instance is a component of the row's key tuple --- src/client/spark/view/Spark/PAndL/PAndLModel.cs | 13 +++---------- 1 file changed, 3 insertions(+), 10 deletions(-) diff --git a/src/client/spark/view/Spark/PAndL/PAndLModel.cs b/src/client/spark/view/Spark/PAndL/PAndLModel.cs index a3bb5ec1d..7b0dee8b7 100644 --- a/src/client/spark/view/Spark/PAndL/PAndLModel.cs +++ b/src/client/spark/view/Spark/PAndL/PAndLModel.cs @@ -14,7 +14,7 @@ namespace mbts.spark { IDisposable _dispose; [ImportingConstructor] - private PAndLModel(IBus bus, OrdersModel order_model/*, AccountService account_service*/) { + private PAndLModel(IBus bus, OrdersModel order_model) { _bus = bus; _order_model = order_model; _mdlookup = new MDLookup(bus.subscribe(SparkTags.SecuritiesTag)); @@ -23,15 +23,8 @@ namespace mbts.spark { public IObservable> on_pnl_data => _order_model.on_fill_tuples.Select(ft => new PnLData(ft, _mdlookup.get_md(ft.security?.Id)), pd => pd.fill.FillKey); - //public async Task get_accounts() { - // var request = await _account_service.get_accounts_async(new GetAccountsRequest()); - // if (request.success) { - // var response = request.value; - // return response.Accounts.ToArray(); - // } - // return new Account[] { }; - //} - + public SecurityMD? get_security_md(SecurityImmutable? si) => _mdlookup.get_md(si?.Id); + public void Dispose() => Disposable.dispose(ref _dispose); } From 0f066fdeb93c72dfabfee042709acac92ad23479 Mon Sep 17 00:00:00 2001 From: Jake Norbut Date: Mon, 20 May 2019 12:58:59 -0500 Subject: [PATCH 061/121] extended to supported calculating pnl via PositionsUnits instead of Fills --- .../spark/view/Spark/PAndL/PnLCalculator.cs | 79 +++++++++++++------ 1 file changed, 54 insertions(+), 25 deletions(-) diff --git a/src/client/spark/view/Spark/PAndL/PnLCalculator.cs b/src/client/spark/view/Spark/PAndL/PnLCalculator.cs index 0966c538d..826f14259 100644 --- a/src/client/spark/view/Spark/PAndL/PnLCalculator.cs +++ b/src/client/spark/view/Spark/PAndL/PnLCalculator.cs @@ -1,5 +1,6 @@ using bts.api; using bts.api.orderman; +using bts.api.position; using mbts.co2; using System; using System.Collections.Generic; @@ -82,27 +83,56 @@ namespace mbts.spark { class PnLCalculator { readonly IEnumerable _data; + readonly IEnumerable<(SecurityImmutable?, PositionUnitImmutable)> _position_data; readonly pnl_calc_type _calc_type; - - public PnLCalculator(IEnumerable data, pnl_calc_type calc_type) { + readonly PAndLModel _pnl_model; + + public PnLCalculator(pnl_calc_type calc_type, IEnumerable data) { _data = data; _calc_type = calc_type; } + public PnLCalculator(PAndLModel model, pnl_calc_type calc_type, IEnumerable<(SecurityImmutable?, PositionUnitImmutable)> data) { + _pnl_model = model; + _position_data = data; + _calc_type = calc_type; + } + + public IEnumerable get_summary_position() { - return (from pnl_item in _data - group pnl_item by (pnl_item.account, pnl_item.exchange, pnl_item.symbol, pnl_item.security, pnl_item.sec_md, pnl_item.security_type) into grouping - /* let start_of_day = _get_start_of_day(grouping.Key.symbol) */ - select new PnLSummaryRow(_calc_type, grouping.Key, - grouping.Where(y => y.fill.FillKind != Fill.Types.FillKind.Sod).Sum(y => (uint)y.buy_quantity), - grouping.Where(y => y.fill.FillKind != Fill.Types.FillKind.Sod).Sum(y => (uint)y.sell_quantity), - grouping.Sum(y => (int)(y.buy_quantity - y.sell_quantity)), - grouping.Where(y => y.fill.FillKind == Fill.Types.FillKind.Sod).Sum(y => (int)(y.buy_quantity - y.sell_quantity)), - grouping.weighted_average(y => y.fill.LastPrice, y => y.buy_quantity), - grouping.weighted_average(y => y.fill.LastPrice, y => y.sell_quantity), - grouping.Select(i => i.fill) - ) - ); + if (_data != null) { + return (from pnl_item in _data + group pnl_item by (pnl_item.account, pnl_item.exchange, pnl_item.symbol, pnl_item.security, pnl_item.sec_md, pnl_item.security_type) into grouping + /* let start_of_day = _get_start_of_day(grouping.Key.symbol) */ + select new PnLSummaryRow(_calc_type, grouping.Key, + grouping.Where(y => y.fill.FillKind != Fill.Types.FillKind.Sod).Sum(y => (uint)y.buy_quantity), + grouping.Where(y => y.fill.FillKind != Fill.Types.FillKind.Sod).Sum(y => (uint)y.sell_quantity), + grouping.Sum(y => (int)(y.buy_quantity - y.sell_quantity)), + grouping.Where(y => y.fill.FillKind == Fill.Types.FillKind.Sod).Sum(y => (int)(y.buy_quantity - y.sell_quantity)), + grouping.weighted_average(y => y.fill.LastPrice, y => y.buy_quantity), + grouping.weighted_average(y => y.fill.LastPrice, y => y.sell_quantity), + grouping.Select(i => i.fill), + null + ) + ); + } else { + return _position_data.Select(p => { + var si = p.Item1; + var pos = p.Item2; + var sec_md = _pnl_model.get_security_md(si); + var key = (p.Item2.Account, si?.Exchange, si?.Symbol, si, sec_md, si?.security_type()); + return new PnLSummaryRow(_calc_type, key, + (uint)pos.DayLongPos, + (uint)pos.DayShortPos, + (int)(pos.DayLongPos + pos.SodLongPos - pos.DayShortPos - pos.SodShortPos), + (int)(pos.SodLongPos - pos.SodShortPos), + pos.DayLongAvgPrice, + pos.DayShortAvgPrice, + null, + pos + ); + }); + } } public static bool get_driver(SecurityMD sec_md, pnl_calc_type pnl_request, ref double driver_price) { @@ -202,26 +232,25 @@ namespace mbts.spark { } public static double get_pnl(double bid_for_long, double ask_for_short, FillImmutable fill, double cpp) { - if (fill.Side == Side.Buy && !Double.IsNaN(bid_for_long)) { return _get_long_pnl(bid_for_long, fill.LastPrice, fill.LastShares, cpp); } - if (fill.Side == Side.Sell && !Double.IsNaN(ask_for_short)) { return _get_short_pnl(ask_for_short, fill.LastPrice, fill.LastShares, cpp); } + if (fill.Side == Side.Buy && !Double.IsNaN(bid_for_long)) { return get_long_pnl(bid_for_long, fill.LastPrice, fill.LastShares, cpp); } + if (fill.Side == Side.Sell && !Double.IsNaN(ask_for_short)) { return get_short_pnl(ask_for_short, fill.LastPrice, fill.LastShares, cpp); } return Double.NaN; } public static double get_pnl(double driver, FillImmutable fill, double cpp) { if (!Double.IsNaN(driver)) { - if (fill.Side == Side.Buy) { return _get_long_pnl(driver, fill.LastPrice, fill.LastShares, cpp); } - if (fill.Side == Side.Sell) { return _get_short_pnl(driver, fill.LastPrice, fill.LastShares, cpp); } + if (fill.Side == Side.Buy) { return get_long_pnl(driver, fill.LastPrice, fill.LastShares, cpp); } + if (fill.Side == Side.Sell) { return get_short_pnl(driver, fill.LastPrice, fill.LastShares, cpp); } } return Double.NaN; } - private static double _get_long_pnl(double driver, double price, double quantity, double cpp) { - return (driver - price) * quantity * cpp; - } + public static double get_long_pnl(double driver, double price, double quantity, double cpp) => + quantity == 0 ? 0 : (driver - price) * quantity * cpp; - private static double _get_short_pnl(double driver, double price, double quantity, double cpp) { - return (price - driver) * quantity * cpp; - } + public static double get_short_pnl(double driver, double price, double quantity, double cpp) => + quantity == 0 ? 0 : (price - driver) * quantity * cpp; + } From b3db0fc4cad5307ab81fdbb47bc5ef4215ec54f2 Mon Sep 17 00:00:00 2001 From: Jake Norbut Date: Mon, 20 May 2019 13:00:01 -0500 Subject: [PATCH 062/121] also extended to supported calculating pnl via PositionsUnit data instead of Fill data --- .../spark/view/Spark/PAndL/PnLSummaryRow.cs | 54 +++++++++++++------ 1 file changed, 39 insertions(+), 15 deletions(-) diff --git a/src/client/spark/view/Spark/PAndL/PnLSummaryRow.cs b/src/client/spark/view/Spark/PAndL/PnLSummaryRow.cs index fa9dc2ec8..343ebfeef 100644 --- a/src/client/spark/view/Spark/PAndL/PnLSummaryRow.cs +++ b/src/client/spark/view/Spark/PAndL/PnLSummaryRow.cs @@ -1,5 +1,6 @@ using bts.api; using bts.api.orderman; +using bts.api.position; using mbts.co2; using mbts.ui; using mbts.ui.controls; @@ -20,6 +21,7 @@ namespace mbts.spark { IEnumerable _fills; double _driver; static readonly Dictionary cpp_dict = config.Configuration.get().cpp_entry; + PositionUnitImmutable? _pu; static PnLSummaryRow() { _brushes = new SignedDataStore(mk_brush(Colors.Green), mk_brush(Colors.Black), mk_brush(Colors.Red), mk_brush(Colors.Black)); @@ -29,7 +31,7 @@ namespace mbts.spark { public PnLSummaryRow(pnl_calc_type calc_type, (string, Venue?, string, SecurityImmutable?, SecurityMD?, SecurityType?) key, - uint day_buy_quant, uint day_sell_quant, int net_pos, int sod_pos, double wavg_buy, double wavg_sell, IEnumerable fills) { + uint day_buy_quant, uint day_sell_quant, int net_pos, int sod_pos, double wavg_buy, double wavg_sell, IEnumerable fills, PositionUnitImmutable? pu) { this.key = key; _start_pnl = new PnLDoubleBrushCell(this, nameof(start_pnl)); _day_pnl = new PnLDoubleBrushCell(this, nameof(day_pnl)); @@ -42,6 +44,7 @@ namespace mbts.spark { _weighted_average_buy = new CellProperty(this, nameof(weighted_average_buy), price => this.security.spec_or_default().format(price), wavg_buy); _weighted_average_sell = new CellProperty(this, nameof(weighted_average_sell), price => this.security.spec_or_default().format(price), wavg_sell); _fills = fills; + _pu = pu; // set the initial pnl and pnl_price when there has been a // position update i.e. this constructor @@ -87,15 +90,26 @@ namespace mbts.spark { double summary_pnl = 0.0; double start_pnl_total = 0.0; double day_pnl_total = 0.0; - foreach (var fill in _fills) { - double _tick_pnl = PnLCalculator.get_pnl(bid.price, ask.price, fill, cpp); - summary_pnl += _tick_pnl; - if (fill.FillKind != Fill.Types.FillKind.Sod) { - day_pnl_total += _tick_pnl; - } else { - start_pnl_total += _tick_pnl; + if (_fills != null) { + foreach (var fill in _fills) { + double _tick_pnl = PnLCalculator.get_pnl(bid.price, ask.price, fill, cpp); + summary_pnl += _tick_pnl; + if (fill.FillKind != Fill.Types.FillKind.Sod) { + day_pnl_total += _tick_pnl; + } else { + start_pnl_total += _tick_pnl; + } } + } else if (_pu.HasValue) { + day_pnl_total = + PnLCalculator.get_long_pnl(bid.price, weighted_average_buy, day_long_quantity, cpp) + + PnLCalculator.get_short_pnl(ask.price, weighted_average_sell, day_short_quantity, cpp); + start_pnl_total = + PnLCalculator.get_long_pnl(bid.price, _pu.Value.SodLongAvgPrice, _pu.Value.SodLongPos, cpp) + + PnLCalculator.get_short_pnl(ask.price, _pu.Value.SodShortAvgPrice, _pu.Value.SodShortPos, cpp); + summary_pnl = day_pnl_total + start_pnl_total; } + _total_pnl.on_next_distinct(summary_pnl, ref dirty); _day_pnl.on_next_distinct(day_pnl_total, ref dirty); _start_pnl.on_next_distinct(start_pnl_total, ref dirty); @@ -105,14 +119,24 @@ namespace mbts.spark { double summary_pnl = 0.0; double start_pnl_total = 0.0; double day_pnl_total = 0.0; - foreach (var fill in _fills) { - double _tick_pnl = PnLCalculator.get_pnl(_driver, fill, cpp); - summary_pnl += _tick_pnl; - if (fill.FillKind != Fill.Types.FillKind.Sod) { - day_pnl_total += _tick_pnl; - } else { - start_pnl_total += _tick_pnl; + if (_fills != null) { + foreach (var fill in _fills) { + double _tick_pnl = PnLCalculator.get_pnl(_driver, fill, cpp); + summary_pnl += _tick_pnl; + if (fill.FillKind != Fill.Types.FillKind.Sod) { + day_pnl_total += _tick_pnl; + } else { + start_pnl_total += _tick_pnl; + } } + } else if (_pu.HasValue) { + day_pnl_total = + PnLCalculator.get_long_pnl(_driver, weighted_average_buy, day_long_quantity, cpp) + + PnLCalculator.get_short_pnl(_driver, weighted_average_sell, day_short_quantity, cpp); + start_pnl_total = + PnLCalculator.get_long_pnl(_driver, _pu.Value.SodLongAvgPrice, _pu.Value.SodLongPos, cpp) + + PnLCalculator.get_short_pnl(_driver, _pu.Value.SodShortAvgPrice, _pu.Value.SodShortPos, cpp); + summary_pnl = day_pnl_total + start_pnl_total; } _total_pnl.on_next_distinct(summary_pnl, ref dirty); _day_pnl.on_next_distinct(day_pnl_total, ref dirty); From a3a33ffb6a90cf27f544ac36c787b9f1ec177ab7 Mon Sep 17 00:00:00 2001 From: Jake Norbut Date: Mon, 20 May 2019 13:13:48 -0500 Subject: [PATCH 063/121] vm now consumes data via position service --- src/client/spark/view/Spark/PAndL/PAndLVM.cs | 59 +++++++++++++++----- 1 file changed, 45 insertions(+), 14 deletions(-) diff --git a/src/client/spark/view/Spark/PAndL/PAndLVM.cs b/src/client/spark/view/Spark/PAndL/PAndLVM.cs index a694a41e1..38473280a 100644 --- a/src/client/spark/view/Spark/PAndL/PAndLVM.cs +++ b/src/client/spark/view/Spark/PAndL/PAndLVM.cs @@ -20,10 +20,12 @@ using mbts.ui.settings; namespace mbts.spark { class PAndLVM : DisposableViewModelBase { + bool _use_position_service = true; - public PAndLVM(IBus bus, PAndLModel model, PAndLStartupData startup_data, IWindowSettingsManager settings_manager) { + public PAndLVM(IBus bus, PAndLModel model, OrdersModel orders_model, PAndLStartupData startup_data, IWindowSettingsManager settings_manager) { _bus = bus; _model = model; + _orders_model = orders_model; _settings_manager = settings_manager; _selected_pnl_calc_type = backing(nameof(selected_pnl_calc_type)); // get initial value from settings @@ -36,21 +38,49 @@ namespace mbts.spark { // since we'll need to create a new PnLCalculator when this changes. (infrequent, if ever) _selected_pnl_calc_type.Value = (startup_data != null && startup_data.calc_type != pnl_calc_type.none) ? startup_data.calc_type : pnl_calc_type.midpoint; _selected_pnl_calc_type.Changed += pnl_calc_type_changed; - var pnl = model.on_pnl_data - .Sample(_POSITION_UPDATE_PERIOD) - .OnDispatcher() - .Subscribe(p => { - if (p != null) { - PnLCalculator calc = new PnLCalculator(p.state.Values, _selected_pnl_calc_type.Value); - var summary_position_rows = calc.get_summary_position(); + IDisposable pnl = null; + + // Circa 2019-05-20, instead of calculating based off our on_pnl_data (i.e. on_fill_tuples), we are instead + // migrating to use consume data published by the position service. Leaving this branch in place + // in case we need to perform additional troubleshooting when this rolls out, but *not* adding a config + // toggle per Taha. + // Once the PnL-via-position service is confirmed good, we can refactor much of how the underlying pnl data + // is structured and calculated. + if (!_use_position_service) { + pnl = model.on_pnl_data + .Sample(_POSITION_UPDATE_PERIOD) + .OnDispatcher() + .Subscribe(p => + { + if (p != null) { + PnLCalculator calc = new PnLCalculator(_selected_pnl_calc_type.Value, p.state.Values); + var summary_position_rows = calc.get_summary_position(); // remove rows for which there is no net pos or no day pos summary_position_rows = summary_position_rows.Where(r => - !(r.start_net_position == 0 && r.day_long_quantity == 0 && r.day_short_quantity == 0) - ); - _pnl_data.sync_with(summary_position_rows); - _last_pnl_update_time = DateTime.Now; - } - }); + !(r.start_net_position == 0 && r.day_long_quantity == 0 && r.day_short_quantity == 0) + ); + _pnl_data.sync_with(summary_position_rows); + _last_pnl_update_time = DateTime.Now; + } + }); + } else { + pnl = _orders_model.on_positions + .Sample(_POSITION_UPDATE_PERIOD) + .OnDispatcher() + .Subscribe(p => { + if (p != null) { + PnLCalculator calc = new PnLCalculator(model, _selected_pnl_calc_type.Value, p.state.Values); + var summary_position_rows = calc.get_summary_position(); + // remove rows for which there is no net pos or no day pos + summary_position_rows = summary_position_rows.Where(r => + !(r.start_net_position == 0 && r.day_long_quantity == 0 && r.day_short_quantity == 0) + ); + _pnl_data.sync_with(summary_position_rows); + _last_pnl_update_time = DateTime.Now; + + } + }); + } pnl_view = CollectionViewSource.GetDefaultView(_pnl_data); // restore filter state @@ -284,6 +314,7 @@ namespace mbts.spark { readonly IBus _bus; readonly PAndLModel _model; + readonly OrdersModel _orders_model; IWindowSettingsManager _settings_manager; static readonly TimeSpan _UPDATE_PERIOD = TimeSpan.FromSeconds(1); static readonly TimeSpan _POSITION_UPDATE_PERIOD = TimeSpan.FromMilliseconds(333); From a7f4944bd6a785fc5a3a909ac166d25192ebf487 Mon Sep 17 00:00:00 2001 From: John Colvin Date: Mon, 20 May 2019 12:14:49 -0500 Subject: [PATCH 064/121] FillsVM - more functional implementation of row filters/sorting --- .../spark/Modules/Trading/Fills/FillsVM.cs | 33 +++++++++----- src/client/spark/Orders/FillTuple.cs | 1 + .../DIYGrid/Utils/DIYGridRowsManager.cs | 12 +++-- src/client/spark/view/Utility/RXCollection.cs | 45 +++++++++++++------ .../Collections/Extensions/ListExtensions.cs | 35 ++++++++++++--- 5 files changed, 91 insertions(+), 35 deletions(-) diff --git a/src/client/spark/Modules/Trading/Fills/FillsVM.cs b/src/client/spark/Modules/Trading/Fills/FillsVM.cs index d3a47734d..ce4ed2b72 100644 --- a/src/client/spark/Modules/Trading/Fills/FillsVM.cs +++ b/src/client/spark/Modules/Trading/Fills/FillsVM.cs @@ -34,7 +34,6 @@ using OrderType = bts.api.orderman.CustomOrderDefinition.CustomOneofCase; namespace mbts.spark { class FillsVM : DisposableViewModelBase, IHandleGridSorting { readonly OrdersModel _model; - readonly OperableCollection _rows; readonly DIYGridSortManager _sort_manager; readonly IDIYGridFiltersManager _filters_manager; readonly ObservableKeyedCollection _columns = new ObservableKeyedCollection(_col_key); @@ -87,7 +86,7 @@ namespace mbts.spark { add_col(Columns.OrderKey, DefaultColumnHeaders.OrderKey, r => r.order_key.ToString(), new RegexFilterVM(r => r.fill.OrderKey.ToString()), DataGridFilterKnownTemplates.Regex, DefaultColumnWidths.OrderKey); add_col(Columns.SecurityKey, DefaultColumnHeaders.SecurityKey, r => r.security_key.ToString(), new RegexFilterVM(r => r.fill.SecurityKey.ToString()), DataGridFilterKnownTemplates.Regex, DefaultColumnWidths.SecurityKey); /// Rows - _rows = new OperableCollection( + rows = new OperableCollection( (ft, fk) => new Row(ft, model), ft => ft.fill.FillKey, new ComparerBuilder(new[] { @@ -97,18 +96,28 @@ namespace mbts.spark { // row sort/filter managers _sort_manager = new DIYGridSortManager(col_tups.WrapSelector(t => (t.col, t.key)), Columns.FillKey); _filters_manager = DIYGridFiltersManager.create(col_tups); - var rows_manager = new DIYGridRowsManager(_filters_manager, _sort_manager, _get_column_comparer, f => _rows.filter = f, c => _rows.comparer = c, _rows.reset, FillsWindow.window_type_name); + var rows_manager = new DIYGridRowsManager(_filters_manager, _sort_manager, _get_column_comparer, FillsWindow.window_type_name); // grid settings grid_settings_manager = new SimpleGridSettingsManager(columns, new Dictionary { }, rows); // this will initialize the immutable properties that correspond to the col_settings_flags above (e.g. Bid/Ask font_color_override, etc.) if (_settings_manager != null) grid_settings_manager.load_from(_settings_manager.current_settings()); - // + var filter_src = Observable.FromEvent>, Func>(cb => rows_manager.filter_changed += cb, cb => rows_manager.filter_changed -= cb); + var comp_src = Observable.FromEvent>, IComparer>(cb => rows_manager.comparer_changed += cb, cb => rows_manager.comparer_changed -= cb); + TableUpdate last_unfiltered_fills = TableUpdate.mk_empty(FillTuple.key_fn); + var fills_buffer = Observable.Create>(o => { + // this is what .Cache() does, but is a little cheaper, since we know subscriptions and callbacks occur on the same thread (i.e., on this Dispatcher). + o.OnNext(last_unfiltered_fills.as_snapshot()); + _unfiltered_fills_changed += o.OnNext; + return Disposable.Create(() => _unfiltered_fills_changed -= o.OnNext); + }); _dispose = new CompositeDisposable( ObservableAutoWrapper.connect(col_tups, columns, ct => ct.col), + RXCollection.connect(rows, fills_buffer.Filter(filter_src, FillTuple.key_fn), comp_src, ft => new Row(ft, model), r => r.fill_tuple), fills_source.OnDispatcher().Subscribe(t => { - _rows.update(t); + last_unfiltered_fills = t; Set(ref _total_row_count, t.state.Count, nameof(total_row_count)); + _unfiltered_fills_changed?.Invoke(t); }), state_obs.OnDispatcher().Subscribe(s => { if (s.grid != null && _settings_manager != null) { @@ -141,8 +150,11 @@ namespace mbts.spark { IDataGridColumnPersistentFilter dv_filter_rx(IObservable> items, Func get_row_val) => DiscreteValuesFilterVM.Create(items, get_row_val); } + // this event is used to forward updates from the main data to the filtered data that drives the rows, after resetting the total row count. + event Action> _unfiltered_fills_changed; + public ObservableCollection columns { get; } = new ObservableCollection(); - public ObservableCollection rows => _rows; + public ObservableCollection rows { get; } = new ObservableCollection(); public int total_row_count => _total_row_count; public SimpleGridSettingsManager grid_settings_manager { get; } @@ -212,8 +224,9 @@ namespace mbts.spark { } public class Row : SecurityRow, IEquatable, IRowVM { + public readonly FillTuple fill_tuple; + readonly OrdersModel _model; - readonly FillTuple _fill_tuple; readonly FillImmutable _fill; readonly OrderRecordImmutable _order; Brush _badge_fill; @@ -221,7 +234,7 @@ namespace mbts.spark { public Row(FillTuple fill_tuple, OrdersModel model) : base(fill_tuple.security ?? SecurityDataHelper.Defaults.SecurityImmutable) { - _fill_tuple = fill_tuple; + this.fill_tuple = fill_tuple; _fill = fill_tuple.fill; _order = fill_tuple.order ?? OrderDataHelper.Defaults.OrderRecordImmutable; _model = model; @@ -231,9 +244,9 @@ namespace mbts.spark { public DateTime fill_time => _fill.Timestamp.to_local_time(); public TimeInForce time_in_force => _order.time_in_force(); public OrderType order_type => _order.order_type(); - public string display_name => OrderUtils.display_name(_order, _fill_tuple.algo); + public string display_name => OrderUtils.display_name(_order, fill_tuple.algo); public OrderMetadata order_metadata => _order_metadata ?? (_order_metadata = _model.factory.get_metadata(_order.Def.CustomDef.CustomCase)); - public Brush badge_fill => _badge_fill ?? (_badge_fill = OrderUtils.badge_fill(_fill_tuple.algo_meta, order_metadata)); + public Brush badge_fill => _badge_fill ?? (_badge_fill = OrderUtils.badge_fill(fill_tuple.algo_meta, order_metadata)); public string account { get; private set; } public string user => _order.user(); public Venue exchange => security.Exchange; diff --git a/src/client/spark/Orders/FillTuple.cs b/src/client/spark/Orders/FillTuple.cs index 7796eb586..77b0e846e 100644 --- a/src/client/spark/Orders/FillTuple.cs +++ b/src/client/spark/Orders/FillTuple.cs @@ -7,6 +7,7 @@ using static bts.api.orderman.Fill.Types; namespace mbts.spark { class FillTuple : IEquatable { + public static readonly Func key_fn = ft => ft.fill.FillKey; public readonly FillImmutable fill; public readonly OrderRecordImmutable? order; public readonly SecurityImmutable? security; diff --git a/src/client/spark/view/Controls/DIYGrid/Utils/DIYGridRowsManager.cs b/src/client/spark/view/Controls/DIYGrid/Utils/DIYGridRowsManager.cs index 0d1c3cc87..268a6b61a 100644 --- a/src/client/spark/view/Controls/DIYGrid/Utils/DIYGridRowsManager.cs +++ b/src/client/spark/view/Controls/DIYGrid/Utils/DIYGridRowsManager.cs @@ -13,7 +13,7 @@ namespace mbts.ui.controls { readonly DeferrableActionWrapper _update; bool _filters_dirty, _sorting_dirty; - public DIYGridRowsManager(IDIYGridFiltersManager filters_manager, DIYGridSortManager sort_manager, Func> get_comparer_for_column, Action> set_filter, Action> set_sorting, Action, Func> reset, string window_name) { + public DIYGridRowsManager(IDIYGridFiltersManager filters_manager, DIYGridSortManager sort_manager, Func> get_comparer_for_column, string window_name) { _filters = filters_manager; _sorting = sort_manager; _update = new DeferrableActionWrapper(() => { @@ -22,16 +22,17 @@ namespace mbts.ui.controls { var comp = build_comparer(out string comp_desc); var filter = build_filter(out string filter_desc); Log.Ui.info($"{window_name} begin filter '{filter_desc}' and sort '{comp_desc}.'"); - reset(comp, filter); + filter_changed?.Invoke(filter); + comparer_changed?.Invoke(comp); } else { var filter = build_filter(out string filter_desc); Log.Ui.info($"{window_name} begin filter '{filter_desc}.'"); - set_filter(filter); + filter_changed?.Invoke(filter); } } else if (_sorting_dirty) { var comp = build_comparer(out string comp_desc); Log.Ui.info($"{window_name} begin sort '{comp_desc}.'"); - set_sorting(comp); + comparer_changed?.Invoke(comp); } _sorting_dirty = false; _filters_dirty = false; @@ -66,6 +67,9 @@ namespace mbts.ui.controls { } } + public event Action> filter_changed; + public event Action> comparer_changed; + public void on_settings(TFiltersState filters, IEnumerable sorting) { using (_update.defer_execution()) { _filters.load_from(filters); diff --git a/src/client/spark/view/Utility/RXCollection.cs b/src/client/spark/view/Utility/RXCollection.cs index 177fd4a13..15a187b9f 100644 --- a/src/client/spark/view/Utility/RXCollection.cs +++ b/src/client/spark/view/Utility/RXCollection.cs @@ -19,22 +19,39 @@ namespace mbts.ui.utility { } static class RXCollection { - // Note: comparer must be 100% deterministic (i.e., if a comprison returns 0, items must be equal) - public static IDisposable connect(ObservableCollection target, IObservable> source, IComparer comparer) where TKey : IEquatable => - source.Subscribe(table => { - foreach (var v in table.removed.Concat(table.changes.Old())) { - int i = CollectionHelper.binary_search(target, v, comparer); - if (i < 0) - Log.assert(false, TableUpdateMessges.sorted_target_error("Comparer failed to find value by binary search (comparer should be 100% deterministic, i.e., equality should be assumed if comparison returns 0)")); - target.RemoveAt(i); + public static IDisposable connect(IList target, + IObservable> table_update_source, + IObservable> comparer_source, + Func view_factory, + Func get_model) where TKey : IEquatable => + connect(target.WrapSelector(get_model, view_factory), table_update_source, comparer_source); + + public static IDisposable connect(IList target, + IObservable> table_update_source, + IObservable> comparer_source) where TKey : IEquatable { + // + IComparer comp = null; + TableUpdate table = null; + return Disposable.Create( + comparer_source.Subscribe(on_comp), + table_update_source.Subscribe(on_table)); + // on comparer + void on_comp(IComparer new_comp) { + if (table != null) { + target.Clear(); + target.InsertSorted(table.state.Values, comp); } - foreach (var v in table.added.Concat(table.changed)) { - int i = CollectionHelper.binary_search(target, v, comparer); - if (i > 0) - Log.assert(false, TableUpdateMessges.sorted_target_error("Binary search for a new value (to be added) yielded a non-negative index (comparer should be 100% deterministic, i.e., equality should be assumed if comparison returns 0)")); - target.Insert(i, v); + comp = new_comp; + } + // on table update + void on_table(TableUpdate new_table) { + if (comp != null) { + table = table == null ? new_table.as_snapshot() : new_table; + target.RemoveSorted(table.removed.Concat(table.changes.Old()), comp); + target.InsertSorted(table.added.Concat(table.changed), comp); } - }); + } + } // Note: comparer must be 100% deterministic (i.e., if a comprison returns 0, items must be equal) public static IObserver> sort_target_observer(IList target, Func convert, Func convert_back, IComparer comparer) where TKey : IEquatable { diff --git a/src/client/spark_core/Collections/Extensions/ListExtensions.cs b/src/client/spark_core/Collections/Extensions/ListExtensions.cs index 814548a71..72c04560a 100644 --- a/src/client/spark_core/Collections/Extensions/ListExtensions.cs +++ b/src/client/spark_core/Collections/Extensions/ListExtensions.cs @@ -1,4 +1,5 @@ -using System; +using bts.utils; +using System; using System.Collections; using System.Collections.Generic; using System.Linq; @@ -8,13 +9,17 @@ namespace bts.collections { public static void InsertSorted(this IList list, T item) => list.InsertSorted(item, Comparer.Default); + public static void InsertSorted(this IList list, IEnumerable items) => + InsertSorted(list, items, Comparer.Default); + + public static void InsertSorted(this IList list, IEnumerable items, IComparer comparer) { + foreach (var item in items) + list.InsertSorted(item, comparer); + } + public static void InsertSorted(this IList list, T item, IComparer comparer) { - for (int i = 0; i < list.Count; i++) - if (comparer.Compare(list[i], item) > 0) { - list.Insert(i, item); - return; - } - list.Add(item); + int i = CollectionHelper.binary_search(list, item, comparer); + list.Insert(i < 0 ? ~i : i, item); } public static void InsertSorted(this List list, T value, IComparer comparer) { @@ -22,6 +27,22 @@ namespace bts.collections { list.Insert(i < 0 ? ~i : i, value); } + public static bool RemoveSorted(this IList list, T item, IComparer comparer) { + int i = CollectionHelper.binary_search(list, item, comparer); + if (i < 0) + return false; + list.RemoveAt(i); + return true; + } + + public static void RemoveSorted(this IList list, IEnumerable items, IComparer comparer) { + foreach (var item in items) { + int i = CollectionHelper.binary_search(list, item, comparer); + if (i >= 0) + list.RemoveAt(i); + } + } + public static T Pop(this IList list, int index) { T rv = list.ElementAt(index); list.RemoveAt(index); From 2c547f37fb275cf38d3f546442ef3d5582210786 Mon Sep 17 00:00:00 2001 From: John Colvin Date: Mon, 20 May 2019 12:30:18 -0500 Subject: [PATCH 065/121] ManualFillsVM - use ObservableAggregateCollection to merge persisted/placeholder rows together --- .../Trading/ManualFills/ManualFillsModel.cs | 71 ++---------- .../Trading/ManualFills/ManualFillsVM.cs | 107 ++++++++---------- src/client/spark/view/Utility/RXCollection.cs | 26 ++--- 3 files changed, 66 insertions(+), 138 deletions(-) diff --git a/src/client/spark/Modules/Trading/ManualFills/ManualFillsModel.cs b/src/client/spark/Modules/Trading/ManualFills/ManualFillsModel.cs index 3d2a78959..0215bfbc3 100644 --- a/src/client/spark/Modules/Trading/ManualFills/ManualFillsModel.cs +++ b/src/client/spark/Modules/Trading/ManualFills/ManualFillsModel.cs @@ -1,8 +1,6 @@ using bts.api.core; using bts.api.position; using bts.collections; -using bts.utils; -using mbts; using mbts.spark; using System; using System.Collections.Generic; @@ -78,83 +76,32 @@ namespace bts.shell.manual_fills { class ManualFillsModel { readonly OrdersModel _model; - TableUpdate _placeholder_table = TableUpdate.mk_empty(ManualFillData.key_fn); public ManualFillsModel(OrdersModel model) : this(model, model.on_manual_fill_tuples, - model.factory.subscribe_account_tuples(), model.factory.subscribe_accounts().Select(d => MappedDictionary.create(d, kv => new AccountTuple(kv.Key, kv.Value))).Table(u => u.id), - model.factory.subscribe_user_tuples(), model.factory.subscribe_users().Select(d => MappedDictionary.create(d, kv => new UserTuple(kv.Key, kv.Value))).Table(a => a.id)) { } - public ManualFillsModel(OrdersModel model, IObservable> manual_fills, - IObservable> accounts, IObservable> account_table, - IObservable> users, IObservable> user_table) { + public ManualFillsModel(OrdersModel model, IObservable> manual_fills, IObservable> accounts, IObservable> users) { _model = model; - data = manual_fills.SelectKeyValue( + this.manual_fills = manual_fills.SelectKeyValue( mft => new ManualFillData(new ManualFillKey(mft.manual_fill.RecordId), mft), - ManualFillData.key_fn - ).MergeTable(Observable.Create>(o => { - o.OnNext(_placeholder_table.as_snapshot()); - _placeholder_table_changed += o.OnNext; - return Disposable.Create(() => _placeholder_table_changed -= o.OnNext); - })); + ManualFillData.key_fn); this.accounts = accounts; - this.account_table = account_table; this.users = users; - this.user_table = user_table; } - event Action> _placeholder_table_changed; + public IObservable> manual_fills { get; } + public IObservable> accounts { get; } + public IObservable> users { get; } - public IObservable> data { get; } - public IObservable> accounts { get; } - public IObservable> account_table { get; } - public IObservable> users { get; } - public IObservable> user_table { get; } + public async Task delete_fills(IEnumerable fills) => + await _model.delete_manual_fills(fills); - public async Task delete_rows(IEnumerable rows) { - var pl_rows = new List(); - var persisted_fills = new List(); - foreach (var r in rows) { - if (r.placeholder_key.HasValue) - pl_rows.Add(r.data); - else persisted_fills.Add(r.data.value.manual_fill.CloneToMutable()); - } - if (pl_rows.Count > 0) { - _placeholder_table = _placeholder_table.with_removed(pl_rows); - _placeholder_table_changed?.Invoke(_placeholder_table); - } - if (persisted_fills.Count > 0) { - await _model.delete_manual_fills(persisted_fills); - } - } - - public async Task create_fills(IEnumerable fills) => + public async Task create_fills(IEnumerable fills) => await _model.create_manual_fills(fills); public async Task update_fills(IEnumerable fills) => await _model.update_manual_fills(fills); - - public void add_new_placeholder(ManualFillTuple value) => - add_new_placeholder(new[] { value }); - - public void add_new_placeholder(IEnumerable values) { - _placeholder_table = _placeholder_table.with_added(values.Select(v => new ManualFillData(new ManualFillKey(Guid.NewGuid()), v)).ToList()); - if (_placeholder_table.has_changes()) - _placeholder_table_changed?.Invoke(_placeholder_table); - } - - public void remove_placeholder(IEnumerable keys) { - var removes = new List(); - foreach (var k in keys.Select(k => new ManualFillKey(k))) { - if (_placeholder_table.state.TryGetValue(k, out var mfd)) - removes.Add(mfd); - } - if (removes.Count > 0) { - _placeholder_table = _placeholder_table.with_removed(removes); - _placeholder_table_changed?.Invoke(_placeholder_table); - } - } } } \ No newline at end of file diff --git a/src/client/spark/Modules/Trading/ManualFills/ManualFillsVM.cs b/src/client/spark/Modules/Trading/ManualFills/ManualFillsVM.cs index e422b5c62..dad122d97 100644 --- a/src/client/spark/Modules/Trading/ManualFills/ManualFillsVM.cs +++ b/src/client/spark/Modules/Trading/ManualFills/ManualFillsVM.cs @@ -3,9 +3,9 @@ using bts.api.core; using bts.api.position; using bts.collections; using mbts; -using mbts.collections; using mbts.spark; using mbts.ui; +using mbts.ui.controls; using mbts.ui.shell; using mbts.ui.utility; using mbts.utils; @@ -16,7 +16,6 @@ using System.Collections.Specialized; using System.ComponentModel; using System.IO; using System.Linq; -using System.Reactive.Disposables; using System.Reactive.Linq; using System.Threading.Tasks; using System.Windows.Data; @@ -33,10 +32,12 @@ namespace bts.shell.manual_fills { class ManualFillsVM : DisposableViewModelBase { const int _max_unconfirmed_removes = 16; + static readonly IComparer _row_key_comparer = Comparer.Create((a, b) => a.key.CompareTo(b.key)); readonly IBus _bus; readonly ManualFillsModel _model; - readonly OperableCollection _rows; - readonly ObservableCollection _placeholder_rows = new ObservableCollection(); + readonly ObservableCollection + _persisted_rows = new ObservableCollection(), + _placeholder_rows = new ObservableCollection(); IDisposable _import_fills_hooks; public static ManualFillsVM create(IBus bus, OrdersModel model) => @@ -45,53 +46,34 @@ namespace bts.shell.manual_fills { public ManualFillsVM(IBus bus, ManualFillsModel model) { _bus = bus; _model = model; - _rows = new OperableCollection( - (mft, _) => new ManualFillRow(mft), - ManualFillData.key_fn, - Comparer.Create((a, b) => a.key.CompareTo(b.key)) - ); - rows_view = CollectionViewSource.GetDefaultView(_rows); + rows_view = CollectionViewSource.GetDefaultView(new ObservableAggregateCollection(_persisted_rows, _placeholder_rows)); // mons var dirty_mon = new ReactiveThresholdMonitor(r => r.subscribe_is_dirty()); var selection_mon = new ReactiveThresholdMonitor(r => r.subscribe_is_selected()); var placeholder_mon = new ReactiveThresholdMonitor(); // commands - commit_all_fills_cmd = ObservantAsyncCommand.create(() => _commit_async(_rows), out var commit_all_fils_observer); + commit_all_fills_cmd = ObservantAsyncCommand.create(() => _commit_async(_persisted_rows), out var commit_all_fils_observer); commit_selected_fills_cmd = ObservantAsyncCommand.create(() => _commit_async(selection_mon.filtered_items), out var commit_selected_fills_observer); //commit_selected_fills_cmd = ObservantCommand.create - revert_all_cmd = ObservantCommand.create(() => { - foreach (var r in _rows) { - if (r.is_dirty) - r.revert(); - } - }, out var revert_all_observer); - revert_selected_fills_cmd = ObservantCommand.create(() => { - foreach (var r in selection_mon.filtered_items) { - // check is_selected as a fail safe, even though this can be assumed - if (r.is_selected) - r.revert(); - } - }, out var revert_selected_fills_observer); - remove_selected_fills_cmd = ObservantCommand.create(() => { - if (selection_mon.filtered_items.Count < _max_unconfirmed_removes || BTSMessageBox.Confirm($"Are you sure you want to delete {selection_mon.filtered_items.Count} fills?", "Delete Fills")) - _model.delete_rows(selection_mon.filtered_items).ignore(); - }, out var remove_selected_fills_observer); + revert_all_cmd = ObservantCommand.create(() => revert(dirty_mon.filtered_items.ToList()), out var revert_all_observer); + revert_selected_fills_cmd = ObservantCommand.create(() => revert(selection_mon.filtered_items.ToList()), out var revert_selected_fills_observer); + remove_selected_fills_cmd = ObservantAsyncCommand.create(remove_selected_fills_async, out var remove_selected_fills_observer); add_fill_cmd = new DelegateCommand(_add_fill); import_fills_cmd = FileDialogCommand.open_file(_import_fills); // var dirty_or_new_src = dirty_mon.CombineLatest(placeholder_mon, LogicalDelegates.Or); _dispose = Disposable.Create( - _model.data.OnDispatcher().Subscribe(_rows.update), + RXCollection.connect(_persisted_rows, + _model.manual_fills.OnDispatcher(), + Observable.Return(Comparer.Create((a, b) => a.key.CompareTo(b.key))), + mfd => new ManualFillRow(mfd), + mfr => mfr.data), _bus.subscribe(SparkTags.SecuritiesTag).OnDispatcher().Subscribe(m => { security_map = m; RaisePropertyChanged(nameof(security_map)); }), - ObservableAutoWrapper.connect_item_hooks(_rows, r => { - var hooks = new CompositeDisposable(dirty_mon.join(r), selection_mon.join(r)); - if (r.placeholder_key.HasValue) - hooks.Add(placeholder_mon.join(r, Observable.Return(true))); - return hooks; - }), + ObservableAutoWrapper.connect_item_hooks(_persisted_rows, r => Disposable.Create(dirty_mon.join(r), selection_mon.join(r))), + ObservableAutoWrapper.connect_item_hooks(_placeholder_rows, r => Disposable.Create(selection_mon.join(r), placeholder_mon.join(r, Observable.Return(true)))), // command param subs dirty_mon.Subscribe(revert_all_observer), dirty_or_new_src.OnDispatcher().Subscribe(commit_all_fils_observer), @@ -99,10 +81,26 @@ namespace bts.shell.manual_fills { selection_mon.CombineLatest(dirty_mon, LogicalDelegates.And).OnDispatcher().Subscribe(revert_selected_fills_observer), selection_mon.CombineLatest(dirty_or_new_src, LogicalDelegates.And).OnDispatcher().Subscribe(commit_selected_fills_observer), // accounts/users subs - model.account_table.OnDispatcher().Subscribe(RXCollection.sort_target_observer(accounts, a => new AccountTuple?(a), a => a.Value, Comparer.Default)), - model.user_table.OnDispatcher().Subscribe(RXCollection.sort_target_observer(users, u => new UserTuple?(u), u => u.Value, Comparer.Default)), + RXCollection.connect(accounts, model.accounts.OnDispatcher(), a => new AccountTuple?(a), a => a.Value, Comparer.Default), + RXCollection.connect(users, model.users.OnDispatcher(), u => new UserTuple?(u), u => u.Value, Comparer.Default), Disposable.Create(() => Disposable.dispose(ref _import_fills_hooks)) ); + // local methods + void revert(IReadOnlyList rows) { + foreach (var r in rows) + r.revert(); + } + async Task remove_selected_fills_async() { + if (selection_mon.filtered_items.Count < _max_unconfirmed_removes || BTSMessageBox.Confirm($"Are you sure you want to delete {selection_mon.filtered_items.Count} fills?", "Delete Fills")) { + var persisted_fills = new List(); + foreach (var r in selection_mon.filtered_items.ToList()) { + if (r.placeholder_key.HasValue) + _placeholder_rows.RemoveSorted(r, _row_key_comparer); + else persisted_fills.Add(r.to_man_fill()); + } + await _model.delete_fills(persisted_fills); + } + } } public ICollectionView rows_view { get; } @@ -121,28 +119,21 @@ namespace bts.shell.manual_fills { public ICommand revert_selected_fills_cmd { get; } void _add_fill() => - _model.add_new_placeholder( - (rows_view.CurrentItem as ManualFillRow)?.to_tuple() ?? new ManualFillTuple( - new ManualFillImmutable(new ManualFill { Side = Side.Buy }), - security_map.Count > 0 ? security_map.Values.First().security : (SecurityImmutable?)null, - accounts.FirstOrDefault(), - users.FirstOrDefault())); + _placeholder_rows.InsertSorted(new ManualFillRow( + new ManualFillData( + new ManualFillKey(Guid.NewGuid()), + new ManualFillTuple(new ManualFillImmutable(new ManualFill()), null, accounts.FirstOrDefault(), users.FirstOrDefault()) + )), + _row_key_comparer); async Task _commit_async(IEnumerable rows) { - var adds = new List<(Guid placeholder_id, ManualFill manual_fill)>(); - var updates = new List(); - foreach (var r in rows) { - if (r.placeholder_key.HasValue) { - adds.Add((r.placeholder_key.Value, r.to_man_fill())); - } else if (r.is_dirty) { - updates.Add(r.to_man_fill()); - } - } + var updates = _persisted_rows.Where(r => r.is_dirty).Select(r => r.to_man_fill()).ToList(); if (updates.Count > 0) await _model.update_fills(updates); - if (adds.Count > 0) { - _model.remove_placeholder(adds.Select(t => t.placeholder_id)); - await _model.create_fills(adds.WrapSelector(t => t.manual_fill, null)); + if (_placeholder_rows.Count > 0) { + var adds = _placeholder_rows.Select(r => r.to_man_fill()).ToList(); + _placeholder_rows.Clear(); + await _model.create_fills(adds); } } @@ -204,13 +195,13 @@ namespace bts.shell.manual_fills { add_imported_fills(); } void add_imported_fills() => - _model.add_new_placeholder(parsed_fills.Select(pmf => new ManualFillTuple(new ManualFillImmutable(pmf.result), + _placeholder_rows.InsertSorted(parsed_fills.Select(pmf => new ManualFillRow(new ManualFillData(new ManualFillKey(Guid.NewGuid()), new ManualFillTuple(new ManualFillImmutable(pmf.result), pmf.result.SecurityKey != 0 && security_map.TryGetValue(pmf.result.SecurityKey, out var smd) ? smd.security : (SecurityImmutable?)null, accounts.FirstOrDefault(a => a?.id == pmf.result.AccountId), - users.FirstOrDefault(a => a?.id == pmf.result.UserId)))); + users.FirstOrDefault(a => a?.id == pmf.result.UserId))))), _row_key_comparer); } catch (Exception ex) { BTSMessageBox.Error($"Error importing fills: {ex}.", ManualFillsWindowFactory.window_name); } } } -} +} \ No newline at end of file diff --git a/src/client/spark/view/Utility/RXCollection.cs b/src/client/spark/view/Utility/RXCollection.cs index 15a187b9f..3fd6e23da 100644 --- a/src/client/spark/view/Utility/RXCollection.cs +++ b/src/client/spark/view/Utility/RXCollection.cs @@ -54,25 +54,15 @@ namespace mbts.ui.utility { } // Note: comparer must be 100% deterministic (i.e., if a comprison returns 0, items must be equal) - public static IObserver> sort_target_observer(IList target, Func convert, Func convert_back, IComparer comparer) where TKey : IEquatable { - var comp_wrapper = comparer.wrap(convert_back); - return new AnonymousObserver>(table => { - foreach (var v in table.removed.Concat(table.changes.Old())) { - var result = convert(v); - int i = CollectionHelper.binary_search(target, result, comp_wrapper); - if (i < 0) - Log.assert(false, TableUpdateMessges.sorted_target_error("Comparer failed to find value by binary search (comparer should be 100% deterministic, i.e., equality is a tautology of a comparison that returns 0)")); - target.RemoveAt(i); - } - foreach (var v in table.added.Concat(table.changed)) { - var result = convert(v); - int i = CollectionHelper.binary_search(target, result, comp_wrapper); - if (i > 0) - Log.assert(false, TableUpdateMessges.sorted_target_error("Binary search for a new value (to be added) yielded a non-negative index (comparer should be 100% deterministic, i.e., equality should be assumed if comparison returns 0)")); - target.Insert(~i, result); - } + public static IDisposable connect(IList target, IObservable> source, Func view_factory, Func get_model, IComparer comparer) where TKey : IEquatable => + connect(target.WrapSelector(get_model, view_factory), source, comparer); + + // Note: comparer must be 100% deterministic (i.e., if a comprison returns 0, items must be equal) + public static IDisposable connect(IList target, IObservable> source, IComparer comparer) where TKey : IEquatable => + source.Subscribe(table => { + target.RemoveSorted(table.removed.Concat(table.changes.Old()), comparer); + target.InsertSorted(table.added.Concat(table.changed), comparer); }); - } public static IObservableCollection from_enumerable(IEnumerable source) => new RXCollectionWrapper(source); From 0f21d9783b35c8cb36457bc728cff3ff3ea2b062 Mon Sep 17 00:00:00 2001 From: John Colvin Date: Mon, 20 May 2019 13:41:14 -0500 Subject: [PATCH 066/121] file extension filter for ManualFills import file dialog --- .../Trading/ManualFills/ManualFillsParser.cs | 2 + .../Trading/ManualFills/ManualFillsVM.cs | 2 +- .../spark/core/Utility/NativeFileHelper.cs | 57 +++++++++++++++++++ .../Extensions/Command/FileDialogCommand.cs | 7 ++- 4 files changed, 65 insertions(+), 3 deletions(-) create mode 100644 src/client/spark/core/Utility/NativeFileHelper.cs diff --git a/src/client/spark/Modules/Trading/ManualFills/ManualFillsParser.cs b/src/client/spark/Modules/Trading/ManualFills/ManualFillsParser.cs index cad137c35..d3cc3d5d2 100644 --- a/src/client/spark/Modules/Trading/ManualFills/ManualFillsParser.cs +++ b/src/client/spark/Modules/Trading/ManualFills/ManualFillsParser.cs @@ -26,6 +26,8 @@ namespace bts.shell.manual_fills { } static class ManualFillsParser { + public const string file_extension = "csv"; + public static Action assign_component(ManualFillComponent component) { switch (component) { case ManualFillComponent.Security: return (mf, val) => mf.SecurityKey = ConvertUtils.try_convert(val)?.Id ?? 0; diff --git a/src/client/spark/Modules/Trading/ManualFills/ManualFillsVM.cs b/src/client/spark/Modules/Trading/ManualFills/ManualFillsVM.cs index dad122d97..75880bae1 100644 --- a/src/client/spark/Modules/Trading/ManualFills/ManualFillsVM.cs +++ b/src/client/spark/Modules/Trading/ManualFills/ManualFillsVM.cs @@ -59,7 +59,7 @@ namespace bts.shell.manual_fills { revert_selected_fills_cmd = ObservantCommand.create(() => revert(selection_mon.filtered_items.ToList()), out var revert_selected_fills_observer); remove_selected_fills_cmd = ObservantAsyncCommand.create(remove_selected_fills_async, out var remove_selected_fills_observer); add_fill_cmd = new DelegateCommand(_add_fill); - import_fills_cmd = FileDialogCommand.open_file(_import_fills); + import_fills_cmd = FileDialogCommand.open_file(_import_fills, extensions: ManualFillsParser.file_extension); // var dirty_or_new_src = dirty_mon.CombineLatest(placeholder_mon, LogicalDelegates.Or); _dispose = Disposable.Create( diff --git a/src/client/spark/core/Utility/NativeFileHelper.cs b/src/client/spark/core/Utility/NativeFileHelper.cs new file mode 100644 index 000000000..7c5ab6983 --- /dev/null +++ b/src/client/spark/core/Utility/NativeFileHelper.cs @@ -0,0 +1,57 @@ +using System.Runtime.InteropServices; + +namespace System.IO { + class NativeFileHelper { + const uint + FILE_ATTRIBUTE_READONLY = 0x00000001, + FILE_ATTRIBUTE_HIDDEN = 0x00000002, + FILE_ATTRIBUTE_SYSTEM = 0x00000004, + FILE_ATTRIBUTE_DIRECTORY = 0x00000010, + FILE_ATTRIBUTE_ARCHIVE = 0x00000020, + FILE_ATTRIBUTE_DEVICE = 0x00000040, + FILE_ATTRIBUTE_NORMAL = 0x00000080, + FILE_ATTRIBUTE_TEMPORARY = 0x00000100, + FILE_ATTRIBUTE_SPARSE_FILE = 0x00000200, + FILE_ATTRIBUTE_REPARSE_POINT = 0x00000400, + FILE_ATTRIBUTE_COMPRESSED = 0x00000800, + FILE_ATTRIBUTE_OFFLINE = 0x00001000, + FILE_ATTRIBUTE_NOT_CONTENT_INDEXED = 0x00002000, + FILE_ATTRIBUTE_ENCRYPTED = 0x00004000, + FILE_ATTRIBUTE_VIRTUAL = 0x00010000, + + SHGFI_ICON = 0x000000100, // get icon + SHGFI_DISPLAYNAME = 0x000000200, // get display name + SHGFI_TYPENAME = 0x000000400, // get type name + SHGFI_ATTRIBUTES = 0x000000800, // get attributes + SHGFI_ICONLOCATION = 0x000001000, // get icon location + SHGFI_EXETYPE = 0x000002000, // return exe type + SHGFI_SYSICONINDEX = 0x000004000, // get system icon index + SHGFI_LINKOVERLAY = 0x000008000, // put a link overlay on icon + SHGFI_SELECTED = 0x000010000, // show icon in selected state + SHGFI_ATTR_SPECIFIED = 0x000020000, // get only specified attributes + SHGFI_LARGEICON = 0x000000000, // get large icon + SHGFI_SMALLICON = 0x000000001, // get small icon + SHGFI_OPENICON = 0x000000002, // get open icon + SHGFI_SHELLICONSIZE = 0x000000004, // get shell size icon + SHGFI_PIDL = 0x000000008, // pszPath is a pidl + SHGFI_USEFILEATTRIBUTES = 0x000000010; // use passed dwFileAttribute + + public static string get_file_type_description(string file_type) => + SHGetFileInfo(file_type, FILE_ATTRIBUTE_NORMAL, out SHFILEINFO shfi, (uint)Marshal.SizeOf(typeof(SHFILEINFO)), SHGFI_USEFILEATTRIBUTES | SHGFI_TYPENAME) != IntPtr.Zero ? + shfi.szTypeName : null; + + [DllImport("shell32")] + static extern IntPtr SHGetFileInfo(string pszPath, uint dwFileAttributes, out SHFILEINFO psfi, uint cbFileInfo, uint flags); + + [StructLayout(LayoutKind.Sequential)] + struct SHFILEINFO { + public IntPtr hIcon; + public int iIcon; + public uint dwAttributes; + [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)] + public string szDisplayName; + [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 80)] + public string szTypeName; + } + } +} \ No newline at end of file diff --git a/src/client/spark/view/Extensions/Command/FileDialogCommand.cs b/src/client/spark/view/Extensions/Command/FileDialogCommand.cs index 419fdbed7..7b0778f1a 100644 --- a/src/client/spark/view/Extensions/Command/FileDialogCommand.cs +++ b/src/client/spark/view/Extensions/Command/FileDialogCommand.cs @@ -7,8 +7,11 @@ using System.Windows.Forms; namespace mbts.ui { public static class FileDialogCommand { - public static IDialogCommand open_file(Action execute) => - new AnonymousFileDialogCommand(_ => _create_open_file_dialog(), (_, file) => execute(file), _ => true); + public static IDialogCommand open_file(Action execute, string initial_dir = null, params string[] extensions) => + open_file(execute, initial_dir, Array.ConvertAll(extensions, ext => (NativeFileHelper.get_file_type_description("." + ext), new[] { ext }))); + + public static IDialogCommand open_file(Action execute, string initial_dir, params (string name, string[] extensions)[] extensions) => + new AnonymousFileDialogCommand(_ => _create_open_file_dialog(null, extensions), (_, file) => execute(file), _ => true); public static IDialogCommand export_proto( Func get_proto, From 03882718a04f65c6526e7c1744c6637cf422ea0b Mon Sep 17 00:00:00 2001 From: John Colvin Date: Mon, 20 May 2019 13:52:09 -0500 Subject: [PATCH 067/121] ManualFills - spark commands menu item groups (i.e., separators in ContextMenu) --- .../ManualFills/ManualFillsWindowFactory.cs | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/src/client/spark/Modules/Trading/ManualFills/ManualFillsWindowFactory.cs b/src/client/spark/Modules/Trading/ManualFills/ManualFillsWindowFactory.cs index 82e214824..85b77ddd0 100644 --- a/src/client/spark/Modules/Trading/ManualFills/ManualFillsWindowFactory.cs +++ b/src/client/spark/Modules/Trading/ManualFills/ManualFillsWindowFactory.cs @@ -54,12 +54,12 @@ namespace bts.shell { } } var cmds = new[] { - new SparkCommandCommand("Add New", Icons.AddBoxes, vm.add_fill_cmd), - new SparkCommandCommand("Revert Selected", Icons.UndoCircular,vm.revert_selected_fills_cmd) { show_in_toolbar = false }, - new SparkCommandCommand("Revert All", Icons.UndoCircular, vm.revert_all_cmd), - new SparkCommandCommand("Save All", Icons.Disk, vm.commit_all_fills_cmd), - new SparkCommandCommand("Delete Selected", Icons.Trash, vm.remove_selected_fills_cmd), - new SparkCommandCommand("Import Fills", Icons.Import, vm.import_fills_cmd) + new SparkCommandCommand("Add New", Icons.AddBoxes, vm.add_fill_cmd) { group = CommandGroups.Add }, + new SparkCommandCommand("Import Fills", Icons.Import, vm.import_fills_cmd) { group = CommandGroups.Add }, + new SparkCommandCommand("Revert Selected", Icons.UndoCircular,vm.revert_selected_fills_cmd) { group = CommandGroups.Revert, show_in_toolbar = false }, + new SparkCommandCommand("Revert All", Icons.UndoCircular, vm.revert_all_cmd) { group = CommandGroups.Revert }, + new SparkCommandCommand("Save All", Icons.Disk, vm.commit_all_fills_cmd) { group = CommandGroups.Save }, + new SparkCommandCommand("Delete Selected", Icons.Trash, vm.remove_selected_fills_cmd) }; return (view, cmds, get_settings, vm); // @@ -102,5 +102,12 @@ namespace bts.shell { static string _col_id(DataGridColumn col) => DataGridEx.GetCellKey(col)?.ToString(); + + static class CommandGroups { + public const string + Add = "Add", + Save = "Save", + Revert = "Revert"; + } } } \ No newline at end of file From 5ae8a6655862d331418a653f4222ba909446b516 Mon Sep 17 00:00:00 2001 From: Eric Harding Date: Mon, 20 May 2019 14:38:41 -0400 Subject: [PATCH 068/121] Add SingleWriterConcurrentVector --- .vscode/launch.json | 2 +- src/lib/md/relay/QueryServer.cpp | 4 +- src/lib/utils/SingleWriterConcurrentVector.h | 176 ++++++++++++++++++ .../SingleWriterConcurrentVectorTests.cpp | 69 +++++++ src/server/sparkd/cmds/Benchmark.cpp | 74 ++++++++ 5 files changed, 322 insertions(+), 3 deletions(-) create mode 100644 src/lib/utils/SingleWriterConcurrentVector.h create mode 100644 src/lib/utils/tests/SingleWriterConcurrentVectorTests.cpp diff --git a/.vscode/launch.json b/.vscode/launch.json index 71b5a8e9e..fac8a7668 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -249,7 +249,7 @@ "program": "/home/bts/spark/build/ninja_debug/server/sparkd", "args": [ "test", - "[mbo]" + "SingleWriterConcurrentVector" ], "stopAtEntry": false, "cwd": "/home/bts/spark/build/ninja_debug/server/", diff --git a/src/lib/md/relay/QueryServer.cpp b/src/lib/md/relay/QueryServer.cpp index 248491942..5ab1a89ac 100644 --- a/src/lib/md/relay/QueryServer.cpp +++ b/src/lib/md/relay/QueryServer.cpp @@ -237,10 +237,10 @@ public: bool snapshot = false; MDConsumerHandle handle = instrument.handle; temp_update.Clear(); - ChannelUpdate* pupd = &temp_update; - pupd->set_key(instrument.handle.key()); bts_assert_d(handle.is_initialized()); if (!handle.is_initialized()) continue; + ChannelUpdate* pupd = &temp_update; + pupd->set_key(instrument.handle.key()); // If the security was just discovered try to ensure all data is sent if (instrument.discovered > TimeStamp(chan.time())) { pupd->mutable_security()->CopyFrom(handle.get_normalized_security()); diff --git a/src/lib/utils/SingleWriterConcurrentVector.h b/src/lib/utils/SingleWriterConcurrentVector.h new file mode 100644 index 000000000..35ef90cb7 --- /dev/null +++ b/src/lib/utils/SingleWriterConcurrentVector.h @@ -0,0 +1,176 @@ +#pragma once + +#include "bts_assert.h" +#include +#include +#include +#include + +namespace bts { + +template +class SingleWriterConcurrentVectorIterator; + +template +class SingleWriterConcurrentVector { + friend class SingleWriterConcurrentVectorIterator; + struct Position { + int32_t chunk; + uint32_t index; + }; + const std::thread::id _tid; + std::vector>> _data; + std::atomic _end; + +public: + SingleWriterConcurrentVector(): + _tid(std::this_thread::get_id()), + _data(), + _end({-1,0}) { + add_chunk(); + } + + void push_back(const T& item) { + bts_assert(std::this_thread::get_id() == _tid); + auto end = _end.load(std::memory_order_acquire); + if (end.index < CHUNK) { + (*_data[end.chunk])[end.index] = item; + // advance pointer only after write is complete + advance_within_chunk(); + } + else { + add_chunk(); + return push_back(item); + } + } + + const T& operator[](uint32_t index) const { + Position p = { (int32_t)(index / CHUNK), index % CHUNK }; + return (*this)[p]; + } + const T& operator[](Position p) const { + return (*_data[p.chunk])[p.index]; + } + + SingleWriterConcurrentVectorIterator begin() const { + return SingleWriterConcurrentVectorIterator(*this, {0,0}); + } + SingleWriterConcurrentVectorIterator end() const { + return SingleWriterConcurrentVectorIterator(*this, _end); + } + size_t size() const { + auto p = _end.load(std::memory_order_acquire); + return p.chunk*CHUNK + p.index; + } +private: + void advance_within_chunk() { + bts_assert_d(std::this_thread::get_id() == _tid); + auto end = _end.load(std::memory_order_acquire); + _end = { end.chunk, end.index + 1 }; + } + void add_chunk() { + bts_assert_d(std::this_thread::get_id() == _tid); + _data.push_back(std::make_unique>()); + auto end = _end.load(); + _end = { end.chunk + 1, 0 }; + } +}; + +template +// forward iterator +class SingleWriterConcurrentVectorIterator { + typedef T value_type; + typedef T& reference; + typedef T* pointer; + typedef std::bidirectional_iterator_tag iterator_category; + + const SingleWriterConcurrentVector& _owner; + typename SingleWriterConcurrentVector::Position _pos; + +public: + SingleWriterConcurrentVectorIterator( + const SingleWriterConcurrentVector& owner, + const typename SingleWriterConcurrentVector::Position& pos) + :_owner(owner), _pos(pos) + { } + + + SingleWriterConcurrentVectorIterator& operator=(const SingleWriterConcurrentVectorIterator& other) { + _owner = other._owner; + _pos = other._pos; + return *this; + } + /* + vector_iterator& operator++() { + size_t element_index = ++my_index; + if( my_item ) { + //TODO: consider using of knowledge about "first_block optimization" here as well? + if( concurrent_vector_base::is_first_element_in_segment(element_index)) { + //if the iterator crosses a segment boundary, the pointer become invalid + //as possibly next segment is in another memory location + my_item= NULL; + } else { + ++my_item; + } + } + return *this; + } + + todo: + cache value pointer + on increment test if element_index == first element in a segment + if it is null item + + */ + SingleWriterConcurrentVectorIterator& operator++() { + ++_pos.index; + if (_pos.index >= CHUNK) { + _pos = { _pos.chunk + 1, 0 }; + } + return *this; + } + SingleWriterConcurrentVectorIterator operator++(int) { + return ++(*this); + } + SingleWriterConcurrentVectorIterator& operator--() { + if (_pos.index == 0) { + _pos = { _pos.chunk - 1, CHUNK-1 }; + } + else { + --_pos.index; + } + return *this; + } + SingleWriterConcurrentVectorIterator operator--(int) { + return --(*this); + } + const T& operator*() const { + return _owner[_pos]; + } + const T* operator->() const { + return &_owner[_pos]; + } + template + friend bool operator==(SingleWriterConcurrentVectorIterator& left, SingleWriterConcurrentVectorIterator& right); + template + friend bool operator!=(SingleWriterConcurrentVectorIterator& left, SingleWriterConcurrentVectorIterator& right); + +}; + +template +void swap(SingleWriterConcurrentVectorIterator& left, SingleWriterConcurrentVectorIterator& right) { + SingleWriterConcurrentVector temp = left; + left = right; + right = temp; +} +template +bool operator==(SingleWriterConcurrentVectorIterator& left, SingleWriterConcurrentVectorIterator& right) { + return &left._owner == &right._owner && left._pos.chunk == right._pos.chunk && left._pos.index == right._pos.index; +} +template +bool operator!=(SingleWriterConcurrentVectorIterator& left, SingleWriterConcurrentVectorIterator& right) { + return &left._owner != &right._owner || left._pos.chunk != right._pos.chunk || left._pos.index != right._pos.index; +} + + +}//namespace \ No newline at end of file diff --git a/src/lib/utils/tests/SingleWriterConcurrentVectorTests.cpp b/src/lib/utils/tests/SingleWriterConcurrentVectorTests.cpp new file mode 100644 index 000000000..6abf67a63 --- /dev/null +++ b/src/lib/utils/tests/SingleWriterConcurrentVectorTests.cpp @@ -0,0 +1,69 @@ +#include "../SingleWriterConcurrentVector.h" +#include "catch/catch.hpp" +#include + +namespace bts { + +struct Chunk { + size_t a,b,c,d; +}; + +TEST_CASE("SingleWriterConcurrentVector", "[collection]") { + SECTION("SEQUENTIAL") { + SingleWriterConcurrentVector intvec; + SingleWriterConcurrentVector chunkvec; + // Insert 9999 elements + for (size_t x=0; x< 9999; ++x) { + intvec.push_back(x); + chunkvec.push_back({ x,x,x,x }); + } + size_t expected = 0; + for(const auto& i : intvec) { + REQUIRE(i == expected); + ++expected; + } + REQUIRE(expected == 9999); + expected = 0; + for(const auto& i : chunkvec) { + REQUIRE(i.a == expected); + REQUIRE(i.b == expected); + REQUIRE(i.c == expected); + REQUIRE(i.d == expected); + ++expected; + } + REQUIRE(expected == 9999); + for (size_t x=0; x< 9999; ++x) { + REQUIRE(intvec[x] == x); + REQUIRE(chunkvec[x].a == x); + REQUIRE(chunkvec[x].b == x); + REQUIRE(chunkvec[x].c == x); + REQUIRE(chunkvec[x].d == x); + } + + auto iter = intvec.begin(); + REQUIRE(*iter == 0); + ++iter; + REQUIRE(*iter == 1); + iter++; + REQUIRE(*iter == 2); + --iter; + REQUIRE(*iter == 1); + iter--; + REQUIRE(*iter == 0); + + REQUIRE(intvec.size() == 9999); + REQUIRE(chunkvec.size() == 9999); + } + SECTION("ONCE MORE, WITH THREADS") { + // SingleWriterConcurrentVector intvec; + // SingleWriterConcurrentVector chunkvec; + // std::thread writer([&]() { + // }); + // auto reader = [&]() { + // }; + // std::vector readers; + // writer.join(); + } +} + +}//ns \ No newline at end of file diff --git a/src/server/sparkd/cmds/Benchmark.cpp b/src/server/sparkd/cmds/Benchmark.cpp index 31e1019c4..40053df52 100644 --- a/src/server/sparkd/cmds/Benchmark.cpp +++ b/src/server/sparkd/cmds/Benchmark.cpp @@ -14,6 +14,7 @@ #include "lib/utils/TimeStamp.h" #include "lib/utils/Uuid.h" #include "lib/utils/mpmc_bounded_queue.h" +#include "lib/utils/SingleWriterConcurrentVector.h" #include "server/sparkd/cmds/CmdRegistry.h" #include "lib/tracing/PerfLog.h" @@ -40,6 +41,7 @@ #include #include +#include namespace bts { namespace cmd { @@ -726,6 +728,78 @@ BENCHMARK(set_hashset_iterate); BENCHMARK(set_stdset_iterate); BENCHMARK(set_btreeset_iterate); + +struct Chunk { + size_t a,b,c,d; +}; + +void tbb_concurrent_vector_insert(benchmark::State& state) { + tbb::concurrent_vector vec; + size_t x = 0; + for (auto _ : state) { + vec.push_back({ ++x, ++x, ++x, ++x }); + } +} +BENCHMARK(tbb_concurrent_vector_insert); +void tbb_concurrent_vector_iterate(benchmark::State& state) { + tbb::concurrent_vector vec; + for (size_t x=0; x < 9999; ++x) { + vec.push_back({ ++x, ++x, ++x, ++x }); + } + for (auto _ : state) { + for (const auto& v : vec) { + benchmark::DoNotOptimize(v); + } + } +} +BENCHMARK(tbb_concurrent_vector_iterate); +void tbb_concurrent_vector_index(benchmark::State& state) { + tbb::concurrent_vector vec; + for (size_t x=0; x < 9999; ++x) { + vec.push_back({ ++x, ++x, ++x, ++x }); + } + for (auto _ : state) { + for (size_t x = 0; x < vec.size(); ++x) { + benchmark::DoNotOptimize(vec[x]); + } + } +} +BENCHMARK(tbb_concurrent_vector_index); + +void swcv_concurrent_vector_insert(benchmark::State& state) { + SingleWriterConcurrentVector vec; + size_t x = 0; + for (auto _ : state) { + vec.push_back({ ++x, ++x, ++x, ++x }); + } +} +BENCHMARK(swcv_concurrent_vector_insert); +void swcv_concurrent_vector_iterate(benchmark::State& state) { + SingleWriterConcurrentVector vec; + for (size_t x=0; x < 9999; ++x) { + vec.push_back({ ++x, ++x, ++x, ++x }); + } + for (auto _ : state) { + for (const auto& v : vec) { + benchmark::DoNotOptimize(v); + } + } +} +BENCHMARK(swcv_concurrent_vector_iterate); +void swcv_concurrent_vector_index(benchmark::State& state) { + SingleWriterConcurrentVector vec; + for (size_t x=0; x < 9999; ++x) { + vec.push_back({ ++x, ++x, ++x, ++x }); + } + for (auto _ : state) { + for (size_t x = 0; x < vec.size(); ++x) { + benchmark::DoNotOptimize(vec[x]); + } + } +} +BENCHMARK(swcv_concurrent_vector_index); + + std::string Benchmark::help() { return "Run micro-benchmarks. Use --benchmark_filter=regex to run only " "specific benchmarks."; From 9c489c03c6abc9dbcf3a2c89a4fc42ae567908e8 Mon Sep 17 00:00:00 2001 From: John Colvin Date: Mon, 20 May 2019 15:33:20 -0500 Subject: [PATCH 069/121] accidentally gave the generic RowParser.AssignColumnValue delegate a name specific to manual fills --- src/client/spark_core/utils/RowParser.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/client/spark_core/utils/RowParser.cs b/src/client/spark_core/utils/RowParser.cs index ed4c0f34a..55dad96e0 100644 --- a/src/client/spark_core/utils/RowParser.cs +++ b/src/client/spark_core/utils/RowParser.cs @@ -4,7 +4,7 @@ using System.Linq; namespace bts.utils { static class RowParser { - public delegate void AssignColumnValue(ref T manual_fill, string cell_text); + public delegate void AssignColumnValue(ref T row, string cell_text); public static AssignColumnValue with_null_check(this AssignColumnValue assignment_del) { return impl; From 2f12f2609ae796edc6d2dce9269aa6e199c1c303 Mon Sep 17 00:00:00 2001 From: John Colvin Date: Mon, 20 May 2019 15:39:51 -0500 Subject: [PATCH 070/121] added RowParser.parse_rows_checked, which supports partial rows (with missing cells) --- src/client/spark_core/utils/RowParser.cs | 48 +++++++++++++++++++++--- 1 file changed, 43 insertions(+), 5 deletions(-) diff --git a/src/client/spark_core/utils/RowParser.cs b/src/client/spark_core/utils/RowParser.cs index 55dad96e0..76a600c6b 100644 --- a/src/client/spark_core/utils/RowParser.cs +++ b/src/client/spark_core/utils/RowParser.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Linq; +using System.Text; namespace bts.utils { static class RowParser { @@ -32,22 +33,59 @@ namespace bts.utils { public static IEnumerable parse_rows(string header_line, IEnumerable rows, IReadOnlyDictionary> column_map, char delimiter) where T : new() => _parse_rows(header_line, rows.GetEnumerator(), column_map, delimiter); + // Assumes that the first row is column headers + public static IEnumerable parse_rows_checked(IEnumerable rows, IReadOnlyDictionary> column_map, char delimiter) where T : new() { + var rows_en = rows.GetEnumerator(); + return rows_en.MoveNext() ? _parse_rows_checked(rows_en.Current, rows_en, column_map, delimiter) : Enumerable.Empty(); + } + + public static IEnumerable parse_rows_checked(string header_line, IEnumerable rows, IReadOnlyDictionary> column_map, char delimiter) where T : new() => + _parse_rows_checked(header_line, rows.GetEnumerator(), column_map, delimiter); + static IEnumerable _parse_rows(string header_line, IEnumerator rows_en, IReadOnlyDictionary> column_map, char delimiter) where T : new() { - var assignment_dels = header_line - .Split(new[] { delimiter }, StringSplitOptions.RemoveEmptyEntries) - .KeyJoin(column_map) - .ToArray(); + var header_cells = header_line.Split(new[] { delimiter }, StringSplitOptions.RemoveEmptyEntries); + if (header_cells.Length == 0) + yield break; + if (header_cells.Length != column_map.Count) + throw new CSVInvalidColumnHeadersException(); + var cell_templates = new AssignColumnValue[header_cells.Length]; + for (int i = 0; i < header_cells.Length; i++) + cell_templates[i] = column_map.TryGetValue(header_cells[i], out var assign) ? assign : throw new CSVInvalidColumnHeadersException(); using (rows_en) { while (rows_en.MoveNext()) { var cells = rows_en.Current.Split(delimiter); var result = new T(); // assumes cells.Length will never exceed columns.Length (this can be changed if necessary) for (int i = 0; i < cells.Length; i++) { - assignment_dels[i](ref result, cells[i]); + cell_templates[i](ref result, cells[i]); } yield return result; } } } + + static IEnumerable _parse_rows_checked(string header_line, IEnumerator rows_en, IReadOnlyDictionary> column_map, char delimiter) where T : new() { + var col_headers = header_line.Split(new[] { delimiter }, StringSplitOptions.RemoveEmptyEntries).Select((name, index) => (name, index)); + var cell_templates = ( + from col in col_headers + join kv in column_map on col.name equals kv.Key + select new { col.index, assign = kv.Value } + ).ToArray(); + using (rows_en) { + while (rows_en.MoveNext()) { + var cells = rows_en.Current.Split(delimiter); + var result = new T(); + for (int i = 0; i < cell_templates.Length; i++) { + var ct = cell_templates[i]; + if (ct.index < cells.Length) + ct.assign(ref result, cells[ct.index]); + + } + yield return result; + } + } + } + + public class CSVInvalidColumnHeadersException : Exception { } } } \ No newline at end of file From bd47816d16690a41b05acdb5a3475760f467c40b Mon Sep 17 00:00:00 2001 From: John Colvin Date: Mon, 20 May 2019 15:57:58 -0500 Subject: [PATCH 071/121] ManualFillsParser - don't pass null string key to dictionary --- .../spark/Modules/Trading/ManualFills/ManualFillsParser.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/client/spark/Modules/Trading/ManualFills/ManualFillsParser.cs b/src/client/spark/Modules/Trading/ManualFills/ManualFillsParser.cs index d3cc3d5d2..7d6ca8968 100644 --- a/src/client/spark/Modules/Trading/ManualFills/ManualFillsParser.cs +++ b/src/client/spark/Modules/Trading/ManualFills/ManualFillsParser.cs @@ -66,7 +66,7 @@ namespace bts.shell.manual_fills { } return missing_components; void add_missing(ManualFillComponent component, string text, ParsedManualFill result) => - missing_components[component].GetOrAdd(text, mk_list).Add(result); + missing_components[component].GetOrAdd(text ?? string.Empty, mk_list).Add(result); } // This implementation prioritizes simplicity over speed. From 44730204a57e9de791293b084cffb1ba02400bdb Mon Sep 17 00:00:00 2001 From: John Colvin Date: Mon, 20 May 2019 19:03:20 -0500 Subject: [PATCH 072/121] fix PropertyChangedDemuxer.PropertyChannel bug on dispose --- .../core/Utility/PropertyChangedDemuxer.cs | 22 +++++++++++++------ 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/src/client/spark/core/Utility/PropertyChangedDemuxer.cs b/src/client/spark/core/Utility/PropertyChangedDemuxer.cs index 3c40e0bd6..49d5ead73 100644 --- a/src/client/spark/core/Utility/PropertyChangedDemuxer.cs +++ b/src/client/spark/core/Utility/PropertyChangedDemuxer.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Collections.Immutable; using System.ComponentModel; using System.Linq; using System.Reactive.Disposables; @@ -62,30 +63,37 @@ namespace mbts.utils { class PropertyChannel : IPropertyChannel, IObservable { T _last_value; + long _id_counter; readonly Func _get; - readonly List> _observers = new List>(); + ImmutableDictionary> _observers = ImmutableDictionary>.Empty; public PropertyChannel(Func get) => _last_value = (_get = get)(); public void on_change() { _last_value = _get(); - foreach (var o in _observers) + foreach (var o in _observers.Values) o.OnNext(_last_value); } public IDisposable Subscribe(IObserver observer) { - _observers.Add(observer); + long id = _id_counter++; + // assume there is no way we ran out of ids, because that would be nuts. + _observers = _observers.Add(id, observer); observer.OnNext(_last_value); - return Disposable.Create(() => _observers.Remove(observer)); + return Disposable.Create(() => _observers = _observers.Remove(id)); } IObservable IPropertyChannel.subscribe() => this.Select(v => (object)v); public void Dispose() { - foreach (var o in _observers) - o.OnCompleted(); - _observers.Clear(); + ImmutableDictionary> local; + do { + local = _observers; + foreach (var o in local.Values) + o.OnCompleted(); + } while (!ReferenceEquals(local, _observers)); // remember: observers is immutable + _observers = _observers.Clear(); } } } From d244c62c26c7a7fb9675b695fa52572c3b34819d Mon Sep 17 00:00:00 2001 From: John Colvin Date: Mon, 20 May 2019 19:32:41 -0500 Subject: [PATCH 073/121] ManualFillsView - add 'size' column --- .../Trading/ManualFills/ManualFillsView.xaml | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/client/spark/Modules/Trading/ManualFills/ManualFillsView.xaml b/src/client/spark/Modules/Trading/ManualFills/ManualFillsView.xaml index 4da2a01bc..d62ef9618 100644 --- a/src/client/spark/Modules/Trading/ManualFills/ManualFillsView.xaml +++ b/src/client/spark/Modules/Trading/ManualFills/ManualFillsView.xaml @@ -187,6 +187,19 @@ + + + + + + + + + + + + + From 16f0beb3d8534759024588ee1dd05eb4285bf95c Mon Sep 17 00:00:00 2001 From: John Colvin Date: Mon, 20 May 2019 19:54:10 -0500 Subject: [PATCH 074/121] ManualFills - added conf to switch between inline/modal transactions (default to modal) --- .../ManualFills/ManualFillRowEditor.xaml | 83 ++++ .../ManualFills/ManualFillRowEditor.xaml.cs | 22 + .../Trading/ManualFills/ManualFillsVM.cs | 14 +- .../Trading/ManualFills/ManualFillsView.xaml | 428 +++++++++--------- .../ManualFills/ManualFillsView.xaml.cs | 55 +++ .../ManualFills/ManualFillsWindowFactory.cs | 227 +++++----- src/lib/config/protos/UI.proto | 4 + 7 files changed, 507 insertions(+), 326 deletions(-) create mode 100644 src/client/spark/Modules/Trading/ManualFills/ManualFillRowEditor.xaml create mode 100644 src/client/spark/Modules/Trading/ManualFills/ManualFillRowEditor.xaml.cs diff --git a/src/client/spark/Modules/Trading/ManualFills/ManualFillRowEditor.xaml b/src/client/spark/Modules/Trading/ManualFills/ManualFillRowEditor.xaml new file mode 100644 index 000000000..a1c7e8191 --- /dev/null +++ b/src/client/spark/Modules/Trading/ManualFills/ManualFillRowEditor.xaml @@ -0,0 +1,83 @@ + + + 4 + 8 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/client/spark/Modules/Trading/Ladder/View/LadderView.xaml.cs b/src/client/spark/Modules/Trading/Ladder/View/LadderView.xaml.cs index 1bfc35beb..f4f6ecea7 100644 --- a/src/client/spark/Modules/Trading/Ladder/View/LadderView.xaml.cs +++ b/src/client/spark/Modules/Trading/Ladder/View/LadderView.xaml.cs @@ -1,4 +1,7 @@ -using bts.collections; +using bts; +using bts.collections; +using bts.utils; +using mbts.ui.converters; using mbts.utils; using System.Linq; using System.Windows; @@ -8,6 +11,35 @@ using System.Windows.Input; namespace mbts.spark { partial class LadderView : UserControl { + public static IMultiValueConverter PositionToolTipConverter = + ValueConverter.create((account, show_day_pos, show_total_pos) => { + const string day_pos_text = "day", total_pos_text = "total"; + if (!account.HasValue) + return string.Empty; + var sb = new StringBuilderEx(account.Value.name); + string primary_pos; + if (show_day_pos) { + primary_pos = day_pos_text; + if (show_total_pos) { + sb.Add(" '", primary_pos, " "); + sb.Add("(", total_pos_text, ")' "); + } else { + sb.Add(" ", primary_pos, " "); + } + } else if (show_total_pos) { + primary_pos = total_pos_text; + sb.Add(" ", primary_pos, " "); + } else { + // unexpected code path (the button for which this tooltip is destined should be invisible in this case), but no need to throw an exception. + return string.Empty; + } + sb.Add("position\n(click to copy "); + if (show_day_pos && show_total_pos) + sb.Add(primary_pos, " position "); + sb.Add("to size)"); + return sb.ToString(); + }); + readonly double _max_zoom, _min_zoom; internal LadderView(LadderVM vm) { diff --git a/src/client/spark/core/Utility/ViewModelBase.cs b/src/client/spark/core/Utility/ViewModelBase.cs index 0ae2c1c51..3fafe9d08 100644 --- a/src/client/spark/core/Utility/ViewModelBase.cs +++ b/src/client/spark/core/Utility/ViewModelBase.cs @@ -327,17 +327,21 @@ namespace mbts.ui.shell { public TModel model { get => _model; set => set(value); } public TView view => _view; - public void set(TModel model) { - if (!_comparer.Equals(model, _model)) { - TView new_view = _convert(model); - changing?.Invoke((_model, _view), (model, new_view)); - TView old_view = _view; - TModel old_model = _model; - _model = model; - _view = new_view; - changed?.Invoke((old_model, old_view), (_model, _view)); - _owner.RaisePropertyChanged(_prop_name); - } + public void set(TModel model) => + set_new(model); + + public bool set_new(TModel model) { + if (_comparer.Equals(model, _model)) + return false; + TView new_view = _convert(model); + changing?.Invoke((_model, _view), (model, new_view)); + TView old_view = _view; + TModel old_model = _model; + _model = model; + _view = new_view; + changed?.Invoke((old_model, old_view), (_model, _view)); + _owner.RaisePropertyChanged(_prop_name); + return true; } public IDisposable Subscribe(IObserver<((TModel model, TView view) oldval, (TModel model, TView view) newval)> observer) { diff --git a/src/client/spark/view/Spark/Core/SparkUtils.cs b/src/client/spark/view/Spark/Core/SparkUtils.cs index 3d3060b7a..7027098c9 100644 --- a/src/client/spark/view/Spark/Core/SparkUtils.cs +++ b/src/client/spark/view/Spark/Core/SparkUtils.cs @@ -53,6 +53,9 @@ namespace mbts.spark { public static DateTime to_local_time(this Google.Protobuf.WellKnownTypes.Timestamp timestamp) => timestamp.ToDateTimeOffset().LocalDateTime; + public static string fmt_position(double value) => + double.IsNaN(value) ? "-" : value.ToString("0"); + // there is a path to cast null to SecurityImmutable, so it is important that the SecurityImmutable?() type is used explicitly. // the path: null -> Security -> SecurityImmutable public static SecurityImmutable? security(this SecurityMD? security_md) => From 366e13ef0558b961ffe1c36a0576a48e66e8d157 Mon Sep 17 00:00:00 2001 From: Pedro Pinto Date: Fri, 24 May 2019 13:56:37 -0400 Subject: [PATCH 110/121] Fixes requoted performance regression introduced in commit e998edc --- src/lib/orderman/actors/order_manager/OrderManager.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/lib/orderman/actors/order_manager/OrderManager.cpp b/src/lib/orderman/actors/order_manager/OrderManager.cpp index e393c90f4..602c153c9 100644 --- a/src/lib/orderman/actors/order_manager/OrderManager.cpp +++ b/src/lib/orderman/actors/order_manager/OrderManager.cpp @@ -537,7 +537,8 @@ void OrderManager::reload_fix_buffer(SessionServicesData &osd) { } void OrderManager::on_poll(trace::PerfLog&log) { - auto scoped_do_work = trace::log_scope(log, {}, "OrderManager::dispatch_incoming_fix_messages"); + auto scoped_do_work = trace::log_scope(log, {}, "OrderManager::dispatch_incoming_fix_messages", + utils::TimeStamp::Interval::from_microseconds(2)); for (auto &osd : me->services_data.sessions) osd->gw.dispatch_incoming_fix_messages(); } From 12d54fdb3144312ad5a68993d547902e06c2bf95 Mon Sep 17 00:00:00 2001 From: Pedro Pinto Date: Fri, 24 May 2019 13:58:59 -0400 Subject: [PATCH 111/121] Added manual test to validate spreader performance. --- .../server_performance/spreader_requote.txt | 69 +++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 testing/manual/server_performance/spreader_requote.txt diff --git a/testing/manual/server_performance/spreader_requote.txt b/testing/manual/server_performance/spreader_requote.txt new file mode 100644 index 000000000..3724f0fb1 --- /dev/null +++ b/testing/manual/server_performance/spreader_requote.txt @@ -0,0 +1,69 @@ +Test Author: Pedro +If it breaks: Pedro + +Spreader Requote Performance test + +Note: This test is fairly cumbersome to setup, because performance is +very sensitive to a number of factors including compilation switches, +hardware used, thread allocation and load on the machine. Pedro has +the best handle on how do all this and should probably be the one +doing this test. + + +1) Make sure you are testing a release server build + +2) Make sure you use real hardware, with hyper threading turned off +and cstates disabled, for the server side + +3) Make sure you isolate the CPU on which you are running the +server. This is best done on Linux by editing /etc/default/grub like +this: + +# this isolates CPU's 0 to 5 for better performance testing +GRUB_CMDLINE_LINUX="isolcpus=0-5" + +4) Now make sure your configuration is set for the lowest latency, +including dedicating a thread to the order manager on the isolated +cores: + +OrdermanCfg { + value { + executor { + thread_name: "oman_orders" + poll: true + hosted_actor: "MDSource" + hosted_actor: "OrderManager" + } + executor { + thread_name: "oman_slow" + poll: false + hosted_actor: "*" + } + } +} + +Threads { + value { + thread_group {name_rx: "mdp3feed" core: 0} + thread_group {name_rx: "oman_orders" core: 1} + thread_group {name_rx: "jf_ar_sim" core: 2} + thread_group {name_rx: ".*" core:6 core:7 core:8 core:9 core:10 } + } +} + +5) Now you are ready to start testing. Start a shell and create a new +synthetic say with 2 CL futures. Use the near month (fast ticking) +future as the non quoting leg, so that the other leg gets quoted +often. Make sure to disable throtling in the synthetic definition +dialog. + +6) Place an order for this synthetic, away from the market. + +7) Now run for say 3 minutes. Visit http://youserver:8000/perf_trace +You should see a graph indicating the median requote times for the +order you created in 5). Make sure you have at least 100 data points +and data the median time is around 12us. If you get anything greater +than 15us, the test has failed. + + + From 68428ab89d67b0ab5312e70f1247fe901d29c270 Mon Sep 17 00:00:00 2001 From: Pedro Pinto Date: Fri, 24 May 2019 13:59:31 -0400 Subject: [PATCH 112/121] Ran clang format. --- .../actors/order_manager/OrderManager.cpp | 35 ++++++++++--------- 1 file changed, 19 insertions(+), 16 deletions(-) diff --git a/src/lib/orderman/actors/order_manager/OrderManager.cpp b/src/lib/orderman/actors/order_manager/OrderManager.cpp index 602c153c9..feae6507d 100644 --- a/src/lib/orderman/actors/order_manager/OrderManager.cpp +++ b/src/lib/orderman/actors/order_manager/OrderManager.cpp @@ -358,7 +358,7 @@ OrderManager::free_buffers(std::string const &session_name) { } void OrderManager::init_order_services() { - auto& log = me->services_data.log; + auto &log = me->services_data.log; for (auto &s : me->services_data.sessions) { auto gw_name = GatewayName(s->session); @@ -410,13 +410,14 @@ void OrderManager::init_order_services() { }; s->exe_report_subs = std::make_unique>( - *this, gw_name, on_exe_r,log); + *this, gw_name, on_exe_r, log); s->cancel_reject_subs = - std::make_unique>(*this, - gw_name,log); + std::make_unique>( + *this, gw_name, log); s->gateway_status_subs = std::make_unique< - ContentSubscription>(*this, gw_name,log); + ContentSubscription>(*this, gw_name, + log); // publishers for execution reports, gateway status and cancel // reject evets @@ -494,18 +495,18 @@ void OrderManager::init_order_services() { me->services_data.cor_id = ev.trace; }; me->services_data.md_subs = - std::make_unique>(*this, on_md_sub, - on_book_update,log); + std::make_unique>( + *this, on_md_sub, on_book_update, log); me->services_data.ticker_subs = - std::make_unique>(*this, - on_ticker_sub,log); + std::make_unique>( + *this, on_ticker_sub, log); me->services_data.settlement_subs = std::make_unique>( - *this, on_settlement_sub,log); + *this, on_settlement_sub, log); me->services_data.time_subs = - std::make_unique>(*this,log); + std::make_unique>(*this, log); me->services_data.order_creation_subs = - std::make_unique>(*this,log); + std::make_unique>(*this, log); } void OrderManager::after_event() { @@ -514,7 +515,8 @@ void OrderManager::after_event() { for (auto &osd : me->services_data.sessions) { auto &fix_stream = osd->current_buffer->first_stream(); if (fix_stream.n_msgs()) { - auto scoped_do_work = trace::log_scope(me->services_data.log, {}, "OrderManager::after_event"); + auto scoped_do_work = trace::log_scope(me->services_data.log, {}, + "OrderManager::after_event"); fix_stream.close(); osd->gw.send_fix(osd->current_buffer, osd->current_trace); reload_fix_buffer(*osd); @@ -536,9 +538,10 @@ void OrderManager::reload_fix_buffer(SessionServicesData &osd) { } } -void OrderManager::on_poll(trace::PerfLog&log) { - auto scoped_do_work = trace::log_scope(log, {}, "OrderManager::dispatch_incoming_fix_messages", - utils::TimeStamp::Interval::from_microseconds(2)); +void OrderManager::on_poll(trace::PerfLog &log) { + auto scoped_do_work = + trace::log_scope(log, {}, "OrderManager::dispatch_incoming_fix_messages", + utils::TimeStamp::Interval::from_microseconds(2)); for (auto &osd : me->services_data.sessions) osd->gw.dispatch_incoming_fix_messages(); } From f6be20ce8d820af0fdfb8d8de8e54a884623bb21 Mon Sep 17 00:00:00 2001 From: Pedro Pinto Date: Fri, 24 May 2019 14:02:06 -0400 Subject: [PATCH 113/121] Updated release notes. --- doc/user/content/release_notes/_index.md | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/doc/user/content/release_notes/_index.md b/doc/user/content/release_notes/_index.md index 0e98e18bd..fa3fac84a 100644 --- a/doc/user/content/release_notes/_index.md +++ b/doc/user/content/release_notes/_index.md @@ -4,6 +4,14 @@ weight = 3 sort_by = "weight" insert_anchor_links = "right" +++ +## 2.0.4 +### User Visible Changes + - Fixed performance regression in order manager + +### Internal changes + - Improved market data diagnostics for bad configurations + - Changed market data default buffer size + ## 2.0.3 ### Internal changes - Improved backwards compatibility in UI settings From 61556eb53478fd48256c0cee04860416f7942393 Mon Sep 17 00:00:00 2001 From: Taha Afzal Date: Fri, 24 May 2019 16:43:56 -0400 Subject: [PATCH 114/121] 'Algo->spreader is not sending the price over' is fixed --- src/lib/orderman/orders/spreader_orders/SpreaderOrder.cpp | 7 +++++++ src/lib/orderman/orders/spreader_orders/SpreaderOrder.h | 3 +++ 2 files changed, 10 insertions(+) diff --git a/src/lib/orderman/orders/spreader_orders/SpreaderOrder.cpp b/src/lib/orderman/orders/spreader_orders/SpreaderOrder.cpp index 24ec42c67..e5d25ad49 100644 --- a/src/lib/orderman/orders/spreader_orders/SpreaderOrder.cpp +++ b/src/lib/orderman/orders/spreader_orders/SpreaderOrder.cpp @@ -82,6 +82,13 @@ OrderOpResult SpreaderOrder::on_update(Price price, Quantity quantity){ _report_state(); return OrderOpResult {}; } +OrderOpResult SpreaderOrder::on_update(const api::orderman::Update& update){ + if(update.has_custom()) + return OrderOpResult{api::orderman::ORDER_ERROR_INVALID_OP, + "Spreader parameter update is not supported"}; + else + return on_update(Price(update.price()),Quantity(update.quantity())); +} OrderOpResult SpreaderOrder::on_cancel(){ if (!_active) return OrderOpResult{api::orderman::ORDER_ERROR_OP_NOT_SUPPORTED}; LOG(OrderMan, Info, "SpreaderOrder.on_cancel key:{}", key().value()); diff --git a/src/lib/orderman/orders/spreader_orders/SpreaderOrder.h b/src/lib/orderman/orders/spreader_orders/SpreaderOrder.h index f49e9c755..fd0fac448 100644 --- a/src/lib/orderman/orders/spreader_orders/SpreaderOrder.h +++ b/src/lib/orderman/orders/spreader_orders/SpreaderOrder.h @@ -115,6 +115,9 @@ public: // called when the user (or a higher level order) // updates the order's price or quantity OrderOpResult on_update(Price, Quantity) override; + // called when the user (or a higher level order) + // updates the order's params (including possibly price or quantity) + OrderOpResult on_update(const api::orderman::Update&) override; // called when the user (or higher level order) cancels an order OrderOpResult on_cancel() override; // detach means to let the lower level orders lose from the higher From 8b17911da069768afd6d15246914143c10a17a6c Mon Sep 17 00:00:00 2001 From: Jake Norbut Date: Fri, 24 May 2019 15:45:28 -0500 Subject: [PATCH 115/121] refining pnl test --- testing/manual/positions_and_fills/PAndL.txt | 35 ++++++++++++-------- 1 file changed, 21 insertions(+), 14 deletions(-) diff --git a/testing/manual/positions_and_fills/PAndL.txt b/testing/manual/positions_and_fills/PAndL.txt index 14b66888f..cbbbb6624 100644 --- a/testing/manual/positions_and_fills/PAndL.txt +++ b/testing/manual/positions_and_fills/PAndL.txt @@ -64,6 +64,7 @@ Check the Totals Footer Force the SOD process to aggregate the fills Configure your sod time to be after your last fill and before now. Note that SODTime is relative to the server's local time, which in the case of DEV will likely be UTC, so you'll need to supply the appropriate UTC time. + https://time.is/UTC Here is a sample config: @@ -75,7 +76,7 @@ SODTime{ } Restart sparkd and the SOD process should consolidate your day fills so... -(note: in previous occasions it was necessary bounce the shell or otherwise cause a MD change for this to work) +(note: you will need to move the market and put it back to facilliate a change in MD to get a PnL price) ACC A: Start Net Pos -1 @@ -97,7 +98,7 @@ SODTime{ (14.0125 - 14.005) * 1 * 5000 PnL Price 14.0125 (displayed as 14.015) -Sell a 1 lot as the current bid in ACC A +Sell a 1 lot at the current bid in ACC A ACC A: Start Net Pos -1 (no change) Start PnL -37.5 @@ -117,26 +118,32 @@ Sell a 1 lot as the current bid in ACC A Total PnL 75 PnL Price 14.0125 (displayed as 14.015) -Buy a lot at the current bid in ACCA via Trading -> Manual Fills +Buy a 1 lot at the current bid in ACCA via Trading -> Manual Fills Click the '+' menu icon from the toolbar along the left side of the window to bring up the Add New popup - Click on the blank security button to launch the security picker - Specify accoutn as ACC A + Click on the blank security button to launch the security picker and pick your security + Specify account as ACC A Specify side as Buy Specify qty as 1 - Specify price at xxx + Specify price at 14.005 + Click OK to state the fill + Click the Save icon to publish it out - (I haven't actually tested the below since at the time of writing the manual fills are not yet published by the position server, but I believe this is what it should be) + The manual fill will be published as part of the SOD so ACC A should reflect as follows: ACC A: - Start Net Pos -1 - Start PnL -37.5 - Day Long 1 + Start Net Pos 0 + Start PnL 0.00 + Day Long 0 Day Short 1 - Day PnL 0 + Day PnL -37.5 Total Net Pos -1 - Total PnL -12.5 - PnL Price 14.0125 (displayed as 14.015) - + Total PnL -37.5 + PnL Price 14.0125 (displayed as 14.015) + +Enter a manual fill in a complely different security of your choosing + Verify a new row is show in the pnl for the appropriate security + Verify the net pos is accurate + For extra credit, create a market in that security and verify the Start PnL and Total PnL reflect appropriately Test sorting and filtering From 45a5049a2de17eeab7735d20368da3068ef828d0 Mon Sep 17 00:00:00 2001 From: John Colvin Date: Fri, 24 May 2019 15:32:48 -0500 Subject: [PATCH 116/121] fix typeo ('or' conditions should be in parenthesis) --- src/client/spark/Modules/Trading/Ladder/VM/LadderVM.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/client/spark/Modules/Trading/Ladder/VM/LadderVM.cs b/src/client/spark/Modules/Trading/Ladder/VM/LadderVM.cs index d004e7555..cd61d43b3 100644 --- a/src/client/spark/Modules/Trading/Ladder/VM/LadderVM.cs +++ b/src/client/spark/Modules/Trading/Ladder/VM/LadderVM.cs @@ -241,7 +241,7 @@ namespace mbts.spark { } else { total = day = double.NaN; } - if ((Set(ref _position, total, nameof(position)) | Set(ref _day_position, day, nameof(day_position))) && show_day_position || show_account_position) + if ((Set(ref _position, total, nameof(position)) | Set(ref _day_position, day, nameof(day_position))) && (show_day_position || show_account_position)) RaisePropertyChanged(nameof(position_display_text)); }), position_src.Connect()); From 9a9024c2c0ddb48b2d392856d7599819ca47b6c5 Mon Sep 17 00:00:00 2001 From: John Colvin Date: Fri, 24 May 2019 15:42:32 -0500 Subject: [PATCH 117/121] OrderBookView - order params/ladder grid splitter should be horizontally stretched (was only 5 pixels wide) --- .../spark/Modules/Trading/OrderBook/View/OrderBookView.xaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/client/spark/Modules/Trading/OrderBook/View/OrderBookView.xaml b/src/client/spark/Modules/Trading/OrderBook/View/OrderBookView.xaml index 16849cc70..a57e16bfe 100644 --- a/src/client/spark/Modules/Trading/OrderBook/View/OrderBookView.xaml +++ b/src/client/spark/Modules/Trading/OrderBook/View/OrderBookView.xaml @@ -548,7 +548,7 @@ Grid.Column="1" Grid.Row="3" Panel.ZIndex="1" - Width="{StaticResource GridSplitterWidth}" + HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Height="{x:Static sp:OrderBookView.GridSplitterHeight}" BorderThickness="0,1,0,0" From 270ab9c3f1ff9a2ccbb5e15e763a0ac55624829c Mon Sep 17 00:00:00 2001 From: John Colvin Date: Fri, 24 May 2019 16:59:26 -0500 Subject: [PATCH 118/121] change 'row' to 'fill' in ImportConflicts view --- .../Trading/ManualFills/ManualFillsImportConflictsView.xaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/client/spark/Modules/Trading/ManualFills/ManualFillsImportConflictsView.xaml b/src/client/spark/Modules/Trading/ManualFills/ManualFillsImportConflictsView.xaml index 04d77bb66..097112a63 100644 --- a/src/client/spark/Modules/Trading/ManualFills/ManualFillsImportConflictsView.xaml +++ b/src/client/spark/Modules/Trading/ManualFills/ManualFillsImportConflictsView.xaml @@ -96,7 +96,7 @@ - + From 567c18f3256e1a6e193781bc23eab82fb61af60b Mon Sep 17 00:00:00 2001 From: John Colvin Date: Fri, 24 May 2019 17:35:05 -0500 Subject: [PATCH 119/121] ManualFills - hardcode min width to 466 (hides horizontal scrollbar when window first opens) --- .../Trading/ManualFills/ManualFillsWindowFactory.cs | 3 ++- src/client/spark/view/Spark/SparkWindow.cs | 11 ++++++++--- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/src/client/spark/Modules/Trading/ManualFills/ManualFillsWindowFactory.cs b/src/client/spark/Modules/Trading/ManualFills/ManualFillsWindowFactory.cs index 0e68d21b2..56a3c86f4 100644 --- a/src/client/spark/Modules/Trading/ManualFills/ManualFillsWindowFactory.cs +++ b/src/client/spark/Modules/Trading/ManualFills/ManualFillsWindowFactory.cs @@ -21,6 +21,7 @@ namespace bts.shell { public const string window_type = "ManualFills", window_name = "Manual Fills"; + const double _window_min_width = 466; readonly IBus _bus; readonly OrdersModel _orders_model; @@ -37,7 +38,7 @@ namespace bts.shell { public override Task load(ManualFillsSettings settings) { return Task.FromResult(SparkWindow.create( - window_type, window_name, _bus, this, Icons.DataEdit, _manual_fills_conf.thread_id, thread_init)); + window_type, window_name, _bus, this, Icons.DataEdit, _manual_fills_conf.thread_id, thread_init, _window_min_width)); // (UIElement, IEnumerable, Func, IDisposable) thread_init() { var vm = ManualFillsVM.create(_bus, _orders_model); diff --git a/src/client/spark/view/Spark/SparkWindow.cs b/src/client/spark/view/Spark/SparkWindow.cs index dfe3908f1..6fbfe6c80 100644 --- a/src/client/spark/view/Spark/SparkWindow.cs +++ b/src/client/spark/view/Spark/SparkWindow.cs @@ -64,8 +64,8 @@ namespace bts.spark { public virtual double MinWidth => 400; public virtual double MinHeight => Math.Max(ABSOLUTE_MIN_HEIGHT, TITLE_PANEL_HEIGHT + COLUMN_HEADER_HEIGHT + GRID_FOOTER_HEIGHT + Commands.Count() * SPARK_MENU_ITEM_HEIGHT); - public static SparkWindow create(string window_type, string title, IBus bus, ISaveWindow save_window, ImageSource icon, string thread_id, Func<(UIElement content, IEnumerable commands, Func get_save_state, IDisposable dispose)> thread_init) => - new AnonymousShared(window_type, title, bus, save_window, icon, thread_id, thread_init); + public static SparkWindow create(string window_type, string title, IBus bus, ISaveWindow save_window, ImageSource icon, string thread_id, Func<(UIElement content, IEnumerable commands, Func get_save_state, IDisposable dispose)> thread_init, double min_width = double.NaN, double min_height = double.NaN) => + new AnonymousShared(window_type, title, bus, save_window, icon, thread_id, thread_init, min_width, min_height); public ShellWindowUI GetContent() { var parent_view = new SparkWindowView(_bus, Commands, Content); @@ -98,17 +98,22 @@ namespace bts.spark { IEnumerable _commands; UIElement _content; IDisposable _dispose; + double _min_width, _min_height; - public AnonymousShared(string window_type, string title, IBus bus, ISaveWindow save_window, ImageSource icon, string thread_id, Func<(UIElement content, IEnumerable commands, Func get_save_state, IDisposable dispose)> thread_init) + public AnonymousShared(string window_type, string title, IBus bus, ISaveWindow save_window, ImageSource icon, string thread_id, Func<(UIElement content, IEnumerable commands, Func get_save_state, IDisposable dispose)> thread_init, double min_width = double.NaN, double min_height = double.NaN) : base(title, bus, save_window, icon) { this.thread_id = thread_id; _thread_init = thread_init ?? throw new ArgumentNullException(nameof(thread_init)); + _min_width = min_width; + _min_height = min_height; } public override string window_type { get; } public override T SaveState => _get_save_state(); public override UIElement Content => _content; public override IEnumerable Commands => _commands; + public override double MinWidth => double.IsNaN(_min_width) ? base.MinWidth : _min_width; + public override double MinHeight => double.IsNaN(_min_height) ? base.MinHeight : _min_height; public string thread_id { get; } From e8a467d5f12d03b241a0c8762f18b2c0763b82cf Mon Sep 17 00:00:00 2001 From: Taha Afzal Date: Mon, 27 May 2019 16:03:44 -0400 Subject: [PATCH 120/121] 1.Position limit check for QHO and 2. QHO crash for rejected orders --- src/lib/orderman/orders/NativeOrder.cpp | 1 + .../orders/queue_holder/state_workers/QHOWorkingStateWorker.cpp | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/src/lib/orderman/orders/NativeOrder.cpp b/src/lib/orderman/orders/NativeOrder.cpp index 2103fd02f..ec727693a 100644 --- a/src/lib/orderman/orders/NativeOrder.cpp +++ b/src/lib/orderman/orders/NativeOrder.cpp @@ -445,6 +445,7 @@ OrderOpResult NativeOrder::on_activate(Price p, Quantity q, // Early return so we skip the expensive report_state() // call. It is OK to spkip it because we are bound to report // our state once the excange aknowledges the order. + update_working_position(); return OrderOpResult{}; } diff --git a/src/lib/orderman/orders/queue_holder/state_workers/QHOWorkingStateWorker.cpp b/src/lib/orderman/orders/queue_holder/state_workers/QHOWorkingStateWorker.cpp index 136c9b699..36966b814 100644 --- a/src/lib/orderman/orders/queue_holder/state_workers/QHOWorkingStateWorker.cpp +++ b/src/lib/orderman/orders/queue_holder/state_workers/QHOWorkingStateWorker.cpp @@ -85,7 +85,7 @@ bool QHOWorkingStateWorker::_analyze_moving_out(){ QueueHolderOrder::TerminationReason::UNEXPECTED_CHILD_CANCEL); return true; } - if(order_man.has_cancelled_order()){ + if(order_man.has_errored_order()){ _porder->_cancel_order(QueueHolderOrder::TerminationReason::CHILD_ERROR); return true; } From 9540a15585efd015635cbaff285755cc3508de1d Mon Sep 17 00:00:00 2001 From: Pedro Pinto Date: Tue, 28 May 2019 07:06:39 -0400 Subject: [PATCH 121/121] Updated release notes and fixed bug that brought Haris down. --- doc/user/content/release_notes/_index.md | 6 ++++++ src/lib/orderman/actors/order_manager/OrderServices.cpp | 7 ++++++- src/web/CMakeDeps.cmake | 2 +- 3 files changed, 13 insertions(+), 2 deletions(-) diff --git a/doc/user/content/release_notes/_index.md b/doc/user/content/release_notes/_index.md index fa3fac84a..b9af27f2d 100644 --- a/doc/user/content/release_notes/_index.md +++ b/doc/user/content/release_notes/_index.md @@ -4,6 +4,12 @@ weight = 3 sort_by = "weight" insert_anchor_links = "right" +++ +## 2.0.5 +### User Visible Changes + - Fixed a bug in the handling of rejected QHO orders + - Implemented stricter position limit enforcement + - Fixed a bug that could cause server failure when trying to attach to already attached orders + ## 2.0.4 ### User Visible Changes - Fixed performance regression in order manager diff --git a/src/lib/orderman/actors/order_manager/OrderServices.cpp b/src/lib/orderman/actors/order_manager/OrderServices.cpp index a6ed180f3..6694524fc 100644 --- a/src/lib/orderman/actors/order_manager/OrderServices.cpp +++ b/src/lib/orderman/actors/order_manager/OrderServices.cpp @@ -207,7 +207,12 @@ OrderOpResult OrderServices::check_is_attachable() { // have either no parent or a dead parent. if (_owner->parent_key() != null_order_key()) { auto current_parent = _data->owner.lookup_order(_owner->parent_key()); - bts_assert(current_parent); + + // If we cannot find a parent that must mean the parent was + // removed by the order clean up process so it must be dead and + // therefore its fine to attach + if (current_parent == nullptr) + return OrderOpResult{}; switch (current_parent->status()) { case ORDER_STATUS_PENDING_WORKING: diff --git a/src/web/CMakeDeps.cmake b/src/web/CMakeDeps.cmake index 40156daa9..21739132b 100644 --- a/src/web/CMakeDeps.cmake +++ b/src/web/CMakeDeps.cmake @@ -1 +1 @@ -set(sources ../../externals/linux/include/google/protobuf/descriptor.proto;../../externals/linux/include/google/protobuf/timestamp.proto;../lib/api/apicore.proto;../lib/api/apigateway.proto;../lib/api/apioptions.proto;../lib/api/services/AdminService.proto;../lib/api/services/AlgoDefinitionService.proto;../lib/api/services/AlgoService.proto;../lib/api/services/ApiUnitTestService.proto;../lib/api/services/AuthService.proto;../lib/api/services/ErrorReportService.proto;../lib/api/services/HelloService.proto;../lib/api/services/MBOService.proto;../lib/api/services/MarketDataAuxiliaryService.proto;../lib/api/services/MarketDataRelay.proto;../lib/api/services/OrderService.proto;../lib/api/services/OutOfProcessURLListener.proto;../lib/api/services/PersistenceService.proto;../lib/api/services/PositionService.proto;../lib/api/services/ShellDiagnosticService.proto;../lib/api/services/ShellLatencyService.proto;../lib/api/services/SyntheticSecurityService.proto;../lib/domain_model/Security.proto;../lib/md/protos/co2.proto;../lib/sparkproto/SparkProtos.proto;api/build.sh;api/node_modules/exit/test/fixtures/create-files.sh;api/node_modules/protobufjs/cli/node_modules/acorn-jsx/node_modules/acorn/bin/update_authors.sh;api/node_modules/watch/release.sh;api/quiet_build.sh;api/src/DynamicService.ts;api/src/ServiceContext.ts;api/src/bts_assert.ts;ui/src/Access.fs;ui/src/Accounts/ClipLimits.fs;ui/src/Accounts/ProductLimits.fs;ui/src/Accounts/SecurityLimits.fs;ui/src/Accounts/State.fs;ui/src/Accounts/Types.fs;ui/src/Accounts/View.fs;ui/src/App.fs;ui/src/Cmd.fs;ui/src/Controls/Button.fs;ui/src/Controls/DataGrid.fs;ui/src/Controls/Fields.fs;ui/src/Controls/Modal.fs;ui/src/Controls/ReactGrid.fs;ui/src/Controls/Table.fs;ui/src/Controls/nbsp.fs;ui/src/Global.fs;ui/src/Imports.fs;ui/src/Lens.fs;ui/src/Login/Login.fs;ui/src/Nav.fs;ui/src/State.fs;ui/src/Types.fs;ui/src/Users/State.fs;ui/src/Users/Types.fs;ui/src/Users/View.fs;ui/src/Util/EnumUtils.fs;ui/src/Util/JSMap.fs;ui/src/obj/Debug/netstandard2.0/ui.AssemblyInfo.fs) \ No newline at end of file +set(sources ../../externals/linux/include/google/protobuf/descriptor.proto;../../externals/linux/include/google/protobuf/timestamp.proto;../lib/api/apicore.proto;../lib/api/apigateway.proto;../lib/api/apioptions.proto;../lib/api/services/AdminService.proto;../lib/api/services/AlgoDefinitionService.proto;../lib/api/services/AlgoService.proto;../lib/api/services/ApiUnitTestService.proto;../lib/api/services/AuthService.proto;../lib/api/services/ErrorReportService.proto;../lib/api/services/HelloService.proto;../lib/api/services/MBOService.proto;../lib/api/services/MarketDataAuxiliaryService.proto;../lib/api/services/MarketDataRelay.proto;../lib/api/services/OrderService.proto;../lib/api/services/OutOfProcessURLListener.proto;../lib/api/services/PersistenceService.proto;../lib/api/services/PositionService.proto;../lib/api/services/ShellDiagnosticService.proto;../lib/api/services/ShellLatencyService.proto;../lib/api/services/SyntheticSecurityService.proto;../lib/domain_model/Security.proto;../lib/md/protos/co2.proto;../lib/sparkproto/SparkProtos.proto;api/build.sh;api/node_modules/cssstyle/scripts/run_tests.sh;api/node_modules/exit/test/fixtures/create-files.sh;api/node_modules/protobufjs/cli/node_modules/acorn-jsx/node_modules/acorn/bin/update_authors.sh;api/node_modules/watch/release.sh;api/quiet_build.sh;api/src/DynamicService.ts;api/src/ServiceContext.ts;api/src/bts_assert.ts;ui/src/Access.fs;ui/src/Accounts/ClipLimits.fs;ui/src/Accounts/ProductLimits.fs;ui/src/Accounts/SecurityLimits.fs;ui/src/Accounts/State.fs;ui/src/Accounts/Types.fs;ui/src/Accounts/View.fs;ui/src/App.fs;ui/src/Cmd.fs;ui/src/Controls/Button.fs;ui/src/Controls/DataGrid.fs;ui/src/Controls/Fields.fs;ui/src/Controls/Modal.fs;ui/src/Controls/ReactGrid.fs;ui/src/Controls/Table.fs;ui/src/Controls/nbsp.fs;ui/src/Global.fs;ui/src/Imports.fs;ui/src/Lens.fs;ui/src/Login/Login.fs;ui/src/Nav.fs;ui/src/State.fs;ui/src/Types.fs;ui/src/Users/State.fs;ui/src/Users/Types.fs;ui/src/Users/View.fs;ui/src/Util/EnumUtils.fs;ui/src/Util/JSMap.fs;ui/src/obj/Debug/netstandard2.0/ui.AssemblyInfo.fs) \ No newline at end of file