using System; using System.Linq; using System.Collections.Generic; using System.Collections.Concurrent; using System.Threading; using System.Threading.Tasks; using System.Reactive.Linq; using System.Reactive.Concurrency; using System.Reactive.Subjects; using bts.api; using bts.api.core; using bts.co2; namespace bts.api.md { using security_key = System.UInt32; public class MarketDataClient : IDisposable { class Data { public long last_update_time; public ChannelUpdate data; } readonly ConcurrentDictionary _data = new ConcurrentDictionary(); readonly ConcurrentDictionary _security_names = new ConcurrentDictionary(); readonly static Lazy _md = new Lazy(() => locate(), true); readonly Lazy _subscription = new Lazy(() => load_request()); private MarketDataClient() { } public static MarketDataClient create() { return new MarketDataClient(); } static MDRequest load_request() { var req = new MDRequest(); var prods = _md.Value.request_products(new VoidMessage()).Products; foreach(var prod in prods) req.Channels.Add(new SecurityChannel() { Exchange = prod.Exchange, Type = prod.Type, Symbol = prod.Symbol, Flags = 0 }); return req; } static MarketDataRelay locate() { using (var ctx = new api.core.ServiceContext()) { return ctx.locate(); } } public Security lookup(security_key key) { Data value; if(_data.TryGetValue(key, out value)) return value.data.Security; else return null; } public string lookup_security_name(security_key key) { _security_names.TryGetValue(key, out string value); return value; } public Security find_security(Func filter) { return _data.Values.Select(x => x.data.Security).Where(xx => filter(xx)).FirstOrDefault(); } public async Task find_security_async(Func filter, TimeSpan? timeout = null) { if(!timeout.HasValue) timeout = TimeSpan.FromMilliseconds(int.MaxValue-1); CancellationTokenSource src = new CancellationTokenSource(timeout.Value); Security security = await Task.Run(async () => { Security sec = null; do { sec = find_security(filter); if (sec == null) await Task.Delay(500); } while (sec == null); return sec; }, src.Token); return security ?? throw new KeyNotFoundException("Can't find security for filter"); ; } public IObservable subscribe(Product product, TimeSpan? update_interval = null, IScheduler scheduler = null) { return subscribe(product, (System.UInt32)(MDRequest.Types.Flags.Security | MDRequest.Types.Flags.Bbo | MDRequest.Types.Flags.Depth | MDRequest.Types.Flags.LastTrade), update_interval, scheduler); } public IObservable subscribe(Product product, uint flags, TimeSpan? update_interval = null, IScheduler scheduler = null) { check_subscriptions(flags, x => (x.Exchange == product.Exchange && x.Type == product.Type && x.Symbol == product.Symbol)); return _subscribe( x => (x.Exchange == product.Exchange && ((SecurityImmutable)x).security_type() == product.Type && x.Symbol == product.Symbol), update_interval, scheduler); } IObservable _subscribe(Func filter, TimeSpan? update_interval = null, IScheduler scheduler = null) { Data[] slots = new Data[0]; long local_security_count = 0; return timer_pump((last_update, observer) => { //if the number of securities has changed, or this is the first call if (slots.Length == 0 || local_security_count != _data.Count) slots = _data.Values.Where(xx => filter(xx.data.Security)).ToArray(); foreach (var slot in slots) { Data data; if (_data.TryGetValue(slot.data.Security.Id, out data)) { if (data.last_update_time > last_update) observer.OnNext(data); } } }, update_interval, scheduler).Select(x => x.data); } /// /// Poll for updates for a security on a specified update_interval, on the specified scheduler with default flags /// /// /// /// /// public IObservable subscribe(Security security, TimeSpan? update_interval = null, IScheduler scheduler = null) { return subscribe(security, (System.UInt32)(MDRequest.Types.Flags.Security | MDRequest.Types.Flags.Bbo | MDRequest.Types.Flags.Depth | MDRequest.Types.Flags.LastTrade), update_interval, scheduler); } /// /// Poll for updates for a security on a specified update_interval, on the specified scheduler /// /// /// /// /// /// public IObservable subscribe(Security security, uint flags, TimeSpan? update_interval = null, IScheduler scheduler = null) { check_subscriptions(flags, x => (x.Exchange == security.Exchange && x.Type == ((SecurityImmutable)security).security_type() && security.Symbol == security.Symbol)); return timer_pump( (last_update, observer) => { Data data; if (_data.TryGetValue(security.Id, out data)) { if (data.last_update_time > last_update) observer.OnNext(data); } }, update_interval, scheduler).Select(x => x.data); } IObservable timer_pump(Action> tick, TimeSpan? interval, IScheduler scheduler = null) { if(!interval.HasValue) interval = TimeSpan.FromMilliseconds(25); long last_update_time = 0; return Observable.Create(observer => { return Observable.Timer(interval.Value, interval.Value, scheduler ?? Scheduler.Default).Subscribe(_ => { tick(last_update_time, observer); last_update_time = DateTime.UtcNow.Ticks; }); }); } bool check_subscriptions(uint flags, Func selector) { foreach (var sub in _subscription.Value.Channels) { if (selector(sub)) { sub.Flags |= flags; return true; } } return false; } /// /// Start pumping market data for all subscribed channels and securities. Call this without await /// /// /// public async Task start_pump(TimeSpan? interval = null) { var rep = await _md.Value.request_names_async(new VoidMessage()); int count = 0; do { foreach (var s in rep.value.Securities) foreach (var ss in s.Names) _security_names[ss.Key] = ss.Value; } while (!rep.success && count++ < 5); while (true) { var reply = await _md.Value.requestmd_async(_subscription.Value); if (reply.success) { foreach (var channel in reply.value.Channels) { long now = DateTime.UtcNow.Ticks; foreach (var update in channel.Updates) { Data data; if (_data.TryGetValue(update.Security.Id, out data)) { //mergefrom concats repeated fields if (update.Book != null) data.data.Book = null; //this can tear, but the book won't tear, so maybe it's ok data.data.MergeFrom(update); data.last_update_time = now; } else { _data[update.Security.Id] = new Data() { last_update_time = now, data = update }; } } } await Task.Delay(interval ?? TimeSpan.FromMilliseconds(100), _cts.Token); } } } Task _pump_task; public void run_pump(TimeSpan? interval = null) { if (_pump_task == null) { _pump_task = this.start_pump(interval) .ContinueWith(ex => { System.Diagnostics.Debug.Assert(ex == null, ex.ToString()); } , TaskContinuationOptions.OnlyOnFaulted); } } public async void run_pump_async(TimeSpan? interval = null) { await this.start_pump(interval); } readonly CancellationTokenSource _cts = new CancellationTokenSource(); public void Dispose() { _cts.Cancel(); _cts.Dispose(); if (_md.IsValueCreated) _md.Value.Dispose(); } } }