From 4a7674eb29f6f9975d5deaa92a116c675cecd3e4 Mon Sep 17 00:00:00 2001 From: John Colvin Date: Fri, 22 May 2020 18:15:53 -0500 Subject: [PATCH] synthetic gui supports integer tick sizes --- .../Spreader/ExecutionStrategyEditor.xaml | 44 +++++++++++++----- .../Spreader/ExecutionStrategyEditor.xaml.cs | 20 +++++++++ .../Modules/Spreader/SyntheticEditorView.xaml | 2 + .../Modules/Spreader/VM/SyntheticEditorVM.cs | 45 ++++++++++++++++--- .../core/GlobalExtensions/DoubleHelper.cs | 15 +++++++ .../Order/AlgoContentTemplateSelector.xaml | 2 + .../BooleanToVisibilityConverter.cs | 3 ++ src/client/spark/view/Spark/Core/TickUtils.cs | 22 +++++++++ 8 files changed, 138 insertions(+), 15 deletions(-) diff --git a/src/client/spark/Modules/Spreader/ExecutionStrategyEditor.xaml b/src/client/spark/Modules/Spreader/ExecutionStrategyEditor.xaml index b45f13e04..07cae21ef 100644 --- a/src/client/spark/Modules/Spreader/ExecutionStrategyEditor.xaml +++ b/src/client/spark/Modules/Spreader/ExecutionStrategyEditor.xaml @@ -549,24 +549,48 @@ - - + + + fractional + + ) + + + > + + + GetValue(TickFactorsProperty) as TickFactors; set => SetValue(TickFactorsProperty, value); } #endregion + #region UseFractionalTicks + public static readonly DependencyProperty UseFractionalTicksProperty = WPFHelper.create_dp( + nameof(UseFractionalTicks), FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, true); + + public bool UseFractionalTicks { + get => (bool)GetValue(UseFractionalTicksProperty); + set => SetValue(UseFractionalTicksProperty, value); + } + #endregion + + #region IntegerTickSize + public static readonly DependencyProperty IntegerTickSizeProperty = WPFHelper.create_dp( + nameof(IntegerTickSize), FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, 1); + + public int IntegerTickSize { + get => (int)GetValue(IntegerTickSizeProperty); + set => SetValue(IntegerTickSizeProperty, value); + } + #endregion + #region IsNew public static readonly DependencyProperty IsNewProperty = DependencyProperty.Register( diff --git a/src/client/spark/Modules/Spreader/SyntheticEditorView.xaml b/src/client/spark/Modules/Spreader/SyntheticEditorView.xaml index 2bb8c16cf..910121358 100644 --- a/src/client/spark/Modules/Spreader/SyntheticEditorView.xaml +++ b/src/client/spark/Modules/Spreader/SyntheticEditorView.xaml @@ -36,6 +36,8 @@ ContractPickerModel="{Binding contract_picker_model}" HedgePricing="{Binding hedge_pricing}" HedgePricingItemsSource="{Binding hedge_pricing_options}" + UseFractionalTicks="{Binding use_fractional_ticks}" + IntegerTickSize="{Binding integer_tick_size}" /> diff --git a/src/client/spark/Modules/Spreader/VM/SyntheticEditorVM.cs b/src/client/spark/Modules/Spreader/VM/SyntheticEditorVM.cs index a99d8043f..ae1709683 100644 --- a/src/client/spark/Modules/Spreader/VM/SyntheticEditorVM.cs +++ b/src/client/spark/Modules/Spreader/VM/SyntheticEditorVM.cs @@ -2,7 +2,6 @@ using bts.collections; using bts.shell; using bts.spark; -using bts.utils; using mbts.ui; using mbts.ui.shell; using mbts.ui.spark; @@ -30,8 +29,16 @@ namespace mbts.spark { readonly Property _has_working_orders, _can_add_leg; Security _synthetic; - bool _set_name_automatically, _set_ticks_automatically, _set_quantities_automatically, _set_price_mults_automatically, _is_setting_quantities, _is_setting_price_mults; + bool + _set_name_automatically, + _set_ticks_automatically, + _set_quantities_automatically, + _set_price_mults_automatically, + _is_setting_quantities, + _is_setting_price_mults, + _use_fractional_ticks = true; TickFactors _tick_factors; + int _integer_tick_size = 1; ExecutionStrategyImmutable _strategy; HedgePricing _hedge_pricing; @@ -44,6 +51,10 @@ namespace mbts.spark { : 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.ContractSpec).low_tick; + if (TickUtils.is_integer_tick(synthetic.ContractSpec)) { + _use_fractional_ticks = false; + _integer_tick_size = DoubleHelper.to_optional_int32(synthetic.ContractSpec.LowTick) ?? 0; + } title = $"Edit '{synthetic.Synthetic.Name}'"; icon = Icons.ModifySynthetic; _has_working_orders = backing(nameof(has_working_orders), security_has_working_orders.OnDispatcher()); @@ -131,14 +142,14 @@ 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()) { - flash_error_msg("Please select ticks", SparkUtils.UI.MessageFlashDuration, nameof(tick_factors)).ignore(); + if (use_fractional_ticks ? (_tick_factors == null || !_tick_factors.is_valid()) : integer_tick_size == 0) { + flash_error_msg("Please select ticks", SparkUtils.UI.MessageFlashDuration, use_fractional_ticks ? nameof(tick_factors) : nameof(integer_tick_size)).ignore(); valid = false; } if (valid) { _synthetic.Synthetic.Legs.SetItems(leg_tuples.Select(l => l.leg)); _synthetic.Symbol = _deduce_symbol(_synthetic); - _synthetic.ContractSpec = _tick_factors.to_spec(); + _synthetic.ContractSpec = use_fractional_ticks ? _tick_factors.to_spec() : TickUtils.create_integer_tick_spec(integer_tick_size); uint? id = await model.create_synthetic(new SecurityImmutable(_synthetic)); if (id.HasValue && id != 0) { if (await model.save_strategy(id.Value, get_strategy())) { @@ -295,6 +306,20 @@ namespace mbts.spark { public bool has_working_orders => _has_working_orders.Value; public bool is_new { get; } public bool is_new_leg_popup_open { get; set; } + public bool use_fractional_ticks { + get => _use_fractional_ticks; + set { + if (Set(ref _use_fractional_ticks, value)) + _set_ticks_automatically = false; + } + } + public int integer_tick_size { + get => _integer_tick_size; + set { + if (Set(ref _integer_tick_size, value)) + _set_ticks_automatically = false; + } + } public bool can_add_leg => _can_add_leg.Value; public SyntheticContract.Types.PriceCalc price_calc { get => _synthetic.Synthetic.PriceCalc; set => _synthetic.Synthetic.PriceCalc = value; } public ObservableCollection leg_tuples { get; } = new ObservableCollection(); @@ -323,6 +348,16 @@ namespace mbts.spark { _strategy = strategy; hedge_pricing = strategy.HedgePricing; leg_tuples.SetItems(_get_legs(_synthetic, strategy)); + if (synthetic.HasContractSpec) { + /// assign the fields and , + /// rather than the properties and + /// because the setters of the former flip to false if the value changes. + /// This behavior causes the ticks to be automatically deduced as legs are added/removed, + /// until the user explicitly enters a different value (as the properties are bound to tick input controls in the view). + Set(ref _use_fractional_ticks, !TickUtils.is_integer_tick(synthetic.ContractSpec), nameof(use_fractional_ticks)); + if (!use_fractional_ticks) + Set(ref _integer_tick_size, DoubleHelper.to_optional_int32(synthetic.ContractSpec.LowTick) ?? 0, nameof(integer_tick_size)); + } RaisePropertyChanged(nameof(name), nameof(tick_factors)); } diff --git a/src/client/spark/core/GlobalExtensions/DoubleHelper.cs b/src/client/spark/core/GlobalExtensions/DoubleHelper.cs index f4473b794..1f2b54713 100644 --- a/src/client/spark/core/GlobalExtensions/DoubleHelper.cs +++ b/src/client/spark/core/GlobalExtensions/DoubleHelper.cs @@ -63,5 +63,20 @@ namespace mbts { public static double? nan_nullable(double value) => double.IsNaN(value) ? (double?)null : value; + + public static int? to_optional_int32(double value) { + if (double.IsNaN(value) || double.IsInfinity(value)) + return null; + if (value > 0) { + value = Math.Floor(value); + if (value <= int.MaxValue) + return (int)value; + } else if (value < 0) { + value = Math.Ceiling(value); + if (value >= int.MinValue) + return (int)value; + } + return null; + } } } diff --git a/src/client/spark/view/Controls/Order/AlgoContentTemplateSelector.xaml b/src/client/spark/view/Controls/Order/AlgoContentTemplateSelector.xaml index 1a522bcce..914cc7097 100644 --- a/src/client/spark/view/Controls/Order/AlgoContentTemplateSelector.xaml +++ b/src/client/spark/view/Controls/Order/AlgoContentTemplateSelector.xaml @@ -1005,6 +1005,8 @@ ContractPickerModel="{Binding contract_picker_model}" HedgePricing="{Binding hedge_pricing}" HedgePricingItemsSource="{Binding hedge_pricing_options}" + UseFractionalTicks="{Binding use_fractional_ticks}" + IntegerTickSize="{Binding integer_tick_size}" /> diff --git a/src/client/spark/view/Converters/BooleanToVisibilityConverter.cs b/src/client/spark/view/Converters/BooleanToVisibilityConverter.cs index c42d1e0f6..6a4a95c15 100644 --- a/src/client/spark/view/Converters/BooleanToVisibilityConverter.cs +++ b/src/client/spark/view/Converters/BooleanToVisibilityConverter.cs @@ -12,6 +12,9 @@ namespace mbts.ui.converters public Visibility VisibilityForFalse { get { return _visfalse; } set { _visfalse = value; } } public static BooleanToVisibilityConverter Instance => new BooleanToVisibilityConverter(); + public static BooleanToVisibilityConverter HiddenInstance => new BooleanToVisibilityConverter { + VisibilityForFalse = Visibility.Hidden + }; public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { bool flag = false; diff --git a/src/client/spark/view/Spark/Core/TickUtils.cs b/src/client/spark/view/Spark/Core/TickUtils.cs index b420ea7b4..af1c1b184 100644 --- a/src/client/spark/view/Spark/Core/TickUtils.cs +++ b/src/client/spark/view/Spark/Core/TickUtils.cs @@ -49,6 +49,28 @@ namespace mbts.spark { 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 bool is_integer_tick(ContractSpecificationImmutable spec) => + spec.MainFraction == 0 && + spec.PriceDisplayFormat == 0 && + spec.LowTick > 0 && + MathUtils.is_int(spec.LowTick) && + spec.HighTick > 0 && + MathUtils.is_int(spec.HighTick); + + public static ContractSpecification create_integer_tick_spec(int low_tick) => + create_integer_tick_spec(low_tick, low_tick); + + public static ContractSpecification create_integer_tick_spec(int low_tick, int high_tick) { + var rv = SecurityDataHelper.Defaults.ContractSpecification; + rv.MainFraction = 0; + rv.SubFraction = 0; + rv.PriceDisplayFormat = 0; + rv.PriceBreak = 0; + rv.LowTick = low_tick; + rv.HighTick = high_tick; + return rv; + } + public static double low_tick(NativePriceSize[] bids, NativePriceSize[] asks) { double low = bids[bids.Length - 1].price; if (double.IsNaN(low))