From c59569310f174ea338fe72bd2396c8694baee417 Mon Sep 17 00:00:00 2001 From: John Colvin Date: Sat, 2 Jun 2018 19:35:22 -0400 Subject: [PATCH] mview - sort working orders by price to optimize working-orders-summary getter --- .../Market/MarketView/MarketViewRow.cs | 125 ++++++++++++++---- .../CollectionTests/SortedSetMapTests.cs | 109 +++++++++++++++ .../spark/core/Collections/SortedSetMap.cs | 124 +++++++++++++++++ .../spark/view/Spark/Core/OrderUtils.cs | 16 ++- .../view/Spark/Utilities/OrderDataHelper.cs | 18 ++- 5 files changed, 358 insertions(+), 34 deletions(-) create mode 100644 src/client/spark/Tests/CollectionTests/SortedSetMapTests.cs create mode 100644 src/client/spark/core/Collections/SortedSetMap.cs diff --git a/src/client/spark/Modules/Market/MarketView/MarketViewRow.cs b/src/client/spark/Modules/Market/MarketView/MarketViewRow.cs index 449eebde1..6bb573111 100644 --- a/src/client/spark/Modules/Market/MarketView/MarketViewRow.cs +++ b/src/client/spark/Modules/Market/MarketView/MarketViewRow.cs @@ -1,7 +1,8 @@ using bts.api; -using bts.api.core; using bts.api.orderman; using mbts.co2; +using mbts.collections; +using mbts.spark.utilities; using mbts.ui; using mbts.ui.controls; using mbts.ui.spark; @@ -24,7 +25,7 @@ namespace mbts.spark { readonly ObservableCollection _orders = new ObservableCollection(); readonly ObservableFilterCollection _working_orders, _working_bids, _working_asks; readonly string _security_text, _security_key_text; - readonly Property _bid_price, _ask_price,_last_price, _day_change, _settlement, _session_high, _session_low; + readonly Property _bid_price, _ask_price, _last_price, _day_change, _settlement, _session_high, _session_low; readonly Property _bid_size, _ask_size; readonly CellProperty _position; readonly CellProperty _last_size; @@ -33,6 +34,7 @@ namespace mbts.spark { readonly CellProperty _working_bid_volume, _working_ask_volume; readonly CellProperty _working_best_bid, _working_best_ask; readonly CellProperty _working_bids_text, _working_asks_text; + readonly MyOrderSummarizer _order_summarizer = new MyOrderSummarizer(); ulong _bbo_ref, _last_ref; public Row(IBus bus, SecurityMD smd, OrdersModel orders_model) : base(smd) { @@ -44,7 +46,7 @@ namespace mbts.spark { _position = new CellProperty(0); _bid_price = backing(nameof(bid_price), double.NaN); _ask_price = backing(nameof(ask_price), double.NaN); - _bid_size = backing(nameof(bid_size), 0); + _bid_size = backing(nameof(bid_size), 0); _ask_size = backing(nameof(ask_size), 0); _last_price = backing(nameof(last_price), double.NaN); _last_size = new CellProperty(0); @@ -104,8 +106,8 @@ namespace mbts.spark { public double session_high => _session_high.Value; public int working_bid_volume => _working_bid_volume.value; public int working_ask_volume => _working_ask_volume.value; - public string working_bids_text => OrderUtils.order_collection_summary(_working_bids, (x, y) => x.CompareTo(y), security.ContractSpec); - public string working_asks_text => OrderUtils.order_collection_summary(_working_asks, (x, y) => y.CompareTo(x), security.ContractSpec); + public string working_bids_text => OrderUtils.order_collection_summary(_order_summarizer.best_working_bid, _order_summarizer.best_working_bid_qty, _order_summarizer.working_bid_qty, security.ContractSpec); + public string working_asks_text => OrderUtils.order_collection_summary(_order_summarizer.best_working_ask, _order_summarizer.best_working_ask_qty, _order_summarizer.working_ask_qty, security.ContractSpec); public double working_best_bid => _working_best_bid.value; public double working_best_ask => _working_best_ask.value; public IList animations => _animation.get_animations().ToList(); @@ -201,38 +203,105 @@ namespace mbts.spark { #region Callbacks void _on_orders(ITableUpdateTransform 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; - } - } + foreach (var o in orders.removed) + _order_summarizer.remove_order(o); + foreach (var o in orders.added) + _order_summarizer.add_order(o); bool dirty = false; - _position.on_next_distinct(_position.value + (int)(removed_sells + added_buys - removed_buys - added_sells), ref dirty); _refresh_orders(ref dirty); if (dirty) invalidated?.Invoke(); } void _refresh_orders(ref bool dirty) { - _working_bid_volume.on_next_distinct(_working_bids.Sum(o => o.working_quantity()), ref dirty); - _working_ask_volume.on_next_distinct(_working_asks.Sum(o => o.working_quantity()), ref dirty); - _working_best_bid.on_next_distinct(_working_bids.Max(o => o.State.Price, double.NaN), ref dirty); - _working_best_ask.on_next_distinct(_working_asks.Min(o => o.State.Price, double.NaN), ref dirty); - _inside_bids.on_next_distinct(_bid_price.Value >= _working_best_bid.value, ref dirty); - _inside_asks.on_next_distinct(_ask_price.Value <= _working_best_ask.value, ref dirty); + _position.on_next_distinct(_order_summarizer.position, ref dirty); + _working_bid_volume.on_next_distinct(_order_summarizer.working_bid_qty, ref dirty); + _working_ask_volume.on_next_distinct(_order_summarizer.working_ask_qty, ref dirty); + _working_best_bid.on_next_distinct(_order_summarizer.best_working_bid, ref dirty); + _working_best_ask.on_next_distinct(_order_summarizer.best_working_ask, ref dirty); + _inside_bids.on_next_distinct(_order_summarizer.best_working_bid >= bid_price, ref dirty); + _inside_asks.on_next_distinct(_order_summarizer.best_working_ask <= ask_price, ref dirty); RaisePropertyChanged(nameof(working_bids_text), nameof(working_asks_text)); } - #endregion + #endregion + + class MyOrderSummarizer { + SortedSetMap _bids, _asks; + int _best_bid_working_qty, _best_ask_working_qty, _position, _working_bid_qty, _working_ask_qty; + + public MyOrderSummarizer() { + _bids = new SortedSetMap( + OrderDataHelper.CompareUtils.bid_price_comparer, + OrderDataHelper.CompareUtils.order_record_id_eq_comparer); + _asks = new SortedSetMap( + OrderDataHelper.CompareUtils.ask_price_comparer, + OrderDataHelper.CompareUtils.order_record_id_eq_comparer); + } + + // bid + public double best_working_bid => _bids.Count > 0 ? _bids.at_index(0).Key : double.NaN; + public int best_working_bid_qty => _best_bid_working_qty; + public int working_bid_qty => _working_bid_qty; + // ask + public double best_working_ask => _asks.Count > 0 ? _asks.at_index(0).Key : double.NaN; + public int best_working_ask_qty => _best_ask_working_qty; + public int working_ask_qty => _working_ask_qty; + // pos + public int position => _position; + + public void add_order(OrderRecordImmutable o) { + // This is a market data form - not an orders form - so just show data for root/non-child orders. + if (o.has_parent()) + return; + switch (o.side()) { + case Side.Buy: + _position += (int)o.filled(); + if (o.is_active()) { + _working_bid_qty += o.working_quantity(); + _bids.add(o.price(), o); + } + break; + case Side.Sell: + _position -= (int)o.filled(); + if (o.is_active()) { + _working_ask_qty += o.working_quantity(); + _asks.add(o.price(), o); + } + break; + default: return; + } + _refresh(); + } + + public void remove_order(OrderRecordImmutable o) { + // This is a market data form - not an orders form - so just show data for root/non-child orders. + if (o.has_parent()) + return; + switch (o.side()) { + case Side.Buy: + _position -= (int)o.filled(); + if (o.is_active()) { + _working_bid_qty -= o.working_quantity(); + _bids.remove(o.price(), o); + } + break; + case Side.Sell: + _position += (int)o.filled(); + if (o.is_active()) { + _working_ask_qty -= o.working_quantity(); + _asks.remove(o.price(), o); + } + break; + default: return; + } + _refresh(); + } + + void _refresh() { + _best_bid_working_qty = _bids.Count > 0 ? _bids.at_index(0).Sum(o => o.working_quantity()) : 0; + _best_ask_working_qty = _asks.Count > 0 ? _asks.at_index(0).Sum(o => o.working_quantity()) : 0; + } + } } } } \ No newline at end of file diff --git a/src/client/spark/Tests/CollectionTests/SortedSetMapTests.cs b/src/client/spark/Tests/CollectionTests/SortedSetMapTests.cs new file mode 100644 index 000000000..3dcd1fc9a --- /dev/null +++ b/src/client/spark/Tests/CollectionTests/SortedSetMapTests.cs @@ -0,0 +1,109 @@ +using mbts.collections; +using NUnit.Framework; +using System.Collections.Generic; + +namespace bts.tests { + [TestFixture] + class SortedSetMapTests { + class TestParams { + public readonly IComparer key_comparer = Comparer.Default; + public readonly IEqualityComparer value_eq_comparer = EqualityComparer.Default; + public readonly Dictionary unsorted_key_vals = new Dictionary { + { 3, new[] { 1, 4, 2, 5, 4, 2, 7 } }, + { 5, new[] { 7, 9, 3, 5, 1 } }, + { 4, new[] { 5, 5, 1, 2, 8, 6 } }, + { 1, new[] { 4, 3, 2, 1 } }, + { 2, new[] { 8, 8, 8, 8 } } + }; + public readonly List sorted_keys; + + public TestParams() { + sorted_keys = new List(unsorted_key_vals.Keys); + sorted_keys.Sort(key_comparer); + } + + public SortedSetMap get_populated_sorted_set_map() { + var ssm = new SortedSetMap(Comparer.Default, EqualityComparer.Default); + foreach (var kv in unsorted_key_vals) { + foreach (int val in kv.Value) + ssm.add(kv.Key, val); + } + return ssm; + } + } + + [Test] + public void test_add_values() { + var tps = new TestParams(); + var ssm = new SortedSetMap(tps.key_comparer, tps.value_eq_comparer); + foreach (var kv in tps.unsorted_key_vals) { + foreach (int val in kv.Value) + ssm.add(kv.Key, val); + } + + Assert.That(ssm.Count == tps.unsorted_key_vals.Count, () => $"Count is incorrect: expected({tps.unsorted_key_vals.Count}), actual({ssm.Count})."); + + // test key-sorting, validate corresponding value-sets + for (int i = 0; i < tps.unsorted_key_vals.Count; i++) { + int expected_key = tps.sorted_keys[i]; + var actual_kv = ssm.at_index(i); + Assert.That(expected_key == actual_kv.Key, "Keys are not sorted correctly."); + var expected_vals = new HashSet(tps.unsorted_key_vals[expected_key]); + Assert.That(actual_kv.SetEquals(expected_vals), () => $"Value set is incorrect: expected({string.Join(", ", expected_vals)}), actual({string.Join(", ", actual_kv)})."); + } + } + + [Test] + public void test_remove_value() { + var tps = new TestParams(); + var ssm = tps.get_populated_sorted_set_map(); + // test remove value + int middle_key = tps.sorted_keys[(int)(0.5 * tps.sorted_keys.Count)]; + int[] middle_values = tps.unsorted_key_vals[middle_key]; + int middle_middle_value = middle_values[(int)(0.5 * middle_values.Length)]; + // this should not remove a key-node, just a value from the value-set + int pre_remove_count = ssm.Count; + var pre_remove_kv = ssm[middle_key]; + Assert.That(ssm.remove(middle_key, middle_middle_value), "Failed to remove value."); + Assert.That(ssm.Count == pre_remove_count, "Key-node should not have been removed. "); + var post_remove_kv = ssm[middle_key]; + Assert.That(ReferenceEquals(pre_remove_kv, post_remove_kv), "Key-value-set should remain the same object."); + Assert.That(!post_remove_kv.Contains(middle_middle_value), "Value was not removed from set."); + } + + [Test] + public void test_remove_key() { + var tps = new TestParams(); + var ssm = tps.get_populated_sorted_set_map(); + int middle_key = tps.sorted_keys[(int)(0.5 * tps.sorted_keys.Count)]; + int pre_remove_count = ssm.Count; + var pre_remove_kv = ssm[middle_key]; + Assert.That(ssm.remove(middle_key), "Failed to remove key."); + Assert.That(ssm.Count == pre_remove_count - 1, "Count was not decremented."); + Assert.That(!ssm.contains_key(middle_key), "Still contains key after it was supposedly removed."); + } + + [Test] + public void test_resize() { + const int initial_capacity = 4, ultimate_length = 10; + var ssm = new SortedSetMap(initial_capacity); + for (int i = 0; i < ultimate_length; i++) + ssm.add(i, 1); + Assert.That(ssm.Count == ultimate_length, $"Count should be {ultimate_length}, actually {ssm.Count}"); + } + + [Test] + public void test_copy() { + var tps = new TestParams(); + var ssm = tps.get_populated_sorted_set_map(); + // test CopyTo + var dest_array = new IGroupingSet[ssm.Count]; + ssm.CopyTo(dest_array, 0); + for (int i = 0; i < dest_array.Length; i++) { + var dkv = dest_array[i]; + var kv = ssm.at_index(i); + Assert.That(kv.Key == dkv.Key, () => $"CopyTo destination array key doesn't match: expectd({dkv.Key}), actual({kv.Key})."); + } + } + } +} \ No newline at end of file diff --git a/src/client/spark/core/Collections/SortedSetMap.cs b/src/client/spark/core/Collections/SortedSetMap.cs new file mode 100644 index 000000000..64793a2ce --- /dev/null +++ b/src/client/spark/core/Collections/SortedSetMap.cs @@ -0,0 +1,124 @@ +using System; +using System.Collections; +using System.Collections.Generic; + +namespace mbts.collections { + /// + /// Similar to a SortedList, but supports both key and index read-access, the latter is achieved by the at_index() method. + /// It also allows for multiple values per key, by internally creating a HashSet<TValue> for each key. + /// + /// + /// + class SortedSetMap : IEnumerable>, ICollection { + readonly IComparer _key_comparer; + readonly IEqualityComparer _value_eq_comparer; + InternalGroupSet[] _key_values; + int _count; + + public SortedSetMap(int capacity = 4) + : this(Comparer.Default, EqualityComparer.Default, capacity) { } + + public SortedSetMap(IComparer key_comparer, IEqualityComparer value_eq_comparer, int capacity = 4) { + _key_comparer = key_comparer; + _value_eq_comparer = value_eq_comparer; + _key_values = new InternalGroupSet[capacity]; + } + + public int Count => _count; + public bool IsReadOnly => false; + public object SyncRoot => null; + public bool IsSynchronized => false; + public IEnumerator> GetEnumerator() { + for (int x = 0; x < _count; ++x) + yield return _key_values[x]; + } + + IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); + public void CopyTo(Array array, int index) => Array.Copy(_key_values, 0, array, index, _count); + + public void add(TKey key, TValue value) { + int pos = _binary_search(key); + if (pos < 0) { + pos = ~pos; + _count++; + if (_count + 1 >= _key_values.Length) + _expand(); + Array.Copy(_key_values, pos, _key_values, pos + 1, _count - pos); + _key_values[pos] = new InternalGroupSet(key, _value_eq_comparer) { value }; + } else { + _key_values[pos].Add(value); + } + } + + public bool remove(TKey key, TValue value) { + int index = _binary_search(key); + if (index >= 0 && index <= _count) { + var set = _key_values[index]; + if (set.Remove(value)) { + if (set.Count == 0) { + _count--; + Array.Copy(_key_values, index + 1, _key_values, index, _count - index); + } + return true; + } + } + return false; + } + + public bool remove(TKey key) { + int index = _binary_search(key); + if (index >= 0 && index <= _count) { + _count--; + Array.Copy(_key_values, index + 1, _key_values, index, _count - index); + return true; + } + return false; + } + + public bool contains_key(TKey key) => + _binary_search(key) >= 0; + + public ISet this[TKey key] { + get { + int index = _binary_search(key); + if (index < 0) + throw new KeyNotFoundException(); + return _key_values[index]; + } + } + + public IGroupingSet at_index(int index) => + _key_values[index]; + + int _binary_search(TKey key) { + int low = 0; + int high = _count - 1; + while (low <= high) { + int i = low + ((high - low) >> 1); + int c = _key_comparer.Compare(_key_values[i].Key, key); + if (c == 0) { + return i; + } else if (c < 0) { + low = i + 1; + } else high = i - 1; + } + return ~low; + } + + void _expand() { + var expanded_values = new InternalGroupSet[_key_values.Length * 2]; + Array.Copy(_key_values, expanded_values, _count); + _key_values = expanded_values; + } + + class InternalGroupSet : HashSet, IGroupingSet { + public InternalGroupSet(TKey key, IEqualityComparer comparer) + : base(comparer) => Key = key; + + public InternalGroupSet(TKey key, IEnumerable values, IEqualityComparer comparer) + : base(values, comparer) => Key = key; + + public TKey Key { get; } + } + } +} \ No newline at end of file diff --git a/src/client/spark/view/Spark/Core/OrderUtils.cs b/src/client/spark/view/Spark/Core/OrderUtils.cs index 9b3707078..448077379 100644 --- a/src/client/spark/view/Spark/Core/OrderUtils.cs +++ b/src/client/spark/view/Spark/Core/OrderUtils.cs @@ -125,9 +125,19 @@ namespace mbts.spark { var price_lookup = orders.ToLookup(o => o.State.Price); var best_price = price_lookup.MaxElement(g => g.Key, compare_price); var best_price_volume = best_price.Sum(o => o.working_quantity()); - return price_lookup.Count == 1 ? - $"{best_price_volume} @ {spec.format(best_price.Key)}" : - $"{best_price_volume} ({best_price_volume + price_lookup.Where(g => g.Key != best_price.Key).SelectMany(g => g).Sum(o => o.working_quantity())}) @ {spec.format(best_price.Key)}"; + return order_collection_summary( + best_price.Key, + best_price_volume, + best_price_volume + price_lookup.Where(g => g.Key != best_price.Key).SelectMany(g => g).Sum(o => o.working_quantity()), + spec); + } + + public static string order_collection_summary(double best_price, int best_price_volume, int total_volume, ContractSpecificationImmutable spec) { + if (total_volume == 0) + return string.Empty; + return total_volume == best_price_volume ? + $"{best_price_volume} @ {spec.format(best_price)}" : + $"{best_price_volume} ({total_volume}) @ {spec.format(best_price)}"; } public static string past_tense(this Side side) { diff --git a/src/client/spark/view/Spark/Utilities/OrderDataHelper.cs b/src/client/spark/view/Spark/Utilities/OrderDataHelper.cs index 72c6f1c7c..bbe1f5f7f 100644 --- a/src/client/spark/view/Spark/Utilities/OrderDataHelper.cs +++ b/src/client/spark/view/Spark/Utilities/OrderDataHelper.cs @@ -5,13 +5,14 @@ using Google.Protobuf; using Google.Protobuf.WellKnownTypes; using mbts.ui; using mbts.ui.utility; +using mbts.utils; using System; -using System.Linq; using System.Collections.Generic; using System.ComponentModel.Composition; -using OrderState = bts.api.orderman.OrderState; +using System.Linq; using System.Reactive.Linq; using System.Reactive.Subjects; +using OrderState = bts.api.orderman.OrderState; namespace mbts.spark.utilities { // at some point, user-default settings for orders can be managed here @@ -237,6 +238,17 @@ namespace mbts.spark.utilities { } static class OrderDataHelper { + public static class CompareUtils { + public static readonly IEqualityComparer order_record_id_eq_comparer = + EqualityComparer.Create(o => o.Key); + + public static readonly IComparer bid_price_comparer = + Comparer.Create((x, y) => MathUtils.epsilon_equals(x, y) ? 0 : y.CompareTo(x)); + + public static readonly IComparer ask_price_comparer = + Comparer.Create((x, y) => MathUtils.epsilon_equals(x, y) ? 0 : x.CompareTo(y)); + } + public static CustomOrderDefinition.CustomOneofCase get_order_type(IMessage order_type_def) { switch(order_type_def.Descriptor.Name) { case nameof(MarketOrderDefinition): return CustomOrderDefinition.CustomOneofCase.Market; @@ -276,4 +288,4 @@ namespace mbts.spark.utilities { public static OrderRecordImmutable OrderRecordImmutable { get; } = new OrderRecordImmutable(OrderRecord); } } -} +} \ No newline at end of file