561 lines
31 KiB
C#
561 lines
31 KiB
C#
using bts.api;
|
|
using bts.api.orderman;
|
|
using bts.collections;
|
|
using bts.shell;
|
|
using bts.spark;
|
|
using bts.utils;
|
|
using mbts.co2;
|
|
using mbts.spark.utilities;
|
|
using mbts.ui;
|
|
using mbts.ui.shell;
|
|
using mbts.utils;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Collections.ObjectModel;
|
|
using System.Collections.Specialized;
|
|
using System.Linq;
|
|
using System.Reactive.Linq;
|
|
using System.Threading.Tasks;
|
|
using System.Windows.Input;
|
|
using System.Windows.Media;
|
|
|
|
namespace mbts.spark {
|
|
class LadderVM : DisposableViewModelBase, IWillLetYouKnowWhenIDispose, IKeyListener {
|
|
static readonly Brush _transparent_status_brush = BTSColors.UI.transparent;
|
|
|
|
readonly IBus _bus;
|
|
readonly LadderModel _model;
|
|
readonly Property<double> _high, _low, _change, _open, _settlement, _left_panel_width, _zoom_factor;
|
|
//readonly Property<double> _window_width, _window_height;
|
|
readonly Property<uint> _orders_column_width, _bid_column_width, _price_column_width, _ask_column_width, _last_column_width, _volume_column_width, _piq_column_width;
|
|
readonly Property<ulong> _volume;
|
|
readonly Property<bool> _auto_clear;
|
|
readonly PropertyAutoWrapper<LadderSettings, bool> _show_day, _show_high, _show_low, _show_open, _show_settle, _show_volume, _show_account,
|
|
_show_tif, _show_order_type, _show_size_preset, _show_auto_clear, _show_clear, _show_cancel_buys,
|
|
_show_cancel_sells, _show_cancel_all, _show_zoom, _show_status_message;
|
|
readonly PropertyAutoWrapper<LadderSettings, Brush> _bid_background, _bid_foreground, _ask_background, _ask_foreground;
|
|
readonly HashSet<AlgoKey> _sticky_algos = new HashSet<AlgoKey>();
|
|
double
|
|
_position,
|
|
_day_position,
|
|
_raw_day_position,
|
|
_left_panel_extent_height,
|
|
_left_panel_view_port_height,
|
|
_left_panel_scroll_position,
|
|
_left_panel_scroll_position_target;
|
|
bool _show_account_position, _show_day_position, _show_raw_day_position;
|
|
ulong _misc_time = 0;
|
|
ulong _last_trade_time = 0;
|
|
IEqualityComparer<double> _equality;
|
|
Dictionary<double, LadderOrderVM> _slot_price_vs_orders;
|
|
IReadOnlyDictionary<uint, OrderSecurity> _cache = DictionaryHelper.empty<uint, OrderSecurity>();
|
|
|
|
public static LadderVM create(IBus bus, LadderModel model, LadderSettings init, IObservable<LadderSettings> settings) =>
|
|
new LadderVM(bus, model, init, settings, new OrderPropertiesEditorVM(model.order_gateway, Observable.Return(model.security_md), settings.Select(s => new InlineOrderProperties(model.order_gateway, s))));
|
|
|
|
public static LadderVM create_readonly(IBus bus, LadderModel model, IObservable<LadderSettings> settings) =>
|
|
create_readonly(bus, model, LadderSettings.Default, settings);
|
|
|
|
public static LadderVM create_readonly(IBus bus, LadderModel model, LadderSettings init, IObservable<LadderSettings> settings) =>
|
|
new LadderVM(bus, model, init, settings);
|
|
|
|
LadderVM(IBus bus, LadderModel model, LadderSettings init, IObservable<LadderSettings> settings, OrderPropertiesEditorVM order_properties_vm = null) {
|
|
_bus = bus;
|
|
_model = model;
|
|
current_settings = init;
|
|
this.settings = settings;
|
|
_equality = TickUtils.tick_equality_comparer(_model.security.ContractSpec);
|
|
_slot_price_vs_orders = new Dictionary<double, LadderOrderVM>(_equality);
|
|
this.order_properties_vm = order_properties_vm;
|
|
_high = backing(nameof(high), double.NaN);
|
|
_low = backing(nameof(low), double.NaN);
|
|
_change = backing(nameof(change), double.NaN);
|
|
_open = backing(nameof(open), double.NaN);
|
|
_settlement = backing(nameof(settlement), double.NaN);
|
|
// init window-specific settings: these properties should not observe settings updates; they are independent from the settings dialog, so it doesn't make sense that the settings dialog should cause them to change.
|
|
var ws = init.get_window_specifics();
|
|
_orders_column_width = backing(nameof(orders_column_width), ws.orders_column_width);
|
|
_bid_column_width = backing(nameof(bid_column_width), ws.bid_column_width);
|
|
_price_column_width = backing(nameof(price_column_width), ws.price_column_width);
|
|
_ask_column_width = backing(nameof(ask_column_width), ws.ask_column_width);
|
|
_last_column_width = backing(nameof(last_column_width), ws.last_column_width);
|
|
_piq_column_width = backing(nameof(piq_column_width), ws.piq_column_width);
|
|
_volume_column_width = backing(nameof(volume_column_width), ws.volume_column_width);
|
|
_left_panel_width = backing(nameof(left_panel_width), ws.order_entry_panel_width);
|
|
_zoom_factor = backing(nameof(zoom_factor), ws.zoom_factor);
|
|
_left_panel_scroll_position_target = init.left_panel_scroll_position;
|
|
// remember: account_positions needs to subscribe to selected_account; both the sub and account_positions need to be disposed
|
|
_volume = backing(nameof(volume), 0ul);
|
|
_auto_clear = backing(nameof(auto_clear), init.auto_clear);
|
|
_status = backing(nameof(status_message), "");
|
|
_status_brush = backing(nameof(status_background), _transparent_status_brush);
|
|
// All settings observers should be aggregated together, so they can all share the same subscription to the settings
|
|
// (these subscriptions are not horribly expensive, but they're not free either - even more so with the .OnDispatcher() extension).
|
|
var settings_observer = new CompositeObserver<LadderSettings> {
|
|
/// first things first: assign our <see cref="current_settings"/>
|
|
(s => current_settings = s),
|
|
// brush props
|
|
(_bid_background = brush_prop(nameof(bid_background), s => s.bid_column.background)).set,
|
|
(_bid_foreground = brush_prop(nameof(bid_foreground), s => s.bid_column.foreground)).set,
|
|
(_ask_background = brush_prop(nameof(ask_background), s => s.ask_column.background)).set,
|
|
(_ask_foreground = brush_prop(nameof(ask_foreground), s => s.ask_column.foreground)).set,
|
|
// bool visibility props
|
|
(_show_day = bool_prop(nameof(show_day), s => s.day_visible)).set,
|
|
(_show_high = bool_prop(nameof(show_high), s => s.high_visible)).set,
|
|
(_show_low = bool_prop(nameof(show_low), s => s.low_visible)).set,
|
|
(_show_open = bool_prop(nameof(show_open), s => s.open_visible)).set,
|
|
(_show_settle = bool_prop(nameof(show_settle), s => s.settle_visible)).set,
|
|
(_show_volume = bool_prop(nameof(show_volume), s => s.volume_visible)).set,
|
|
(_show_account = bool_prop(nameof(show_account), s => s.account_visible)).set,
|
|
(_show_tif = bool_prop(nameof(show_tif), s => s.tif_visible)).set,
|
|
(_show_order_type = bool_prop(nameof(show_order_type), s => s.order_type_visible)).set,
|
|
(_show_size_preset = bool_prop(nameof(show_size_preset), s => s.size_presets_visible)).set,
|
|
(_show_auto_clear = bool_prop(nameof(show_auto_clear), s => s.auto_clear_visible)).set,
|
|
(_show_clear = bool_prop(nameof(show_clear), s => s.clear_visible)).set,
|
|
(_show_cancel_buys = bool_prop(nameof(show_cancel_buys), s => s.cancel_buys_visible)).set,
|
|
(_show_cancel_sells = bool_prop(nameof(show_cancel_sells), s => s.cancel_sells_visible)).set,
|
|
(_show_cancel_all = bool_prop(nameof(show_cancel_all), s => s.cancel_all_visible)).set,
|
|
(_show_zoom = bool_prop(nameof(show_zoom), s => s.zoom_slider_visible)).set,
|
|
(_show_status_message = bool_prop(nameof(show_status_message), s => s.status_message_visible)).set,
|
|
s => {
|
|
if (
|
|
Set(ref _show_account_position, s.account_position_visible, nameof(show_account_position)) |
|
|
Set(ref _show_day_position, s.day_position_visible, nameof(show_day_position)) |
|
|
Set(ref _show_raw_day_position, s.raw_day_position_visible, nameof(show_raw_day_position))) {
|
|
RaisePropertyChanged(nameof(position_display_text));
|
|
}
|
|
size_calculator.buttons = s.calculator_buttons;
|
|
_auto_clear.set(s.auto_clear);
|
|
// column widths
|
|
_orders_column_width.set(s.orders_column.width);
|
|
_bid_column_width.set(s.bid_column.width);
|
|
_price_column_width.set(s.price_column.width);
|
|
_ask_column_width.set(s.ask_column.width);
|
|
_last_column_width.set(s.last_column.width);
|
|
_piq_column_width.set(s.piq_column.width);
|
|
_volume_column_width.set(s.volume_column.general.width);
|
|
// left panel/zoom
|
|
_left_panel_width.set(s.order_entry_panel_initial_width);
|
|
_zoom_factor.set(s.zoom_factor);
|
|
}
|
|
};
|
|
PropertyAutoWrapper<LadderSettings, bool> bool_prop(string name, Func<LadderSettings, bool> get_bool) =>
|
|
backing(name, init, get_bool, EqualityComparer.Create(get_bool));
|
|
|
|
PropertyAutoWrapper<LadderSettings, Brush> brush_prop(string name, Func<LadderSettings, uint> get_color_uint) =>
|
|
backing(name, init, s => get_color_uint(s).rgb_brush(), EqualityComparer.Create(get_color_uint));
|
|
//
|
|
//_window_height = backing(nameof(window_height), settings_manager.subscribe_current().OnDispatcher().Select(s => s.zoom_factor), (scale) => scale * 600);
|
|
//_window_width = backing(nameof(window_width), settings_manager.subscribe_current().OnDispatcher().Select(s => s.zoom_factor), (scale) => scale * 420);
|
|
position_click_command = new DelegateCommand(() => size_calculator.value = DoubleHelper.safe_convert_to_uint(Math.Abs(_show_day_position ? day_position : (_show_raw_day_position ? _raw_day_position : position))));
|
|
send_order_command = new AsyncCommand<(double price, Side? side, MouseButton button)>(send_order_async);
|
|
cancel_command = new AsyncCommand<LadderModel.CancelType>(_model.cancel_orders, true);
|
|
show_book_command = new DelegateCommand(_show_book);
|
|
show_ticket_command = new DelegateCommand(_show_ticket);
|
|
settings_command = new DelegateCommand(() => request_settings_dialog?.Invoke());
|
|
|
|
orders = new ObservableCollection<LadderOrderVM>();
|
|
|
|
_dispose = Disposable.Create(
|
|
settings.OnDispatcher().Subscribe(settings_observer),
|
|
get_order_properties_editor_hooks(),
|
|
bus.subscribe(SparkTags.LogActivity).OnDispatcher().Subscribe(status_update),
|
|
// we need to hook this up to model.orders which is filtered for just the security
|
|
model.orders.OnDispatcher().Subscribe(working => {
|
|
// Here we manage N orders to 1 price slot in the ladder
|
|
Dictionary<double, HashSet<uint>> orders_used_in_this_update = new Dictionary<double, HashSet<uint>>(_equality);
|
|
_cache = working.state;
|
|
|
|
foreach (var os in _cache.Values) {
|
|
var wo = os.order;
|
|
var slot_price_target = wo.price();
|
|
var order_type = wo.order_type();
|
|
var filled = wo.filled();
|
|
if (order_type == CustomOrderDefinition.CustomOneofCase.Stop) {
|
|
// if the stop order has been submitted but hasn't been triggered
|
|
// we draw the badge at the stop/trigger price, else the price price
|
|
if (filled == 0) { slot_price_target = wo.stop_price(); }
|
|
} else if (order_type == CustomOrderDefinition.CustomOneofCase.StopLimit) {
|
|
// consistent behavior as Stop order above.
|
|
if (filled == 0) { slot_price_target = wo.stop_limit_price(); }
|
|
}
|
|
|
|
if (_slot_price_vs_orders.TryGetValue(slot_price_target, out LadderOrderVM slot_vm)) {
|
|
slot_vm.add_order((wo, os.piq, os.algo, os.meta));
|
|
} else {
|
|
slot_vm = new LadderOrderVM((wo, os.piq, os.algo, os.meta), slot_price_target, this, _bus, settings.Select(s => s.badge_style).OnDispatcher());
|
|
_slot_price_vs_orders[slot_price_target] = slot_vm;
|
|
}
|
|
|
|
// keep track of slot prices vs orders; we use this below to remove orders from the LadderOrderVMs
|
|
// that are not found in this callback run
|
|
if (!orders_used_in_this_update.TryGetValue(slot_price_target, out HashSet<uint> orders_at_level)) {
|
|
orders_at_level = new HashSet<uint>();
|
|
}
|
|
orders_at_level.Add(wo.Key);
|
|
orders_used_in_this_update[slot_price_target] = orders_at_level;
|
|
|
|
}
|
|
|
|
// remove any orders from the lovm that aren't in here
|
|
foreach(var kvp in orders_used_in_this_update) {
|
|
var slot_vm = _slot_price_vs_orders[kvp.Key];
|
|
foreach(var existing_order_key in slot_vm.order_keys) {
|
|
if (!kvp.Value.Contains(existing_order_key)) {
|
|
slot_vm.remove_order(existing_order_key);
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
// update dirty brushes and text
|
|
foreach (var lovm in _slot_price_vs_orders.Values) {
|
|
lovm.update();
|
|
}
|
|
|
|
// complete removal of slots that were not updated
|
|
// orders_used_in_this_update.Keys are the price levels in the update
|
|
foreach (var slot_price in _slot_price_vs_orders.Keys.ToList()) {
|
|
if (!orders_used_in_this_update.Keys.Contains(slot_price)) {
|
|
_slot_price_vs_orders.TryGetValue(slot_price, out LadderOrderVM removal_candidate);
|
|
if (removal_candidate != null) {
|
|
removal_candidate.Dispose();
|
|
}
|
|
_slot_price_vs_orders.Remove(slot_price);
|
|
}
|
|
}
|
|
|
|
orders.sync_with(_slot_price_vs_orders.Values);
|
|
}),
|
|
WorkLoop.Start($"Ladder '{security}'", _tick),
|
|
Disposable.Create(() => _on_disposed_impl?.Invoke(true))
|
|
);
|
|
IDisposable get_order_properties_editor_hooks() {
|
|
if (order_properties_vm == null)
|
|
return Disposable.empty;
|
|
var position_src = order_properties_vm.observe_account(true)
|
|
.Select(a => a?.name ?? string.Empty)
|
|
.SelectManyInSeries(a => _model.order_gateway.on_security_positions(security.Id)
|
|
.Select(table => table.state.Values.FirstOrDefault(pos => pos.Item2.Account == a, tup => tup.Item2, (bts.api.position.PositionUnitImmutable?)null)));
|
|
return Disposable.Create(
|
|
settings.OnDispatcher().Subscribe(s => {
|
|
_sticky_algos.SetItems(s.favorite_algos.stickied_algos);
|
|
order_properties_vm.on_favorite_algos(s.favorite_algos);
|
|
}),
|
|
order_properties_vm,
|
|
position_src.OnDispatcher().Subscribe(pos => {
|
|
double total, day, raw_day;
|
|
if (pos.HasValue) {
|
|
total = pos.Value.total_pos();
|
|
day = pos.Value.day_pos();
|
|
raw_day = pos.Value.raw_day_pos();
|
|
} else {
|
|
total = day = raw_day = double.NaN;
|
|
}
|
|
if ((
|
|
Set(ref _position, total, nameof(position)) |
|
|
Set(ref _day_position, day, nameof(day_position)) |
|
|
Set(ref _raw_day_position, raw_day, nameof(raw_day_position))
|
|
) && (show_day_position || show_account_position || show_raw_day_position))
|
|
RaisePropertyChanged(nameof(position_display_text));
|
|
}));
|
|
}
|
|
}
|
|
|
|
public IBus bus => _bus;
|
|
|
|
public event Action on_tick, request_settings_dialog, settings_invalidated;
|
|
|
|
public MDConsumerHandle md => _model.md;
|
|
// we return only entries who have piqs and whose piqs are not uint.Max
|
|
// uint.Max happens when the server is restarted but the client is still up
|
|
// maybe long term we don't show this data while the exchange connection is down
|
|
public IEnumerable<(double, uint)> price_vs_piq => _slot_price_vs_orders.Values.Where(result => result.min_piq.HasValue && result.min_piq.Value != uint.MaxValue).Select(o => (o.price, o.min_piq.Value));
|
|
public bool is_synthetic => _model.security.ContractCase == Security.ContractOneofCase.Synthetic;
|
|
|
|
Property<string> _status;
|
|
public string status_message => _status.Value;
|
|
Property<Brush> _status_brush;
|
|
public Brush status_background => _status_brush.Value;
|
|
|
|
public ICommand position_click_command { get; }
|
|
public ICommand status_click_command => SparkCommands.ShowActivityLog.ICommand(_bus);
|
|
|
|
public async void status_update(Activity a) {
|
|
if (a.security == _model.security && a.category != ActivityLogCategory.modify) {
|
|
_status.set(a.message);
|
|
_status_brush.set(a.category.fill);
|
|
await Task.Delay(TimeSpan.FromSeconds(5));
|
|
if (_status.Value == a.message) {
|
|
_status.set("");
|
|
_status_brush.set(_transparent_status_brush);
|
|
}
|
|
}
|
|
}
|
|
|
|
event Action<bool> _on_disposed_impl;
|
|
event Action<bool> IWillLetYouKnowWhenIDispose.OnDisposed {
|
|
add => _on_disposed_impl += value;
|
|
remove => _on_disposed_impl -= value;
|
|
}
|
|
public SecurityImmutable security => _model.security;
|
|
public LadderSettings current_settings { get; private set; }
|
|
public double high => _high.Value;
|
|
public double low => _low.Value;
|
|
public double change => _change.Value;
|
|
public double open => _open.Value;
|
|
public double settlement => _settlement.Value;
|
|
public double position => _position;
|
|
public double day_position => _day_position;
|
|
public double raw_day_position => _raw_day_position;
|
|
public ulong volume => _volume.Value;
|
|
public bool auto_clear { get => _auto_clear.Value; set => _auto_clear.set(value); }
|
|
public CalculatorLogic size_calculator { get; } = new CalculatorLogic(0);
|
|
/// Important: <see cref="order_properties_vm"/> can be null.
|
|
public OrderPropertiesEditorVM order_properties_vm { get; }
|
|
public ObservableCollection<LadderOrderVM> orders { get; }
|
|
public IAsyncCommand cancel_command { get; }
|
|
public ICommand send_order_command { get; }
|
|
public ICommand show_book_command { get; }
|
|
public ICommand show_ticket_command { get; }
|
|
public ICommand settings_command { get; }
|
|
public Brush bid_background => _bid_background.view;
|
|
public Brush bid_foreground => _bid_foreground.view;
|
|
public Brush ask_background => _ask_background.view;
|
|
public Brush ask_foreground => _ask_foreground.view;
|
|
public bool show_day => _show_day.view;
|
|
public bool show_high => _show_high.view;
|
|
public bool show_low => _show_low.view;
|
|
public bool show_open => _show_open.view;
|
|
public bool show_settle => _show_settle.view;
|
|
public bool show_volume => _show_volume.view;
|
|
public bool show_account => _show_account.view;
|
|
public bool show_tif => _show_tif.view;
|
|
public bool show_order_type => _show_order_type.view;
|
|
public bool show_account_position => _show_account_position;
|
|
public bool show_day_position => _show_day_position;
|
|
public bool show_raw_day_position => _show_raw_day_position;
|
|
public bool show_size_preset => _show_size_preset.view;
|
|
public bool show_auto_clear => _show_auto_clear.view;
|
|
public bool show_clear => _show_clear.view;
|
|
public bool show_cancel_buys => _show_cancel_buys.view;
|
|
public bool show_cancel_sells => _show_cancel_sells.view;
|
|
public bool show_cancel_all => _show_cancel_all.view;
|
|
public bool show_zoom => _show_zoom.view;
|
|
public bool show_status_message => _show_status_message.view;
|
|
// left panel scroll props
|
|
public double left_panel_view_port_height {
|
|
get => _left_panel_view_port_height;
|
|
set {
|
|
using (detect_property_changes(nameof(left_panel_scroll_position)))
|
|
_left_panel_view_port_height = value;
|
|
}
|
|
}
|
|
public double left_panel_scroll_position {
|
|
get {
|
|
if (!_left_panel_scroll_position_target.is_nan()) {
|
|
if (_left_panel_scroll_position_target < _max_left_panel_scroll_position) {
|
|
_left_panel_scroll_position = _left_panel_scroll_position_target;
|
|
_left_panel_scroll_position_target = double.NaN;
|
|
}
|
|
}
|
|
return Math.Min(_left_panel_scroll_position, _left_panel_extent_height);
|
|
}
|
|
set => Set(ref _left_panel_scroll_position, value);
|
|
}
|
|
public double left_panel_extent_height {
|
|
get => _left_panel_extent_height;
|
|
set {
|
|
using (detect_property_changes(nameof(left_panel_scroll_position)))
|
|
Set(ref _left_panel_extent_height, value);
|
|
}
|
|
}
|
|
public double left_panel_width { get => _left_panel_width.Value; set => _left_panel_width.set(value); }
|
|
public double zoom_factor { get => _zoom_factor.Value; set => _zoom_factor.set(value); }
|
|
public string position_display_text {
|
|
get {
|
|
string rv = null;
|
|
if (show_day_position)
|
|
rv = SparkUtils.fmt_position(day_position);
|
|
if (show_raw_day_position) {
|
|
var raw_day_text = SparkUtils.fmt_position(raw_day_position);
|
|
rv = rv == null ? raw_day_text : $"{rv} / {raw_day_text}";
|
|
}
|
|
if (show_account_position) {
|
|
var acc_text = SparkUtils.fmt_position(position);
|
|
rv = rv == null ? SparkUtils.fmt_position(position) : $"{rv} ({acc_text})";
|
|
}
|
|
return rv ?? "";
|
|
}
|
|
}
|
|
//public double window_height { get => _window_height.Value; set => _window_height.set(value); }
|
|
//public double window_width { get => _window_width.Value; set => _window_width.set(value); }
|
|
public uint orders_column_width { get => _orders_column_width.Value; set => _orders_column_width.set(value); }
|
|
public uint bid_column_width { get => _bid_column_width.Value; set => _bid_column_width.set(value); }
|
|
public uint price_column_width { get => _price_column_width.Value; set => _price_column_width.set(value); }
|
|
public uint ask_column_width { get => _ask_column_width.Value; set => _ask_column_width.set(value); }
|
|
public uint last_column_width { get => _last_column_width.Value; set => _last_column_width.set(value); }
|
|
public uint piq_column_width { get => _piq_column_width.Value; set => _piq_column_width.set(value); }
|
|
public uint volume_column_width { get => _volume_column_width.Value; set => _volume_column_width.set(value); }
|
|
public uint alternate_order_size_from_settings => current_settings.alternate_order_size;
|
|
public bool show_warn_on_cross_from_settings => current_settings.show_warn_on_cross;
|
|
public bool enforce_max_clip_from_settings => current_settings.enforce_max_clip;
|
|
public uint max_clip_from_settings => current_settings.max_clip;
|
|
internal LadderModel model => _model;
|
|
internal IObservable<LadderSettings> settings { get; }
|
|
|
|
double _max_left_panel_scroll_position =>
|
|
Math.Max(0, _left_panel_extent_height - _left_panel_view_port_height);
|
|
|
|
internal OrderRecordImmutable get_root_at_price((OrderRecordImmutable order, uint? _, AlgoData? __, AlgoDefinitionMetadata? meta) op) {
|
|
var order = op.order;
|
|
double p = order.price();
|
|
foreach (var o in lineage(order.ParentKey)) {
|
|
if (_equality.Equals(p, o.price()))
|
|
order = o;
|
|
}
|
|
return order;
|
|
}
|
|
|
|
internal IEnumerable<OrderRecordImmutable> lineage(uint key) {
|
|
while (key != 0 && _cache.TryGetValue(key, out var op)) {
|
|
yield return op.order;
|
|
key = op.order.ParentKey;
|
|
}
|
|
}
|
|
|
|
public LadderSettings get_save_state() {
|
|
var rv = current_settings.with_window_specifics(
|
|
new LadderSettings.WindowSpecifics(
|
|
left_panel_width,
|
|
orders_column_width,
|
|
bid_column_width,
|
|
price_column_width,
|
|
ask_column_width,
|
|
last_column_width,
|
|
volume_column_width,
|
|
zoom_factor,
|
|
piq_column_width));
|
|
rv.auto_clear = auto_clear;
|
|
rv.left_panel_scroll_position = left_panel_scroll_position;
|
|
if (order_properties_vm != null) {
|
|
rv.default_time_in_force = order_properties_vm.selected_time_in_force;
|
|
if (order_properties_vm.selected_account.HasValue)
|
|
rv.default_account = order_properties_vm.selected_account.Value.id;
|
|
if (order_properties_vm.selected_algo_definition != null)
|
|
rv.default_algo = AlgoID.from_algo_key(order_properties_vm.selected_algo_definition.key);
|
|
}
|
|
return rv;
|
|
}
|
|
|
|
public void key_down(KeyEventArgs e) {
|
|
if (order_properties_vm == null)
|
|
return;
|
|
if (KeyboardExtensions.IsDown(Key.Space)) {
|
|
_bus?.post(SparkTags.RequestRecenterLadder, null);
|
|
}
|
|
// the ctrl modifier requirement helps avoid keyboard-input collisions w/ algo-templates popup
|
|
if (!KeyboardExtensions.IsCtrlDown()) return;
|
|
var new_algo = order_properties_vm.algo_definitions.FirstOrDefault(o => o.algo_root.key_match(e));
|
|
if (new_algo != null) {
|
|
order_properties_vm.selected_algo_definition = new_algo;
|
|
e.Handled = true;
|
|
}
|
|
}
|
|
|
|
public void on_mini_orderbook_closed(bts.shell.orderbook.OrderBookStartupData ob_state) {
|
|
current_settings = SparkUtils.clone_proto(current_settings);
|
|
current_settings.mini_ob = new LadderMiniOrderBookSettings(ob_state.orderbook_grid.general);
|
|
settings_invalidated?.Invoke();
|
|
}
|
|
|
|
public void recenter_all_ladders() {
|
|
_bus?.post(SparkTags.RequestRecenterLadder, null);
|
|
}
|
|
|
|
public Task send_order_async((double price, Side? side, MouseButton button) args) {
|
|
if (order_properties_vm != null && args.button != MouseButton.Middle) {
|
|
var size_from_alternate = args.button == MouseButton.Right;
|
|
if (!DoubleHelper.IsNaNOrInfinity(args.price) && args.side.HasValue && !HasErrors && !order_properties_vm.HasErrors && (!size_from_alternate || current_settings.alternate_order_size > 0)) {
|
|
if (!size_from_alternate && (!size_calculator.value.HasValue || size_calculator.value.Value == 0)) {
|
|
size_calculator.flash_error();
|
|
} else {
|
|
uint size = size_from_alternate ? current_settings.alternate_order_size : (uint)Math.Abs(size_calculator.value ?? 0);
|
|
// enforce max clip
|
|
if (enforce_max_clip_from_settings && size > max_clip_from_settings) {
|
|
size = max_clip_from_settings;
|
|
}
|
|
return send_order_async(args.side.Value, size, args.price);
|
|
}
|
|
}
|
|
}
|
|
return Task.CompletedTask;
|
|
}
|
|
|
|
public async Task send_order_async(Side side, uint size, double price) {
|
|
var order = order_properties_vm.create(side);
|
|
if (order.CustomDef != null) {
|
|
if (order.CustomDef.Stop != null) {
|
|
order.CustomDef.Stop.StopPrice = price;
|
|
} else if (order.CustomDef.StopLimit != null) {
|
|
// swap the prices here so that the level clicked
|
|
// in the ladder is the StopPrice (trigger) and the Price
|
|
// is what was specificed in the extra parameter box
|
|
double temp_price = order.CustomDef.StopLimit.StopPrice;
|
|
order.CustomDef.StopLimit.StopPrice = price;
|
|
price = temp_price;
|
|
}
|
|
}
|
|
await _model.order_gateway.send_order(security, order, price, size);
|
|
if (auto_clear)
|
|
size_calculator.value = 0;
|
|
var fav_algo = order_properties_vm.selected_favorite_algo_definition;
|
|
if (fav_algo != null && !_sticky_algos.Contains(fav_algo.key) && !order_properties_vm.try_pop_selected_algo_definition()) {
|
|
var key = new AlgoKey(OrderDataHelper.get_default_algo(OrderDataHelper.get_algo_format(security.ContractCase)));
|
|
if (order_properties_vm.try_get_algo_definition(key, out var r)) {
|
|
order_properties_vm.selected_algo_definition = r;
|
|
}
|
|
}
|
|
}
|
|
|
|
void _update_header() {
|
|
bool last_updated = md.get_last_trade(ref _last_trade_time, out NativePriceSize last, out DateTime time, out ulong volume);
|
|
if (_misc_time != _model.md.misc_timestamp || last_updated) {
|
|
_high.set(md.session_high);
|
|
_low.set(md.session_low);
|
|
_open.set(md.open_price);
|
|
_settlement.set(md.settlement_price);
|
|
if (!double.IsNaN(last.price)) {
|
|
_change.set(last.price - md.settlement_price);
|
|
} else {
|
|
ulong unused = 0;
|
|
_model.md.get_bbo(ref unused, out NativePriceSize bid, out NativePriceSize ask);
|
|
double mid = TickUtils.mid_tick(security.ContractSpec, bid.price, ask.price);
|
|
_change.set(mid - md.settlement_price);
|
|
}
|
|
_volume.set(volume);
|
|
_misc_time = _model.md.misc_timestamp;
|
|
}
|
|
|
|
}
|
|
|
|
void _show_book() {
|
|
var ob_settings = bts.shell.orderbook.OrderBookStartupData.Default;
|
|
ob_settings.filters = new bts.shell.orderbook.FilterStates {
|
|
security_criteria = new SecurityCriteria(security)
|
|
};
|
|
_bus.post(SparkTags.ShowOrderBookTag, ob_settings);
|
|
}
|
|
|
|
void _show_ticket() {
|
|
if (order_properties_vm != null)
|
|
_bus.post(SparkTags.ShowOrderTicketTag, (new OrderDefinitionImmutable(order_properties_vm.create(Side.Buy)), 0d, 0u));
|
|
}
|
|
|
|
void _tick() {
|
|
_update_header();
|
|
on_tick?.Invoke();
|
|
}
|
|
}
|
|
}
|