diff --git a/src/client/spark/Modules/Trading/Ladder/LadderSettings.cs b/src/client/spark/Modules/Trading/Ladder/LadderSettings.cs index 4cb4e2f99..750d3422c 100644 --- a/src/client/spark/Modules/Trading/Ladder/LadderSettings.cs +++ b/src/client/spark/Modules/Trading/Ladder/LadderSettings.cs @@ -6,6 +6,7 @@ using bts.utils; using mbts.ui; using mbts.ui.settings; using mbts.ui.spark; +using mbts.ui.utility.text; using ProtoBuf; using System; using System.Collections.Generic; @@ -13,6 +14,7 @@ using System.Collections.Immutable; using System.ComponentModel; using System.Linq; using System.Windows; +using System.Windows.Media; namespace mbts.spark { // IMPORTANT: BE SURE TO ADD ANY NEW PROPERTIES TO THE EQUALS FUNCTION! @@ -324,6 +326,124 @@ namespace mbts.spark { public static bool operator !=(LadderMiniOrderBookSettings a, LadderMiniOrderBookSettings b) => !a.Equals(b); } + [ProtoContract] + class LadderPanelPricePointSettings : IEquatable { + public static LadderPanelPricePointSettings Default => + new LadderPanelPricePointSettings { + day_change = new DaySettings { + font = _default_font, + negative_font_color = Colors.Red.rgb_color(), + negative_background_color = Colors.Transparent.rgb_color(), + positive_font_color = Colors.Green.rgb_color(), + positive_background_color = Colors.Transparent.rgb_color() + }, + session_low = _default_item, + session_high = _default_item, + open = _default_item, + settlement = _default_item, + volume = _default_item + }; + + [ProtoMember(1)] + public DaySettings day_change { get; set; } + [ProtoMember(2)] + public ItemSettings session_low { get; set; } + [ProtoMember(3)] + public ItemSettings session_high { get; set; } + [ProtoMember(4)] + public ItemSettings open { get; set; } + [ProtoMember(5)] + public ItemSettings settlement { get; set; } + [ProtoMember(6)] + public ItemSettings volume { get; set; } + + public bool Equals(LadderPanelPricePointSettings other) => + other != null && + day_change.safe_equals(other.day_change) && + session_high.safe_equals(other.session_high) && + session_high.safe_equals(other.session_high) && + open.safe_equals(other.open) && + settlement.safe_equals(other.settlement) && + volume.safe_equals(other.volume); + + static FontParamsProto _default_font => + new FontParamsProto { + font_color = Colors.Black.rgb_color(), + font_size = FontSizes.Regular, + typeface = new TypefaceProto(TextUtils.StandardFontFamilyName, FontWeights.Bold.ToOpenTypeWeight()) + }; + + static ItemSettings _default_item => + new ItemSettings { + font = _default_font, + background_color = Colors.Transparent.rgb_color(), + show_label = true + }; + + [ProtoContract] + readonly struct SignedColorPair : IEquatable { + [ProtoMember(1)] + public uint positive_color { get; } + [ProtoMember(2)] + public uint negative_color { get; } + + public bool Equals(SignedColorPair other) => + positive_color == other.positive_color && + negative_color == other.negative_color; + } + + [ProtoContract] + public class ItemSettings : IEquatable { + [ProtoMember(1)] + public FontParamsProto font { get; set; } + [ProtoMember(2)] + public uint background_color { get; set; } + [ProtoMember(3)] + public bool show_label { get; set; } + + public bool Equals(ItemSettings other) => + other != null && + font.safe_equals(other.font) && + background_color == other.background_color && + show_label == other.show_label; + } + + [ProtoContract] + public class DaySettings : IEquatable { + [ProtoMember(1)] + public FontParamsProto font { get; set; } + // [neutral] background + [ProtoMember(2)] + public uint background_color { get; set; } + [ProtoMember(3)] + public bool show_label { get; set; } + + // negative + [ProtoMember(4)] + public uint negative_font_color { get; set; } + [ProtoMember(5)] + public uint negative_background_color { get; set; } + + // positive + [ProtoMember(6)] + public uint positive_font_color { get; set; } + [ProtoMember(7)] + public uint positive_background_color { get; set; } + + public uint neutral_foreground => font?.font_color ?? 0; + + public bool Equals(DaySettings other) => + other != null && + font.safe_equals(other.font) && + background_color == other.background_color && + show_label == other.show_label && + negative_font_color == other.negative_font_color && + negative_background_color == other.negative_background_color && + positive_font_color == other.positive_font_color && + positive_background_color == other.positive_background_color; + } + } + // IMPORTANT: BE SURE TO ADD ANY NEW PROPERTIES TO THE EQUALS FUNCTION! // 1 important reason at this time: // From the settings dialog, default settings will only be persisted when they are NOT EQUAL to the current default settings. @@ -433,6 +553,7 @@ namespace mbts.spark { auto_clear_visible = true, clear_visible = true, order_entry_panel_initial_width = _default_order_entry_panel_width, + panel_price_point_settings = LadderPanelPricePointSettings.Default, zoom_slider_visible = false, zoom_factor = _default_zoom_factor, net_change = NetChangeTerms.None, @@ -619,6 +740,8 @@ namespace mbts.spark { [ProtoMember(900)] public LadderOrderLimits ladder_order_limits { get; set; } + [ProtoMember(901)] + public LadderPanelPricePointSettings panel_price_point_settings { get; set; } public void sanitize() => sanitize(this); @@ -653,6 +776,8 @@ namespace mbts.spark { s.mini_ob = new LadderMiniOrderBookSettings(s.mini_ob.value.with(sorting_criterion: bts.shell.orderbook.OrderBookStartupData.default_mini_grid.general.sorting_criterion)); #pragma warning restore CS0612 // Type or member is obsolete s.row_height = LadderRender.measure_row_height(s); + if (s.panel_price_point_settings is null) + s.panel_price_point_settings = LadderPanelPricePointSettings.Default; return s; } @@ -727,7 +852,8 @@ namespace mbts.spark { a.price_display_format == b.price_display_format && a.status_message_visible == b.status_message_visible && a.ladder_order_limits.safe_equals(b.ladder_order_limits) && - a._ticks_per_row == b._ticks_per_row; + a._ticks_per_row == b._ticks_per_row && + a.panel_price_point_settings.safe_equals(b.panel_price_point_settings); public bool Equals(LadderSettings other) => profile_equals(this, other) && diff --git a/src/client/spark/Modules/Trading/Ladder/VM/LadderPanelPricePointVM.cs b/src/client/spark/Modules/Trading/Ladder/VM/LadderPanelPricePointVM.cs new file mode 100644 index 000000000..3f76a73cf --- /dev/null +++ b/src/client/spark/Modules/Trading/Ladder/VM/LadderPanelPricePointVM.cs @@ -0,0 +1,116 @@ +using bts.proto; +using mbts.ui; +using mbts.ui.shell; +using System.Windows; +using System.Windows.Media; +using mbts.ui.utility.text; +using FontFamily = System.Windows.Media.FontFamily; + +namespace mbts.spark { + class LadderPanelPricePointVM : ViewModelBase, IValueFactoryVM { + Color _background_color = Colors.Transparent, _font_color = Colors.Black; + FontFamily _font_family = TextUtils.StandardFontFamily; + double _font_size = FontSizes.Small; + FontWeight _font_weight = FontWeights.Bold; + bool _show = true, _show_label = true; + + public Color background_color { get => _background_color; set => Set(ref _background_color, value); } + public Color font_color { get => _font_color; set => Set(ref _font_color, value); } + public FontFamily font_family { get => _font_family; set => Set(ref _font_family, value); } + public double font_size { get => _font_size; set => Set(ref _font_size, value); } + public FontWeight font_weight { get => _font_weight; set => Set(ref _font_weight, value); } + public bool show { + get => _show; + set { + if (Set(ref _show, value)) + RaisePropertyChanged(nameof(actual_show_label)); + } + } + public bool show_label { + get => _show_label; + set { + if (Set(ref _show_label, value)) + RaisePropertyChanged(nameof(actual_show_label)); + } + } + public bool actual_show_label => _show && _show_label; + + public LadderPanelPricePointSettings.ItemSettings create() => + new LadderPanelPricePointSettings.ItemSettings { + font = new FontParamsProto { + font_color = font_color.rgb_color(), + font_size = font_size, + typeface = new TypefaceProto(font_family.name_or_default(), font_weight.ToOpenTypeWeight()) + }, + background_color = background_color.rgb_color(), + show_label = show_label + }; + + public void load_from(LadderPanelPricePointSettings.ItemSettings value) { + if (value is null) + return; + load_from(value.font); + show_label = value.show_label; + background_color = value.background_color.argb_color(); + } + + protected void load_from(FontParamsProto font) { + if (font is null) { + font_weight = FontWeights.Bold; + font_size = FontSizes.Small; + show_label = true; + } else { + font_family = string.IsNullOrEmpty(font.font_family) ? + TextUtils.StandardFontFamily : + new FontFamily(font.font_family); + font_weight = FontWeight.FromOpenTypeWeight(font.font_weight); + font_size = font.font_size; + font_color = font.font_color.argb_color(); + } + } + } + + class LadderPanelRelativePricePointVM : LadderPanelPricePointVM, IValueFactoryVM { + Color + _negative_font_color = Colors.Red, + _negative_background_color = Colors.Transparent, + _positive_font_color = Colors.Green, + _positive_background_color = Colors.Transparent; + + // negative + public Color negative_font_color { get => _negative_font_color; set => Set(ref _negative_font_color, value); } + public Color negative_background_color { get => _negative_background_color; set => Set(ref _negative_background_color, value); } + // positive + public Color positive_font_color { get => _positive_font_color; set => Set(ref _positive_font_color, value); } + public Color positive_background_color { get => _positive_background_color; set => Set(ref _positive_background_color, value); } + + public void load_from(LadderPanelPricePointSettings.DaySettings value) { + if (value is null) + return; + load_from(value.font); + show_label = value.show_label; + background_color = value.background_color.argb_color(); + // negative + negative_font_color = value.negative_font_color.argb_color(); + negative_background_color = value.negative_background_color.argb_color(); + // positive + positive_font_color = value.positive_font_color.argb_color(); + positive_background_color = value.positive_background_color.argb_color(); + } + + public new LadderPanelPricePointSettings.DaySettings create() { + var base_settings = base.create(); + return new LadderPanelPricePointSettings.DaySettings { + font = base_settings.font, + show_label = base_settings.show_label, + background_color = base_settings.background_color, + // negative + negative_font_color = negative_font_color.rgb_color(), + negative_background_color = negative_background_color.rgb_color(), + // positive + positive_font_color = positive_font_color.rgb_color(), + positive_background_color = positive_background_color.rgb_color() + }; + } + } +} diff --git a/src/client/spark/Modules/Trading/Ladder/VM/LadderSettingsVM.cs b/src/client/spark/Modules/Trading/Ladder/VM/LadderSettingsVM.cs index d3761bf85..f6d18a1d4 100644 --- a/src/client/spark/Modules/Trading/Ladder/VM/LadderSettingsVM.cs +++ b/src/client/spark/Modules/Trading/Ladder/VM/LadderSettingsVM.cs @@ -16,6 +16,7 @@ using mbts.ui.spark; using mbts.ui.utility; using mbts.utils; using System; +using System.Collections; using System.Collections.Generic; using System.Collections.Immutable; using System.Collections.ObjectModel; @@ -31,6 +32,10 @@ using TextUtils = mbts.ui.utility.text.TextUtils; namespace mbts.spark { class LadderSettingsVM : DisposableViewModelBase, IWindowSettingsVM, IValueFactory, IObservable, ITemplateSelectorKey { + public static Type PanelPricePointType => typeof(PanelPricePointVM); + public static Type PanelRelativePricePointType => typeof(PanelRelativePricePointVM); + public static Type PanelVisibilityType => typeof(OrderEntryPanelItemVisibilityVM); + readonly Property _badge_style; readonly Property _alternate_order_size, _max_clip; readonly DelegateProperty _row_height; @@ -68,12 +73,6 @@ namespace mbts.spark { _session_range_fill = new LadderMarketBreakLineVM("Session Range", 0, 0, false, can_edit_refresh_mode: false); OrderEntryPanelItemVisibilityVM _zoom_slider_visbility = new OrderEntryPanelItemVisibilityVM("Zoom Slider"), - _day_visibility = new OrderEntryPanelItemVisibilityVM("Day Change"), - _high_visibility = new OrderEntryPanelItemVisibilityVM("High"), - _low_visibility = new OrderEntryPanelItemVisibilityVM("Low"), - _open_visibility = new OrderEntryPanelItemVisibilityVM("Open"), - _settle_visibility = new OrderEntryPanelItemVisibilityVM("Settle"), - _volume_visibility = new OrderEntryPanelItemVisibilityVM("Volume"), _account_visibility = new OrderEntryPanelItemVisibilityVM("Account"), _tif_visibility = new OrderEntryPanelItemVisibilityVM("Time In Force"), _order_type_visibility = new OrderEntryPanelItemVisibilityVM("Order Type"), @@ -87,6 +86,14 @@ namespace mbts.spark { _cancel_sells_visibility = new OrderEntryPanelItemVisibilityVM("Cancel Sells"), _cancel_all_visibility = new OrderEntryPanelItemVisibilityVM("Cancel All"), _status_message_visibility = new OrderEntryPanelItemVisibilityVM("Status Message"); + PanelRelativePricePointVM + _day_change = new PanelRelativePricePointVM("Day Change"); + PanelPricePointVM + _session_high = new PanelPricePointVM("High"), + _session_low = new PanelPricePointVM("Low"), + _open = new PanelPricePointVM("Open"), + _settlement = new PanelPricePointVM("Settle"), + _volume_panel_item = new PanelPricePointVM("Volume"); public LadderSettingsVM(OrdersModel model, SecurityImmutable security) { _model = model; @@ -283,15 +290,15 @@ namespace mbts.spark { } } - public IEnumerable order_entry_panel_items { + public IEnumerable order_entry_panel_items { get { yield return _zoom_slider_visbility; - yield return _day_visibility; - yield return _high_visibility; - yield return _low_visibility; - yield return _open_visibility; - yield return _settle_visibility; - yield return _volume_visibility; + yield return _day_change; + yield return _session_high; + yield return _session_low; + yield return _open; + yield return _settlement; + yield return _volume_panel_item; yield return _account_visibility; yield return _tif_visibility; yield return _order_type_visibility; @@ -330,12 +337,12 @@ namespace mbts.spark { yield return _session_range_fill; // left panel yield return _zoom_slider_visbility; - yield return _day_visibility; - yield return _high_visibility; - yield return _low_visibility; - yield return _open_visibility; - yield return _settle_visibility; - yield return _volume_visibility; + yield return _day_change; + yield return _session_high; + yield return _session_low; + yield return _open; + yield return _settlement; + yield return _volume_panel_item; yield return _account_visibility; yield return _tif_visibility; yield return _order_type_visibility; @@ -395,12 +402,18 @@ namespace mbts.spark { zoom_factor = settings.zoom_factor; // left-panel (show/hide) _zoom_slider_visbility.show = settings.zoom_slider_visible; - _day_visibility.show = settings.day_visible; - _high_visibility.show = settings.high_visible; - _low_visibility.show = settings.low_visible; - _open_visibility.show = settings.open_visible; - _settle_visibility.show = settings.settle_visible; - _volume_visibility.show = settings.volume_visible; + _day_change.show = settings.day_visible; + _day_change.load_from(settings.panel_price_point_settings.day_change); + _session_high.show = settings.high_visible; + _session_high.load_from(settings.panel_price_point_settings.session_high); + _session_low.show = settings.low_visible; + _session_low.load_from(settings.panel_price_point_settings.session_low); + _open.show = settings.open_visible; + _open.load_from(settings.panel_price_point_settings.open); + _settlement.show = settings.settle_visible; + _settlement.load_from(settings.panel_price_point_settings.settlement); + _volume_panel_item.show = settings.volume_visible; + _volume_panel_item.load_from(settings.panel_price_point_settings.volume); _account_visibility.show = settings.account_visible; _tif_visibility.show = settings.tif_visible; _order_type_visibility.show = settings.order_type_visible; @@ -463,6 +476,15 @@ namespace mbts.spark { clone.session_low_line = _session_low_mkt_line.get_settings(); clone.session_high_line = _session_high_mkt_line.get_settings(); clone.session_range_fill = _session_range_fill.get_settings(); + // left-panel price items + clone.panel_price_point_settings = new LadderPanelPricePointSettings { + day_change = _day_change.create(), + open = _open.create(), + session_low = _session_low.create(), + session_high = _session_high.create(), + volume = _volume_panel_item.create(), + settlement = _settlement.create(), + }; // left-panel (defaults) clone.default_time_in_force = default_time_in_force; // - account @@ -477,12 +499,12 @@ namespace mbts.spark { clone.order_entry_panel_initial_width = _order_entry_panel_width; clone.zoom_factor = zoom_factor; // left-panel (components) - clone.day_visible = _day_visibility.show; - clone.high_visible = _high_visibility.show; - clone.low_visible = _low_visibility.show; - clone.open_visible = _open_visibility.show; - clone.settle_visible = _settle_visibility.show; - clone.volume_visible = _volume_visibility.show; + clone.day_visible = _day_change.show; + clone.high_visible = _session_high.show; + clone.low_visible = _session_low.show; + clone.open_visible = _open.show; + clone.settle_visible = _settlement.show; + clone.volume_visible = _volume_panel_item.show; clone.account_visible = _account_visibility.show; clone.tif_visible = _tif_visibility.show; clone.order_type_visible = _order_type_visibility.show; @@ -675,7 +697,7 @@ namespace mbts.spark { }; } - public class OrderEntryPanelItemVisibilityVM : ViewModelBase { + public class OrderEntryPanelItemVisibilityVM : ViewModelBase, ITemplateSelectorKey { readonly Property _show; public OrderEntryPanelItemVisibilityVM(string name) { @@ -686,6 +708,63 @@ namespace mbts.spark { public string name { get; } public bool show { get => _show.Value; set => _show.Value = value; } + string ITemplateSelectorKey.key => LadderSettingsPanelTemplateKeys.VisibilityItem; + } + + public class PanelPricePointVM : LadderPanelPricePointVM, ITemplateSelectorKey { + ObservableCollection + _font_color_palettes, + _background_color_palettes; + + public PanelPricePointVM(string name) => + this.name = name; + + public string name { get; } + + public ObservableCollection font_color_palettes => _font_color_palettes ?? + (_font_color_palettes = ColorPalette.all_presets().to_viewmodels()); + + public ObservableCollection background_color_palettes => _background_color_palettes ?? + (_background_color_palettes = ColorPalette.all_presets().to_viewmodels()); + + string ITemplateSelectorKey.key => LadderSettingsPanelTemplateKeys.PricePoint; + } + + public class PanelRelativePricePointVM : LadderPanelRelativePricePointVM, ITemplateSelectorKey { + ObservableCollection + _negative_font_color_palettes, + _negative_background_color_palettes, + _neutral_font_color_palettes, + _neutral_background_color_palettes, + _positive_font_color_palettes, + _positive_background_color_palettes; + + public PanelRelativePricePointVM(string name) => + this.name = name; + + public string name { get; } + //negative + public ObservableCollection negative_font_color_palettes => _negative_font_color_palettes ?? + (_negative_font_color_palettes = ColorPalette.all_presets().to_viewmodels()); + + public ObservableCollection negative_background_color_palettes => _negative_background_color_palettes ?? + (_negative_background_color_palettes = ColorPalette.all_presets().to_viewmodels()); + + // neutral + public ObservableCollection neutral_font_color_palettes => _neutral_font_color_palettes ?? + (_neutral_font_color_palettes = ColorPalette.all_presets().to_viewmodels()); + + public ObservableCollection neutral_background_color_palettes => _neutral_background_color_palettes ?? + (_neutral_background_color_palettes = ColorPalette.all_presets().to_viewmodels()); + + // positive + public ObservableCollection positive_font_color_palettes => _positive_font_color_palettes ?? + (_positive_font_color_palettes = ColorPalette.all_presets().to_viewmodels()); + + public ObservableCollection positive_background_color_palettes => _positive_background_color_palettes ?? + (_positive_background_color_palettes = ColorPalette.all_presets().to_viewmodels()); + + string ITemplateSelectorKey.key => LadderSettingsPanelTemplateKeys.RelativePricePoint; } public class VolumeColumnVM : ViewModelBase { diff --git a/src/client/spark/Modules/Trading/Ladder/VM/LadderVM.cs b/src/client/spark/Modules/Trading/Ladder/VM/LadderVM.cs index dd03a3c63..ede771337 100644 --- a/src/client/spark/Modules/Trading/Ladder/VM/LadderVM.cs +++ b/src/client/spark/Modules/Trading/Ladder/VM/LadderVM.cs @@ -10,6 +10,7 @@ using mbts.spark.utilities; using mbts.ui; using mbts.ui.shell; using mbts.ui.utility; +using mbts.user.settings; using mbts.utils; using System; using System.Collections.Generic; @@ -28,11 +29,11 @@ namespace mbts.spark { readonly IBus _bus; readonly LadderModel _model; - readonly Property _high, _low, _change, _open, _settlement, _left_panel_width, _zoom_factor; + readonly Property _session_high, _session_low, _day_change, _open, _settlement, _left_panel_width, _zoom_factor; readonly Property _orders_column_width, _bid_column_width, _price_column_width, _ask_column_width, _last_column_width, _volume_column_width, _piq_column_width; readonly Property _volume; readonly Property _auto_clear; - readonly PropertyAutoWrapper _show_day, _show_high, _show_low, _show_open, _show_settle, _show_volume, _show_account, + readonly PropertyAutoWrapper _show_day, _show_high, _show_low, _show_open, _show_settlement, _show_volume, _show_account, _show_tif, _show_order_type, _show_size_preset, _show_auto_clear, _show_clear, _show_cancel_buys, _show_cancel_sells, _show_cancel_all, _show_zoom, _show_status_message; readonly PropertyAutoWrapper _bid_background, _bid_foreground, _ask_background, _ask_foreground; @@ -74,9 +75,9 @@ namespace mbts.spark { size_calculator.display_divisor = display_quantity_divisor; _slot_price_vs_orders = new Dictionary(_equality); this.order_properties_vm = order_properties_vm; - _high = backing(nameof(high), double.NaN); - _low = backing(nameof(low), double.NaN); - _change = backing(nameof(change), double.NaN); + _session_high = backing(nameof(session_high), double.NaN); + _session_low = backing(nameof(session_low), double.NaN); + _day_change = backing(nameof(day_change), double.NaN); _open = backing(nameof(open), double.NaN); _settlement = backing(nameof(settlement), double.NaN); // init window-specific settings: these properties should not observe settings updates; they are independent from the settings dialog, so it doesn't make sense that the settings dialog should cause them to change. @@ -111,7 +112,7 @@ namespace mbts.spark { (_show_high = bool_prop(nameof(show_high), s => s.high_visible)).set, (_show_low = bool_prop(nameof(show_low), s => s.low_visible)).set, (_show_open = bool_prop(nameof(show_open), s => s.open_visible)).set, - (_show_settle = bool_prop(nameof(show_settle), s => s.settle_visible)).set, + (_show_settlement = bool_prop(nameof(show_settlement), s => s.settle_visible)).set, (_show_volume = bool_prop(nameof(show_volume), s => s.volume_visible)).set, (_show_account = bool_prop(nameof(show_account), s => s.account_visible)).set, (_show_tif = bool_prop(nameof(show_tif), s => s.tif_visible)).set, @@ -144,8 +145,15 @@ namespace mbts.spark { _piq_column_width.set(s.piq_column.width); _volume_column_width.set(s.volume_column.general.width); // left panel/zoom + day_change_style.load_from(s.panel_price_point_settings.day_change); + open_style.load_from(s.panel_price_point_settings.open); + session_low_style.load_from(s.panel_price_point_settings.session_low); + session_high_style.load_from(s.panel_price_point_settings.session_high); + settlement_style.load_from(s.panel_price_point_settings.settlement); + volume_style.load_from(s.panel_price_point_settings.volume); _left_panel_width.set(s.order_entry_panel_initial_width); _zoom_factor.set(s.zoom_factor); + RaisePropertyChanged(nameof(day_change_font_color), nameof(day_change_background_color)); } }; PropertyAutoWrapper bool_prop(string name, Func get_bool) => @@ -171,6 +179,7 @@ namespace mbts.spark { CompareUtils.nullable_compare(spec.full_ticks_from_zero(a), spec.full_ticks_from_zero(b))); return _order_price_source(model.orders, price_comp, p => TickUtils.normalize_price(spec, p)); }); + _day_change.Changed += (o, n) => RaisePropertyChanged(nameof(day_change_font_color), nameof(day_change_background_color)); _dispose = Disposable.Create( settings.OnDispatcher().Subscribe(settings_observer), @@ -277,15 +286,30 @@ namespace mbts.spark { _spec = TickUtils.multiply_tick_size(security.ContractSpec, current_settings?.ticks_per_row ?? 1)).Value; public uint display_quantity_divisor { get; } public LadderSettings current_settings { get; private set; } - public double high => _high.Value; - public double low => _low.Value; - public double change => _change.Value; + // left panel values + public double session_high => _session_high.Value; + public double session_low => _session_low.Value; + public double day_change => _day_change.Value; public double open => _open.Value; public double settlement => _settlement.Value; public double position => _position; public double day_position => _day_position; public double raw_day_position => _raw_day_position; public ulong volume => _volume.Value; + // left panel styles + public LadderPanelRelativePricePointVM day_change_style { get; } = new LadderPanelRelativePricePointVM(); + public Color day_change_font_color => day_change > 0 ? + day_change_style.positive_font_color : + (day_change < 0 ? day_change_style.negative_font_color : day_change_style.font_color); + public Color day_change_background_color => day_change > 0 ? + day_change_style.positive_background_color : + (day_change < 0 ? day_change_style.negative_background_color : day_change_style.background_color); + public LadderPanelPricePointVM open_style { get; } = new LadderPanelPricePointVM(); + public LadderPanelPricePointVM settlement_style { get; } = new LadderPanelPricePointVM(); + public LadderPanelPricePointVM session_high_style { get; } = new LadderPanelPricePointVM(); + public LadderPanelPricePointVM session_low_style { get; } = new LadderPanelPricePointVM(); + public LadderPanelPricePointVM volume_style { get; } = new LadderPanelPricePointVM(); + // public bool auto_clear { get => _auto_clear.Value; set => _auto_clear.set(value); } public CalculatorLogic size_calculator { get; } = new CalculatorLogic(0); /// Important: can be null. @@ -304,7 +328,7 @@ namespace mbts.spark { public bool show_high => _show_high.view; public bool show_low => _show_low.view; public bool show_open => _show_open.view; - public bool show_settle => _show_settle.view; + public bool show_settlement => _show_settlement.view; public bool show_volume => _show_volume.view; public bool show_account => _show_account.view; public bool show_tif => _show_tif.view; @@ -320,6 +344,11 @@ namespace mbts.spark { public bool show_cancel_all => _show_cancel_all.view; public bool show_zoom => _show_zoom.view; public bool show_status_message => _show_status_message.view; + + public TickDisplaySettings day_change_display_settings => + TickDisplaySettings.Default.with( + pad_with_leading_zeros: false, + show_zero_integer: false); // left panel scroll props public double left_panel_view_port_height { get => _left_panel_view_port_height; @@ -423,6 +452,14 @@ namespace mbts.spark { if (order_properties_vm.selected_algo_definition != null) rv.default_algo = AlgoID.from_algo_key(order_properties_vm.selected_algo_definition.key); } + rv.panel_price_point_settings = new LadderPanelPricePointSettings { + day_change = day_change_style.create(), + open = open_style.create(), + settlement = settlement_style.create(), + session_low = session_low_style.create(), + session_high = session_high_style.create(), + volume = volume_style.create() + }; return rv; } @@ -596,17 +633,17 @@ namespace mbts.spark { void _update_header() { bool last_updated = md.get_last_trade(ref _last_trade_time, out NativePriceSize last, out DateTime time, out ulong volume); if (_misc_time != _model.md.misc_timestamp || last_updated) { - _high.set(md.session_high); - _low.set(md.session_low); + _session_high.set(md.session_high); + _session_low.set(md.session_low); _open.set(md.open_price); _settlement.set(md.settlement_price); if (!double.IsNaN(last.price)) { - _change.set(last.price - md.settlement_price); + _day_change.set(last.price - md.settlement_price); } else { ulong unused = 0; _model.md.get_bbo(ref unused, out NativePriceSize bid, out NativePriceSize ask); double mid = TickUtils.mid_tick(security.ContractSpec, bid.price, ask.price); - _change.set(mid - md.settlement_price); + _day_change.set(mid - md.settlement_price); } _volume.set(volume); _misc_time = _model.md.misc_timestamp; diff --git a/src/client/spark/Modules/Trading/Ladder/View/LadderSettingsView.xaml b/src/client/spark/Modules/Trading/Ladder/View/LadderSettingsView.xaml index df9f04833..a1fcdb828 100644 --- a/src/client/spark/Modules/Trading/Ladder/View/LadderSettingsView.xaml +++ b/src/client/spark/Modules/Trading/Ladder/View/LadderSettingsView.xaml @@ -173,6 +173,191 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Font Color + + Background Color + + Show Label + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + @@ -963,27 +1210,23 @@ Left Panel Width - - - + - - + + Zoom Factor - - - + - - + + Algo - + @@ -1020,30 +1263,16 @@ - + - - - - - - - - - - - - - - - - + + + + 1.5 0.5 @@ -345,107 +362,182 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + to_viewmodels(this IEnumerable source) => + new ObservableCollection(source.empty_if_null().Select(cp => new ColorPaletteVM(cp))); + } } \ No newline at end of file diff --git a/src/client/spark/view/Converters/ColorToBrushConverter.cs b/src/client/spark/view/Converters/ColorToBrushConverter.cs index 779ca71ce..af930199a 100644 --- a/src/client/spark/view/Converters/ColorToBrushConverter.cs +++ b/src/client/spark/view/Converters/ColorToBrushConverter.cs @@ -5,6 +5,8 @@ using System.Windows.Media; namespace mbts.ui.converters { class ColorToBrushConverter : IValueConverter { + public static ColorToBrushConverter Instance => new ColorToBrushConverter(); + public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { if (value is Color color) return new SolidColorBrush(color); if (value is BTSColor bts_color) return new SolidColorBrush(bts_color); @@ -24,4 +26,4 @@ namespace mbts.ui.converters { public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) => throw new NotImplementedException(); } -} \ No newline at end of file +} diff --git a/src/client/spark/view/Converters/TickDisplayConverter.cs b/src/client/spark/view/Converters/TickDisplayConverter.cs index 799c82154..fb88fb6ef 100644 --- a/src/client/spark/view/Converters/TickDisplayConverter.cs +++ b/src/client/spark/view/Converters/TickDisplayConverter.cs @@ -1,4 +1,5 @@ using bts.api; +using mbts; using mbts.spark; using System; using System.Globalization; @@ -17,7 +18,8 @@ namespace mbts.ui.spark.converters { public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture) { var price = get_val(values, 0); var spec = get_val(values, 1) ?? SecurityDataHelper.Defaults.ContractSpecificationImmutable; - return spec.format(price); + var settings = get_val(values, 2); + return spec.format(price, settings); } public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture) => diff --git a/src/client/spark/view/Utility/TextUtils.cs b/src/client/spark/view/Utility/TextUtils.cs index 808fe462a..1917aff7f 100644 --- a/src/client/spark/view/Utility/TextUtils.cs +++ b/src/client/spark/view/Utility/TextUtils.cs @@ -122,6 +122,9 @@ namespace mbts.ui.utility.text { public static string name(this System.Windows.Media.FontFamily font_family) => font_family.FamilyNames.Values.FirstOrDefault() ?? string.Empty; + public static string name_or_default(this System.Windows.Media.FontFamily font_family) => + font_family?.FamilyNames?.Values?.FirstOrDefault() ?? StandardFontFamilyName; + public static string display_name(this System.Windows.Media.FontFamily family, double size) => $"{family}, {size} pt"; diff --git a/src/client/spark_core/data/ticks/TickDisplaySettings.cs b/src/client/spark_core/data/ticks/TickDisplaySettings.cs new file mode 100644 index 000000000..c4e93f23b --- /dev/null +++ b/src/client/spark_core/data/ticks/TickDisplaySettings.cs @@ -0,0 +1,34 @@ +using System; + +namespace mbts.user.settings { + public class TickDisplaySettings { + public bool pad_with_leading_zeros { get; set; } = true; + public double display_multiplier { get; set; } = double.NaN; + public bool show_commas { get; set; } = false; + public string nan_text { get; set; } = "-"; + public bool show_decimal_part { get; set; } = true; + public bool show_zero_integer { get; set; } = true; + + public static TickDisplaySettings Default => + new TickDisplaySettings(); + } + + public static class TickDisplaySettingsExtensions { + public static TickDisplaySettings with( + this TickDisplaySettings settings, + bool? pad_with_leading_zeros = null, + double? display_multiplier = null, + bool? show_commas = null, + string nan_text = null, + bool? show_decimal_part = null, + bool? show_zero_integer = null) => + new TickDisplaySettings { + pad_with_leading_zeros = pad_with_leading_zeros ?? settings.pad_with_leading_zeros, + display_multiplier = display_multiplier ?? settings.display_multiplier, + show_commas = show_commas ?? settings.show_commas, + nan_text = nan_text ?? settings.nan_text, + show_decimal_part = show_decimal_part ?? settings.show_decimal_part, + show_zero_integer = show_zero_integer ?? settings.show_zero_integer + }; + } +} diff --git a/src/client/spark_core/data/ticks/TickExtensions.cs b/src/client/spark_core/data/ticks/TickExtensions.cs index ee1b625ea..ce3e23726 100644 --- a/src/client/spark_core/data/ticks/TickExtensions.cs +++ b/src/client/spark_core/data/ticks/TickExtensions.cs @@ -9,7 +9,7 @@ using static bts.utils.MathUtils; namespace mbts { public static class TickExtensions { - const string _nan_text = "-"; + public static string _nan_text => TickDisplaySettings.Default.nan_text; #region Public Extensions (is_valid, format, parse, tick_up, tick_down) public static bool is_valid(this TickFactors tick) => @@ -42,18 +42,27 @@ namespace mbts { public static string format(this TickFactors tick, double value, double display_multiplier, bool show_commas) => format(tick, value, display_multiplier, show_commas, _nan_text); - public static string format(this TickFactors tick, double value, double display_multiplier, bool show_commas, string nan_text, bool hide_decimal_part = false) { - if (double.IsNaN(value)) return nan_text; + public static string format(this TickFactors tick, double value, TickDisplaySettings settings) { + if (settings is null) + settings = TickDisplaySettings.Default; + if (double.IsNaN(value)) return settings.nan_text; if (double.IsInfinity(value)) return value.ToString(); - if (!tick.is_valid()) return _format_no_tick(value, display_multiplier, show_commas); - return _format_tick(tick, double.IsNaN(display_multiplier) ? value : value * display_multiplier, show_commas, hide_decimal_part); + if (!tick.is_valid()) return _format_no_tick(value, settings.display_multiplier, settings.show_commas); + return _format_tick(tick, value, settings); } + public static string format(this TickFactors tick, double value, double display_multiplier, bool show_commas, string nan_text, bool show_decimal_part = false) => + tick.format(value, TickDisplaySettings.Default.with( + display_multiplier: display_multiplier, + show_commas: show_commas, + nan_text: nan_text, + show_decimal_part: show_decimal_part)); + public static string format_without_decimal_part(this RegionalTickFactors regional_tick, double value) => format_without_decimal_part(value <= regional_tick.low_tick_inclusive_end ? regional_tick.low_tick : regional_tick.high_tick, value); - public static string format(this RegionalTickFactors regional_tick, double value) => - format(value <= regional_tick.low_tick_inclusive_end ? regional_tick.low_tick : regional_tick.high_tick, value); + public static string format(this RegionalTickFactors regional_tick, double value, TickDisplaySettings settings = null) => + format(value <= regional_tick.low_tick_inclusive_end ? regional_tick.low_tick : regional_tick.high_tick, value, settings); public static string format(this RegionalTickFactors regional_tick, double value, double display_multiplier) => format(value <= regional_tick.low_tick_inclusive_end ? regional_tick.low_tick : regional_tick.high_tick, value, display_multiplier); @@ -312,41 +321,64 @@ namespace mbts { return integer_part + numerator_part / tick.tick_base; } - private static string _format_tick(TickFactors tick, double value, bool show_commas, bool hide_decimal_part) { - if (value < 0) return "-" + _format_tick(tick, Math.Abs(value), show_commas, hide_decimal_part); - try { - checked { - int integer_part = (int)Math.Floor(value); - double numerator_dec = value - integer_part; - double unit_frac = tick.tick_unit / tick.tick_base; - int n_units = (int)Math.Round(numerator_dec / unit_frac); - if (n_units == tick.tick_base) { - if (hide_decimal_part) { - return integer_part.ToString(); + private static string _format_tick(TickFactors tick, double value, TickDisplaySettings settings) { + if (!settings.display_multiplier.is_nan()) + value *= settings.display_multiplier; + // Hide the sign from the 'core_impl.' If the value is negative, + // flip it before calling core_impl, then prepend a '-' to the resultant string. + bool negate = value < 0; + if (negate) + value = -value; + var text = core_impl(); + if (!settings.show_zero_integer) { + // Hide the integer if its zero. This could be better optimized, + // but speed shouldn't be an issue, so it doesn't seem worth the time/effort. + var parts = text.Split(tick.separator()); + if (parts.Length > 1 && parts[0] == "0") + text = parts[1]; + } + if (negate) + text = "-" + text; + return text; + // Formats a 'normalized' value. This method expects 'value' to be positive, + // and [already] multiplied by the display multiplier (if non-nan). + string core_impl() { + try { + checked { + int integer_part = (int)Math.Floor(value); + double numerator_dec = value - integer_part; + double unit_frac = tick.tick_unit / tick.tick_base; + int n_units = (int)Math.Round(numerator_dec / unit_frac); + if (n_units == tick.tick_base) { + if (settings.show_decimal_part) { + return (integer_part + 1).ToString() + tick.separator() + new string('0', + settings.pad_with_leading_zeros ? display_digits(tick.tick_base) : 1); + } else { + return integer_part.ToString(); + } + } + double decimal_part = n_units * tick.tick_unit; + if (decimal_part == tick.tick_base) { + ++integer_part; + decimal_part = 0; } else { - return (integer_part + 1).ToString() + tick.separator() + new string('0', display_digits(tick.tick_base)); + // some ticks are not formatted with 100% precision, using less than the required display_digits + // for such cases, we need to truncate the decimal part rather than round it. + decimal_part = Math.Floor(decimal_part); + } + string integer_part_text = settings.show_commas ? integer_part.ToString("#,0") : integer_part.ToString(); + if (settings.show_decimal_part) { + return integer_part_text + tick.separator() + decimal_part.ToString( + settings.pad_with_leading_zeros ? new string('0', display_digits(tick.tick_base)) : "0"); + } else { + return integer_part_text; } } - double decimal_part = n_units * tick.tick_unit; - if (decimal_part == tick.tick_base) { - ++integer_part; - decimal_part = 0; - } else { - // some ticks are not formatted with 100% precision, using less than the required display_digits - // for such cases, we need to truncate the decimal part rather than round it. - decimal_part = Math.Floor(decimal_part); - } - string integer_part_text = show_commas ? integer_part.ToString("#,0") : integer_part.ToString(); - if (hide_decimal_part) { - return integer_part_text; - } else { - return integer_part_text + tick.separator() + decimal_part.ToString(new string('0', display_digits(tick.tick_base))); - } } - } - catch (OverflowException ex) { - Log.Ui.warn($"Arithmetic overflow exception in format_tick (tick:{tick}, value:{value}):{ex}"); - return "Overflow"; + catch (OverflowException ex) { + Log.Ui.warn($"Arithmetic overflow exception in format_tick (tick:{tick}, value:{value}):{ex}"); + return "Overflow"; + } } } diff --git a/src/client/spark_core/data/ticks/TickUtils.cs b/src/client/spark_core/data/ticks/TickUtils.cs index 007b42d21..0315aebee 100644 --- a/src/client/spark_core/data/ticks/TickUtils.cs +++ b/src/client/spark_core/data/ticks/TickUtils.cs @@ -52,11 +52,11 @@ namespace mbts.spark { _get(spec).format_without_decimal_part(price / spec.ticksize(price)) : _get(spec).format(price); - public static string format(this ContractSpecificationImmutable spec, double price) => - _get(spec).format(price); + public static string format(this ContractSpecificationImmutable spec, double price, TickDisplaySettings settings = null) => + _get(spec).format(price, settings); - public static string format(this ContractSpecificationImmutable? spec, double price) => - spec.spec_or_default().format(price); + public static string format(this ContractSpecificationImmutable? spec, double price, TickDisplaySettings settings = null) => + spec.spec_or_default().format(price, settings); public static string format(this ContractSpecificationImmutable spec, double price, double display_multiplier, bool show_commas, string nan_text) => _get(spec).format(price, display_multiplier, show_commas, nan_text); diff --git a/src/client/spark_test/Security/TickTests.cs b/src/client/spark_test/Security/TickTests.cs new file mode 100644 index 000000000..ceeb2a280 --- /dev/null +++ b/src/client/spark_test/Security/TickTests.cs @@ -0,0 +1,114 @@ +using bts.test; +using mbts; +using mbts.user.settings; + +namespace bts.tests { + [TestGroup] + class TickTests { + [Test] + public void test_pad_with_leading_zeros() { + var settings = TickDisplaySettings.Default; + // Test with a decimal tick type: quarters + var tf = new TickFactors(100, 25); + // 25/100 + // Show leading zeros + var res = tf.format(1, settings.with(pad_with_leading_zeros: true)); + Assert.IsTrue(res == "1.00"); + // Show leading zeros (with positive tick) + res = tf.format(1.25, settings.with(pad_with_leading_zeros: true)); + Assert.IsTrue(res == "1.25"); + + // Hide leading zeros + res = tf.format(1, settings.with(pad_with_leading_zeros: false)); + Assert.IsTrue(res == "1.0"); + // Hide leading zeros (with positive tick) + res = tf.format(1.25, settings.with(pad_with_leading_zeros: false)); + Assert.IsTrue(res == "1.25"); + + // Test with a non-decimal tick type: quarters of quarters of eigths + tf = new TickFactors(3200, 25); + // Show leading zeros + res = tf.format(1, settings.with(pad_with_leading_zeros: true)); + Assert.IsTrue(res == "1-0000"); + // Show leading zeros (with positive tick) + res = tf.format(1 + 100.0 / 3200.0, settings.with(pad_with_leading_zeros: true)); + Assert.IsTrue(res == "1-0100"); + + // Hide leading zeros + res = tf.format(1, settings.with(pad_with_leading_zeros: false)); + Assert.IsTrue(res == "1-0"); + // Hide leading zeros (with positive tick) + res = tf.format(1 + 100.0 / 3200.0, settings.with(pad_with_leading_zeros: false)); + Assert.IsTrue(res == "1-100"); + } + + [Test] + public void test_show_zero_integer() { + var settings = TickDisplaySettings.Default; + + // Test with a decimal tick type: quarters + var tf = new TickFactors(100, 25); + // Show zero integer + var res = tf.format(0, settings.with(show_zero_integer: true)); + Assert.IsTrue(res == "0.00"); + // Show zero integer (with positive decimal) + res = tf.format(0.25, settings.with(show_zero_integer: true)); + Assert.IsTrue(res == "0.25"); + + // Hide zero integer + res = tf.format(0, settings.with(show_zero_integer: false)); + Assert.IsTrue(res == "00"); + // Hide zero integer (with positive decimal) + res = tf.format(0.25, settings.with(show_zero_integer: false)); + Assert.IsTrue(res == "25"); + // Hide zero integer (with positive integer, non-zero decimal). + // The 'show_zero_integer' setting should bear no effect. + res = tf.format(1.25, settings.with(show_zero_integer: false)); + Assert.IsTrue(res == "1.25"); + + // Hide zero integer (with negative decimal) + res = tf.format(-0.25, settings.with(show_zero_integer: false)); + Assert.IsTrue(res == "-25"); + // Hide zero integer (with negative integer, non-zero decimal). + // The 'show_zero_integer' setting should bear no effect. + res = tf.format(-1.25, settings.with(show_zero_integer: false)); + Assert.IsTrue(res == "-1.25"); + + // Test with a non-decimal tick type: quarters of quarters of eigths + tf = new TickFactors(3200, 25); + // Hide zero integer (with positive tick) + res = tf.format(25.0/3200.0, settings.with(show_zero_integer: false)); + Assert.IsTrue(res == "0025"); + // Hide zero integer (with positive integer, non-zero tick). + // The 'show_zero_integer' setting should bear no effect. + res = tf.format(1 + 25.0/3200.0, settings.with(show_zero_integer: false)); + Assert.IsTrue(res == "1-0025"); + + // Hide zero integer (with negative tick) + res = tf.format(-25.0/3200.0, settings.with(show_zero_integer: false)); + Assert.IsTrue(res == "-0025"); + // Hide zero integer (with negative tick, non-zero integer). + // The 'show_zero_integer' setting should bear no effect. + res = tf.format(-1 - 25.0/3200.0, settings.with(show_zero_integer: false)); + Assert.IsTrue(res == "-1-0025"); + + // Hide zero integer (with negative tick), and hide leading zeros + res = tf.format(-25.0/3200.0, settings.with(show_zero_integer: false, pad_with_leading_zeros: false)); + Assert.IsTrue(res == "-25"); + // Hide zero integer (with negative integer, non-zero tick), and hide leading zeros. + // The 'show_zero_integer' setting should bear no effect. + res = tf.format(-1 - 25.0/3200.0, settings.with(show_zero_integer: false, pad_with_leading_zeros: false)); + Assert.IsTrue(res == "-1-25"); + + // Hide zero integer (with negative tick) + res = tf.format(-25.0/3200.0, settings.with(show_zero_integer: false)); + // This isn't pretty, but it's logical: a negative number, + // greater than -1, in tick form. + Assert.IsTrue(res == "-0025"); + // Hide zero integer (with negative tick), and hide leading zeros + res = tf.format(-25.0/3200.0, settings.with(show_zero_integer: false, pad_with_leading_zeros: false)); + // Should be the same as above, except without the leading zeros in the tick part. + Assert.IsTrue(res == "-25"); + } + } +}