111 lines
4.5 KiB
C#
111 lines
4.5 KiB
C#
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<IList<TradeInfo>> create(
|
|
MDConsumer consumer,
|
|
IObservable<IReadOnlyDictionary<uint, SecurityMD>> security_md_map,
|
|
TimeSpan poll_interval) =>
|
|
security_md_map.Scan(
|
|
(channels: ImmutableDictionary<ProductImmutable, ChannelConsumer>.Empty, active: new List<ChannelConsumer>(0)),
|
|
(channel_data, map) => {
|
|
var (channels, active) = channel_data;
|
|
var products = map.Values.Select(s => s.security.product()).Distinct();
|
|
active = new List<ChannelConsumer>();
|
|
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<IList<TradeInfo>> with_greeks(
|
|
IObservable<IList<TradeInfo>> trades_source,
|
|
IObservable<IReadOnlyDictionary<uint, GreeksTuple>> greeks_tuples) =>
|
|
Observable.Create<IList<TradeInfo>>(o => {
|
|
IReadOnlyDictionary<uint, GreeksTuple> cached_greeks_tuples =
|
|
new Dictionary<uint, GreeksTuple>();
|
|
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<IList<TradeInfo>> with_volume(
|
|
IObservable<IList<TradeInfo>> trades_source,
|
|
IObservable<IReadOnlyDictionary<uint, SecurityMD>> security_md_source
|
|
) {
|
|
var volume_source = security_md_source.Select(sec_map => new Func<uint, double>(
|
|
id => sec_map.TryGetValue(id, out var smd) ? smd.md.total_volume : double.NaN));
|
|
return with_volume(trades_source, volume_source);
|
|
}
|
|
|
|
public static IObservable<IList<TradeInfo>> with_volume(
|
|
IObservable<IList<TradeInfo>> trades_source,
|
|
IObservable<Func<uint, double>> 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<uint, SecurityMD> _security_mds;
|
|
long _cursor = 0;
|
|
|
|
public ChannelConsumer(MDConsumerChannel channel) =>
|
|
_channel = channel;
|
|
|
|
public ChannelConsumer(MDConsumerChannel channel, IEnumerable<SecurityMD> security_mds) {
|
|
_channel = channel;
|
|
_security_mds = security_mds.ToDictionaryCrushed(s => s.id);
|
|
}
|
|
|
|
public IEnumerable<TradeInfo> 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));
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|