diff --git a/src/client/spark_api/Co2/Co2NativeMethods.cs b/src/client/spark_api/Co2/Co2NativeMethods.cs index 9ea47866b..2304fa71d 100644 --- a/src/client/spark_api/Co2/Co2NativeMethods.cs +++ b/src/client/spark_api/Co2/Co2NativeMethods.cs @@ -141,7 +141,7 @@ namespace mbts.co2 private static unsafe extern void raw_co2_compute_synthetic_book( byte[] security_buffer, int security_length, - byte[][] leg_books, + IntPtr[] leg_books, int[] leg_lengths, int leg_books_length, raw_receive_pb_or_string callback); @@ -151,12 +151,28 @@ namespace mbts.co2 string error = null; var security_buffer = synthetic.ToByteArray(); - var book_buffers = books.Select(b => b.ToByteArray()).ToArray(); - var lengths = book_buffers.Select(b => b.Length).ToArray(); + // var h = GCHandle.Alloc(null, GCHandleType.Pinned). + var book_buffers = + books + .Select(b => b.ToByteArray()) + .ToArray(); + GCHandle[] book_buffer_handles = + book_buffers + .Select(b => GCHandle.Alloc(b,GCHandleType.Pinned)) + .ToArray(); + IntPtr[] book_buffer_pointers = + book_buffer_handles + .Select(h => h.AddrOfPinnedObject()) + .ToArray(); + var lengths = + book_buffers + .Select(b => b.Length) + .ToArray(); + raw_co2_compute_synthetic_book( security_buffer, security_buffer.Length, - book_buffers, + book_buffer_pointers, lengths, book_buffers.Length, (byte* buffer, int length) => { diff --git a/src/client/spark_api/Co2/IntPrice.cs b/src/client/spark_api/Co2/IntPrice.cs index 989fa4063..8fef62607 100644 --- a/src/client/spark_api/Co2/IntPrice.cs +++ b/src/client/spark_api/Co2/IntPrice.cs @@ -52,6 +52,17 @@ namespace mbts.co2 { return _mantissa.GetHashCode(); } + public static IntPrice from_double(double price, sbyte precision) { + if (double.IsNaN(price)) return NaN; + if (Math.Abs(price%1) != 0) { + double rounding = 0.5 * Math.Pow(10,-precision); + price += (price > 0) ? rounding : -rounding; + price *= Math.Pow(10,precision); + return new IntPrice((long)price, (sbyte)-precision); + } + return new IntPrice((long)price, 0); + } + public static IntPrice NaN = new IntPrice(0,127); } diff --git a/src/client/spark_test/Co2/Co2Tests.cs b/src/client/spark_test/Co2/Co2Tests.cs index af5edc856..f707ae875 100644 --- a/src/client/spark_test/Co2/Co2Tests.cs +++ b/src/client/spark_test/Co2/Co2Tests.cs @@ -286,6 +286,46 @@ namespace mbts.co2.tests assert_about_equal(dt_utc_now.Second, DateTime.UtcNow.Second); } + [Test,Ignore("Sample code")] + public void merge_synthetic_book() { + Security security = new Security(); + PriceSizeMessage mkPriceSize(double price, UInt32 size) { + var iprice = IntPrice.from_double(price, 11); + return new PriceSizeMessage() { + Mantissa = iprice.mantissa, + Exponent = iprice.exponent, + Size = size + }; + } + IEnumerable mkPrices(params (double,uint)[] args) { + return args.Select((ps) => mkPriceSize(ps.Item1,ps.Item2)); + } + var leg1 = new BookMessage() { + Bids = { + mkPriceSize(2000, 10), + mkPriceSize(1999.75, 5), + mkPriceSize(1999.5, 7) + }, + Asks = { + mkPriceSize(2000.25,15), + mkPriceSize(2000.5, 5), + mkPriceSize(2001,10) + } + }.immutable(); + var leg2 = new BookMessage() { + Bids = { + mkPrices( (2003, 5), (2002.75, 15), (2002.5, 12)) + }, + Asks = { + mkPrices( (2003.25, 12), (2003.5, 3), (2003.75,5)) + } + }.immutable(); + + var result = Co2NativeTestMethods.co2_compute_synthetic_book(security.immutable(), leg1, leg2); + Assert.IsTrue(result.Bids.First().Size == 5); + Assert.IsTrue(result.Asks.First().Size == 2); + } + } }