LocalMDDaemon works now

This commit is contained in:
Taha committed 2025-03-03 00:07:31 -05:00
1 parent 649931ae3c
commit 21b725e5df
4 files changed
+165 -23

No files matched your search

+9 -9
View File
@@ -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...");
+104
View File
@@ -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();
}
}
+29 -12
View File
@@ -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<uint, SecurityMD> _server_sec_map = new Dictionary<uint, SecurityMD>();
ConcurrentDictionary<uint, SecurityMD> _server_sec_map = new ConcurrentDictionary<uint, SecurityMD>();
Dictionary<uint, SecurityMD> _fake_sec_map = new Dictionary<uint, SecurityMD>();
private Dictionary<uint, SecurityKey> _exchange_id_sec_id_map = new Dictionary<uint, SecurityKey>();
private ConcurrentDictionary<uint, SecurityKey> _exchange_id_sec_id_map = new ConcurrentDictionary<uint, SecurityKey>();
private readonly IList<IDisposable> _subs = new List<IDisposable>();
private SyncQueue<LocalMDDaemon.BBO> _waiting_bbo = new SyncQueue<LocalMDDaemon.BBO>();
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<uint, SecurityMD> 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<uint, SecurityMD>(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<uint, SecurityMD> 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;
}
+23 -2
View File
@@ -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<long, sbyte> double_to_mant_exp(double v) {
if (_is_invalid_double(v)) {
return Tuple.Create<long, sbyte>(0, 127);
}
sbyte exp = -7;
double v_m = v * Math.Pow(10, Math.Abs(exp));
if (_is_invalid_double(v_m)) {
return Tuple.Create<long, sbyte>(0, 127);
}
long m = Convert.ToInt64(v_m);
return Tuple.Create<long, sbyte>(m, exp);
}
public IntPrice(double v) :
this(double_to_mant_exp(v)) {
}
public IntPrice(Tuple<long, sbyte> t)
: this(t.Item1, t.Item2) { }
public IntPrice(long mantissa, sbyte exponent) {
_mantissa = mantissa;
_exponent = exponent;