122 lines
5.1 KiB
C#
122 lines
5.1 KiB
C#
namespace bts.BlueOrb{
|
|
using BCG;
|
|
using BCG.Utilities.General;
|
|
|
|
using bts.api.core;
|
|
|
|
using Bts.DataStreamCore;
|
|
using Bts.PubSub;
|
|
|
|
using Spark.Api.MarketData;
|
|
using Spark.Core.Reactive;
|
|
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Diagnostics;
|
|
using System.Linq;
|
|
using System.Reactive.Linq;
|
|
|
|
// TODO: Use dependency injection to instantiate this stream, and others like it that depend on
|
|
// IMarketDataClient. Until then, hide it from DSQA.
|
|
[DataStreamUserRespresentation(/*"books"*/ false)]
|
|
public class BookStream : ViewStream, IWorker {
|
|
private readonly TimeSpan _poll_period = TimeSpan.FromMilliseconds(500);
|
|
private DateTime _last_poll_time = DateTime.MinValue;
|
|
readonly IWorkScheduler scheduler;
|
|
private int _id = 0;
|
|
IReadOnlyDictionary<uint, BookTuple>? _cached_state = null;
|
|
IMarketDataClient _md_client;
|
|
readonly Stopwatch _timer = new();
|
|
private readonly IBlueTableSchema _bbo_schema = BlueDataUtility.form_schema("books",
|
|
Tuple.Create("id", TypeCode.Int32),
|
|
Tuple.Create("sec_key", TypeCode.UInt32),
|
|
Tuple.Create("sec", TypeCode.String),
|
|
Tuple.Create("level", TypeCode.UInt32),
|
|
Tuple.Create("bid_price", TypeCode.Double),
|
|
Tuple.Create("bid_sz", TypeCode.Single),
|
|
Tuple.Create("ask_price", TypeCode.Double),
|
|
Tuple.Create("ask_sz", TypeCode.Single));
|
|
|
|
// TODO: add cons for symbol, exchange etc
|
|
public BookStream([MarketDataClientParam] IMarketDataClient mdClient, IWorkScheduler scheduler) {
|
|
bts.Log.Gen.info("BookStream.cons");
|
|
this.scheduler = scheduler;
|
|
_md_client = mdClient;
|
|
}
|
|
|
|
protected override void myOnFirstSubscriber() {
|
|
scheduler.Add(this);
|
|
_md_client.Books().Subscribe(_on_books);
|
|
}
|
|
|
|
private void _log_updates(TableUpdate<BookTuple, uint> update) {
|
|
int i = 0;
|
|
foreach (BookTuple bt in update.AddedItems) {
|
|
bts.Log.Gen.info($"BookStream added: idx: {i++} bid: {bt.book.bids[0].price} / {bt.book.bids[0].size} ask: {bt.book.asks[0].price} / {bt.book.asks[0].size}");
|
|
}
|
|
foreach (Change<BookTuple> c in update.Changes) {
|
|
var bt = c.New;
|
|
bts.Log.Gen.info($"BookStream changes: idx: {i} bid: {bt.book.bids[0].price} / {bt.book.bids[0].size} ask: {bt.book.asks[0].price} / {bt.book.asks[0].size}");
|
|
}
|
|
}
|
|
|
|
private void _on_books(TableUpdate<BookTuple, uint> update) {
|
|
//_log_updates(update);
|
|
bts.Log.Gen.info($"BookStream._on_books state: {update.Index.Count()} changes: {update.Changes.Count()} added: {update.AddedItems.Count()}");
|
|
_cached_state = update.Index;
|
|
}
|
|
|
|
protected override void myOnLastSubscriber() {
|
|
scheduler.Remove(this);
|
|
}
|
|
|
|
private Dictionary<uint, Dictionary<int, int>> _id_map = new Dictionary<uint, Dictionary<int, int>>();
|
|
private int _get_id(uint skey, int depth) {
|
|
var d_map = _id_map.GetOrAdd(skey, _ => new() { {depth, _id++} });
|
|
if (!d_map.TryGetValue(depth, out int rv)) {
|
|
rv = _id++;
|
|
d_map.Add(depth, rv);
|
|
}
|
|
return rv;
|
|
}
|
|
|
|
private void _scan_bbo() {
|
|
// this shouldn be made incremental using the TableUpdate
|
|
bts.Log.Gen.info("BookStream._scan_bbo");
|
|
_timer.Restart();
|
|
if (_cached_state == null)
|
|
return;
|
|
BlueTable new_state = new BlueTable(_bbo_schema);
|
|
foreach (var (skey, bt) in _cached_state) {
|
|
for (int level = 0; level < bt.book.bids.Count; level++) {
|
|
int id = _get_id(skey, level);
|
|
IBlueRow r = new BlueRow(_bbo_schema, id, skey, bt.security.ToString(), Convert.ToUInt32(level), bt.book.bids[level].price, bt.book.bids[0].size,
|
|
bt.book.asks[level].price, bt.book.asks[level].size);
|
|
new_state.addRow(r);
|
|
}
|
|
}
|
|
DataPacket dp = null;
|
|
if (myLastValue == null) {
|
|
dp = new DataPacket(new_state, new_state, new BlueTable(_bbo_schema));
|
|
} else {
|
|
var old_dp = (IDataPacket)myLastValue;
|
|
IBlueRowCollection added_rows, del_rows;
|
|
BlueDataUtility.compute_differences_fast(new_state, old_dp.newRows, out del_rows, out added_rows);
|
|
dp = new DataPacket(new_state, added_rows, del_rows);
|
|
}
|
|
if (dp.addedRows.Count == 0 && dp.deletedRows.Count == 0)
|
|
return;
|
|
_timer.Stop();
|
|
bts.Log.Gen.info($"BookStream._scan_bbo time: {_timer.Elapsed} msecs");
|
|
myFireEvent(dp);
|
|
}
|
|
|
|
public void doWork() {
|
|
if (DateTime.Now - _last_poll_time > _poll_period) {
|
|
_last_poll_time = DateTime.Now;
|
|
_scan_bbo();
|
|
}
|
|
}
|
|
}
|
|
}
|