diff --git a/src/client/spark/Orders/OrderTypeVMs/StopLimitOrderVM.cs b/src/client/spark/Orders/OrderTypeVMs/StopLimitOrderVM.cs index e651a1393..e72f6854f 100644 --- a/src/client/spark/Orders/OrderTypeVMs/StopLimitOrderVM.cs +++ b/src/client/spark/Orders/OrderTypeVMs/StopLimitOrderVM.cs @@ -11,30 +11,24 @@ namespace mbts.spark { public StopLimitOrderVM(OrdersModel model, IObservable security_md_source, string id, string display_name, OrderTypeEditingPermissions editing_permissions) : base(model, id, display_name, security_md_source, editing_permissions) { _stop_price = new UserProperty(backing(nameof(stop_price), double.NaN)); + _stop_price.Changed += on_stop_price_changed; + concat_disposable(Disposable.Create(() => _stop_price.Changed -= on_stop_price_changed)); + void on_stop_price_changed(double oldval, double newval) => _validate(); } public override bool editable_properties_open { get => base.editable_properties_open; set { - if (value) { - if (_update_inline_props()) - _validate(); - } + if (value) + _ensure_init_stop_price(); base.editable_properties_open = value; } } - public double stop_price { - get => _stop_price.Value; - set { - _stop_price.Value = value; - _validate(); - } - } + public double stop_price { get => _stop_price.Value; set => _stop_price.Value = value; } public override void assign_to(CustomOrderDefinition value) { - _update_inline_props(); - _validate(); + _ensure_init_stop_price(); var sl = factory.stop_limit_order(); sl.StopPrice = stop_price; value.StopLimit = sl; @@ -43,24 +37,23 @@ namespace mbts.spark { protected override void load_from_internal(CustomOrderDefinitionImmutable value) { base.load_from_internal(value); // TODO: the StopLimitOrderDefinition needs relative vs absolute prices to function as a useful template (talk to Pedro) + _stop_price.bypass_setter(value.StopLimit.StopPrice); } protected override void _on_security_changed(SecurityMD? security_md) { base._on_security_changed(security_md); - if (!security_md.HasValue) { - if (double.IsNaN(stop_price)) { - // If security is null, then it is safe to default to 0.0 and clear the error - // (obviously the user isn't placing any orders with a null security!), thus removing the red error border. - // Note: algo vms in AlgoStudioWindow pass a null security_md down the pipe (1/31/2019). - stop_price = 0; - } + if (double.IsNaN(stop_price) && !_stop_price.setter_called && !security_md.HasValue) { + // If security is null, then it is safe to default to 0.0 and clear the error + // (obviously the user isn't placing any orders with a null security!), thus removing the red error border. + // Note: algo vms in AlgoStudioWindow pass a null security_md down the pipe (1/31/2019). + stop_price = 0; } } // no need to constantly poll the md, just check the inline props when the popup opens, and right before sending an order // (if the user never looks at the popup, assume that the bid_price will be used) // TODO: add settings so the trigger-price can default to something relative to the md, e.g "bid-price" or "ask-price - nticks" or "best-bid-price with a minimum size of 10" - bool _update_inline_props() { + bool _ensure_init_stop_price() { if (security_md.HasValue && !_stop_price.setter_called && security_md.Value.md.get_bbo(ref _bbo_time_ref, out NativePriceSize bid, out NativePriceSize ask)) { if (!double.IsNaN(bid.price)) { _stop_price.bypass_setter(bid.price);