336 lines
12 KiB
C#
336 lines
12 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;
|
|
|
|
namespace bts.api.md
|
|
{
|
|
using security_key = System.UInt32;
|
|
public class MarketDataClient : IDisposable
|
|
{
|
|
class Data
|
|
{
|
|
public long last_update_time;
|
|
public ChannelUpdate data;
|
|
}
|
|
|
|
const UInt32 _all_flags = (uint)(
|
|
MDRequest.Types.Flags.Security |
|
|
MDRequest.Types.Flags.Bbo |
|
|
MDRequest.Types.Flags.Direct |
|
|
MDRequest.Types.Flags.Implied |
|
|
MDRequest.Types.Flags.LastTrade);
|
|
|
|
readonly ConcurrentDictionary<security_key, Data> _data = new ConcurrentDictionary<security_key, Data>();
|
|
readonly ConcurrentDictionary<security_key, string> _security_key_to_desc= new ConcurrentDictionary<security_key, string>();
|
|
readonly ConcurrentDictionary<string, security_key> _security_desc_to_key = new ConcurrentDictionary<string, security_key>();
|
|
readonly MDRequest _subscription;
|
|
readonly Venue _venue;
|
|
|
|
readonly static Lazy<MarketDataRelay> _md = new Lazy<MarketDataRelay>(() => Locator.locate<MarketDataRelay>(), true);
|
|
readonly static ConcurrentDictionary<api.Venue, MarketDataClient> _venue_cache = new ConcurrentDictionary<api.Venue, api.md.MarketDataClient>();
|
|
static readonly List<Product> _all_products;
|
|
static MarketDataClient()
|
|
{
|
|
_all_products = _md.Value.request_products(new VoidMessage()).Products.ToList();
|
|
}
|
|
|
|
public static IEnumerable<api.Venue> cached_exchanges => _venue_cache.Keys;
|
|
public static bool create_or_get(api.Venue exchange, out MarketDataClient md, user.AuthToken token = null)
|
|
{
|
|
if (!_venue_cache.TryGetValue(exchange, out md))
|
|
{
|
|
_venue_cache[exchange] = md = new MarketDataClient(exchange, token);
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
public MarketDataClient(bts.api.Venue exchange = Venue.Unset, user.AuthToken token = null)
|
|
{
|
|
_venue = exchange;
|
|
_subscription = load_request(exchange, token);
|
|
}
|
|
|
|
public void set_auth_token(user.AuthToken toke)
|
|
{
|
|
_subscription.Token = toke?.Token ?? string.Empty;
|
|
}
|
|
|
|
MDRequest load_request(bts.api.Venue exchange, user.AuthToken token)
|
|
{
|
|
var req = new MDRequest() { Token = token?.Token ?? string.Empty } ;
|
|
var prods = _all_products.Where(x=>x.Exchange == exchange || exchange == bts.api.Venue.Unset);
|
|
foreach(var prod in prods)
|
|
req.Channels.Add(new SecurityChannel() { Exchange = prod.Exchange, Type = prod.Type, Symbol = prod.Symbol, Flags = 0 });
|
|
return req;
|
|
}
|
|
|
|
public IEnumerable<SecurityImmutable> get_securities()
|
|
{
|
|
return _data.Values.Select(x => new SecurityImmutable(x.data.Security));
|
|
}
|
|
|
|
public SecurityImmutable? lookup(security_key key)
|
|
{
|
|
Data value;
|
|
if(_data.TryGetValue(key, out value))
|
|
return value.data.Security;
|
|
else
|
|
return null;
|
|
}
|
|
|
|
public bool lookup_security_desc(security_key key, out string security_desc)
|
|
{
|
|
return _security_key_to_desc.TryGetValue(key, out security_desc);
|
|
}
|
|
|
|
public bool lookup_security_key_from_desc(string security_desc, out security_key key)
|
|
{
|
|
return _security_desc_to_key.TryGetValue(security_desc, out key);
|
|
}
|
|
|
|
public Security find_security(Func<Security, bool> filter)
|
|
{
|
|
return _data.Values.Select(x => x.data.Security).Where(xx => filter(xx)).FirstOrDefault();
|
|
}
|
|
|
|
public async Task<Security> find_security_async(Func<Security, bool> filter, TimeSpan? timeout = null)
|
|
{
|
|
if(!timeout.HasValue) {
|
|
timeout = TimeSpan.FromMilliseconds(int.MaxValue-1);
|
|
}
|
|
CancellationTokenSource src = new CancellationTokenSource(timeout.Value);
|
|
Security security = await Task.Run<Security>(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<ChannelUpdate> subscribe(Product product,
|
|
TimeSpan? update_interval = null, IScheduler scheduler = null)
|
|
{
|
|
return subscribe(product, _all_flags, update_interval, scheduler);
|
|
}
|
|
|
|
|
|
public IObservable<ChannelUpdate> 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<ChannelUpdate> _subscribe(Func<Security, bool> filter,
|
|
TimeSpan? update_interval = null, IScheduler scheduler = null)
|
|
{
|
|
Data[] slots = new Data[0]; long local_security_count = 0;
|
|
return timer_pump<Data>((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);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Poll for updates for a security on a specified update_interval, on the specified scheduler with default flags
|
|
/// </summary>
|
|
/// <param name="security"></param>
|
|
/// <param name="update_interval"></param>
|
|
/// <param name="scheduler"></param>
|
|
/// <returns></returns>
|
|
public IObservable<ChannelUpdate> subscribe(Security security,
|
|
TimeSpan? update_interval = null, IScheduler scheduler = null)
|
|
{
|
|
return subscribe(security, _all_flags, update_interval, scheduler);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Poll for updates for a security on a specified update_interval, on the specified scheduler
|
|
/// </summary>
|
|
/// <param name="security"></param>
|
|
/// <param name="flags"></param>
|
|
/// <param name="update_interval"></param>
|
|
/// <param name="scheduler"></param>
|
|
/// <returns></returns>
|
|
public IObservable<ChannelUpdate> 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<Data>( (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<T> timer_pump<T>(Action<long, IObserver<T>> tick, TimeSpan? interval, IScheduler scheduler = null)
|
|
{
|
|
if(!interval.HasValue)
|
|
interval = TimeSpan.FromMilliseconds(25);
|
|
long 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 = DateTime.UtcNow.Ticks;
|
|
});
|
|
});
|
|
}
|
|
|
|
bool check_subscriptions(uint flags, Func<SecurityChannel, bool> selector)
|
|
{
|
|
foreach (var sub in _subscription.Channels)
|
|
{
|
|
if (selector(sub))
|
|
{
|
|
sub.Flags |= flags;
|
|
return true;
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Start pumping market data for all subscribed channels and securities. Call this without await
|
|
/// </summary>
|
|
/// <param name="interval"></param>
|
|
/// <returns></returns>
|
|
public async Task start_pump(TimeSpan? interval = null)
|
|
{
|
|
await _start_pump(interval);
|
|
}
|
|
|
|
async Task _start_pump(TimeSpan? interval = null, int num_pumps = -1)
|
|
{
|
|
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_desc_to_key[ss.Value] = ss.Key;
|
|
_security_key_to_desc[ss.Key] = ss.Value;
|
|
}
|
|
}
|
|
}
|
|
while (!rep.success && count++ < 5);
|
|
count = 0;
|
|
while (num_pumps < 0 || count++ < num_pumps)
|
|
{
|
|
var reply = await _md.Value.requestmd_async(_subscription);
|
|
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.Direct != null)
|
|
data.data.Direct = 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);
|
|
}
|
|
|
|
public void pump_once()
|
|
{
|
|
if (_pump_task == null)
|
|
{
|
|
_pump_task = this._start_pump(null, 1)
|
|
.ContinueWith(ex =>
|
|
{
|
|
System.Diagnostics.Debug.Assert(ex == null, ex.ToString());
|
|
}
|
|
, TaskContinuationOptions.OnlyOnFaulted);
|
|
}
|
|
}
|
|
|
|
|
|
readonly CancellationTokenSource _cts = new CancellationTokenSource();
|
|
public void Dispose()
|
|
{
|
|
_cts.Cancel();
|
|
_cts.Dispose();
|
|
if (_md.IsValueCreated)
|
|
_md.Value.Dispose();
|
|
}
|
|
}
|
|
}
|