refactor: Replace ShowOrderTicket tag's anon tuple w/ proper type
This commit is contained in:
1 parent
328dd9b2d3
commit
29ca6e98fb
6 files changed
+180
-157
No files matched your search
@@ -1,139 +1,140 @@
|
||||
using bts.api;
|
||||
using bts.api.core;
|
||||
using bts.api.orderman;
|
||||
using bts.spark;
|
||||
|
||||
using mbts;
|
||||
using mbts.config;
|
||||
using mbts.persist;
|
||||
using mbts.spark;
|
||||
using mbts.ui;
|
||||
using mbts.ui.spark;
|
||||
|
||||
using Spark.Api.Orders;
|
||||
using Spark.Core.Disposables;
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.Immutable;
|
||||
using System.Composition;
|
||||
using System.Reactive.Linq;
|
||||
using System.Windows;
|
||||
|
||||
using LadderSettings = mbts.spark.LadderSettings;
|
||||
|
||||
namespace bts.shell {
|
||||
[Export(typeof(IShellDaemon))]
|
||||
class OrderTicketDaemon : IShellDaemon {
|
||||
readonly IBus _bus;
|
||||
readonly OrdersModel _model;
|
||||
readonly UserSettingsHub _settings_hub;
|
||||
readonly ErrorReporter _error_reporter;
|
||||
readonly bool _close_on_send;
|
||||
IReadOnlyDictionary<uint, SecurityMD> _md_map;
|
||||
TableUpdate<(SecurityImmutable? sec, OrderRecordImmutable order), uint> _orders;
|
||||
IDisposable _dispose;
|
||||
|
||||
[ImportingConstructor]
|
||||
public OrderTicketDaemon(IBus bus, OrdersModel model, UserSettingsHub settings_hub, ErrorReporter error_reporter)
|
||||
: this(bus, model, settings_hub, error_reporter, Configuration.get<bts.config.OrderTicketOptions>().close_on_send) { }
|
||||
|
||||
public OrderTicketDaemon(IBus bus, OrdersModel model, UserSettingsHub settings_hub, ErrorReporter error_reporter, bool close_on_send) {
|
||||
_bus = bus;
|
||||
_model = model;
|
||||
_settings_hub = settings_hub;
|
||||
_error_reporter = error_reporter;
|
||||
_close_on_send = close_on_send;
|
||||
_dispose = Disposable.Create(
|
||||
model.on_orders.OnDispatcher().Subscribe(o => _orders = o),
|
||||
bus.subscribe(SparkTags.SecuritiesTag).OnDispatcher().Subscribe(m => _md_map = m),
|
||||
bus.subscribe(SparkTags.ShowOrderTicketFromSecurityTag).OnDispatcher().Subscribe(_show_order_ticket),
|
||||
bus.subscribe(SparkTags.ShowOrderTicketTag).OnDispatcher().Subscribe(_show_order_ticket),
|
||||
bus.subscribe(SparkTags.ShowOrderTicketAutoLookupTag).Subscribe(_show_order_tickets),
|
||||
bus.subscribe(SparkTags.ShowModifyOrderTag).OnDispatcher().Subscribe(_show_modify_order)
|
||||
);
|
||||
}
|
||||
|
||||
public void Dispose() =>
|
||||
Disposable.dispose(ref _dispose);
|
||||
|
||||
void _show_order_window(Window window) {
|
||||
foreach (var kv in AlgoStudioModelUtils.get_dynamic_resources(_model.algo_model.ui_configuration))
|
||||
window.Resources[kv.Key] = kv.Value;
|
||||
// the dynamic algo 'SecurityKey' template has a ContractPickerPopup that expects the security map to be available as a DynamicResource.
|
||||
var sub = _bus.subscribe(SparkTags.SecuritiesTag)
|
||||
.OnDispatcher()
|
||||
.Subscribe(sec_map => window.Resources[ResourceKeys.Securities] = sec_map);
|
||||
window.Closed += (s, e) => Disposable.dispose(ref sub);
|
||||
window.Show(-200, -160, true);
|
||||
}
|
||||
|
||||
void _show_order_ticket(SecurityMD smd) {
|
||||
var settings_src = _settings_hub.subscribe_default_settings()
|
||||
.Select(s => s.get_ladder_profile(smd.security)?.value ?? LadderSettings.Default);
|
||||
var accounts_src = _bus.subscribe(SparkTags.AccountsTag);
|
||||
_show_order_window(new OrderTicket {
|
||||
DataContext = new NewOrderTicketVM(_model, smd, accounts_src, settings_src) {
|
||||
close_on_send = _close_on_send
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
void _show_order_ticket((OrderDefinitionImmutable def, double price, uint size) ops) {
|
||||
var (def, price, size) = ops;
|
||||
if (!_md_map.TryGetValue(def.SecurityKey, out SecurityMD smd)) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (def.Account.is_null_or_empty() && def.AccountId == 0) {
|
||||
var settings = _settings_hub.current.get_ladder_profile(smd.security);
|
||||
var default_account = settings?.value?.default_account;
|
||||
if (default_account.HasValue) {
|
||||
var mdef = def.CloneToMutable();
|
||||
// The order ticket will resolve AccountId to a name
|
||||
mdef.AccountId = default_account.Value;
|
||||
def = new(mdef);
|
||||
}
|
||||
}
|
||||
|
||||
var accounts = _bus.subscribe(SparkTags.AccountsTag);
|
||||
var calc_buttons = Observable.Return(LadderSettings.Default.calculator_buttons);
|
||||
_show_order_window(new OrderTicket {
|
||||
DataContext = new NewOrderTicketVM(
|
||||
_model, smd, accounts, def, price, size, calc_buttons) {
|
||||
close_on_send = _close_on_send
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
void _show_order_tickets(IImmutableSet<uint> securities) {
|
||||
foreach (var id in securities.empty_if_null()) {
|
||||
if (_md_map.TryGetValue(id, out var smd))
|
||||
_show_order_ticket(smd);
|
||||
}
|
||||
}
|
||||
|
||||
void _show_modify_order(uint order_key) {
|
||||
if (_orders.state.TryGetValue(order_key, out var ori) && ori.sec.HasValue
|
||||
&& (ori.order.HasDef && ori.order.Def.HasCustomDef && !(ori.order.Def.CustomDef.HasStop || ori.order.Def.CustomDef.HasStopLimit))) {
|
||||
var md = _md_map.TryGetValue(ori.order.security_key());
|
||||
_show_order_window(new OrderModificationDialog {
|
||||
DataContext = new ExistingOrderTicketVM(_model,
|
||||
Observable.Return((md, ori.order)),
|
||||
_bus.subscribe(SparkTags.AccountsTag),
|
||||
Observable.Return(LadderSettings.Default.calculator_buttons)
|
||||
)
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
LadderSettings _get_settings(SecurityImmutable sec) {
|
||||
var settings = _settings_hub.current;
|
||||
if (settings.ladder_profiles.TryGetFirst(
|
||||
lsp => lsp.security_criteria.includes(sec), out var lsp)) {
|
||||
return lsp.value;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
using bts.api;
|
||||
using bts.api.core;
|
||||
using bts.api.orderman;
|
||||
using bts.spark;
|
||||
|
||||
using mbts;
|
||||
using mbts.config;
|
||||
using mbts.persist;
|
||||
using mbts.spark;
|
||||
using mbts.ui;
|
||||
using mbts.ui.spark;
|
||||
|
||||
using Spark.Api.Orders;
|
||||
using Spark.Core.Disposables;
|
||||
using Spark.Orders;
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.Immutable;
|
||||
using System.Composition;
|
||||
using System.Reactive.Linq;
|
||||
using System.Windows;
|
||||
|
||||
using LadderSettings = mbts.spark.LadderSettings;
|
||||
|
||||
namespace bts.shell {
|
||||
[Export(typeof(IShellDaemon))]
|
||||
class OrderTicketDaemon : IShellDaemon {
|
||||
readonly IBus _bus;
|
||||
readonly OrdersModel _model;
|
||||
readonly UserSettingsHub _settings_hub;
|
||||
readonly ErrorReporter _error_reporter;
|
||||
readonly bool _close_on_send;
|
||||
IReadOnlyDictionary<uint, SecurityMD> _md_map;
|
||||
TableUpdate<(SecurityImmutable? sec, OrderRecordImmutable order), uint> _orders;
|
||||
IDisposable _dispose;
|
||||
|
||||
[ImportingConstructor]
|
||||
public OrderTicketDaemon(IBus bus, OrdersModel model, UserSettingsHub settings_hub, ErrorReporter error_reporter)
|
||||
: this(bus, model, settings_hub, error_reporter, Configuration.get<bts.config.OrderTicketOptions>().close_on_send) { }
|
||||
|
||||
public OrderTicketDaemon(IBus bus, OrdersModel model, UserSettingsHub settings_hub, ErrorReporter error_reporter, bool close_on_send) {
|
||||
_bus = bus;
|
||||
_model = model;
|
||||
_settings_hub = settings_hub;
|
||||
_error_reporter = error_reporter;
|
||||
_close_on_send = close_on_send;
|
||||
_dispose = Disposable.Create(
|
||||
model.on_orders.OnDispatcher().Subscribe(o => _orders = o),
|
||||
bus.subscribe(SparkTags.SecuritiesTag).OnDispatcher().Subscribe(m => _md_map = m),
|
||||
bus.subscribe(SparkTags.ShowOrderTicketFromSecurityTag).OnDispatcher().Subscribe(_show_order_ticket),
|
||||
bus.subscribe(SparkTags.ShowOrderTicketTag).OnDispatcher().Subscribe(_show_order_ticket),
|
||||
bus.subscribe(SparkTags.ShowOrderTicketAutoLookupTag).Subscribe(_show_order_tickets),
|
||||
bus.subscribe(SparkTags.ShowModifyOrderTag).OnDispatcher().Subscribe(_show_modify_order)
|
||||
);
|
||||
}
|
||||
|
||||
public void Dispose() =>
|
||||
Disposable.dispose(ref _dispose);
|
||||
|
||||
void _show_order_window(Window window) {
|
||||
foreach (var kv in AlgoStudioModelUtils.get_dynamic_resources(_model.algo_model.ui_configuration))
|
||||
window.Resources[kv.Key] = kv.Value;
|
||||
// the dynamic algo 'SecurityKey' template has a ContractPickerPopup that expects the security map to be available as a DynamicResource.
|
||||
var sub = _bus.subscribe(SparkTags.SecuritiesTag)
|
||||
.OnDispatcher()
|
||||
.Subscribe(sec_map => window.Resources[ResourceKeys.Securities] = sec_map);
|
||||
window.Closed += (s, e) => Disposable.dispose(ref sub);
|
||||
window.Show(-200, -160, true);
|
||||
}
|
||||
|
||||
void _show_order_ticket(SecurityMD smd) {
|
||||
var settings_src = _settings_hub.subscribe_default_settings()
|
||||
.Select(s => s.get_ladder_profile(smd.security)?.value ?? LadderSettings.Default);
|
||||
var accounts_src = _bus.subscribe(SparkTags.AccountsTag);
|
||||
_show_order_window(new OrderTicket {
|
||||
DataContext = new NewOrderTicketVM(_model, smd, accounts_src, settings_src) {
|
||||
close_on_send = _close_on_send
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
void _show_order_ticket(OrderTicketParams ops) {
|
||||
var def = ops.definition;
|
||||
if (!_md_map.TryGetValue(def.SecurityKey, out SecurityMD smd)) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (def.Account.is_null_or_empty() && def.AccountId == 0) {
|
||||
var settings = _settings_hub.current.get_ladder_profile(smd.security);
|
||||
var default_account = settings?.value?.default_account;
|
||||
if (default_account.HasValue) {
|
||||
var mdef = def.CloneToMutable();
|
||||
// The order ticket will resolve AccountId to a name
|
||||
mdef.AccountId = default_account.Value;
|
||||
def = new(mdef);
|
||||
}
|
||||
}
|
||||
|
||||
var accounts = _bus.subscribe(SparkTags.AccountsTag);
|
||||
var calc_buttons = Observable.Return(LadderSettings.Default.calculator_buttons);
|
||||
_show_order_window(new OrderTicket {
|
||||
DataContext = new NewOrderTicketVM(
|
||||
_model, smd, accounts, def, ops.price, ops.quantity, calc_buttons) {
|
||||
close_on_send = _close_on_send
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
void _show_order_tickets(IImmutableSet<uint> securities) {
|
||||
foreach (var id in securities.empty_if_null()) {
|
||||
if (_md_map.TryGetValue(id, out var smd))
|
||||
_show_order_ticket(smd);
|
||||
}
|
||||
}
|
||||
|
||||
void _show_modify_order(uint order_key) {
|
||||
if (_orders.state.TryGetValue(order_key, out var ori) && ori.sec.HasValue
|
||||
&& (ori.order.HasDef && ori.order.Def.HasCustomDef && !(ori.order.Def.CustomDef.HasStop || ori.order.Def.CustomDef.HasStopLimit))) {
|
||||
var md = _md_map.TryGetValue(ori.order.security_key());
|
||||
_show_order_window(new OrderModificationDialog {
|
||||
DataContext = new ExistingOrderTicketVM(_model,
|
||||
Observable.Return((md, ori.order)),
|
||||
_bus.subscribe(SparkTags.AccountsTag),
|
||||
Observable.Return(LadderSettings.Default.calculator_buttons)
|
||||
)
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
LadderSettings _get_settings(SecurityImmutable sec) {
|
||||
var settings = _settings_hub.current;
|
||||
if (settings.ladder_profiles.TryGetFirst(
|
||||
lsp => lsp.security_criteria.includes(sec), out var lsp)) {
|
||||
return lsp.value;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -18,6 +18,7 @@
|
||||
using Spark.Api.Position;
|
||||
using Spark.Core;
|
||||
using Spark.Core.Disposables;
|
||||
using Spark.Orders;
|
||||
using Spark.Trading;
|
||||
|
||||
using System;
|
||||
@@ -391,39 +392,41 @@
|
||||
}
|
||||
|
||||
void _show_order_ticket(MarketViewColumnType col) {
|
||||
var order = _orders_model.factory.order(security);
|
||||
switch (col) {
|
||||
case MarketViewColumnType.Bid:
|
||||
_bus.post(SparkTags.ShowOrderTicketTag, (order.with(side: Side.Sell), bid_price, 0u));
|
||||
_bus.post(SparkTags.ShowOrderTicketTag, get_params(bid_price));
|
||||
break;
|
||||
case MarketViewColumnType.BidMergedQty:
|
||||
_bus.post(SparkTags.ShowOrderTicketTag, (order.with(side: Side.Sell), bid_price, bid_qty));
|
||||
_bus.post(SparkTags.ShowOrderTicketTag, get_params(bid_price, bid_qty));
|
||||
break;
|
||||
case MarketViewColumnType.BidDirectQty:
|
||||
_bus.post(SparkTags.ShowOrderTicketTag, (order.with(side: Side.Sell), bid_price, dbid_qty));
|
||||
_bus.post(SparkTags.ShowOrderTicketTag, get_params(bid_price, dbid_qty));
|
||||
break;
|
||||
case MarketViewColumnType.BidImpliedQty:
|
||||
_bus.post(SparkTags.ShowOrderTicketTag, (order.with(side: Side.Sell), bid_price, ibid_qty));
|
||||
_bus.post(SparkTags.ShowOrderTicketTag, get_params(bid_price, ibid_qty));
|
||||
break;
|
||||
case MarketViewColumnType.Ask:
|
||||
_bus.post(SparkTags.ShowOrderTicketTag, (order.with(side: Side.Buy), ask_price, 0u));
|
||||
_bus.post(SparkTags.ShowOrderTicketTag, get_params(ask_price));
|
||||
break;
|
||||
case MarketViewColumnType.AskMergedQty:
|
||||
_bus.post(SparkTags.ShowOrderTicketTag, (order.with(side: Side.Buy), ask_price, ask_qty));
|
||||
_bus.post(SparkTags.ShowOrderTicketTag, get_params(ask_price, ask_qty));
|
||||
break;
|
||||
case MarketViewColumnType.AskDirectQty:
|
||||
_bus.post(SparkTags.ShowOrderTicketTag, (order.with(side: Side.Sell), ask_price, dask_qty));
|
||||
_bus.post(SparkTags.ShowOrderTicketTag, get_params(ask_price, dask_qty));
|
||||
break;
|
||||
case MarketViewColumnType.AskImpliedQty:
|
||||
_bus.post(SparkTags.ShowOrderTicketTag, (order.with(side: Side.Sell), ask_price, iask_qty));
|
||||
_bus.post(SparkTags.ShowOrderTicketTag, get_params(ask_price, iask_qty));
|
||||
break;
|
||||
case MarketViewColumnType.Last:
|
||||
_bus.post(SparkTags.ShowOrderTicketTag, (order, last_price, 0u));
|
||||
_bus.post(SparkTags.ShowOrderTicketTag, get_params(last_price));
|
||||
break;
|
||||
case MarketViewColumnType.LastSize:
|
||||
_bus.post(SparkTags.ShowOrderTicketTag, (order, last_price, last_qty));
|
||||
_bus.post(SparkTags.ShowOrderTicketTag, get_params(last_price, last_qty));
|
||||
break;
|
||||
}
|
||||
|
||||
OrderTicketParams get_params(double price, double quantity = 0) =>
|
||||
new(new(new() {SecurityKey = security_key }), price, quantity);
|
||||
}
|
||||
|
||||
void _refresh_orders(ref bool dirty) {
|
||||
|
||||
@@ -882,8 +882,9 @@ namespace mbts.spark {
|
||||
if (order_properties_vm is null) {
|
||||
return;
|
||||
}
|
||||
_bus.post(SparkTags.ShowOrderTicketTag,
|
||||
(new OrderDefinitionImmutable(order_properties_vm.create(Side.Buy)), 0d, 0u));
|
||||
_bus.post(
|
||||
SparkTags.ShowOrderTicketTag,
|
||||
new(new(order_properties_vm.create(Side.Buy)), 0d, 0u));
|
||||
}
|
||||
|
||||
void _tick() {
|
||||
|
||||
@@ -0,0 +1,20 @@
|
||||
#nullable enable
|
||||
namespace Spark.Orders {
|
||||
using bts.api.orderman;
|
||||
|
||||
class OrderTicketParams {
|
||||
public OrderTicketParams(
|
||||
OrderDefinitionImmutable? definition = null,
|
||||
double price = double.NaN,
|
||||
double quantity = 0
|
||||
) {
|
||||
this.definition = definition ?? new(new());
|
||||
this.price = price;
|
||||
this.quantity = quantity;
|
||||
}
|
||||
|
||||
public OrderDefinitionImmutable definition { get; }
|
||||
public double price { get; }
|
||||
public double quantity { get; }
|
||||
}
|
||||
}
|
||||
@@ -89,9 +89,6 @@ namespace bts.spark {
|
||||
public static PartialCommand<IImmutableSet<uint>> ShowOrderTicketAutoLookup(KeyGesture key_gesture = null) =>
|
||||
SparkCommand.create("Order Ticket", Icons.AddOrders, SparkTags.ShowOrderTicketAutoLookupTag, key_gesture);
|
||||
|
||||
public static PartialCommand<(OrderDefinitionImmutable def, double price, uint size)> ShowOrderTicket(KeyGesture key_gesture = null) =>
|
||||
SparkCommand.create("Order Ticket", Icons.AddOrders, SparkTags.ShowOrderTicketTag, key_gesture);
|
||||
|
||||
public static PartialCommand<SecurityMD> ShowOrderTicketFromSecurity(KeyGesture key_gesture = null) =>
|
||||
SparkCommand.create("Order Ticket", Icons.AddOrders, SparkTags.ShowOrderTicketFromSecurityTag, key_gesture);
|
||||
|
||||
|
||||
@@ -14,6 +14,7 @@ namespace mbts.spark {
|
||||
using mbts.ui;
|
||||
using mbts.ui.controls;
|
||||
|
||||
using Spark.Orders;
|
||||
using Spark.Trading;
|
||||
|
||||
using System;
|
||||
@@ -61,8 +62,8 @@ namespace mbts.spark {
|
||||
public static ITag<IImmutableSet<uint>> ShowOrderTicketAutoLookupTag { get; } = _mk_tag<IImmutableSet<uint>>();
|
||||
public static ITag<SecurityMD> ShowOrderTicketFromSecurityTag { get; } = _mk_tag<SecurityMD>();
|
||||
|
||||
public static ITag<(OrderDefinitionImmutable def, double price, uint size)> ShowOrderTicketTag { get; } =
|
||||
_mk_tag<(OrderDefinitionImmutable def, double price, uint size)>();
|
||||
public static ITag<OrderTicketParams> ShowOrderTicketTag { get; } =
|
||||
_mk_tag<OrderTicketParams>();
|
||||
public static ITag<RFQRequestImmutable> RFQSend { get; } = _mk_tag<RFQRequestImmutable>();
|
||||
public static ITag<Tuple<RFQRequestSecurityImmutable, Point>> RFQDialog { get; } =
|
||||
_mk_tag<Tuple<RFQRequestSecurityImmutable, Point>>();
|
||||
|
||||
Reference in new issue
Block a user