From aa39349fc4fa13b2dedaa0356e20e673d1823c25 Mon Sep 17 00:00:00 2001 From: John Colvin Date: Fri, 8 Mar 2019 14:09:53 -0600 Subject: [PATCH] SPARK-2092 Orderbook update ui supports spreader algos --- .../Modules/Trading/OrderBook/OrderBookVM.cs | 42 ++++++---- .../Orders/OrderTypeVMs/OrderTypeVMFactory.cs | 79 ++++++++++++++++--- .../Orders/OrderTypeVMs/SpreaderOrderVM.cs | 31 +++----- .../core/Utility/PropertyUpdateThrottle.cs | 59 ++++++++++++++ .../Order/AlgoContentTemplateSelector.xaml | 4 +- .../view/Spark/Utilities/OrderDataHelper.cs | 31 ++++++++ 6 files changed, 198 insertions(+), 48 deletions(-) create mode 100644 src/client/spark/core/Utility/PropertyUpdateThrottle.cs diff --git a/src/client/spark/Modules/Trading/OrderBook/OrderBookVM.cs b/src/client/spark/Modules/Trading/OrderBook/OrderBookVM.cs index 5229b1b50..bc6636ad8 100644 --- a/src/client/spark/Modules/Trading/OrderBook/OrderBookVM.cs +++ b/src/client/spark/Modules/Trading/OrderBook/OrderBookVM.cs @@ -72,6 +72,11 @@ namespace mbts.spark { concat_disposable( model.on_order_tuples.OnDispatcher().Subscribe(_row_collection.update), grid_settings_manager, + algo_vm.connect_data(Observable.Return(model)), + algo_vm.connect_data(Observable.FromEvent, SecurityImmutable?>( + cb => (oldval, newval) => cb(newval), + cb => _selected_algo_mon.common_spreader_sec_changed += cb, + cb => _selected_algo_mon.common_spreader_sec_changed -= cb)), algo_vm, Disposable.Create(() => { _columns.filter_changed -= on_filter_changed; @@ -92,8 +97,8 @@ namespace mbts.spark { algo_vm.algo_root = null; } } - void on_all_selected_orders_active_changed(bool all_active) { - algo_vm.editing_permissions_mask = all_active ? AlgoEditingPermissions.AllowEditValues : AlgoEditingPermissions.None; + void on_all_selected_orders_active_changed(bool oldval, bool newval) { + algo_vm.editing_permissions_mask = newval ? AlgoEditingPermissions.AllowEditValues : AlgoEditingPermissions.None; modify_selected_algos_async_cmd.RaiseCanExecuteChanged(); } } @@ -336,19 +341,23 @@ namespace mbts.spark { /// In terms of user-interaction, this means the user may edit the algo params for multiple orders at once if they are all active and share a common algo structure. /// class SelectedAlgosMonitor { + static readonly TimeSpan _throttle = TimeSpan.FromSeconds(0.1); + readonly PropertyUpdateThrottle _common_algo = new PropertyUpdateThrottle(_throttle); + readonly PropertyUpdateThrottle _common_spreader_sec = new PropertyUpdateThrottle(_throttle); + readonly PropertyUpdateThrottle _all_active = new PropertyUpdateThrottle(_throttle); + readonly SetMap<(AlgoStructureKey structure, SecurityImmutable? spreader_sec), OrderRecordImmutable> _algo_structure_index = new SetMap<(AlgoStructureKey structure, SecurityImmutable? spreader_sec), OrderRecordImmutable>(); readonly DynamicOrderHelper _dyn_helper; - readonly SetMap _algo_structure_index = new SetMap(); int _inactive_order_count; public SelectedAlgosMonitor(DynamicOrderHelper dyn_helper) => _dyn_helper = dyn_helper; - public event PropertyChange common_algo_changed; - public event Action all_active_changed; - - public CustomOrderDefinitionImmutable? common_algo => - _algo_structure_index.Count == 1 ? _algo_structure_index.First().First().Def.CustomDef : (CustomOrderDefinitionImmutable?)null; + public event PropertyChange common_algo_changed { add => _common_algo.changed += value; remove => _common_algo.changed -= value; } + public event PropertyChange common_spreader_sec_changed { add => _common_spreader_sec.changed += value; remove => _common_spreader_sec.changed -= value; } + public event PropertyChange all_active_changed { add => _all_active.changed += value; remove => _all_active.changed -= value; } + public CustomOrderDefinitionImmutable? common_algo => _algo_structure_index.Count == 1 ? _algo_structure_index.First().First().Def.CustomDef : (CustomOrderDefinitionImmutable?)null; + public SecurityImmutable? common_spreader_sec => _algo_structure_index.Count == 1 ? _algo_structure_index.First().Key.spreader_sec : null; public bool all_active => _inactive_order_count == 0 && _algo_structure_index.Count > 0; public void on_row_selected(OrderBookRowVM row) { @@ -365,11 +374,9 @@ namespace mbts.spark { var old_algo = common_algo; bool old_all_active = all_active; return Disposable.Create(() => { - var new_algo = common_algo; - if (!EqualityComparer.nullable_equals(old_algo, new_algo, OrderDataHelper.EqualsHelpers.equals)) - common_algo_changed?.Invoke(old_algo, new_algo); - if (old_all_active != all_active) - all_active_changed?.Invoke(!old_all_active); + _common_algo.value = common_algo; + _common_spreader_sec.value = common_spreader_sec; + _all_active.value = all_active; }); } @@ -381,16 +388,21 @@ namespace mbts.spark { } void _select_algo(OrderSecurityAlgoMeta osam) { - _algo_structure_index.add(AlgoStructureSerializer.structure_key(osam.order.Def.CustomDef, _dyn_helper.dynamic_type_index), osam.order); + _algo_structure_index.add((AlgoStructureSerializer.structure_key(osam.order.Def.CustomDef, _dyn_helper.dynamic_type_index), _get_spreader_sec(osam)), osam.order); if (!osam.order.is_active()) _inactive_order_count++; } void _unselect_algo(OrderSecurityAlgoMeta osam) { - _algo_structure_index.remove(AlgoStructureSerializer.structure_key(osam.order.Def.CustomDef, _dyn_helper.dynamic_type_index), osam.order); + _algo_structure_index.remove((AlgoStructureSerializer.structure_key(osam.order.Def.CustomDef, _dyn_helper.dynamic_type_index), _get_spreader_sec(osam)), osam.order); if (!osam.order.is_active()) _inactive_order_count--; } + + static SecurityImmutable? _get_spreader_sec(OrderSecurityAlgoMeta osam) => + OrderDataHelper.enumerate_algo_hierarchy(osam.order.Def.CustomDef.CloneToMutable()) + .Any(algo => algo.CustomCase == CustomOrderDefinition.CustomOneofCase.Spreader) ? + osam.security : null; } } } \ 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 879070ed1..f372882f2 100644 --- a/src/client/spark/Orders/OrderTypeVMs/OrderTypeVMFactory.cs +++ b/src/client/spark/Orders/OrderTypeVMs/OrderTypeVMFactory.cs @@ -98,7 +98,11 @@ namespace mbts.spark { } #region OrderTypeVM Base Classes - abstract class OrderTypeVMBase : DisposableViewModelBase, IAlgoVM, IObserver, IObserver, IObserver where TMutable : class, IMessage, new() where TImmutable : struct, IImmutableMessage { + abstract class OrderTypeVMBase : DisposableViewModelBase, IAlgoVM, + IObserver, + IObserver, + IObserver, + IObserver where TMutable : class, IMessage, new() where TImmutable : struct, IImmutableMessage { static readonly FieldDescriptor _descriptor = OrderTypeVMUtils.find_descriptor(); readonly IDictionary _children_vm = new ExpandoObject(); protected CompositeDisposable _resources = new CompositeDisposable(); @@ -108,6 +112,7 @@ namespace mbts.spark { readonly ObservableAutoWrapper _child_algo_selectors; readonly Property _is_expanded; readonly DeferrableActionWrapper _def_change_notifier; + SecurityInput _security_input; AlgoEditingPermissions _editing_permissions; IDisposable _pending_load; @@ -170,8 +175,8 @@ namespace mbts.spark { public virtual bool is_expanded { get => _is_expanded.Value; set => _is_expanded.Value = value; } // protected (observed props) protected OrdersModel model { get; private set; } - protected SecurityMD? security_md { get; private set; } - protected SecurityImmutable? security => security_md?.security; + protected SecurityMD? security_md => _security_input.security_md; + protected SecurityImmutable? security => _security_input.security; public void assign_to(AlgoDef value) { var algo = new TMutable(); @@ -347,6 +352,7 @@ namespace mbts.spark { protected virtual void on_model_internal() { } protected virtual void on_security_md_internal() { } + protected virtual void on_security_internal() { } // model void IObserver.OnNext(OrdersModel model) { @@ -366,19 +372,40 @@ namespace mbts.spark { void IObserver.OnCompleted() { } // security md void IObserver.OnNext(SecurityMD? security_md) { - this.security_md = security_md; - using (_def_change_notifier.defer_execution()) { - on_security_md_internal(); - _populate_selectors(); - } + using (_def_change_notifier.defer_execution()) + using (_update_sec_input()) + _security_input = _security_input.with_security_md(security_md); } void IObserver.OnError(Exception error) { } void IObserver.OnCompleted() { } + // security + void IObserver.OnNext(SecurityImmutable? security) { + using (_def_change_notifier.defer_execution()) + using (_update_sec_input()) + _security_input = _security_input.with_security(security); + } + + void IObserver.OnError(Exception error) { } + void IObserver.OnCompleted() { } // editing permissions void IObserver.OnNext(AlgoEditingPermissions editing_permissions) => this.editing_permissions = editing_permissions; void IObserver.OnError(Exception error) { } void IObserver.OnCompleted() { } + IDisposable _update_sec_input() { + var old_sec = security; + var old_sec_md = security_md; + return Disposable.Create(() => { + if (security != old_sec) { + on_security_internal(); + if (_editing_permissions.HasFlag(AlgoEditingPermissions.AllowEditStructure)) + _populate_selectors(); + } + if (security_md != old_sec_md) + on_security_md_internal(); + }); + } + IDisposable _selector_data_hooks; void _populate_selectors() { if (model != null && security.HasValue) { @@ -396,6 +423,40 @@ namespace mbts.spark { } } + // security input may come in 2 forms: + // 1) SecurityImmutable? + // 2) SecurityMD? + // This structure resolves conflicts that may arise from the different inputs + struct SecurityInput { + readonly SecurityMD? _raw_security_md; + readonly SecurityImmutable? _raw_security; + + public readonly SecurityMD? security_md; + public readonly SecurityImmutable? security; + + SecurityInput(SecurityImmutable? raw_security, SecurityMD? raw_security_md, SecurityImmutable? security, SecurityMD? security_md) { + _raw_security = raw_security; + _raw_security_md = raw_security_md; + this.security_md = security_md; + this.security = security; + } + + public SecurityInput with_security(SecurityImmutable? security) => + new SecurityInput( + security, + _raw_security_md, + security, + security.HasValue && _raw_security_md.HasValue && _raw_security_md.Value.security.Id != security.Value.Id ? + null : _raw_security_md); + + public SecurityInput with_security_md(SecurityMD? security_md) => + new SecurityInput( + _raw_security, + security_md, + security_md?.security ?? _raw_security, + security_md); + } + struct AlgoSelectorData { public readonly string prop_name; public readonly AlgoSelector selector; @@ -484,4 +545,4 @@ namespace mbts.spark { } #endregion } -} \ No newline at end of file +} diff --git a/src/client/spark/Orders/OrderTypeVMs/SpreaderOrderVM.cs b/src/client/spark/Orders/OrderTypeVMs/SpreaderOrderVM.cs index ad61e47cc..9c7e49df5 100644 --- a/src/client/spark/Orders/OrderTypeVMs/SpreaderOrderVM.cs +++ b/src/client/spark/Orders/OrderTypeVMs/SpreaderOrderVM.cs @@ -1,4 +1,5 @@ using bts; +using bts.api; using bts.api.orderman; using mbts.ui.controls; using mbts.ui.utility; @@ -11,11 +12,9 @@ using SpreaderDefi = bts.api.orderman.SpreaderOrderDefinitionImmutable; namespace mbts.spark { static partial class OrderTypeVMFactory { - class SpreaderOrderVM : OrderTypeVMBase, IAlgoVM, ILazyAlgo, IObserver, IObserver { + class SpreaderOrderVM : OrderTypeVMBase, IAlgoVM, ILazyAlgo, IObserver { readonly Property _template; readonly StrategyEditorWrapper _strategy_editor_wrapper; - OrdersModel _model; - SecurityMD? _security_md; IDisposable _strategy_sub; public SpreaderOrderVM(AlgoKey key, string display_name, AlgoEditingPermissions editing_permissions) @@ -80,27 +79,15 @@ namespace mbts.spark { set_error(nameof(template), "Waiting for security and/or strategy.", true); else clear_error(nameof(template)); } - - void IObserver.OnCompleted() { } - void IObserver.OnError(Exception error) { } - void IObserver.OnNext(SecurityMD? security_md) { - _security_md = security_md; - _reset_strategy_sub(); - } - - void IObserver.OnCompleted() { } - void IObserver.OnError(Exception error) { } - void IObserver.OnNext(OrdersModel model) { - _model = model; - _reset_strategy_sub(); - } + protected override void on_model_internal() => _reset_strategy_sub(); + protected override void on_security_internal() => _reset_strategy_sub(); void _reset_strategy_sub() { Disposable.dispose(ref _strategy_sub); - if (_model != null) { - if (_security_md.HasValue) { - _strategy_sub = _model.spreader.security_strategy(_security_md.Value.security.Id) - .Select(strategy => new SpreaderDefi?(_model.factory.spreader_orderi(_security_md.Value.security, strategy))) + if (model != null) { + if (security.HasValue) { + _strategy_sub = model.spreader.security_strategy(security.Value.Id) + .Select(strategy => new SpreaderDefi?(model.factory.spreader_orderi(security.Value, strategy))) .OnDispatcher() .Subscribe(s => { is_loaded = true; @@ -140,7 +127,7 @@ namespace mbts.spark { } } - OrdersModel _model => _owner._model; + OrdersModel _model => _owner.model; SpreaderDefi? _template { get => _owner.template; set => _owner.template = value; } public void Dispose() { diff --git a/src/client/spark/core/Utility/PropertyUpdateThrottle.cs b/src/client/spark/core/Utility/PropertyUpdateThrottle.cs new file mode 100644 index 000000000..458ccf754 --- /dev/null +++ b/src/client/spark/core/Utility/PropertyUpdateThrottle.cs @@ -0,0 +1,59 @@ +using mbts.ui.shell; +using System; +using System.Collections.Generic; +using System.Threading.Tasks; + +namespace bts.utils { + class PropertyUpdateThrottle { + readonly TimeSpan _throttle; + readonly IEqualityComparer _comparer; + bool _busy, _dirty, _scheduled; + T _value, _last_fired; + + public PropertyUpdateThrottle(TimeSpan throttle) + : this(throttle, EqualityComparer.Default) { } + + public PropertyUpdateThrottle(TimeSpan throttle, IEqualityComparer comparer) { + _throttle = throttle; + _comparer = comparer; + } + + public T value { + get => _value; + set { + if (!_comparer.Equals(_value, value)) { + _value = value; + _fire(); + } + } + } + + public event PropertyChange changed; + + async void _fire() { + if (!_busy) { + _busy = true; + _dirty = false; + var old = _last_fired; + _last_fired = _value; + changed?.Invoke(old, _value); + await Task.Delay(_throttle); + _busy = false; + } else if (!_dirty) { + _dirty = true; + _delay_fire(); + } + } + + async void _delay_fire() { + if (!_scheduled) { + _scheduled = true; + await Task.Delay(100); + if (_dirty) { + _dirty = false; + changed?.Invoke(_last_fired, _value); + } + } + } + } +} \ No newline at end of file diff --git a/src/client/spark/view/Controls/Order/AlgoContentTemplateSelector.xaml b/src/client/spark/view/Controls/Order/AlgoContentTemplateSelector.xaml index c323c7a8c..a601c8793 100644 --- a/src/client/spark/view/Controls/Order/AlgoContentTemplateSelector.xaml +++ b/src/client/spark/view/Controls/Order/AlgoContentTemplateSelector.xaml @@ -946,7 +946,7 @@ - + - + diff --git a/src/client/spark/view/Spark/Utilities/OrderDataHelper.cs b/src/client/spark/view/Spark/Utilities/OrderDataHelper.cs index e72199762..f03bcd1b4 100644 --- a/src/client/spark/view/Spark/Utilities/OrderDataHelper.cs +++ b/src/client/spark/view/Spark/Utilities/OrderDataHelper.cs @@ -308,6 +308,37 @@ namespace mbts.spark.utilities { f.FieldType == FieldType.Message && f.MessageType.ClrType == typeof(CustomOrderDefinition); } + public static IEnumerable enumerate_algo_children(CustomOrderDefinition algo) { + switch (algo.CustomCase) { + case CustomOrderDefinition.CustomOneofCase.Dynamic: + foreach (var child in enumerate_dynamic_algo_children(algo.Dynamic)) + yield return child; + break; + default: + foreach (var cad in get_child_algo_data(algo)) { + if (cad.get_child(algo) is CustomOrderDefinition child) + yield return child; + } + break; + } + } + + public static IEnumerable enumerate_dynamic_algo_children(DynamicOrderDefinition dynamic_algo) { + foreach (var kv in dynamic_algo.OrderParams) { + if (kv.Value.ValueCase == DynamicOrderParam.ValueOneofCase.AsOrderDef) + yield return kv.Value.AsOrderDef; + } + } + + public static IEnumerable enumerate_algo_hierarchy(CustomOrderDefinition algo) { + yield return algo; + foreach (var child in enumerate_algo_children(algo)) { + yield return child; + foreach (var grand_child in enumerate_algo_hierarchy(child)) + yield return grand_child; + } + } + public static IReadOnlyList get_child_algo_data(CustomOrderDefinitionImmutable defi) => get_child_algo_data(defi.CloneToMutable());