From 7a712fbaf29fc76fca3791e78d701d447edc0be6 Mon Sep 17 00:00:00 2001 From: John Colvin Date: Fri, 26 Jan 2018 15:52:33 -0500 Subject: [PATCH] MarketView - reduce slowness with large security sets --- .../Market/MarketView/MarketViewRow.cs | 166 +++++++++++------- .../Modules/Market/MarketView/MarketViewVM.cs | 11 +- .../GenericCollectionExtensions.cs | 72 ++++++++ .../spark/view/Spark/Core/OrderUtils.cs | 2 +- .../spark/view/Utility/CommandHelper.cs | 42 +++++ 5 files changed, 229 insertions(+), 64 deletions(-) create mode 100644 src/client/spark/view/Utility/CommandHelper.cs diff --git a/src/client/spark/Modules/Market/MarketView/MarketViewRow.cs b/src/client/spark/Modules/Market/MarketView/MarketViewRow.cs index fe7c56fd9..dc595bb3d 100644 --- a/src/client/spark/Modules/Market/MarketView/MarketViewRow.cs +++ b/src/client/spark/Modules/Market/MarketView/MarketViewRow.cs @@ -1,7 +1,7 @@ using bts.api; using bts.api.core; using bts.api.orderman2; -using mbts.collections; +using mbts.co2; using mbts.spark.utilities; using mbts.ui; using mbts.ui.spark; @@ -20,75 +20,68 @@ namespace mbts.spark { public class Row : SecurityMDRow, IDisposable { + readonly IBus _bus; + readonly AnimationManager _animation = new AnimationManager(); + readonly ObservableCollection _orders = new ObservableCollection(); + readonly ObservableFilterCollection _working_orders, _working_bids, _working_asks; + readonly Property _bid_price, _ask_price, _last_price, _day_change, _settlement, _session_high, _session_low; + readonly Property _position; + readonly Property _bid_size, _ask_size, _last_size; + readonly Property _volume; + readonly Property _inside_bids, _inside_asks; + readonly DelegateProperty _working_bids_active, _working_asks_active; + readonly DelegateProperty _working_bid_volume, _working_ask_volume; + readonly DelegateProperty _working_best_bid, _working_best_ask; + ulong _bbo_ref, _last_ref; + public Row(IBus bus, SecurityMD smd, IObservable> orders) : base(smd) { _bus = bus; - var position_src = orders.Scan(0, (pos, tu) => { - foreach (var o in tu.added) pos += (int)o.State.FilledQuantity; - foreach (var o in tu.removed) pos -= (int)o.State.FilledQuantity; - return pos; - }); - _position = backing(nameof(position), position_src, 0); - _orders = new TableUpdateCollection(orders.ObserveOnDispatcher()); - var md_pump = new SecurityMDPump(smd.md); - _bid_price = backing(nameof(bid_price), md_pump.bid_price, double.NaN); - _bid_size = backing(nameof(bid_size), md_pump.bid_size, (ushort)0); - _ask_price = backing(nameof(ask_price), md_pump.ask_price, double.NaN); - _ask_size = backing(nameof(ask_size), md_pump.ask_size, (ushort)0); - _last_price = backing(nameof(last_price), md_pump.last_price, double.NaN); - _last_size = backing(nameof(last_size), md_pump.last_size, (ushort)0); - _settlement = backing(nameof(settlement), md_pump.settlement_price, double.NaN); - _volume = backing(nameof(volume), md_pump.volume, (ulong)0); + _position = backing(nameof(position), 0); + _bid_price = backing(nameof(bid_price), double.NaN); + _bid_size = backing(nameof(bid_size), (ushort)0); + _ask_price = backing(nameof(ask_price), double.NaN); + _ask_size = backing(nameof(ask_size), (ushort)0); + _last_price = backing(nameof(last_price), double.NaN); + _last_size = backing(nameof(last_size), (ushort)0); + _settlement = backing(nameof(settlement), double.NaN); + _volume = backing(nameof(volume), 0ul); // _working_orders = new ObservableFilterCollection(_orders, o => o.is_active()); _working_bids = new ObservableFilterCollection(_working_orders, o => o.Def.Side == Side.Buy); _working_asks = new ObservableFilterCollection(_working_orders, o => o.Def.Side == Side.Sell); - _working_bids_sorted = new ObservableSortedCollection(_working_bids, Comparer.Create((x, y) => y.State.Price.CompareTo(x.State.Price))); - _working_asks_sorted = new ObservableSortedCollection(_working_asks, Comparer.Create((x, y) => x.State.Price.CompareTo(y.State.Price))); - _inside_bids = new ObservableFilterCollection(_working_bids_sorted, o => o.State.Price >= bid_price); - _inside_asks = new ObservableFilterCollection(_working_asks_sorted, o => o.State.Price <= ask_price); - var day_change_src = md_pump.settlement_price.CombineLatest(md_pump.last_price, (s, l) => l - s); - var working_best_bid_src = _working_bids_sorted.ObserveSelf(true).Select(wbs => wbs.Count == 0 ? double.NaN : _working_bids_sorted.First().State.Price).DistinctUntilChanged(); - var working_best_ask_src = _working_asks_sorted.ObserveSelf(true).Select(was => was.Count == 0 ? double.NaN : _working_asks_sorted.First().State.Price).DistinctUntilChanged(); - var working_bids_volume_src = _working_bids.ObserveSelf(true).Select(wbs => wbs.Aggregate(0u, (v, o) => v + o.State.Quantity - o.State.FilledQuantity)).DistinctUntilChanged(); - var working_asks_volume_src = _working_asks.ObserveSelf(true).Select(was => was.Aggregate(0u, (v, o) => v + o.State.Quantity - o.State.FilledQuantity)).DistinctUntilChanged(); - // - _day_change = backing(nameof(day_change), day_change_src, double.NaN); - _session_low = backing(nameof(session_low), md_pump.session_low, double.NaN); - _session_high = backing(nameof(session_high), md_pump.session_high, double.NaN); - _working_bid_volume = backing(nameof(working_bid_volume), working_bids_volume_src); - _working_ask_volume = backing(nameof(working_ask_volume), working_asks_volume_src); - _working_bids_text = backing(nameof(working_bids_text), working_bids_volume_src.CombineLatest(working_best_bid_src, (size, price) => size == 0u ? string.Empty : $"{smd.security.ContractSpec.format(price)} @ {size}"), string.Empty); - _working_asks_text = backing(nameof(working_asks_text), working_asks_volume_src.CombineLatest(working_best_ask_src, (size, price) => size == 0u ? string.Empty : $"{smd.security.ContractSpec.format(price)} @ {size}"), string.Empty); - _working_best_bid = backing(nameof(working_best_bid), working_best_bid_src); - _working_best_ask = backing(nameof(working_best_ask), working_best_ask_src); + _working_bids_active = backing(nameof(working_bids_active), () => _working_bids.Count > 0); + _working_asks_active = backing(nameof(working_asks_active), () => _working_asks.Count > 0); + _working_best_bid = backing(nameof(working_best_bid), () => _working_bids.Max(o => o.State.Price, double.NaN)); + _working_best_ask = backing(nameof(working_best_ask), () => _working_asks.Min(o => o.State.Price, double.NaN)); + _inside_bids = backing(nameof(inside_bids), _working_best_bid.Select(vals => vals.newval >= bid_price), false); + _inside_asks = backing(nameof(inside_asks), _working_best_ask.Select(vals => vals.newval <= ask_price), false); + _working_bid_volume = backing(nameof(working_bid_volume), () => _working_bids.Sum(o => o.working_quantity())); + _working_ask_volume = backing(nameof(working_ask_volume), () => _working_asks.Sum(o => o.working_quantity())); + // + _day_change = backing(nameof(day_change), double.NaN); + _session_low = backing(nameof(session_low), double.NaN); + _session_high = backing(nameof(session_high), double.NaN); _animation.AnimationsChanged += _animation_AnimationsChanged; - _dispose = Disposable.Create(md_pump, _orders, - position_src.PairWithPrevious().Select(vals => vals.curr > vals.prev).ObserveOnDispatcher().Subscribe(_animation.animate_position), - _working_bids.ObserveSelf(true).Select(_ => _working_bids.Count > 0).DistinctUntilChanged().ObserveOnDispatcher().Subscribe(_animation.animate_working_bids_active), - _working_asks.ObserveSelf(true).Select(_ => _working_asks.Count > 0).DistinctUntilChanged().ObserveOnDispatcher().Subscribe(_animation.animate_working_asks_active), - _inside_bids.ObserveSelf(true).Select(_ => _inside_bids.Count > 0).DistinctUntilChanged().ObserveOnDispatcher().Subscribe(_animation.animate_working_bids_inside), - _inside_asks.ObserveSelf(true).Select(_ => _inside_asks.Count > 0).DistinctUntilChanged().ObserveOnDispatcher().Subscribe(_animation.animate_working_asks_inside), - day_change_src.PairWithPrevious().Select(vals => vals.curr > vals.prev).ObserveOnDispatcher().Subscribe(_animation.animate_day_change), + _dispose = Disposable.Create( + _working_bids.Subscribe(_working_bids_CollectionChanged), + _working_asks.Subscribe(_working_asks_CollectionChanged), + _position.ObserveOnDispatcher().Subscribe(vals => _animation.animate_position(vals.newval > vals.oldval)), + _working_bids_active.ObserveOnDispatcher().Subscribe(vals => _animation.animate_working_bids_active(vals.newval)), + _working_asks_active.ObserveOnDispatcher().Subscribe(vals => _animation.animate_working_asks_active(vals.newval)), + _inside_bids.ObserveOnDispatcher().Subscribe(vals => _animation.animate_working_bids_inside(vals.newval)), + _inside_asks.ObserveOnDispatcher().Subscribe(vals => _animation.animate_working_asks_inside(vals.newval)), + _day_change.ObserveOnDispatcher().Subscribe(vals => _animation.animate_day_change(vals.newval > vals.oldval)), + orders.Subscribe(_on_orders), Disposable.Create(() => _animation.AnimationsChanged -= _animation_AnimationsChanged) ); } - readonly IBus _bus; - readonly AnimationManager _animation = new AnimationManager(); - readonly ObservableSortedCollection _working_bids_sorted, _working_asks_sorted; - readonly TableUpdateCollection _orders; - readonly ObservableFilterCollection _working_orders, _working_bids, _working_asks, _inside_bids, _inside_asks; - readonly Property _bid_price, _ask_price, _last_price, _day_change, _settlement, _session_high, _session_low, _working_best_bid, _working_best_ask; - readonly Property _position; - readonly Property _bid_size, _ask_size, _last_size; - readonly Property_volume; - readonly Property _working_bid_volume, _working_ask_volume; - readonly Property _working_bids_text, _working_asks_text; - - void _animation_AnimationsChanged() { RaisePropertyChanged(nameof(animations)); } - #region Bound Properties + public bool inside_bids => _inside_bids.Value; + public bool inside_asks => _inside_asks.Value; + public bool working_bids_active => _working_bids.Count > 0; + public bool working_asks_active => _working_asks.Count > 0; public int position => _position.Value; public double last_price => _last_price.Value; public ushort last_size => _last_size.Value; @@ -103,9 +96,9 @@ namespace mbts.spark public double session_high => _session_high.Value; public uint working_bid_volume => _working_bid_volume.Value; public uint working_ask_volume => _working_ask_volume.Value; - public string working_bids_text => _working_bids_text.Value; - public string working_asks_text => _working_asks_text.Value; - public double working_best_bid => _working_best_ask.Value; + public string working_bids_text => working_bid_volume == 0u ? string.Empty : $"{security.ContractSpec.format(working_best_bid)} @ {working_bid_volume}"; + public string working_asks_text => working_ask_volume == 0u ? string.Empty : $"{security.ContractSpec.format(working_best_ask)} @ {working_ask_volume}"; + public double working_best_bid => _working_best_bid.Value; public double working_best_ask => _working_best_ask.Value; public IEnumerable animations => _animation.get_animations(); public ICommand show_ladder => new DelegateCommand(() => _bus.post(SparkTags.ShowLadderTag, md)); @@ -137,6 +130,59 @@ namespace mbts.spark } }); #endregion + + public void refresh() { + if (md.md.get_bbo(ref _bbo_ref, out NativePriceSize b, out NativePriceSize a)) { + _bid_price.set(b.price); + _bid_size.set(b.size); + _ask_price.set(a.price); + _ask_size.set(a.size); + } + if (md.md.get_last_trade(ref _last_ref, out NativePriceSize ps, out DateTime ts, out ulong v)) { + _last_price.set(ps.price); + _last_size.set(ps.size); + _volume.set(v); + } + _settlement.set(md.md.settlement_price); + _session_low.set(md.md.session_low); + _session_high.set(md.md.session_high); + _day_change.set(last_price - session_high); + } + + void _on_orders(TableUpdate orders) { + uint removed_buys = 0, removed_sells = 0, added_buys = 0, added_sells = 0; + foreach (var o in orders.removed) { + _orders.Remove(o); + switch (o.Def.Side) { + case Side.Buy: removed_buys += o.State.FilledQuantity; break; + case Side.Sell: removed_sells += o.State.FilledQuantity; break; + } + } + foreach (var o in orders.added) { + _orders.Add(o); + switch (o.Def.Side) { + case Side.Buy: added_buys += o.State.FilledQuantity; break; + case Side.Sell: added_sells += o.State.FilledQuantity; break; + } + } + _position.Value += (int)(removed_sells + added_buys - removed_buys - added_sells); + } + + void _working_bids_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e) { + _working_best_bid.refresh(); + _working_bid_volume.refresh(); + _working_bids_active.refresh(); + RaisePropertyChanged(nameof(working_bids_text)); + } + + void _working_asks_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e) { + _working_best_ask.refresh(); + _working_ask_volume.refresh(); + _working_asks_active.refresh(); + RaisePropertyChanged(nameof(working_asks_text)); + } + + void _animation_AnimationsChanged() => RaisePropertyChanged(nameof(animations)); } } } \ No newline at end of file diff --git a/src/client/spark/Modules/Market/MarketView/MarketViewVM.cs b/src/client/spark/Modules/Market/MarketView/MarketViewVM.cs index a8a1a9f19..88f9600fe 100644 --- a/src/client/spark/Modules/Market/MarketView/MarketViewVM.cs +++ b/src/client/spark/Modules/Market/MarketView/MarketViewVM.cs @@ -31,10 +31,15 @@ namespace mbts.spark working_ask_volume_filter.load_state(filters.working_ask_volume); volume_filter.load_state(filters.volume); } - _resources.Add(hook_filters(rows_view, + _resources.AddRange(hook_filters(rows_view, security_filter, bid_price_filter, bid_size_filter, ask_price_filter, ask_size_filter, last_price_filter, last_size_filter, day_change_filter, - settlement_filter, session_low_filter, session_high_filter, position_filter, working_bid_volume_filter, working_ask_volume_filter, volume_filter - )); + settlement_filter, session_low_filter, session_high_filter, position_filter, working_bid_volume_filter, working_ask_volume_filter, volume_filter), + WorkLoop.Start(_refresh) + ); + } + + void _refresh() { + foreach (var r in _rows_vm) r.refresh(); } readonly IBus _bus; diff --git a/src/client/spark/core/GlobalExtensions/GenericCollectionExtensions.cs b/src/client/spark/core/GlobalExtensions/GenericCollectionExtensions.cs index cbff6c71d..1273a9f3c 100644 --- a/src/client/spark/core/GlobalExtensions/GenericCollectionExtensions.cs +++ b/src/client/spark/core/GlobalExtensions/GenericCollectionExtensions.cs @@ -41,6 +41,78 @@ namespace System.Collections.Generic public static ReadOnlyDictionary ToReadOnlyDictionary(this IEnumerable enumerable, Func key_selector, Func element_selector, IEqualityComparer comparer) => new ReadOnlyDictionary(enumerable.ToDictionary(key_selector, element_selector, comparer)); + + #region Min + public static T Min(this IEnumerable enumerable, Func select_value) where T : IComparable => + Min(enumerable, select_value, default(T)); + + public static T Min(this IEnumerable enumerable, Func select_value, T return_if_empty) where T : IComparable { + var etr = enumerable.GetEnumerator(); + if (!etr.MoveNext()) return return_if_empty; + T min = select_value(etr.Current); + while (etr.MoveNext()) { + T val = select_value(etr.Current); + if (val.CompareTo(min) == -1) min = val; + } + return min; + } + + public static T Min(this IEnumerable enumerable, Func select_value, Comparison comparison) => + Min(enumerable, select_value, comparison, default); + + public static T Min(this IEnumerable enumerable, Func select_value, Comparison comparison, T return_if_empty) { + var etr = enumerable.GetEnumerator(); + if (!etr.MoveNext()) return return_if_empty; + T min = select_value(etr.Current); + while (etr.MoveNext()) { + T val = select_value(etr.Current); + if (comparison(val, min) == -1) min = val; + } + return min; + } + #endregion + + #region Max + public static T Max(this IEnumerable enumerable, Func select_value) where T : IComparable => + Max(enumerable, select_value, default(T)); + + public static T Max(this IEnumerable enumerable, Func select_value, T return_if_empty) where T : IComparable { + var etr = enumerable.GetEnumerator(); + if (!etr.MoveNext()) return return_if_empty; + T max = select_value(etr.Current); + while (etr.MoveNext()) { + T val = select_value(etr.Current); + if (val.CompareTo(max) == 1) max = val; + } + return max; + } + + public static T Max(this IEnumerable enumerable, Func select_value, Comparison comparison) => + Max(enumerable, select_value, comparison, default); + + + public static T Max(this IEnumerable enumerable, Func select_value, Comparison comparison, T return_if_empty) { + var etr = enumerable.GetEnumerator(); + if (!etr.MoveNext()) return return_if_empty; + T max = select_value(etr.Current); + while (etr.MoveNext()) { + T val = select_value(etr.Current); + if (comparison(val, max) == 1) max = val; + } + return max; + } + #endregion + + #region Sum + public static uint Sum(this IEnumerable enumerable, Func select) => + Sum(enumerable.Select(select)); + + public static uint Sum(this IEnumerable enumerable) { + uint sum = 0; + foreach (uint val in enumerable) sum += val; + return sum; + } + #endregion } public static class CollectionExtensions diff --git a/src/client/spark/view/Spark/Core/OrderUtils.cs b/src/client/spark/view/Spark/Core/OrderUtils.cs index 5cd357b5b..d3814a116 100644 --- a/src/client/spark/view/Spark/Core/OrderUtils.cs +++ b/src/client/spark/view/Spark/Core/OrderUtils.cs @@ -8,7 +8,6 @@ using System.Collections.Generic; using System.Linq; using System.Text; using System.Text.RegularExpressions; -using System.Threading.Tasks; using static mbts.ui.utilities.DisplayNameHelper; namespace mbts.spark { @@ -19,6 +18,7 @@ namespace mbts.spark { var size = order.State.Quantity; return filled(order) / (double)size; } + public static uint working_quantity(this OrderRecordImmutable order) => order.State.Quantity - order.State.FilledQuantity; public static string full_description(this OrderRecordImmutable order, SecurityImmutable security) { StringBuilder sb = new StringBuilder(); sb.Append("Price: "); diff --git a/src/client/spark/view/Utility/CommandHelper.cs b/src/client/spark/view/Utility/CommandHelper.cs new file mode 100644 index 000000000..8e45c2c52 --- /dev/null +++ b/src/client/spark/view/Utility/CommandHelper.cs @@ -0,0 +1,42 @@ +using System.Windows; +using System.Windows.Input; + +namespace mbts.ui.utilities +{ + static class CommandHelper + { + #region Paraphrased from https://referencesource.microsoft.com/#PresentationFramework/src/Framework/MS/Internal/Commands/CommandHelpers.cs + + public static bool CanExecuteCommandSource(ICommandSource commandSource) { + ICommand command = commandSource.Command; + if (command != null) { + object parameter = commandSource.CommandParameter; + IInputElement target = commandSource.CommandTarget; + RoutedCommand routed = command as RoutedCommand; + if (routed != null) { + if (target == null) target = commandSource as IInputElement; + return routed.CanExecute(parameter, target); + } else return command.CanExecute(parameter); + } + return false; + } + + public static void ExecuteCommandSource(ICommandSource commandSource) { + ICommand command = commandSource.Command; + if (command != null) { + object parameter = commandSource.CommandParameter; + IInputElement target = commandSource.CommandTarget; + RoutedCommand routed = command as RoutedCommand; + if (routed != null) { + if (target == null) + target = commandSource as IInputElement; + if (routed.CanExecute(parameter, target)) + routed.Execute(parameter, target); + } else if (command.CanExecute(parameter)) + command.Execute(parameter); + } + } + + #endregion + } +} \ No newline at end of file