From 21b725e5df9d5d74ccac450cd2d52cdfcf421627 Mon Sep 17 00:00:00 2001 From: Taha Date: Mon, 3 Mar 2025 00:07:31 -0500 Subject: [PATCH] LocalMDDaemon works now --- src/client/spark/Daemons/LocalMDDaemon.cs | 18 ++-- src/client/spark/Daemons/LocalMDDaemon2.cs | 104 ++++++++++++++++++++ src/client/spark/Daemons/MergedMDDaemon.cs | 41 +++++--- src/client/spark_api/MarketData/IntPrice.cs | 25 ++++- 4 files changed, 165 insertions(+), 23 deletions(-) create mode 100644 src/client/spark/Daemons/LocalMDDaemon2.cs diff --git a/src/client/spark/Daemons/LocalMDDaemon.cs b/src/client/spark/Daemons/LocalMDDaemon.cs index 3db7574bd..9fe25cf87 100644 --- a/src/client/spark/Daemons/LocalMDDaemon.cs +++ b/src/client/spark/Daemons/LocalMDDaemon.cs @@ -39,19 +39,19 @@ namespace bts { public ulong total_volume => 0; - public double settlement_price => 100.0; + public double settlement_price => Double.NaN; public DateTime settlement_date => DateTime.Now; - public double previous_settlement_price => 200.0; + public double previous_settlement_price => Double.NaN; public DateTime previous_settlement_date => DateTime.Now; - public double open_price => 300; + public double open_price => Double.NaN; public DateTime open_date => DateTime.Now; - public double close_price => 300; + public double close_price => Double.NaN; public DateTime close_date => DateTime.Now; - public double session_high => 1000; - public double session_low => 500; - public double fixing_price => 20; + public double session_high => Double.NaN; + public double session_low => Double.NaN; + public double fixing_price => Double.NaN; public bool get_bbo(ref ulong time, out NativePriceSize bid, out NativePriceSize ask) { @@ -169,8 +169,7 @@ namespace bts { private void _start_reading() { // TODO, fix this - string exe_path = //@".\dist\producer_delayed\producer_delayed.exe"; //UNCOMMENT FOR DELAYED - @"C:\Users\m_tah\Downloads\HelloWorld\HelloWorld\dist\producer_replay\producer_replay.exe"; + string exe_path = @".\dist\producer_replay\producer_replay.exe"; // delete pipe if already exists using NamedPipeClientStream old_pipe_client = new NamedPipeClientStream(".", "MyPipe", PipeDirection.In); @@ -185,6 +184,7 @@ namespace bts { // Connect to the named pipe using NamedPipeClientStream pipe_client = new NamedPipeClientStream(".", "MyPipe", PipeDirection.In); + System.Threading.Thread.Sleep(100); pipe_client.Connect(); bts.Log.Gen.info("Connected to pipe. Waiting for messages..."); diff --git a/src/client/spark/Daemons/LocalMDDaemon2.cs b/src/client/spark/Daemons/LocalMDDaemon2.cs new file mode 100644 index 000000000..a23ec7a3b --- /dev/null +++ b/src/client/spark/Daemons/LocalMDDaemon2.cs @@ -0,0 +1,104 @@ +using bts; + +using System; +using System.Diagnostics; +using System.IO; +using System.IO.Pipes; +using System.Text; + +class LocalMDDaemon2 { + static Process producerProcess = null; + static void StartOrRestartProducerProcess(string exePath) { + // foreach (var process in Process.GetProcessesByName(Path.GetFileNameWithoutExtension(exePath))) + // { + // try + // { + // Console.WriteLine($"Stopping existing producer process (PID: {process.Id})..."); + // process.Kill(); + // process.WaitForExit(); + // } + // catch (Exception ex) + // { + // Console.WriteLine($"Error stopping process: {ex.Message}"); + // } + // } + // if (producerProcess != null && !producerProcess.HasExited) + // { + // Console.WriteLine("Stopping existing producer process..."); + // producerProcess.Kill(); + // producerProcess.WaitForExit(); + // producerProcess.Dispose(); + // } + + Console.WriteLine("Starting new producer process..."); + + producerProcess = new Process(); + producerProcess.StartInfo.FileName = exePath; // Path to the EXE + producerProcess.StartInfo.UseShellExecute = false; + // producerProcess.StartInfo.RedirectStandardOutput = true; + // producerProcess.StartInfo.RedirectStandardError = true; + producerProcess.Start(); + Console.WriteLine("Producer process started."); + } + + public static void start() { + + // string exePath = @".\dist\producer\producer.exe"; //uncomment for live + // string exePath = @".\dist\producer_delayed\producer_delayed.exe"; //UNCOMMENT FOR DELAYED + string exePath = //@".\dist\producer_replay\producer_replay.exe"; //UNCOMMENT FOR replay + @"C:\Users\m_tah\Downloads\HelloWorld\HelloWorld\dist\producer_replay\producer_replay.exe"; + + + // delete pipe if already exists + // using NamedPipeClientStream oldpipeClient = new NamedPipeClientStream(".", "MyPipe", PipeDirection.In); + // try{ + // oldpipeClient.Close(); + // oldpipeClient.Dispose(); + // }catch{ + + // } + StartOrRestartProducerProcess(exePath); + + // Connect to the named pipe + using NamedPipeClientStream pipeClient = new NamedPipeClientStream(".", "MyPipe", PipeDirection.In); + + // Thread.Sleep(2000); + pipeClient.Connect(); + + Console.WriteLine("Connected to pipe. Waiting for messages..."); + + byte[] buffer = new byte[1024]; + var start = true; + while (true) { + try { + int bytesRead = pipeClient.Read(buffer, 0, buffer.Length); + + if (bytesRead == 0)// this means there was an error + { + Console.WriteLine("0 bytes recieved ending..."); + if (start) { + start = false; + continue; + } + break; + } + + double double1 = BitConverter.ToDouble(buffer, 0); + double double2 = BitConverter.ToDouble(buffer, 8); + double double3 = BitConverter.ToDouble(buffer, 16); + double double4 = BitConverter.ToDouble(buffer, 24); + uint int5 = BitConverter.ToUInt32(buffer, 32); + Log.Gen.info($"Received: {double1}, {double2}, {double3}, {double4}, {int5}"); + + } catch (IOException) { + Console.WriteLine("Pipe disconnected. Attempting to reconnect..."); + pipeClient.Dispose(); + System.Threading.Thread.Sleep(1000); + pipeClient.Connect(); // Try reconnecting + } + } + + // Console.WriteLine("Pipe closed. Exiting..."); + // producerProcess?.WaitForExit(); + } +} diff --git a/src/client/spark/Daemons/MergedMDDaemon.cs b/src/client/spark/Daemons/MergedMDDaemon.cs index af3b18c28..44c4cc3c6 100644 --- a/src/client/spark/Daemons/MergedMDDaemon.cs +++ b/src/client/spark/Daemons/MergedMDDaemon.cs @@ -1,10 +1,7 @@ using BCG; using bts.api; -using bts.api.synthetic; -using bts.collections; -using bts.md; -using bts.utils; + using mbts; using mbts.co2; @@ -14,12 +11,14 @@ using mbts.ui; using Spark.Core.Disposables; using System; +using System.Collections.Concurrent; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Composition; using System.Linq; using System.Reactive.Linq; using System.Threading.Tasks; +using System.Threading; using SecurityKey = System.UInt32; @@ -30,36 +29,52 @@ namespace bts { private readonly IBus _bus; private LocalMDDaemon _local_md_daemon; IDisposable _dispose; - Dictionary _server_sec_map = new Dictionary(); + ConcurrentDictionary _server_sec_map = new ConcurrentDictionary(); Dictionary _fake_sec_map = new Dictionary(); - private Dictionary _exchange_id_sec_id_map = new Dictionary(); + private ConcurrentDictionary _exchange_id_sec_id_map = new ConcurrentDictionary(); private readonly IList _subs = new List(); private SyncQueue _waiting_bbo = new SyncQueue(); private readonly Venue _local_md_exchange = Venue.Cboe; + private bool _localmd_daemon_inited = false; private void _init_local_md_daemon() { + if (_localmd_daemon_inited) + return; + //LocalMDDaemon2.start(); + _localmd_daemon_inited = true; _local_md_daemon = new LocalMDDaemon(); _local_md_daemon.on_bbo += (bbo_pack) => { //bts.Log.Gen.info($"MergedMDDaemon on_bbo, local bbo callback, bbo: {bbo_pack}"); _waiting_bbo.enqueue(bbo_pack); }; + System.Threading.Thread dequeue_bbo_thr = new Thread(_dequeue_bbo_thread_func); + dequeue_bbo_thr.IsBackground = true; + dequeue_bbo_thr.Priority = ThreadPriority.Normal; + dequeue_bbo_thr.Start(); } [ImportingConstructor] public MergedMDDaemon(IBus bus) { bts.Log.Gen.info("MergedMDDaemon started"); _bus = bus; - _init_local_md_daemon(); + _subs.Add(_bus.subscribe(SparkTags.ServerSecuritiesTag).OnDispatcher().Subscribe( d => _on_server_sec_map(d))); _subs.Add(_bus.subscribe(SparkTags.FakeSecuritiesTag).OnDispatcher().Subscribe( d => _on_fake_sec_map(d))); - _subs.Add(_bus.subscribe(SparkTags.SparkTimer).OnDispatcher().Subscribe( - ts => _on_timer())); + // _subs.Add(_bus.subscribe(SparkTags.SparkTimer).OnDispatcher().Subscribe( + // ts => _on_timer())); } - private void _on_timer() { - _process_local_bbo_updates(); + // private void _on_timer() { + // //_process_local_bbo_updates(); + // } + + private void _dequeue_bbo_thread_func() { + while (true) { + System.Threading.Thread.Sleep(50); + _process_local_bbo_updates(); + } } private void _process_local_bbo_updates() { @@ -107,7 +122,6 @@ namespace bts { private void _on_server_sec_map(IReadOnlyDictionary sec_map) { //_log_options(sec_map); bts.Log.Gen.info(String.Format("MergedMDDaemon._on_server_sec_map sz: {0}", sec_map.Count())); - //_server_sec_map = new Dictionary(sec_map); foreach (var kv in sec_map) { SecurityMD md = kv.Value; uint sec_id = kv.Key; @@ -121,6 +135,7 @@ namespace bts { } } _merge_and_post(); + _init_local_md_daemon(); } private void _on_fake_sec_map(IReadOnlyDictionary sec_map) { @@ -134,7 +149,9 @@ namespace bts { var sec = sec_md.security; return SecurityExpirationHelper.is_unexpired(sec); }; + // server secs var to_pub = _server_sec_map.Where(kv => is_active(kv.Value)).ToDictionary(kv => kv.Key, kv => kv.Value); + // add fake secs to the map foreach (var kv in _fake_sec_map) { to_pub[kv.Key] = kv.Value; } diff --git a/src/client/spark_api/MarketData/IntPrice.cs b/src/client/spark_api/MarketData/IntPrice.cs index 55eb74f03..7408b850e 100644 --- a/src/client/spark_api/MarketData/IntPrice.cs +++ b/src/client/spark_api/MarketData/IntPrice.cs @@ -11,10 +11,31 @@ namespace mbts.co2 { [FieldOffset(7)] readonly sbyte _exponent; - public IntPrice(double v) : - this(Double.IsNaN(v) ? 0 : Convert.ToInt64(v * Math.Pow(10, 7)), Double.IsNaN(v) ? (sbyte)127 : (sbyte)7) { + private static bool _is_invalid_double(double value) { + return (Double.IsNaN(value) || double.IsInfinity(value) || + value >= long.MaxValue || value <= long.MinValue); } + private static Tuple double_to_mant_exp(double v) { + if (_is_invalid_double(v)) { + return Tuple.Create(0, 127); + } + sbyte exp = -7; + double v_m = v * Math.Pow(10, Math.Abs(exp)); + if (_is_invalid_double(v_m)) { + return Tuple.Create(0, 127); + } + long m = Convert.ToInt64(v_m); + return Tuple.Create(m, exp); + } + + public IntPrice(double v) : + this(double_to_mant_exp(v)) { + } + + public IntPrice(Tuple t) + : this(t.Item1, t.Item2) { } + public IntPrice(long mantissa, sbyte exponent) { _mantissa = mantissa; _exponent = exponent;