diff --git a/src/client/spark/Modules/Spreader/ExecutionStrategyEditor.xaml b/src/client/spark/Modules/Spreader/ExecutionStrategyEditor.xaml index 6055d5bfe..0094ac79f 100644 --- a/src/client/spark/Modules/Spreader/ExecutionStrategyEditor.xaml +++ b/src/client/spark/Modules/Spreader/ExecutionStrategyEditor.xaml @@ -14,6 +14,7 @@ + 285 16 @@ -365,20 +366,11 @@ Grid.Column="2" VerticalAlignment="Center" Margin="6,0,0,0" Text="Ticks" IsEnabled="{TemplateBinding IsNew}" Visibility="{Binding LegTuples.Count, RelativeSource={RelativeSource TemplatedParent}, Converter={StaticResource IntNotZeroVisibilityConverter}}" /> - - - - - - - - - - + /> Pricing GetValue(TickFactorsProperty) as TickFactors; set => SetValue(TickFactorsProperty, value); } #endregion - #region AvailableTickFactors - public static readonly DependencyProperty AvailableTickFactorsProperty = DependencyProperty.Register( - nameof(AvailableTickFactors), typeof(IEnumerable), typeof(ExecutionStrategyEditor), new PropertyMetadata(new TickFactors[0])); - - public IEnumerable AvailableTickFactors { get => GetValue(AvailableTickFactorsProperty) as IEnumerable; set => SetValue(AvailableTickFactorsProperty, value); } - #endregion - #region IsNew public static readonly DependencyProperty IsNewProperty = DependencyProperty.Register( @@ -253,11 +246,11 @@ namespace mbts.ui.spark { class ExecutionStrategyErrorAdorner : NotifyDataErrorAdorner, IDisposable { readonly ChildProperty _name_text_box; - readonly ChildProperty _tick_combo_box; + readonly ChildProperty _tick_factors_picker; public ExecutionStrategyErrorAdorner(ExecutionStrategyEditor editor) : base(editor) { _name_text_box = child_property("PART_Name_TextBox"); - _tick_combo_box = child_property("PART_Tick_ComboBox"); + _tick_factors_picker = child_property("PART_TickFactorsPicker"); } protected override void OnRender(DrawingContext drawing_context) { @@ -269,7 +262,7 @@ namespace mbts.ui.spark { if (_vm != null && _vm.GetErrors(nameof(SyntheticEditorVM.name)).Cast().Any()) this.draw_border(drawing_context, _name_text_box.value, null, new Pen(Brushes.Red, 1)); if (_vm != null && _vm.GetErrors(nameof(SyntheticEditorVM.tick_factors)).Cast().Any()) - this.draw_border(drawing_context, _tick_combo_box.value, null, new Pen(Brushes.Red, 1)); + this.draw_border(drawing_context, _tick_factors_picker.value, null, new Pen(Brushes.Red, 1)); string error_text = string.Join(", ", _vm.AllErrors); this.draw_relative_text(RelativePosition.Below, error_text, drawing_context, _name_text_box.value, BTSColors.header_error_text_brush, 0, y_offset); } diff --git a/src/client/spark/Modules/Spreader/SyntheticEditorView.xaml b/src/client/spark/Modules/Spreader/SyntheticEditorView.xaml index 704959c14..a14c6b8f0 100644 --- a/src/client/spark/Modules/Spreader/SyntheticEditorView.xaml +++ b/src/client/spark/Modules/Spreader/SyntheticEditorView.xaml @@ -23,7 +23,6 @@ HorizontalAlignment="Stretch" Margin="6" SyntheticName="{Binding name}" TickFactors="{Binding tick_factors}" - AvailableTickFactors="{Binding available_tick_factors_view}" IsNew="{Binding is_new}" CanUserAddNewLeg="{Binding can_add_leg}" IsNewLegPopupOpen="{Binding is_new_leg_popup_open}" diff --git a/src/client/spark/Modules/Spreader/VM/SyntheticEditorVM.cs b/src/client/spark/Modules/Spreader/VM/SyntheticEditorVM.cs index e9996aa90..a70aeb29c 100644 --- a/src/client/spark/Modules/Spreader/VM/SyntheticEditorVM.cs +++ b/src/client/spark/Modules/Spreader/VM/SyntheticEditorVM.cs @@ -11,11 +11,9 @@ using mbts.utils; using System; using System.Collections.Generic; using System.Collections.ObjectModel; -using System.ComponentModel; using System.Linq; using System.Reactive.Linq; using System.Threading.Tasks; -using System.Windows.Data; using System.Windows.Input; using System.Windows.Media; @@ -24,7 +22,6 @@ namespace mbts.spark { /// The view model designed to serve as the DataContext for the SyntheticEditorView /// partial class SyntheticEditorVM : DisposableViewModelBase, IRequestViewClose, IRequestBindingSourceUpdate, IDisposable { - readonly ObservableCollection _available_tick_factors = new ObservableCollection(TickUtils.synthetic_presets); readonly QuantityModel _quantity_model; readonly PriceMultiplierModel _price_mult_model; readonly List _resources = new List(); @@ -42,7 +39,7 @@ namespace mbts.spark { public SyntheticEditorVM(SpreaderModel model, SecurityImmutable synthetic, ExecutionStrategyImmutable strategy, IObservable security_has_working_orders) : this(synthetic, strategy, false) { ExceptionAssert.InvalidOperation.That(synthetic.Id != 0, "This constructor may only be used for already-existing synthetic securities (id must be non-zero)."); - _tick_factors = TickUtils.get_regional_ticks(synthetic).low_tick; + _tick_factors = TickUtils.get_regional_ticks(synthetic.ContractSpec).low_tick; title = $"Edit '{synthetic.Synthetic.Name}'"; icon = Icons.ModifySynthetic; _has_working_orders = backing(nameof(has_working_orders), security_has_working_orders.OnDispatcher()); @@ -95,7 +92,7 @@ namespace mbts.spark { _set_ticks_automatically = false; _set_quantities_automatically = false; _set_price_mults_automatically = false; - _tick_factors = TickUtils.get_regional_ticks(_synthetic).low_tick; + _tick_factors = TickUtils.get_regional_ticks(_synthetic.ContractSpec).low_tick; } else { _deduce_name(); _deduce_ticks(); @@ -122,7 +119,7 @@ namespace mbts.spark { flash_error_msg("Name already taken", SparkUtils.UI.MessageFlashDuration, nameof(name)).ignore(); valid = false; } - if (_tick_factors == null || !_tick_factors.is_valid() || !_available_tick_factors.Contains(_tick_factors)) { + if (_tick_factors == null || !_tick_factors.is_valid()) { flash_error_msg("Please select ticks", SparkUtils.UI.MessageFlashDuration, nameof(tick_factors)).ignore(); valid = false; } @@ -134,6 +131,8 @@ namespace mbts.spark { _synthetic.ContractSpec.LowTick = tick_frac; _synthetic.ContractSpec.HighTick = tick_frac; _synthetic.ContractSpec.PriceBreak = double.MaxValue; + _synthetic.ContractSpec.MainFraction = (uint)MathUtils.factor_out(_tick_factors.tick_base, 10); + _synthetic.ContractSpec.PriceDisplayFormat = (uint)_tick_factors.display_digits(); uint? id = await model.create_synthetic(_synthetic); if (id.HasValue && id != 0) { if (await model.save_strategy(id.Value, get_strategy())) { @@ -153,9 +152,6 @@ namespace mbts.spark { _set_ticks_automatically = is_new; _set_quantities_automatically = is_new; _set_price_mults_automatically = is_new; - available_tick_factors_view = CollectionViewSource.GetDefaultView(_available_tick_factors); - ((ListCollectionView)available_tick_factors_view).CustomSort = TickUtils.CompareUtils.base_unit_comparer.as_non_generic(); - available_tick_factors_view.GroupDescriptions.Add(new PropertyGroupDescription(nameof(TickFactors.tick_base))); _quantity_model = new QuantityModel(!is_new); _price_mult_model = new PriceMultiplierModel(!is_new); @@ -247,9 +243,11 @@ namespace mbts.spark { public TickFactors tick_factors { get => _tick_factors; set { - _tick_factors = value; - _set_ticks_automatically = false; - RaisePropertyChanged(); + if (_tick_factors != value) { + _tick_factors = value; + _set_ticks_automatically = false; + RaisePropertyChanged(); + } } } public bool has_working_orders => _has_working_orders.Value; @@ -263,7 +261,6 @@ namespace mbts.spark { public IRaiseCanExecuteChangedCommand commit { get; } public ICommand remove_leg { get; } public ContractPickerModel contract_picker_model { get; } - public ICollectionView available_tick_factors_view { get; } public ExecutionStrategyImmutable get_strategy() { var mutable_strategy = _strategy.CloneToMutable(); @@ -330,8 +327,6 @@ namespace mbts.spark { void _deduce_ticks() { _tick_factors = TickUtils.deduce_ticks(leg_tuples.Select(l => l.leg.Security.ContractSpec)); - if (_tick_factors != null && !_available_tick_factors.Contains(_tick_factors)) - _available_tick_factors.Add(_tick_factors); RaisePropertyChanged(nameof(tick_factors)); } @@ -340,4 +335,4 @@ namespace mbts.spark { else clear_error(nameof(leg_count)); } } -} \ No newline at end of file +} diff --git a/src/client/spark/core/GlobalExtensions/TickExtensions.cs b/src/client/spark/core/GlobalExtensions/TickExtensions.cs index 5174402b7..8400fc04b 100644 --- a/src/client/spark/core/GlobalExtensions/TickExtensions.cs +++ b/src/client/spark/core/GlobalExtensions/TickExtensions.cs @@ -271,7 +271,8 @@ namespace mbts { string integer_part_text = show_commas ? integer_part.ToString("#,0") : integer_part.ToString(); return integer_part_text + tick.separator() + decimal_part.ToString(new string('0', display_digits(tick.tick_base))); } - } catch (OverflowException ex) { + } + catch (OverflowException ex) { Logger.warn(bts.log.Subsystem.Ui, $"Arithmetic overflow exception in format_tick (tick:{tick}, value:{value}):{ex}"); return "Overflow"; } @@ -279,7 +280,7 @@ namespace mbts { private static double _round_tick(double tick_unit_frac, double input) { return input - Math.IEEERemainder(input, tick_unit_frac); - } + } #endregion } } \ No newline at end of file diff --git a/src/client/spark/core/Utility/AnonymousEqualityComparer.cs b/src/client/spark/core/Utility/AnonymousEqualityComparer.cs index 99b86c440..ce4b27e89 100644 --- a/src/client/spark/core/Utility/AnonymousEqualityComparer.cs +++ b/src/client/spark/core/Utility/AnonymousEqualityComparer.cs @@ -1,4 +1,5 @@ using System; +using System.Collections; using System.Collections.Generic; namespace mbts.utils { @@ -25,6 +26,9 @@ namespace mbts.utils { public static IEqualityComparer CreateNullable(Func equals) where T : struct => new AnonymousEqualityComparer((a, b) => a.HasValue ? b.HasValue && equals(a.Value, b.Value) : !b.HasValue); + public static IEqualityComparer to_non_generic(this IEqualityComparer comparer) => + new AnonymousEqualityComparer(comparer.Equals, comparer.GetHashCode); + public static bool NullCheckEquals(T x, T y, Func non_null_equals) where T : class, IEquatable => x == null ? y == null : y != null && @@ -55,7 +59,7 @@ namespace mbts.utils { static void _accumulate_hash(ref int aggregate_hash, ref int item_hash) => aggregate_hash = aggregate_hash * _hash_multiplier + item_hash; - private class AnonymousEqualityComparer : IEqualityComparer { + private class AnonymousEqualityComparer : IEqualityComparer, IEqualityComparer { readonly Func _equals; readonly Func _get_hash_code; @@ -67,6 +71,9 @@ namespace mbts.utils { public bool Equals(T x, T y) => _equals(x, y); public int GetHashCode(T obj) => _get_hash_code(obj); + + bool IEqualityComparer.Equals(object x, object y) => x is T xt && y is T yt && _equals(xt, yt); + int IEqualityComparer.GetHashCode(object obj) => throw new NotImplementedException(); } } } \ No newline at end of file diff --git a/src/client/spark/core/Utility/CompareUtils.cs b/src/client/spark/core/Utility/CompareUtils.cs index ee081f19f..89ebe5f3e 100644 --- a/src/client/spark/core/Utility/CompareUtils.cs +++ b/src/client/spark/core/Utility/CompareUtils.cs @@ -23,9 +23,12 @@ namespace mbts.utils { #endregion #region Comparer - public static IComparer as_non_generic(this IComparer comparer) => + public static IComparer to_non_generic(this IComparer comparer) => new NonGenericComparerWrapper(comparer); + public static IComparer wrap(this IComparer inner_comparer, Func selector) => + Comparer.Create((a, b) => inner_comparer.Compare(a == null ? default : selector(a), b == null ? default : selector(b))); + class NonGenericComparerWrapper : IComparer { readonly IComparer _comparer; @@ -36,4 +39,4 @@ namespace mbts.utils { } #endregion } -} \ No newline at end of file +} diff --git a/src/client/spark/core/Utility/IValueFactoryVM.cs b/src/client/spark/core/Utility/IValueFactoryVM.cs new file mode 100644 index 000000000..68322105e --- /dev/null +++ b/src/client/spark/core/Utility/IValueFactoryVM.cs @@ -0,0 +1,6 @@ +namespace mbts.spark { + interface IValueFactoryVM { + T create(); + void load_from(T value); + } +} \ No newline at end of file diff --git a/src/client/spark/view/Controls/Order/OrderDetailsPopupControl.xaml b/src/client/spark/view/Controls/Order/OrderDetailsPopupControl.xaml index d73ba0daa..8267e375f 100644 --- a/src/client/spark/view/Controls/Order/OrderDetailsPopupControl.xaml +++ b/src/client/spark/view/Controls/Order/OrderDetailsPopupControl.xaml @@ -375,7 +375,6 @@ DataContext="{Binding strategy_editor}" SyntheticName="{Binding name}" TickFactors="{Binding tick_factors}" - AvailableTickFactors="{Binding available_tick_factors_view}" IsNew="False" IsNewLegPopupOpen="{Binding is_new_leg_popup_open}" PriceCalc="{Binding price_calc}" diff --git a/src/client/spark/view/Controls/PriceEntryControl.xaml.cs b/src/client/spark/view/Controls/PriceEntryControl.xaml.cs index 44edd0ac6..a73750489 100644 --- a/src/client/spark/view/Controls/PriceEntryControl.xaml.cs +++ b/src/client/spark/view/Controls/PriceEntryControl.xaml.cs @@ -1,10 +1,12 @@ using bts.api; using mbts.spark; +using mbts.ui.extensions; using mbts.ui.spark.converters; using System; using System.Windows; using System.Windows.Controls; using System.Windows.Input; +using static mbts.ui.utility.text.TextUtils; namespace mbts.ui.spark { /// @@ -16,7 +18,7 @@ namespace mbts.ui.spark { #region Value public static readonly DependencyProperty ValueProperty = DependencyProperty.Register("Value", typeof(double), typeof(PriceEntryControl),new FrameworkPropertyMetadata(double.NaN, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, null, CoerceValue)); - private static object CoerceValue(DependencyObject d, object baseValue) { return TickUtils.get_regional_ticks((SecurityImmutable)d.GetValue(SecurityProperty)).round((double)baseValue); } + private static object CoerceValue(DependencyObject d, object baseValue) { return TickUtils.get_regional_ticks((ContractSpecificationImmutable)d.GetValue(ContractSpecProperty)).round((double)baseValue); } public double Value { get => (double)GetValue(ValueProperty); set => SetValue(ValueProperty, value); } #endregion @@ -37,11 +39,11 @@ namespace mbts.ui.spark { public ICommand TickDownCommand { get => GetValue(TickDownCommandProperty) as ICommand; private set => SetValue(TickDownCommandPropertyKey, value); } #endregion - #region Security - public static readonly DependencyProperty SecurityProperty = DependencyProperty.Register( - nameof(Security), typeof(SecurityImmutable), typeof(PriceEntryControl), new PropertyMetadata(SecurityDataHelper.Defaults.SecurityImmutable, OnSecurityChanged)); - static void OnSecurityChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) => ((PriceEntryControl)d)._update_text_binding(); - public SecurityImmutable? Security { get => GetValue(SecurityProperty) as SecurityImmutable?; set => SetValue(SecurityProperty, value); } + #region ContractSpec + public static readonly DependencyProperty ContractSpecProperty = DependencyProperty.Register("ContractSpec", typeof(ContractSpecificationImmutable), typeof(PriceEntryControl), + new PropertyMetadata(SecurityDataHelper.Defaults.ContractSpecificationImmutablePennies, OnContractSpecChanged)); + static void OnContractSpecChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) => ((PriceEntryControl)d)._update_text_binding(); + public ContractSpecificationImmutable? ContractSpec { get => GetValue(ContractSpecProperty) as ContractSpecificationImmutable?; set => SetValue(ContractSpecProperty, value); } #endregion TextBox _text; @@ -69,8 +71,8 @@ namespace mbts.ui.spark { if (double.IsNaN(v)) { SetCurrentValue(ValueProperty, TickDefaultValue); } else { - var sec = Security; - if (sec.HasValue) SetCurrentValue(ValueProperty, sec.Value.tickup(v)); + var spec = ContractSpec; + if (spec.HasValue) SetCurrentValue(ValueProperty, spec.Value.tickup(v)); else SetCurrentValue(ValueProperty, TickDefaultValue); } } @@ -83,21 +85,21 @@ namespace mbts.ui.spark { if (double.IsNaN(v)) { SetCurrentValue(ValueProperty, TickDefaultValue); } else { - var sec = Security; - if (sec.HasValue) SetCurrentValue(ValueProperty, sec.Value.tickdown(v)); + var spec = ContractSpec; + if (spec.HasValue) SetCurrentValue(ValueProperty, spec.Value.tickdown(v)); else SetCurrentValue(ValueProperty, TickDefaultValue); } } void _update_text_binding() { if (_text != null) { - var sec = Security; - if (sec != null) { + var spec = ContractSpec; + if (spec != null) { var be = _text.GetBindingExpression(TextBox.TextProperty); if (be != null) { var converter = be.ParentBinding.Converter as TickDisplayTwoWayConverter; if (converter != null) { - converter.Security = sec; + converter.ContractSpec = spec; be.UpdateTarget(); } } @@ -105,4 +107,4 @@ namespace mbts.ui.spark { } } } -} \ No newline at end of file +} diff --git a/src/client/spark/view/Controls/TickFactorsPicker/TickFactorsPicker.xaml b/src/client/spark/view/Controls/TickFactorsPicker/TickFactorsPicker.xaml new file mode 100644 index 000000000..204ea5bb9 --- /dev/null +++ b/src/client/spark/view/Controls/TickFactorsPicker/TickFactorsPicker.xaml @@ -0,0 +1,75 @@ + + + + + + + \ No newline at end of file diff --git a/src/client/spark/view/Controls/TickFactorsPicker/TickFactorsPicker.xaml.cs b/src/client/spark/view/Controls/TickFactorsPicker/TickFactorsPicker.xaml.cs new file mode 100644 index 000000000..a9ff8fd3c --- /dev/null +++ b/src/client/spark/view/Controls/TickFactorsPicker/TickFactorsPicker.xaml.cs @@ -0,0 +1,157 @@ +using mbts.spark; +using mbts.spark.utilities; +using mbts.ui.converters; +using mbts.ui.shell; +using mbts.ui.utility; +using mbts.user.settings; +using mbts.utils; +using System; +using System.Collections.Generic; +using System.Collections.ObjectModel; +using System.ComponentModel; +using System.Linq; +using System.Windows; +using System.Windows.Controls; +using System.Windows.Data; + +namespace mbts.ui.controls { + /// + /// Interaction logic for TickPicker.xaml + /// + class TickFactorsPicker : Control { + readonly ObservableCollection _tick_unit_presets = new ObservableCollection(); + + static TickFactorsPicker() => + DefaultStyleKeyProperty.OverrideMetadata(typeof(TickFactorsPicker), new FrameworkPropertyMetadata(typeof(TickFactorsPicker))); + + public TickFactorsPicker() { + Controller = new TickFactorsController(this); + TickBasePresetsView.GroupDescriptions.Add(new PropertyGroupDescription(nameof(TickBaseVM.reduced_base_group))); + ((ListCollectionView)TickBasePresetsView).CustomSort = TickUtils.CompareUtils.base_comparer.wrap(vm => vm.value).to_non_generic(); + TickUnitPresetsView = CollectionViewSource.GetDefaultView(new ObservableAutoWrapper(_tick_unit_presets, v => new TickUnitVM(v))); + TickUnitPresetsView.GroupDescriptions.Add(new PropertyGroupDescription(nameof(TickUnitVM.reduced_unit_group))); + ((ListCollectionView)TickUnitPresetsView).CustomSort = Comparer.Default.wrap(vm => vm.value).to_non_generic(); + } + + public static IEnumerable TickBasePresets => + new [] { + 8, 16, 32, 64, 100 + }.SelectMany(i => Enumerable.Range(0, 4).Select(log10 => i * Math.Pow(10, log10))); + + public static IMultiValueConverter TickUnitVisibleTextConverter => + ValueConverter.create((double tick_base, double tick_unit) => { + int display_digits = TickExtensions.display_digits(tick_base); + return ((int)(tick_unit)).ToString(new string('0', display_digits)); + }); + + public static IValueConverter TickUnitHiddenTextConverter => + ValueConverter.create((double tick_unit) => new string(tick_unit.ToString().SkipWhile(c => c != '.').ToArray())); + + public static readonly DependencyProperty SelectedTickFactorsProperty = + WPFHelper.create_dp( + nameof(SelectedTickFactors), + FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, + (o, v) => o.Controller.load_from(v), + new TickFactors()); + + public TickFactors SelectedTickFactors { + get => GetValue(SelectedTickFactorsProperty) as TickFactors; + set => SetValue(SelectedTickFactorsProperty, value); + } + + public TickFactorsController Controller { get; } + + public ICollectionView TickBasePresetsView { get; } = CollectionViewSource.GetDefaultView(TickBasePresets.Select(v => new TickBaseVM(v)).ToList()); + public ICollectionView TickUnitPresetsView { get; } + + static IEnumerable _get_tick_unit_presets(double tick_base) { + double reduced_base = MathUtils.factor_out(tick_base, 10, out int base_log10); + return reduced_base == 1 ? + _get_dbased_tick_unit_presets(base_log10) : + _get_2based_tick_unit_presets((int)Math.Log(reduced_base, 2), base_log10); + } + + static IEnumerable _get_2based_tick_unit_presets(int log2, int log10) => + (log10 == 0 ? new[] { 1.0 } : _get_dbased_tick_unit_presets(log10)).Concat( + Enumerable.Range(1, log2 - 1).SelectMany(pow2 => { + double root = Math.Pow(2, pow2); + return Enumerable.Range(0, log10 + 1).Select(pow10 => root * Math.Pow(10, pow10)); + })); + + static IEnumerable _get_dbased_tick_unit_presets(int log10) => + Enumerable.Range(0, log10).SelectMany(_coins); + + static IEnumerable _coins(int pow10) { + double d = Math.Pow(10, pow10); + yield return d; + yield return 1.25 * d; + yield return 2.5 * d; + yield return 5.0 * d; + } + + public class TickFactorsController : ViewModelBase, IValueFactoryVM { + readonly TickFactorsPicker _view; + readonly Property _tick_base, _tick_unit; + + public TickFactorsController(TickFactorsPicker view) { + var init = (_view = view).SelectedTickFactors; + _tick_base = backing(nameof(tick_base), init.tick_base); + _tick_unit = backing(nameof(tick_unit), init.tick_unit); + } + + public double tick_base { + get => _tick_base.Value; + set { + _tick_base.set(value); + WPFHelper.set_current_if_changed(_view, SelectedTickFactorsProperty, new TickFactors(value, tick_unit)); + _view._tick_unit_presets.sync_with(_get_tick_unit_presets(value)); + } + } + public double tick_unit { + get => _tick_unit.Value; + set { + _tick_unit.set(value); + WPFHelper.set_current_if_changed(_view, SelectedTickFactorsProperty, new TickFactors(tick_base, value)); + } + } + + public TickFactors create() => new TickFactors(tick_base, tick_unit); + public void load_from(TickFactors value) { + if (value != null) { + _tick_base.set(value.tick_base); + _tick_unit.set(value.tick_unit); + } + } + } + + public class TickBaseVM : IEquatable { + public TickBaseVM(double value) => + this.value = value; + + public double value { get; } + public double reduced_base_group => MathUtils.factor_out(value, 10); + + public bool Equals(TickBaseVM other) => other != null && value == other.value; + public override bool Equals(object obj) => Equals(obj as TickBaseVM); + public override int GetHashCode() => value.GetHashCode(); + } + + public class TickUnitVM : IEquatable { + public TickUnitVM(double value) => + this.value = value; + + public double value { get; } + public double reduced_unit_group { + get { + if (!MathUtils.is_int(value)) + return 1; + else return MathUtils.factor_out(value, 10, 5); + } + } + + public bool Equals(TickUnitVM other) => other != null && value == other.value; + public override bool Equals(object obj) => Equals(obj as TickUnitVM); + public override int GetHashCode() => value.GetHashCode(); + } + } +} \ No newline at end of file diff --git a/src/client/spark/view/Converters/AnonymousValueConverter.cs b/src/client/spark/view/Converters/AnonymousValueConverter.cs index 28aab2c2c..5b7a396f5 100644 --- a/src/client/spark/view/Converters/AnonymousValueConverter.cs +++ b/src/client/spark/view/Converters/AnonymousValueConverter.cs @@ -2,8 +2,15 @@ using System.Globalization; using System.Windows.Data; -namespace mbts.ui.converters -{ +namespace mbts.ui.converters { + static class ValueConverter { + public static IValueConverter create(Func convert) => + new AnonymousValueConverter(convert); + + public static IMultiValueConverter create(Func convert) => + new AnonymousMultiValueConverter(convert); + } + public class AnonymousValueConverter : IValueConverter { public AnonymousValueConverter(Func convert) : this(convert, null) { } public AnonymousValueConverter(Func convert, Func convert_back) { diff --git a/src/client/spark/view/Converters/Numeric/DoubleTextConverter.cs b/src/client/spark/view/Converters/Numeric/DoubleTextConverter.cs index a6d100d81..15bcec2e7 100644 --- a/src/client/spark/view/Converters/Numeric/DoubleTextConverter.cs +++ b/src/client/spark/view/Converters/Numeric/DoubleTextConverter.cs @@ -13,7 +13,6 @@ namespace mbts.ui.converters Minimum = Double.NaN; Maximum = Double.NaN; Multiplier = Double.NaN; - Format = FORMAT_WITH_COMMAS; HideNaN = true; } diff --git a/src/client/spark/view/Spark/Core/TickUtils.cs b/src/client/spark/view/Spark/Core/TickUtils.cs index 709d1e3af..e24392ead 100644 --- a/src/client/spark/view/Spark/Core/TickUtils.cs +++ b/src/client/spark/view/Spark/Core/TickUtils.cs @@ -11,26 +11,35 @@ using System.Linq; namespace mbts.spark { static class TickUtils { + internal static ContractSpecificationImmutable spec_or_default(this ContractSpecificationImmutable? spec) => + spec.HasValue ? spec.Value : SecurityDataHelper.Defaults.ContractSpecificationImmutable; + + internal static ContractSpecificationImmutable spec_or_default(this SecurityImmutable security) => + security.HasContractSpec ? security.ContractSpec : SecurityDataHelper.Defaults.ContractSpecificationImmutable; + + internal static ContractSpecificationImmutable spec_or_default(this SecurityImmutable? security) => + security.HasValue && security.Value.HasContractSpec ? security.Value.ContractSpec : SecurityDataHelper.Defaults.ContractSpecificationImmutable; + public static double ticksize(this ContractSpecificationImmutable spec, double price) => Math.Abs(price) > spec.PriceBreak ? spec.HighTick : spec.LowTick; - public static string format(this SecurityImmutable security, double price) => - TickStore.get(security).format(price); + public static string format(this ContractSpecificationImmutable spec, double price) => + TickStore.get(spec).format(price); - public static string format(this SecurityImmutable? security, double price) => - security.HasValue ? security.Value.format(price) : price.ToString("0.00000"); + public static string format(this ContractSpecificationImmutable? spec, double price) => + spec.spec_or_default().format(price); - public static string format(this SecurityImmutable security, double price, double display_multiplier, bool show_commas, string nan_text) => - TickStore.get(security).format(price, display_multiplier, show_commas, nan_text); + public static string format(this ContractSpecificationImmutable spec, double price, double display_multiplier, bool show_commas, string nan_text) => + TickStore.get(spec).format(price, display_multiplier, show_commas, nan_text); - public static string format(this SecurityImmutable? security, double price, double display_multiplier, bool show_commas, string nan_text) => - TickStore.get(security).format(price, display_multiplier, show_commas, nan_text); + public static string format(this ContractSpecificationImmutable? spec, double price, double display_multiplier, bool show_commas, string nan_text) => + spec.spec_or_default().format(price, display_multiplier, show_commas, nan_text); - public static double parse(this SecurityImmutable security, string text, double display_multiplier, string nan_text) => - TickStore.get(security).parse(text, display_multiplier, nan_text); + public static double parse(this ContractSpecificationImmutable spec, string text, double display_multiplier, string nan_text) => + TickStore.get(spec).parse(text, display_multiplier, nan_text); - public static double parse(this SecurityImmutable? security, string text, double display_multiplier, string nan_text) => - TickStore.get(security).parse(text, display_multiplier, nan_text); + public static double parse(this ContractSpecificationImmutable? spec, string text, double display_multiplier, string nan_text) => + spec.spec_or_default().parse(text, display_multiplier, nan_text); public static double low_tick(NativePriceSize[] bids, NativePriceSize[] asks) { double low = bids[bids.Length - 1].price; @@ -50,14 +59,17 @@ namespace mbts.spark { return high; } - public static (int n_low, int n_high) tick_diff(SecurityImmutable security, double x, double y) => - get_regional_ticks(security).tickdiff(x, y); + public static (int n_low, int n_high) tick_diff(ContractSpecificationImmutable spec, double x, double y) => + get_regional_ticks(spec).tickdiff(x, y); - public static int normalized_tick_diff(SecurityImmutable security, double x, double y) { - var (n_low, n_high) = tick_diff(security, x, y); + public static int normalized_tick_diff(ContractSpecificationImmutable spec, double x, double y) { + var (n_low, n_high) = tick_diff(spec, x, y); return n_low + n_high; } + public static int normalized_tick_diff(SecurityImmutable security, double x, double y) => + normalized_tick_diff(security.spec_or_default(), x, y); + public static double mid_tick(ContractSpecificationImmutable spec, double bid, double ask) { double raw_mid; if (double.IsNaN(bid)) { @@ -72,36 +84,33 @@ namespace mbts.spark { return raw_mid - (raw_mid % tick_size); } - public static IReadOnlyCollection synthetic_presets => - TickStore.synthetic_presets; - public static TickFactors deduce_ticks(IEnumerable contract_specs) { - var gcd = MathUtils.GCD(contract_specs.Select(s => s.LowTick)); - if (gcd == 0) return null; - return TickStore.get_tick_factors(gcd); + var ticks = new HashSet(contract_specs.Select(TickStore.get).SelectMany(rtf => new[] { rtf.low_tick, rtf.high_tick })); + switch (ticks.Count) { + case 0: return TickStore.default_rtf.low_tick; + case 1: return ticks.First(); + default: + double tick_base_lcm = MathUtils.LCM(ticks.Select(t => (int)t.tick_base)); + double tick_unit_gcd = MathUtils.GCD(ticks.Select(t => t.tick_unit * (tick_base_lcm / t.tick_base))); + return new TickFactors(tick_base_lcm, tick_unit_gcd); + } } - public static double tickup(this SecurityImmutable security, double value) => - TickStore.get(security).tickup(value); + public static double tickup(this ContractSpecificationImmutable spec, double value) => + TickStore.get(spec).tickup(value); - public static double tickup(this SecurityImmutable security, double value, int count) => - TickStore.get(security).tickup(value, (uint)count); + public static double tickup(this ContractSpecificationImmutable spec, double value, int count) => + TickStore.get(spec).tickup(value, (uint)count); - public static double tickdown(this SecurityImmutable security, double value) => - TickStore.get(security).tickdown(value); + public static double tickdown(this ContractSpecificationImmutable spec, double value) => + TickStore.get(spec).tickdown(value); - public static double tickdown(this SecurityImmutable security, double value, int count) => - TickStore.get(security).tickdown(value, (uint)count); - - public static bool tick_equals(this SecurityImmutable security, double x, double y) => - tick_equals(security.ContractSpec, x, y); + public static double tickdown(this ContractSpecificationImmutable spec, double value, int count) => + TickStore.get(spec).tickdown(value, (uint)count); public static bool tick_equals(this ContractSpecificationImmutable contract, double x, double y) => MathUtils.RoundFractionalUnits(x, contract.LowTick) == MathUtils.RoundFractionalUnits(y, contract.LowTick); - public static IEqualityComparer tick_equality_comparer(this SecurityImmutable security) => - security.ContractSpec.tick_equality_comparer(); - public static IEqualityComparer tick_equality_comparer(this ContractSpecificationImmutable contract) => EqualityComparer.Create((double a, double b) => tick_equals(contract, a, b), x => MathUtils.RoundFractionalUnits(x, contract.LowTick)); @@ -113,110 +122,68 @@ namespace mbts.spark { return intpart + floatpart; } + public static RegionalTickFactors get_regional_ticks(ContractSpecificationImmutable spec) => + TickStore.get(spec); + public static RegionalTickFactors get_regional_ticks(SecurityImmutable security) => - TickStore.get(security); + get_regional_ticks(security.spec_or_default()); + + static TickFactors _inflate_to_base(TickFactors tick_factors, double new_base) => + new TickFactors(new_base, tick_factors.tick_unit * new_base / tick_factors.tick_base); public static class CompareUtils { + public static IComparer base_comparer => + Comparer.Create(_compare_base); + + public static IComparer unit_comparer => + Comparer.Create(_compare_unit); + public static IComparer base_unit_comparer => Comparer.Create(utils.CompareUtils.safe_comparison(compare_base_unit)); public static int compare_base_unit(TickFactors a, TickFactors b) { - int comp = a.reduced_base().CompareTo(b.reduced_base()); - if (comp != 0) - return comp; - comp = a.tick_base.CompareTo(b.tick_base); - if (comp != 0) - return comp; - return a.tick_unit.CompareTo(b.tick_unit); + int comp = _compare_base(a.tick_base, b.tick_base); + return comp == 0 ? a.tick_unit.CompareTo(b.tick_unit) : comp; + } + + static int _compare_base(double a, double b) { + double reduced_a = MathUtils.factor_out(a, 10); + double reduced_b = MathUtils.factor_out(b, 10); + int comp = reduced_a.CompareTo(reduced_b); + return comp == 0 ? a.CompareTo(b) : comp; + } + + static int _compare_unit(double a, double b) => + _compare_unit(a, b, 0, 0); + + static int _compare_unit(double a, double b, int a_mult10, int b_mult10) { + // fractional-coins are a special case, as they may not be an integer to begin with + if (!MathUtils.is_int(a)) + return _compare_unit(a * 10, b, ++a_mult10, b_mult10); + if (!MathUtils.is_int(b)) + return _compare_unit(a, b * 10, a_mult10, ++b_mult10); + double reduced_a = a_mult10 > 0 ? a : MathUtils.factor_out(a, 10, 5); + double reduced_b = b_mult10 > 0 ? b : MathUtils.factor_out(b, 10, 5); + int comp = reduced_a.CompareTo(reduced_b); + return comp == 0 ? (a * Math.Pow(10, -a_mult10)).CompareTo(b * Math.Pow(10, -b_mult10)) : comp; } } static class TickStore { - static readonly RegionalTickFactors _default_rtf = new RegionalTickFactors(new TickFactors(1000, 1), new TickFactors(1000, 1), double.MaxValue); + public static readonly RegionalTickFactors default_rtf = new RegionalTickFactors(new TickFactors(1000, 1), new TickFactors(1000, 1), double.MaxValue); static readonly ConcurrentDictionary _exchange_ticks = new ConcurrentDictionary(); - static readonly ReadOnlyDictionary _synthetic_presets_cache = new[] { - // 100 based - new { tick_unit = 0.01, tick_factors = new TickFactors(100, 1) }, // 1/100 - new { tick_unit = 0.05, tick_factors = new TickFactors(100, 5) }, // 1/20 - new { tick_unit = 0.10, tick_factors = new TickFactors(100, 10) }, // 1/10 - new { tick_unit = 0.25, tick_factors = new TickFactors(100, 25) }, // 1/4 - new { tick_unit = 0.50, tick_factors = new TickFactors(100, 50) }, // 1/2 - // 1,000 based - new { tick_unit = 0.001, tick_factors = new TickFactors(1000, 1) }, // 1/1,000 - new { tick_unit = 0.005, tick_factors = new TickFactors(1000, 5) }, // 1/200 - new { tick_unit = 0.025, tick_factors = new TickFactors(1000, 25) }, // 1/40 - // 10,000 based - new { tick_unit = 0.0001, tick_factors = new TickFactors(10000, 1) }, // 1/10,000 - new { tick_unit = 0.0005, tick_factors = new TickFactors(10000, 5) }, // 1/2,000 - new { tick_unit = 0.0025, tick_factors = new TickFactors(10000, 25) }, // 1/400 - // 64 based - new { tick_unit = 0.015625, tick_factors = new TickFactors(64, 1) }, // 1/64 - new { tick_unit = 0.03125, tick_factors = new TickFactors(64, 2) }, // 1/32 - new { tick_unit = 0.0625, tick_factors = new TickFactors(64, 4) }, // 1/16 - new { tick_unit = 0.125, tick_factors = new TickFactors(64, 8) }, // 1/8 - // 640 based - new { tick_unit = 0.0015625, tick_factors = new TickFactors(640, 1) }, // 1/640 - new { tick_unit = 0.003125, tick_factors = new TickFactors(640, 2) }, // 1/320 - new { tick_unit = 0.00625, tick_factors = new TickFactors(640, 4) }, // 1/160 - new { tick_unit = 0.0078125, tick_factors = new TickFactors(640, 5) }, // 1/128 - new { tick_unit = 0.0125, tick_factors = new TickFactors(640, 8) }, // 1/80 - new { tick_unit = 0.0390625, tick_factors = new TickFactors(640, 25) }, // 5/128 - new { tick_unit = 0.078125, tick_factors = new TickFactors(640, 50) }, // 5/64 - // 6,400 based - new { tick_unit = 0.00015625, tick_factors = new TickFactors(6400, 1) }, // 1/6,400 - new { tick_unit = 0.0003125, tick_factors = new TickFactors(6400, 2) }, // 1/3,200 - new { tick_unit = 0.000625, tick_factors = new TickFactors(6400, 4) }, // 1/1,600 - new { tick_unit = 0.00078125, tick_factors = new TickFactors(6400, 5) }, // 1/1,280 - new { tick_unit = 0.00125, tick_factors = new TickFactors(6400, 8) }, // 1/800 - new { tick_unit = 0.00390625, tick_factors = new TickFactors(6400, 25) }, // 1/256 - // 64,000 based - new { tick_unit = 0.000015625, tick_factors = new TickFactors(64000, 1) }, // 1/6,400 - new { tick_unit = 0.00003125, tick_factors = new TickFactors(64000, 2) }, // 1/3,200 - new { tick_unit = 0.0000625, tick_factors = new TickFactors(64000, 4) }, // 1/1,600 - new { tick_unit = 0.000078125, tick_factors = new TickFactors(64000, 5) }, // 1/12,800 - new { tick_unit = 0.000125, tick_factors = new TickFactors(64000, 8) }, // 1/8,000 - new { tick_unit = 0.000390625, tick_factors = new TickFactors(64000, 25) }, // 1/2,560 - new { tick_unit = 0.001953125, tick_factors = new TickFactors(64000, 125) },// 1/512 - }.ToReadOnlyDictionary(obj => obj.tick_unit, obj => obj.tick_factors, EqualityComparer.Create(MathUtils.epsilon_equals)); - static readonly double[] _descending_synthetic_tick_sizes; + public static RegionalTickFactors get(ContractSpecification spec) => + _exchange_ticks.GetOrAdd(new SpecKey(spec), _create_rtf); - static TickStore() => - _descending_synthetic_tick_sizes = _synthetic_presets_cache.Select(kv => kv.Key).Ordered(new ReverseComparer(Comparer.Default)).ToArray(); + public static RegionalTickFactors get(ContractSpecificationImmutable spec) => + _exchange_ticks.GetOrAdd(new SpecKey(spec), _create_rtf); - public static IReadOnlyCollection synthetic_presets => _synthetic_presets_cache.Values; - - public static RegionalTickFactors get(SecurityImmutable security) { - if (security.HasContractSpec) { - if (security.ContractCase == Security.ContractOneofCase.Synthetic) { - return get(security.ContractSpec.LowTick); - } else if (security.HasGlbx) { - return _exchange_ticks.GetOrAdd(new SpecKey(security.ContractSpec, security.Glbx), _create_rtf); - } - } - return _default_rtf; - } + public static RegionalTickFactors get(SecurityImmutable security) => + security.HasContractSpec ? get(security.ContractSpec) : default_rtf; public static RegionalTickFactors get(SecurityImmutable? security) => - security.HasValue ? get(security.Value) : _default_rtf; - - public static RegionalTickFactors get(double tick_size) { - var tf = get_tick_factors(tick_size); - return new RegionalTickFactors(tf, tf, double.MaxValue); - } - - public static TickFactors get_tick_factors(double tick_size) { - if (_synthetic_presets_cache.TryGetValue(tick_size, out TickFactors result)) - return result; - // try to return the largest known tick that is divisible by this tick - foreach (var tfu in _descending_synthetic_tick_sizes) { - double mod = tick_size % tfu; - if (MathUtils.epsilon_equals(mod, 0)) { - return _synthetic_presets_cache[tfu]; - } - } - return new TickFactors(1 / tick_size, tick_size); - } + security.HasValue ? get(security.Value) : default_rtf; static RegionalTickFactors _create_rtf(SpecKey key) { double tick_base = key.denominator; @@ -231,7 +198,7 @@ namespace mbts.spark { var low_tf = _try_create_tf(low_tick, reduced_target_base, display_digits_override); var high_tf = _try_create_tf(high_tick, reduced_target_base, display_digits_override); if (low_tf == null) { - low_tf = high_tf ?? (high_tf = _default_rtf.low_tick); + low_tf = high_tf ?? (high_tf = default_rtf.low_tick); } else if (high_tf == null) high_tf = low_tf; else { @@ -290,7 +257,7 @@ namespace mbts.spark { static TickFactors _create_tf(double tick_size) { if (!_check_tick_size(tick_size)) - return _default_rtf.low_tick; + return default_rtf.low_tick; double matched_unit = double.MinValue; TickFactors matched_tf = null; foreach (var kv in _exchange_ticks) { @@ -315,19 +282,22 @@ namespace mbts.spark { static bool _check_tick_size(double tick_size) => tick_size > 0 && tick_size <= 0.5; - static TickFactors _inflate_to_base(TickFactors tick_factors, double new_base) => - new TickFactors(new_base, tick_factors.tick_unit * new_base / tick_factors.tick_base); - struct SpecKey : IEquatable { public readonly uint denominator, display_digits; public readonly double low_tick, high_tick, price_break; - public SpecKey(ContractSpecificationImmutable spec, GLBXExchangeDataImmutable glbx_data) { - denominator = glbx_data.MainFraction; - display_digits = glbx_data.PriceDisplayFormat; - low_tick = spec.LowTick; - high_tick = spec.HighTick; - price_break = spec.PriceBreak; + public SpecKey(ContractSpecification spec) + : this(spec.MainFraction, spec.PriceDisplayFormat, spec.LowTick, spec.HighTick, spec.PriceBreak) { } + + public SpecKey(ContractSpecificationImmutable spec) + : this(spec.MainFraction, spec.PriceDisplayFormat, spec.LowTick, spec.HighTick, spec.PriceBreak) { } + + public SpecKey(uint denominator, uint display_digits, double low_tick, double high_tick, double price_break) { + this.denominator = denominator; + this.display_digits = display_digits; + this.low_tick = low_tick; + this.high_tick = high_tick; + this.price_break = price_break; } public bool Equals(SpecKey other) => @@ -345,4 +315,4 @@ namespace mbts.spark { } } } -} \ No newline at end of file +} diff --git a/src/client/spark/view/Spark/Utilities/MathUtils.cs b/src/client/spark/view/Spark/Utilities/MathUtils.cs index 45dd61e02..1f66f612f 100644 --- a/src/client/spark/view/Spark/Utilities/MathUtils.cs +++ b/src/client/spark/view/Spark/Utilities/MathUtils.cs @@ -61,22 +61,29 @@ namespace mbts.spark.utilities { if (value > max) return max; return value; } - public static bool is_within_range(double min, double max, double value) => - value >= min && value <= max; - public static int RoundFractionalUnits(double value, double fractional_unit) => (int)Math.Round(value / fractional_unit); public static bool is_int(double d) => - epsilon_equals(d, (int)d); + epsilon_equals(d, Math.Round(d)); - public static double factor_out(double value, double factor) => - factor_out(value, factor, out int _); + public static bool is_int(double d, out double with_int_value) { + with_int_value = Math.Round(d); + return epsilon_equals(with_int_value, d); + } + + public static double factor_out(double value, params double[] factors) { + foreach (double f in factors) + value = factor_out(value, f, out int _); + return value; + } public static double factor_out(double value, double factor, out int n_factors_removed) { n_factors_removed = 0; + if (value == 0) + return value; double quotient = value / factor; - while (is_int(quotient)) { + while (is_int(quotient, out quotient)) { value = quotient; ++n_factors_removed; quotient /= factor; @@ -84,4 +91,4 @@ namespace mbts.spark.utilities { return value; } } -} \ No newline at end of file +} diff --git a/src/client/spark/view/Spark/Utilities/SecurityDataHelper.cs b/src/client/spark/view/Spark/Utilities/SecurityDataHelper.cs index b8bfb5d6e..e5a51422a 100644 --- a/src/client/spark/view/Spark/Utilities/SecurityDataHelper.cs +++ b/src/client/spark/view/Spark/Utilities/SecurityDataHelper.cs @@ -1,10 +1,8 @@ using bts.api; using System.Collections.Generic; -namespace mbts.spark -{ - static class SecurityDataHelper - { +namespace mbts.spark { + static class SecurityDataHelper { public static class EqualityComparer { public static readonly IEqualityComparer MDComparer = utils.EqualityComparer.Create(Equals, GetHashCode); public static bool Equals(SecurityMD x, SecurityMD y) => x.security.Id == y.security.Id; @@ -14,16 +12,35 @@ namespace mbts.spark } public static class Defaults { - public static ContractSpecification ContractSpecification => new ContractSpecification { CashPerPoint = 1.0, LowTick = 0.01, HighTick = 0.01, PriceBreak = double.MaxValue, PriceMultiplier = 1.0 }; - public static GLBXExchangeData GlbxExchangeData => new GLBXExchangeData { MainFraction = 1000, PriceDisplayFormat = 3 }; + public static ContractSpecification ContractSpecification => + new ContractSpecification { + CashPerPoint = 1.0, + LowTick = 0.0001, + HighTick = 0.0001, + PriceBreak = double.MaxValue, + PriceMultiplier = 1.0, + MainFraction = 10000 + }; public static readonly ContractSpecificationImmutable ContractSpecificationImmutable = new ContractSpecificationImmutable(ContractSpecification); - public static Security Security => new Security { - Symbol = string.Empty, - Exchange = Venue.Bts, - ContractSpec = ContractSpecification, - Glbx = GlbxExchangeData - }; + + public static ContractSpecification ContractSpecificationPennies => + new ContractSpecification { + CashPerPoint = 1.0, + LowTick = 0.01, + HighTick = 0.01, + PriceBreak = double.MaxValue, + PriceMultiplier = 1.0, + MainFraction = 100 + }; + public static ContractSpecificationImmutable ContractSpecificationImmutablePennies = new ContractSpecificationImmutable(ContractSpecificationPennies); + + public static Security Security => + new Security { + Symbol = string.Empty, + Exchange = Venue.Bts, + ContractSpec = ContractSpecification + }; public static readonly SecurityImmutable SecurityImmutable = new SecurityImmutable(Security); } } -} \ No newline at end of file +} diff --git a/src/client/spark/view/Utility/WPFHelper.cs b/src/client/spark/view/Utility/WPFHelper.cs index c6e2bfd3e..4a510361a 100644 --- a/src/client/spark/view/Utility/WPFHelper.cs +++ b/src/client/spark/view/Utility/WPFHelper.cs @@ -212,6 +212,15 @@ namespace mbts.ui { d.SetValue(property, new_value); } + public static void set_current_if_changed(DependencyObject d, DependencyProperty property, T new_value) => + set_current_if_changed(d, property, new_value, EqualityComparer.Default); + + public static void set_current_if_changed(DependencyObject d, DependencyProperty property, T new_value, IEqualityComparer comparer) { + object curr = d.GetValue(property); + if (!comparer.to_non_generic().Equals(d.GetValue(property), new_value)) + d.SetCurrentValue(property, new_value); + } + public static PropertyChangedCallback prop_changed_callback(PropertyChangedCallback callback) => (d, e) => callback(ObjectHelper.try_convert(d), ObjectHelper.try_convert(e.OldValue), ObjectHelper.try_convert(e.NewValue)); diff --git a/src/lib/domain_model/Security.proto b/src/lib/domain_model/Security.proto index 18f6662ff..b2f8fd4bf 100644 --- a/src/lib/domain_model/Security.proto +++ b/src/lib/domain_model/Security.proto @@ -55,9 +55,9 @@ message GLBXExchangeData { string security_desc = 20; string cfi_code = 21; string cme_symbol = 22; - uint32 main_fraction = 30; - uint32 sub_fraction = 31; - uint32 price_display_format = 32; + uint32 do_not_use_main_fraction = 30; + uint32 do_not_use_sub_fraction = 31; + uint32 do_not_use_price_display_format = 32; uint32 market_segment_id = 33; } message CBOEExchangeData { @@ -117,6 +117,10 @@ message ContractSpecification { double high_tick = 30; double cash_per_point = 40; double price_multiplier = 50; + + uint32 main_fraction = 60; + uint32 sub_fraction = 61; + uint32 price_display_format = 62; } message Security { diff --git a/src/lib/md/Diag/ShmDumper.cpp b/src/lib/md/Diag/ShmDumper.cpp index 47da5f1eb..a2ca9b62f 100644 --- a/src/lib/md/Diag/ShmDumper.cpp +++ b/src/lib/md/Diag/ShmDumper.cpp @@ -72,9 +72,9 @@ string dump_security(const FixedSecurity& security) { res << "'display_factor': " << (int)security.display_factor << ", "; res << "'numeric_id': " << security.numeric_id << ", "; res << "'cboe_class_key': " << security.cboe_class_key << ", "; - res << "'cme_main_frac': " << (int)security.cme_main_fraction << ", "; - res << "'cme_sub_fract': " << (int)security.cme_sub_fraction << ", "; - res << "'cme_price_dformat': " << (int)security.cme_price_display_format << ", "; + res << "'main_frac': " << (int)security.cme_main_fraction << ", "; + res << "'sub_fract': " << (int)security.cme_sub_fraction << ", "; + res << "'price_format': " << (int)security.cme_price_display_format << ", "; res << "'market_segment': " << security.cme_market_segment_id << ", "; diff --git a/src/lib/md/SecurityDB.cpp b/src/lib/md/SecurityDB.cpp index dcb04b6de..a2e740313 100644 --- a/src/lib/md/SecurityDB.cpp +++ b/src/lib/md/SecurityDB.cpp @@ -72,7 +72,19 @@ size_t load_securities( if (symbols.find(us) != symbols.end()) { auto data = sqlite3_column_blob(select, 4); auto len = sqlite3_column_bytes(select, 4); + // Migrate from old tick stuff if (security.ParseFromArray(data, len)) { + if (security.has_glbx()) { + if (security.contract_spec().main_fraction() == 0) { + security.mutable_contract_spec()->set_main_fraction(security.glbx().do_not_use_main_fraction()); + } + if (security.contract_spec().sub_fraction() == 0) { + security.mutable_contract_spec()->set_sub_fraction(security.glbx().do_not_use_sub_fraction()); + } + if (security.contract_spec().price_display_format() == 0) { + security.mutable_contract_spec()->set_price_display_format(security.glbx().do_not_use_price_display_format()); + } + } bts_assert(security_key == security.id()); count++; callback(security); diff --git a/src/lib/md/Shm/FixedSecurity.cpp b/src/lib/md/Shm/FixedSecurity.cpp index 4221112fe..4b527f1f1 100644 --- a/src/lib/md/Shm/FixedSecurity.cpp +++ b/src/lib/md/Shm/FixedSecurity.cpp @@ -140,6 +140,11 @@ void get_contract_spec(const FixedSecurity& source, api::ContractSpecification& spec.set_price_break(spec.price_break() * spec.price_multiplier()); spec.set_low_tick(spec.low_tick() * spec.price_multiplier()); spec.set_high_tick(spec.high_tick() * spec.price_multiplier()); + + // Tick info + spec.set_main_fraction(source.cme_main_fraction); + spec.set_sub_fraction(source.cme_sub_fraction); + spec.set_price_display_format(source.cme_price_display_format); } Security convert_security(const FixedSecurity& security) { @@ -153,7 +158,7 @@ void convert_security(const FixedSecurity& source, Security* presult) { dest.set_id(source.key); dest.set_exchange(source.exchange); dest.set_symbol(source.symbol()); - + switch(source.exchange) { case Venue::VENUE_GLBX: dest.mutable_glbx()->set_exchange_id(source.numeric_id); @@ -161,9 +166,6 @@ void convert_security(const FixedSecurity& source, Security* presult) { dest.mutable_glbx()->set_cfi_code(source.cfi_code()); dest.mutable_glbx()->set_cme_symbol(source.alt_symbol()); if (source.cme_market_segment_id != 0) dest.mutable_glbx()->set_market_segment_id(source.cme_market_segment_id); - if (source.cme_main_fraction != 0) dest.mutable_glbx()->set_main_fraction(source.cme_main_fraction); - if (source.cme_sub_fraction != 0) dest.mutable_glbx()->set_sub_fraction(source.cme_sub_fraction); - if (source.cme_price_display_format) dest.mutable_glbx()->set_price_display_format(source.cme_price_display_format); break; case Venue::VENUE_BTS: break; @@ -262,9 +264,6 @@ void convert_security(const Security& src, FixedSecurity& dest, bool set_securit dest.cfi_code(src.glbx().cfi_code()); dest.alt_symbol(src.glbx().cme_symbol()); dest.cme_market_segment_id = src.glbx().market_segment_id(); - dest.cme_main_fraction = src.glbx().main_fraction(); - dest.cme_sub_fraction = src.glbx().sub_fraction(); - dest.cme_price_display_format = src.glbx().price_display_format(); break; case Security::ExchangeDataCase::kCboe: dest.cboe_class_key = src.cboe().class_key(); @@ -279,6 +278,9 @@ void convert_security(const Security& src, FixedSecurity& dest, bool set_securit dest.low_tick = src.contract_spec().low_tick() / src.contract_spec().price_multiplier(); dest.high_tick = src.contract_spec().high_tick() / src.contract_spec().price_multiplier(); dest.cash_per_point = src.contract_spec().cash_per_point(); + dest.cme_main_fraction = src.contract_spec().main_fraction(); + dest.cme_sub_fraction = src.contract_spec().sub_fraction(); + dest.cme_price_display_format = src.contract_spec().price_display_format(); switch(securitytype_of(src)) { case SECURITYTYPE_FUTURE: {