From d48f34d10985f928fd93c490e8d569b3d8a78c4e Mon Sep 17 00:00:00 2001 From: John Colvin Date: Sat, 4 Feb 2023 00:26:28 -0500 Subject: [PATCH] feat (internal): Add fills to OrdersClient --- .../{spark => spark_api}/Orders/FillTuple.cs | 212 +++++++++--------- .../Orders/FillsSubscriptionFilter.cs | 31 +++ src/client/spark_api/Orders/OrdersClient.cs | 24 ++ 3 files changed, 161 insertions(+), 106 deletions(-) rename src/client/{spark => spark_api}/Orders/FillTuple.cs (95%) create mode 100644 src/client/spark_api/Orders/FillsSubscriptionFilter.cs diff --git a/src/client/spark/Orders/FillTuple.cs b/src/client/spark_api/Orders/FillTuple.cs similarity index 95% rename from src/client/spark/Orders/FillTuple.cs rename to src/client/spark_api/Orders/FillTuple.cs index 39a72b0d5..ebbcb6b30 100644 --- a/src/client/spark/Orders/FillTuple.cs +++ b/src/client/spark_api/Orders/FillTuple.cs @@ -1,106 +1,106 @@ -using bts; -using bts.api; -using bts.api.orderman; -using bts.utils; - -using Spark.Api.Orders; - -using System; - -using static bts.api.orderman.Fill.Types; - -namespace mbts.spark { - class FillTuple : IEquatable { - public static readonly Func key_fn = ft => ft.fill.FillKey; - public readonly FillImmutable fill; - public readonly SecurityImmutable? security; - - OrderRecordTupleWithRoot? _order_tuple; - - public FillTuple( - FillImmutable fill, - SecurityImmutable? security, - OrderRecordTupleWithRoot? order_tuple) { - this.fill = fill; - this.security = security; - _order_tuple = order_tuple; - } - - public FillTuple((SecurityImmutable? sec, FillImmutable val) fill, OrderRecordTuple? order_tuple) - : this(fill.val, fill.sec, order_tuple.HasValue ? new(order_tuple.Value) : null) { } - - public AlgoData? algo => - _order_tuple.HasValue ? _order_tuple.Value.order.algo : (AlgoData?)null; - - public AlgoDefinitionMetadata? algo_meta => - _order_tuple.HasValue ? _order_tuple.Value.order.meta : null; - - public OrderRecordImmutable? order => - _order_tuple.HasValue ? _order_tuple.Value.order.order : (OrderRecordImmutable?)null; - - public AlgoData? root_algo => - _order_tuple.HasValue ? _order_tuple.Value.root.algo : (AlgoData?)null; - - public AlgoDefinitionMetadata? root_algo_meta => - _order_tuple.HasValue ? _order_tuple.Value.root.meta : null; - - public OrderRecordTuple? root_order_security => _order_tuple?.root; - - public AccountTuple account_tuple => new(account_id, account); - - public string account { - get { - var account_string = string.Empty; - if (fill.FillKind != FillKind.Sod && order.HasValue && order.Value.HasDef) account_string = order.Value.Def.Account; - else account_string = fill.Account; - return account_string == null ? string.Empty : account_string; - } - } - - public uint account_id => - fill.FillKind != FillKind.Sod && order.HasValue ? - order.Value.account_id() : - fill.InternalAccountId; - - public TimeInForce time_in_force => - order?.time_in_force() ?? TimeInForce.Undefined; - - public bool Equals(FillTuple other) => - other != null && - _order_tuple.Equals(other._order_tuple) && - security.HasValue == other.security.HasValue && - fill.Equals(other.fill); - - public override bool Equals(object obj) => - Equals(obj as FillTuple); - - public override int GetHashCode() => - EqualityComparer.aggregate_hash( - fill.FillKey.GetHashCode(), - order.HasValue ? order.Value.Key.GetHashCode() : 0, - security.HasValue ? security.Value.Id.GetHashCode() : 0); - } - - static class FillTupleExtensions { - public static DateTime fill_time(this FillTuple fill_tuple) => - fill_tuple.fill.Timestamp.to_local_time(); - - public static TimeInForce time_in_force(this FillTuple fill_tuple) => - fill_tuple.order.HasValue ? fill_tuple.order.Value.time_in_force() : TimeInForce.Undefined; - - public static CustomOrderDefinition.CustomOneofCase order_type(this FillTuple fill_tuple) => - fill_tuple.order.HasValue ? fill_tuple.order.Value.order_type() : CustomOrderDefinition.CustomOneofCase.None; - - public static Venue exchange(this FillTuple fill_tuple) => - fill_tuple.security.HasValue ? fill_tuple.security.Value.Exchange : Venue.Unset; - - public static uint order_key(this FillTuple fill_tuple) => - fill_tuple.order.HasValue ? fill_tuple.order.Value.Key : 0u; - - public static string user(this FillTuple fill_tuple) => - fill_tuple.order.HasValue ? fill_tuple.order.Value.user() : string.Empty; - - public static uint user_id(this FillTuple fill_tuple) => - fill_tuple.order?.user_id() ?? 0; - } -} +using bts; +using bts.api; +using bts.api.orderman; +using bts.utils; + +using Spark.Api.Orders; + +using System; + +using static bts.api.orderman.Fill.Types; + +namespace mbts.spark { + public sealed class FillTuple : IEquatable { + public static readonly Func key_fn = ft => ft.fill.FillKey; + public readonly FillImmutable fill; + public readonly SecurityImmutable? security; + + OrderRecordTupleWithRoot? _order_tuple; + + public FillTuple( + FillImmutable fill, + SecurityImmutable? security, + OrderRecordTupleWithRoot? order_tuple) { + this.fill = fill; + this.security = security; + _order_tuple = order_tuple; + } + + public FillTuple((SecurityImmutable? sec, FillImmutable val) fill, OrderRecordTuple? order_tuple) + : this(fill.val, fill.sec, order_tuple.HasValue ? new(order_tuple.Value) : null) { } + + public AlgoData? algo => + _order_tuple.HasValue ? _order_tuple.Value.order.algo : (AlgoData?)null; + + public AlgoDefinitionMetadata? algo_meta => + _order_tuple.HasValue ? _order_tuple.Value.order.meta : null; + + public OrderRecordImmutable? order => + _order_tuple.HasValue ? _order_tuple.Value.order.order : (OrderRecordImmutable?)null; + + public AlgoData? root_algo => + _order_tuple.HasValue ? _order_tuple.Value.root.algo : (AlgoData?)null; + + public AlgoDefinitionMetadata? root_algo_meta => + _order_tuple.HasValue ? _order_tuple.Value.root.meta : null; + + public OrderRecordTuple? root_order_security => _order_tuple?.root; + + public AccountTuple account_tuple => new(account_id, account); + + public string account { + get { + var account_string = string.Empty; + if (fill.FillKind != FillKind.Sod && order.HasValue && order.Value.HasDef) account_string = order.Value.Def.Account; + else account_string = fill.Account; + return account_string == null ? string.Empty : account_string; + } + } + + public uint account_id => + fill.FillKind != FillKind.Sod && order.HasValue ? + order.Value.account_id() : + fill.InternalAccountId; + + public TimeInForce time_in_force => + order?.time_in_force() ?? TimeInForce.Undefined; + + public bool Equals(FillTuple other) => + other != null && + _order_tuple.Equals(other._order_tuple) && + security.HasValue == other.security.HasValue && + fill.Equals(other.fill); + + public override bool Equals(object obj) => + Equals(obj as FillTuple); + + public override int GetHashCode() => + EqualityComparer.aggregate_hash( + fill.FillKey.GetHashCode(), + order.HasValue ? order.Value.Key.GetHashCode() : 0, + security.HasValue ? security.Value.Id.GetHashCode() : 0); + } + + static class FillTupleExtensions { + public static DateTime fill_time(this FillTuple fill_tuple) => + fill_tuple.fill.Timestamp.ToDateTimeOffset().LocalDateTime; + + public static TimeInForce time_in_force(this FillTuple fill_tuple) => + fill_tuple.order.HasValue ? fill_tuple.order.Value.time_in_force() : TimeInForce.Undefined; + + public static CustomOrderDefinition.CustomOneofCase order_type(this FillTuple fill_tuple) => + fill_tuple.order.HasValue ? fill_tuple.order.Value.order_type() : CustomOrderDefinition.CustomOneofCase.None; + + public static Venue exchange(this FillTuple fill_tuple) => + fill_tuple.security.HasValue ? fill_tuple.security.Value.Exchange : Venue.Unset; + + public static uint order_key(this FillTuple fill_tuple) => + fill_tuple.order.HasValue ? fill_tuple.order.Value.Key : 0u; + + public static string user(this FillTuple fill_tuple) => + fill_tuple.order.HasValue ? fill_tuple.order.Value.user() : string.Empty; + + public static uint user_id(this FillTuple fill_tuple) => + fill_tuple.order?.user_id() ?? 0; + } +} diff --git a/src/client/spark_api/Orders/FillsSubscriptionFilter.cs b/src/client/spark_api/Orders/FillsSubscriptionFilter.cs new file mode 100644 index 000000000..fccfdb088 --- /dev/null +++ b/src/client/spark_api/Orders/FillsSubscriptionFilter.cs @@ -0,0 +1,31 @@ +#nullable enable +namespace Spark.Api.Orders { + using mbts.spark; + + using System; + using System.Collections.Generic; + using System.Linq; + + public class FillsSubscriptionFilter { + readonly HashSet? _account_ids; + readonly SecurityCriteria? _security_criteria; + + public FillsSubscriptionFilter( + IEnumerable? account_ids = null, SecurityCriteria? security_criteria = null) { + _account_ids = account_ids?.null_if_empty()?.ToHashSet(); + _security_criteria = security_criteria; + } + + public bool includes(FillTuple ft) => + to_predicates().All(p => p(ft)); + + public IEnumerable> to_predicates() { + if (_account_ids is not null) { + yield return o => _account_ids.Contains(o.account_id); + } + if (_security_criteria.is_defined()) { + yield return o => o.security.HasValue && _security_criteria.includes(o.security.Value); + } + } + } +} diff --git a/src/client/spark_api/Orders/OrdersClient.cs b/src/client/spark_api/Orders/OrdersClient.cs index d51df84d9..e4b9da259 100644 --- a/src/client/spark_api/Orders/OrdersClient.cs +++ b/src/client/spark_api/Orders/OrdersClient.cs @@ -17,6 +17,7 @@ namespace Spark.Api.Orders { public class OrdersClient : IDisposable { readonly IObservable> _orders; readonly IObservable> _orders_with_root; + readonly IObservable> _fills; IDisposable? _dispose; public OrdersClient( @@ -49,6 +50,25 @@ namespace Spark.Api.Orders { o => o.root.order.Key) .Cache() .ToObservable()); + + var fills = bootstraps.emplace( + admin_client.permission_filter( + order_service.subscribei_fills().Naggle(interval.Value, scheduler), f => f.Account) + .join(sec_resolver, f => f.SecurityKey) + .Table() + .Cache() + .ToObservable()); + + var fill_order_tuples_join = fills.LeftJoin( + _orders, + f => f.val.FillKey, + o => o.order.Key, + f => f.val.OrderKey, + (f, fk, o, ok) => new FillTuple(f, o), + (f, fk, ok) => new FillTuple(f, null), + f => f.fill.FillKey); + _fills = bootstraps.emplace(fill_order_tuples_join.Cache().ToObservable()); + _dispose = bootstraps; } @@ -59,6 +79,10 @@ namespace Spark.Api.Orders { public IObservable> orders_with_root( OrdersSubscriptionFilter filter) => _orders_with_root.Filter(o => filter.includes(o.order)); + + public IObservable> fills( + FillsSubscriptionFilter? filter = null) => + filter is null ? _fills : _fills.Filter(filter.includes); public void Dispose() => Disposable.dispose(ref _dispose);