mview - sort working orders by price to optimize working-orders-summary getter

This commit is contained in:
John Colvin committed 2018-06-02 19:36:00 -04:00
1 parent a86b231581
commit c59569310f
5 files changed
+358 -34

No files matched your search

@@ -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<OrderRecordImmutable> _orders = new ObservableCollection<OrderRecordImmutable>();
readonly ObservableFilterCollection<OrderRecordImmutable> _working_orders, _working_bids, _working_asks;
readonly string _security_text, _security_key_text;
readonly Property<double> _bid_price, _ask_price,_last_price, _day_change, _settlement, _session_high, _session_low;
readonly Property<double> _bid_price, _ask_price, _last_price, _day_change, _settlement, _session_high, _session_low;
readonly Property<ushort> _bid_size, _ask_size;
readonly CellProperty<int> _position;
readonly CellProperty<ushort> _last_size;
@@ -33,6 +34,7 @@ namespace mbts.spark {
readonly CellProperty<int> _working_bid_volume, _working_ask_volume;
readonly CellProperty<double> _working_best_bid, _working_best_ask;
readonly CellProperty<string> _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<int>(0);
_bid_price = backing(nameof(bid_price), double.NaN);
_ask_price = backing(nameof(ask_price), double.NaN);
_bid_size = backing<ushort>(nameof(bid_size), 0);
_bid_size = backing<ushort>(nameof(bid_size), 0);
_ask_size = backing<ushort>(nameof(ask_size), 0);
_last_price = backing(nameof(last_price), double.NaN);
_last_size = new CellProperty<ushort>(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<CellAnimation> animations => _animation.get_animations().ToList();
@@ -201,38 +203,105 @@ namespace mbts.spark {
#region Callbacks
void _on_orders(ITableUpdateTransform<OrderRecordImmutable, uint> 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<double, OrderRecordImmutable> _bids, _asks;
int _best_bid_working_qty, _best_ask_working_qty, _position, _working_bid_qty, _working_ask_qty;
public MyOrderSummarizer() {
_bids = new SortedSetMap<double, OrderRecordImmutable>(
OrderDataHelper.CompareUtils.bid_price_comparer,
OrderDataHelper.CompareUtils.order_record_id_eq_comparer);
_asks = new SortedSetMap<double, OrderRecordImmutable>(
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;
}
}
}
}
}
@@ -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<int> key_comparer = Comparer<int>.Default;
public readonly IEqualityComparer<int> value_eq_comparer = EqualityComparer<int>.Default;
public readonly Dictionary<int, int[]> unsorted_key_vals = new Dictionary<int, int[]> {
{ 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<int> sorted_keys;
public TestParams() {
sorted_keys = new List<int>(unsorted_key_vals.Keys);
sorted_keys.Sort(key_comparer);
}
public SortedSetMap<int, int> get_populated_sorted_set_map() {
var ssm = new SortedSetMap<int, int>(Comparer<int>.Default, EqualityComparer<int>.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<int, int>(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<int>(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<int, int>(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<int, int>[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}).");
}
}
}
}
@@ -0,0 +1,124 @@
using System;
using System.Collections;
using System.Collections.Generic;
namespace mbts.collections {
/// <summary>
/// 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&lt;TValue&gt; for each key.
/// </summary>
/// <typeparam name="TKey"></typeparam>
/// <typeparam name="TValue"></typeparam>
class SortedSetMap<TKey, TValue> : IEnumerable<IGroupingSet<TKey, TValue>>, ICollection {
readonly IComparer<TKey> _key_comparer;
readonly IEqualityComparer<TValue> _value_eq_comparer;
InternalGroupSet[] _key_values;
int _count;
public SortedSetMap(int capacity = 4)
: this(Comparer<TKey>.Default, EqualityComparer<TValue>.Default, capacity) { }
public SortedSetMap(IComparer<TKey> key_comparer, IEqualityComparer<TValue> 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<IGroupingSet<TKey, TValue>> 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<TValue> this[TKey key] {
get {
int index = _binary_search(key);
if (index < 0)
throw new KeyNotFoundException();
return _key_values[index];
}
}
public IGroupingSet<TKey, TValue> 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<TValue>, IGroupingSet<TKey, TValue> {
public InternalGroupSet(TKey key, IEqualityComparer<TValue> comparer)
: base(comparer) => Key = key;
public InternalGroupSet(TKey key, IEnumerable<TValue> values, IEqualityComparer<TValue> comparer)
: base(values, comparer) => Key = key;
public TKey Key { get; }
}
}
}
+13 -3
View File
@@ -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) {
@@ -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<OrderRecordImmutable> order_record_id_eq_comparer =
EqualityComparer.Create<OrderRecordImmutable, uint>(o => o.Key);
public static readonly IComparer<double> bid_price_comparer =
Comparer<double>.Create((x, y) => MathUtils.epsilon_equals(x, y) ? 0 : y.CompareTo(x));
public static readonly IComparer<double> ask_price_comparer =
Comparer<double>.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);
}
}
}
}