diff --git a/src/client/spark/Orders/OrderTypeVMs/TriggerOrderVM.cs b/src/client/spark/Orders/OrderTypeVMs/TriggerOrderVM.cs index 8b6063a6a..328d9cd40 100644 --- a/src/client/spark/Orders/OrderTypeVMs/TriggerOrderVM.cs +++ b/src/client/spark/Orders/OrderTypeVMs/TriggerOrderVM.cs @@ -11,72 +11,97 @@ using System.Linq; using System.Reactive.Linq; using static bts.api.orderman.CustomOrderDefinition; -namespace mbts.spark -{ - static partial class OrderTypeVMFactory - { - class InlineTriggerOrderProperties - { - public InlineTriggerOrderProperties (CustomOneofCase selected_child_order_type, IReadOnlyCollection child_order_types) - { +namespace mbts.spark { + static partial class OrderTypeVMFactory { + class InlineTriggerOrderProperties { + public readonly InlineUserProperty selected_child_order_type; + public readonly InlineUserProperty> child_order_types; + public readonly InlineUserProperty trigger_price_type; + public readonly InlineUserProperty payup_price_type; + public readonly InlineUserProperty fixed_price; + public readonly InlineUserProperty payup_ticks; + public readonly InlineUserProperty use_fixed_price, is_priceout_checked, is_timeout_checked; + public readonly InlineUserProperty timeout_secs; + + public InlineTriggerOrderProperties(CustomOneofCase selected_child_order_type, IReadOnlyCollection child_order_types) { ExceptionAssert.InvalidOperation.That(child_order_types.Contains(selected_child_order_type), $"Child order type {selected_child_order_type} is not in child_order_types"); this.selected_child_order_type = new InlineUserProperty(selected_child_order_type, true); this.child_order_types = new InlineUserProperty>(child_order_types, true); - trigger_price_type = new InlineUserProperty(TriggerPriceType.Bid, false); - order_price = new InlineUserProperty(double.NaN, true); + payup_price_type = new InlineUserProperty(); + use_fixed_price = new InlineUserProperty(false); + fixed_price = new InlineUserProperty(); + payup_ticks = new InlineUserProperty(); + is_priceout_checked = new InlineUserProperty(); + is_timeout_checked = new InlineUserProperty(); + timeout_secs = new InlineUserProperty(10); } - public readonly InlineUserProperty selected_child_order_type; - public readonly InlineUserProperty> child_order_types; - - public readonly InlineUserProperty trigger_price_type; - public readonly InlineUserProperty order_price; } - class TriggerOrderVM : OrderTypeVMBase, IHierarchicalOrderTypeVM, IDisposable - { + class TriggerOrderVM : OrderTypeVMBase, IHierarchicalOrderTypeVM, IDisposable { readonly IObservable _security_md_source; - SecurityImmutable? _security; + readonly UserProperty _payup_price_type; + readonly UserProperty _trigger_price_type; + readonly UserProperty _fixed_price; + readonly UserProperty _payup_ticks, _timeout_secs; + readonly UserProperty _use_fixed_price, _is_timeout_checked, _is_priceout_checked; + SecurityMD? _security_md; + SecurityImmutable? _security => _security_md.HasValue ? _security_md.Value.security : null; ObservableCollection _order_types = new ObservableCollection(); ObservableAutoWrapper _child_order_choices; InlineTriggerOrderProperties _inline_props; IOrderTypeVM _child_order; IDisposable _dispose, _child_order_errors_sub; - - public TriggerPriceType trigger_price_type { get; set; } = TriggerPriceType.Bid; - public double order_price { get; set; } = double.NaN; + ulong _bbo_ref = 0, _last_trade_ref = 0; + double _best_bid = double.NaN, _best_ask = double.NaN, _last_trade_price = double.NaN; public TriggerOrderVM(OrdersModel model, IObservable security_md_source) : this(model, security_md_source, security_md_source.Select(smd => new InlineTriggerOrderProperties(model.factory.default_order_type(smd.security), model.factory.default_order_types(smd.security).ToList()))) { } - public TriggerOrderVM(OrdersModel model, IObservable security_md_source, - IObservable inline_properties) : base(model) - { + public TriggerOrderVM(OrdersModel model, IObservable security_md_source, IObservable inline_properties) : base(model) { + _trigger_price_type = user_backing(nameof(trigger_price_type), TriggerPriceType.Bid); + _use_fixed_price = user_backing(nameof(use_fixed_price), true); + _fixed_price = user_backing(nameof(fixed_price), double.NaN); + _payup_price_type = user_backing(nameof(payup_price_type), TriggerPayupPrice.Types.TriggerPayupPriceType.Trigger); + _payup_ticks = user_backing(nameof(payup_ticks), 0); + _is_priceout_checked = user_backing(nameof(is_priceout_checked), false); + _is_timeout_checked = user_backing(nameof(is_timeout_checked), false); + _timeout_secs = user_backing(nameof(timeout_secs), 0); _security_md_source = security_md_source; _dispose = Disposable.Create( security_md_source.OnDispatcher() - .Subscribe(smd => _security = smd.security), + .Subscribe(smd => { + _security_md = smd; + _refresh_md(); + if (double.IsNaN(fixed_price) || !_fixed_price.setter_called) { + switch (trigger_price_type) { + case TriggerPriceType.Bid: _fixed_price.bypass_setter(_best_bid); break; + case TriggerPriceType.Ask: _fixed_price.bypass_setter(_best_ask); break; + case TriggerPriceType.LastTradePrice: _fixed_price.bypass_setter(_last_trade_price); break; + } + } + }), inline_properties.OnDispatcher() .Subscribe(p => { - if (_order_types.Count == 0 || p.child_order_types.force_override) - { + if (_order_types.Count == 0 || p.child_order_types.force_override) { _order_types.SetItems(p.child_order_types.value); } - if (_child_order_choices != null && (_child_order == null || p.selected_child_order_type.force_override)) - { + if (_child_order_choices != null && (_child_order == null || p.selected_child_order_type.force_override)) { _select_child_order(p.selected_child_order_type.value); - } - if (p.trigger_price_type.force_override) - { - trigger_price_type = p.trigger_price_type.value; - RaisePropertyChanged(nameof(trigger_price_type)); } - if (p.order_price.force_override) - { - order_price = p.order_price.value; - RaisePropertyChanged(nameof(order_price)); - } + // trigger price + _trigger_price_type.on_inline_prop(p.trigger_price_type); + // fixed price + _use_fixed_price.on_inline_prop(p.use_fixed_price); + _fixed_price.on_inline_prop(p.fixed_price); + // payup + _payup_price_type.on_inline_prop(p.payup_price_type); + _payup_ticks.on_inline_prop(p.payup_ticks); + // exit + _is_priceout_checked.on_inline_prop(p.is_priceout_checked); + _is_timeout_checked.on_inline_prop(p.is_timeout_checked); + _timeout_secs.on_inline_prop(p.timeout_secs); _inline_props = p; _validate(); }) @@ -84,65 +109,39 @@ namespace mbts.spark _validate(); } - public IEnumerable trigger_price_types => EnumHelper.get_values_except(TriggerPriceType.LastTradePrice); - - public override void assign_to(CustomOrderDefinition value) - { - if (_child_order == null) - _select_child_order(model.factory.default_order_type(_security.Value, true)); - _validate(); - if (!HasErrors) - { - var i = factory.trigger_order(); - i.TriggerCondition = new TriggerCondition() - { - PriceType = trigger_price_type, - AbsolutePriceTrigger = new AbsolutePriceTrigger() { TriggerQuantity = new TriggerQuantity() } - }; - i.TriggerAction = new TriggerAction() - { - Price = order_price, - SecurityId = _security.Value.Id - }; - child_order.assign_to(i.Child); - value.Trigger = i; - } - } - - public double price_tick - { - get { return _security.Value.ContractSpec.LowTick; } - } - - public ContractSpecificationImmutable spec - { - get { return _security.Value.ContractSpec; } - } - - public ICollection child_order_types - { - get - { - if (_child_order_choices == null) + // trigger price + public IEnumerable trigger_price_types => EnumHelper.get_values_except(TriggerPriceType.LastTradePrice); + public TriggerPriceType trigger_price_type { get => _trigger_price_type.Value; set => _trigger_price_type.Value = value; } + // fixed price + public bool use_fixed_price { get => _use_fixed_price.Value; set => _use_fixed_price.Value = value; } + public double fixed_price { get => _fixed_price.Value; set => _fixed_price.Value = value; } + // payup + public IEnumerable payup_price_types => EnumHelper.get_values(); + public TriggerPayupPrice.Types.TriggerPayupPriceType payup_price_type { get => _payup_price_type.Value; set => _payup_price_type.Value = value; } + public int payup_ticks { get => _payup_ticks.Value; set => _payup_ticks.Value = value; } + // exit + public bool is_priceout_checked { get => _is_priceout_checked.Value; set => _is_priceout_checked.Value = value; } + public bool is_timeout_checked { get => _is_timeout_checked.Value; set => _is_timeout_checked.Value = value; } + public int timeout_secs { get => _timeout_secs.Value; set => _timeout_secs.Value = value; } + // + public ContractSpecificationImmutable spec => _security.spec_or_default(); + public ICollection child_order_types { + get { + if (_child_order_choices == null) _child_order_choices = new ObservableAutoWrapper(_order_types, ot => create(ot, model, _security_md_source)); return _child_order_choices; } } - - public IOrderTypeVM child_order - { - get - { + public IOrderTypeVM child_order { + get { init(); return _child_order; } - set - { + set { Disposable.dispose(ref _child_order_errors_sub); _child_order = value; _child_order?.init(); - if (_child_order is INotifyDataErrorInfo ndei) - { + if (_child_order is INotifyDataErrorInfo ndei) { ndei.ErrorsChanged += on_errors_changed; void on_errors_changed(object s, DataErrorsChangedEventArgs e) => _validate(); @@ -151,40 +150,80 @@ namespace mbts.spark _validate(); RaisePropertyChanged(nameof(child_order)); } - } + } - public void Dispose() - { + public override void assign_to(CustomOrderDefinition value) { + if (_child_order == null) + _select_child_order(model.factory.default_order_type(_security.Value, true)); + _validate(); + if (!HasErrors) { + var i = factory.trigger_order(); + i.TriggerAction = new TriggerAction { + SecurityId = _security.Value.Id, + }; + i.TriggerCondition = new TriggerCondition() { + PriceType = trigger_price_type, + AbsolutePriceTrigger = new AbsolutePriceTrigger() { TriggerQuantity = new TriggerQuantity() }, + }; + i.ExitCondition = new TriggerExitCondition { + ConditionalCancel = is_priceout_checked, + TimeoutEnabled = is_timeout_checked, + TimeoutSeconds = (uint)(timeout_secs) + }; + if (use_fixed_price) { + // should already be null, setting it here for clarity + i.TriggerAction.Payup = null; + i.TriggerAction.Price = fixed_price; + } else { + i.TriggerAction.Price = double.NaN; + i.TriggerAction.Payup = new TriggerPayupPrice { + PayupTicks = payup_ticks, + PayupPriceType = payup_price_type + }; + } + child_order.assign_to(i.Child); + value.Trigger = i; + } + } + + public void Dispose() { Disposable.dispose(ref _dispose); if (_child_order_choices != null) _child_order_choices.OfType().DisposeAll(); } - public override void init() - { + public override void init() { if (_child_order == null && _security.HasValue) _select_child_order(); _validate(); } - void _select_child_order() - { + void _refresh_md() { + if (_security.HasValue) { + _security_md.Value.md.get_bbo(ref _bbo_ref, out co2.NativePriceSize bid, out co2.NativePriceSize ask); + _best_bid = bid.price; + _best_ask = ask.price; + _security_md.Value.md.get_last_trade(ref _last_trade_ref, out co2.NativePriceSize ps, out DateTime ts, out ulong v); + } + } + + void _select_child_order() { CustomOneofCase ot = _inline_props != null && _inline_props.selected_child_order_type.has_value ? _inline_props.selected_child_order_type.value : model.factory.default_order_type(_security.Value, true); _select_child_order(ot); } - void _select_child_order(CustomOneofCase ot) - { + void _select_child_order(CustomOneofCase ot) { _child_order = child_order_types.FirstOrDefault(vm => vm.order_type == ot); if (_child_order == null) _child_order = child_order_types.FirstOrDefault(); RaisePropertyChanged(nameof(child_order)); } - void _validate() - { + void _validate() { + assert(!use_fixed_price || !double.IsNaN(fixed_price), nameof(fixed_price)); + assert(!is_timeout_checked || timeout_secs > 0, nameof(timeout_secs)); if (_child_order == null || (_child_order is INotifyDataErrorInfo ndei && ndei.HasErrors)) set_error(nameof(child_order), string.Empty, true); else clear_error(nameof(child_order)); diff --git a/src/client/spark/core/Utility/ViewModelBase.cs b/src/client/spark/core/Utility/ViewModelBase.cs index a6da9680f..cee1235bf 100644 --- a/src/client/spark/core/Utility/ViewModelBase.cs +++ b/src/client/spark/core/Utility/ViewModelBase.cs @@ -281,6 +281,16 @@ namespace mbts.ui.shell /// Sets 'setter_called' to false. /// public void reset() => setter_called = false; + + public void on_inline_prop(InlineUserProperty inline_prop) { + if (inline_prop.force_override || (inline_prop.has_value && !setter_called)) + bypass_setter(inline_prop.value); + } + + public void on_inline_prop(InlineUserProperty inline_prop, Func select) { + if (inline_prop.force_override || (inline_prop.has_value && !setter_called)) + bypass_setter(select(inline_prop.value)); + } } #endregion @@ -440,6 +450,13 @@ namespace mbts.ui.shell return prop; } + protected UserProperty user_backing(string name, T default_value = default) => + new UserProperty(backing(name, default_value)); + + protected UserProperty user_backing(Property property) => + new UserProperty(property); + + // TODO: maybe include a revert_on_error flag? protected class PropertyValidation { public readonly Func validate; diff --git a/src/client/spark/view/Controls/Order/OrderDetailsPopupControl.xaml b/src/client/spark/view/Controls/Order/OrderDetailsPopupControl.xaml index 0fb815cbe..7cbc07c8e 100644 --- a/src/client/spark/view/Controls/Order/OrderDetailsPopupControl.xaml +++ b/src/client/spark/view/Controls/Order/OrderDetailsPopupControl.xaml @@ -1,8 +1,11 @@ - + @@ -13,6 +16,8 @@ + 4 + 4 +