diff --git a/src/client/spark/core/GlobalExtensions/GenericCollectionExtensions.cs b/src/client/spark/core/GlobalExtensions/GenericCollectionExtensions.cs index e670c8d73..6f8441e4e 100644 --- a/src/client/spark/core/GlobalExtensions/GenericCollectionExtensions.cs +++ b/src/client/spark/core/GlobalExtensions/GenericCollectionExtensions.cs @@ -14,7 +14,11 @@ namespace System.Collections.Generic } public static IEnumerable Ordered(this IEnumerable enumerable, Func compare) { - return enumerable.OrderBy(t => t, Comparer.Create(new Comparison(compare))); + return enumerable.Ordered(Comparer.Create(new Comparison(compare))); + } + + public static IEnumerable Ordered(this IEnumerable enumerable, IComparer comparer) { + return enumerable.OrderBy(t => t, comparer); } #endregion diff --git a/src/client/spark/mspark/MarketView/MarketViewRow.cs b/src/client/spark/mspark/MarketView/MarketViewRow.cs index 8deb07d3b..169e3e785 100644 --- a/src/client/spark/mspark/MarketView/MarketViewRow.cs +++ b/src/client/spark/mspark/MarketView/MarketViewRow.cs @@ -26,7 +26,7 @@ namespace mbts.spark public Row(MarketViewVM owner, MarketViewModel model, SecurityMD smd, IObservable position) { _owner = owner; _model = model; - security = AddCell(Columns.Security, smd.security); + security = AddCell(Columns.Security, smd.security, SecurityDataHelper.Comparer.Instance); this.position = AddCell(Columns.Position, position, 0); var md_pump = new SecurityMDPump(smd.md); bid_price = AddCell(Columns.Bid, md_pump.bid_price, double.NaN); diff --git a/src/client/spark/mspark/MarketView/MarketViewView.xaml b/src/client/spark/mspark/MarketView/MarketViewView.xaml index f76bf5616..d07ae060b 100644 --- a/src/client/spark/mspark/MarketView/MarketViewView.xaml +++ b/src/client/spark/mspark/MarketView/MarketViewView.xaml @@ -105,7 +105,7 @@ - + diff --git a/src/client/spark/mspark/SecurityBrowser/SecuritySource.cs b/src/client/spark/mspark/SecurityBrowser/SecuritySource.cs index 7b42148df..e065364ca 100644 --- a/src/client/spark/mspark/SecurityBrowser/SecuritySource.cs +++ b/src/client/spark/mspark/SecurityBrowser/SecuritySource.cs @@ -23,7 +23,7 @@ namespace mbts.spark [ImportingConstructor] public InternalSource(IBus bus) { publisher = new StatePublisher(bus.subscribe>(SparkTags.SecuritiesTag) - .Select(m => m.Values.Select(s => s.security).Ordered((s1,s2)=>s1.Id.CompareTo(s2.Id)).ToArray())); + .Select(m => m.Values.Select(s => s.security).Ordered(SecurityDataHelper.Comparer.Instance).ToArray())); _instance = this; } diff --git a/src/client/spark/mspark/Shell/Fills/FillsVM.cs b/src/client/spark/mspark/Shell/Fills/FillsVM.cs index 6e79cc482..4b5eaefdd 100644 --- a/src/client/spark/mspark/Shell/Fills/FillsVM.cs +++ b/src/client/spark/mspark/Shell/Fills/FillsVM.cs @@ -64,7 +64,7 @@ namespace mbts.spark } private IEnumerable _context_menu_item_factory() { var selected_rows = _rows.Where(r => r.is_selected); - var securities = new HashSet(selected_rows.Select(r => r.security)); + var securities = new HashSet(selected_rows.Select(r => r.security.value)); yield return MenuItemVM.Create(new LadderMenuItem(_bus), securities); yield return MenuItemVM.Create(new OrderTicketMenuItem(_model), securities); yield return MenuItemVM.Create(new MarketViewMenuItem(_bus), securities); @@ -92,6 +92,7 @@ namespace mbts.spark _fill = fill.Fill; _entry = fill.OrderBookEntry; _model = model; + security = new SelectableViewModelBase(_fill.Order.Security, SecurityDataHelper.Comparer.Instance); uint aggr_fills = 0; foreach (var f in _entry.Fills) if (f.Timestamp <= _fill.Timestamp) aggr_fills += f.Size; order_percentage = (double)aggr_fills / (double)_fill.Order.Size; @@ -108,7 +109,7 @@ namespace mbts.spark public string account => _fill.Order.Account; public string user => _fill.Order.User; public Venue exchange => _fill.Order.Security.Exchange; - public Security security => _fill.Order.Security; + public SelectableViewModelBase security { get; } public BuySell side => _fill.Order.Side; public double price => _fill.Price; public uint size => _fill.Size; diff --git a/src/client/spark/mspark/Shell/MarketModule.cs b/src/client/spark/mspark/Shell/MarketModule.cs index 88ded8c09..533512843 100644 --- a/src/client/spark/mspark/Shell/MarketModule.cs +++ b/src/client/spark/mspark/Shell/MarketModule.cs @@ -159,7 +159,7 @@ namespace mbts.spark synthetic = SyntheticDataHelper.Defaults.Security(find_unique_synthetic_name(), s); strategy = _synthetic_strategy_map.TryGetValue(s.Id, out SyntheticExecutionStrategy ses) ? ses.ExecutionStrategy.Clone() : SyntheticDataHelper.Defaults.ExecutionStrategy(s); } else { - synthetic = SyntheticDataHelper.Defaults.Security(find_unique_synthetic_name(), securities.Ordered()); + synthetic = SyntheticDataHelper.Defaults.Security(find_unique_synthetic_name(), securities.Ordered(SecurityDataHelper.Comparer.Instance)); strategy = SyntheticDataHelper.Defaults.ExecutionStrategy(securities); } show_synthetic_editor(synthetic, strategy); diff --git a/src/client/spark/mspark/Shell/OrderBook/OrderBookRowVM.cs b/src/client/spark/mspark/Shell/OrderBook/OrderBookRowVM.cs index 6fdb85d54..e1d63a195 100644 --- a/src/client/spark/mspark/Shell/OrderBook/OrderBookRowVM.cs +++ b/src/client/spark/mspark/Shell/OrderBook/OrderBookRowVM.cs @@ -17,6 +17,7 @@ namespace mbts.spark _parent = parent; _model = model; _order = entry; + security = new SelectableViewModelBase(_order.order().Security, SecurityDataHelper.Comparer.Instance); _visible = backing(nameof(is_visible), !is_child); _expanded = backing(nameof(is_expanded), false); details_command = new DelegateCommand(() => _model.show_order_details(order_id)); @@ -34,8 +35,9 @@ namespace mbts.spark void ignore() {/*These setters are just for WPF databinding :(*/ throw new NotImplementedException(); } - // need actual security prop for sorting - public Security security => _order.order().Security; + // the grid view uses this for sorting, it was just a bts.api.Security once upon a time, but that class no longer implements IComparable + // this SelectableViewModeBase is essentially a wrapper class, which implements ISelecatble and IComparable + public SelectableViewModelBase security { get; } public OrderBookEntry order => _order; public string security_text { // indent child 3 spaces diff --git a/src/client/spark/mspark/Shell/OrderBook/OrderBookVM.cs b/src/client/spark/mspark/Shell/OrderBook/OrderBookVM.cs index c4e902150..ccaf82668 100644 --- a/src/client/spark/mspark/Shell/OrderBook/OrderBookVM.cs +++ b/src/client/spark/mspark/Shell/OrderBook/OrderBookVM.cs @@ -80,7 +80,7 @@ namespace mbts.spark { private IEnumerable _context_menu_item_factory() { var selected_rows = this.orders.Where(vm => vm.is_selected && vm.is_visible).ToList(); var orders = selected_rows.Select(vm => vm.order).ToList(); - var securities = new HashSet(selected_rows.Select(vm => vm.security)); + var securities = new HashSet(selected_rows.Select(vm => vm.security.value)); yield return MenuItemVM.Create(new LadderMenuItem(_bus), securities); yield return MenuItemVM.Create(new OrderTicketMenuItem(_model), securities); yield return MenuItemVM.Create(new MarketViewMenuItem(_bus), securities); diff --git a/src/client/spark/mspark/Shell/TimeAndSales/TimeAndSalesVM.cs b/src/client/spark/mspark/Shell/TimeAndSales/TimeAndSalesVM.cs index 2c89cac96..caeba7e31 100644 --- a/src/client/spark/mspark/Shell/TimeAndSales/TimeAndSalesVM.cs +++ b/src/client/spark/mspark/Shell/TimeAndSales/TimeAndSalesVM.cs @@ -92,7 +92,7 @@ namespace mbts.spark private class Row : GridRowViewModelBase { public Row(Security security, TradeMessage trade) { - this.security = AddCell(Columns.Security, security); + this.security = AddCell(Columns.Security, security, SecurityDataHelper.Comparer.Instance); quantity = AddCell(Columns.Size, trade.size); price = AddCell(Columns.Price, trade.mantissa * Math.Pow(10, trade.exponent)); trade_time = AddCell(Columns.TradeTime, trade.time.lldatetime()); diff --git a/src/client/spark/mspark/Shell/TimeAndSales/TimeAndSalesView.xaml b/src/client/spark/mspark/Shell/TimeAndSales/TimeAndSalesView.xaml index a385a0eb3..dde08709b 100644 --- a/src/client/spark/mspark/Shell/TimeAndSales/TimeAndSalesView.xaml +++ b/src/client/spark/mspark/Shell/TimeAndSales/TimeAndSalesView.xaml @@ -40,7 +40,7 @@ - + diff --git a/src/client/spark/mspark/Utilities/Grid/GridRowViewModelBase.cs b/src/client/spark/mspark/Utilities/Grid/GridRowViewModelBase.cs index 641e9adb5..f36f012dd 100644 --- a/src/client/spark/mspark/Utilities/Grid/GridRowViewModelBase.cs +++ b/src/client/spark/mspark/Utilities/Grid/GridRowViewModelBase.cs @@ -16,14 +16,17 @@ namespace mbts.spark.utilities private void _check_column_present(TColumn column) { if (_column_containers.Any(c => c.column.Equals(column))) throw new InvalidOperationException("Column '{0}' already added.".fmt(column)); } - public SelectableViewModelBase AddCell(TColumn column, T initial_value = default(T)) { + public SelectableViewModelBase AddCell(TColumn column) { return AddCell(column, default(T)); } + public SelectableViewModelBase AddCell(TColumn column, T initial_value) { return AddCell(column, initial_value, Comparer.Default); } + public SelectableViewModelBase AddCell(TColumn column, T initial_value, IComparer comparer) { _check_column_present(column); - var cell = new SelectableViewModelBase(initial_value); + var cell = new SelectableViewModelBase(initial_value, comparer); _column_containers.Add(new ColumnContainer(column, cell.observe_is_selected(), this)); return cell; } - public RXReadOnlyDataGridCell AddCell(TColumn column, IObservable data, T initial_value = default(T)) { + public RXReadOnlyDataGridCell AddCell(TColumn column, IObservable data) { return AddCell(column, data, default(T)); } + public RXReadOnlyDataGridCell AddCell(TColumn column, IObservable data, T initial_value) { _check_column_present(column); var cell = new RXReadOnlyDataGridCell(data, initial_value); _column_containers.Add(new ColumnContainer(column, cell.observe_is_selected(), this)); diff --git a/src/client/spark/mspark/Utilities/SecurityDataHelper.cs b/src/client/spark/mspark/Utilities/SecurityDataHelper.cs new file mode 100644 index 000000000..7b351eba8 --- /dev/null +++ b/src/client/spark/mspark/Utilities/SecurityDataHelper.cs @@ -0,0 +1,44 @@ +using bts.api; +using System; +using System.Collections.Generic; +using System.Linq; +using static bts.api.Security; + +namespace mbts.spark +{ + public static class SecurityDataHelper + { + public static class Comparer + { + public static readonly IComparer Instance = Comparer.Create(Compare); + + public static int Compare(Security x, Security y) { + if (x == null) return y == null ? 0 : -1; + if (y == null) return 1; + if (x.ContractCase == ContractOneofCase.Future) return y.ContractCase == ContractOneofCase.Future ? Compare(x.Symbol, x.Future, y.Symbol, y.Future) : -1; + if (y.ContractCase == ContractOneofCase.Future) return 1; + var xlegs = SecurityHelper.get_flat_future_legs(x).ToArray(); + var ylegs = SecurityHelper.get_flat_future_legs(y).ToArray(); + int max = Math.Min(xlegs.Length, ylegs.Length); + for (int i = 0; i < max; i++) { + int rv = Compare(xlegs[i].Item2, xlegs[i].Item3, ylegs[i].Item2, ylegs[i].Item3); + if (rv != 0) return rv; + } + for (int i = 0; i < max; i++) { + int rv = xlegs[i].Item4.CompareTo(ylegs[i].Item4); + if (rv != 0) return rv; + } + for (int i = 0; i < max; i++) { + int rv = xlegs[i].Item1.CompareTo(ylegs[i].Item1); + if (rv != 0) return rv; + } + return xlegs.Length.CompareTo(ylegs.Length); + } + + public static int Compare(string x_sym, FutureContract x, string y_sym, FutureContract y) { + int sym_comp = x_sym.CompareTo(y_sym); + return sym_comp == 0 ? x.RawExpDate().CompareTo(y.RawExpDate()) : sym_comp; + } + } + } +} \ No newline at end of file diff --git a/src/client/spark/view/Utility/SelectableViewModelBase.cs b/src/client/spark/view/Utility/SelectableViewModelBase.cs index 68ff68575..69e19c683 100644 --- a/src/client/spark/view/Utility/SelectableViewModelBase.cs +++ b/src/client/spark/view/Utility/SelectableViewModelBase.cs @@ -2,6 +2,7 @@ using System; using System.Collections; using System.Collections.Generic; +using System.Globalization; using System.Reactive.Linq; namespace mbts.ui.shell @@ -16,21 +17,23 @@ namespace mbts.ui.shell public IObservable observe_is_selected() { return _is_selected.Select(vals => vals.newval); } } - public class SelectableViewModelBase : SelectableViewModelBase, IComparable>, IComparable + public class SelectableViewModelBase : SelectableViewModelBase, IComparable>, IComparable, IFormattable { - public SelectableViewModelBase(T initial_value = default(T)) { + public SelectableViewModelBase() : this(default(T)) { } + public SelectableViewModelBase(T initial_value) : this(initial_value, Comparer.Default) { } + public SelectableViewModelBase(T initial_value, IComparer comparer) { _value = backing(nameof(value), initial_value); + _comparer = comparer ?? Comparer.Default; } + private readonly Property _value; + private readonly IComparer _comparer; + public T value { get => _value.Value; set => _value.set(value); } - public int CompareTo(SelectableViewModelBase other) { - if (other == null) return -1; - if (other.value == null) return value == null ? 0 : -1; - var comp = Comparer.Default; - return comp == null ? 0 : comp.Compare(value, other.value); - } - + public int CompareTo(SelectableViewModelBase other) { return _comparer.Compare(value, other.value); } public int CompareTo(object obj) { return CompareTo(obj as SelectableViewModelBase); } + public override string ToString() { return ToString(null, CultureInfo.CurrentCulture); } + public string ToString(string format, IFormatProvider formatProvider) { return (value as IFormattable)?.ToString(format, formatProvider); } } } \ No newline at end of file diff --git a/src/client/spark_api/security/DateFormatter.cs b/src/client/spark_api/security/DateFormatter.cs index f98c0185d..cd1f7390e 100644 --- a/src/client/spark_api/security/DateFormatter.cs +++ b/src/client/spark_api/security/DateFormatter.cs @@ -54,13 +54,7 @@ namespace bts.api #region Extensions public static string SecurityName(this Security security) { - switch (security.ContractCase) { - case Security.ContractOneofCase.Future: - case Security.ContractOneofCase.Spread: return security.Symbol; - case Security.ContractOneofCase.Synthetic: return security.Synthetic.Legs.Count > 0 ? SecurityName(security.Synthetic.Legs[0].Security) : string.Empty; - case Security.ContractOneofCase.None: return "N/A"; - default: return "Unknown"; - } + return security.ContractCase == Security.ContractOneofCase.Synthetic ? security.Synthetic.Name : security.Symbol; } #endregion } diff --git a/src/client/spark_api/security/SecurityFormatter.cs b/src/client/spark_api/security/SecurityFormatter.cs index 5305d17ca..203887bd6 100644 --- a/src/client/spark_api/security/SecurityFormatter.cs +++ b/src/client/spark_api/security/SecurityFormatter.cs @@ -2,7 +2,7 @@ using System; using System.Collections.Generic; using System.Linq; -using FutureLeg = System.Tuple; +using FutureLeg = System.Tuple; namespace bts.api { @@ -24,7 +24,7 @@ namespace bts.api if (part_formatters.Count > 0 && part_formatters[0].separator == "!") { if (part_formatters[0].add_part(s, out string result)) return result; } - return string.Join("", SecurityHelper.get_flat_future_legs(s).Select(flt => _format_future_leg(flt, s.Symbol, part_formatters))); + return string.Join("", SecurityHelper.get_flat_future_legs(s).Select(flt => _format_future_leg(flt, part_formatters))); } } @@ -52,18 +52,18 @@ namespace bts.api } } - private static string _format_future(Security s, IEnumerable part_formatters) { return _format(s, s.Symbol, part_formatters, _add_future); } - private static string _format_future_leg(FutureLeg l, string symbol, IEnumerable part_formatters) { return _format(l, symbol, part_formatters, _add_future_leg); } + private static string _format_future(Security s, IEnumerable part_formatters) { return _format(s, part_formatters, _add_future); } + private static string _format_future_leg(FutureLeg l, IEnumerable part_formatters) { return _format(l, part_formatters, _add_future_leg); } - private delegate bool AddPart(string symbol, T t, IPartFormatter part_formatter, out string result); - private static bool _add_future(string symbol, Security s, IPartFormatter part_formatter, out string result) { return part_formatter.add_part(s, out result); } - private static bool _add_future_leg(string symbol, FutureLeg l, IPartFormatter part_formatter, out string result) { return part_formatter.add_part(l, symbol, out result); } + private delegate bool AddPart(T t, IPartFormatter part_formatter, out string result); + private static bool _add_future(Security s, IPartFormatter part_formatter, out string result) { return part_formatter.add_part(s, out result); } + private static bool _add_future_leg(FutureLeg l, IPartFormatter part_formatter, out string result) { return part_formatter.add_part(l, out result); } - private static string _format(T t, string symbol, IEnumerable part_formatters, AddPart add_part) { + private static string _format(T t, IEnumerable part_formatters, AddPart add_part) { var adds = new List>(); foreach (var pf in part_formatters) { string p; - if(add_part(symbol, t, pf, out p)) adds.Add(Tuple.Create(p, pf)); + if(add_part(t, pf, out p)) adds.Add(Tuple.Create(p, pf)); } string result = string.Empty; if (adds.Count > 0) { @@ -76,7 +76,7 @@ namespace bts.api private interface IPartFormatter { string separator { get; } - bool add_part(FutureLeg fl, string symbol, out string result); + bool add_part(FutureLeg fl, out string result); bool add_part(Security s, out string result); } @@ -89,11 +89,11 @@ namespace bts.api case ContractParts.Symbol: return new SymbolFormatter(pft.Argument); case ContractParts.MappedExp: { _parse_exp_formatting_arg(pft.Argument, out DateFormat df, out string separator); - return new Formatter(s => s.Future.MappedExpDate(), fl => fl.Item2.MappedExpDate(), date => DateFormatter.Format(date, df), separator); + return new Formatter(s => s.Future.MappedExpDate(), fl => fl.Item3.MappedExpDate(), date => DateFormatter.Format(date, df), separator); } case ContractParts.RawExp: { _parse_exp_formatting_arg(pft.Argument, out DateFormat df, out string separator); - return new Formatter(s => s.Future.RawExpDate(), fl => fl.Item2.RawExpDate(), date => DateFormatter.Format(date, df), separator); + return new Formatter(s => s.Future.RawExpDate(), fl => fl.Item3.RawExpDate(), date => DateFormatter.Format(date, df), separator); } case ContractParts.Quantity: return new QuantityFormatter(pft.Argument); default: throw new NotImplementedException(); @@ -118,7 +118,7 @@ namespace bts.api this.separator = separator; } public string separator { get; } - public bool add_part(FutureLeg fl, string symbol, out string result) { + public bool add_part(FutureLeg fl, out string result) { result = null; return false; } @@ -138,15 +138,15 @@ namespace bts.api } private bool _first_leg = true; public string separator { get; } - public bool add_part(FutureLeg fl, string symbol, out string result) { + public bool add_part(FutureLeg fl, out string result) { if (_first_leg) { - if (fl.Item3 > 0) result = fl.Item3 == 1 ? string.Empty : fl.Item3.ToString(); - else result = fl.Item3 == -1 ? "-" : fl.Item3.ToString("-0"); + if (fl.Item4 > 0) result = fl.Item4 == 1 ? string.Empty : fl.Item3.ToString(); + else result = fl.Item4 == -1 ? "-" : fl.Item4.ToString("-0"); _first_leg = false; } else { - if (fl.Item3 == 1) result = "+"; - else if (fl.Item3 == -1) result = "-"; - else result = fl.Item3.ToString("+0;-0"); + if (fl.Item4 == 1) result = "+"; + else if (fl.Item4 == -1) result = "-"; + else result = fl.Item4.ToString("+0;-0"); } return true; } @@ -160,7 +160,7 @@ namespace bts.api this.separator = separator; } public string separator { get; } - public bool add_part(FutureLeg fl, string symbol, out string result) { result = symbol; return true; } + public bool add_part(FutureLeg fl, out string result) { result = fl.Item2; return true; } public bool add_part(Security s, out string result) { result = s.Symbol; return true; } } private class Formatter : IPartFormatter { @@ -188,7 +188,7 @@ namespace bts.api return false; } } - public bool add_part(FutureLeg fl, string symbol, out string result) { + public bool add_part(FutureLeg fl, out string result) { T val = _get_leg_value(fl); if (!val.Equals(_last_val)) { result = _format(_last_val = val); diff --git a/src/client/spark_api/security/SecurityHelper.cs b/src/client/spark_api/security/SecurityHelper.cs index 0b5f98395..0b1693b2f 100644 --- a/src/client/spark_api/security/SecurityHelper.cs +++ b/src/client/spark_api/security/SecurityHelper.cs @@ -6,7 +6,7 @@ using System; using System.Collections.Generic; using System.Linq; using static bts.api.Security; -using FutureLeg = System.Tuple; +using FutureLeg = System.Tuple; using bts.log; namespace bts.api @@ -27,7 +27,7 @@ namespace bts.api private static IEnumerable _get_flat_future_legs(RepeatedField legs, int qty) { foreach (var l in legs) { switch (l.Security.ContractCase) { - case ContractOneofCase.Future: yield return new FutureLeg(l.Security.Exchange, l.Security.Future, qty * l.SignedQuantity()); break; + case ContractOneofCase.Future: yield return new FutureLeg(l.Security.Exchange, l.Security.Symbol, l.Security.Future, qty * l.SignedQuantity()); break; case ContractOneofCase.Spread: foreach (var tup in _get_flat_future_legs(l.Security.Spread.Legs, qty * l.SignedQuantity())) yield return tup; break; case ContractOneofCase.Synthetic: foreach (var tup in _get_flat_future_legs(l.Security.Synthetic.Legs, qty * l.SignedQuantity())) yield return tup; break; }