SPARK-685 TimeAndSalesModel - trade cursor cannot be shared between different channels
This commit is contained in:
1 parent
91bde6eab5
commit
3f9ce2fa11
2 files changed
+67
-32
No files matched your search
@@ -1,46 +1,53 @@
|
||||
using bts.api;
|
||||
using bts.co2;
|
||||
using mbts.co2;
|
||||
using mbts.collections.extensions;
|
||||
using System;
|
||||
using System.Collections.Concurrent;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Reactive.Linq;
|
||||
|
||||
namespace mbts.spark
|
||||
{
|
||||
namespace mbts.spark {
|
||||
struct TradeInfo {
|
||||
public SecurityImmutable security;
|
||||
public TradeMessageImmutable trade;
|
||||
public readonly SecurityImmutable security;
|
||||
public readonly TradeMessageImmutable trade;
|
||||
|
||||
public TradeInfo(SecurityImmutable security, TradeMessageImmutable trade) {
|
||||
this.security = security;
|
||||
this.trade = trade;
|
||||
}
|
||||
}
|
||||
class TimeAndSalesModel
|
||||
{
|
||||
public TimeAndSalesModel(IBus bus, MDConsumer consumer, SecurityCriteria security_criteria) {
|
||||
var user_syms = UserSymbol.get_all();
|
||||
var cache = new ConcurrentDictionary<uint, MDConsumerChannel>();
|
||||
trades = security_criteria
|
||||
.subscribe_md_map(bus)
|
||||
.Select(md_map => {
|
||||
var channels = md_map.Values.Select(smd => cache.GetOrAdd(smd.security.Id, _ => consumer.get_channel(smd.security.Symbol, smd.security.security_type(), smd.security.Exchange)));
|
||||
return Observable.Interval(SparkUtils.DefaultPollInterval)
|
||||
.Select(_ => {
|
||||
var info = new List<TradeInfo>();
|
||||
foreach (var channel in channels) {
|
||||
foreach (var trade in channel.read_trades(ref _cursor)) {
|
||||
if (md_map.TryGetValue(trade.Key, out SecurityMD smd)) {
|
||||
info.Add(new TradeInfo() {
|
||||
trade = trade,
|
||||
security = smd.security
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
return info;
|
||||
});
|
||||
}).SelectMany(vals => vals);
|
||||
}
|
||||
|
||||
class TimeAndSalesModel {
|
||||
static readonly TimeSpan _poll_interval = TimeSpan.FromSeconds(0.4);
|
||||
|
||||
public TimeAndSalesModel(IBus bus, MDConsumer consumer, SecurityCriteria security_criteria) =>
|
||||
trades = security_criteria.subscribe_md_map(bus)
|
||||
.Scan(new Dictionary<uint, SecurityTradeConsumer>(),
|
||||
(cache, md_map) => {
|
||||
var copy = new Dictionary<uint, SecurityTradeConsumer>(cache);
|
||||
copy.sync_with(md_map, (id, smd) => new SecurityTradeConsumer(smd.security, consumer.get_channel(smd.security.Symbol, smd.security.security_type(), smd.security.Exchange)));
|
||||
return copy;
|
||||
}
|
||||
).Select(cache =>
|
||||
Observable.Interval(_poll_interval)
|
||||
.Select(_ => new List<TradeInfo>(cache.Values.SelectMany(c => c.read_trades())))
|
||||
).SelectMany(_ => _);
|
||||
|
||||
Int64 _cursor = 0;
|
||||
public IObservable<IReadOnlyList<TradeInfo>> trades { get; }
|
||||
|
||||
private class SecurityTradeConsumer {
|
||||
readonly SecurityImmutable _security;
|
||||
readonly MDConsumerChannel _channel;
|
||||
long _cursor = 0;
|
||||
|
||||
public SecurityTradeConsumer(SecurityImmutable security, MDConsumerChannel channel) {
|
||||
_security = security;
|
||||
_channel = channel;
|
||||
}
|
||||
|
||||
public IEnumerable<TradeInfo> read_trades() =>
|
||||
_channel.read_trades(ref _cursor).Select(tm => new TradeInfo(_security, tm));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -588,5 +588,33 @@ namespace mbts.collections.extensions
|
||||
if (dictionary.TryGetValue(key, out TValue curr)) update_existing(curr);
|
||||
else dictionary.Add(key, create_new());
|
||||
}
|
||||
|
||||
// this overload prevents 'ambiguous calls' compilation errors due to Dictionary implementing IDictionary, and IReadOnlyDictionary.
|
||||
public static void sync_with<TKey, TValue, TSourceValue>(this IDictionary<TKey, TValue> dictionary, Dictionary<TKey, TSourceValue> source, Func<TKey, TSourceValue, TValue> selector) =>
|
||||
_sync_with(dictionary, source, selector);
|
||||
|
||||
public static void sync_with<TKey, TValue, TSourceValue>(this IDictionary<TKey, TValue> dictionary, IDictionary<TKey, TSourceValue> source, Func<TKey, TSourceValue, TValue> selector) =>
|
||||
_sync_with(dictionary, source, selector);
|
||||
|
||||
public static void sync_with<TKey, TValue, TSourceValue>(this IDictionary<TKey, TValue> dictionary, IReadOnlyDictionary<TKey, TSourceValue> source, Func<TKey, TSourceValue, TValue> selector) =>
|
||||
_sync_with_ro(dictionary, source, selector);
|
||||
|
||||
static void _sync_with<TKey, TValue, TSourceValue>(this IDictionary<TKey, TValue> dictionary, IDictionary<TKey, TSourceValue> source, Func<TKey, TSourceValue, TValue> selector) {
|
||||
var removes = dictionary.Keys.Except(source.Keys).ToList();
|
||||
foreach (var k in removes)
|
||||
dictionary.Remove(k);
|
||||
var adds = source.Except(dictionary.Keys, kv => kv.Key).ToList();
|
||||
foreach (var kv in adds)
|
||||
dictionary.Add(kv.Key, selector(kv.Key, kv.Value));
|
||||
}
|
||||
|
||||
static void _sync_with_ro<TKey, TValue, TSourceValue>(this IDictionary<TKey, TValue> dictionary, IReadOnlyDictionary<TKey, TSourceValue> source, Func<TKey, TSourceValue, TValue> selector) {
|
||||
var removes = dictionary.Keys.Except(source.Keys).ToList();
|
||||
foreach (var k in removes)
|
||||
dictionary.Remove(k);
|
||||
var adds = source.Except(dictionary.Keys, kv => kv.Key).ToList();
|
||||
foreach (var kv in adds)
|
||||
dictionary.Add(kv.Key, selector(kv.Key, kv.Value));
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user