249 lines
9.5 KiB
C#
249 lines
9.5 KiB
C#
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;
|
|
using mbts;
|
|
using SecurityKey = System.UInt32;
|
|
|
|
namespace bts.md
|
|
{
|
|
|
|
public class MarketDataClient : IDisposable
|
|
{
|
|
static uint naggle_ms = 100;
|
|
readonly static bts.md.MDSource _subscription = new MDSource(naggle_ms);
|
|
|
|
readonly ConcurrentDictionary<SecurityKey, SecurityMD> _data = new ConcurrentDictionary<SecurityKey, SecurityMD>();
|
|
readonly List<IDisposable> _disp = new List<IDisposable>();
|
|
readonly uint _default_pump_ms;
|
|
public MarketDataClient(uint default_pump_ms = 250, AuthTokenImmutable? token = null)
|
|
{
|
|
_default_pump_ms = default_pump_ms;
|
|
if (token != null)
|
|
set_auth_token(token);
|
|
|
|
_disp.Add(_subscription.on_securities.Subscribe( md =>
|
|
{
|
|
foreach (var entry in md)
|
|
_data.AddOrUpdate(entry.Key, entry.Value, (_, __) => entry.Value);
|
|
}));
|
|
}
|
|
public IEnumerable<SecurityImmutable> securities => _data.Values.Select(x => x.security);
|
|
public IObservable<IEnumerable<SecurityImmutable>> on_securities
|
|
{
|
|
get { return _subscription.on_securities.Select(x => x.Values.Select(xx => xx.security)); }
|
|
}
|
|
|
|
public void set_auth_token(AuthTokenImmutable? toke)
|
|
{
|
|
if(toke.HasValue)
|
|
_subscription.set_token(toke.Value);
|
|
}
|
|
public IObservable< IEnumerable<IMDBook> > subscribe_book(Func<SecurityImmutable, bool> filter,
|
|
TimeSpan? update_interval = null, IScheduler scheduler = null)
|
|
{
|
|
return _subscribe<MDBook>(filter, update_interval, scheduler);
|
|
}
|
|
public IObservable< IEnumerable<IMDLast> > subscribe_last(Func<SecurityImmutable, bool> filter,
|
|
TimeSpan? update_interval = null, IScheduler scheduler = null)
|
|
{
|
|
return _subscribe<MDLast>(filter, update_interval, scheduler);
|
|
}
|
|
public IObservable< IEnumerable<IMDMisc> > subscribe_misc(Func<SecurityImmutable, bool> filter,
|
|
TimeSpan? update_interval = null, IScheduler scheduler = null)
|
|
{
|
|
return _subscribe<MDMisc>(filter, update_interval, scheduler);
|
|
}
|
|
public IObservable< IEnumerable<IMDCombined> > subscribe_combined(Func<SecurityImmutable, bool> filter,
|
|
TimeSpan? update_interval = null, IScheduler scheduler = null)
|
|
{
|
|
return _subscribe<MDCombined>(filter, update_interval, scheduler);
|
|
}
|
|
|
|
IObservable<IEnumerable<T>> _subscribe<T>(Func<SecurityImmutable, bool> filter,
|
|
TimeSpan? update_interval = null, IScheduler scheduler = null) where T : IMDData, IMDUpdate, new()
|
|
{
|
|
List<Slot<T>> slots = new List<Slot<T>>(); int data_size = 0;
|
|
return timer_pump<IEnumerable<T>>((last_timer_tick, observer) =>
|
|
{
|
|
//if the number of securities has changed, or this is the first call
|
|
if (data_size != _data.Count)
|
|
{
|
|
slots = _data.Values.Where(x => filter(x.security)).Select(xx => new Slot<T>(xx)).ToList();
|
|
data_size = _data.Count;
|
|
}
|
|
List<T> updates = new List<T>();
|
|
foreach (var slot in slots)
|
|
{
|
|
bool did_update = slot.data.update(slot.secmd, ref slot.last_update_time);
|
|
//if (slot.last_update_time > last_timer_tick)
|
|
if(did_update)
|
|
updates.Add(slot.data);
|
|
}
|
|
if(updates.Count > 0)
|
|
observer.OnNext(updates);
|
|
},
|
|
update_interval, scheduler);
|
|
}
|
|
|
|
public IObservable<IEnumerable<IMDBook>> subscribe_book(IEnumerable<SecurityKey> ids, TimeSpan? update_interval = null,
|
|
IScheduler scheduler = null)
|
|
{
|
|
return _subscribe<MDBook>(ids, update_interval, scheduler);
|
|
}
|
|
public IObservable<IEnumerable<IMDLast>> subscribe_last(IEnumerable<SecurityKey> ids, TimeSpan? update_interval = null,
|
|
IScheduler scheduler = null)
|
|
{
|
|
return _subscribe<MDLast>(ids, update_interval, scheduler);
|
|
}
|
|
public IObservable<IEnumerable<IMDMisc>> subscribe_misc(IEnumerable<SecurityKey> ids, TimeSpan? update_interval = null,
|
|
IScheduler scheduler = null)
|
|
{
|
|
return _subscribe<MDMisc>(ids, update_interval, scheduler);
|
|
}
|
|
public IObservable<IEnumerable<IMDCombined>> subscribe_combined(IEnumerable<SecurityKey> ids, TimeSpan? update_interval = null,
|
|
IScheduler scheduler = null)
|
|
{
|
|
return _subscribe<MDCombined>(ids, update_interval, scheduler);
|
|
}
|
|
|
|
IObservable<IEnumerable<T>> _subscribe<T>(IEnumerable<SecurityKey> ids, TimeSpan? update_interval = null,
|
|
IScheduler scheduler = null) where T : IMDData, IMDUpdate, new()
|
|
{
|
|
List<Slot<T>> slots = ids.Where(ii => _data.TryGetValue(ii, out SecurityMD _)).Select(x =>
|
|
{
|
|
_data.TryGetValue(x, out SecurityMD smd);
|
|
return new Slot<T>(smd);
|
|
}).ToList();
|
|
return timer_pump<IEnumerable<T>>((last_timer_tick, observer) =>
|
|
{
|
|
List<T> updates = new List<T>();
|
|
foreach (var slot in slots)
|
|
{
|
|
bool did_update = slot.data.update(slot.secmd, ref slot.last_update_time);
|
|
//if (slot.last_update_time > last_timer_tick)
|
|
if(did_update)
|
|
updates.Add(slot.data);
|
|
}
|
|
if(updates.Count > 0)
|
|
observer.OnNext(updates);
|
|
},
|
|
update_interval, scheduler);
|
|
}
|
|
|
|
class Slot<T> where T : IMDData, IMDUpdate, new()
|
|
{
|
|
public Slot(SecurityMD md)
|
|
{
|
|
secmd = md;
|
|
last_update_time = 0;
|
|
data = new T();
|
|
}
|
|
public SecurityMD secmd;
|
|
public ulong last_update_time;
|
|
public T data;
|
|
}
|
|
|
|
IObservable<T> timer_pump<T>(Action<ulong, IObserver<T>> tick, TimeSpan? interval, IScheduler scheduler = null)
|
|
{
|
|
if(!interval.HasValue)
|
|
interval = TimeSpan.FromMilliseconds(_default_pump_ms);
|
|
ulong last_update_time = 0;
|
|
return Observable.Create<T>(observer =>
|
|
{
|
|
return Observable.Timer(interval.Value, interval.Value, scheduler ?? Scheduler.Default).Subscribe(_ =>
|
|
{
|
|
tick(last_update_time, observer);
|
|
last_update_time = (ulong)DateTime.UtcNow.Ticks;
|
|
});
|
|
});
|
|
}
|
|
|
|
public void Dispose()
|
|
{
|
|
_disp.ForEach(x => x.Dispose());
|
|
}
|
|
|
|
#region security search
|
|
|
|
public SecurityImmutable? lookup(SecurityKey key)
|
|
{
|
|
if (_data.TryGetValue(key, out SecurityMD value))
|
|
return value.security;
|
|
else
|
|
return null;
|
|
}
|
|
|
|
public bool lookup_security_desc(SecurityKey key, out string security_desc)
|
|
{
|
|
security_desc = null;
|
|
var sec = lookup(key);
|
|
if (sec.HasValue)
|
|
{
|
|
security_desc = sec.Value.security_desc();
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
public bool lookup_security_key_from_desc(string security_desc, out SecurityKey key)
|
|
{
|
|
key = 0;
|
|
var fnd = find(x => x.security_desc() == security_desc).TryFirst();
|
|
if (fnd.HasValue)
|
|
{
|
|
key = fnd.Value.security.Id;
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
IEnumerable<SecurityMD> find(Func<SecurityImmutable, bool> filter)
|
|
{
|
|
Func<SecurityMD, bool> filt = (SecurityMD secmd) => filter(secmd.security);
|
|
return _data.Values.Where(filt);
|
|
}
|
|
|
|
public SecurityImmutable? find_security(Func<SecurityImmutable, bool> filter)
|
|
{
|
|
var fnd = find(filter).TryFirst();
|
|
return fnd?.security;
|
|
}
|
|
|
|
public async Task<SecurityImmutable?> find_security_async(Func<SecurityImmutable, bool> filter, TimeSpan? timeout = null)
|
|
{
|
|
if (!timeout.HasValue)
|
|
{
|
|
timeout = TimeSpan.FromMilliseconds(10000);
|
|
}
|
|
CancellationTokenSource src = new CancellationTokenSource(timeout.Value);
|
|
SecurityImmutable? security = await Task.Run<SecurityImmutable?>(async () =>
|
|
{
|
|
SecurityImmutable? 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"); ;
|
|
}
|
|
|
|
|
|
#endregion
|
|
}
|
|
}
|