From d19c6894b67815ec9ff2dcc0ec00d17f8fa03c95 Mon Sep 17 00:00:00 2001 From: John Colvin Date: Mon, 5 Mar 2018 20:44:24 -0500 Subject: [PATCH] ladder - corner cases for one-sided/empty markets --- src/client/spark/view/Spark/Core/TickUtils.cs | 11 ++- .../spark/view/Spark/Ladder/LadderGridHost.cs | 93 ++++++++++++------- .../spark/view/Spark/Ladder/LadderSettings.cs | 6 +- .../spark/view/Spark/Ladder/LadderVM.cs | 32 +++---- .../spark/view/Spark/Ladder/LadderView.xaml | 36 ++++++- .../spark/view/Spark/Ladder/LaderRender.cs | 30 +++--- 6 files changed, 129 insertions(+), 79 deletions(-) diff --git a/src/client/spark/view/Spark/Core/TickUtils.cs b/src/client/spark/view/Spark/Core/TickUtils.cs index d2a60a1f8..5d9666a15 100644 --- a/src/client/spark/view/Spark/Core/TickUtils.cs +++ b/src/client/spark/view/Spark/Core/TickUtils.cs @@ -18,7 +18,8 @@ namespace mbts.spark { new { tick_unit = 0.25, tick_factors = new TickFactors(100, 25), jump_quantities = new[] { 1, 2, 4, 8, 20, 40 } }, new { tick_unit = 0.0025, tick_factors = new TickFactors(10000, 25), jump_quantities = new[] { 1, 2, 4, 20, 40, 200 } }, new { tick_unit = 0.0050, tick_factors = new TickFactors(10000, 50), jump_quantities = new[] { 1, 2, 10, 20, 100, 500 } }, - new { tick_unit = 0.015625, tick_factors = new TickFactors(64, 1), jump_quantities = new[] { 1, 4, 8, 16, 32, 64 } } + new { tick_unit = 0.015625, tick_factors = new TickFactors(64, 1), jump_quantities = new[] { 1, 4, 8, 16, 32, 64 } }, + new { tick_unit = 0.0078125, tick_factors = new TickFactors(640, 5), jump_quantities = new[] { 1, 5, 10, 20, 100, 320 } } }.ToDictionary(obj => obj.tick_unit, obj => (obj.tick_factors, obj.jump_quantities))); #endregion @@ -63,11 +64,11 @@ namespace mbts.spark { return mid; } - public static double atm_tick(ContractSpecificationImmutable spec, double bid, double ask, ATMLine atm_line) { + public static double atm_tick(ContractSpecificationImmutable spec, double bid, double ask, MarketBreakType atm_line) { switch (atm_line) { - case ATMLine.Bid: return double.IsNaN(bid) ? ask : bid; - case ATMLine.Ask: return double.IsNaN(ask) ? bid : ask; - case ATMLine.Mid: + case MarketBreakType.Bid: return double.IsNaN(bid) ? ask : bid; + case MarketBreakType.Ask: return double.IsNaN(ask) ? bid : ask; + case MarketBreakType.Mid: default: return mid_tick(spec, bid, ask); } } diff --git a/src/client/spark/view/Spark/Ladder/LadderGridHost.cs b/src/client/spark/view/Spark/Ladder/LadderGridHost.cs index 07a735d48..5d3b8a13a 100644 --- a/src/client/spark/view/Spark/Ladder/LadderGridHost.cs +++ b/src/client/spark/view/Spark/Ladder/LadderGridHost.cs @@ -39,7 +39,7 @@ namespace mbts.spark { bool _price_dirty = false; double[] _price_list = null; IComparer _price_comparer; - double _last_mid; + double _last_market_break; NativePriceSize _last_trade; public LadderGridHost() { @@ -163,8 +163,10 @@ namespace mbts.spark { else return row <= price_list.Length ? _size_from_price(price_list[row], depth, spec) : null; } - static ushort? _size_from_price(double price, NativePriceSize[] depth, ContractSpecificationImmutable spec) => - depth.FirstOrDefault(nps => spec.tick_equals(price, nps.price), nps => new ushort?(nps.size)); + static ushort? _size_from_price(double price, NativePriceSize[] depth, ContractSpecificationImmutable spec) { + if (double.IsNaN(price)) return null; + else return depth.FirstOrDefault(nps => spec.tick_equals(price, nps.price), nps => nps.size); + } int _row_for_point(Point point) { var row_offset = (_scrollViewer.ContentVerticalOffset / _settings.row_height); @@ -216,7 +218,7 @@ namespace mbts.spark { _render_market_col(column, _bids); break; case ColumnType.Price: - LadderRender.render_prices(column.visual, _last_mid, _row_count, _settings.row_height, _price_list, column.settings, security.ContractSpec); + LadderRender.render_prices(column.visual, _row_count, _settings.row_height, _price_list, column.settings, security.ContractSpec); break; case ColumnType.Ask: _render_market_col(column, _asks); @@ -260,7 +262,7 @@ namespace mbts.spark { if (_scrollViewer != null && _settings != null) { double target_position = _row_count * _settings.row_height / 2 - (ActualHeight / 2); if (_price_list != null) { - var mid = TickUtils.atm_tick(security.ContractSpec, _bids[0].price, _asks[0].price, _settings.atm_line); + var mid = TickUtils.atm_tick(security.ContractSpec, _bids[0].price, _asks[0].price, _settings.market_break_type); var orig_mid = _price_list[_price_list.Length / 2]; var drift = _settings.row_height * TickUtils.ticks_between(mid, orig_mid, _vm.security.ContractSpec); target_position += drift; @@ -283,34 +285,59 @@ namespace mbts.spark { void _tick() { if (_settings != null) { - if (_vm.md.get_depth(ref _prices_updated, ref _bids, ref _asks)) { - var mid = TickUtils.atm_tick(security.ContractSpec, _bids[0].price, _asks[0].price, _settings.atm_line); - if (_price_dirty || _prices_out_of_range()) { - _price_list = LadderRender.render_prices(_pricevis, mid, _row_count, _settings.row_height, _settings.price_column, security.ContractSpec); - _ordershost.update_settings(_price_list, _price_comparer, _settings.row_height, _settings.orders_column); - LadderRender.render_grid(_gridvis, _price_list, _row_count, _settings.row_height, _settings.grid_line_color); - } - Logger.assert_d(_price_list != null); - var midIndex = LadderRender.index_of(_price_list, mid, _price_comparer); - LadderRender.render_sizes(_bidvis, midIndex, _price_list, _bids, _settings.row_height, security.ContractSpec, _settings.bid_column); - LadderRender.render_sizes(_askvis, midIndex, _price_list, _asks, _settings.row_height, security.ContractSpec, _settings.ask_column); - if (mid != _last_mid) { - _last_mid = mid; - LadderRender.render_mid(_midvis, midIndex, _settings.row_height); - } - ulong unused = 0; - _vm.md.get_last_trade(ref unused, out NativePriceSize last, out DateTime _, out ulong __); - if (_price_dirty || _last_trade != last) { - _last_trade = last; - var last_index = LadderRender.index_of(_price_list, last.price, _price_comparer); - if (last_index > 0) { - LadderRender.render_last(_lastvis, last, last_index, _settings.row_height, _settings.last_column); - } - } - _price_dirty = false; + ulong unused = 0; + _vm.md.get_last_trade(ref unused, out NativePriceSize last, out DateTime _, out ulong __); + double market_break = _get_market_break(); + bool depth_changed = _vm.md.get_depth(ref _prices_updated, ref _bids, ref _asks); + bool update_prices = _price_dirty || _prices_out_of_range(); + if (update_prices) { + _price_list = LadderRender.render_prices(_pricevis, market_break, _row_count, _settings.row_height, _settings.price_column, security.ContractSpec); + _ordershost.update_settings(_price_list, _price_comparer, _settings.row_height, _settings.orders_column); + LadderRender.render_grid(_gridvis, _price_list, _row_count, _settings.row_height, _settings.grid_line_color); } + int market_break_index = LadderRender.index_of(_price_list, market_break, _price_comparer); + if (update_prices || market_break != _last_market_break) { + _last_market_break = market_break; + LadderRender.render_mid(_midvis, market_break_index, _settings.row_height); + } + if (update_prices || _last_trade != last) { + _last_trade = last; + var last_index = LadderRender.index_of(_price_list, last.price, _price_comparer); + if (last_index > 0) { + LadderRender.render_last(_lastvis, last, last_index, _settings.row_height, _settings.last_column); + } + } + if (update_prices || depth_changed) { + Logger.assert_d(_price_list != null); + LadderRender.render_sizes(_bidvis, market_break_index, _price_list, _bids, _settings.row_height, security.ContractSpec, _settings.bid_column); + LadderRender.render_sizes(_askvis, market_break_index, _price_list, _asks, _settings.row_height, security.ContractSpec, _settings.ask_column); + } + _price_dirty = false; } } + + double _get_market_break() { + double best_bid = _get_top_price(_bids); + double best_ask = _get_top_price(_asks); + switch (_settings.market_break_type) { + case MarketBreakType.Bid: + return double.IsNaN(best_bid) ? + double.IsNaN(best_ask) ? _vm.low : security.ContractSpec.tickdown(best_ask) : + best_bid; + case MarketBreakType.Ask: + return double.IsNaN(best_ask) ? + double.IsNaN(best_bid) ? security.ContractSpec.tickdown(_vm.high) : best_bid : + security.ContractSpec.tickdown(best_ask); + case MarketBreakType.Mid: + return double.IsNaN(best_bid) ? + double.IsNaN(best_ask) ? 0.5 * (_vm.low + _vm.high) : security.ContractSpec.tickdown(best_ask) : + double.IsNaN(best_ask) ? best_bid : TickUtils.mid_tick(security.ContractSpec, best_bid, best_ask); + default: throw new NotImplementedException($"Unknown MarketBreakType ({_settings.market_break_type})"); + } + } + + double _get_top_price(NativePriceSize[] prices) => + prices != null && prices.Length > 0 ? prices[0].price : double.NaN; void _settings_updated(LadderSettings s) { _settings = s; @@ -335,7 +362,7 @@ namespace mbts.spark { } void _render_market_col(Column column, NativePriceSize[] depth) { - var mid = LadderRender.index_of(_price_list, _last_mid, _price_comparer); + var mid = LadderRender.index_of(_price_list, _last_market_break, _price_comparer); LadderRender.render_sizes(column.visual, mid, _price_list, depth, _settings.row_height, security.ContractSpec, column.settings); } @@ -346,8 +373,8 @@ namespace mbts.spark { } bool _prices_out_of_range() { - double low = TickUtils.low_tick(_bids, _asks); - double high = TickUtils.high_tick(_bids, _asks); + double low = _bids == null ? double.NaN : TickUtils.low_tick(_bids, _asks); + double high = _asks == null ? double.NaN : TickUtils.high_tick(_bids, _asks); return (!double.IsNaN(low) && _price_list[_price_list.Length - 1] > low) || (!double.IsNaN(high) && _price_list[0] < high); diff --git a/src/client/spark/view/Spark/Ladder/LadderSettings.cs b/src/client/spark/view/Spark/Ladder/LadderSettings.cs index 6f49f6f0e..2961bc810 100644 --- a/src/client/spark/view/Spark/Ladder/LadderSettings.cs +++ b/src/client/spark/view/Spark/Ladder/LadderSettings.cs @@ -3,7 +3,7 @@ using mbts.ui; using ProtoBuf; namespace mbts.spark { - public enum ATMLine { Bid, Ask, Mid } + public enum MarketBreakType { Bid, Ask, Mid } [ProtoContract] public class LadderColumnSettings { @@ -49,7 +49,7 @@ namespace mbts.spark { return new LadderSettings { row_height = 20, grid_line_color = 0x535353, - atm_line = ATMLine.Bid, + market_break_type = MarketBreakType.Bid, orders_column = orders_column, bid_column = bid_column, ask_column = ask_column, @@ -71,7 +71,7 @@ namespace mbts.spark { [ProtoMember(2)] public uint grid_line_color { get; set; } [ProtoMember(3)] - public ATMLine atm_line { get; set; } + public MarketBreakType market_break_type { get; set; } // Column settings [ProtoMember(4)] diff --git a/src/client/spark/view/Spark/Ladder/LadderVM.cs b/src/client/spark/view/Spark/Ladder/LadderVM.cs index 94c53f407..fcb8b9abc 100644 --- a/src/client/spark/view/Spark/Ladder/LadderVM.cs +++ b/src/client/spark/view/Spark/Ladder/LadderVM.cs @@ -29,8 +29,7 @@ namespace mbts.spark { class LadderVM : OrderPropertiesEditorVM, IKeyListener { readonly IBus _bus; readonly LadderModel _model; - readonly Property _high, _low, _change, _open; - readonly Property _change_double; + readonly Property _high, _low, _change, _open; readonly Property _volume; readonly Property _revert; LadderSettings _settings; @@ -47,11 +46,10 @@ namespace mbts.spark { : base(model.order_gateway, Observable.Return(model.security), settings) { _bus = bus; _model = model; - _high = backing(nameof(high), ""); - _low = backing(nameof(low), ""); - _change = backing(nameof(change), ""); - _change_double = backing(nameof(change_double), 0.00); - _open = backing(nameof(open), ""); + _high = backing(nameof(high), double.NaN); + _low = backing(nameof(low), double.NaN); + _change = backing(nameof(change), double.NaN); + _open = backing(nameof(open), double.NaN); _volume = backing(nameof(volume), 0ul); _revert = backing(nameof(revert), false); log_categories = new[] { order_category, cancel_category, modify_category, fill_category, warn_category }; @@ -82,11 +80,10 @@ namespace mbts.spark { public bool is_synthetic => _model.security.ContractCase == Security.ContractOneofCase.Synthetic; public LogViewerMessageCollection log_messages { get; } = new LogViewerMessageCollection(); public LadderSettings current_settings => _settings; - public string high => _high.Value; - public string low => _low.Value; - public string change => _change.Value; - public double change_double => _change_double.Value; - public string open => _open.Value; + public double high => _high.Value; + public double low => _low.Value; + public double change => _change.Value; + public double open => _open.Value; public ulong volume => _volume.Value; public bool revert { get => _revert.Value; set => _revert.set(value); } public ObservableCollection orders { get; } = new ObservableCollection(); @@ -159,20 +156,19 @@ namespace mbts.spark { void _update_header() { if (_misc_time != _model.md.misc_timestamp) { var spec = security.ContractSpec; - _high.set(spec.format(md.session_high)); - _low.set(spec.format(md.session_low)); - _open.set(spec.format(md.open_price)); + _high.set(md.session_high); + _low.set(md.session_low); + _open.set(md.open_price); ulong unused = 0; md.get_last_trade(ref unused, out NativePriceSize last, out DateTime time, out ulong volume); if (!double.IsNaN(last.price)) { - _change_double.set(last.price - md.settlement_price); + _change.set(last.price - md.settlement_price); } else { unused = 0; _model.md.get_bbo(ref unused, out NativePriceSize bid, out NativePriceSize ask); double mid = TickUtils.mid_tick(spec, bid.price, ask.price); - _change_double.set(mid - md.settlement_price); + _change.set(mid - md.settlement_price); } - _change.set(spec.format(_change_double.Value)); _volume.set(volume); } } diff --git a/src/client/spark/view/Spark/Ladder/LadderView.xaml b/src/client/spark/view/Spark/Ladder/LadderView.xaml index 7363349a0..a4d971c70 100644 --- a/src/client/spark/view/Spark/Ladder/LadderView.xaml +++ b/src/client/spark/view/Spark/Ladder/LadderView.xaml @@ -104,10 +104,38 @@ - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +