refactor: Simplify TradesSource SecurityCriteria filter
This commit is contained in:
1 parent
4499a3751f
commit
b403cacb93
4 files changed
+49
-40
No files matched your search
@@ -193,11 +193,13 @@
|
||||
Disposable.Create(() => _vm.request_show_ladder -= show_ladder)
|
||||
};
|
||||
|
||||
var sec_mds_src = _bus.subscribe(SparkTags.SecuritiesTag);
|
||||
var secs_src = security_criteria_src
|
||||
.StartWith(() => _security_criteria)
|
||||
.SelectSeries(sc => sc.subscribe_map(sec_mds_src));
|
||||
var trades_source = TradesSource.create(
|
||||
_consumer,
|
||||
_bus.subscribe(SparkTags.SecuritiesTag)
|
||||
.Select(sec_md_map => MappedDictionary.create(sec_md_map, sec_md => sec_md.security)),
|
||||
security_criteria_src.StartWith(() => _security_criteria).Select(sc => new[] { sc }),
|
||||
secs_src,
|
||||
_trade_poll_interval);
|
||||
trades_source = TradesSource.with_greeks(trades_source, _bus.subscribe(SparkTags.Greeks));
|
||||
trades_source = TradesSource.with_volume(
|
||||
|
||||
@@ -59,17 +59,26 @@ namespace bts.shell {
|
||||
_order_notifications = new NotificationProcessor<OrderRecordTuple>(notifier);
|
||||
_trade_notifications = new NotificationProcessor<TradeInfo>(notifier);
|
||||
_consumer = consumer;
|
||||
var trade_criteria_src = _trade_criteria_subject
|
||||
.StartWith(() => _trade_criteria_refs.Keys);
|
||||
var trade_secs_src = trade_criteria_src.SelectSeries(criterias => {
|
||||
var secs = bus.subscribe(SparkTags.SecuritiesTag);
|
||||
if (criterias.is_null_or_empty()) {
|
||||
return secs.Select(smds => MappedDictionary.create(smds, s => s.security));
|
||||
}
|
||||
// Take a snapshot of 'criterias' (IEnumerable) and store it in a List to:
|
||||
// a) prevent potentially expensive repetative enumeration in the callbacks (for ex,
|
||||
// if 'criterias' were an expensive System.Linq expression)
|
||||
// b) ensure the sequence never mutates
|
||||
var criterias_list = criterias.ToList();
|
||||
return secs.Select(smds =>
|
||||
smds.Values.Where(s => criterias_list.Any(c => c.includes(s.security)))
|
||||
.ToDictionary(s => s.id, s => s.security));
|
||||
});
|
||||
_dispose = Disposable.Create(
|
||||
bus.subscribe(SparkTags.LogActivity).Subscribe(_on_activity),
|
||||
orders_model.on_orders.Subscribe(_on_orders),
|
||||
TradesSource.create(
|
||||
consumer,
|
||||
bus.subscribe(SparkTags.SecuritiesTag).BufferLatest(TimeSpan.FromSeconds(2))
|
||||
.Table(s => s.security.Id)
|
||||
.Where(t => t.has_changes())
|
||||
.Select(m => MappedDictionary.create(m.state, smd => smd.security)),
|
||||
_trade_criteria_subject,
|
||||
TimeSpan.FromSeconds(0.2))
|
||||
TradesSource.create(consumer, trade_secs_src, TimeSpan.FromSeconds(0.2))
|
||||
.Subscribe(_trade_notifications.process_items));
|
||||
}
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
namespace mbts.spark {
|
||||
namespace mbts.spark {
|
||||
using bts.api;
|
||||
using bts.api.core;
|
||||
using bts.co2;
|
||||
@@ -7,7 +7,6 @@ namespace mbts.spark {
|
||||
|
||||
using mbts.analytics;
|
||||
using mbts.co2;
|
||||
using mbts.utils;
|
||||
|
||||
using Spark.Core.Disposables;
|
||||
|
||||
@@ -18,38 +17,27 @@ namespace mbts.spark {
|
||||
using System.Reactive.Linq;
|
||||
|
||||
public static class TradesSource {
|
||||
public static IEnumerable<ProductImmutable> aggregate_security_criteria(
|
||||
IEnumerable<SecurityImmutable> securities,
|
||||
IEnumerable<SecurityCriteria> security_criteria_items) =>
|
||||
securities.Where(s => security_criteria_items.Any(sc => sc.includes(s)))
|
||||
.Select(s => s.product())
|
||||
.Distinct();
|
||||
|
||||
public static IObservable<IList<TradeInfo>> create(
|
||||
MDConsumer consumer,
|
||||
IObservable<IReadOnlyDictionary<uint, SecurityImmutable>> security_map,
|
||||
IObservable<IEnumerable<SecurityCriteria>> security_criteria,
|
||||
TimeSpan poll_interval) =>
|
||||
security_criteria
|
||||
.CombineLatest(security_map, (criteria, map) => (criteria, map))
|
||||
.Scan(
|
||||
(channels: ImmutableDictionary<ProductImmutable, ChannelConsumer>.Empty, active: new List<ChannelConsumer>(0)),
|
||||
(channel_data, data) => {
|
||||
var (criteria, map) = data;
|
||||
var (channels, active) = channel_data;
|
||||
var products = aggregate_security_criteria(map.Values, criteria);
|
||||
active = new List<ChannelConsumer>();
|
||||
foreach (var p in products) {
|
||||
if (!channels.TryGetValue(p, out var c)) {
|
||||
c = new ChannelConsumer(
|
||||
consumer.get_channel(p.Symbol, p.Type, p.Exchange),
|
||||
map.Values.Where(s => s.product().Equals(p)));
|
||||
channels = channels.Add(p, c);
|
||||
}
|
||||
active.Add(c);
|
||||
security_map.Scan(
|
||||
(channels: ImmutableDictionary<ProductImmutable, ChannelConsumer>.Empty, active: new List<ChannelConsumer>(0)),
|
||||
(channel_data, map) => {
|
||||
var (channels, active) = channel_data;
|
||||
var products = map.Values.Select(s => s.product()).Distinct();
|
||||
active = new List<ChannelConsumer>();
|
||||
foreach (var p in products) {
|
||||
if (!channels.TryGetValue(p, out var c)) {
|
||||
c = new ChannelConsumer(
|
||||
consumer.get_channel(p.Symbol, p.Type, p.Exchange),
|
||||
map.Values.Where(s => s.product().Equals(p)));
|
||||
channels = channels.Add(p, c);
|
||||
}
|
||||
return (channels, active);
|
||||
})
|
||||
active.Add(c);
|
||||
}
|
||||
return (channels, active);
|
||||
})
|
||||
.SelectSeries(channel_data => {
|
||||
var (channels, active) = channel_data;
|
||||
return Observable.Interval(poll_interval)
|
||||
|
||||
@@ -32,6 +32,16 @@ namespace mbts.spark {
|
||||
.Select(smd => smd.security).Where(self.includes));
|
||||
}
|
||||
|
||||
public static IObservable<IReadOnlyDictionary<uint, SecurityImmutable>> subscribe_map(
|
||||
this SecurityCriteria? self, IObservable<IReadOnlyDictionary<uint, SecurityMD>> md_source) {
|
||||
if (self is null) {
|
||||
return md_source.Select(m => MappedDictionary.create(m, smd => smd.security));
|
||||
}
|
||||
return md_source.Select(m => m
|
||||
.Where(kv => self.includes(kv.Value.security))
|
||||
.ToDictionary(kv => kv.Key, kv => kv.Value.security));
|
||||
}
|
||||
|
||||
public static IObservable<IEnumerable<uint>> subscribe_keys(
|
||||
this SecurityCriteria self, IObservable<IReadOnlyDictionary<uint, SecurityMD>> md_source) {
|
||||
return self is null ? md_source.Select(kv => kv.Values.Select(smd => smd.security.Id)) :
|
||||
|
||||
Reference in new issue
Block a user