Add merge_synthetic_book sample test code
This commit is contained in:
1 parent
8095100bee
commit
e39313ecf6
3 files changed
+71
-4
No files matched your search
@@ -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) => {
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
|
||||
@@ -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<PriceSizeMessage> 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);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user