From aaa2bcd20dc308e3786929ba2d5469133b7f3c54 Mon Sep 17 00:00:00 2001 From: John Colvin Date: Mon, 20 Aug 2018 19:22:30 -0400 Subject: [PATCH] SPARK-1550 Position in Ladder --- src/client/spark/Orders/OrdersModel.cs | 20 ++++++++++++++++++- .../core/GlobalExtensions/DoubleHelper.cs | 14 +++++++++++++ .../spark/view/Spark/Core/SparkUtils.cs | 5 +++++ .../spark/view/Spark/Ladder/LadderVM.cs | 11 ++++++++-- .../spark/view/Spark/Ladder/LadderView.xaml | 13 +++++++++++- 5 files changed, 59 insertions(+), 4 deletions(-) diff --git a/src/client/spark/Orders/OrdersModel.cs b/src/client/spark/Orders/OrdersModel.cs index 4a5b2a51a..261a845d8 100644 --- a/src/client/spark/Orders/OrdersModel.cs +++ b/src/client/spark/Orders/OrdersModel.cs @@ -1,6 +1,7 @@ using bts.api; using bts.api.core; using bts.api.orderman; +using bts.api.position; using bts.spark; using mbts.collections; using mbts.spark.utilities; @@ -19,20 +20,24 @@ namespace mbts.spark { readonly IBus _bus; readonly OrderFactory _factory; readonly OrderService _service; + readonly PositionService _position_service; readonly SpreaderModel _spreader; readonly ErrorReporter _error; readonly OrderActivitySummarizer _activity; readonly TableUpdateDemuxer _security_orders_demuxer; + readonly TableUpdateDemuxer _security_positions_demuxer; IDisposable _dispose; [ImportingConstructor] - private OrdersModel(IBus bus, OrderFactory factory, OrderService service, SpreaderModel spreader, ErrorReporter error) { + private OrdersModel(IBus bus, OrderFactory factory, OrderService service, PositionService position_service, SpreaderModel spreader, ErrorReporter error) { _bus = bus; _factory = factory; _service = service; + _position_service = position_service; _spreader = spreader; _error = error; on_orders = _service.subscribei_orders().Table().Cache(); + on_positions = _position_service.subscribei_position_data().Table().Cache(); IReadOnlyDictionary empty_orders = new Dictionary(); working_orders = on_orders.Scan(empty_orders, (worders, table) => _update_working_orders(worders, table)).Replay(1).RefCount(); @@ -41,8 +46,10 @@ namespace mbts.spark { _activity = new OrderActivitySummarizer(bus, on_orders); on_fill_tuples = FillTupleSource.create_observable(bus.subscribe(SparkTags.SecuritiesTag), on_orders, on_fills); _security_orders_demuxer = new TableUpdateDemuxer(o => o.Key, o => o.security_key()); + _security_positions_demuxer = new TableUpdateDemuxer(o => o.RecordId, p => p.SecurityId); _dispose = Disposable.Create( on_orders.Subscribe(_security_orders_demuxer), + on_positions.Subscribe(_security_positions_demuxer), _bus.subscribe(SparkTags.CreateOrder).Subscribe(a => send_order(a.definition, a.price, a.size).ignore()), _bus.subscribe(SparkTags.RecreateOrder).Subscribe(a => a.ForEach(x => send_order(x.definition, x.price, x.size).ignore())), _bus.subscribe(SparkTags.CancelOrders).Subscribe(x => cancel_orders(x).ignore()), @@ -59,9 +66,13 @@ namespace mbts.spark { public IObservable> on_fills { get; } public IObservable> on_fill_tuples { get; } public IObservable> working_orders { get; } + public IObservable> on_positions { get; } public IObservable> on_security_orders(uint security) => _security_orders_demuxer.subscribe(security); + + public IObservable> on_security_positions(uint security) => + _security_positions_demuxer.subscribe(security); #endregion #region Input @@ -176,6 +187,13 @@ namespace mbts.spark { .Select(t => t.state.Any(kv => kv.Value.is_active())) .StartWith(false); + public static IObservable security_total_position(this OrdersModel orders_model, uint security_key, string account) => + // PositionUnit.Account is actually the clearing account; this code assumes that clearing account and account are equivalent. + // However, it still must match account to PositionUnit.Account.Trimmed() because the exchange likes to slap spaces on the end. + orders_model.on_security_positions(security_key).filter(p => p.Account.Trim() == account).Select(table => + // the state should always have 1 element, since the PositionUnit-identity is security + account + table.state.Values.Sum(p => p.total_pos())); + public static IObservable> on_security_root_orders(this OrdersModel orders_model, uint security_key) => orders_model.on_security_orders(security_key).Scan(new RootOrderData(), (rod, table) => { // ** using HashSet/List pairs, rather than a Dictionary, because TableUpdateTransform calls for IReadOnlyLists, while Dictionary.Values only implements IReadOnlyCollection. diff --git a/src/client/spark/core/GlobalExtensions/DoubleHelper.cs b/src/client/spark/core/GlobalExtensions/DoubleHelper.cs index 724b44b2d..44151f91f 100644 --- a/src/client/spark/core/GlobalExtensions/DoubleHelper.cs +++ b/src/client/spark/core/GlobalExtensions/DoubleHelper.cs @@ -10,5 +10,19 @@ namespace mbts { public static double CoerceNaNOrInfinity(double value) => CoerceNaNOrInfinity(value, 0.0); public static double CoerceNaNOrInfinity(double value, double if_nan_or_infinity) => IsNaNOrInfinity(value) ? if_nan_or_infinity : value; public static double MultiplyIfNotNaN(double value, double mult) => double.IsNaN(mult) ? value : value * mult; + public static uint safe_convert_to_uint(double value) { + switch (value) { + case double.PositiveInfinity: return uint.MaxValue; + case double.NegativeInfinity: + case double.NaN: return 0; + default: + if (value < 0) { + return 0; + } else if (value > uint.MaxValue) { + return uint.MaxValue; + } + return (uint)value; + } + } } } \ No newline at end of file diff --git a/src/client/spark/view/Spark/Core/SparkUtils.cs b/src/client/spark/view/Spark/Core/SparkUtils.cs index 20e5f6b6e..a32078128 100644 --- a/src/client/spark/view/Spark/Core/SparkUtils.cs +++ b/src/client/spark/view/Spark/Core/SparkUtils.cs @@ -1,5 +1,6 @@ using bts.api; using bts.api.core; +using bts.api.position; using mbts.collections.extensions; using mbts.ui; using mbts.utils; @@ -74,6 +75,10 @@ namespace mbts.spark { return (smd, smd.security.Id == security_key); } + public static double sod_pos(this PositionUnitImmutable position_unit) => position_unit.SodLongPos - position_unit.SodShortPos; + public static double day_pos(this PositionUnitImmutable position_unit) => position_unit.DayLongPos - position_unit.DayShortPos; + public static double total_pos(this PositionUnitImmutable position_unit) => position_unit.sod_pos() + position_unit.day_pos(); + public static void lazy_assert(bool test, Func get_msg) { if (!test) Logger.assert(false, get_msg()); diff --git a/src/client/spark/view/Spark/Ladder/LadderVM.cs b/src/client/spark/view/Spark/Ladder/LadderVM.cs index cace4d170..c2e6151a6 100644 --- a/src/client/spark/view/Spark/Ladder/LadderVM.cs +++ b/src/client/spark/view/Spark/Ladder/LadderVM.cs @@ -21,7 +21,7 @@ namespace mbts.spark { class LadderVM : DisposableViewModelBase, IKeyListener { readonly IBus _bus; readonly LadderModel _model; - readonly Property _high, _low, _change, _open, _settlement; + readonly Property _high, _low, _change, _open, _settlement, _position; readonly Property _volume; readonly Property _auto_clear; readonly Property _bid_background, _bid_foreground, _ask_background, _ask_foreground; @@ -36,11 +36,15 @@ namespace mbts.spark { _equality = TickUtils.tick_equality_comparer(_model.security.ContractSpec); _slot_price_vs_orders = new Dictionary(_equality); _settings_manager = settings_manager; + order_properties_vm = new OrderPropertiesEditorVM(_model.order_gateway, Observable.Return(_model.security_md), _settings_manager.subscribe_current().Select(s => new InlineOrderProperties(_model.order_gateway, s))); _high = backing(nameof(high), double.NaN); _low = backing(nameof(low), double.NaN); _change = backing(nameof(change), double.NaN); _open = backing(nameof(open), double.NaN); _settlement = backing(nameof(settlement), double.NaN); + _position = backing(nameof(position), order_properties_vm.observe_account(true) + .SelectMany(a => _model.order_gateway.security_total_position(security.Id, a)) + .OnDispatcher()); _volume = backing(nameof(volume), 0ul); _auto_clear = backing(nameof(auto_clear), false); _status = backing(nameof(status_message), ""); @@ -53,6 +57,7 @@ namespace mbts.spark { Property brush_prop(string name, Func get_brush) => backing(name, settings_manager.subscribe_current().OnDispatcher(), get_brush); // + position_click_command = new DelegateCommand(() => size_calculator.value = DoubleHelper.safe_convert_to_uint(Math.Abs(position))); send_order_command = new DelegateCommand<(double price, Side? side, bool size_from_alternate)>(send_order); cancel_command = new AsyncCommand(_model.cancel_orders, true); show_book_command = new DelegateCommand(_show_book); @@ -71,7 +76,7 @@ namespace mbts.spark { _dispose = Disposable.Create( settings_command, - order_properties_vm = new OrderPropertiesEditorVM(model.order_gateway, Observable.Return(model.security_md), _settings_manager.subscribe_current().Select(s => new InlineOrderProperties(model.order_gateway, s))), + order_properties_vm, size_calculator = new CalculatorLogic(0, _settings_manager.subscribe_current().Select(s => s.calculator_buttons)), bus.subscribe(SparkTags.LogActivity).OnDispatcher().Subscribe(status_update), model.order_gateway.working_orders.OnDispatcher().Subscribe(working => { @@ -175,6 +180,7 @@ namespace mbts.spark { Property _status_brush; public Brush status_background => _status_brush.Value; + public ICommand position_click_command { get; } public ICommand status_click_command => SparkCommands.ShowActivityLog.ICommand(_bus); public async void status_update(Activity a) { @@ -195,6 +201,7 @@ namespace mbts.spark { public double change => _change.Value; public double open => _open.Value; public double settlement => _settlement.Value; + public double position => _position.Value; public ulong volume => _volume.Value; public bool auto_clear { get => _auto_clear.Value; set => _auto_clear.set(value); } public CalculatorLogic size_calculator { get; } diff --git a/src/client/spark/view/Spark/Ladder/LadderView.xaml b/src/client/spark/view/Spark/Ladder/LadderView.xaml index 1f3feaabf..933b75260 100644 --- a/src/client/spark/view/Spark/Ladder/LadderView.xaml +++ b/src/client/spark/view/Spark/Ladder/LadderView.xaml @@ -372,11 +372,22 @@ ItemsSource="{Binding order_types}" SelectedItem="{Binding selected_order_type}" /> +