integrate ICollectionView into OrderBook with 'CVOrderBookVM'
This commit is contained in:
1 parent
701e883aea
commit
4bafada602
15 files changed
+1246
-342
No files matched your search
@@ -57,6 +57,18 @@ namespace mbts.collections {
|
||||
return false;
|
||||
}
|
||||
|
||||
public bool remove(TKey key) =>
|
||||
_dictionary.Remove(key);
|
||||
|
||||
public void clear() => _dictionary.Clear();
|
||||
|
||||
public bool try_pop(TKey key, out ISet<TElement> value) {
|
||||
if (try_lookup(key, out value)) {
|
||||
remove(key);
|
||||
return true;
|
||||
} else return false;
|
||||
}
|
||||
|
||||
public bool try_lookup(TKey key, out ISet<TElement> value) {
|
||||
if (_dictionary.TryGetValue(key, out InternalGroupSet result)) {
|
||||
value = result;
|
||||
|
||||
@@ -449,6 +449,23 @@ namespace System.Collections.Generic {
|
||||
.ToDictionary(vals => vals.k, vals => (vals.Item2, vals.Item3))
|
||||
);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// If TryGetValue succeeds, result will be set with the corresponding value, and the key will be removed (returns true).
|
||||
/// Otherwise, result is set to default, and nothing is removed (returns false).
|
||||
/// </summary>
|
||||
/// <typeparam name="TKey"></typeparam>
|
||||
/// <typeparam name="TValue"></typeparam>
|
||||
/// <param name="dictionary"></param>
|
||||
/// <param name="key"></param>
|
||||
/// <param name="result"></param>
|
||||
/// <returns></returns>
|
||||
public static bool TryPopValue<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, TKey key, out TValue result) {
|
||||
if (dictionary.TryGetValue(key, out result)) {
|
||||
dictionary.Remove(key);
|
||||
return true;
|
||||
} else return false;
|
||||
}
|
||||
}
|
||||
|
||||
public static class ListExtensions
|
||||
|
||||
@@ -20,6 +20,9 @@ namespace mbts.utils {
|
||||
return Create<T?>((x, y) => x.HasValue ? y.HasValue && eqc.Equals(key_selector(x.Value), key_selector(y.Value)) : !y.HasValue);
|
||||
}
|
||||
|
||||
public static IEqualityComparer<T?> CreateNullable<T>(Func<T, T, bool> equals) where T : struct =>
|
||||
new AnonymousEqualityComparer<T?>((a, b) => a.HasValue ? b.HasValue && equals(a.Value, b.Value) : !b.HasValue);
|
||||
|
||||
public static bool NullCheckEquals<T>(T x, T y, Func<T, T, bool> non_null_equals) where T : class, IEquatable<T> =>
|
||||
x == null ? y == null :
|
||||
y != null &&
|
||||
|
||||
@@ -13,6 +13,11 @@ namespace mbts.utils {
|
||||
#region Comparison
|
||||
public static Comparison<T> safe_comparison<T>(Func<T, T, int> compare) =>
|
||||
(a, b) => a.safe_compare(b, compare);
|
||||
|
||||
public static Comparison<T?> nullable_comparison<T>(Func<T, T, int> compare) where T : struct =>
|
||||
(a, b) => a.HasValue ?
|
||||
b.HasValue ? compare(a.Value, b.Value) : 1 :
|
||||
b.HasValue ? -1 : 0;
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
@@ -45,7 +45,7 @@ namespace mbts.ui.controls {
|
||||
internal interface IKeyedColumn<TColKey> {
|
||||
TColKey key { get; }
|
||||
}
|
||||
|
||||
|
||||
internal interface IFilterableColumn {
|
||||
IDataGridColumnFilter filter { get; }
|
||||
string filter_template { get; }
|
||||
@@ -57,13 +57,16 @@ namespace mbts.ui.controls {
|
||||
original_width = double.NaN;
|
||||
|
||||
public static readonly DependencyProperty SortDirectionProperty =
|
||||
WPFHelper.create_dp<ListSortDirection?, ColumnBase>(nameof(SortDirection));
|
||||
WPFHelper.create_dp<ListSortDirection?, ColumnBase>(
|
||||
nameof(SortDirection), (o, v) => o.OnSortDirectionChanged(v));
|
||||
|
||||
public ListSortDirection? SortDirection { get => GetValue(SortDirectionProperty) as ListSortDirection?; set => SetValue(SortDirectionProperty, value); }
|
||||
|
||||
public string sort_member_path { get; set; }
|
||||
public double original_width { get; set; }
|
||||
public bool is_collapsed { get; set; }
|
||||
|
||||
protected virtual void OnSortDirectionChanged(ListSortDirection? new_value) { }
|
||||
}
|
||||
|
||||
internal abstract class ColumnBase<TColKey> : ColumnBase, IKeyedColumn<TColKey> {
|
||||
|
||||
@@ -9,7 +9,7 @@ using static mbts.ui.utilities.DisplayNameHelper;
|
||||
|
||||
namespace mbts.spark {
|
||||
static class OrderUtils {
|
||||
public static uint filled(OrderRecordImmutable order) =>
|
||||
public static uint filled(this OrderRecordImmutable order) =>
|
||||
order.HasState ? order.State.FilledQuantity : 0u;
|
||||
|
||||
public static double fill_ratio(OrderRecordImmutable order) {
|
||||
@@ -20,18 +20,54 @@ namespace mbts.spark {
|
||||
return double.NaN;
|
||||
}
|
||||
|
||||
public static uint quantity(this OrderRecordImmutable order) =>
|
||||
order.HasState ? order.State.Quantity : 0u;
|
||||
|
||||
public static int working_quantity(this OrderRecordImmutable order) =>
|
||||
order.HasState ? (int)order.State.Quantity - (int)order.State.FilledQuantity : 0;
|
||||
|
||||
public static double price(this OrderRecordImmutable order) =>
|
||||
order.HasState ? order.State.Price : double.NaN;
|
||||
|
||||
public static double avg_price(this OrderRecordImmutable order) =>
|
||||
order.HasState ? order.State.AvgPrice : double.NaN;
|
||||
|
||||
public static double last_price(this OrderRecordImmutable order) =>
|
||||
order.HasState ? order.State.LastPrice : double.NaN;
|
||||
|
||||
public static string last_msg(this OrderRecordImmutable order) =>
|
||||
order.HasState ? order.State.LastMsg : string.Empty;
|
||||
|
||||
public static DateTime created(this OrderRecordImmutable order) =>
|
||||
order.HasState && order.State.HasCreated ? order.State.Created.ToDateTime().ToLocalTime() : DateTime.MinValue;
|
||||
|
||||
public static DateTime last_change(this OrderRecordImmutable order) =>
|
||||
order.HasState && order.State.HasLastChange ? order.State.LastChange.ToDateTime().ToLocalTime() : DateTime.MinValue;
|
||||
|
||||
public static OrderStatus status(this OrderRecordImmutable order) =>
|
||||
order.HasState ? order.State.Status : OrderStatus.Error;
|
||||
|
||||
public static uint security_key(this OrderRecordImmutable order) =>
|
||||
order.HasDef ? order.Def.SecurityKey : 0u;
|
||||
|
||||
public static Side side(this OrderRecordImmutable order) =>
|
||||
order.HasDef ? order.Def.Side : Side.Undefined;
|
||||
|
||||
public static string user(this OrderRecordImmutable order) =>
|
||||
order.HasDef ? order.Def.User : string.Empty;
|
||||
|
||||
public static string account(this OrderRecordImmutable order) =>
|
||||
order.HasDef ? order.Def.Account : string.Empty;
|
||||
|
||||
public static CustomOrderDefinition.CustomOneofCase order_type(this OrderRecordImmutable order) =>
|
||||
order.HasDef && order.Def.HasCustomDef ? order.Def.CustomDef.CustomCase : CustomOrderDefinition.CustomOneofCase.None;
|
||||
|
||||
public static OrderSubType sub_type(this OrderRecordImmutable order) =>
|
||||
order.HasDef ? order.Def.SubType : OrderSubType.None;
|
||||
|
||||
public static TimeInForce time_in_force(this OrderRecordImmutable order) =>
|
||||
order.HasDef ? order.Def.TimeInForce : TimeInForce.Undefined;
|
||||
|
||||
public static string full_description(this OrderRecordImmutable order, SecurityImmutable security) {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
string sec_key_text;
|
||||
|
||||
@@ -0,0 +1,111 @@
|
||||
using mbts.utils;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel;
|
||||
|
||||
namespace mbts.spark {
|
||||
partial class CVOrderBookRowVM {
|
||||
public interface IOrderBookCell : IComparable, INotifyPropertyChanged {
|
||||
string text { get; }
|
||||
bool refresh();
|
||||
}
|
||||
|
||||
public static class CellFactory {
|
||||
public static OrderBookCell<T> create<T>(CVOrderBookRowVM row, ICVOrderBookColumn column, Func<CVOrderBookRowVM, T> get_value, Func<CVOrderBookRowVM, OrderBookCell<T>> get_cell, Func<T, string> format = null) =>
|
||||
new OrderBookCell<T>(row, column, get_value, get_cell, format, new Comparison<T>(Comparer<T>.Default.Compare), EqualityComparer<T>.Default);
|
||||
|
||||
public static OrderBookCell<T> create<T>(CVOrderBookRowVM row, ICVOrderBookColumn column, Func<CVOrderBookRowVM, T> get_value, Func<CVOrderBookRowVM, OrderBookCell<T>> get_cell, Comparison<T> comparison, Func<T, string> format = null) =>
|
||||
new OrderBookCell<T>(row, column, get_value, get_cell, format, comparison, EqualityComparer<T>.Default);
|
||||
|
||||
public static OrderBookCell<T?> create_nullable<T>(CVOrderBookRowVM row, ICVOrderBookColumn column, Func<CVOrderBookRowVM, T?> get_value, Func<CVOrderBookRowVM, OrderBookCell<T?>> get_cell, Func<T?, string> format = null)
|
||||
where T : struct => new OrderBookCell<T?>(row, column, get_value, get_cell, format, CompareUtils.nullable_comparison<T>(Comparer<T>.Default.Compare), EqualityComparer.CreateNullable<T>(EqualityComparer<T>.Default.Equals));
|
||||
|
||||
public static OrderBookCell<T?> create_nullable<T>(CVOrderBookRowVM row, ICVOrderBookColumn column, Func<CVOrderBookRowVM, T?> get_value, Func<CVOrderBookRowVM, OrderBookCell<T?>> get_cell, Func<T, T, int> compare, Func<T?, string> format = null)
|
||||
where T : struct => new OrderBookCell<T?>(row, column, get_value, get_cell, format, CompareUtils.nullable_comparison(compare), EqualityComparer.CreateNullable<T>(EqualityComparer<T>.Default.Equals));
|
||||
}
|
||||
|
||||
public class OrderBookCell<T> : IComparable<OrderBookCell<T>>, IOrderBookCell {
|
||||
readonly CVOrderBookRowVM _row;
|
||||
readonly ICVOrderBookColumn _column;
|
||||
readonly Func<CVOrderBookRowVM, T> _get_value;
|
||||
readonly Func<CVOrderBookRowVM, OrderBookCell<T>> _get_cell;
|
||||
readonly Func<T, string> _format;
|
||||
readonly Comparison<T> _comparison;
|
||||
readonly IEqualityComparer<T> _eq_comparer;
|
||||
T _value;
|
||||
string _text;
|
||||
|
||||
public OrderBookCell(CVOrderBookRowVM row, ICVOrderBookColumn column, Func<CVOrderBookRowVM, T> get_value, Func<CVOrderBookRowVM, OrderBookCell<T>> get_cell, Func<T, string> format, Comparison<T> comparison, IEqualityComparer<T> eq_comparer) {
|
||||
_row = row;
|
||||
_column = column;
|
||||
_get_value = get_value;
|
||||
_get_cell = get_cell;
|
||||
_format = format ?? new Func<T, string>(v => v?.ToString() ?? string.Empty);
|
||||
_comparison = comparison;
|
||||
_eq_comparer = eq_comparer;
|
||||
_value = _get_value(_row);
|
||||
}
|
||||
|
||||
public event PropertyChangedEventHandler PropertyChanged;
|
||||
|
||||
public T value => _value;
|
||||
public string text => _text ?? (_text = _format(_value));
|
||||
|
||||
public bool refresh() {
|
||||
var v = _get_value(_row);
|
||||
if (!_eq_comparer.Equals(v, _value)) {
|
||||
_value = v;
|
||||
_text = null;
|
||||
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(value)));
|
||||
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(text)));
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public int CompareTo(OrderBookCell<T> other) {
|
||||
if (other == null)
|
||||
return 1;
|
||||
var lin = _lineage(_row);
|
||||
var olin = _lineage(other._row);
|
||||
int c = 0;
|
||||
using (var en = lin.GetEnumerator())
|
||||
using (var oen = olin.GetEnumerator()) {
|
||||
while (en.MoveNext() && oen.MoveNext()) {
|
||||
if (en.Current.order_key.value != oen.Current.order_key.value) {
|
||||
c = _comparison(_get_cell(en.Current).value, _get_cell(oen.Current).value);
|
||||
if (c != 0)
|
||||
return c;
|
||||
else {
|
||||
c = en.Current.order_key.value.CompareTo(oen.Current.order_key.value);
|
||||
return _column.SortDirection.HasValue && _column.SortDirection.Value == ListSortDirection.Ascending ? c : -c;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return _column.SortDirection.HasValue && _column.SortDirection.Value == ListSortDirection.Ascending ?
|
||||
lin.Count.CompareTo(olin.Count) : olin.Count.CompareTo(lin.Count);
|
||||
}
|
||||
|
||||
public int CompareTo(object obj) =>
|
||||
CompareTo(obj as OrderBookCell<T>);
|
||||
|
||||
public override string ToString() => text;
|
||||
|
||||
// any performance benefits of caching parent rows should be negligable - _book_vm.get_parent() uses a dictionary lookup
|
||||
static List<CVOrderBookRowVM> _lineage(CVOrderBookRowVM r) {
|
||||
var origin = r;
|
||||
var rv = new List<CVOrderBookRowVM>(2); // 99% of the time, this list won't have more than 2 elements
|
||||
while (r.order.has_parent()) {
|
||||
r = r._book_vm.get_order_vm(r.parent_order_key.value);
|
||||
if (r == null)
|
||||
break;
|
||||
rv.Add(r);
|
||||
}
|
||||
rv.Reverse();
|
||||
rv.Add(origin);
|
||||
return rv;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,519 @@
|
||||
using bts.api;
|
||||
using bts.api.orderman;
|
||||
using mbts.spark.filters;
|
||||
using mbts.ui;
|
||||
using mbts.ui.converters;
|
||||
using mbts.ui.utility;
|
||||
using mbts.ui.utility.text;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.ObjectModel;
|
||||
using System.ComponentModel;
|
||||
using System.Linq;
|
||||
using System.Reactive.Linq;
|
||||
using System.Windows;
|
||||
using System.Windows.Controls;
|
||||
using System.Windows.Controls.Primitives;
|
||||
using System.Windows.Data;
|
||||
using System.Windows.Media;
|
||||
using static bts.shell.OrderBookWindowFactory;
|
||||
using static mbts.spark.OrderBookView;
|
||||
using static mbts.ui.controls.DIYGrid;
|
||||
using OrderType = bts.api.orderman.CustomOrderDefinition.CustomOneofCase;
|
||||
|
||||
namespace mbts.spark {
|
||||
// This interface is used to hide ColumnBase.SortDirection, and offer a SortDirection member that does NOT require a DependencyProperty lookup.
|
||||
// In OrderBookRowVM, each "cell(property)" has a corresponding TreeComparable property that the column uses for sorting
|
||||
// - each columns' sort_member_path points to the TreeComparable.
|
||||
// The TreeComparable class needs to know which direction it is sorting so it can force child-orders to always be beneath their parents.
|
||||
// - when comparing with their parent, it must return 1 if Ascending, -1 if Descending.
|
||||
// Since the TreeComparables in each row must access their respective column SortDirections in their CompareTo methods, those DP lookups could add significant overhead in large collections.
|
||||
// I considered adding ListSortDirection members directly to OrderBookVM, and letting the TreeComparables refer to that property,
|
||||
// but decided it would be better to let the Columns own it so we wouldn't have to worry about keeping the 2 props in sync.
|
||||
// TODO: Maybe binding to the SortDirection would be less smelly?
|
||||
// - initially, I tried to bind to the SortDirection of each column, unfortunately, the binding target was not updated before the ICollectionView sorting took place.
|
||||
// I decided it was not worth changing the DIYGrid.Sorting function to force the SortDirection binding targets to update immediately, before the ICollectionView sorting.
|
||||
interface ICVOrderBookColumn {
|
||||
ListSortDirection? SortDirection { get; }
|
||||
IDataGridColumnFilter<CVOrderBookRowVM> filter { get; }
|
||||
}
|
||||
|
||||
partial class CVOrderBookVM {
|
||||
public class CVOrderBookColumnCollection : ObservableCollection<ColumnBase>, IDisposable {
|
||||
public CVOrderBookColumnCollection(OrdersModel model, OrderBookStartupData startup_data) {
|
||||
// security
|
||||
Add(security_column = new SecurityColumn(this,
|
||||
new RegexFilterVM<CVOrderBookRowVM>(r => r.security.text, value_path(nameof(OrderBookRowVM.security_text)))));
|
||||
// time_created
|
||||
Add(time_created_column = new TimeCreatedColumn(
|
||||
new TimeFilterVM<CVOrderBookRowVM>(r => r.time_created.value, value_path(nameof(OrderBookRowVM.time_created)))));
|
||||
// last_change
|
||||
Add(last_change_column = new LastChangeTimeColumn(
|
||||
new TimeFilterVM<CVOrderBookRowVM>(r => r.last_change.value, value_path(nameof(CVOrderBookRowVM.last_change)))));
|
||||
// user
|
||||
Add(user_column = new SimpleTextColumn<string[]>(
|
||||
Columns.User,
|
||||
DefaultColumnHeaders.User,
|
||||
nameof(CVOrderBookRowVM.user),
|
||||
r => r.user.value,
|
||||
DefaultColumnWidths.User,
|
||||
DiscreteValuesFilterVM.Create<CVOrderBookRowVM, string>(
|
||||
model.factory.users,
|
||||
r => r.user.value,
|
||||
value_path(nameof(CVOrderBookRowVM.user))),
|
||||
DataGridFilterKnownTemplates.DiscreteValues));
|
||||
// account
|
||||
Add(account_column = new SimpleTextColumn<string[]>(
|
||||
Columns.Account,
|
||||
DefaultColumnHeaders.Account,
|
||||
nameof(CVOrderBookRowVM.account),
|
||||
r => r.account.value,
|
||||
DefaultColumnWidths.Account,
|
||||
DiscreteValuesFilterVM.Create<CVOrderBookRowVM, string, string[]>(
|
||||
model.factory.subscribe_accounts(),
|
||||
r => r.account.value,
|
||||
vals => vals.ToArray(),
|
||||
vals => Observable.Return(vals),
|
||||
value_path(nameof(CVOrderBookRowVM.account))),
|
||||
DataGridFilterKnownTemplates.DiscreteValues));
|
||||
// exchange
|
||||
Add(exchange_column = new SimpleTextColumn<Venue[]>(
|
||||
Columns.Exchange,
|
||||
DefaultColumnHeaders.Exchange,
|
||||
nameof(CVOrderBookRowVM.exchange),
|
||||
r => r.exchange.text,
|
||||
DefaultColumnWidths.Exchange,
|
||||
DiscreteValuesFilterVM.Create<CVOrderBookRowVM, Venue>(
|
||||
EnumHelper.get_values_except(Venue.Unset).ToList(),
|
||||
r => r.exchange.value,
|
||||
value_path(nameof(CVOrderBookRowVM.exchange))),
|
||||
DataGridFilterKnownTemplates.EnumValues));
|
||||
// time in force
|
||||
Add(time_in_force_column = new SimpleTextColumn<TimeInForce[]>(
|
||||
Columns.TimeInForce,
|
||||
DefaultColumnHeaders.TimeInForce,
|
||||
nameof(CVOrderBookRowVM.time_in_force),
|
||||
r => r.time_in_force.text,
|
||||
DefaultColumnWidths.TimeInForce,
|
||||
DiscreteValuesFilterVM.Create<CVOrderBookRowVM, TimeInForce>(
|
||||
EnumHelper.get_values<TimeInForce>().ToList(),
|
||||
r => r.time_in_force.value,
|
||||
value_path(nameof(CVOrderBookRowVM.time_in_force))),
|
||||
DataGridFilterKnownTemplates.EnumValues));
|
||||
// order type
|
||||
Add(order_type_column = new OrderBookOrderTypeColumn(
|
||||
DiscreteValuesFilterVM.Create<CVOrderBookRowVM, OrderType>(
|
||||
EnumHelper.get_values_except(OrderType.None).ToList(),
|
||||
r => r.order_type.value,
|
||||
value_path(nameof(CVOrderBookRowVM.order_type)))));
|
||||
// order sub type
|
||||
Add(sub_type_column = new SimpleTextColumn<OrderSubType[]>(
|
||||
Columns.SubType,
|
||||
"Sub Type",
|
||||
nameof(CVOrderBookRowVM.order_sub_type),
|
||||
r => r.order_sub_type.text,
|
||||
80,
|
||||
DiscreteValuesFilterVM.Create<CVOrderBookRowVM, OrderSubType>(
|
||||
EnumHelper.get_values<OrderSubType>().ToList(),
|
||||
r => r.order_sub_type.value,
|
||||
value_path(nameof(CVOrderBookRowVM.order_sub_type))),
|
||||
SubTypeFilterTemplateKey));
|
||||
// side
|
||||
Add(side_column = new SideColumn(
|
||||
DiscreteValuesFilterVM.Create<CVOrderBookRowVM, Side>(
|
||||
new[] { Side.Buy, Side.Sell },
|
||||
r => r.side.value,
|
||||
value_path(nameof(CVOrderBookRowVM.side)))));
|
||||
// quantity
|
||||
Add(qty_column = new SizeColumn(
|
||||
new UIntMinMax<CVOrderBookRowVM>(r => r.quantity.value, value_path(nameof(CVOrderBookRowVM.quantity)))));
|
||||
// price
|
||||
Add(price_column = new PriceColumn(
|
||||
new DoubleMinMax<CVOrderBookRowVM>(r => r.price.value, value_path(nameof(CVOrderBookRowVM.price)))));
|
||||
// filled quantity
|
||||
Add(filled_qty_column = new FilledColumn(
|
||||
new UIntMinMax<CVOrderBookRowVM>(r => r.filled_quantity.value, value_path(nameof(CVOrderBookRowVM.filled_quantity)))));
|
||||
// avg filled price
|
||||
Add(avg_price_column = new AvgFilledColumn(
|
||||
new DoubleMinMax<CVOrderBookRowVM>(r => r.avg_price.value, value_path(nameof(CVOrderBookRowVM.avg_price)))));
|
||||
// last filled price
|
||||
Add(last_price_column = new LastFilledColumn(
|
||||
new DoubleMinMax<CVOrderBookRowVM>(r => r.last_price.value, value_path(nameof(CVOrderBookRowVM.last_price)))));
|
||||
// status
|
||||
Add(status_column = new SimpleTextColumn<OrderStatus[]>(
|
||||
Columns.Status,
|
||||
"Status",
|
||||
nameof(CVOrderBookRowVM.status),
|
||||
r => r.status.text,
|
||||
80,
|
||||
DiscreteValuesFilterVM.Create<CVOrderBookRowVM, OrderStatus>(
|
||||
EnumHelper.get_values<OrderStatus>().ToList(),
|
||||
r => r.status.value,
|
||||
value_path(nameof(CVOrderBookRowVM.status))),
|
||||
DataGridFilterKnownTemplates.EnumValues));
|
||||
// last message
|
||||
Add(last_message_column = new OrderBookLastMessageColumn(
|
||||
new RegexFilterVM<CVOrderBookRowVM>(
|
||||
r => r.last_msg.value,
|
||||
value_path(nameof(CVOrderBookRowVM.last_msg)))));
|
||||
// security key
|
||||
Add(security_key_column = new SimpleTextColumn<string>(
|
||||
Columns.SecurityKey,
|
||||
DefaultColumnHeaders.SecurityKey,
|
||||
nameof(CVOrderBookRowVM.security_key),
|
||||
r => r.security_key.text,
|
||||
DefaultColumnWidths.SecurityKey,
|
||||
new RegexFilterVM<CVOrderBookRowVM>(
|
||||
r => r.security_key.text,
|
||||
value_path(nameof(CVOrderBookRowVM.security_key))),
|
||||
DataGridFilterKnownTemplates.Regex));
|
||||
// order key
|
||||
Add(order_key_column = new SimpleTextColumn<string>(
|
||||
Columns.OrderKey,
|
||||
DefaultColumnHeaders.OrderKey,
|
||||
nameof(CVOrderBookRowVM.order_key),
|
||||
r => r.order_key.text,
|
||||
DefaultColumnWidths.OrderKey,
|
||||
new RegexFilterVM<CVOrderBookRowVM>(
|
||||
r => r.order_key.text,
|
||||
value_path(nameof(CVOrderBookRowVM.order_key))),
|
||||
DataGridFilterKnownTemplates.Regex));
|
||||
// parent key (this is for diagnostic purposes, remove this column once OrderBook seems stable again)
|
||||
Add(parent_key_column = new SimpleTextColumn<string>(
|
||||
Columns.ParentKey,
|
||||
"Parent Key",
|
||||
nameof(CVOrderBookRowVM.parent_order_key),
|
||||
r => r.parent_order_key.text,
|
||||
80,
|
||||
new RegexFilterVM<CVOrderBookRowVM>(
|
||||
r => r.parent_order_key.text,
|
||||
value_path(nameof(CVOrderBookRowVM.parent_order_key))),
|
||||
DataGridFilterKnownTemplates.Regex));
|
||||
|
||||
string value_path(string cell_name) =>
|
||||
string.Join(".", cell_name, nameof(CVOrderBookRowVM.OrderBookCell<uint>.value)); //uint is an arbitrary 'T', just want to get compile check on 'value' path.
|
||||
|
||||
if (startup_data?.filters is FilterStates fs) {
|
||||
// TODO: add "security filter" data to FilterStates
|
||||
time_created_column.filter.load_state(fs.time_created);
|
||||
user_column.filter.load_state(fs.users);
|
||||
account_column.filter.load_state(fs.accounts);
|
||||
price_column.filter.load_state(fs.price);
|
||||
qty_column.filter.load_state(fs.qty);
|
||||
filled_qty_column.filter.load_state(fs.qty_filled);
|
||||
side_column.filter.load_state(fs.sides);
|
||||
order_type_column.filter.load_state(fs.order_types);
|
||||
// TODO: add "sub_type column.filter" data to FilterStates
|
||||
time_in_force_column.filter.load_state(fs.tifs);
|
||||
exchange_column.filter.load_state(fs.exchanges);
|
||||
|
||||
order_key_column.filter.load_state(fs.order_key_regex);
|
||||
avg_price_column.filter.load_state(fs.avg_price);
|
||||
last_price_column.filter.load_state(fs.last_price);
|
||||
status_column.filter.load_state(fs.statuses);
|
||||
last_message_column.filter.load_state(fs.exchange_message_regex);
|
||||
last_change_column.filter.load_state(fs.last_change);
|
||||
security_column.filter.load_state(fs.security_regex);
|
||||
}
|
||||
if (startup_data?.sort_criteria is List<SortingCriteria> lsc) {
|
||||
foreach (var sc in lsc)
|
||||
this.ApplyToFirst(c => c.sort_member_path == sc.col_name, c => c.SortDirection = sc.is_ascending ? ListSortDirection.Ascending : ListSortDirection.Descending);
|
||||
}
|
||||
foreach (var c in this.Cast<ICVOrderBookColumn>())
|
||||
c.filter.filter_changed += _set_active_filters;
|
||||
_set_active_filters();
|
||||
}
|
||||
|
||||
List<IDataGridColumnFilter<CVOrderBookRowVM>> _active_filters;
|
||||
|
||||
public event Action filter_changed;
|
||||
|
||||
public SecurityColumn security_column { get; }
|
||||
public TimeCreatedColumn time_created_column { get; }
|
||||
public LastChangeTimeColumn last_change_column { get; }
|
||||
public SimpleTextColumn<string[]> user_column { get; }
|
||||
public SimpleTextColumn<string[]> account_column { get; }
|
||||
public SimpleTextColumn<Venue[]> exchange_column { get; }
|
||||
public SimpleTextColumn<TimeInForce[]> time_in_force_column { get; }
|
||||
public OrderBookOrderTypeColumn order_type_column { get; }
|
||||
public SimpleTextColumn<OrderSubType[]> sub_type_column { get; }
|
||||
public SideColumn side_column { get; }
|
||||
public SizeColumn qty_column { get; }
|
||||
public PriceColumn price_column { get; }
|
||||
public FilledColumn filled_qty_column { get; }
|
||||
public LastFilledColumn last_price_column { get; }
|
||||
public AvgFilledColumn avg_price_column { get; }
|
||||
public SimpleTextColumn<OrderStatus[]> status_column { get; }
|
||||
public OrderBookLastMessageColumn last_message_column { get; }
|
||||
public SimpleTextColumn<string> security_key_column { get; }
|
||||
public SimpleTextColumn<string> order_key_column { get; }
|
||||
public SimpleTextColumn<string> parent_key_column { get; }
|
||||
|
||||
public bool filter(CVOrderBookRowVM row) => _active_filters.All(f => f.filter(row));
|
||||
|
||||
public FilterStates get_filter_states() =>
|
||||
new FilterStates {
|
||||
order_key_regex = order_key_column.filter.get_state(),
|
||||
time_created = time_created_column.filter.get_state(),
|
||||
users = user_column.filter.get_state(),
|
||||
accounts = account_column.filter.get_state(),
|
||||
exchanges = exchange_column.filter.get_state(),
|
||||
tifs = time_in_force_column.filter.get_state(),
|
||||
order_types = order_type_column.filter.get_state(),
|
||||
sides = side_column.filter.get_state(),
|
||||
qty = qty_column.filter.get_state(),
|
||||
price = price_column.filter.get_state(),
|
||||
qty_filled = filled_qty_column.filter.get_state(),
|
||||
avg_price = avg_price_column.filter.get_state(),
|
||||
last_price = last_price_column.filter.get_state(),
|
||||
statuses = status_column.filter.get_state(),
|
||||
exchange_message_regex = last_message_column.filter.get_state(),
|
||||
last_change = last_change_column.filter.get_state(),
|
||||
security_regex = security_column.filter.get_state()
|
||||
};
|
||||
|
||||
public void Dispose() {
|
||||
foreach (var c in this.Cast<ICVOrderBookColumn>()) {
|
||||
c.filter.filter_changed -= _set_active_filters;
|
||||
(c.filter as IDisposable)?.Dispose();
|
||||
}
|
||||
}
|
||||
|
||||
void _set_active_filters() {
|
||||
_active_filters = this
|
||||
.Cast<ICVOrderBookColumn>()
|
||||
.Select(c => c.filter)
|
||||
.Where(f => f.active.GetValueOrDefault())
|
||||
.ToList();
|
||||
filter_changed?.Invoke();
|
||||
}
|
||||
}
|
||||
|
||||
public abstract class OrderbookColumnBase<T> : ColumnBase<Columns>, IKeyedColumn<Columns>, IFilterableColumn, ICVOrderBookColumn {
|
||||
ListSortDirection? _cached_sort_direction;
|
||||
readonly string _filter_template;
|
||||
|
||||
protected OrderbookColumnBase(Columns col_key, string header, string sort_prop, double width, IDataGridColumnPersistentFilter<CVOrderBookRowVM, T> filter, string filter_template) : base(col_key) {
|
||||
Width = width;
|
||||
Header = header;
|
||||
sort_member_path = sort_prop;
|
||||
this.filter = filter;
|
||||
_filter_template = filter_template;
|
||||
_cached_sort_direction = SortDirection;
|
||||
}
|
||||
|
||||
public IDataGridColumnPersistentFilter<CVOrderBookRowVM, T> filter { get; }
|
||||
|
||||
IDataGridColumnFilter<CVOrderBookRowVM> ICVOrderBookColumn.filter => filter;
|
||||
IDataGridColumnFilter IFilterableColumn.filter => filter;
|
||||
string IFilterableColumn.filter_template => _filter_template;
|
||||
ListSortDirection? ICVOrderBookColumn.SortDirection => _cached_sort_direction;
|
||||
|
||||
protected override void OnSortDirectionChanged(ListSortDirection? new_value) => _cached_sort_direction = new_value;
|
||||
}
|
||||
|
||||
public class SimpleTextColumn<TFilterState> : OrderbookColumnBase<TFilterState>, IRenderColumn<CVOrderBookRowVM> {
|
||||
readonly Func<CVOrderBookRowVM, string> _get_text;
|
||||
public SimpleTextColumn(Columns key, string header, string sort_prop, Func<CVOrderBookRowVM, string> get_text, double width, IDataGridColumnPersistentFilter<CVOrderBookRowVM, TFilterState> filter, string filter_template)
|
||||
: base(key, header, sort_prop, width, filter, filter_template) => _get_text = get_text;
|
||||
|
||||
public void draw(CVOrderBookRowVM row, DrawingContext dc, ColumnGeometry position, double row_height) {
|
||||
string text = _get_text(row);
|
||||
if (!string.IsNullOrEmpty(text))
|
||||
CellRender.draw_text(dc, RenderUtils.get_glyph_context(row), RenderUtils.font_size, text, position, RowHeight,
|
||||
Brushes.Black, null, HorizontalAlignment.Left, VerticalAlignment.Center, DefaultCellPadding);
|
||||
}
|
||||
}
|
||||
|
||||
#region Explicit Column Defs
|
||||
public class SideColumn : OrderbookColumnBase<Side[]>, IRenderColumn<CVOrderBookRowVM> {
|
||||
public SideColumn(IDataGridColumnPersistentFilter<CVOrderBookRowVM, Side[]> filter)
|
||||
: base(Columns.Side, "B/S", nameof(CVOrderBookRowVM.side), DefaultColumnWidths.Side, filter, DataGridFilterKnownTemplates.EnumValues) { }
|
||||
|
||||
public void draw(CVOrderBookRowVM row, DrawingContext dc, ColumnGeometry position, double row_height) =>
|
||||
CellRender.draw_text(dc, RenderUtils.get_glyph_context(row), RenderUtils.font_size, row.side.text, position, RowHeight,
|
||||
RenderUtils.get_side_brush(row), null, HorizontalAlignment.Left, VerticalAlignment.Center, DefaultCellPadding);
|
||||
}
|
||||
|
||||
public class SizeColumn : OrderbookColumnBase<UIntMinMaxState>, IRenderColumn<CVOrderBookRowVM> {
|
||||
public SizeColumn(IDataGridColumnPersistentFilter<CVOrderBookRowVM, UIntMinMaxState> filter)
|
||||
: base(Columns.Size, "Qty", nameof(CVOrderBookRowVM.quantity), DefaultColumnWidths.Size, filter, DataGridFilterKnownTemplates.UIntMinMax) {
|
||||
sort_member_path = nameof(CVOrderBookRowVM.quantity);
|
||||
}
|
||||
|
||||
public void draw(CVOrderBookRowVM row, DrawingContext dc, ColumnGeometry position, double row_height) =>
|
||||
CellRender.draw_text(dc, RenderUtils.get_glyph_context(row), RenderUtils.font_size, row.quantity.text, position, RowHeight,
|
||||
RenderUtils.get_side_brush(row), null, HorizontalAlignment.Right, VerticalAlignment.Center, DefaultCellPadding);
|
||||
}
|
||||
|
||||
public class FilledColumn : OrderbookColumnBase<UIntMinMaxState>, IRenderColumn<CVOrderBookRowVM> {
|
||||
public FilledColumn(IDataGridColumnPersistentFilter<CVOrderBookRowVM, UIntMinMaxState> filter)
|
||||
: base(Columns.Filled, "Filled Qty", nameof(CVOrderBookRowVM.filled_quantity), DefaultColumnWidths.Size, filter, DataGridFilterKnownTemplates.UIntMinMax) {
|
||||
sort_member_path = nameof(CVOrderBookRowVM.filled_quantity);
|
||||
}
|
||||
|
||||
public void draw(CVOrderBookRowVM row, DrawingContext dc, ColumnGeometry position, double row_height) =>
|
||||
CellRender.draw_text(dc, RenderUtils.get_glyph_context(row), RenderUtils.font_size, row.filled_quantity.text, position, RowHeight,
|
||||
RenderUtils.get_side_brush(row), null, HorizontalAlignment.Right, VerticalAlignment.Center, DefaultCellPadding);
|
||||
}
|
||||
|
||||
public class PriceColumn : OrderbookColumnBase<DoubleMinMaxState>, IRenderColumn<CVOrderBookRowVM> {
|
||||
public PriceColumn(IDataGridColumnPersistentFilter<CVOrderBookRowVM, DoubleMinMaxState> filter)
|
||||
: base(Columns.Price, "Price", nameof(CVOrderBookRowVM.price), DefaultColumnWidths.Price, filter, DataGridFilterKnownTemplates.DoubleMinMax) { }
|
||||
|
||||
public void draw(CVOrderBookRowVM row, DrawingContext dc, ColumnGeometry position, double row_height) =>
|
||||
CellRender.draw_text(dc, RenderUtils.get_glyph_context(row), RenderUtils.font_size, row.price.text,
|
||||
position, RowHeight, Brushes.Black, null, HorizontalAlignment.Right, VerticalAlignment.Center, DefaultCellPadding);
|
||||
}
|
||||
|
||||
public class AvgFilledColumn : OrderbookColumnBase<DoubleMinMaxState>, IRenderColumn<CVOrderBookRowVM> {
|
||||
public AvgFilledColumn(IDataGridColumnPersistentFilter<CVOrderBookRowVM, DoubleMinMaxState> filter)
|
||||
: base(Columns.AvgFilled, "Avg Filled", nameof(CVOrderBookRowVM.avg_price), DefaultColumnWidths.Price, filter, DataGridFilterKnownTemplates.DoubleMinMax) { }
|
||||
|
||||
public void draw(CVOrderBookRowVM row, DrawingContext dc, ColumnGeometry position, double row_height) {
|
||||
CellRender.draw_text(dc, RenderUtils.get_glyph_context(row), RenderUtils.font_size, row.avg_price.text,
|
||||
position, RowHeight, Brushes.Black, null, HorizontalAlignment.Right, VerticalAlignment.Center, DefaultCellPadding);
|
||||
}
|
||||
}
|
||||
|
||||
public class LastFilledColumn : OrderbookColumnBase<DoubleMinMaxState>, IRenderColumn<CVOrderBookRowVM> {
|
||||
public LastFilledColumn(IDataGridColumnPersistentFilter<CVOrderBookRowVM, DoubleMinMaxState> filter)
|
||||
: base(Columns.LastPrice, "Last Filled", nameof(CVOrderBookRowVM.last_price), DefaultColumnWidths.Price, filter, DataGridFilterKnownTemplates.DoubleMinMax) { }
|
||||
|
||||
public void draw(CVOrderBookRowVM row, DrawingContext dc, ColumnGeometry position, double row_height) =>
|
||||
CellRender.draw_text(dc, RenderUtils.get_glyph_context(row), RenderUtils.font_size, row.last_price.text,
|
||||
position, RowHeight, Brushes.Black, null, HorizontalAlignment.Right, VerticalAlignment.Center, DefaultCellPadding);
|
||||
}
|
||||
|
||||
public class TimeCreatedColumn : OrderbookColumnBase<TimeFilterTuple>, IRenderColumn<CVOrderBookRowVM> {
|
||||
public TimeCreatedColumn(IDataGridColumnPersistentFilter<CVOrderBookRowVM, TimeFilterTuple> filter)
|
||||
: base(Columns.Time, "Time", nameof(CVOrderBookRowVM.time_created), DefaultColumnWidths.Time, filter, DataGridFilterKnownTemplates.TimeFilter) { }
|
||||
|
||||
public void draw(CVOrderBookRowVM row, DrawingContext dc, ColumnGeometry position, double row_height) =>
|
||||
CellRender.draw_text(dc, RenderUtils.get_glyph_context(row), RenderUtils.font_size, row.time_created.text,
|
||||
position, RowHeight, Brushes.Black, null, HorizontalAlignment.Left, VerticalAlignment.Center, DefaultCellPadding);
|
||||
}
|
||||
|
||||
public class LastChangeTimeColumn : OrderbookColumnBase<TimeFilterTuple>, IRenderColumn<CVOrderBookRowVM> {
|
||||
public LastChangeTimeColumn(IDataGridColumnPersistentFilter<CVOrderBookRowVM, TimeFilterTuple> filter)
|
||||
: base(Columns.LastChange, "Last Change", nameof(CVOrderBookRowVM.last_change), DefaultColumnWidths.Time, filter, DataGridFilterKnownTemplates.TimeFilter) { }
|
||||
|
||||
public void draw(CVOrderBookRowVM row, DrawingContext dc, ColumnGeometry position, double row_height) =>
|
||||
CellRender.draw_text(dc, RenderUtils.get_glyph_context(row), RenderUtils.font_size, row.last_change.text,
|
||||
position, RowHeight, Brushes.Black, null, HorizontalAlignment.Left, VerticalAlignment.Center, DefaultCellPadding);
|
||||
}
|
||||
|
||||
public class OrderBookOrderTypeColumn : OrderTypeColumn<Columns>, ICVOrderBookColumn {
|
||||
ListSortDirection? _cached_sort_direction;
|
||||
public OrderBookOrderTypeColumn(IDataGridColumnPersistentFilter<CVOrderBookRowVM, OrderType[]> filter)
|
||||
: base(Columns.OrderType, new Binding($"{nameof(CVOrderBookRowVM.order_type)}.{nameof(CVOrderBookRowVM.OrderBookCell<OrderType>.value)}"), new Binding(nameof(CVOrderBookRowVM.order_metadata)), filter) {
|
||||
this.filter = filter;
|
||||
sort_member_path = nameof(CVOrderBookRowVM.order_type);
|
||||
font_weight_binding = RenderUtils.get_font_weight_binding();
|
||||
_cached_sort_direction = SortDirection;
|
||||
}
|
||||
|
||||
public new IDataGridColumnPersistentFilter<CVOrderBookRowVM, OrderType[]> filter { get; }
|
||||
|
||||
IDataGridColumnFilter<CVOrderBookRowVM> ICVOrderBookColumn.filter => filter;
|
||||
ListSortDirection? ICVOrderBookColumn.SortDirection => _cached_sort_direction;
|
||||
|
||||
protected override void OnSortDirectionChanged(ListSortDirection? new_value) => _cached_sort_direction = new_value;
|
||||
}
|
||||
|
||||
public class OrderBookLastMessageColumn : TextColumn<Columns>, ICVOrderBookColumn {
|
||||
ListSortDirection? _cached_sort_direction;
|
||||
|
||||
public OrderBookLastMessageColumn(IDataGridColumnPersistentFilter<CVOrderBookRowVM, string> filter)
|
||||
: base(Columns.LastMessage, new Binding(nameof(CVOrderBookRowVM.last_msg)), filter, DataGridFilterKnownTemplates.Regex) {
|
||||
this.filter = filter;
|
||||
Header = "Last Message";
|
||||
Width = 80;
|
||||
sort_member_path = nameof(CVOrderBookRowVM.last_msg);
|
||||
tooltip_binding = new Binding(nameof(CVOrderBookRowVM.last_msg));
|
||||
}
|
||||
|
||||
public new IDataGridColumnPersistentFilter<CVOrderBookRowVM, string> filter { get; }
|
||||
|
||||
IDataGridColumnFilter<CVOrderBookRowVM> ICVOrderBookColumn.filter => filter;
|
||||
ListSortDirection? ICVOrderBookColumn.SortDirection => _cached_sort_direction;
|
||||
|
||||
protected override void OnSortDirectionChanged(ListSortDirection? new_value) => _cached_sort_direction = new_value;
|
||||
}
|
||||
|
||||
public class SecurityColumn : FrameworkElementColumnBase<Columns>, ICVOrderBookColumn, IFilterableColumn {
|
||||
readonly BindingBase _font_weight_binding = RenderUtils.get_font_weight_binding();
|
||||
readonly IValueConverter _expander_visiblilty_converter = new ui.converters.BooleanToVisibilityConverter { VisibilityForFalse = Visibility.Hidden };
|
||||
readonly IValueConverter _margin_converter = new AnonymousValueConverter<int, Thickness>(level => new Thickness(10 * level, 0, 0, 0));
|
||||
|
||||
ListSortDirection? _cached_sort_direction;
|
||||
|
||||
public SecurityColumn(CVOrderBookColumnCollection owner, IDataGridColumnPersistentFilter<CVOrderBookRowVM, string> filter) : base(Columns.Security) {
|
||||
this.filter = filter;
|
||||
Header = DefaultColumnHeaders.Security;
|
||||
Width = DefaultColumnWidths.Security;
|
||||
sort_member_path = nameof(CVOrderBookRowVM.security);
|
||||
_cached_sort_direction = SortDirection;
|
||||
}
|
||||
|
||||
public IDataGridColumnPersistentFilter<CVOrderBookRowVM, string> filter { get; }
|
||||
|
||||
IDataGridColumnFilter<CVOrderBookRowVM> ICVOrderBookColumn.filter => filter;
|
||||
IDataGridColumnFilter IFilterableColumn.filter { get; }
|
||||
string IFilterableColumn.filter_template => DataGridFilterKnownTemplates.Regex;
|
||||
ListSortDirection? ICVOrderBookColumn.SortDirection => _cached_sort_direction;
|
||||
|
||||
public override FrameworkElement view_factory() {
|
||||
// expander
|
||||
var toggle_button = new ToggleButton { ClickMode = ClickMode.Press, IsThreeState = false };
|
||||
toggle_button.SetBinding(ToggleButton.IsCheckedProperty, new Binding(nameof(CVOrderBookRowVM.is_expanded)));
|
||||
toggle_button.SetBinding(ToggleButton.MarginProperty, new Binding(nameof(CVOrderBookRowVM.depth_level)) { Converter = _margin_converter });
|
||||
toggle_button.SetBinding(ToggleButton.VisibilityProperty, new Binding(nameof(CVOrderBookRowVM.is_parent)) { Converter = _expander_visiblilty_converter });
|
||||
toggle_button.SetResourceReference(ToggleButton.StyleProperty, ExpandCollapseToggleStyleKey);
|
||||
//
|
||||
var security_view = new TextBlock();
|
||||
security_view.SetBinding(TextBlock.TextProperty, nameof(CVOrderBookRowVM.security));
|
||||
security_view.SetBinding(TextBlock.FontWeightProperty, _font_weight_binding);
|
||||
//
|
||||
var grid = new Grid();
|
||||
Grid.SetColumn(security_view, 1);
|
||||
grid.ColumnDefinitions.Add(new ColumnDefinition { Width = GridLength.Auto });
|
||||
grid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) });
|
||||
grid.Children.Add(toggle_button);
|
||||
grid.Children.Add(security_view);
|
||||
return grid;
|
||||
}
|
||||
|
||||
protected override void OnSortDirectionChanged(ListSortDirection? new_value) => _cached_sort_direction = new_value;
|
||||
}
|
||||
#endregion
|
||||
|
||||
static class RenderUtils {
|
||||
public static readonly Brush _working_order_brush = 0xffedcdu.rgb_brush();
|
||||
public static readonly GlyphContext _child_glyph_context = TextUtils.StandardGlyphContext.with(FontWeights.Light);
|
||||
public static double font_size = FontSizes.Small;
|
||||
|
||||
public static readonly IValueConverter
|
||||
_sub_type_text_converter = new ui.spark.converters.OrderSubTypeDisplayTextConverter(),
|
||||
_side_brush_converter = new AnonymousValueConverter<Side, Brush>(s => s == Side.Buy ? BTSColors.MarketData.bid : BTSColors.MarketData.ask),
|
||||
_font_weight_converter = new AnonymousValueConverter<bool, FontWeight>(is_child => is_child ? FontWeights.Light : FontWeights.Normal);
|
||||
public static readonly IMultiValueConverter
|
||||
_price_text_converter = new ui.spark.converters.TickDisplayConverter(),
|
||||
_background_converter = new AnonymousMultiValueConverter<bool, OrderStatus, Brush>(
|
||||
(selected, status) => selected || status != OrderStatus.Working ? Brushes.Transparent : _working_order_brush);
|
||||
|
||||
public static BindingBase _get_background_binding() {
|
||||
var mb = new MultiBinding { Converter = _background_converter };
|
||||
mb.Bindings.Add(new Binding(nameof(ListBoxItem.IsSelected)) { RelativeSource = new RelativeSource(RelativeSourceMode.FindAncestor, typeof(ListBoxItem), 1) });
|
||||
mb.Bindings.Add(new Binding(nameof(CVOrderBookRowVM.status)));
|
||||
return mb;
|
||||
}
|
||||
|
||||
static RenderUtils() => _working_order_brush.Freeze();
|
||||
|
||||
public static GlyphContext get_glyph_context(CVOrderBookRowVM r) => r.is_child ? _child_glyph_context : TextUtils.StandardGlyphContext;
|
||||
public static Brush get_side_brush(CVOrderBookRowVM r) => r.side.value == Side.Buy ? BTSColors.MarketData.bid : BTSColors.MarketData.ask;
|
||||
public static BindingBase get_font_weight_binding() => new Binding(nameof(CVOrderBookRowVM.is_child)) { Converter = _font_weight_converter };
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,205 @@
|
||||
using bts.api;
|
||||
using bts.api.orderman;
|
||||
using bts.spark;
|
||||
using mbts.ui;
|
||||
using mbts.ui.shell;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Windows.Input;
|
||||
using OrderStatus = bts.api.orderman.OrderStatus;
|
||||
|
||||
namespace mbts.spark {
|
||||
partial class CVOrderBookRowVM : ViewModelBase, IEquatable<CVOrderBookRowVM>, ui.controls.DIYGrid.IRowVM, ui.controls.DIYGrid.IActionableRowVM {
|
||||
readonly CVOrderBookVM _book_vm;
|
||||
readonly OrdersModel _model;
|
||||
readonly Property<bool> _visible, _expanded;
|
||||
readonly HashSet<uint> _child_order_ids = new HashSet<uint>();
|
||||
readonly ObservantCommand _show_order_modify_cmd;
|
||||
OrderRecordImmutable _order;
|
||||
SecurityImmutable? _security;
|
||||
|
||||
public CVOrderBookRowVM(IBus bus, CVOrderBookVM source_vm, OrdersModel model, OrderRecordImmutable record, SecurityImmutable? s) {
|
||||
_book_vm = source_vm;
|
||||
_model = model;
|
||||
_order = record;
|
||||
_security = s;
|
||||
// cells
|
||||
security = CellFactory.create_nullable(this, _book_vm.columns.security_column, r => r._security, r => r.security, v => v.HasValue ? v.Value.ToString() : "unknown security");
|
||||
security_key = CellFactory.create(this, _book_vm.columns.security_key_column, r => r._order.security_key(), r => r.security_key);
|
||||
parent_order_key = CellFactory.create(this, _book_vm.columns.parent_key_column, r => r._order.ParentKey, r => r.parent_order_key);
|
||||
order_key = CellFactory.create(this, _book_vm.columns.order_key_column, r => r._order.Key, r => r.order_key);
|
||||
time_created = CellFactory.create(this, _book_vm.columns.time_created_column, r => r._order.created(), r => r.time_created, format_date);
|
||||
user = CellFactory.create(this, _book_vm.columns.user_column, r => _order.user(), r => r.user);
|
||||
account = CellFactory.create(this, _book_vm.columns.account_column, r => r._order.account(), r => r.account);
|
||||
exchange = CellFactory.create(this, _book_vm.columns.exchange_column, r => r._security.HasValue ? _security.Value.Exchange : Venue.Unset, r => r.exchange);
|
||||
time_in_force = CellFactory.create(this, _book_vm.columns.time_in_force_column, r => r._order.time_in_force(), r => r.time_in_force);
|
||||
order_type = CellFactory.create(this, _book_vm.columns.order_type_column, r => r._order.order_type(), r => r.order_type);
|
||||
order_sub_type = CellFactory.create(this, _book_vm.columns.sub_type_column, r => r._order.sub_type(), r => r.order_sub_type, format_sub_type);
|
||||
side = CellFactory.create(this, _book_vm.columns.side_column, r => r._order.side(), r => r.side);
|
||||
last_change = CellFactory.create(this, _book_vm.columns.last_change_column, r => _order.last_change(), r => r.last_change, format_date);
|
||||
price = CellFactory.create(this, _book_vm.columns.price_column, r => _order.price(), r => r.price, format_price);
|
||||
quantity = CellFactory.create(this, _book_vm.columns.qty_column, r => _order.quantity(), r => r.quantity);
|
||||
filled_quantity = CellFactory.create(this, _book_vm.columns.filled_qty_column, r => _order.filled(), r => r.filled_quantity);
|
||||
avg_price = CellFactory.create(this, _book_vm.columns.avg_price_column, r => _order.avg_price(), r => r.avg_price, format_price);
|
||||
last_price = CellFactory.create(this, _book_vm.columns.last_price_column, r => _order.last_price(), r => r.last_price, format_price);
|
||||
status = CellFactory.create(this, _book_vm.columns.status_column, r => _order.status(), r => r.status);
|
||||
last_msg = CellFactory.create(this, _book_vm.columns.last_message_column, r => _order.last_msg(), r => r.last_msg);
|
||||
//
|
||||
string format_price(double v) => _security.HasValue ? _security.Value.ContractSpec.format(v) : v.ToString();
|
||||
string format_date(DateTime v) => $"{v:M/d} {v:h:mm:ss.fff tt}";
|
||||
string format_sub_type(OrderSubType v) {
|
||||
switch (v) {
|
||||
case OrderSubType.HedgingLeg: return "H";
|
||||
case OrderSubType.QuotingLeg: return "Q";
|
||||
default: return string.Empty;
|
||||
}
|
||||
}
|
||||
//
|
||||
_visible = backing(nameof(is_visible), record.has_parent() && source_vm.get_order_vm(record.ParentKey) is CVOrderBookRowVM parent ? parent.is_visible && parent.is_expanded : true);
|
||||
_expanded = backing(nameof(is_expanded), true);
|
||||
toggle_command = new DelegateCommand(() => is_expanded = !is_expanded);
|
||||
_show_order_modify_cmd = new ObservantCommand(() => bus.post(SparkTags.ShowModifyOrderTag, _order.Key));
|
||||
_show_order_modify_cmd.OnNext(status.value == OrderStatus.Working);
|
||||
}
|
||||
|
||||
public event Action invalidated;
|
||||
|
||||
public OrderRecordImmutable order => _order;
|
||||
// Core
|
||||
public OrderBookCell<uint> order_key { get; }
|
||||
public OrderBookCell<uint> parent_order_key { get; }
|
||||
public OrderBookCell<uint> security_key { get; }
|
||||
public OrderBookCell<SecurityImmutable?> security { get; }
|
||||
public OrderBookCell<Venue> exchange { get; }
|
||||
// Def
|
||||
public OrderBookCell<CustomOrderDefinition.CustomOneofCase> order_type { get; }
|
||||
public OrderBookCell<Side> side { get; }
|
||||
public OrderBookCell<string> user { get; }
|
||||
public OrderBookCell<string> account { get; }
|
||||
public OrderBookCell<TimeInForce> time_in_force { get; }
|
||||
public OrderBookCell<DateTime> time_created { get; }
|
||||
// State
|
||||
public OrderBookCell<DateTime> last_change { get; }
|
||||
public OrderBookCell<OrderStatus> status { get; }
|
||||
public OrderBookCell<double> price { get; }
|
||||
public OrderBookCell<uint> quantity { get; }
|
||||
public OrderBookCell<uint> filled_quantity { get; }
|
||||
public OrderBookCell<double> last_price { get; }
|
||||
public OrderBookCell<double> avg_price { get; }
|
||||
public OrderBookCell<string> last_msg { get; }
|
||||
public OrderBookCell<OrderSubType> order_sub_type { get; }
|
||||
public string order_sub_type_abbreviation => _order.Def.SubType == OrderSubType.None ? string.Empty : _order.Def.SubType.ToString().Substring(0, 1);
|
||||
public OrderMetadata order_metadata => _model.factory.get_metadata(order_type.value);
|
||||
// Commands
|
||||
public ICommand toggle_command { get; }
|
||||
ICommand ui.controls.DIYGrid.IActionableRowVM.default_command => _show_order_modify_cmd;
|
||||
// Accessories
|
||||
public bool is_parent => _child_order_ids.Count > 0;
|
||||
public bool is_child => _order.has_parent();
|
||||
public IReadOnlyCollection<uint> child_order_ids => _child_order_ids;
|
||||
public bool is_visible { get => _visible.Value; set => _visible.set(value); }
|
||||
public bool is_expanded {
|
||||
get => _expanded.Value;
|
||||
set {
|
||||
_expanded.set(value);
|
||||
foreach (var k in child_order_ids) {
|
||||
if (_book_vm.get_order_vm(k) is CVOrderBookRowVM vm)
|
||||
vm.is_visible = value;
|
||||
}
|
||||
}
|
||||
}
|
||||
public int depth_level {
|
||||
get {
|
||||
int i = 1;
|
||||
var curr = _order;
|
||||
while (curr.has_parent() && _book_vm.try_get_order(curr.ParentKey, out curr))
|
||||
i++;
|
||||
return i;
|
||||
}
|
||||
}
|
||||
|
||||
// '_dynamic_render_cells' are presented by 'RenderColumns,' and may change value during the lifetime of this row
|
||||
// Note: by instantiating all these cells inside a CellPropertyGroup, we can update all of them with a single call.
|
||||
// this reduces the risk of accidentally failing to update one of the cells, esp if we make changes to them down the road.
|
||||
IEnumerable<IOrderBookCell> _dynamic_render_cells {
|
||||
get {
|
||||
yield return last_change;
|
||||
yield return price;
|
||||
yield return quantity;
|
||||
yield return filled_quantity;
|
||||
yield return avg_price;
|
||||
yield return last_price;
|
||||
yield return status;
|
||||
yield return last_msg;
|
||||
}
|
||||
}
|
||||
|
||||
IEnumerable<IOrderBookCell> _security_dependent_cells {
|
||||
get {
|
||||
yield return security;
|
||||
yield return price;
|
||||
yield return avg_price;
|
||||
yield return last_price;
|
||||
}
|
||||
}
|
||||
|
||||
public void add_child_order(uint child_order_id) {
|
||||
_child_order_ids.Add(child_order_id);
|
||||
// we don't need to raise this every single time
|
||||
if (_child_order_ids.Count == 1)
|
||||
RaisePropertyChanged("is_parent");
|
||||
_book_vm.get_order_vm(child_order_id)?.on_parent_updated();
|
||||
}
|
||||
|
||||
public void remove_child_order(uint child_order_id) {
|
||||
if (_child_order_ids.Remove(child_order_id)) {
|
||||
if (_child_order_ids.Count == 0)
|
||||
RaisePropertyChanged(nameof(is_parent));
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Raises PropertyChanged events for all live sorting properties, then calls parent_updated() on all descendents.
|
||||
/// <para>Note: call this method when parent order is added, removed, or changed.</para>
|
||||
/// </summary>
|
||||
public void on_parent_updated() {
|
||||
foreach (var p in _book_vm.live_sorting_props)
|
||||
RaisePropertyChanged(p);
|
||||
is_visible = _book_vm.get_order_vm(parent_order_key.value) is CVOrderBookRowVM parent ? parent.is_visible && parent.is_expanded : true;
|
||||
foreach (var k in _child_order_ids) {
|
||||
var child_vm = _book_vm.get_order_vm(k);
|
||||
if (child_vm != null)
|
||||
child_vm.on_parent_updated();
|
||||
}
|
||||
}
|
||||
|
||||
public void update_order(OrderRecordImmutable order) {
|
||||
_order = order;
|
||||
// use Count(), as opposed to Any(), to force it to iterate through all cells
|
||||
if (_dynamic_render_cells.Count(c => c.refresh()) > 0)
|
||||
invalidated?.Invoke();
|
||||
_show_order_modify_cmd.OnNext(status.value == OrderStatus.Working);
|
||||
foreach (var p in _book_vm.live_sorting_props)
|
||||
RaisePropertyChanged(p);
|
||||
foreach (var child in _child_order_ids)
|
||||
_book_vm.get_order_vm(child)?.on_parent_updated();
|
||||
}
|
||||
|
||||
public void update_security(SecurityImmutable security) {
|
||||
_security = security;
|
||||
// use Count(), as opposed to Any(), to force it to iterate through all cells
|
||||
if (_security_dependent_cells.Count(c => c.refresh()) > 0)
|
||||
invalidated?.Invoke();
|
||||
if (_book_vm.live_sorting_props.Contains(nameof(security)))
|
||||
RaisePropertyChanged(nameof(security));
|
||||
foreach (var child in _child_order_ids)
|
||||
_book_vm.get_order_vm(child)?.on_parent_updated();
|
||||
}
|
||||
|
||||
public bool Equals(CVOrderBookRowVM other) => other != null && other.order_key == order_key;
|
||||
public override bool Equals(object obj) => Equals(obj as CVOrderBookRowVM);
|
||||
public override int GetHashCode() => _order.GetHashCode();
|
||||
public override string ToString() => $"{order_key} {last_change} {parent_order_key}";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,212 @@
|
||||
using bts.api;
|
||||
using bts.api.core;
|
||||
using bts.api.orderman;
|
||||
using mbts.collections;
|
||||
using mbts.ui;
|
||||
using mbts.ui.controls;
|
||||
using mbts.ui.shell;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.ObjectModel;
|
||||
using System.Collections.Specialized;
|
||||
using System.ComponentModel;
|
||||
using System.Linq;
|
||||
using System.Reactive.Linq;
|
||||
using System.Windows.Controls;
|
||||
using System.Windows.Data;
|
||||
using static bts.shell.OrderBookWindowFactory;
|
||||
|
||||
namespace mbts.spark {
|
||||
partial class CVOrderBookVM : DisposableViewModelBase, IHandleRowSelection, IOrderBookVM {
|
||||
// these come from -> ((ICollectionViewLiveShaping)rows_view).LiveSortingProperties
|
||||
// each row accesses the public 'live_sorting_props' when it, or an ancestor order, is updated,
|
||||
// copying the collection locally avoids constantly casting rows_view to ICollectionViewLiveShaping.
|
||||
readonly ObservableCollection<string> _live_sorting_props;
|
||||
readonly Property<CVOrderBookRowVM> _current_row;
|
||||
|
||||
IReadOnlyDictionary<uint, OrderRecordImmutable> _orders_cache = new Dictionary<uint, OrderRecordImmutable>();
|
||||
Dictionary<uint, CVOrderBookRowVM> _order_vms_cache = new Dictionary<uint, CVOrderBookRowVM>();
|
||||
ObservableCollection<CVOrderBookRowVM> _order_vms = new ObservableCollection<CVOrderBookRowVM>();
|
||||
ObservableCollection<CVOrderBookRowVM> _selected_rows = new ObservableCollection<CVOrderBookRowVM>();
|
||||
SetMap<uint, uint> _parent_to_children = new SetMap<uint, uint>();
|
||||
SetMap<uint, uint> _security_orders = new SetMap<uint, uint>();
|
||||
|
||||
public CVOrderBookVM(IBus bus, OrdersModel model, OrderBookStartupData state) {
|
||||
columns = new CVOrderBookColumnCollection(model, state);
|
||||
rows_view = CollectionViewSource.GetDefaultView(_order_vms);
|
||||
columns.filter_changed += rows_view.Refresh;
|
||||
var cvls = (ICollectionViewLiveShaping)rows_view;
|
||||
_live_sorting_props = cvls.LiveSortingProperties;
|
||||
cvls.IsLiveFiltering = true;
|
||||
cvls.IsLiveSorting = true;
|
||||
cvls.LiveFilteringProperties.Add(nameof(CVOrderBookRowVM.is_visible));
|
||||
rows_view.Filter = obj => {
|
||||
if (obj is CVOrderBookRowVM vm) {
|
||||
if (!vm.is_visible)
|
||||
return false;
|
||||
if (vm.child_order_ids.Count > 0 && vm.is_expanded) {
|
||||
foreach (var child_key in vm.child_order_ids) {
|
||||
if (columns.filter(_order_vms_cache[child_key]))
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return columns.filter(vm);
|
||||
}
|
||||
return false;
|
||||
};
|
||||
_current_row = backing<CVOrderBookRowVM>(nameof(current_row));
|
||||
if (state?.sort_criteria is List<SortingCriteria> lsc && lsc.Count > 0) {
|
||||
foreach (var sc in lsc) {
|
||||
rows_view.SortDescriptions.Add(new SortDescription(sc.col_name, sc.is_ascending ? ListSortDirection.Ascending : ListSortDirection.Descending));
|
||||
if (!_live_sorting_props.Contains(sc.col_name))
|
||||
_live_sorting_props.Add(sc.col_name);
|
||||
}
|
||||
} else {
|
||||
rows_view.SortDescriptions.Add(new SortDescription(nameof(CVOrderBookRowVM.time_created), ListSortDirection.Descending));
|
||||
_live_sorting_props.Add(nameof(CVOrderBookRowVM.time_created));
|
||||
}
|
||||
|
||||
SetMap<uint, uint> missing_security_orders = new SetMap<uint, uint>();
|
||||
IReadOnlyDictionary<uint, SecurityMD> security_dict = new Dictionary<uint, SecurityMD>();
|
||||
|
||||
_dispose = Disposable.Create(
|
||||
bus.subscribe(SparkTags.SecuritiesTag).ObserveOnDispatcher().Subscribe(on_securities),
|
||||
model.on_orders.ObserveOnDispatcher().Subscribe(on_orders),
|
||||
columns);
|
||||
|
||||
void on_orders(TableUpdate<OrderRecordImmutable, uint> table) {
|
||||
// check sum (seems like there is a bug in orders api, see repro steps below)
|
||||
// 1) with an CVOrderBook open, enter a few 500+ lot iceberg order with < 20 show_quantity
|
||||
// 2) wait until the orders are filled, or until fills inexplicably stop coming in
|
||||
// 3) open another CVOrderBook, all the orders in the new book will have 'Status = inactive'
|
||||
// 4) open the log file and scroll to the bottom to find a log-message that came from the code below
|
||||
IEnumerable<OrderRecordImmutable> added, removed;
|
||||
if (_orders_cache.Count != (table.state.Count + table.removed.Count - table.added.Count)) {
|
||||
Logger.error(bts.log.Subsystem.Ui, $"CVOrderBook detected non-incremental changes in orders api, will delete all rows and (re)add everything in api state: " +
|
||||
$"orderbook count before update({_orders_cache.Count}), update.state count({table.state.Count}), update.added count({table.added.Count}), update.removed count({table.removed.Count}).");
|
||||
_order_vms.Clear();
|
||||
_parent_to_children.clear();
|
||||
added = table.state.Values;
|
||||
removed = Enumerable.Empty<OrderRecordImmutable>();
|
||||
} else {
|
||||
added = table.added;
|
||||
removed = table.removed;
|
||||
}
|
||||
_orders_cache = table.state;
|
||||
var changed_keys = new HashSet<uint>();
|
||||
foreach (var o in removed) {
|
||||
// if state has it, then it wasn't actually removed, just changed
|
||||
if (table.state.TryGetValue(o.Key, out OrderRecordImmutable changed_order)) {
|
||||
changed_keys.Add(o.Key);
|
||||
_order_vms_cache[o.Key].update_order(changed_order);
|
||||
} else if (_order_vms_cache.TryPopValue(o.Key, out CVOrderBookRowVM vm)) {
|
||||
_security_orders.remove(o.Def.SecurityKey, o.Key);
|
||||
_order_vms.Remove(vm);
|
||||
if (o.has_parent()) {
|
||||
_parent_to_children.remove(o.ParentKey, o.Key);
|
||||
get_order_vm(o.ParentKey)?.remove_child_order(o.Key);
|
||||
}
|
||||
if (_parent_to_children.try_pop(o.Key, out ISet<uint> children)) {
|
||||
foreach (var k in children)
|
||||
_order_vms_cache[k].on_parent_updated();
|
||||
}
|
||||
missing_security_orders.remove(o.Key);
|
||||
}
|
||||
}
|
||||
var orphans = new List<CVOrderBookRowVM>();
|
||||
foreach (var o in added) {
|
||||
if (changed_keys.Contains(o.Key))
|
||||
continue;
|
||||
_security_orders.add(o.Def.SecurityKey, o.Key);
|
||||
var vm = new CVOrderBookRowVM(bus, this, model, o, security_dict.TryGetValue(o.Def.SecurityKey, out SecurityMD smd) ? new SecurityImmutable?(smd.security) : null);
|
||||
if (!vm.security.value.HasValue)
|
||||
missing_security_orders.add(o.Def.SecurityKey, o.Key);
|
||||
_order_vms_cache.Add(o.Key, vm);
|
||||
if (_parent_to_children.try_lookup(o.Key, out ISet<uint> children))
|
||||
foreach (var k in children) {
|
||||
// ** if children arrived in earlier update, this will notify them
|
||||
// that their parent has arrived, and cause them to be properly sorted **
|
||||
vm.add_child_order(k);
|
||||
}
|
||||
if (o.has_parent()) {
|
||||
_parent_to_children.add(o.ParentKey, o.Key);
|
||||
if (_order_vms_cache.TryGetValue(o.ParentKey, out CVOrderBookRowVM pvm)) {
|
||||
pvm.add_child_order(o.Key);
|
||||
} else {
|
||||
// look for orphans' parents again after all new orders have been added to _order_vm_cache
|
||||
orphans.Add(vm);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
_order_vms.Add(vm);
|
||||
}
|
||||
// ** add the orphans now, most likely their parent was added after them in the iteration
|
||||
// if not, they will be notified when their parent arrives, and be properly sorted then. **
|
||||
foreach (var vm in orphans) {
|
||||
get_order_vm(vm.parent_order_key.value)?.add_child_order(vm.order_key.value);
|
||||
_order_vms.Add(vm);
|
||||
}
|
||||
order_received?.Invoke(this, new EventArgs());
|
||||
}
|
||||
|
||||
// I assume we don't need to worry about orders "losing" or "changing" their security
|
||||
// once the order vm discovers its security (by id), it will never change.
|
||||
void on_securities(IReadOnlyDictionary<uint, SecurityMD> securities) {
|
||||
security_dict = securities;
|
||||
foreach (var missing_sec_orders in missing_security_orders.ToList()) {
|
||||
if (securities.TryGetValue(missing_sec_orders.Key, out SecurityMD smd)) {
|
||||
missing_security_orders.remove(missing_sec_orders.Key);
|
||||
foreach (var order_key in missing_sec_orders) {
|
||||
var vm = _order_vms_cache[order_key];
|
||||
vm.update_security(smd.security);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public event EventHandler<OrderRecordImmutable?> selected_order_changed;
|
||||
public event EventHandler order_received;
|
||||
|
||||
public CVOrderBookColumnCollection columns { get; }
|
||||
public IReadOnlyList<string> live_sorting_props => _live_sorting_props;
|
||||
public ICollectionView rows_view { get; }
|
||||
// this is a misnomer, but i want to support both kinds of OrderBookVMs, and OrderBookView currently binds to 'visible_orders,'
|
||||
public ICollectionView visible_orders => rows_view;
|
||||
public CVOrderBookRowVM current_row { get => _current_row.Value; set => _current_row.Value = value ?? _selected_rows.FirstOrDefault(); }
|
||||
public OrderRecordImmutable? current_order => current_row?.order;
|
||||
|
||||
public IEnumerable<uint> active_order_keys => _active_order_records.Select(o => o.Key);
|
||||
public IEnumerable<uint> active_buy_order_keys => _active_order_records.Where(o => o.Def.Side == Side.Buy).Select(o => o.Key);
|
||||
public IEnumerable<uint> active_sell_order_keys => _active_order_records.Where(o => o.Def.Side == Side.Sell).Select(o => o.Key);
|
||||
public IEnumerable<OrderRecordImmutable> selected_orders => _selected_rows.Select(r => r.order);
|
||||
|
||||
IEnumerable<OrderRecordImmutable> _active_order_records => _order_vms.Where(x => x.order.is_active()).Select(x => x.order);
|
||||
|
||||
public bool try_get_order(uint key, out OrderRecordImmutable order) =>
|
||||
_orders_cache.TryGetValue(key, out order);
|
||||
|
||||
public CVOrderBookRowVM get_order_vm(uint key) =>
|
||||
_order_vms_cache.TryGetValue(key, out CVOrderBookRowVM vm) ? vm : null;
|
||||
|
||||
// need to add sort_member_path to ICollectionView.LiveSortingProperties
|
||||
// since child-orders' sort position must always follow their parent,
|
||||
// if their parent is added after the child, the sort order must be updated.
|
||||
public void on_sorting(DIYGridSortingEventArgs e) {
|
||||
if (e.Column.SortDirection.HasValue) {
|
||||
if (!_live_sorting_props.Contains(e.Column.sort_member_path))
|
||||
_live_sorting_props.Add(e.Column.sort_member_path);
|
||||
} else {
|
||||
_live_sorting_props.Remove(e.Column.sort_member_path);
|
||||
}
|
||||
}
|
||||
|
||||
public void row_selection_changed(SelectionChangedEventArgs e) {
|
||||
RowSelectionHelper.apply_changes(_selected_rows, e);
|
||||
selected_order_changed?.Invoke(this, current_order);
|
||||
}
|
||||
|
||||
public List<SortingCriteria> get_sorting_states() => rows_view.GetSortingCriteria();
|
||||
public FilterStates get_filter_states() => columns.get_filter_states();
|
||||
}
|
||||
}
|
||||
@@ -22,7 +22,7 @@ using static mbts.spark.OrderBookView;
|
||||
using static mbts.ui.controls.DIYGrid;
|
||||
|
||||
namespace mbts.spark {
|
||||
partial class OrderBookVM : DisposableViewModelBase, IHandleRowSelection {
|
||||
partial class OrderBookVM : DisposableViewModelBase, IHandleRowSelection, IOrderBookVM {
|
||||
readonly OrderBookWindowFactory _creator;
|
||||
readonly OrdersModel _model;
|
||||
ObservableCollection<OrderBookRowVM> _selected_rows = new ObservableCollection<OrderBookRowVM>();
|
||||
@@ -221,8 +221,8 @@ namespace mbts.spark {
|
||||
}
|
||||
}
|
||||
|
||||
public void sort_grid(ColumnBase column) {
|
||||
if (_get_comparer_for_column(column, out Comparer<OrderBookRowVM> sorter)) {
|
||||
public void on_sorting(DIYGridSortingEventArgs e) {
|
||||
if (_get_comparer_for_column(e.Column, out Comparer<OrderBookRowVM> sorter)) {
|
||||
SortedTree<OrderBookRowVM, uint> forest = new SortedTree<OrderBookRowVM, uint>(
|
||||
sorter
|
||||
, row_vm => row_vm.order.Key
|
||||
@@ -236,6 +236,10 @@ namespace mbts.spark {
|
||||
_orders.sync_with(forest);
|
||||
}
|
||||
}
|
||||
// We ALWAYS want to set this here, else the grid tries to be helpful
|
||||
// and sort with its own CollectionView, which destroys the SortedTree
|
||||
// sort order; we want it to never use the CollectionView
|
||||
e.Handled = true;
|
||||
}
|
||||
|
||||
private bool _get_comparer_for_sort_description(SortDescription sc, out Comparer<OrderBookRowVM> s) {
|
||||
@@ -434,6 +438,7 @@ namespace mbts.spark {
|
||||
public OrderBookRowVM current_row { get => _current_row.Value; set => _current_row.Value = value ?? _selected_rows.FirstOrDefault(); }
|
||||
public OrderRecordImmutable? current_order => current_row?.order;
|
||||
public ObservableCollection<OrderBookRowVM> selected_rows => _selected_rows;
|
||||
public IEnumerable<OrderRecordImmutable> selected_orders => _selected_rows.Select(r => r.order);
|
||||
|
||||
#region Filters
|
||||
public IDataGridColumnPersistentFilter<OrderBookRowVM, DoubleMinMaxState> price_filter { get; } = new DoubleMinMax<OrderBookRowVM>(r => r.price, nameof(OrderBookRowVM.price));
|
||||
@@ -456,6 +461,7 @@ namespace mbts.spark {
|
||||
public IDataGridColumnPersistentFilter<OrderBookRowVM, string> exchange_message_filter { get; }
|
||||
public IDataGridColumnPersistentFilter<OrderBookRowVM, string> security_filter { get; } = new RegexFilterVM<OrderBookRowVM>(r => r.security_text, nameof(OrderBookRowVM.security_text));
|
||||
|
||||
|
||||
public FilterStates get_filter_states() =>
|
||||
new FilterStates {
|
||||
order_key_regex = order_key_filter.get_state(),
|
||||
|
||||
@@ -53,24 +53,57 @@
|
||||
<Style x:Key="RightAlignedSideColorCodedTextElement" TargetType="{x:Type TextBlock}" BasedOn="{StaticResource SideColorCodedTextElement}">
|
||||
<Setter Property="TextAlignment" Value="Right" />
|
||||
</Style>
|
||||
|
||||
<Style x:Key="{x:Static sp:OrderBookView.RowStyleKey}" TargetType="ListBoxItem" BasedOn="{StaticResource DIYGridRowStyle}">
|
||||
<Style.Triggers>
|
||||
<MultiDataTrigger>
|
||||
<MultiDataTrigger.Conditions>
|
||||
<Condition Binding="{Binding IsSelected, RelativeSource={RelativeSource Self}}" Value="False"/>
|
||||
<Condition Binding="{Binding status}" Value="{uc:Enum Working, Type=order:OrderStatus}"/>
|
||||
</MultiDataTrigger.Conditions>
|
||||
<MultiDataTrigger.Setters>
|
||||
<Setter Property="Background" Value="{x:Static Member=working_order_background_brush, MemberType={uc:Nested uc:BTSColors.Orders}}"/>
|
||||
</MultiDataTrigger.Setters>
|
||||
</MultiDataTrigger>
|
||||
</Style.Triggers>
|
||||
</Style>
|
||||
|
||||
<Style x:Key="{x:Static sp:OrderBookView.CVRowStyleKey}" TargetType="ListBoxItem" BasedOn="{StaticResource DIYGridRowStyle}">
|
||||
<Style.Triggers>
|
||||
<MultiDataTrigger>
|
||||
<MultiDataTrigger.Conditions>
|
||||
<Condition Binding="{Binding IsSelected, RelativeSource={RelativeSource Self}}" Value="False"/>
|
||||
<Condition Binding="{Binding status.value}" Value="{uc:Enum Working, Type=order:OrderStatus}"/>
|
||||
</MultiDataTrigger.Conditions>
|
||||
<MultiDataTrigger.Setters>
|
||||
<Setter Property="Background" Value="{x:Static Member=working_order_background_brush, MemberType={uc:Nested uc:BTSColors.Orders}}"/>
|
||||
</MultiDataTrigger.Setters>
|
||||
</MultiDataTrigger>
|
||||
</Style.Triggers>
|
||||
</Style>
|
||||
|
||||
<DataTemplate x:Key="{x:Static sp:OrderBookView.RowTemplateKey}">
|
||||
<sp:OrderBookRowView/>
|
||||
</DataTemplate>
|
||||
|
||||
<DataTemplate x:Key="{x:Static sp:OrderBookView.CVRowTemplateKey}">
|
||||
<sp:CVOrderBookRowView/>
|
||||
</DataTemplate>
|
||||
</ResourceDictionary>
|
||||
</UserControl.Resources>
|
||||
<AdornerDecorator>
|
||||
<Grid>
|
||||
<Grid.RowDefinitions>
|
||||
<!-- Table -->
|
||||
<RowDefinition />
|
||||
</Grid.RowDefinitions>
|
||||
<Grid.ColumnDefinitions>
|
||||
<ColumnDefinition />
|
||||
</Grid.ColumnDefinitions>
|
||||
|
||||
|
||||
<GridSplitter Width="2" Grid.Row="2" Grid.Column="0" />
|
||||
|
||||
<!--<c:DataGridEx Grid.Row="2" Grid.Column="1" x:Name="_grid" IsReadOnly="True" BorderThickness="0" GridLinesVisibility="None" CanUserSortColumns="true" SelectionUnit="FullRow" Sorting="data_grid_sorting"
|
||||
RowHeight="{Binding row_height}" ItemsSource="{Binding visible_orders}" CurrentItem="{Binding current_row, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" uc:DataGridBehavior.NotifyColumnsOfSorting="True">
|
||||
<DataGrid.Resources>
|
||||
<c:DIYGrid
|
||||
x:Name="_grid"
|
||||
Grid.Row="2"
|
||||
Columns="{Binding columns}"
|
||||
CurrentRow="{Binding current_row, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
|
||||
Rows="{Binding visible_orders}"
|
||||
RowHeight="{x:Static sp:OrderBookView.RowHeight}"
|
||||
RowStyle="{StaticResource {x:Static sp:OrderBookView.ActiveRowStyleKey}}"
|
||||
RowTemplate="{StaticResource {x:Static sp:OrderBookView.ActiveRowTemplateKey}}"
|
||||
Sorting="data_grid_sorting"
|
||||
>
|
||||
<c:DIYGrid.Resources>
|
||||
<ResourceDictionary>
|
||||
<SolidColorBrush x:Key="TreeViewItem.TreeArrow.Static.Checked.Fill" Color="#FF595959"/>
|
||||
<SolidColorBrush x:Key="TreeViewItem.TreeArrow.Static.Checked.Stroke" Color="#FF262626"/>
|
||||
@@ -133,305 +166,18 @@
|
||||
</Style.Triggers>
|
||||
</Style>
|
||||
</ResourceDictionary>
|
||||
</DataGrid.Resources>
|
||||
<DataGrid.RowStyle>
|
||||
<Style TargetType="DataGridRow">
|
||||
<Setter Property="uc:AttachedCommands.DoubleClick" Value="{Binding show_order_modify_command}"/>
|
||||
<Setter Property="IsSelected" Value="{Binding is_selected, Mode=OneWayToSource, UpdateSourceTrigger=PropertyChanged}"/>
|
||||
<Setter Property="Visibility" Value="{Binding is_visible, Converter={StaticResource BoolToVisibility}}" />
|
||||
<Style.Triggers>
|
||||
<DataTrigger Binding="{Binding is_child}" Value="true">
|
||||
<Setter Property="FontWeight" Value="Light" />
|
||||
</DataTrigger>
|
||||
<DataTrigger Binding="{Binding status}" Value="Working">
|
||||
<Setter Property="Background" Value="#ffedcd" />
|
||||
</DataTrigger>
|
||||
</Style.Triggers>
|
||||
</Style>
|
||||
</DataGrid.RowStyle>
|
||||
<DataGrid.Columns>
|
||||
<DataGridTemplateColumn Header="Security" SortMemberPath="security" IsReadOnly="true"
|
||||
c:DataGridEx.ColumnName="security_text"
|
||||
c:DataGridFilterProperties.Filter="{Binding DataContext.security_filter, Source={uc:DirectReference _grid}}"
|
||||
c:DataGridFilterProperties.FilterActive="{Binding DataContext.security_filter.active, Source={uc:DirectReference _grid}}"
|
||||
c:DataGridFilterProperties.FilterOpen="{Binding DataContext.security_filter.open, Source={uc:DirectReference _grid}}"
|
||||
c:DataGridFilterProperties.FilterTemplate="{StaticResource RegexTemplate}">
|
||||
<DataGridTemplateColumn.CellTemplate>
|
||||
<DataTemplate>
|
||||
<StackPanel Orientation="Horizontal">
|
||||
<ToggleButton x:Name="toggle_button" Command="{Binding toggle_command}" IsChecked="{Binding is_expanded}" ClickMode="Press" Style="{StaticResource ExpandCollapseToggleStyle}" Margin="{Binding Margin}"/>
|
||||
<TextBlock Text="{Binding security_text}" />
|
||||
</StackPanel>
|
||||
<DataTemplate.Triggers>
|
||||
<DataTrigger Binding="{Binding is_parent}" Value="False">
|
||||
<Setter TargetName="toggle_button" Property="Visibility" Value="Hidden" />
|
||||
</DataTrigger>
|
||||
</DataTemplate.Triggers>
|
||||
</DataTemplate>
|
||||
</DataGridTemplateColumn.CellTemplate>
|
||||
</DataGridTemplateColumn>
|
||||
<DataGridTextColumn Header="Time" Binding="{Binding time_created, StringFormat='{}{0,5:M/d} {0,15:h:mm:ss.fff tt}'}"
|
||||
ElementStyle="{StaticResource RightAlignedColumn}"
|
||||
c:DataGridEx.ColumnName="time_created"
|
||||
c:DataGridFilterProperties.Filter="{Binding DataContext.time_created_filter, Source={uc:DirectReference _grid}}"
|
||||
c:DataGridFilterProperties.FilterActive="{Binding DataContext.time_created_filter.active, Source={uc:DirectReference _grid}}"
|
||||
c:DataGridFilterProperties.FilterOpen="{Binding DataContext.time_created_filter.open, Source={uc:DirectReference _grid}}"
|
||||
c:DataGridFilterProperties.FilterTemplate="{StaticResource TimeFilterTemplate}"
|
||||
/>
|
||||
<DataGridTextColumn Header="Last Change" Binding="{Binding last_change, StringFormat='{}{0,5:M/d} {0,15:h:mm:ss.fff tt}'}"
|
||||
ElementStyle="{StaticResource RightAlignedColumn}"
|
||||
c:DataGridEx.ColumnName="last_change"
|
||||
c:DataGridFilterProperties.Filter="{Binding DataContext.last_change_filter, Source={uc:DirectReference _grid}}"
|
||||
c:DataGridFilterProperties.FilterActive="{Binding DataContext.last_change_filter.active, Source={uc:DirectReference _grid}}"
|
||||
c:DataGridFilterProperties.FilterOpen="{Binding DataContext.last_change_filter.open, Source={uc:DirectReference _grid}}"
|
||||
c:DataGridFilterProperties.FilterTemplate="{StaticResource TimeFilterTemplate}"
|
||||
/>
|
||||
<DataGridTextColumn Header="User" Binding="{Binding user}"
|
||||
c:DataGridEx.ColumnName="user"
|
||||
c:DataGridFilterProperties.Filter="{Binding DataContext.user_filter, Source={uc:DirectReference _grid}}"
|
||||
c:DataGridFilterProperties.FilterActive="{Binding DataContext.user_filter.active, Source={uc:DirectReference _grid}}"
|
||||
c:DataGridFilterProperties.FilterOpen="{Binding DataContext.user_filter.open, Source={uc:DirectReference _grid}}"
|
||||
c:DataGridFilterProperties.FilterTemplate="{StaticResource DiscreteValuesTemplate}"
|
||||
/>
|
||||
<DataGridTextColumn Header="Account" Binding="{Binding account}"
|
||||
c:DataGridEx.ColumnName="account"
|
||||
c:DataGridFilterProperties.Filter="{Binding DataContext.account_filter, Source={uc:DirectReference _grid}}"
|
||||
c:DataGridFilterProperties.FilterActive="{Binding DataContext.account_filter.active, Source={uc:DirectReference _grid}}"
|
||||
c:DataGridFilterProperties.FilterOpen="{Binding DataContext.account_filter.open, Source={uc:DirectReference _grid}}"
|
||||
c:DataGridFilterProperties.FilterTemplate="{StaticResource DiscreteValuesTemplate}"
|
||||
/>
|
||||
<DataGridTextColumn Header="Exchange" Binding="{Binding exchange}"
|
||||
c:DataGridEx.ColumnName="exchange"
|
||||
c:DataGridFilterProperties.Filter="{Binding DataContext.exchange_filter, Source={uc:DirectReference _grid}}"
|
||||
c:DataGridFilterProperties.FilterActive="{Binding DataContext.exchange_filter.active, Source={uc:DirectReference _grid}}"
|
||||
c:DataGridFilterProperties.FilterOpen="{Binding DataContext.exchange_filter.open, Source={uc:DirectReference _grid}}"
|
||||
c:DataGridFilterProperties.FilterTemplate="{StaticResource EnumValuesTemplate}"
|
||||
/>
|
||||
<DataGridTextColumn Header="TIF" Binding="{Binding tif}"
|
||||
c:DataGridEx.ColumnName="tif"
|
||||
c:DataGridFilterProperties.Filter="{Binding DataContext.tif_filter, Source={uc:DirectReference _grid}}"
|
||||
c:DataGridFilterProperties.FilterActive="{Binding DataContext.tif_filter.active, Source={uc:DirectReference _grid}}"
|
||||
c:DataGridFilterProperties.FilterOpen="{Binding DataContext.tif_filter.open, Source={uc:DirectReference _grid}}"
|
||||
c:DataGridFilterProperties.FilterTemplate="{StaticResource EnumValuesTemplate}"
|
||||
/>
|
||||
<usp:DataGridOrderTypeColumn Header="Order Type" ElementStyle="{StaticResource OrderTypeElement}"
|
||||
c:DataGridEx.ColumnName="order_type"
|
||||
c:DataGridFilterProperties.Filter="{Binding DataContext.order_type_filter, Source={uc:DirectReference _grid}}"
|
||||
c:DataGridFilterProperties.FilterActive="{Binding DataContext.order_type_filter.active, Source={uc:DirectReference _grid}}"
|
||||
c:DataGridFilterProperties.FilterOpen="{Binding DataContext.order_type_filter.open, Source={uc:DirectReference _grid}}"
|
||||
c:DataGridFilterProperties.FilterTemplate="{StaticResource EnumValuesTemplate}"
|
||||
/>
|
||||
<DataGridTextColumn Header="Sub Type" Binding="{Binding order_sub_type, Converter={StaticResource OrderSubTypeDisplayTextConverter}}"
|
||||
c:DataGridEx.ColumnName="order_sub_type_abbreviation"
|
||||
c:DataGridFilterProperties.Filter="{Binding DataContext.order_sub_type_filter, Source={uc:DirectReference _grid}}"
|
||||
c:DataGridFilterProperties.FilterActive="{Binding DataContext.order_sub_type_filter.active, Source={uc:DirectReference _grid}}"
|
||||
c:DataGridFilterProperties.FilterOpen="{Binding DataContext.order_sub_type_filter.open, Source={uc:DirectReference _grid}}">
|
||||
</DataGridTextColumn>
|
||||
<DataGridTextColumn Header="B/S" ElementStyle="{StaticResource SideColorCodedTextElement}" Binding="{Binding side}"
|
||||
c:DataGridEx.ColumnName="side"
|
||||
c:DataGridFilterProperties.Filter="{Binding DataContext.side_filter, Source={uc:DirectReference _grid}}"
|
||||
c:DataGridFilterProperties.FilterActive="{Binding DataContext.side_filter.active, Source={uc:DirectReference _grid}}"
|
||||
c:DataGridFilterProperties.FilterOpen="{Binding DataContext.side_filter.open, Source={uc:DirectReference _grid}}"
|
||||
c:DataGridFilterProperties.FilterTemplate="{StaticResource EnumValuesTemplate}"
|
||||
/>
|
||||
<DataGridTextColumn Header="Qty" Binding="{Binding quantity}"
|
||||
ElementStyle="{StaticResource RightAlignedSideColorCodedTextElement}"
|
||||
c:DataGridEx.ColumnName="quantity"
|
||||
c:DataGridFilterProperties.Filter="{Binding DataContext.qty_filter, Source={uc:DirectReference _grid}}"
|
||||
c:DataGridFilterProperties.FilterActive="{Binding DataContext.qty_filter.active, Source={uc:DirectReference _grid}}"
|
||||
c:DataGridFilterProperties.FilterOpen="{Binding DataContext.qty_filter.open, Source={uc:DirectReference _grid}}"
|
||||
c:DataGridFilterProperties.FilterTemplate="{StaticResource UIntMinMaxTemplate}"
|
||||
/>
|
||||
<DataGridTextColumn Header="Price" ElementStyle="{StaticResource RightAlignedColumn}"
|
||||
c:DataGridEx.ColumnName="price"
|
||||
c:DataGridFilterProperties.Filter="{Binding DataContext.price_filter, Source={uc:DirectReference _grid}}"
|
||||
c:DataGridFilterProperties.FilterActive="{Binding DataContext.price_filter.active, Source={uc:DirectReference _grid}}"
|
||||
c:DataGridFilterProperties.FilterOpen="{Binding DataContext.price_filter.open, Source={uc:DirectReference _grid}}"
|
||||
c:DataGridFilterProperties.FilterTemplate="{StaticResource DoubleMinMaxTemplate}"
|
||||
>
|
||||
<DataGridTextColumn.Binding>
|
||||
<MultiBinding Converter="{StaticResource TickDisplayConverter}">
|
||||
<Binding Path="price"/>
|
||||
<Binding Path="security.ContractSpec"/>
|
||||
</MultiBinding>
|
||||
</DataGridTextColumn.Binding>
|
||||
</DataGridTextColumn>
|
||||
<DataGridTextColumn Header="Filled Qty" Binding="{Binding filled_quantity}"
|
||||
ElementStyle="{StaticResource RightAlignedSideColorCodedTextElement}"
|
||||
c:DataGridEx.ColumnName="filled_quantity"
|
||||
c:DataGridFilterProperties.Filter="{Binding DataContext.qty_filled_filter, Source={uc:DirectReference _grid}}"
|
||||
c:DataGridFilterProperties.FilterActive="{Binding DataContext.qty_filled_filter.active, Source={uc:DirectReference _grid}}"
|
||||
c:DataGridFilterProperties.FilterOpen="{Binding DataContext.qty_filled_filter.open, Source={uc:DirectReference _grid}}"
|
||||
c:DataGridFilterProperties.FilterTemplate="{StaticResource UIntMinMaxTemplate}"
|
||||
/>
|
||||
<DataGridTextColumn Header="Avg Filled" ElementStyle="{StaticResource RightAlignedColumn}"
|
||||
c:DataGridEx.ColumnName="avg_price"
|
||||
c:DataGridFilterProperties.Filter="{Binding DataContext.avg_price_filter, Source={uc:DirectReference _grid}}"
|
||||
c:DataGridFilterProperties.FilterActive="{Binding DataContext.avg_price_filter.active, Source={uc:DirectReference _grid}}"
|
||||
c:DataGridFilterProperties.FilterOpen="{Binding DataContext.avg_price_filter.open, Source={uc:DirectReference _grid}}"
|
||||
c:DataGridFilterProperties.FilterTemplate="{StaticResource DoubleMinMaxTemplate}">
|
||||
<DataGridTextColumn.Binding>
|
||||
<MultiBinding Converter="{StaticResource TickDisplayConverter}">
|
||||
<Binding Path="avg_price"/>
|
||||
<Binding Path="security.ContractSpec"/>
|
||||
</MultiBinding>
|
||||
</DataGridTextColumn.Binding>
|
||||
</DataGridTextColumn>
|
||||
<DataGridTextColumn Header="Last Filled" ElementStyle="{StaticResource RightAlignedColumn}"
|
||||
c:DataGridEx.ColumnName="last_price"
|
||||
c:DataGridFilterProperties.Filter="{Binding DataContext.last_price_filter, Source={uc:DirectReference _grid}}"
|
||||
c:DataGridFilterProperties.FilterActive="{Binding DataContext.last_price_filter.active, Source={uc:DirectReference _grid}}"
|
||||
c:DataGridFilterProperties.FilterOpen="{Binding DataContext.last_price_filter.open, Source={uc:DirectReference _grid}}"
|
||||
c:DataGridFilterProperties.FilterTemplate="{StaticResource DoubleMinMaxTemplate}">
|
||||
<DataGridTextColumn.Binding>
|
||||
<MultiBinding Converter="{StaticResource TickDisplayConverter}">
|
||||
<Binding Path="last_price"/>
|
||||
<Binding Path="security.ContractSpec"/>
|
||||
</MultiBinding>
|
||||
</DataGridTextColumn.Binding>
|
||||
</DataGridTextColumn>
|
||||
<DataGridTextColumn Header="Status" Binding="{Binding status}"
|
||||
c:DataGridEx.ColumnName="status"
|
||||
c:DataGridFilterProperties.Filter="{Binding DataContext.status_filter, Source={uc:DirectReference _grid}}"
|
||||
c:DataGridFilterProperties.FilterActive="{Binding DataContext.status_filter.active, Source={uc:DirectReference _grid}}"
|
||||
c:DataGridFilterProperties.FilterOpen="{Binding DataContext.status_filter.open, Source={uc:DirectReference _grid}}"
|
||||
c:DataGridFilterProperties.FilterTemplate="{StaticResource EnumValuesTemplate}"
|
||||
/>
|
||||
<DataGridTextColumn Header="Last Message" Binding="{Binding last_msg}"
|
||||
c:DataGridEx.ColumnName="last_msg"
|
||||
c:DataGridFilterProperties.Filter="{Binding DataContext.exchange_message_filter, Source={uc:DirectReference _grid}}"
|
||||
c:DataGridFilterProperties.FilterActive="{Binding DataContext.exchange_message_filter.active, Source={uc:DirectReference _grid}}"
|
||||
c:DataGridFilterProperties.FilterOpen="{Binding DataContext.exchange_message_filter.open, Source={uc:DirectReference _grid}}"
|
||||
c:DataGridFilterProperties.FilterTemplate="{StaticResource RegexTemplate}">
|
||||
<DataGridTextColumn.CellStyle>
|
||||
<Style TargetType="DataGridCell" BasedOn="{StaticResource {x:Type DataGridCell}}">
|
||||
<Setter Property="ToolTip" Value="{Binding last_msg}" />
|
||||
</Style>
|
||||
</DataGridTextColumn.CellStyle>
|
||||
</DataGridTextColumn>
|
||||
<DataGridTextColumn Header="Security Key" Binding="{Binding security_key}"
|
||||
c:DataGridEx.ColumnName="security_key"
|
||||
c:DataGridFilterProperties.Filter="{Binding DataContext.security_key_filter, Source={uc:DirectReference _grid}}"
|
||||
c:DataGridFilterProperties.FilterActive="{Binding DataContext.security_key_filter.active, Source={uc:DirectReference _grid}}"
|
||||
c:DataGridFilterProperties.FilterOpen="{Binding DataContext.security_key_filter.open, Source={uc:DirectReference _grid}}"
|
||||
c:DataGridFilterProperties.FilterTemplate="{StaticResource RegexTemplate}"
|
||||
/>
|
||||
<DataGridTextColumn Header="Order Key" Width="*" Binding="{Binding order_key}"
|
||||
c:DataGridEx.ColumnName="order_key"
|
||||
c:DataGridFilterProperties.Filter="{Binding DataContext.order_key_filter, Source={uc:DirectReference _grid}}"
|
||||
c:DataGridFilterProperties.FilterActive="{Binding DataContext.order_key_filter.active, Source={uc:DirectReference _grid}}"
|
||||
c:DataGridFilterProperties.FilterOpen="{Binding DataContext.order_key_filter.open, Source={uc:DirectReference _grid}}"
|
||||
c:DataGridFilterProperties.FilterTemplate="{StaticResource RegexTemplate}"
|
||||
/>
|
||||
</DataGrid.Columns>
|
||||
|
||||
</c:DataGridEx>-->
|
||||
|
||||
<c:DIYGrid
|
||||
x:Name="_grid"
|
||||
Grid.Row="2"
|
||||
Columns="{Binding columns}"
|
||||
CurrentRow="{Binding current_row, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
|
||||
Rows="{Binding visible_orders}"
|
||||
RowHeight="{x:Static sp:OrderBookView.RowHeight}"
|
||||
Sorting="data_grid_sorting"
|
||||
>
|
||||
<c:DIYGrid.Resources>
|
||||
<ResourceDictionary>
|
||||
<SolidColorBrush x:Key="TreeViewItem.TreeArrow.Static.Checked.Fill" Color="#FF595959"/>
|
||||
<SolidColorBrush x:Key="TreeViewItem.TreeArrow.Static.Checked.Stroke" Color="#FF262626"/>
|
||||
<SolidColorBrush x:Key="TreeViewItem.TreeArrow.MouseOver.Stroke" Color="#FF27C7F7"/>
|
||||
<SolidColorBrush x:Key="TreeViewItem.TreeArrow.MouseOver.Fill" Color="#FFCCEEFB"/>
|
||||
<SolidColorBrush x:Key="TreeViewItem.TreeArrow.MouseOver.Checked.Stroke" Color="#FF1CC4F7"/>
|
||||
<SolidColorBrush x:Key="TreeViewItem.TreeArrow.MouseOver.Checked.Fill" Color="#FF82DFFB"/>
|
||||
<PathGeometry x:Key="TreeArrow" Figures="M0,0 L0,6 L6,0 z"/>
|
||||
<SolidColorBrush x:Key="TreeViewItem.TreeArrow.Static.Fill" Color="#FFFFFFFF"/>
|
||||
<SolidColorBrush x:Key="TreeViewItem.TreeArrow.Static.Stroke" Color="#FF818181"/>
|
||||
<Style x:Key="{x:Static sp:OrderBookView.ExpandCollapseToggleStyleKey}" TargetType="{x:Type ToggleButton}">
|
||||
<Setter Property="Focusable" Value="False"/>
|
||||
<Setter Property="Width" Value="16"/>
|
||||
<Setter Property="Height" Value="16"/>
|
||||
<Setter Property="Template">
|
||||
<Setter.Value>
|
||||
<ControlTemplate TargetType="{x:Type ToggleButton}">
|
||||
<Border Background="Transparent" Height="16" Padding="0,5,5,5" Width="16">
|
||||
<Path x:Name="ExpandPath" Data="{StaticResource TreeArrow}" Fill="{StaticResource TreeViewItem.TreeArrow.Static.Fill}" Stroke="{StaticResource TreeViewItem.TreeArrow.Static.Stroke}">
|
||||
<Path.RenderTransform>
|
||||
<RotateTransform Angle="135" CenterY="3" CenterX="3"/>
|
||||
</Path.RenderTransform>
|
||||
</Path>
|
||||
</Border>
|
||||
<ControlTemplate.Triggers>
|
||||
<Trigger Property="IsChecked" Value="True">
|
||||
<Setter Property="RenderTransform" TargetName="ExpandPath">
|
||||
<Setter.Value>
|
||||
<RotateTransform Angle="180" CenterY="3" CenterX="3"/>
|
||||
</Setter.Value>
|
||||
</Setter>
|
||||
<Setter Property="Fill" TargetName="ExpandPath" Value="{StaticResource TreeViewItem.TreeArrow.Static.Checked.Fill}"/>
|
||||
<Setter Property="Stroke" TargetName="ExpandPath" Value="{StaticResource TreeViewItem.TreeArrow.Static.Checked.Stroke}"/>
|
||||
</Trigger>
|
||||
<Trigger Property="IsMouseOver" Value="True">
|
||||
<Setter Property="Stroke" TargetName="ExpandPath" Value="{StaticResource TreeViewItem.TreeArrow.MouseOver.Stroke}"/>
|
||||
<Setter Property="Fill" TargetName="ExpandPath" Value="{StaticResource TreeViewItem.TreeArrow.MouseOver.Fill}"/>
|
||||
</Trigger>
|
||||
<MultiTrigger>
|
||||
<MultiTrigger.Conditions>
|
||||
<Condition Property="IsMouseOver" Value="True"/>
|
||||
<Condition Property="IsChecked" Value="True"/>
|
||||
</MultiTrigger.Conditions>
|
||||
<Setter Property="Stroke" TargetName="ExpandPath" Value="{StaticResource TreeViewItem.TreeArrow.MouseOver.Checked.Stroke}"/>
|
||||
<Setter Property="Fill" TargetName="ExpandPath" Value="{StaticResource TreeViewItem.TreeArrow.MouseOver.Checked.Fill}"/>
|
||||
</MultiTrigger>
|
||||
</ControlTemplate.Triggers>
|
||||
</ControlTemplate>
|
||||
</Setter.Value>
|
||||
</Setter>
|
||||
</Style>
|
||||
<Style TargetType="{x:Type ToolTip}">
|
||||
<Style.Triggers>
|
||||
<Trigger Property="Content" Value="{x:Static sys:String.Empty}">
|
||||
<Setter Property="Visibility" Value="Collapsed" />
|
||||
</Trigger>
|
||||
<Trigger Property="Content" Value="{x:Null}">
|
||||
<Setter Property="Visibility" Value="Collapsed" />
|
||||
</Trigger>
|
||||
</Style.Triggers>
|
||||
</Style>
|
||||
</ResourceDictionary>
|
||||
</c:DIYGrid.Resources>
|
||||
<c:DIYGrid.InputBindings>
|
||||
<MouseBinding MouseAction="LeftDoubleClick" Command="{x:Static c:DIYGrid.DefaultRowCommand}"/>
|
||||
<KeyBinding Key="Return" Command="{x:Static c:DIYGrid.DefaultRowCommand}"/>
|
||||
</c:DIYGrid.InputBindings>
|
||||
<c:DIYGrid.CommandBindings>
|
||||
<CommandBinding
|
||||
</c:DIYGrid.Resources>
|
||||
<c:DIYGrid.InputBindings>
|
||||
<MouseBinding MouseAction="LeftDoubleClick" Command="{x:Static c:DIYGrid.DefaultRowCommand}"/>
|
||||
<KeyBinding Key="Return" Command="{x:Static c:DIYGrid.DefaultRowCommand}"/>
|
||||
</c:DIYGrid.InputBindings>
|
||||
<c:DIYGrid.CommandBindings>
|
||||
<CommandBinding
|
||||
Command="{x:Static c:DIYGrid.DefaultRowCommand}"
|
||||
Executed="{x:Static c:DIYGrid.DefaultRowActionExecuted}"
|
||||
CanExecute="{x:Static c:DIYGrid.DefaultRowActionCanExecute}"
|
||||
/>
|
||||
</c:DIYGrid.CommandBindings>
|
||||
<c:DIYGrid.RowStyle>
|
||||
<Style TargetType="ListBoxItem" BasedOn="{StaticResource DIYGridRowStyle}">
|
||||
<Style.Triggers>
|
||||
<MultiDataTrigger>
|
||||
<MultiDataTrigger.Conditions>
|
||||
<Condition Binding="{Binding IsSelected, RelativeSource={RelativeSource Self}}" Value="False"/>
|
||||
<Condition Binding="{Binding status}" Value="{uc:Enum Working, Type=order:OrderStatus}"/>
|
||||
</MultiDataTrigger.Conditions>
|
||||
<MultiDataTrigger.Setters>
|
||||
<Setter Property="Background" Value="{x:Static Member=working_order_background_brush, MemberType={uc:Nested uc:BTSColors.Orders}}"/>
|
||||
</MultiDataTrigger.Setters>
|
||||
</MultiDataTrigger>
|
||||
</Style.Triggers>
|
||||
</Style>
|
||||
</c:DIYGrid.RowStyle>
|
||||
<c:DIYGrid.RowTemplate>
|
||||
<DataTemplate>
|
||||
<sp:OrderBookRowView/>
|
||||
</DataTemplate>
|
||||
</c:DIYGrid.RowTemplate>
|
||||
</c:DIYGrid>
|
||||
</Grid>
|
||||
</c:DIYGrid.CommandBindings>
|
||||
</c:DIYGrid>
|
||||
</AdornerDecorator>
|
||||
</UserControl>
|
||||
@@ -1,34 +1,61 @@
|
||||
using bts.api.orderman;
|
||||
using bts.shell;
|
||||
using mbts.ui;
|
||||
using mbts.ui.controls;
|
||||
using System.ComponentModel;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Windows.Controls;
|
||||
|
||||
namespace mbts.spark {
|
||||
interface IOrderBookVM : IDisposable {
|
||||
event EventHandler<OrderRecordImmutable?> selected_order_changed;
|
||||
event EventHandler order_received;
|
||||
//
|
||||
OrderRecordImmutable? current_order { get; }
|
||||
IEnumerable<uint> active_buy_order_keys { get; }
|
||||
IEnumerable<uint> active_sell_order_keys { get; }
|
||||
IEnumerable<uint> active_order_keys { get; }
|
||||
IEnumerable<OrderRecordImmutable> selected_orders { get; }
|
||||
//
|
||||
void on_sorting(DIYGridSortingEventArgs e);
|
||||
List<SortingCriteria> get_sorting_states();
|
||||
OrderBookWindowFactory.FilterStates get_filter_states();
|
||||
}
|
||||
|
||||
internal partial class OrderBookView : UserControl {
|
||||
public enum Columns { Security, Time, LastChange, User, Account, Exchange, TimeInForce, OrderType, SubType, Side, Size, Price, Filled, AvgFilled, LastPrice, Status, LastMessage, SecurityKey, OrderKey }
|
||||
public enum Columns { Security, Time, LastChange, User, Account, Exchange, TimeInForce, OrderType, SubType, Side, Size, Price, Filled, AvgFilled, LastPrice, Status, LastMessage, SecurityKey, OrderKey, ParentKey }
|
||||
public const string
|
||||
SubTypeFilterTemplateKey = "SubTypeFilterTemplate",
|
||||
ExpandCollapseToggleStyleKey = "ExpandCollapseToggleStyle";
|
||||
ExpandCollapseToggleStyleKey = "ExpandCollapseToggleStyle",
|
||||
RowStyleKey = "RowStyle",
|
||||
RowTemplateKey = "RowTemplateKey",
|
||||
CVRowStyleKey = "CVRowStyle",
|
||||
CVRowTemplateKey = "CVRowTemplateKey";
|
||||
|
||||
public static string ActiveRowStyleKey =>
|
||||
OrderBookWindow.cv_orderbook_enabled ? CVRowStyleKey : RowStyleKey;
|
||||
|
||||
public static string ActiveRowTemplateKey =>
|
||||
OrderBookWindow.cv_orderbook_enabled ? CVRowTemplateKey : RowTemplateKey;
|
||||
|
||||
public const double RowHeight = 17;
|
||||
OrderBookVM _vm;
|
||||
//DataGridEx _grid;
|
||||
public OrderBookView(OrderBookVM vm) {
|
||||
IOrderBookVM _vm;
|
||||
|
||||
public OrderBookView(IOrderBookVM vm) {
|
||||
InitializeComponent();
|
||||
_vm = vm;
|
||||
DataContext = vm;
|
||||
}
|
||||
|
||||
public void data_grid_sorting(object sender, DIYGridSortingEventArgs args) {
|
||||
args.Column.SortDirection = args.Column.SortDirection == ListSortDirection.Ascending ? ListSortDirection.Descending : ListSortDirection.Ascending;
|
||||
_vm.sort_grid(args.Column);
|
||||
|
||||
// We ALWAYS want to set this here, else the grid tries to be helpful
|
||||
// and sort with its own CollectionView, which destroys the SortedTree
|
||||
// sort order; we want it to never use the CollectionView
|
||||
args.Handled = true;
|
||||
}
|
||||
void data_grid_sorting(object sender, DIYGridSortingEventArgs args) =>
|
||||
_vm.on_sorting(args);
|
||||
}
|
||||
|
||||
class OrderBookRowView : DIYGrid.HighPerformanceRow<OrderBookRowVM, OrderBookView.Columns> {
|
||||
public OrderBookRowView() : base(OrderBookView.RowHeight) { }
|
||||
}
|
||||
|
||||
class CVOrderBookRowView : DIYGrid.HighPerformanceRow<CVOrderBookRowVM, OrderBookView.Columns> {
|
||||
public CVOrderBookRowView() : base(OrderBookView.RowHeight) { }
|
||||
}
|
||||
}
|
||||
@@ -11,11 +11,12 @@ using System.Windows.Media;
|
||||
|
||||
namespace mbts.spark {
|
||||
internal class OrderBookWindow : SparkWindow<OrderBookWindowFactory.OrderBookStartupData>, ISharedWindow {
|
||||
public static readonly bool cv_orderbook_enabled = config.Configuration.get<bts.config.toggles.CVOrderBook>().enabled;
|
||||
|
||||
const string _window_type = "OrderBook", ORDERBOOK_THREAD_ID = "OrderBook";
|
||||
OrderBookWindowFactory.OrderBookStartupData _state;
|
||||
OrdersModel _model;
|
||||
OrderBookVM _vm;
|
||||
IOrderBookVM _vm;
|
||||
OrderBookView _view;
|
||||
OrderBookWindowFactory _factory;
|
||||
SparkCommand[] _commands;
|
||||
@@ -52,11 +53,13 @@ namespace mbts.spark {
|
||||
}
|
||||
|
||||
public void thread_init() {
|
||||
_vm = new OrderBookVM(_factory, base._bus, _model, _state);
|
||||
_vm = cv_orderbook_enabled ?
|
||||
(IOrderBookVM)new CVOrderBookVM(_bus, _model, _state) :
|
||||
(IOrderBookVM)new OrderBookVM(_factory, base._bus, _model, _state);
|
||||
_view = new OrderBookView(_vm);
|
||||
_view.DataContext = _vm;
|
||||
|
||||
_show_fills_command = SparkCommands.ShowFills.complete(() => new FillsWindowFactory.StartupData(_vm.selected_rows.Select(r => r.order.Key).ToLArray()));
|
||||
_show_fills_command = SparkCommands.ShowFills.complete(() => new FillsWindowFactory.StartupData(_vm.selected_orders.Select(r => r.Key).ToArray()));
|
||||
|
||||
_modify_order_command = SparkCommands.ShowModifyOrder.complete(
|
||||
() => _vm.current_order.Value.Key,
|
||||
@@ -90,13 +93,8 @@ namespace mbts.spark {
|
||||
);
|
||||
|
||||
_cancel_selected_orders_command = SparkCommands.CancelSelectedOrders.complete(
|
||||
() => _vm.selected_rows.Where(x => x.status == bts.api.orderman.OrderStatus.Working).Select(x => x.order.Key),
|
||||
can_execute: () => {
|
||||
if (_vm.selected_rows.Count > 0) {
|
||||
return _vm.selected_rows.Any(x => x.status == bts.api.orderman.OrderStatus.Working);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
() => _vm.selected_orders.Where(x => x.status() == OrderStatus.Working).Select(x => x.Key),
|
||||
can_execute: () => _vm.selected_orders.Any(x => x.status() == OrderStatus.Working)
|
||||
);
|
||||
|
||||
_commands = new SparkCommand[] {
|
||||
|
||||
@@ -10,4 +10,8 @@ package bts.config.toggles;
|
||||
|
||||
message LaddersOnThreads {
|
||||
optional bool enabled = 1 [default = true];
|
||||
}
|
||||
|
||||
message CVOrderBook {
|
||||
optional bool enabled = 1 [default = true];
|
||||
}
|
||||
Reference in new issue
Block a user