refactor: Clean up MarketViewRow
* Define fields of the same type in separate statements * Insert some new lines for easier code browsing * Replace large delegates with local fns
This commit is contained in:
1 parent
dfa18c632e
commit
2fdce0280a
1 file changed
+78
-60
@@ -1,6 +1,8 @@
|
||||
namespace mbts.spark {
|
||||
using bts.api;
|
||||
using bts.api.core;
|
||||
using bts.api.orderman;
|
||||
using bts.api.position;
|
||||
using bts.collections;
|
||||
using bts.utils;
|
||||
|
||||
@@ -26,27 +28,38 @@
|
||||
public class Row : DisposableViewModelBase, ISecurityMDRow, IEquatable<Row>, DIYGrid.IRowVM, DIYGrid.IActionableRowVM {
|
||||
readonly IBus _bus;
|
||||
readonly OrdersModel _orders_model;
|
||||
readonly AnimationManager _animation = new AnimationManager();
|
||||
readonly ObservableCollection<OrderRecordImmutable> _orders = new ObservableCollection<OrderRecordImmutable>();
|
||||
readonly ObservableFilterCollection<OrderRecordImmutable> _working_orders, _working_bids, _working_asks;
|
||||
readonly Property<double> _bid_price, _ask_price, _last_price, _day_change, _settlement, _session_high, _session_low;
|
||||
readonly AnimationManager _animation = new();
|
||||
readonly ObservableCollection<OrderRecordImmutable> _orders = new();
|
||||
readonly ObservableFilterCollection<OrderRecordImmutable> _working_orders;
|
||||
readonly ObservableFilterCollection<OrderRecordImmutable> _working_bids;
|
||||
readonly ObservableFilterCollection<OrderRecordImmutable> _working_asks;
|
||||
readonly Property<double> _bid_price;
|
||||
readonly Property<double> _ask_price;
|
||||
readonly Property<double> _last_price;
|
||||
readonly Property<double> _day_change;
|
||||
readonly Property<double> _settlement;
|
||||
readonly Property<double> _session_high;
|
||||
readonly Property<double> _session_low;
|
||||
readonly Property<bool> _last_price_from_spread;
|
||||
readonly CellProperty<double> _day_position;
|
||||
readonly CellProperty<double> _raw_day_position;
|
||||
readonly CellProperty<uint> _last_size;
|
||||
readonly CellProperty<ulong> _volume;
|
||||
readonly CellProperty<bool> _inside_bids, _inside_asks;
|
||||
readonly CellProperty<int> _working_bid_volume, _working_ask_volume;
|
||||
readonly CellProperty<double> _working_best_bid, _working_best_ask;
|
||||
readonly MyOrderSummarizer _order_summarizer = new MyOrderSummarizer();
|
||||
readonly MDDisplayHelper.SizeFormatter _size_formatter = MDDisplayHelper.get_formatter(MarketSizeDisplay.get_default());
|
||||
PropertyAutoWrapper<uint, uint>
|
||||
_bid_direct_size,
|
||||
_bid_implied_size,
|
||||
_bid_merged_size,
|
||||
_ask_direct_size,
|
||||
_ask_implied_size,
|
||||
_ask_merged_size;
|
||||
readonly CellProperty<bool> _inside_bids;
|
||||
readonly CellProperty<bool> _inside_asks;
|
||||
readonly CellProperty<int> _working_bid_volume;
|
||||
readonly CellProperty<int> _working_ask_volume;
|
||||
readonly CellProperty<double> _working_best_bid;
|
||||
readonly CellProperty<double> _working_best_ask;
|
||||
readonly MyOrderSummarizer _order_summarizer = new();
|
||||
|
||||
readonly PropertyAutoWrapper<uint, uint> _bid_direct_size;
|
||||
readonly PropertyAutoWrapper<uint, uint> _bid_implied_size;
|
||||
readonly PropertyAutoWrapper<uint, uint> _bid_merged_size;
|
||||
readonly PropertyAutoWrapper<uint, uint> _ask_direct_size;
|
||||
readonly PropertyAutoWrapper<uint, uint> _ask_implied_size;
|
||||
readonly PropertyAutoWrapper<uint, uint> _ask_merged_size;
|
||||
|
||||
ulong _bbo_ref, _last_ref;
|
||||
uint? _display_qty_divisor_backing;
|
||||
bool _is_visible;
|
||||
@@ -99,7 +112,6 @@
|
||||
ask_implied_size_click = new DelegateCommand(() => _show_order_ticket(MarketViewView.Columns.AskImpliedSize));
|
||||
working_asks_click = new DelegateCommand(() => _show_order_book(MarketViewView.Columns.WorkingAsks));
|
||||
_animation.AnimationsChanged += on_animations_changed;
|
||||
void on_animations_changed() => RaisePropertyChanged(nameof(animations));
|
||||
_dispose = Disposable.Create(
|
||||
attach_prop_changed_cb(_bid_direct_size, mk_echo_prop_changed_cb<(uint, uint)>(nameof(bid_direct_size_text))),
|
||||
attach_prop_changed_cb(_bid_implied_size, mk_echo_prop_changed_cb<(uint, uint)>(nameof(bid_implied_size_text))),
|
||||
@@ -107,25 +119,14 @@
|
||||
attach_prop_changed_cb(_ask_direct_size, mk_echo_prop_changed_cb<(uint, uint)>(nameof(ask_direct_size_text))),
|
||||
attach_prop_changed_cb(_ask_implied_size, mk_echo_prop_changed_cb<(uint, uint)>(nameof(ask_implied_size_text))),
|
||||
attach_prop_changed_cb(_ask_merged_size, mk_echo_prop_changed_cb<(uint, uint)>(nameof(ask_merged_size_text))),
|
||||
_orders_model.on_security_root_orders(security.Id).OnDispatcher().Subscribe(_on_orders),
|
||||
_orders_model.on_security_positions(security.Id).OnDispatcher().Subscribe(t => {
|
||||
var new_day_pos = t.state.Values.Sum(p => p.Item2.day_pos());
|
||||
bool day_pos_dirty = _day_position.on_next_distinct(new_day_pos);
|
||||
if (day_pos_dirty) {
|
||||
if (!new_day_pos.is_nan() && !day_position.is_nan()) {
|
||||
_animation.animate_position(new_day_pos > _day_position.value);
|
||||
}
|
||||
}
|
||||
bool raw_day_pos_dirty = _raw_day_position.on_next_distinct(t.state.Values.Sum(p => p.Item2.raw_day_pos()));
|
||||
if (day_pos_dirty || raw_day_pos_dirty) {
|
||||
raise_invalidated();
|
||||
}
|
||||
}),
|
||||
_orders_model.on_security_root_orders(security.Id).OnDispatcher().Subscribe(on_orders),
|
||||
_orders_model.on_security_positions(security.Id).OnDispatcher().Subscribe(on_security_positions),
|
||||
_inside_bids.OnDispatcher().Subscribe(v => _animation.animate_working_bids_inside(v.newval)),
|
||||
_inside_asks.OnDispatcher().Subscribe(v => _animation.animate_working_asks_inside(v.newval)),
|
||||
_day_change.OnDispatcher().Subscribe(v => _animation.animate_day_change(v.newval > v.oldval)),
|
||||
Disposable.Create(() => _animation.AnimationsChanged -= on_animations_changed)
|
||||
);
|
||||
Disposable.Create(() => _animation.AnimationsChanged -= on_animations_changed));
|
||||
|
||||
void on_animations_changed() => RaisePropertyChanged(nameof(animations));
|
||||
|
||||
uint convert_display_qty_uint(uint value) =>
|
||||
value.safe_divide(_display_qty_divisor);
|
||||
@@ -139,6 +140,32 @@
|
||||
string fmt_display_qty_double(double value) =>
|
||||
value.safe_divide(_display_qty_divisor).ToString();
|
||||
|
||||
void on_orders(ITableUpdateTransform<OrderRecordImmutable, uint> orders) {
|
||||
foreach (var o in orders.removed) {
|
||||
_order_summarizer.remove_order(o);
|
||||
}
|
||||
foreach (var o in orders.added) {
|
||||
_order_summarizer.add_order(o);
|
||||
}
|
||||
bool dirty = false;
|
||||
_refresh_orders(ref dirty);
|
||||
if (dirty)
|
||||
invalidated?.Invoke();
|
||||
}
|
||||
|
||||
void on_security_positions(TableUpdate<(SecurityImmutable? sec, PositionUnitImmutable pos), int> table) {
|
||||
var new_day_pos = table.state.Values.Sum(p => p.pos.day_pos());
|
||||
bool day_pos_dirty = _day_position.on_next_distinct(new_day_pos);
|
||||
if (day_pos_dirty && !new_day_pos.is_nan() && !day_position.is_nan()) {
|
||||
_animation.animate_position(new_day_pos > _day_position.value);
|
||||
}
|
||||
bool raw_day_pos_dirty = _raw_day_position.on_next_distinct(
|
||||
table.state.Values.Sum(p => p.pos.raw_day_pos()));
|
||||
if (day_pos_dirty || raw_day_pos_dirty) {
|
||||
raise_invalidated();
|
||||
}
|
||||
}
|
||||
|
||||
PropertyChange<T> mk_echo_prop_changed_cb<T>(params string[] props) =>
|
||||
(oldval, newval) => RaisePropertyChanged(props);
|
||||
|
||||
@@ -150,17 +177,12 @@
|
||||
|
||||
public event Action invalidated;
|
||||
|
||||
// security
|
||||
public SecurityMD security_md { get; }
|
||||
public SecurityImmutable security => security_md.security;
|
||||
public uint security_key => security_md.security.Id;
|
||||
SecurityMD ISecurityMDRow.md => security_md;
|
||||
|
||||
public bool is_visible {
|
||||
get => _is_visible;
|
||||
set => Set(ref _is_visible, value);
|
||||
}
|
||||
|
||||
#region Bound Properties
|
||||
public bool inside_bids => _inside_bids.value;
|
||||
public bool inside_asks => _inside_asks.value;
|
||||
public double day_position => _day_position.value;
|
||||
@@ -171,25 +193,29 @@
|
||||
public bool last_price_from_spread => _last_price_from_spread.Value;
|
||||
public uint last_size => _last_size.value;
|
||||
public string last_size_text => _last_size.text;
|
||||
|
||||
// bid price/size
|
||||
public double bid_price => _bid_price.Value;
|
||||
public uint bid_direct_size => _bid_direct_size.model;
|
||||
public uint bid_implied_size => _bid_merged_size.model - _bid_direct_size.model;
|
||||
public uint bid_merged_size => _bid_merged_size.model;
|
||||
|
||||
// bid size text
|
||||
public string bid_direct_size_text => _bid_direct_size.view.ToString();
|
||||
public string bid_implied_size_text => _bid_implied_size.view.ToString();
|
||||
public string bid_merged_size_text => _bid_merged_size.view.ToString();
|
||||
|
||||
// ask price/size
|
||||
public double ask_price => _ask_price.Value;
|
||||
public uint ask_direct_size => _ask_direct_size.model;
|
||||
public uint ask_implied_size => _ask_implied_size.model;
|
||||
public uint ask_merged_size => _ask_merged_size.model;
|
||||
|
||||
// ask size text
|
||||
public string ask_direct_size_text => _ask_direct_size.view.ToString();
|
||||
public string ask_implied_size_text => _ask_implied_size.view.ToString();
|
||||
public string ask_merged_size_text => _ask_merged_size.view.ToString();
|
||||
//
|
||||
|
||||
public ulong volume => _volume.value;
|
||||
public string volume_text => _volume.text;
|
||||
public double settlement => _settlement.Value;
|
||||
@@ -210,7 +236,8 @@
|
||||
security.ContractSpec);
|
||||
public double working_best_bid => _working_best_bid.value;
|
||||
public double working_best_ask => _working_best_ask.value;
|
||||
// ice props
|
||||
|
||||
// ice
|
||||
public string market_description => security.market_description();
|
||||
public string cleared_alias => security.cleared_alias();
|
||||
public string product_description => security.product_name();
|
||||
@@ -234,6 +261,12 @@
|
||||
// animations
|
||||
public IList<ui.spark.CellAnimation> animations => _animation.get_animations().ToList();
|
||||
public ObservableCollection<RowAnimation> row_animations { get; } = new ObservableCollection<RowAnimation>();
|
||||
|
||||
public bool is_visible {
|
||||
get => _is_visible;
|
||||
set => Set(ref _is_visible, value);
|
||||
}
|
||||
|
||||
// commands
|
||||
public ICommand bid_price_click { get; }
|
||||
public ICommand bid_merged_size_click { get; }
|
||||
@@ -247,9 +280,6 @@
|
||||
public ICommand working_asks_click { get; }
|
||||
public ICommand default_command { get; }
|
||||
|
||||
public IReadOnlyList<DIYGrid.IGridCellStore> cell_stores => new DIYGrid.IGridCellStore[0];
|
||||
#endregion
|
||||
|
||||
uint _display_qty_divisor => _display_qty_divisor_backing ??
|
||||
(_display_qty_divisor_backing = _orders_model.security_auxiliary_model.get_display_quantity_divisor(security)).Value;
|
||||
IMDConsumerHandle _md => security_md.md;
|
||||
@@ -273,19 +303,22 @@
|
||||
_ask_price.set(a.price);
|
||||
_refresh_inside_working_orers(ref dirty);
|
||||
}
|
||||
|
||||
if (_md.get_last_trade(ref _last_ref, out NativePriceSize ps, out DateTime ts, out ulong v, out bool from_spread)) {
|
||||
_last_price.set(ps.price);
|
||||
_last_price_from_spread.set(from_spread);
|
||||
_last_size.on_next_distinct(ps.size, ref dirty);
|
||||
_volume.on_next_distinct(v, ref dirty);
|
||||
}
|
||||
|
||||
dirty |=
|
||||
_settlement.set_new(_md.settlement_price) |
|
||||
_session_low.set_new(_md.session_low) |
|
||||
_session_high.set_new(_md.session_high) |
|
||||
_day_change.set_new(last_price - settlement);
|
||||
if (dirty)
|
||||
if (dirty) {
|
||||
invalidated?.Invoke();
|
||||
}
|
||||
}
|
||||
|
||||
public bool Equals(Row other) =>
|
||||
@@ -298,7 +331,6 @@
|
||||
public override int GetHashCode() =>
|
||||
security_key.GetHashCode();
|
||||
|
||||
#region Show Windows
|
||||
void _show_ladder() => _bus.post(SparkTags.ShowLadderTag, security_md);
|
||||
|
||||
void _show_order_book(MarketViewView.Columns col) {
|
||||
@@ -353,19 +385,6 @@
|
||||
break;
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region Callbacks
|
||||
void _on_orders(ITableUpdateTransform<OrderRecordImmutable, uint> orders) {
|
||||
foreach (var o in orders.removed)
|
||||
_order_summarizer.remove_order(o);
|
||||
foreach (var o in orders.added)
|
||||
_order_summarizer.add_order(o);
|
||||
bool dirty = false;
|
||||
_refresh_orders(ref dirty);
|
||||
if (dirty)
|
||||
invalidated?.Invoke();
|
||||
}
|
||||
|
||||
void _refresh_orders(ref bool dirty) {
|
||||
_working_bid_volume.on_next_distinct(_order_summarizer.working_bid_qty, ref dirty);
|
||||
@@ -387,7 +406,6 @@
|
||||
_inside_bids.on_next_distinct(!double.IsNaN(working_best_bid) && OrderDataHelper.PriceComparer.Compare(working_best_bid, bid_price) >= 0, ref dirty);
|
||||
_inside_asks.on_next_distinct(!double.IsNaN(working_best_ask) && OrderDataHelper.PriceComparer.Compare(working_best_ask, ask_price) <= 0, ref dirty);
|
||||
}
|
||||
#endregion
|
||||
|
||||
class MyOrderSummarizer {
|
||||
SortedSetMap<double, OrderRecordImmutable> _bids, _asks;
|
||||
|
||||
Reference in new issue
Block a user