SPARK-700 disallow changes to synth if working orders exist

This commit is contained in:
John Colvin committed 2018-03-20 16:22:56 -04:00
1 parent e3190bade5
commit b76db3b06e
6 files changed
+79 -18

No files matched your search

@@ -15,13 +15,13 @@ using System.Threading.Tasks;
namespace bts.shell {
[Export(typeof(IInitializable))]
partial class ShowSyntheticEditorHandler : IInitializable, IDisposable {
readonly SpreaderModel _model;
readonly OrdersModel _model;
readonly IBus _bus;
IDisposable _dispose;
[ImportingConstructor]
public ShowSyntheticEditorHandler(IBus bus, SpreaderModel model) {
public ShowSyntheticEditorHandler(IBus bus, OrdersModel model) {
_bus = bus;
_model = model;
}
@@ -56,8 +56,9 @@ namespace bts.shell {
}
async Task _show_synthetic_strategy_async(SecurityImmutable verified_synthetic) {
var lookup = await _model.lookup_strategy_async(verified_synthetic);
if (lookup.success) _show_synthetic_editor(new SyntheticEditorVM(_model, verified_synthetic, lookup.result));
var lookup = await _model.spreader.lookup_strategy_async(verified_synthetic);
if (lookup.success)
_show_synthetic_editor(new SyntheticEditorVM(_model.spreader, verified_synthetic, lookup.result, _model.security_has_working_orders(verified_synthetic.Id)));
}
void _show_synthetic_create(IEnumerable<ContractLeg> legs) {
@@ -67,7 +68,7 @@ namespace bts.shell {
Symbol = legs.FirstOrDefault()?.Security.Symbol ?? string.Empty,
ContractSpec = SecurityDataHelper.Defaults.ContractSpecification
});
_show_synthetic_editor(new SyntheticEditorVM(_bus, _model, sec, new ExecutionStrategyImmutable(SyntheticDataHelper.Defaults.ExecutionStrategy(sec))));
_show_synthetic_editor(new SyntheticEditorVM(_bus, _model.spreader, sec, new ExecutionStrategyImmutable(SyntheticDataHelper.Defaults.ExecutionStrategy(sec))));
}
#region IInitializable
@@ -32,10 +32,10 @@ namespace mbts.spark {
() => new SecurityCriteria(_vm.Current.selected_securities), () => true
), SparkCommand.create("Remove", Icons.RemoveSynthetic, SparkTags.SyntheticDeleteTag).complete(
_vm.Observable.SelectMany(vm => vm.current_row_observable),
r => r != null && r.n_working_orders == 0, r => r == null ? 0 : r.security_key
can_row_change, r => r == null ? 0 : r.security_key
), SparkCommand.create("Change", Icons.ModifySynthetic, SparkTags.ShowSyntheticStrategyTag).complete(
_vm.Observable.SelectMany(vm => vm.current_security_observable),
s => s.HasValue, ObjectHelper.ValueOrDefault
_vm.Observable.SelectMany(vm => vm.current_row_observable),
can_row_change, r => r == null ? SecurityDataHelper.Defaults.SecurityImmutable : r.security
), SparkCommands.ShowLadder.complete(
_vm.Observable.SelectMany(vm => vm.current_security_md), md => md.HasValue, ObjectHelper.ValueOrDefault
), SparkCommands.ShowOrders.complete(() => new OrderBookWindowFactory.OrderBookStartupData {
@@ -44,6 +44,9 @@ namespace mbts.spark {
}
})
};
bool can_row_change(SyntheticManagementDialogVM.Row r) =>
r != null && r.n_working_orders == 0;
}
public void initialize(StartupData state) {
@@ -12,6 +12,7 @@ using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Reactive.Linq;
using System.Windows.Data;
using System.Windows.Input;
using System.Windows.Media;
@@ -20,10 +21,12 @@ namespace mbts.spark {
/// <summary>
/// The view model designed to serve as the DataContext for the SyntheticEditorView
/// </summary>
partial class SyntheticEditorVM : ViewModelBaseWithValidation, IRequestViewClose, IRequestBindingSourceUpdate, IDisposable {
partial class SyntheticEditorVM : DisposableViewModelBase, IRequestViewClose, IRequestBindingSourceUpdate, IDisposable {
readonly ObservableCollection<TickFactors> _available_tick_factors = new ObservableCollection<TickFactors>(TickUtils.all_present_ticks());
readonly QuantityModel _quantity_model;
readonly PriceMultiplierModel _price_mult_model;
readonly List<IDisposable> _resources = new List<IDisposable>();
readonly Property<bool> _has_working_orders;
Security _synthetic;
bool _set_name_automatically, _set_ticks_automatically, _set_quantities_automatically, _set_price_mults_automatically, _is_setting_quantities, _is_setting_price_mults;
TickFactors _tick_factors;
@@ -34,16 +37,24 @@ namespace mbts.spark {
/// </summary>
/// <param name="synthetic">The already-existing synthetic security who's strategy may be modified</param>
/// <param name="strategy">The already-existing strategy that may be modified</param>
public SyntheticEditorVM(SpreaderModel model, SecurityImmutable synthetic, ExecutionStrategyImmutable strategy)
public SyntheticEditorVM(SpreaderModel model, SecurityImmutable synthetic, ExecutionStrategyImmutable strategy, IObservable<bool> 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.ContractSpec).low_tick;
title = $"Edit '{synthetic.Synthetic.Name}'";
icon = Icons.ModifySynthetic;
commit = new DelegateCommand(async () => {
_has_working_orders = backing(nameof(has_working_orders), security_has_working_orders.ObserveOnDispatcher());
var commit = new ObservantCommand(async () => {
await model.save_strategy(_synthetic.Id, get_strategy());
RequestClose?.Invoke(this, new EventArgs());
});
_resources.Add(security_has_working_orders.Select(v => !v).ObserveOnDispatcher().Subscribe(commit));
_resources.Add(_has_working_orders.ObserveOnDispatcher().Subscribe(vals => {
if (vals.newval)
set_error(nameof(has_working_orders), "Security has working orders");
else clear_error(nameof(has_working_orders));
}));
this.commit = commit;
}
/// <summary>
/// Initializes a new instance of SyntheticEditorVM class. This constructor may only be used for new synthetic securities (id must be zero).
@@ -210,6 +221,7 @@ namespace mbts.spark {
RaisePropertyChanged();
}
}
public bool has_working_orders => _has_working_orders.Value;
public bool is_new { get; }
public bool is_new_leg_popup_open { get; set; }
public SyntheticContract.Types.PriceCalc price_calc { get => _synthetic.Synthetic.PriceCalc; set => _synthetic.Synthetic.PriceCalc = value; }
@@ -221,11 +233,6 @@ namespace mbts.spark {
public ContractPickerModel contract_picker_model { get; }
public ICollectionView available_tick_factors_view { get; }
public void Dispose() {
foreach (var r in rows) r.Dispose();
contract_picker_model?.Dispose();
}
public ExecutionStrategyImmutable get_strategy() {
var mutable_strategy = _strategy.CloneToMutable();
mutable_strategy.LegParamsMap.Clear();
@@ -241,6 +248,13 @@ namespace mbts.spark {
RaisePropertyChanged(nameof(name), nameof(tick_factors));
}
protected override void Dispose(bool disposing) {
foreach (var r in rows) r.Dispose();
contract_picker_model?.Dispose();
Disposable.dispose(_resources);
base.Dispose(disposing);
}
static string _deduce_symbol(SecurityImmutable security) {
if (!string.IsNullOrWhiteSpace(security.Symbol)) return security.Symbol;
if (security.Synthetic.Legs.Count() > 0) return security.Synthetic.Legs.First().Security.Symbol;
@@ -45,8 +45,10 @@ namespace mbts.spark {
public SyntheticEditorVM strategy_editor {
get {
if (_strategy_editor == null && template.HasValue)
_strategy_editor = new SyntheticEditorVM(model.spreader, template.Value.Security, template.Value.ExecutionStrategy);
if (_strategy_editor == null && template.HasValue) {
var def = template.Value;
_strategy_editor = new SyntheticEditorVM(model.spreader, def.Security, def.ExecutionStrategy, model.security_has_working_orders(def.Security.Id));
}
return _strategy_editor;
}
}
+6
View File
@@ -262,6 +262,12 @@ namespace mbts.spark {
}
#endregion
public static IObservable<bool> security_has_working_orders(this OrdersModel orders_model, uint security_key) =>
orders_model
.on_security_orders(security_key)
.Select(t => t.state.Any(kv => kv.Value.is_active()))
.StartWith(false);
public static async Task<(OrderRecordImmutable result, bool success)> order_from_key(this OrdersModel orders_model, uint order_key) {
var lookup = await orders_model.on_orders.FirstAsyncTask(t => t.state.ContainsKey(order_key), TimeSpan.FromSeconds(3));
return lookup.success ? (lookup.result.state[order_key], true) : (default, false);
@@ -0,0 +1,35 @@
using System;
using System.Windows.Input;
namespace mbts.ui {
class ObservantCommand<T> : ICommand, IRaiseCanExecuteChangedCommand, IObserver<bool> {
readonly Action<T> _execute;
bool _can_execute;
public ObservantCommand(Action<T> execute) =>
_execute = execute;
public event EventHandler CanExecuteChanged;
public bool CanExecute(object parameter) => _can_execute;
public void Execute(object parameter) => _execute(ObjectHelper.try_convert<T>(parameter));
public void OnCompleted() { }
public void OnError(Exception error) { }
public void OnNext(bool value) {
_can_execute = value;
_raise_can_execute_changed();
}
void _raise_can_execute_changed() =>
CanExecuteChanged.Invoke(this, new EventArgs());
void IRaiseCanExecuteChangedCommand.RaiseCanExecuteChanged() =>
_raise_can_execute_changed();
}
class ObservantCommand : ObservantCommand<object> {
public ObservantCommand(Action execute) : base(_ => execute()) { }
public ObservantCommand(Action<object> execute) : base(execute) { }
}
}