MarketView - reduce slowness with large security sets
This commit is contained in:
1 parent
332b7380be
commit
7a712fbaf2
5 files changed
+229
-64
No files matched your search
@@ -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<OrderRecordImmutable> _orders = new ObservableCollection<OrderRecordImmutable>();
|
||||
readonly ObservableFilterCollection<OrderRecordImmutable> _working_orders, _working_bids, _working_asks;
|
||||
readonly Property<double> _bid_price, _ask_price, _last_price, _day_change, _settlement, _session_high, _session_low;
|
||||
readonly Property<int> _position;
|
||||
readonly Property<ushort> _bid_size, _ask_size, _last_size;
|
||||
readonly Property<ulong> _volume;
|
||||
readonly Property<bool> _inside_bids, _inside_asks;
|
||||
readonly DelegateProperty<bool> _working_bids_active, _working_asks_active;
|
||||
readonly DelegateProperty<uint> _working_bid_volume, _working_ask_volume;
|
||||
readonly DelegateProperty<double> _working_best_bid, _working_best_ask;
|
||||
ulong _bbo_ref, _last_ref;
|
||||
|
||||
public Row(IBus bus, SecurityMD smd, IObservable<TableUpdate<OrderRecordImmutable, uint>> 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<OrderRecordImmutable, uint>(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<OrderRecordImmutable>(_orders, o => o.is_active());
|
||||
_working_bids = new ObservableFilterCollection<OrderRecordImmutable>(_working_orders, o => o.Def.Side == Side.Buy);
|
||||
_working_asks = new ObservableFilterCollection<OrderRecordImmutable>(_working_orders, o => o.Def.Side == Side.Sell);
|
||||
_working_bids_sorted = new ObservableSortedCollection<OrderRecordImmutable>(_working_bids, Comparer<OrderRecordImmutable>.Create((x, y) => y.State.Price.CompareTo(x.State.Price)));
|
||||
_working_asks_sorted = new ObservableSortedCollection<OrderRecordImmutable>(_working_asks, Comparer<OrderRecordImmutable>.Create((x, y) => x.State.Price.CompareTo(y.State.Price)));
|
||||
_inside_bids = new ObservableFilterCollection<OrderRecordImmutable>(_working_bids_sorted, o => o.State.Price >= bid_price);
|
||||
_inside_asks = new ObservableFilterCollection<OrderRecordImmutable>(_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<OrderRecordImmutable> _working_bids_sorted, _working_asks_sorted;
|
||||
readonly TableUpdateCollection<OrderRecordImmutable, uint> _orders;
|
||||
readonly ObservableFilterCollection<OrderRecordImmutable> _working_orders, _working_bids, _working_asks, _inside_bids, _inside_asks;
|
||||
readonly Property<double> _bid_price, _ask_price, _last_price, _day_change, _settlement, _session_high, _session_low, _working_best_bid, _working_best_ask;
|
||||
readonly Property<int> _position;
|
||||
readonly Property<ushort> _bid_size, _ask_size, _last_size;
|
||||
readonly Property<ulong>_volume;
|
||||
readonly Property<uint> _working_bid_volume, _working_ask_volume;
|
||||
readonly Property<string> _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<CellAnimation> 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<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;
|
||||
}
|
||||
}
|
||||
_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));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
|
||||
@@ -41,6 +41,78 @@ namespace System.Collections.Generic
|
||||
|
||||
public static ReadOnlyDictionary<TKey, TElement> ToReadOnlyDictionary<T, TKey, TElement>(this IEnumerable<T> enumerable, Func<T, TKey> key_selector, Func<T, TElement> element_selector, IEqualityComparer<TKey> comparer) =>
|
||||
new ReadOnlyDictionary<TKey, TElement>(enumerable.ToDictionary(key_selector, element_selector, comparer));
|
||||
|
||||
#region Min
|
||||
public static T Min<TElement, T>(this IEnumerable<TElement> enumerable, Func<TElement, T> select_value) where T : IComparable<T> =>
|
||||
Min(enumerable, select_value, default(T));
|
||||
|
||||
public static T Min<TElement, T>(this IEnumerable<TElement> enumerable, Func<TElement, T> select_value, T return_if_empty) where T : IComparable<T> {
|
||||
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<TElement, T>(this IEnumerable<TElement> enumerable, Func<TElement, T> select_value, Comparison<T> comparison) =>
|
||||
Min(enumerable, select_value, comparison, default);
|
||||
|
||||
public static T Min<TElement, T>(this IEnumerable<TElement> enumerable, Func<TElement, T> select_value, Comparison<T> 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<TElement, T>(this IEnumerable<TElement> enumerable, Func<TElement, T> select_value) where T : IComparable<T> =>
|
||||
Max(enumerable, select_value, default(T));
|
||||
|
||||
public static T Max<TElement, T>(this IEnumerable<TElement> enumerable, Func<TElement, T> select_value, T return_if_empty) where T : IComparable<T> {
|
||||
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<TElement, T>(this IEnumerable<TElement> enumerable, Func<TElement, T> select_value, Comparison<T> comparison) =>
|
||||
Max(enumerable, select_value, comparison, default);
|
||||
|
||||
|
||||
public static T Max<TElement, T>(this IEnumerable<TElement> enumerable, Func<TElement, T> select_value, Comparison<T> 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<T>(this IEnumerable<T> enumerable, Func<T, uint> select) =>
|
||||
Sum(enumerable.Select(select));
|
||||
|
||||
public static uint Sum(this IEnumerable<uint> enumerable) {
|
||||
uint sum = 0;
|
||||
foreach (uint val in enumerable) sum += val;
|
||||
return sum;
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
|
||||
public static class CollectionExtensions
|
||||
|
||||
@@ -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: ");
|
||||
|
||||
@@ -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
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user