Update fills to use SecurityStore.join
This commit is contained in:
1 parent
e08cd7bdbf
commit
e05fc076e8
8 files changed
+85
-92
No files matched your search
@@ -39,7 +39,7 @@ namespace mbts.spark {
|
||||
user_filter = DiscreteValuesFilterVM.Create<Row, string>(_model.factory.subscribe_users().Select(d => d.Values), r => r.user, nameof(Row.user));
|
||||
security_filter = DiscreteValuesFilterVM.Create<Row, SecurityImmutable, SecurityCriteria>(
|
||||
fills_source.with_respect_to(
|
||||
ft => ft.security_md.HasValue ? ft.security_md.Value.security : SecurityDataHelper.Defaults.SecurityImmutable,
|
||||
ft => ft.security ?? SecurityDataHelper.Defaults.SecurityImmutable,
|
||||
s => s.Id, ft => ft.key
|
||||
).Select(t => (t.added.Values, t.removed.Values)),
|
||||
r => r.security,
|
||||
@@ -186,7 +186,8 @@ namespace mbts.spark {
|
||||
readonly FillImmutable _fill;
|
||||
readonly OrderRecordImmutable _order;
|
||||
|
||||
public Row(FillTuple fill_tuple, OrdersModel model) : base(fill_tuple.security_md.HasValue ? fill_tuple.security_md.Value.security : SecurityDataHelper.Defaults.SecurityImmutable) {
|
||||
public Row(FillTuple fill_tuple, OrdersModel model) :
|
||||
base(fill_tuple.security ?? SecurityDataHelper.Defaults.SecurityImmutable) {
|
||||
_fill = fill_tuple.fill;
|
||||
_order = fill_tuple.order ?? OrderDataHelper.Defaults.OrderRecordImmutable;
|
||||
_model = model;
|
||||
|
||||
@@ -10,14 +10,14 @@ namespace mbts.spark {
|
||||
class FillTuple : IEquatable<FillTuple> {
|
||||
public readonly FillImmutable fill;
|
||||
public readonly OrderRecordImmutable? order;
|
||||
public readonly SecurityMD? security_md;
|
||||
public readonly SecurityImmutable? security;
|
||||
public readonly FillTupleKey key;
|
||||
|
||||
public FillTuple(FillImmutable fill, OrderRecordImmutable? order, SecurityMD? security_md) {
|
||||
public FillTuple(FillImmutable fill, OrderRecordImmutable? order, SecurityImmutable? security) {
|
||||
this.fill = fill;
|
||||
this.order = order;
|
||||
this.security_md = security_md;
|
||||
key = new FillTupleKey(fill.FillKey, order.HasValue ? order.Value.Key : 0u, security_md.HasValue ? security_md.Value.security.Id : 0u);
|
||||
this.security = security;
|
||||
key = new FillTupleKey(fill.FillKey, order.HasValue ? order.Value.Key : 0u, security?.Id ?? 0);
|
||||
}
|
||||
|
||||
public string account {
|
||||
@@ -31,7 +31,7 @@ namespace mbts.spark {
|
||||
|
||||
public bool Equals(FillTuple other) => other != null
|
||||
&& other.order.HasValue == order.HasValue
|
||||
&& other.security_md.HasValue == security_md.HasValue
|
||||
&& other.security.HasValue == security.HasValue
|
||||
&& other.fill.Equals(fill);
|
||||
|
||||
public override bool Equals(object obj) => Equals(obj as FillTuple);
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using bts.api.core;
|
||||
using bts.api;
|
||||
using bts.api.core;
|
||||
using bts.api.orderman;
|
||||
using mbts.collections;
|
||||
using mbts.utils;
|
||||
@@ -14,15 +15,14 @@ namespace mbts.spark {
|
||||
// returns a ref-count Observable.Create-wrapper that initializes/destroys the [hot-observable] FillTupleSource.
|
||||
// (initializes when the first subscription is made, and destroys when the last subscription is disposed).
|
||||
public static IObservable<IIncrementalUpdate<FillTuple, FillTupleKey>> create_observable(
|
||||
IObservable<IReadOnlyDictionary<uint, SecurityMD>> securities,
|
||||
IObservable<TableUpdate<OrderRecordImmutable, uint>> orders,
|
||||
IObservable<TableUpdate<FillImmutable, string>> fills) {
|
||||
IObservable<TableUpdate<(SecurityImmutable?,OrderRecordImmutable), uint>> orders,
|
||||
IObservable<TableUpdate<(SecurityImmutable?,FillImmutable), string>> fills) {
|
||||
FillTupleSource source = null;
|
||||
int ref_count = 0;
|
||||
return Observable.Create<IIncrementalUpdate<FillTuple, FillTupleKey>>(observer => {
|
||||
if (Interlocked.Increment(ref ref_count) == 1) {
|
||||
SparkUtils.lazy_assert(source == null, () => fmt_assert_msg("source should be null, there must be a leak."));
|
||||
source = new FillTupleSource(securities, orders, fills);
|
||||
source = new FillTupleSource(orders, fills);
|
||||
}
|
||||
IDisposable sub = source.Subscribe(observer);
|
||||
return Disposable.Create(() => {
|
||||
@@ -38,19 +38,16 @@ namespace mbts.spark {
|
||||
readonly Dictionary<FillTupleKey, FillTuple> _state = new Dictionary<FillTupleKey, FillTuple>();
|
||||
readonly SetMap<uint, string> _order_fills = new SetMap<uint, string>();
|
||||
readonly SetMap<uint, string> _security_fills = new SetMap<uint, string>();
|
||||
IReadOnlyDictionary<string, FillImmutable> _fills_cache = new Dictionary<string, FillImmutable>(0);
|
||||
IReadOnlyDictionary<uint, OrderRecordImmutable> _orders_cache = new Dictionary<uint, OrderRecordImmutable>(0);
|
||||
IReadOnlyDictionary<uint, SecurityMD> _security_md_cache = new Dictionary<uint, SecurityMD>(0);
|
||||
IReadOnlyDictionary<string, (SecurityImmutable? sec, FillImmutable fill)> _fills_cache = new Dictionary<string, (SecurityImmutable?, FillImmutable)>();
|
||||
IReadOnlyDictionary<uint, (SecurityImmutable?,OrderRecordImmutable)> _orders_cache = new Dictionary<uint, (SecurityImmutable?,OrderRecordImmutable)>();
|
||||
IDisposable _dispose;
|
||||
object _lock = new object();
|
||||
|
||||
FillTupleSource(
|
||||
IObservable<IReadOnlyDictionary<uint, SecurityMD>> securities,
|
||||
IObservable<TableUpdate<OrderRecordImmutable, uint>> orders,
|
||||
IObservable<TableUpdate<FillImmutable, string>> fills) =>
|
||||
IObservable<TableUpdate<(SecurityImmutable?,OrderRecordImmutable), uint>> orders,
|
||||
IObservable<TableUpdate<(SecurityImmutable?,FillImmutable), string>> fills) =>
|
||||
//
|
||||
_dispose = Disposable.Create(
|
||||
securities.Subscribe(_update_securities),
|
||||
orders.Subscribe(_update_orders),
|
||||
fills.AddRemoveOnly().Subscribe(_update_fills));
|
||||
|
||||
@@ -63,19 +60,19 @@ namespace mbts.spark {
|
||||
|
||||
public void Dispose() => Disposable.dispose(ref _dispose);
|
||||
|
||||
void _update_fills(LegacyTableUpdate<FillImmutable, string> fills) {
|
||||
void _update_fills(LegacyTableUpdate<(SecurityImmutable?,FillImmutable), string> fills) {
|
||||
lock (_lock) {
|
||||
var removed = new Dictionary<FillTupleKey, FillTuple>();
|
||||
var added = new Dictionary<FillTupleKey, FillTuple>();
|
||||
|
||||
foreach (FillImmutable fill in fills.removed) {
|
||||
FillTuple old_ft = new FillTuple(fill, _get_order(fill.OrderKey), _get_security_md(fill.SecurityKey));
|
||||
foreach (var (security,fill) in fills.removed) {
|
||||
FillTuple old_ft = new FillTuple(fill, _get_order(fill.OrderKey), security);
|
||||
removed.Add(old_ft.key, old_ft);
|
||||
_security_fills.remove(fill.SecurityKey, fill.FillKey);
|
||||
_order_fills.remove(fill.OrderKey, fill.FillKey);
|
||||
}
|
||||
foreach (FillImmutable fill in fills.added) {
|
||||
FillTuple new_ft = new FillTuple(fill, _get_order(fill.OrderKey), _get_security_md(fill.SecurityKey));
|
||||
foreach (var (security,fill) in fills.added) {
|
||||
FillTuple new_ft = new FillTuple(fill, _get_order(fill.OrderKey), security);
|
||||
added.Add(new_ft.key, new_ft);
|
||||
_security_fills.add(fill.SecurityKey, fill.FillKey);
|
||||
_order_fills.add(fill.OrderKey, fill.FillKey);
|
||||
@@ -85,35 +82,33 @@ namespace mbts.spark {
|
||||
}
|
||||
}
|
||||
|
||||
void _update_orders(TableUpdate<OrderRecordImmutable, uint> orders) {
|
||||
void _update_orders(TableUpdate<(SecurityImmutable?,OrderRecordImmutable), uint> orders) {
|
||||
lock (_lock) {
|
||||
Dictionary<FillTupleKey, FillTuple> added = new Dictionary<FillTupleKey, FillTuple>(), removed = new Dictionary<FillTupleKey, FillTuple>();
|
||||
HashSet<uint> changed_keys = new HashSet<uint>();
|
||||
foreach (var o in orders.removed) {
|
||||
foreach (var (_,o) /*winking owl*/ in orders.removed) {
|
||||
if (_order_fills.try_lookup(o.Key, out ISet<string> fill_keys)) {
|
||||
OrderRecordImmutable? curr_order = null;
|
||||
if (orders.try_get(o.Key, out OrderRecordImmutable newval)) {
|
||||
curr_order = newval;
|
||||
if (orders.try_get(o.Key, out var newval)) {
|
||||
curr_order = newval.Item2;
|
||||
changed_keys.Add(o.Key);
|
||||
}
|
||||
IEnumerable<FillImmutable> fills = fill_keys.Select(fk => _fills_cache[fk]);
|
||||
foreach (FillImmutable fill in fills) {
|
||||
SecurityMD? sec_md = _get_security_md(fill.SecurityKey);
|
||||
FillTuple old_ft = new FillTuple(fill, o, sec_md);
|
||||
var fills = fill_keys.Select(fk => _fills_cache[fk]);
|
||||
foreach (var (security, fill) in fills) {
|
||||
FillTuple old_ft = new FillTuple(fill, o, security);
|
||||
removed.Add(old_ft.key, old_ft);
|
||||
FillTuple new_ft = new FillTuple(fill, curr_order, sec_md);
|
||||
FillTuple new_ft = new FillTuple(fill, curr_order, security);
|
||||
added.Add(new_ft.key, new_ft);
|
||||
}
|
||||
}
|
||||
}
|
||||
foreach (var o in orders.added) {
|
||||
foreach (var (_,o) in orders.added) {
|
||||
if (!changed_keys.Contains(o.Key) && _order_fills.try_lookup(o.Key, out ISet<string> fill_keys)) {
|
||||
IEnumerable<FillImmutable> fills = fill_keys.Select(fk => _fills_cache[fk]);
|
||||
foreach (FillImmutable fill in fills) {
|
||||
SecurityMD? sec_md = _get_security_md(fill.SecurityKey);
|
||||
FillTuple old_ft = new FillTuple(fill, null, sec_md);
|
||||
var fills = fill_keys.Select(fk => _fills_cache[fk]);
|
||||
foreach (var (security,fill) in fills) {
|
||||
FillTuple old_ft = new FillTuple(fill, null, security);
|
||||
removed.Add(old_ft.key, old_ft);
|
||||
FillTuple new_ft = new FillTuple(fill, o, sec_md);
|
||||
FillTuple new_ft = new FillTuple(fill, o, security);
|
||||
added.Add(new_ft.key, new_ft);
|
||||
}
|
||||
}
|
||||
@@ -123,46 +118,8 @@ namespace mbts.spark {
|
||||
}
|
||||
}
|
||||
|
||||
void _update_securities(IReadOnlyDictionary<uint, SecurityMD> securities) {
|
||||
lock (_lock) {
|
||||
Dictionary<FillTupleKey, FillTuple> added = new Dictionary<FillTupleKey, FillTuple>(), removed = new Dictionary<FillTupleKey, FillTuple>();
|
||||
_security_md_cache.Keys.difference(securities.Keys, out IEnumerable<uint> added_keys, out IEnumerable<uint> removed_keys);
|
||||
foreach (var k in removed_keys) {
|
||||
if (_security_fills.try_lookup(k, out ISet<string> fill_keys)) {
|
||||
IEnumerable<FillImmutable> fills = fill_keys.Select(fk => _fills_cache[fk]);
|
||||
SecurityMD old_sec_md = _security_md_cache[k];
|
||||
foreach (FillImmutable fill in fills) {
|
||||
OrderRecordImmutable? order = _get_order(fill.OrderKey);
|
||||
FillTuple old_ft = new FillTuple(fill, order, old_sec_md);
|
||||
removed.Add(old_ft.key, old_ft);
|
||||
FillTuple new_ft = new FillTuple(fill, order, null);
|
||||
added.Add(new_ft.key, new_ft);
|
||||
}
|
||||
}
|
||||
}
|
||||
foreach (var k in added_keys) {
|
||||
if (_security_fills.try_lookup(k, out ISet<string> fill_keys)) {
|
||||
IEnumerable<FillImmutable> fills = fill_keys.Select(fk => _fills_cache[fk]);
|
||||
SecurityMD new_sec_md = securities[k];
|
||||
foreach (FillImmutable fill in fills) {
|
||||
OrderRecordImmutable? order = _get_order(fill.OrderKey);
|
||||
FillTuple old_ft = new FillTuple(fill, order, null);
|
||||
removed.Add(old_ft.key, old_ft);
|
||||
FillTuple new_ft = new FillTuple(fill, order, new_sec_md);
|
||||
added.Add(new_ft.key, new_ft);
|
||||
}
|
||||
}
|
||||
}
|
||||
_security_md_cache = securities;
|
||||
_apply_changes(added, removed);
|
||||
}
|
||||
}
|
||||
|
||||
SecurityMD? _get_security_md(uint key) =>
|
||||
_security_md_cache.TryGetValue(key, out SecurityMD smd) ? smd : new SecurityMD?();
|
||||
|
||||
OrderRecordImmutable? _get_order(uint key) =>
|
||||
_orders_cache.TryGetValue(key, out OrderRecordImmutable o) ? o : new OrderRecordImmutable?();
|
||||
_orders_cache.TryGetValue(key, out var o) ? o.Item2 : new OrderRecordImmutable?();
|
||||
|
||||
void _apply_changes(Dictionary<FillTupleKey, FillTuple> added, Dictionary<FillTupleKey, FillTuple> removed) {
|
||||
foreach (var kv in removed)
|
||||
|
||||
@@ -71,10 +71,10 @@ namespace mbts.spark {
|
||||
_service.subscribei_fills()
|
||||
.Naggle(interval, scheduler)
|
||||
.PermissionFilter(admin_model.on_admin_data)
|
||||
//.join(sstore, f => )
|
||||
.join(sstore, f => f.SecurityKey)
|
||||
.Table().Cache();
|
||||
_activity = new OrderActivitySummarizer(bus, on_orders);
|
||||
on_fill_tuples = FillTupleSource.create_observable(bus.subscribe(SparkTags.SecuritiesTag), on_orders.Select(i => i.order, order => order.Key), on_fills);
|
||||
on_fill_tuples = FillTupleSource.create_observable(on_orders, on_fills);
|
||||
_security_orders_demuxer = new TableUpdateDemuxer<OrderRecordImmutable, uint, uint>(o => o.Key, o => o.security_key());
|
||||
_security_positions_demuxer = new TableUpdateDemuxer<PositionUnitImmutable, int, uint>(o => o.RecordId, p => p.SecurityId);
|
||||
_dispose = Disposable.Create(
|
||||
@@ -94,7 +94,7 @@ namespace mbts.spark {
|
||||
public DynamicOrderHelper dynamic_helper => _dynamic_helper;
|
||||
public IObservable<OrderUpdate> on_orders { get; }
|
||||
public IObservable<IIncrementalUpdate<SecurityImmutable, uint>> on_order_securities { get; }
|
||||
public IObservable<TableUpdate<FillImmutable, string>> on_fills { get; }
|
||||
public IObservable<TableUpdate<(SecurityImmutable?,FillImmutable), string>> on_fills { get; }
|
||||
public IObservable<IIncrementalUpdate<FillTuple, FillTupleKey>> on_fill_tuples { get; }
|
||||
public IObservable<IReadOnlyDictionary<uint,OrderSecurity>> working_orders { get; }
|
||||
public IObservable<TableUpdate<PositionUnitImmutable, int>> on_positions { get; }
|
||||
|
||||
@@ -0,0 +1,30 @@
|
||||
using mbts;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel.Composition;
|
||||
using System.Reactive.Linq;
|
||||
|
||||
namespace bts {
|
||||
class MDLookup : IDisposable {
|
||||
IReadOnlyDictionary<uint, SecurityMD> _state;
|
||||
IDisposable _dispose;
|
||||
|
||||
[ImportingConstructor]
|
||||
public MDLookup(IObservable<IReadOnlyDictionary<uint, SecurityMD>> src) {
|
||||
_dispose = src.Subscribe(s => _state = s);
|
||||
}
|
||||
|
||||
public void Dispose() { Disposable.dispose(ref _dispose); }
|
||||
|
||||
public SecurityMD? get_md(uint key) {
|
||||
if (_state?.TryGetValue(key, out var md) == true) {
|
||||
return md;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
public SecurityMD? get_md(uint? key) {
|
||||
if (key.HasValue) return get_md(key.Value);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -139,11 +139,11 @@ namespace bts {
|
||||
///////////////
|
||||
#region public
|
||||
|
||||
// Guarunteed to return a security if it has ever been seen by the system
|
||||
// Guaranteed to return a security if it has ever been seen by the system
|
||||
public Task<SecurityImmutable?> get_security(security_key key) {
|
||||
return get_security(key, _live_securities);
|
||||
}
|
||||
// Guarunteed to return a security if it has ever been seen by the system
|
||||
// Guaranteed to return a security if it has ever been seen by the system
|
||||
public Task<IReadOnlyDictionary<security_key, SecurityImmutable>> get_securities(IEnumerable<security_key> keys) {
|
||||
return get_securities(keys, _live_securities);
|
||||
}
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using bts.api;
|
||||
using bts;
|
||||
using bts.api;
|
||||
using mbts.utils;
|
||||
using System;
|
||||
using System.ComponentModel.Composition;
|
||||
@@ -8,18 +9,18 @@ namespace mbts.spark {
|
||||
class PAndLModel : IDisposable {
|
||||
readonly IBus _bus;
|
||||
readonly OrdersModel _order_model;
|
||||
//readonly AccountService _account_service;
|
||||
readonly MDLookup _mdlookup;
|
||||
IDisposable _dispose;
|
||||
|
||||
[ImportingConstructor]
|
||||
private PAndLModel(IBus bus, OrdersModel order_model/*, AccountService account_service*/) {
|
||||
_bus = bus;
|
||||
_order_model = order_model;
|
||||
//_account_service = account_service;
|
||||
_mdlookup = new MDLookup(bus.subscribe(SparkTags.SecuritiesTag));
|
||||
}
|
||||
|
||||
public IObservable<ITableUpdateTransform<PnLData, FillTupleKey>> on_pnl_data =>
|
||||
_order_model.on_fill_tuples.convert(ft => new PnLData(ft)).table(pd => pd.key);
|
||||
_order_model.on_fill_tuples.Select(ft => new PnLData(ft, _mdlookup.get_md(ft.security?.Id))).table(pd => pd.key);
|
||||
|
||||
//public async Task<Account[]> get_accounts() {
|
||||
// var request = await _account_service.get_accounts_async(new GetAccountsRequest());
|
||||
|
||||
@@ -28,19 +28,23 @@ namespace mbts.spark {
|
||||
class PnLData {
|
||||
|
||||
readonly FillTuple _fill_tuple;
|
||||
readonly SecurityMD? _md;
|
||||
|
||||
public PnLData(FillTuple fill_tuple) =>
|
||||
public PnLData(FillTuple fill_tuple, SecurityMD? md) {
|
||||
// It is entirely possible for the FillTuple to have a security but for no SecrityMD to be available. For example: expired security
|
||||
_md = md;
|
||||
_fill_tuple = fill_tuple;
|
||||
}
|
||||
|
||||
public FillTupleKey key => _fill_tuple.key;
|
||||
public FillImmutable fill => _fill_tuple.fill;
|
||||
public SecurityMD? sec_md => _fill_tuple.security_md;
|
||||
public SecurityMD? sec_md => _md;
|
||||
public SecurityImmutable? security => _fill_tuple.security;
|
||||
public OrderRecordImmutable? order => _fill_tuple.order;
|
||||
|
||||
public string account => _fill_tuple.account;
|
||||
public Venue? exchange => sec_md?.security.Exchange;
|
||||
public string symbol => sec_md?.security.Symbol;
|
||||
public SecurityImmutable? security => sec_md?.security;
|
||||
public Venue? exchange => security?.Exchange;
|
||||
public string symbol => security?.Symbol;
|
||||
public double buy_quantity => (fill.Side == Side.Buy) ? fill.LastShares : 0;
|
||||
public double sell_quantity => (fill.Side == Side.Sell) ? fill.LastShares : 0;
|
||||
public SecurityType? security_type => security?.security_type();
|
||||
|
||||
Reference in new issue
Block a user