restricting orders with piqs to just ladders for now

This commit is contained in:
Jake Norbut committed 2019-04-17 17:33:46 -05:00
1 parent 6692797c3f
commit ff2d150dc4
7 files changed
+75 -29

No files matched your search

@@ -19,7 +19,7 @@ namespace bts.shell {
readonly OrdersModel _model;
readonly ErrorReporter _error_reporter;
IReadOnlyDictionary<uint, SecurityMD> _md_map;
TableUpdate<OrderSecurity, uint> _orders;
TableUpdate<(SecurityImmutable? sec, OrderRecordImmutable order), uint> _orders;
IDisposable _dispose;
[ImportingConstructor]
@@ -60,13 +60,13 @@ namespace bts.shell {
}
void _show_modify_order(uint order_key) {
if (_orders.state.TryGetValue(order_key, out var ori) && ori.security.HasValue
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)),
Observable.Return(_model.factory.default_ladder_settings(ori.security.Value).calculator_buttons)
Observable.Return(_model.factory.default_ladder_settings(ori.sec.Value).calculator_buttons)
)
});
}
@@ -55,7 +55,7 @@ namespace bts.shell {
void _on_activity(Activity activity) =>
_activity_notifications.process_item(activity);
void _on_orders(TableUpdate<OrderSecurity,uint> update) {
void _on_orders(TableUpdate<(SecurityImmutable? sec, OrderRecordImmutable order), uint> update) {
// must update notification_service_data_cache first, some of the predicate/triggers (esp. ToastNotifications) will use the notification_service_data_cache to lookup order-history
_data_cache.update(update.as_iincremental());
foreach (var o in update.added.Concat(update.changed))
@@ -50,7 +50,7 @@ namespace bts.shell {
public DateTime last_order_reset_time { get; private set; }
public bool is_first_order_update { get; private set; }
internal void update(IIncrementalUpdate<OrderSecurity, uint> data) {
internal void update(IIncrementalUpdate<(SecurityImmutable? sec, OrderRecordImmutable order), uint> data) {
DateTime now = DateTime.Now;
if (data.removed.Count == _order_cache.Count) {
last_order_reset_time = now;
@@ -27,7 +27,7 @@ namespace bts.spark {
static readonly IEqualityComparer<OrderRecordImmutable> _id_comparer =
bts.utils.EqualityComparer.Create<OrderRecordImmutable, uint>(a => a.Key);
public OrderActivitySummarizer(IBus bus, IObservable<TableUpdate<OrderSecurity, uint>> orders) {
public OrderActivitySummarizer(IBus bus, IObservable<TableUpdate<(SecurityImmutable? sec, OrderRecordImmutable order), uint>> orders) {
_bus = bus;
_dispose = Disposable.Create(
bus.subscribe(SparkTags.SecuritiesTag).Subscribe(sec => _security = sec),
@@ -124,12 +124,12 @@ namespace bts.spark {
#endregion
void orders_updated(TableUpdate<OrderSecurity, uint> update) {
void orders_updated(TableUpdate<(SecurityImmutable? sec, OrderRecordImmutable order), uint> update) {
foreach (var o in update.added) {
if (!string.IsNullOrEmpty(o.order.State.LastMsg) && o.order.State.Created.ToDateTime().Date == DateTime.Today) {
if (o.security.HasValue) {
log_status(ActivityLogCategory.warn, o.security.Value, o.order.State.LastMsg);
if (o.sec.HasValue) {
log_status(ActivityLogCategory.warn, o.sec.Value, o.order.State.LastMsg);
}
}
}
@@ -137,7 +137,7 @@ namespace bts.spark {
foreach (var order in update.changes) {
var neworder = order.New.order;
var oldorder = order.Old.order;
var security = order.New.security ?? order.Old.security;
var security = order.New.sec ?? order.Old.sec;
//
// description
var mod_desc = modified_description(oldorder, neworder, security?.ContractSpec);
+16 -5
View File
@@ -4,16 +4,27 @@ using System;
namespace bts.api.orderman {
public readonly struct OrderSecurity: IEquatable<OrderSecurity> {
public OrderSecurity(SecurityImmutable? sec, OrderRecordImmutable order) {
this.security = sec;
this.order = order;
piq = null;
}
public OrderSecurity((SecurityImmutable? sec, OrderRecordImmutable order) t) {
security = t.sec;
order = t.order;
piq = null;
}
public OrderSecurity(SecurityImmutable? sec, OrderRecordImmutable order, uint? piq) {
this.security = sec;
this.order = order;
this.piq = piq;
}
public OrderSecurity(OrderSecurity t) {
security = t.security;
order = t.order;
piq = t.piq;
}
//public OrderSecurity(OrderSecurity t) {
// security = t.security;
// order = t.order;
// piq = t.piq;
//}
public static implicit operator OrderSecurity(ValueTuple<SecurityImmutable?, OrderRecordImmutable> x) => new OrderSecurity(x);
public SecurityImmutable? security { get; }
public OrderRecordImmutable order { get; }
public uint? piq { get; }
+48 -13
View File
@@ -20,7 +20,8 @@ using System.Reactive.Concurrency;
using System.Reactive.Linq;
using System.Reactive.Subjects;
using System.Threading.Tasks;
using OrderUpdate = bts.api.core.TableUpdate<bts.api.orderman.OrderSecurity, uint>;
using OrderUpdate = bts.api.core.TableUpdate<(bts.api.SecurityImmutable? sec, bts.api.orderman.OrderRecordImmutable order), uint>;
using OrderUpdatePiq = bts.api.core.TableUpdate<bts.api.orderman.OrderSecurity, uint>;
namespace mbts.spark {
[Export]
@@ -54,7 +55,7 @@ namespace mbts.spark {
this.algo_model = algo_model;
var scheduler = ThreadPoolScheduler.Instance;
var interval = TimeSpan.FromMilliseconds(100);
var pre_on_orders =
on_orders =
_service
.subscribei_orders()
.Naggle(interval, scheduler)
@@ -62,8 +63,9 @@ namespace mbts.spark {
.join(sstore, order => order.security_key())
.Table()
.Cache();
if (_piq_enabled) {
on_orders = pre_on_orders
on_orders_with_piq = on_orders
.LeftJoin<
(SecurityImmutable?, OrderRecordImmutable),
OrderPIQImmutable,
@@ -75,9 +77,9 @@ namespace mbts.spark {
(si_ort, r, p) => new OrderSecurity(si_ort.Item1, si_ort.Item2, p.Position),
(si_ort, r) => new OrderSecurity(si_ort.Item1, si_ort.Item2, null),
osp => osp.order.Key);
} else {
on_orders = pre_on_orders.Select((ft) => new OrderSecurity(ft.Item1, ft.Item2, null), os => os.order.Key)
on_orders_with_piq = on_orders
.Select((ft) => new OrderSecurity(ft.Item1, ft.Item2, null), os => os.order.Key)
.Replay(1);
}
@@ -88,11 +90,18 @@ namespace mbts.spark {
(workers, table) => _update_working_orders(workers, table))
.Replay(1).RefCount();
working_orders_with_piq =
on_orders_with_piq.Scan(
ImmutableDictionary<uint, OrderSecurity>.Empty,
(workers, table) => _update_working_orders(workers, table))
.Replay(1).RefCount();
on_order_securities =
on_orders
.as_iincremental()
.filter(t => t.security.HasValue)
.Select(t => t.security.Value);
.filter(t => t.sec.HasValue)
.Select(t => t.sec.Value);
_activity = new OrderActivitySummarizer(bus, on_orders);
_security_orders_demuxer = new TableUpdateDemuxer<OrderRecordImmutable, uint, uint>(o => o.Key, o => o.security_key());
@@ -109,8 +118,8 @@ namespace mbts.spark {
t => t.order.Key,
am => am.algo.key,
t => new AlgoKey(new bts.persist.algo.AlgoPersistedKey(t.order.Def.AlgoTemplateKey)),
(os, okey, am, mkey) => new OrderSecurityAlgoMeta(os.order, os.security, am.algo, am.meta),
(os, okey, mkey) => new OrderSecurityAlgoMeta(os.order, os.security, null, null),
(os, okey, am, mkey) => new OrderSecurityAlgoMeta(os.order, os.sec, am.algo, am.meta),
(os, okey, mkey) => new OrderSecurityAlgoMeta(os.order, os.sec, null, null),
ot => ot.order.Key);
on_order_tuples = order_secs_algo_meta_join;
var on_fills =
@@ -129,9 +138,8 @@ namespace mbts.spark {
f => f.fill.FillKey);
on_fill_tuples = fill_order_tuples_join;
_dispose = Disposable.Create(
// on_orders changed from an IObservable to an IConnectableObservable, so we call
// RefCount() here to start the connection
on_orders.RefCount().Select(t => t.order, o => o.Key).Subscribe(_security_orders_demuxer),
on_orders.Select(t => t.order, o => o.Key).Subscribe(_security_orders_demuxer),
on_orders_with_piq.RefCount().Select(t => t.order, o => o.Key).Subscribe(_security_orders_demuxer),
on_positions.Subscribe(_security_positions_demuxer),
order_secs_algo_meta_join.Connect(),
algo_meta_join.Connect(),
@@ -148,11 +156,13 @@ namespace mbts.spark {
public OrderFactory factory => _factory;
public SpreaderModel spreader => _spreader;
public DynamicOrderHelper dynamic_helper => _dynamic_helper;
public IConnectableObservable<OrderUpdate> on_orders { get; }
public IObservable<OrderUpdate> on_orders { get; }
public IConnectableObservable<OrderUpdatePiq> on_orders_with_piq { get; }
public IObservable<TableUpdate<OrderSecurityAlgoMeta, uint>> on_order_tuples { get; }
public IObservable<IIncrementalUpdate<SecurityImmutable, uint>> on_order_securities { get; }
public IObservable<TableUpdate<FillTuple, string>> on_fill_tuples { get; }
public IObservable<IReadOnlyDictionary<uint,OrderSecurity>> working_orders { get; }
public IObservable<IReadOnlyDictionary<uint, OrderSecurity>> working_orders_with_piq { get; }
public IObservable<TableUpdate<PositionUnitImmutable, int>> on_positions { get; }
public AdminModel admin_model { get; }
@@ -249,8 +259,33 @@ namespace mbts.spark {
}
return clone.ToImmutableDictionary();
}
static ImmutableDictionary<uint, OrderSecurity>
_update_working_orders(ImmutableDictionary<uint, OrderSecurity> target, OrderUpdatePiq table) {
var clone = target.ToBuilder();
foreach (var o in table.removed) {
if (o.order.is_active()) {
clone.Remove(o.order.Key);
}
}
foreach (var o in table.changes) {
if (o.Old.order.is_active()) {
clone.Remove(o.Old.order.Key);
}
if (o.New.order.is_active()) {
clone.Add(o.New.order.Key, o.New);
}
}
foreach (var o in table.added) {
if (o.order.is_active()) {
clone.Add(o.order.Key, o);
}
}
return clone.ToImmutableDictionary();
}
}
// usinig new OrdersModel extensions to reduce the amount of code change needed to switch uis over to the new OrdersModel,
// without changing any code in the actual new OrdersModel class.
static class OrdersModelExtensions {
@@ -133,7 +133,7 @@ namespace mbts.spark {
account_positions,
size_calculator = new CalculatorLogic(0, _settings_manager.subscribe_current().Select(s => s.calculator_buttons)),
bus.subscribe(SparkTags.LogActivity).OnDispatcher().Subscribe(status_update),
model.order_gateway.working_orders.OnDispatcher().Subscribe(working => {
model.order_gateway.working_orders_with_piq.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);