SPARK-2092 Orderbook update ui supports spreader algos

This commit is contained in:
John Colvin committed 2019-03-08 14:36:39 -06:00
1 parent d1c915fef3
commit aa39349fc4
6 files changed
+198 -48

No files matched your search

@@ -72,6 +72,11 @@ namespace mbts.spark {
concat_disposable(
model.on_order_tuples.OnDispatcher().Subscribe(_row_collection.update),
grid_settings_manager,
algo_vm.connect_data(Observable.Return(model)),
algo_vm.connect_data(Observable.FromEvent<PropertyChange<SecurityImmutable?>, SecurityImmutable?>(
cb => (oldval, newval) => cb(newval),
cb => _selected_algo_mon.common_spreader_sec_changed += cb,
cb => _selected_algo_mon.common_spreader_sec_changed -= cb)),
algo_vm,
Disposable.Create(() => {
_columns.filter_changed -= on_filter_changed;
@@ -92,8 +97,8 @@ namespace mbts.spark {
algo_vm.algo_root = null;
}
}
void on_all_selected_orders_active_changed(bool all_active) {
algo_vm.editing_permissions_mask = all_active ? AlgoEditingPermissions.AllowEditValues : AlgoEditingPermissions.None;
void on_all_selected_orders_active_changed(bool oldval, bool newval) {
algo_vm.editing_permissions_mask = newval ? AlgoEditingPermissions.AllowEditValues : AlgoEditingPermissions.None;
modify_selected_algos_async_cmd.RaiseCanExecuteChanged();
}
}
@@ -336,19 +341,23 @@ namespace mbts.spark {
/// In terms of user-interaction, this means the user may edit the algo params for multiple orders at once if they are all active and share a common algo structure.
/// </summary>
class SelectedAlgosMonitor {
static readonly TimeSpan _throttle = TimeSpan.FromSeconds(0.1);
readonly PropertyUpdateThrottle<CustomOrderDefinitionImmutable?> _common_algo = new PropertyUpdateThrottle<CustomOrderDefinitionImmutable?>(_throttle);
readonly PropertyUpdateThrottle<SecurityImmutable?> _common_spreader_sec = new PropertyUpdateThrottle<SecurityImmutable?>(_throttle);
readonly PropertyUpdateThrottle<bool> _all_active = new PropertyUpdateThrottle<bool>(_throttle);
readonly SetMap<(AlgoStructureKey structure, SecurityImmutable? spreader_sec), OrderRecordImmutable> _algo_structure_index = new SetMap<(AlgoStructureKey structure, SecurityImmutable? spreader_sec), OrderRecordImmutable>();
readonly DynamicOrderHelper _dyn_helper;
readonly SetMap<AlgoStructureKey, OrderRecordImmutable> _algo_structure_index = new SetMap<AlgoStructureKey, OrderRecordImmutable>();
int _inactive_order_count;
public SelectedAlgosMonitor(DynamicOrderHelper dyn_helper) =>
_dyn_helper = dyn_helper;
public event PropertyChange<CustomOrderDefinitionImmutable?> common_algo_changed;
public event Action<bool> all_active_changed;
public CustomOrderDefinitionImmutable? common_algo =>
_algo_structure_index.Count == 1 ? _algo_structure_index.First().First().Def.CustomDef : (CustomOrderDefinitionImmutable?)null;
public event PropertyChange<CustomOrderDefinitionImmutable?> common_algo_changed { add => _common_algo.changed += value; remove => _common_algo.changed -= value; }
public event PropertyChange<SecurityImmutable?> common_spreader_sec_changed { add => _common_spreader_sec.changed += value; remove => _common_spreader_sec.changed -= value; }
public event PropertyChange<bool> all_active_changed { add => _all_active.changed += value; remove => _all_active.changed -= value; }
public CustomOrderDefinitionImmutable? common_algo => _algo_structure_index.Count == 1 ? _algo_structure_index.First().First().Def.CustomDef : (CustomOrderDefinitionImmutable?)null;
public SecurityImmutable? common_spreader_sec => _algo_structure_index.Count == 1 ? _algo_structure_index.First().Key.spreader_sec : null;
public bool all_active => _inactive_order_count == 0 && _algo_structure_index.Count > 0;
public void on_row_selected(OrderBookRowVM row) {
@@ -365,11 +374,9 @@ namespace mbts.spark {
var old_algo = common_algo;
bool old_all_active = all_active;
return Disposable.Create(() => {
var new_algo = common_algo;
if (!EqualityComparer.nullable_equals(old_algo, new_algo, OrderDataHelper.EqualsHelpers.equals))
common_algo_changed?.Invoke(old_algo, new_algo);
if (old_all_active != all_active)
all_active_changed?.Invoke(!old_all_active);
_common_algo.value = common_algo;
_common_spreader_sec.value = common_spreader_sec;
_all_active.value = all_active;
});
}
@@ -381,16 +388,21 @@ namespace mbts.spark {
}
void _select_algo(OrderSecurityAlgoMeta osam) {
_algo_structure_index.add(AlgoStructureSerializer.structure_key(osam.order.Def.CustomDef, _dyn_helper.dynamic_type_index), osam.order);
_algo_structure_index.add((AlgoStructureSerializer.structure_key(osam.order.Def.CustomDef, _dyn_helper.dynamic_type_index), _get_spreader_sec(osam)), osam.order);
if (!osam.order.is_active())
_inactive_order_count++;
}
void _unselect_algo(OrderSecurityAlgoMeta osam) {
_algo_structure_index.remove(AlgoStructureSerializer.structure_key(osam.order.Def.CustomDef, _dyn_helper.dynamic_type_index), osam.order);
_algo_structure_index.remove((AlgoStructureSerializer.structure_key(osam.order.Def.CustomDef, _dyn_helper.dynamic_type_index), _get_spreader_sec(osam)), osam.order);
if (!osam.order.is_active())
_inactive_order_count--;
}
static SecurityImmutable? _get_spreader_sec(OrderSecurityAlgoMeta osam) =>
OrderDataHelper.enumerate_algo_hierarchy(osam.order.Def.CustomDef.CloneToMutable())
.Any(algo => algo.CustomCase == CustomOrderDefinition.CustomOneofCase.Spreader) ?
osam.security : null;
}
}
}
@@ -98,7 +98,11 @@ namespace mbts.spark {
}
#region OrderTypeVM Base Classes
abstract class OrderTypeVMBase<TMutable, TImmutable> : DisposableViewModelBase, IAlgoVM, IObserver<SecurityMD?>, IObserver<OrdersModel>, IObserver<AlgoEditingPermissions> where TMutable : class, IMessage, new() where TImmutable : struct, IImmutableMessage {
abstract class OrderTypeVMBase<TMutable, TImmutable> : DisposableViewModelBase, IAlgoVM,
IObserver<SecurityMD?>,
IObserver<SecurityImmutable?>,
IObserver<OrdersModel>,
IObserver<AlgoEditingPermissions> where TMutable : class, IMessage, new() where TImmutable : struct, IImmutableMessage {
static readonly FieldDescriptor _descriptor = OrderTypeVMUtils.find_descriptor<TMutable>();
readonly IDictionary<string, object> _children_vm = new ExpandoObject();
protected CompositeDisposable _resources = new CompositeDisposable();
@@ -108,6 +112,7 @@ namespace mbts.spark {
readonly ObservableAutoWrapper<IAlgoSelector, AlgoSelectorData> _child_algo_selectors;
readonly Property<bool> _is_expanded;
readonly DeferrableActionWrapper _def_change_notifier;
SecurityInput _security_input;
AlgoEditingPermissions _editing_permissions;
IDisposable _pending_load;
@@ -170,8 +175,8 @@ namespace mbts.spark {
public virtual bool is_expanded { get => _is_expanded.Value; set => _is_expanded.Value = value; }
// protected (observed props)
protected OrdersModel model { get; private set; }
protected SecurityMD? security_md { get; private set; }
protected SecurityImmutable? security => security_md?.security;
protected SecurityMD? security_md => _security_input.security_md;
protected SecurityImmutable? security => _security_input.security;
public void assign_to(AlgoDef value) {
var algo = new TMutable();
@@ -347,6 +352,7 @@ namespace mbts.spark {
protected virtual void on_model_internal() { }
protected virtual void on_security_md_internal() { }
protected virtual void on_security_internal() { }
// model
void IObserver<OrdersModel>.OnNext(OrdersModel model) {
@@ -366,19 +372,40 @@ namespace mbts.spark {
void IObserver<OrdersModel>.OnCompleted() { }
// security md
void IObserver<SecurityMD?>.OnNext(SecurityMD? security_md) {
this.security_md = security_md;
using (_def_change_notifier.defer_execution()) {
on_security_md_internal();
_populate_selectors();
}
using (_def_change_notifier.defer_execution())
using (_update_sec_input())
_security_input = _security_input.with_security_md(security_md);
}
void IObserver<SecurityMD?>.OnError(Exception error) { }
void IObserver<SecurityMD?>.OnCompleted() { }
// security
void IObserver<SecurityImmutable?>.OnNext(SecurityImmutable? security) {
using (_def_change_notifier.defer_execution())
using (_update_sec_input())
_security_input = _security_input.with_security(security);
}
void IObserver<SecurityImmutable?>.OnError(Exception error) { }
void IObserver<SecurityImmutable?>.OnCompleted() { }
// editing permissions
void IObserver<AlgoEditingPermissions>.OnNext(AlgoEditingPermissions editing_permissions) => this.editing_permissions = editing_permissions;
void IObserver<AlgoEditingPermissions>.OnError(Exception error) { }
void IObserver<AlgoEditingPermissions>.OnCompleted() { }
IDisposable _update_sec_input() {
var old_sec = security;
var old_sec_md = security_md;
return Disposable.Create(() => {
if (security != old_sec) {
on_security_internal();
if (_editing_permissions.HasFlag(AlgoEditingPermissions.AllowEditStructure))
_populate_selectors();
}
if (security_md != old_sec_md)
on_security_md_internal();
});
}
IDisposable _selector_data_hooks;
void _populate_selectors() {
if (model != null && security.HasValue) {
@@ -396,6 +423,40 @@ namespace mbts.spark {
}
}
// security input may come in 2 forms:
// 1) SecurityImmutable?
// 2) SecurityMD?
// This structure resolves conflicts that may arise from the different inputs
struct SecurityInput {
readonly SecurityMD? _raw_security_md;
readonly SecurityImmutable? _raw_security;
public readonly SecurityMD? security_md;
public readonly SecurityImmutable? security;
SecurityInput(SecurityImmutable? raw_security, SecurityMD? raw_security_md, SecurityImmutable? security, SecurityMD? security_md) {
_raw_security = raw_security;
_raw_security_md = raw_security_md;
this.security_md = security_md;
this.security = security;
}
public SecurityInput with_security(SecurityImmutable? security) =>
new SecurityInput(
security,
_raw_security_md,
security,
security.HasValue && _raw_security_md.HasValue && _raw_security_md.Value.security.Id != security.Value.Id ?
null : _raw_security_md);
public SecurityInput with_security_md(SecurityMD? security_md) =>
new SecurityInput(
_raw_security,
security_md,
security_md?.security ?? _raw_security,
security_md);
}
struct AlgoSelectorData {
public readonly string prop_name;
public readonly AlgoSelector selector;
@@ -484,4 +545,4 @@ namespace mbts.spark {
}
#endregion
}
}
}
@@ -1,4 +1,5 @@
using bts;
using bts.api;
using bts.api.orderman;
using mbts.ui.controls;
using mbts.ui.utility;
@@ -11,11 +12,9 @@ using SpreaderDefi = bts.api.orderman.SpreaderOrderDefinitionImmutable;
namespace mbts.spark {
static partial class OrderTypeVMFactory {
class SpreaderOrderVM : OrderTypeVMBase<SpreaderDef, SpreaderDefi>, IAlgoVM, ILazyAlgo, IObserver<SecurityMD?>, IObserver<OrdersModel> {
class SpreaderOrderVM : OrderTypeVMBase<SpreaderDef, SpreaderDefi>, IAlgoVM, ILazyAlgo, IObserver<OrdersModel> {
readonly Property<SpreaderDefi?> _template;
readonly StrategyEditorWrapper _strategy_editor_wrapper;
OrdersModel _model;
SecurityMD? _security_md;
IDisposable _strategy_sub;
public SpreaderOrderVM(AlgoKey key, string display_name, AlgoEditingPermissions editing_permissions)
@@ -80,27 +79,15 @@ namespace mbts.spark {
set_error(nameof(template), "Waiting for security and/or strategy.", true);
else clear_error(nameof(template));
}
void IObserver<SecurityMD?>.OnCompleted() { }
void IObserver<SecurityMD?>.OnError(Exception error) { }
void IObserver<SecurityMD?>.OnNext(SecurityMD? security_md) {
_security_md = security_md;
_reset_strategy_sub();
}
void IObserver<OrdersModel>.OnCompleted() { }
void IObserver<OrdersModel>.OnError(Exception error) { }
void IObserver<OrdersModel>.OnNext(OrdersModel model) {
_model = model;
_reset_strategy_sub();
}
protected override void on_model_internal() => _reset_strategy_sub();
protected override void on_security_internal() => _reset_strategy_sub();
void _reset_strategy_sub() {
Disposable.dispose(ref _strategy_sub);
if (_model != null) {
if (_security_md.HasValue) {
_strategy_sub = _model.spreader.security_strategy(_security_md.Value.security.Id)
.Select(strategy => new SpreaderDefi?(_model.factory.spreader_orderi(_security_md.Value.security, strategy)))
if (model != null) {
if (security.HasValue) {
_strategy_sub = model.spreader.security_strategy(security.Value.Id)
.Select(strategy => new SpreaderDefi?(model.factory.spreader_orderi(security.Value, strategy)))
.OnDispatcher()
.Subscribe(s => {
is_loaded = true;
@@ -140,7 +127,7 @@ namespace mbts.spark {
}
}
OrdersModel _model => _owner._model;
OrdersModel _model => _owner.model;
SpreaderDefi? _template { get => _owner.template; set => _owner.template = value; }
public void Dispose() {
@@ -0,0 +1,59 @@
using mbts.ui.shell;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
namespace bts.utils {
class PropertyUpdateThrottle<T> {
readonly TimeSpan _throttle;
readonly IEqualityComparer<T> _comparer;
bool _busy, _dirty, _scheduled;
T _value, _last_fired;
public PropertyUpdateThrottle(TimeSpan throttle)
: this(throttle, EqualityComparer<T>.Default) { }
public PropertyUpdateThrottle(TimeSpan throttle, IEqualityComparer<T> comparer) {
_throttle = throttle;
_comparer = comparer;
}
public T value {
get => _value;
set {
if (!_comparer.Equals(_value, value)) {
_value = value;
_fire();
}
}
}
public event PropertyChange<T> changed;
async void _fire() {
if (!_busy) {
_busy = true;
_dirty = false;
var old = _last_fired;
_last_fired = _value;
changed?.Invoke(old, _value);
await Task.Delay(_throttle);
_busy = false;
} else if (!_dirty) {
_dirty = true;
_delay_fire();
}
}
async void _delay_fire() {
if (!_scheduled) {
_scheduled = true;
await Task.Delay(100);
if (_dirty) {
_dirty = false;
changed?.Invoke(_last_fired, _value);
}
}
}
}
}
@@ -946,7 +946,7 @@
</VisualStateManager.VisualStateGroups>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<usp:ExecutionStrategyEditor
x:Name="PART_Strategy_ExecutionStrategyEditor"
@@ -963,7 +963,7 @@
RemoveLegCommand="{Binding remove_leg}"
ContractPickerModel="{Binding contract_picker_model}"
/>
<c:DialogSkirt Grid.Row="1" Margin="-4" RightItems="{Binding skirt_items}"/>
<c:DialogSkirt Grid.Row="1" Margin="-4" VerticalAlignment="Top" RightItems="{Binding skirt_items}"/>
</Grid>
</DataTemplate>
</usp:AlgoContentTemplateSelector.SpreaderTemplate>
@@ -308,6 +308,37 @@ namespace mbts.spark.utilities {
f.FieldType == FieldType.Message && f.MessageType.ClrType == typeof(CustomOrderDefinition);
}
public static IEnumerable<CustomOrderDefinition> enumerate_algo_children(CustomOrderDefinition algo) {
switch (algo.CustomCase) {
case CustomOrderDefinition.CustomOneofCase.Dynamic:
foreach (var child in enumerate_dynamic_algo_children(algo.Dynamic))
yield return child;
break;
default:
foreach (var cad in get_child_algo_data(algo)) {
if (cad.get_child(algo) is CustomOrderDefinition child)
yield return child;
}
break;
}
}
public static IEnumerable<CustomOrderDefinition> enumerate_dynamic_algo_children(DynamicOrderDefinition dynamic_algo) {
foreach (var kv in dynamic_algo.OrderParams) {
if (kv.Value.ValueCase == DynamicOrderParam.ValueOneofCase.AsOrderDef)
yield return kv.Value.AsOrderDef;
}
}
public static IEnumerable<CustomOrderDefinition> enumerate_algo_hierarchy(CustomOrderDefinition algo) {
yield return algo;
foreach (var child in enumerate_algo_children(algo)) {
yield return child;
foreach (var grand_child in enumerate_algo_hierarchy(child))
yield return grand_child;
}
}
public static IReadOnlyList<ChildAlgoData> get_child_algo_data(CustomOrderDefinitionImmutable defi) =>
get_child_algo_data(defi.CloneToMutable());