From 4e3bdad812196cc8ecd52a3f6b2fdfd9cd40f016 Mon Sep 17 00:00:00 2001 From: John Colvin Date: Thu, 4 Apr 2019 17:21:47 -0500 Subject: [PATCH] QuantityMeasureControl - change QuantityMeasure prop to nullable QuantityMeasureImmutable --- .../AlgoParamTemplateSelector.cs | 2 +- .../spark/Orders/OrderTypeVMs/JoinOrderVM.cs | 4 +-- .../spark/Orders/OrderTypeVMs/LeanOrderVM.cs | 4 +-- .../Orders/OrderTypeVMs/OrderTypeVMFactory.cs | 20 +++++------ .../spark/Orders/OrderTypeVMs/TickOrderVM.cs | 4 +-- .../spark/Orders/OrderTypeVMs/TopOrderVM.cs | 4 +-- .../Order/QuantityMeasureControl.xaml.cs | 33 ++++++++++++------- .../Converters/AnonymousValueConverter.cs | 14 ++++---- 8 files changed, 46 insertions(+), 39 deletions(-) diff --git a/src/client/spark/Modules/Trading/AlgoStudio/View/AlgoEditorGridView/AlgoParamTemplateSelector.cs b/src/client/spark/Modules/Trading/AlgoStudio/View/AlgoEditorGridView/AlgoParamTemplateSelector.cs index 63967f2e0..4fdac9a20 100644 --- a/src/client/spark/Modules/Trading/AlgoStudio/View/AlgoEditorGridView/AlgoParamTemplateSelector.cs +++ b/src/client/spark/Modules/Trading/AlgoStudio/View/AlgoEditorGridView/AlgoParamTemplateSelector.cs @@ -35,7 +35,7 @@ namespace mbts.ui.spark { return StringTemplate; if (pvm is IAlgoParamVM) return OrderDefTemplate; - if (pvm is IAlgoParamVM) + if (pvm is IAlgoParamVM) return QuantityMeasureTemplate; if (pvm is IAlgoParamVM) return ExecutionStrategyTemplate; diff --git a/src/client/spark/Orders/OrderTypeVMs/JoinOrderVM.cs b/src/client/spark/Orders/OrderTypeVMs/JoinOrderVM.cs index 96740ecd1..150de6405 100644 --- a/src/client/spark/Orders/OrderTypeVMs/JoinOrderVM.cs +++ b/src/client/spark/Orders/OrderTypeVMs/JoinOrderVM.cs @@ -5,13 +5,13 @@ using JoinDefi = bts.api.orderman.JoinOrderDefinitionImmutable; namespace mbts.spark { static partial class OrderTypeVMFactory { class JoinOrderVM : OrderTypeVMBase { - readonly Property _join_threshold; + readonly Property _join_threshold; public JoinOrderVM(AlgoKey key, string display_name, AlgoEditingPermissions editing_permissions) : base(key, display_name, editing_permissions) => _join_threshold = add_property(nameof(join_threshold), JoinDef.JoinThresholdFieldNumber, new QuantityMeasureImmutable(new QuantityMeasure { Ratio = 1.0, Absolute = 1 })); - public QuantityMeasureImmutable join_threshold { get => _join_threshold.Value; set => _join_threshold.set(value); } + public QuantityMeasureImmutable? join_threshold { get => _join_threshold.Value; set => _join_threshold.set(value); } } } } \ No newline at end of file diff --git a/src/client/spark/Orders/OrderTypeVMs/LeanOrderVM.cs b/src/client/spark/Orders/OrderTypeVMs/LeanOrderVM.cs index 94e61ae46..c5819b540 100644 --- a/src/client/spark/Orders/OrderTypeVMs/LeanOrderVM.cs +++ b/src/client/spark/Orders/OrderTypeVMs/LeanOrderVM.cs @@ -7,7 +7,7 @@ using LeanDefi = bts.api.orderman.LeanOrderDefinitionImmutable; namespace mbts.spark { static partial class OrderTypeVMFactory { class LeanOrderVM : OrderTypeVMBase { - readonly Property _cross_threshold; + readonly Property _cross_threshold; readonly Property _max_chase_ticks; readonly Property _trade_toward_behavior; @@ -22,7 +22,7 @@ namespace mbts.spark { public LeanDef.Types.OnToward trade_toward_behavior { get => _trade_toward_behavior.Value; set => _trade_toward_behavior.set(value); } public IEnumerable trade_toward_types => EnumHelper.get_values_except(); // auxiliary - public QuantityMeasureImmutable cross_threshold { get => _cross_threshold.Value; set => _cross_threshold.set(value); } + public QuantityMeasureImmutable? cross_threshold { get => _cross_threshold.Value; set => _cross_threshold.set(value); } public uint max_chase_ticks { get => _max_chase_ticks.Value; set => _max_chase_ticks.set(value); } } } diff --git a/src/client/spark/Orders/OrderTypeVMs/OrderTypeVMFactory.cs b/src/client/spark/Orders/OrderTypeVMs/OrderTypeVMFactory.cs index bb328dba3..362cba548 100644 --- a/src/client/spark/Orders/OrderTypeVMs/OrderTypeVMFactory.cs +++ b/src/client/spark/Orders/OrderTypeVMs/OrderTypeVMFactory.cs @@ -368,22 +368,18 @@ namespace mbts.spark { return prop; } - protected Property add_property(string prop_name, int target_field_number, TImmutableValue init_value = default, AlgoParamPermissions permissions = AlgoParamPermissions.ReadWrite) where TMutableValue : class, IMessage, new() where TImmutableValue : IImmutableMessage { + protected Property add_property(string prop_name, int target_field_number, TImmutableValue? init_value = null, AlgoParamPermissions permissions = AlgoParamPermissions.ReadWrite) + where TMutableValue : class, IMessage, new() + where TImmutableValue : struct, IImmutableMessage { var prop = backing(prop_name, init_value); var field_desc = new TMutable().Descriptor.FindFieldByNumber(target_field_number); - var param = new AlgoParamVM(prop_name, DisplayNameHelper.snake_to_title_case(prop_name), init_value) { permissions = permissions }; - _link_props(param, prop); + var param = new AlgoParamVM(prop_name, DisplayNameHelper.snake_to_title_case(prop_name), init_value) { permissions = permissions }; _algo_params.Add(param); - add_property( + _auto_props.Add(new AutoProp( prop_name, - mdef => field_desc.Accessor.GetValue(mdef) as TMutableValue, - (mdef, mval) => field_desc.Accessor.SetValue(mdef, mval), - mval => prop.set(ProtoHelper.immutable_factory(mval ?? new TMutableValue())), - () => { - // TODO: perhaps CloneToMutable() should be a member of IImmutableMessage. - // this method makes things so simple for the caller...but this dynamic cast is a little sketch. - return ((dynamic)prop.Value).CloneToMutable(); - }); + algo => field_desc.Accessor.SetValue(algo, prop.Value.HasValue ? ((dynamic)prop.Value.Value).CloneToMutable() : null), + algo => prop.set(field_desc.Accessor.GetValue(algo) is TMutableValue mval ? ProtoHelper.immutable_factory(mval) : (TImmutableValue?)null), + _link_props(param, prop))); return prop; } diff --git a/src/client/spark/Orders/OrderTypeVMs/TickOrderVM.cs b/src/client/spark/Orders/OrderTypeVMs/TickOrderVM.cs index c66e7aeab..302ea84a5 100644 --- a/src/client/spark/Orders/OrderTypeVMs/TickOrderVM.cs +++ b/src/client/spark/Orders/OrderTypeVMs/TickOrderVM.cs @@ -6,7 +6,7 @@ namespace mbts.spark { static partial class OrderTypeVMFactory { class TickOrderVM : OrderTypeVMBase { readonly Property _tick_offset_on_deploy, _cross_ticks, _max_chase_ticks; - readonly Property _cross_threshold; + readonly Property _cross_threshold; readonly Property _detach_on_toward; public TickOrderVM(AlgoKey key, string display_name, AlgoEditingPermissions editing_permissions) @@ -18,7 +18,7 @@ namespace mbts.spark { _detach_on_toward = add_property(nameof(detach_on_toward), TickDef.DetachOnTowardFieldNumber, true); } - public QuantityMeasureImmutable cross_threshold { get => _cross_threshold.Value; set => _cross_threshold.set(value); } + public QuantityMeasureImmutable? cross_threshold { get => _cross_threshold.Value; set => _cross_threshold.set(value); } public uint cross_ticks { get => _cross_ticks.Value; set => _cross_ticks.set(value); } public uint tick_offset_on_deploy { get => _tick_offset_on_deploy.Value; set => _tick_offset_on_deploy.set(value); } public uint max_chase_ticks { get => _max_chase_ticks.Value; set => _max_chase_ticks.set(value); } diff --git a/src/client/spark/Orders/OrderTypeVMs/TopOrderVM.cs b/src/client/spark/Orders/OrderTypeVMs/TopOrderVM.cs index 398bbf282..e5e9545c7 100644 --- a/src/client/spark/Orders/OrderTypeVMs/TopOrderVM.cs +++ b/src/client/spark/Orders/OrderTypeVMs/TopOrderVM.cs @@ -6,7 +6,7 @@ namespace mbts.spark { static partial class OrderTypeVMFactory { class TopOrderVM : OrderTypeVMBase { readonly Property _watch_level_tick_offset; - readonly Property _top_threshold; + readonly Property _top_threshold; public TopOrderVM(AlgoKey key, string display_name, AlgoEditingPermissions editing_permissions) : base(key, display_name, editing_permissions) { @@ -15,7 +15,7 @@ namespace mbts.spark { } public uint watch_level_tick_offset { get => _watch_level_tick_offset.Value; set => _watch_level_tick_offset.set(value); } - public QuantityMeasureImmutable top_threshold { get => _top_threshold.Value; set => _top_threshold.set(value); } + public QuantityMeasureImmutable? top_threshold { get => _top_threshold.Value; set => _top_threshold.set(value); } } } } \ No newline at end of file diff --git a/src/client/spark/view/Controls/Order/QuantityMeasureControl.xaml.cs b/src/client/spark/view/Controls/Order/QuantityMeasureControl.xaml.cs index f0e231461..4a10af218 100644 --- a/src/client/spark/view/Controls/Order/QuantityMeasureControl.xaml.cs +++ b/src/client/spark/view/Controls/Order/QuantityMeasureControl.xaml.cs @@ -36,7 +36,7 @@ namespace mbts.ui.spark.algo { FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, (o, oldval, newval) => { o._update_gate.try_update(() => { - var mutable = o.QuantityMeasure.CloneToMutable(); + var mutable = o.QuantityMeasure?.CloneToMutable() ?? new bts.spark.QuantityMeasure(); switch (oldval) { case bts.spark.QuantityMeasure.QuantityOneofCase.Absolute: o._cached_absolute = mutable.Absolute; break; case bts.spark.QuantityMeasure.QuantityOneofCase.Ratio: o._cached_ratio = mutable.Ratio; break; @@ -70,7 +70,7 @@ namespace mbts.ui.spark.algo { FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, (o, v) => { o._update_gate.try_update(() => { - var mutable = o.QuantityMeasure.CloneToMutable(); + var mutable = o.QuantityMeasure?.CloneToMutable() ?? new QuantityMeasure(); mutable.Absolute = v; o.SetCurrentValue(QuantityMeasureProperty, new QuantityMeasureImmutable(mutable)); o.SetCurrentValue(QuantityCaseProperty, bts.spark.QuantityMeasure.QuantityOneofCase.Absolute); @@ -90,7 +90,7 @@ namespace mbts.ui.spark.algo { FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, (o, v) => { o._update_gate.try_update(() => { - var mutable = o.QuantityMeasure.CloneToMutable(); + var mutable = o.QuantityMeasure?.CloneToMutable() ?? new QuantityMeasure(); mutable.Ratio = v; o.SetCurrentValue(QuantityMeasureProperty, new QuantityMeasureImmutable(mutable)); o.SetCurrentValue(QuantityCaseProperty, bts.spark.QuantityMeasure.QuantityOneofCase.Ratio); @@ -103,23 +103,32 @@ namespace mbts.ui.spark.algo { #endregion #region QuantityMeasure - public static readonly DependencyProperty QuantityMeasureProperty = WPFHelper.create_dp( + public static readonly DependencyProperty QuantityMeasureProperty = WPFHelper.create_dp( nameof(QuantityMeasure), FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, (o, oldval, newval) => o._update_gate.try_update(() => { - switch (oldval.QuantityCase) { - case bts.spark.QuantityMeasure.QuantityOneofCase.Absolute: o._cached_absolute = oldval.Absolute; break; - case bts.spark.QuantityMeasure.QuantityOneofCase.Ratio: o._cached_ratio = oldval.Ratio; break; + if (oldval.HasValue) { + switch (oldval.Value.QuantityCase) { + case bts.spark.QuantityMeasure.QuantityOneofCase.Absolute: o._cached_absolute = oldval.Value.Absolute; break; + case bts.spark.QuantityMeasure.QuantityOneofCase.Ratio: o._cached_ratio = oldval.Value.Ratio; break; + } } - switch (newval.QuantityCase) { - case bts.spark.QuantityMeasure.QuantityOneofCase.Absolute: o.SetCurrentValue(AbsoluteProperty, newval.Absolute); break; - case bts.spark.QuantityMeasure.QuantityOneofCase.Ratio: o.SetCurrentValue(RatioProperty, newval.Ratio); break; + if (newval.HasValue) { + switch (newval.Value.QuantityCase) { + case bts.spark.QuantityMeasure.QuantityOneofCase.Absolute: o.SetCurrentValue(AbsoluteProperty, newval.Value.Absolute); break; + case bts.spark.QuantityMeasure.QuantityOneofCase.Ratio: o.SetCurrentValue(RatioProperty, newval.Value.Ratio); break; + } + o.SetCurrentValue(QuantityCaseProperty, newval.Value.QuantityCase); + } else { + o.SetCurrentValue(QuantityCaseProperty, bts.spark.QuantityMeasure.QuantityOneofCase.None); } - o.SetCurrentValue(QuantityCaseProperty, newval.QuantityCase); }), _default_quantity_measure); - public QuantityMeasureImmutable QuantityMeasure { get => (QuantityMeasureImmutable)GetValue(QuantityMeasureProperty); set => SetValue(QuantityMeasureProperty, value); } + public QuantityMeasureImmutable? QuantityMeasure { + get => GetValue(QuantityMeasureProperty) as QuantityMeasureImmutable?; + set => SetValue(QuantityMeasureProperty, value); + } #endregion } } \ 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 eecc1ca38..46ce179c7 100644 --- a/src/client/spark/view/Converters/AnonymousValueConverter.cs +++ b/src/client/spark/view/Converters/AnonymousValueConverter.cs @@ -23,15 +23,17 @@ namespace mbts.ui.converters { DoubleTextConverter = new DoubleTextConverter(), IntTextConverter = new IntTextConverter(), UIntTextConverter = new UIntTextConverter(), - QuantityMeasureTextConverter = ValueConverter.create(fmt), + QuantityMeasureTextConverter = ValueConverter.create(fmt), SimpleString = new AnonymousValueConverter(obj => obj?.ToString()); - public static string fmt(QuantityMeasureImmutable qm) { - switch (qm.QuantityCase) { - case QuantityMeasure.QuantityOneofCase.Absolute: return qm.Absolute.ToString(); - case QuantityMeasure.QuantityOneofCase.Ratio: return qm.Ratio.ToString("#0.00#"); - default: return "N/A"; + public static string fmt(QuantityMeasureImmutable? qm) { + if (qm.HasValue) { + switch (qm.Value.QuantityCase) { + case QuantityMeasure.QuantityOneofCase.Absolute: return qm.Value.Absolute.ToString(); + case QuantityMeasure.QuantityOneofCase.Ratio: return qm.Value.Ratio.ToString("#0.00#"); + } } + return "N/A"; } }