namespace bts.BlueOrb { using BCG.Utilities.General; using bts; using bts.api; using bts.md; using BCG; using mbts.utils; using Spark.Core; using Spark.Core.Disposables; using Spark.Core.Reactive; using System; using System.Collections.Generic; using System.Reactive.Linq; [DataStreamUserRespresentation("InstrumentsStream")] public class InstrumentsStream : ViewStream, IWorker { private readonly TimeSpan _poll_period = TimeSpan.FromMilliseconds(500); private DateTime _last_poll_time = DateTime.MinValue; private ViewStreamDatabase _vsd; IReadOnlyDictionary? _cached_state; MarketDataClient _md_client; private bool _dirty = false; private readonly IBlueTableSchema _instrument_schema = BlueDataUtility.form_schema("instruments", Tuple.Create("id", TypeCode.Int32), Tuple.Create("sec_key", TypeCode.UInt32), Tuple.Create("sec", TypeCode.String), Tuple.Create("sec_type", TypeCode.String), Tuple.Create("venue", TypeCode.String), Tuple.Create("symbol", TypeCode.String), Tuple.Create("currency", TypeCode.String), Tuple.Create("cpp", TypeCode.Double), Tuple.Create("expiration", TypeCode.DateTime), Tuple.Create("strike", TypeCode.Double), Tuple.Create("callput", TypeCode.String) ); IDisposable? subscription; // TODO: add cons for symbol, exchange etc public InstrumentsStream(MarketDataClient mdClient) : this(mdClient, ViewStreamDatabase.instance) { } public InstrumentsStream(MarketDataClient mdClient, ViewStreamDatabase vsd) { bts.Log.Gen.info("InstrumentsStream.cons"); _vsd = vsd; _md_client = mdClient; } protected override void myOnFirstSubscriber() { Log.isnull(subscription); _vsd.addWorker(this); subscription = _md_client.securities().Subscribe(_on_instruments); } private void _log_updates(IReadOnlyDictionary state) { foreach (SecurityImmutable security in state.Values) { bts.Log.Gen.info($"InstrumentsStream sec: {security.ToString()}"); } } private void _on_instruments(IReadOnlyDictionary state) { _dirty = true; //_log_updates(state); // just cacching in here, will populate the rows in _poll_scan and fire the datapacket bts.Log.Gen.info($"InstrumentsStream._on_instruments state: {state.Count}"); _cached_state = state; } protected override void myOnLastSubscriber() { Disposable.dispose(ref subscription); _vsd.removeWorker(this); } private void _scan_instruments() { // this shouldn be made incremental using the TableUpdate var count = _cached_state == null ? 0 : _cached_state.Count; bts.Log.Gen.info($"InstrumentsStream._scan_instruments _cached_state: {count}"); if (_cached_state == null) return; BlueTable new_state = new BlueTable(_instrument_schema); foreach (var kv in _cached_state) { var skey = kv.Key; int id = Convert.ToInt32(skey); SecurityImmutable sec = kv.Value; bts.api.SecurityType sec_type = sec.security_type(); object exp = DBNull.Value; if (sec_type == bts.api.SecurityType.Future || sec_type == bts.api.SecurityType.Option) exp = sec.security_expiration().assert_value(); object strike = DBNull.Value; if (sec_type == bts.api.SecurityType.Option) strike = sec.strike(); object cp = DBNull.Value; if (sec_type == bts.api.SecurityType.Option) cp = sec.Option.CallPut.ToString(); IBlueRow r = new BlueRow(_instrument_schema, id, skey, sec.security_desc(), sec.security_type().ToString(), sec.display_exchange().ToString(), sec.Symbol, sec.Currency, sec.ContractSpec.CashPerPoint, exp, strike, cp); new_state.addRow(r); } DataPacket? dp; if (myLastValue == null) { dp = new DataPacket(new_state, new_state, new BlueTable(_instrument_schema)); } else { var old_dp = ExceptionAssert.of_type(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; myFireEvent(dp); } public void doWork() { if (_dirty && DateTime.Now - _last_poll_time > _poll_period) { _last_poll_time = DateTime.Now; _dirty = false; _scan_instruments(); } } } }