437 lines
21 KiB
C#
437 lines
21 KiB
C#
using bts;
|
|
using bts.api;
|
|
using bts.api.orderman;
|
|
using bts.collections;
|
|
using bts.spark;
|
|
using bts.utils;
|
|
using mbts.ui;
|
|
using mbts.ui.controls;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.ComponentModel;
|
|
using System.Reactive.Linq;
|
|
using System.Runtime.CompilerServices;
|
|
using System.Windows.Input;
|
|
using System.Windows.Media;
|
|
using Columns = mbts.spark.OrderBookGridView.ColumnKeys;
|
|
|
|
namespace mbts.spark {
|
|
partial class OrderBookRowVM : IEquatable<OrderBookRowVM>, DIYGrid.IRowVM, IHierarchicalRow<OrderSecurityRootPair>, INotifyPropertyChanged {
|
|
public static readonly IComparer<OrderBookRowVM> key_comparer = Comparer<uint>.Default.wrap<uint, OrderBookRowVM>(r => r.order_key).to_safe();
|
|
|
|
const string _unknown_security_price_format_string = "0.#########";
|
|
const int _unknown_security_price_num_fractional_digits = 9;
|
|
readonly OrderBookVM _owner;
|
|
Color? _root_algo_badge_color_backing;
|
|
Brush _badge_fill, _root_algo_badge_fill;
|
|
IRaiseCanExecuteChangedCommand _show_order_modify_cmd, _toggle_expanded_cmd, _cancel_cmd, _detach_command, _attach_command, _update_command, _modify_order_quantity_andor_price, _revert_prospective_order_quantity_andor_price;
|
|
bool _is_parent, _is_expanded, _is_selected, _is_attachable, _is_detachable;
|
|
int _depth_level;
|
|
SecurityImmutable? _security => order_tuple.order.security;
|
|
// for mini-ob
|
|
uint _prospective_quantity;
|
|
double _prospective_price;
|
|
Action<bool> _on_selection_changed;
|
|
|
|
public OrderBookRowVM(OrderBookVM owner, OrderSecurityRootPair order_tuple, bool is_expanded, Action<bool> on_selection_changed) {
|
|
_owner = owner;
|
|
this.order_tuple = order_tuple;
|
|
is_visible = !is_child;
|
|
_is_expanded = is_expanded;
|
|
prospective_quantity = this.quantity;
|
|
prospective_price = this.price;
|
|
_on_selection_changed = on_selection_changed;
|
|
}
|
|
|
|
//// This takes care of the case where the OB has all rows visible
|
|
//// and an order is cancelled outside of the OB (say via the ladder).
|
|
//// Since the OB window Commands only revaluate their CanExecuteChanged
|
|
//// when the OrderBookVM.selected_order_changes, this emulates the selected
|
|
//// order changing since the current_row setter invokes the selected_order_changed
|
|
//// handler
|
|
public IObservable<OrderSecurityRootPair> subscribe_data() {
|
|
return Observable.FromEvent<Action, OrderSecurityRootPair>(
|
|
cb => () => cb(order_tuple),
|
|
cb => invalidated += cb,
|
|
cb => invalidated -= cb);
|
|
}
|
|
|
|
public event Action invalidated;
|
|
public event Action<(OrderSecurityRootPair oldval, OrderSecurityRootPair newval)> updated;
|
|
public event PropertyChangedEventHandler PropertyChanged;
|
|
|
|
// Core
|
|
public OrderSecurityRootPair order_tuple { get; private set; }
|
|
public OrderRecordImmutable order => order_tuple.order.order;
|
|
public OrderMetadata order_metadata => _owner.get_metadata(this);
|
|
public Brush badge_fill => _badge_fill ?? (_badge_fill = OrderUtils.badge_fill(order_tuple.order.meta, order_metadata));
|
|
public uint order_key => order.Key;
|
|
public uint parent_order_key => order.ParentKey;
|
|
public uint security_key => order.security_key();
|
|
public SecurityImmutable? security => _security;
|
|
public ContractSpecificationImmutable? contract_spec => security.spec_or_default();
|
|
public string security_text => security.display_name();
|
|
public Venue exchange => _security.exchange_or_default();
|
|
public string symbol => _security.symbol_or_default();
|
|
// Def
|
|
public CustomOrderDefinition.CustomOneofCase order_type => order.order_type();
|
|
public string display_name => order_tuple.order.display_name();
|
|
public OrderSubType order_sub_type => order.sub_type();
|
|
public string order_sub_type_text => _fmt_order_sub_type(order_sub_type);
|
|
public Side side => order.side();
|
|
public string user => order.user();
|
|
public string account => order.account();
|
|
public TimeInForce time_in_force => order.time_in_force();
|
|
public DateTime time_created => order.created();
|
|
public string time_created_text => _fmt_date(time_created);
|
|
// State
|
|
public DateTime last_change => order.last_change();
|
|
public string last_change_text => _fmt_date(last_change);
|
|
public OrderStatus status => order.status();
|
|
public double price => order.price();
|
|
public double last_price => order.last_price();
|
|
public double avg_price => order.avg_price();
|
|
public string price_text => _fmt_price(price);
|
|
public string last_price_text => _fmt_price(last_price);
|
|
public string avg_price_text => _fmt_price(avg_price);
|
|
public uint quantity => order.quantity();
|
|
public uint filled_quantity => order.filled();
|
|
public string last_msg => order.last_msg();
|
|
public uint piq => order_tuple.order.piq ?? uint.MaxValue;
|
|
public string piq_text => piq == uint.MaxValue ? string.Empty : piq.ToString();
|
|
public bool is_non_terminal => order.is_non_terminal();
|
|
public bool is_attachable { get => _is_attachable; set => SetVisual(ref _is_attachable, value); }
|
|
public bool is_detachable { get => _is_detachable; set => SetVisual(ref _is_detachable, value); }
|
|
public string filled_status_desc => order.filled_status_description(security.spec_or_default());
|
|
|
|
// Root Algo
|
|
public CustomOrderDefinition.CustomOneofCase root_algo_order_type => order_tuple.root.algo.value.CustomCase;
|
|
public OrderMetadata root_algo_order_metadata => _owner.get_metadata(root_algo_order_type);
|
|
public Brush root_algo_badge_fill => _root_algo_badge_fill ?? (_root_algo_badge_fill = new SolidColorBrush(_root_algo_badge_color));
|
|
public string root_algo_display_name => order_tuple.root.display_name();
|
|
|
|
// Tree
|
|
public bool is_parent { get => _is_parent; set => Set(ref _is_parent, value); }
|
|
public bool is_child => order.has_parent();
|
|
public bool is_visible { get; set; }
|
|
public bool is_expanded {
|
|
get => _is_expanded;
|
|
set {
|
|
if (Set(ref _is_expanded, value))
|
|
_owner.on_expanded_changed(this);
|
|
}
|
|
}
|
|
public int depth_level { get => _depth_level; set => Set(ref _depth_level, value); }
|
|
public bool is_selected {
|
|
get => _is_selected;
|
|
set {
|
|
if (Set(ref _is_selected, value)) {
|
|
_on_selection_changed(value);
|
|
}
|
|
}
|
|
}
|
|
public uint prospective_quantity {
|
|
get => _prospective_quantity;
|
|
set {
|
|
if (Set(ref _prospective_quantity, value)) {
|
|
RaisePropertyChanged(nameof(is_qty_dirty));
|
|
}
|
|
}
|
|
}
|
|
public double prospective_price {
|
|
get => _prospective_price;
|
|
set {
|
|
if (Set(ref _prospective_price, value)) {
|
|
RaisePropertyChanged(nameof(is_price_dirty));
|
|
}
|
|
}
|
|
}
|
|
public bool is_qty_dirty => this.prospective_quantity != this.quantity;
|
|
public bool is_price_dirty => this.prospective_price != this.price;
|
|
|
|
// Commands
|
|
public ICommand toggle_command => _toggle_expanded_cmd ?? (
|
|
_toggle_expanded_cmd = new DelegateCommand(() => is_expanded = !is_expanded));
|
|
|
|
public ICommand show_order_modify_cmd =>
|
|
_show_order_modify_cmd ?? (_show_order_modify_cmd = new DelegateCommand(() => _owner.show_order_modify(this), () => status == OrderStatus.Working));
|
|
|
|
public ICommand cancel_command => _cancel_cmd ??
|
|
(_cancel_cmd = new DelegateCommand(() => _owner.cancel_order(this), () => status == OrderStatus.Working || status == OrderStatus.Disconnected));
|
|
|
|
public ICommand modify_order_quantity_andor_price {
|
|
get {
|
|
if (_modify_order_quantity_andor_price == null) {
|
|
_modify_order_quantity_andor_price = new DelegateCommand(execute, can_execute);
|
|
invalidated += _modify_order_quantity_andor_price.RaiseCanExecuteChanged;
|
|
// impl
|
|
void execute() {
|
|
if (is_qty_dirty && is_price_dirty) {
|
|
_owner.modify_order_price_and_quantity(this, prospective_price, prospective_quantity);
|
|
} else if (is_qty_dirty) {
|
|
_owner.modify_order_quantity(this, prospective_quantity);
|
|
} else if (is_price_dirty) {
|
|
_owner.modify_order_price(this, this.prospective_price);
|
|
}
|
|
_unselect_me();
|
|
}
|
|
bool can_execute() => status == OrderStatus.Working && (is_price_dirty || is_qty_dirty);
|
|
}
|
|
return _modify_order_quantity_andor_price;
|
|
}
|
|
}
|
|
|
|
public ICommand revert_prospective_order_quantity_andor_price => _revert_prospective_order_quantity_andor_price ??
|
|
(_revert_prospective_order_quantity_andor_price = new DelegateCommand(
|
|
() => {
|
|
prospective_price = price;
|
|
prospective_quantity = quantity;
|
|
},
|
|
() => this.status == OrderStatus.Working && (is_price_dirty || is_qty_dirty)
|
|
)
|
|
);
|
|
|
|
public ICommand detach_command => _detach_command ??
|
|
(_detach_command = new DelegateCommand(() => {
|
|
_owner.detach_orders(this);
|
|
})
|
|
);
|
|
|
|
public ICommand attach_command => _attach_command ?? (
|
|
_attach_command = new DelegateCommand(() => {
|
|
// the _select me will trigger the OBVM.attach_vm to get primed
|
|
if (!is_selected) {
|
|
_select_me();
|
|
}
|
|
_owner.begin_attach_algos();
|
|
})
|
|
);
|
|
|
|
public ICommand update_command => _update_command ?? (
|
|
_update_command = new DelegateCommand(() => {
|
|
if (!is_selected) {
|
|
_select_me();
|
|
}
|
|
})
|
|
);
|
|
|
|
// private props
|
|
Color _root_algo_badge_color {
|
|
get => _root_algo_badge_color_backing ?? (_root_algo_badge_color_backing = OrderUtils.badge_color(order_tuple.root.meta, root_algo_order_metadata)).Value;
|
|
set {
|
|
if (_root_algo_badge_color_backing != value) {
|
|
_root_algo_badge_color_backing = value;
|
|
_root_algo_badge_fill = null;
|
|
}
|
|
}
|
|
}
|
|
|
|
#region IHierarchicalRow
|
|
bool IHierarchicalRow.is_parent => is_parent;
|
|
bool IHierarchicalRow.is_expanded => is_expanded;
|
|
int IHierarchicalRow.depth_level => depth_level;
|
|
OrderSecurityRootPair IHierarchicalRow<OrderSecurityRootPair>.value => order_tuple;
|
|
|
|
void IHierarchicalRow.on_is_parent_changed(bool is_parent) => this.is_parent = is_parent;
|
|
/// Do NOT actually call the <see cref="is_expanded"/> setter (it has side effects) - the row collection calls this method when the row is expanded/collapsed by some **external means, outside of this class.
|
|
/// It should not trigger any functional response beyond modifying the value returned by <see cref="is_expanded"/>.
|
|
/// **external means are:
|
|
/// <see cref="OperableHierarchicalCollection{TRow, T, TKey}.expand_all"/>,
|
|
/// <see cref="OperableHierarchicalCollection{TRow, T, TKey}.expand_row(TKey)"/>
|
|
/// <see cref="OperableHierarchicalCollection{TRow, T, TKey}.collapse_all"/>
|
|
/// <see cref="OperableHierarchicalCollection{TRow, T, TKey}.collapse_row(TKey)"/>
|
|
void IHierarchicalRow.on_is_expanded_changed(bool is_expanded) => Set(ref _is_expanded, is_expanded, nameof(is_expanded));
|
|
void IHierarchicalRow.on_depth_level_changed(int depth_level) => this.depth_level = depth_level;
|
|
void IHierarchicalRow<OrderSecurityRootPair>.on_value_changed(OrderSecurityRootPair value) => update(value);
|
|
#endregion
|
|
|
|
private void _unselect_me() {
|
|
this.is_selected = false;
|
|
}
|
|
|
|
private void _select_me() {
|
|
this.is_selected = true;
|
|
}
|
|
|
|
// These fns present a way for the orderbook to avoid noisy, intermittent collection changes during "Expand/Collapse All" ops,
|
|
// when incremental changes are ignored in favor of a full reset, after 'is_expanded' is updated for all rows.
|
|
internal void expand_internal() => Set(ref _is_expanded, true, nameof(is_expanded));
|
|
internal void collapse_internal() => Set(ref _is_expanded, false, nameof(is_expanded));
|
|
|
|
public void update(OrderSecurityRootPair order_tuple) {
|
|
Log.assert(order_tuple.order.order.Key == this.order.Key, "OrderBookRowVM: update_order was called with an order with the wrong key.");
|
|
// _show_order_modify_cmd is lazy (instantiated in the default_command getter); it might be null, don't not force instantiation
|
|
bool? could_modify_execute = _show_order_modify_cmd?.CanExecute(null);
|
|
var old_order_tuple = this.order_tuple;
|
|
var dirty = ViewRefresh.get_dirty_cells(old_order_tuple, order_tuple);
|
|
this.order_tuple = order_tuple;
|
|
// order type
|
|
if ((Columns.OrderType & dirty) != 0)
|
|
RaisePropertyChanged(nameof(order_type), nameof(order_metadata));
|
|
// quantity
|
|
if ((Columns.Size & dirty) != 0)
|
|
RaisePropertyChanged(nameof(quantity), nameof(is_qty_dirty));
|
|
// we need to update the prospective_quantity here b/c an update could have come in while the row was in edit mode
|
|
// via the Mini OB update column mechanism (vs the EditableQty column)
|
|
// prospecive price does not need to be updated in the same manner b/c if the price is changed, the whole
|
|
// row will no longer match the filter
|
|
prospective_quantity = quantity;
|
|
// price
|
|
if ((Columns.Price & dirty) != 0) {
|
|
RaisePropertyChanged(nameof(price));
|
|
RaisePropertyChanged(nameof(is_price_dirty));
|
|
}
|
|
// security
|
|
if ((Columns.Security & dirty) != 0)
|
|
RaisePropertyChanged(nameof(security), nameof(security_text));
|
|
// badge fill
|
|
if (old_order_tuple.order.meta?.badge_color != order_tuple.order.meta?.badge_color) {
|
|
// invalidate by setting to null (will be re-instantiated in the public getter)
|
|
_badge_fill = null;
|
|
RaisePropertyChanged(nameof(badge_fill));
|
|
}
|
|
// display name
|
|
if (old_order_tuple.order.algo.name != order_tuple.order.algo.name)
|
|
RaisePropertyChanged(nameof(display_name));
|
|
// root badge fill
|
|
if (_root_algo_badge_color_backing.HasValue) {
|
|
// only need to invalidate it if it had a value (it is lazily instantiated)
|
|
_root_algo_badge_color = OrderUtils.badge_color(order_tuple.root.meta, root_algo_order_metadata);
|
|
RaisePropertyChanged(nameof(root_algo_badge_fill));
|
|
}
|
|
// root display name
|
|
if (old_order_tuple.root.algo.name != order_tuple.root.algo.name)
|
|
RaisePropertyChanged(nameof(root_algo_display_name));
|
|
// root order type
|
|
if (old_order_tuple.root.algo.value.CustomCase != order_tuple.root.algo.value.CustomCase) {
|
|
RaisePropertyChanged(nameof(root_algo_order_type));
|
|
}
|
|
// status
|
|
if ((Columns.Status & dirty) != 0)
|
|
RaisePropertyChanged(nameof(status));
|
|
// last message
|
|
if ((Columns.LastMessage & dirty) != 0)
|
|
RaisePropertyChanged(nameof(last_msg));
|
|
// filled status desc
|
|
if ((ViewRefresh.filled_status_desc_mask & dirty) != 0)
|
|
RaisePropertyChanged(nameof(filled_status_desc));
|
|
// show order modify cmd
|
|
if (could_modify_execute.HasValue && could_modify_execute.Value != _show_order_modify_cmd.CanExecute(null))
|
|
_show_order_modify_cmd.RaiseCanExecuteChanged();
|
|
// always raise updated
|
|
updated?.Invoke((old_order_tuple, order_tuple));
|
|
// terminal state
|
|
if (old_order_tuple.order.order.is_non_terminal() != order_tuple.order.order.is_non_terminal()) {
|
|
// while we call refresh_is_at/detachable in the OrderBookRowCollection.update
|
|
// on all the children of a modified row, we used to not call it on the row istelf,
|
|
// which should of course be done here. This manifested via the very top parent in any
|
|
// length of attachable orders *still* showing an attach icon after some descendant was
|
|
// detached
|
|
RaisePropertyChanged(nameof(is_non_terminal), nameof(is_attachable), nameof(is_detachable));
|
|
|
|
// this will update the cancel column in the mini ob
|
|
cancel_command.TryRaiseCanExecuteChanged();
|
|
}
|
|
if ((Columns.Piq & dirty) != 0) {
|
|
RaisePropertyChanged(nameof(piq), nameof(piq_text));
|
|
}
|
|
// always refresh attach and detach b/c
|
|
// conditionally raise invalidated
|
|
if ((dirty & ViewRefresh.invalidate_mask) != 0)
|
|
raise_invalidated();
|
|
}
|
|
|
|
public void raise_invalidated() => invalidated?.Invoke();
|
|
|
|
public bool Equals(OrderBookRowVM other) => other != null && other.order.Key == order.Key;
|
|
public override bool Equals(object obj) => Equals(obj as OrderBookRowVM);
|
|
public override int GetHashCode() => (int)order.Key;
|
|
public override string ToString() => $"{order_key} {last_change} {parent_order_key}";
|
|
|
|
static string _fmt_date(DateTime v) => $"{v:M/d} {v:h:mm:ss.fff tt}";
|
|
static string _fmt_order_sub_type(OrderSubType v) {
|
|
switch (v) {
|
|
case OrderSubType.HedgingLeg: return "H";
|
|
case OrderSubType.QuotingLeg: return "Q";
|
|
default: return string.Empty;
|
|
}
|
|
}
|
|
string _fmt_price(double v) => security.HasValue ?
|
|
security.Value.ContractSpec.format(v) :
|
|
Math.Round(v, _unknown_security_price_num_fractional_digits, MidpointRounding.AwayFromZero).ToString(_unknown_security_price_format_string);
|
|
|
|
void RaisePropertyChanged(params string[] props) {
|
|
var pc = PropertyChanged;
|
|
if (pc == null)
|
|
return;
|
|
for (int i = 0; i < props.Length; i++)
|
|
pc(this, new PropertyChangedEventArgs(props[i]));
|
|
}
|
|
|
|
bool Set<T>(ref T backing, T newval, [CallerMemberName]string prop = "") where T : struct, IEquatable<T> {
|
|
if (!backing.Equals(newval)) {
|
|
backing = newval;
|
|
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(prop));
|
|
return true;
|
|
} else return false;
|
|
}
|
|
bool SetVisual<T>(ref T backing, T newval, [CallerMemberName]string prop = "") where T : struct, IEquatable<T> {
|
|
if (Set(ref backing, newval, prop)) {
|
|
raise_invalidated();
|
|
return true;
|
|
} else return false;
|
|
}
|
|
|
|
static class ViewRefresh {
|
|
public const Columns
|
|
invalidate_mask =
|
|
Columns.Security |
|
|
Columns.LastChange |
|
|
Columns.Filled |
|
|
Columns.LastPrice |
|
|
Columns.AvgFilled |
|
|
Columns.Price |
|
|
Columns.Size |
|
|
Columns.Status |
|
|
Columns.ParentKey |
|
|
Columns.AttachedTargetOrderKey |
|
|
Columns.LastMessage |
|
|
Columns.Piq,
|
|
filled_status_desc_mask =
|
|
Columns.Filled |
|
|
Columns.AvgFilled |
|
|
Columns.Status;
|
|
|
|
public static Columns get_dirty_cells(OrderSecurityRootPair oldval, OrderSecurityRootPair newval) {
|
|
var dirty = (Columns)0;
|
|
if (oldval.order.security != newval.order.security)
|
|
dirty |= Columns.Security;
|
|
if (oldval.order.order.last_change() != newval.order.order.last_change())
|
|
dirty |= Columns.LastChange;
|
|
if (oldval.order.order.filled() != newval.order.order.filled())
|
|
dirty |= Columns.Filled;
|
|
if (oldval.order.order.last_price() != newval.order.order.last_price())
|
|
dirty |= Columns.LastPrice;
|
|
if (oldval.order.order.avg_price() != newval.order.order.avg_price())
|
|
dirty |= Columns.AvgFilled;
|
|
if (oldval.order.order.price() != newval.order.order.price())
|
|
dirty |= Columns.Price;
|
|
if (oldval.order.order.quantity() != newval.order.order.quantity())
|
|
dirty |= Columns.Size;
|
|
if (oldval.order.order.ParentKey != newval.order.order.ParentKey)
|
|
dirty |= Columns.ParentKey;
|
|
if (oldval.order.order.attached_target_order_key() != newval.order.order.attached_target_order_key())
|
|
dirty |= Columns.AttachedTargetOrderKey;
|
|
if (oldval.order.order.status() != newval.order.order.status())
|
|
dirty |= Columns.Status;
|
|
if (oldval.order.order.last_msg() != newval.order.order.last_msg())
|
|
dirty |= Columns.LastMessage;
|
|
if (oldval.order.piq != newval.order.piq) {
|
|
dirty |= Columns.Piq;
|
|
}
|
|
return dirty;
|
|
}
|
|
}
|
|
}
|
|
}
|