namespace mbts.spark { using bts.api; using bts.api.core; using bts.co2; using bts.collections; using bts.utils; using mbts.analytics; using mbts.co2; using Spark.Core.Disposables; using Spark.Core.Reactive; using System; using System.Collections.Generic; using System.Collections.Immutable; using System.Linq; using System.Reactive.Linq; public static class TradesSource { public static IObservable> create( MDConsumer consumer, IObservable> security_md_map, TimeSpan poll_interval) => security_md_map.Scan( (channels: ImmutableDictionary.Empty, active: new List(0)), (channel_data, map) => { var (channels, active) = channel_data; var products = map.Values.Select(s => s.security.product()).Distinct(); active = new List(); 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.security.product().Equals(p))); channels = channels.Add(p, c); } active.Add(c); } return (channels, active); }) .SelectSeries(channel_data => { var (channels, active) = channel_data; return Observable.Interval(poll_interval) .Select(_ => active.SelectMany(c => c.read_trades()).ToList()) .Where(trades => trades.Count > 0); }); public static IObservable> with_greeks( IObservable> trades_source, IObservable> greeks_tuples) => Observable.Create>(o => { IReadOnlyDictionary cached_greeks_tuples = new Dictionary(); var greeks_tuples_sub = greeks_tuples.Subscribe( greeks_tuples => cached_greeks_tuples = greeks_tuples); var trades_sub = trades_source.Subscribe(trades => { // use local copy bc cached_greeks_tuples may be re-assigned on another thread var local_greeks = cached_greeks_tuples; for (int i = 0; i < trades.Count; i++) { if (local_greeks.TryGetValue(trades[i].security.Id, out var gt)) { trades[i] = trades[i].with(greeks_tuple: gt); } } o.OnNext(trades); }); return Disposable.Create(greeks_tuples_sub, trades_sub); }); public static IObservable> with_volume( IObservable> trades_source, IObservable> security_md_source ) { var volume_source = security_md_source.Select(sec_map => new Func( id => sec_map.TryGetValue(id, out var smd) ? smd.md.total_volume : double.NaN)); return with_volume(trades_source, volume_source); } public static IObservable> with_volume( IObservable> trades_source, IObservable> volume_source ) => trades_source.CombineLatest(volume_source, (trades, get_volume) => trades.WrapSelector( t => t.with(volume: get_volume(t.security.Id)), null)); class ChannelConsumer { readonly MDConsumerChannel _channel; readonly Dictionary _security_mds; long _cursor = 0; public ChannelConsumer(MDConsumerChannel channel) => _channel = channel; public ChannelConsumer(MDConsumerChannel channel, IEnumerable security_mds) { _channel = channel; _security_mds = security_mds.ToDictionaryCrushed(s => s.id); } public IEnumerable read_trades() { foreach (var tm in _channel.read_trades(ref _cursor)) { if (tm != null && _security_mds.TryGetValue(tm.Key, out var smd)) yield return new TradeInfo( smd, new TradeMessageImmutable(tm)); } } } } }