Skip to content

Commit

Permalink
cost and order accepted event (#10)
Browse files Browse the repository at this point in the history
* cost and order accepted event

event for order accepted
fill and cancel returns cost, the remaining amount
moved order amount to OrderWrapper
  • Loading branch information
ArjunVachhani authored May 3, 2020
1 parent eda5c18 commit 0d15049
Show file tree
Hide file tree
Showing 28 changed files with 1,781 additions and 1,067 deletions.
1 change: 0 additions & 1 deletion OrderMatcher/OrderMatcher.Performance/Program.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using BenchmarkDotNet.Running;
using MessagePack;
using System;

namespace OrderMatcher.Performance
{
Expand Down
34 changes: 17 additions & 17 deletions OrderMatcher/OrderMatcher.Performance/SerializeBenchmark.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,24 +32,24 @@ public class SerializeBenchmark
public SerializeBenchmark()
{
book = new Book();
book.AddOrderOpenBook(new Order { CancelOn = 1000, IsBuy = true, IsTip = false, OpenQuantity = 1000, OrderId = 3434, Price = 234 });
book.AddOrderOpenBook(new Order { CancelOn = 1000, IsBuy = true, IsTip = false, OpenQuantity = 1000, OrderId = 3434, Price = 235 });
book.AddOrderOpenBook(new Order { CancelOn = 1000, IsBuy = true, IsTip = false, OpenQuantity = 1000, OrderId = 3435, Price = 236 });
book.AddOrderOpenBook(new Order { CancelOn = 1000, IsBuy = true, IsTip = false, OpenQuantity = 1000, OrderId = 3436, Price = 237 });
book.AddOrderOpenBook(new Order { CancelOn = 1000, IsBuy = true, IsTip = false, OpenQuantity = 1000, OrderId = 3437, Price = 238 });
book.AddOrderOpenBook(new Order { CancelOn = 1000, IsBuy = false, IsTip = false, OpenQuantity = 1000, OrderId = 3438, Price = 239 });
book.AddOrderOpenBook(new Order { CancelOn = 1000, IsBuy = false, IsTip = false, OpenQuantity = 1000, OrderId = 3439, Price = 240 });
book.AddOrderOpenBook(new Order { CancelOn = 1000, IsBuy = false, IsTip = false, OpenQuantity = 1000, OrderId = 3440, Price = 241 });
book.AddOrderOpenBook(new Order { CancelOn = 1000, IsBuy = false, IsTip = false, OpenQuantity = 1000, OrderId = 3441, Price = 242 });
book.AddOrderOpenBook(new Order { CancelOn = 1000, IsBuy = false, IsTip = false, OpenQuantity = 1000, OrderId = 3442, Price = 243 });
book.AddOrderOpenBook(new Order { CancelOn = 1000, IsBuy = true, IsTip = false, OpenQuantity = 1000, OrderId = 3434, Price = 234 });
book.AddOrderOpenBook(new Order { CancelOn = 1000, IsBuy = true, IsTip = false, OpenQuantity = 1000, OrderId = 3434, Price = 235 });
book.AddOrderOpenBook(new Order { CancelOn = 1000, IsBuy = true, IsTip = false, OpenQuantity = 1000, OrderId = 3435, Price = 236 });
book.AddOrderOpenBook(new Order { CancelOn = 1000, IsBuy = true, IsTip = false, OpenQuantity = 1000, OrderId = 3436, Price = 237 });
book.AddOrderOpenBook(new Order { CancelOn = 1000, IsBuy = true, IsTip = false, OpenQuantity = 1000, OrderId = 3437, Price = 238 });
book.AddOrderOpenBook(new Order { CancelOn = 1000, IsBuy = false, IsTip = false, OpenQuantity = 1000, OrderId = 3438, Price = 239 });
book.AddOrderOpenBook(new Order { CancelOn = 1000, IsBuy = false, IsTip = false, OpenQuantity = 1000, OrderId = 3439, Price = 240 });
book.AddOrderOpenBook(new Order { CancelOn = 1000, IsBuy = false, IsTip = false, OpenQuantity = 1000, OrderId = 3440, Price = 241 });
book.AddOrderOpenBook(new Order { CancelOn = 1000, IsBuy = false, IsTip = false, OpenQuantity = 1000, OrderId = 3441, Price = 242 });
book.AddOrderOpenBook(new Order { CancelOn = 1000, IsBuy = false, IsTip = false, OpenQuantity = 1000, OrderId = 3442, Price = 243 });
bid = book.BidSide.Select(x => new KeyValuePair<Price, Quantity>(x.Key, x.Value.Quantity)).ToList();
ask = book.AskSide.Select(x => new KeyValuePair<Price, Quantity>(x.Key, x.Value.Quantity)).ToList();

var order = new Order {CancelOn = 12345678, IsBuy = true,OrderId = 56789, Price = 404, OpenQuantity = 1000};
var order = new Order { CancelOn = 12345678, IsBuy = true, OrderId = 56789, Price = 404, OpenQuantity = 1000 };
var orderWrapper = new OrderWrapper() { Order = order, OrderCondition = OrderCondition.ImmediateOrCancel, StopPrice = 9534, TotalQuantity = 7878234 };
orderJsonString = JsonConvert.SerializeObject(orderWrapper);
orderBinarySerialized = OrderSerializer.Serialize(orderWrapper);
orderMsgPck = MessagePackSerializer.Serialize(new Order2 { IsBuy = true, IsTip = false, OpenQuantity = 100, OrderId = 1001, Price = 400, Quantity = 100, Sequnce = 0, StopPrice = 0 });
orderMsgPck = MessagePackSerializer.Serialize(new Order2 { IsBuy = true, IsTip = false, OpenQuantity = 100, OrderId = 1001, Price = 400, Quantity = 100, Sequnce = 0, StopPrice = 0 });

fillJsonString = JsonConvert.SerializeObject(new Fill { MakerOrderId = 10001, MatchQuantity = 2000, MatchRate = 2400, TakerOrderId = 9999, Timestamp = 10303 });
fillBinary = FillSerializer.Serialize(new Fill { MakerOrderId = 10001, MatchQuantity = 2000, MatchRate = 2400, TakerOrderId = 9999, Timestamp = 10303 });
Expand Down Expand Up @@ -171,22 +171,22 @@ public void bookBinaryDeserialize()
[Benchmark]
public void orderJsonSerialize()
{
var order = new Order {IsBuy = true, IsTip = false, OpenQuantity = 100, OrderId = 1001, Price = 400,Sequnce = 0};
var orderWrapper = new OrderWrapper() {TotalQuantity = 100 , OrderCondition = OrderCondition.None, StopPrice = 0 };
var order = new Order { IsBuy = true, IsTip = false, OpenQuantity = 100, OrderId = 1001, Price = 400, Sequnce = 0 };
var orderWrapper = new OrderWrapper() { TotalQuantity = 100, OrderCondition = OrderCondition.None, StopPrice = 0 };
var orderJsonString = JsonConvert.SerializeObject(orderWrapper);
}

[Benchmark]
public void orderMsgPckSerialize()
{
var msgPck = MessagePackSerializer.Serialize(new Order2 { IsBuy = true, IsTip = false, OpenQuantity = 100, OrderId = 1001, Price = 400, Quantity = 100, Sequnce = 0, StopPrice = 0 });
var msgPck = MessagePackSerializer.Serialize(new Order2 { IsBuy = true, IsTip = false, OpenQuantity = 100, OrderId = 1001, Price = 400, Quantity = 100, Sequnce = 0, StopPrice = 0 });
}

[Benchmark]
public void orderBinarySerialize()
{
var order = new Order {IsBuy = true, IsTip = false, OpenQuantity = 100, OrderId = 1001, Price = 400,Sequnce = 0};
var orderWrapper = new OrderWrapper() {TotalQuantity = 100 , OrderCondition = OrderCondition.None, StopPrice = 0 };
var order = new Order { IsBuy = true, IsTip = false, OpenQuantity = 100, OrderId = 1001, Price = 400, Sequnce = 0 };
var orderWrapper = new OrderWrapper() { TotalQuantity = 100, OrderCondition = OrderCondition.None, StopPrice = 0 };
var bytes = OrderSerializer.Serialize(orderWrapper);
}

Expand Down
98 changes: 49 additions & 49 deletions OrderMatcher/OrderMatcher.Tests/BookSerializerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,22 +103,22 @@ public void Deserialize_CheckCorrectBidCountAskCountForEmpty()
public void Deserialize_CheckCorrectBidCountAskCountWithPriceLevel()
{
var book = new Book();
book.AddOrderOpenBook(new Order { IsBuy = true, Price = 9, OpenQuantity = 10 });
book.AddOrderOpenBook(new Order { IsBuy = true, Price = 8, OpenQuantity = 9 });
book.AddOrderOpenBook(new Order { IsBuy = false, Price = 10, OpenQuantity = 10 });
book.AddOrderOpenBook(new Order { IsBuy = true, Price = 9, OpenQuantity = 10 });
book.AddOrderOpenBook(new Order { IsBuy = true, Price = 8, OpenQuantity = 9 });
book.AddOrderOpenBook(new Order { IsBuy = false, Price = 10, OpenQuantity = 10 });
var bytes = BookSerializer.Serialize(book, 9, 11000, DateTime.UtcNow.Ticks);
var messageLength = BitConverter.ToInt32(bytes, 0);
Assert.Equal(131, messageLength);
var bookDepth = BookSerializer.Deserialize(bytes);
Assert.Equal(11000, bookDepth.LTP);
Assert.Equal(2, bookDepth.Bid.Count);
Assert.Single(bookDepth.Ask);
Assert.Equal((Price)9, bookDepth.Bid[0].Key);
Assert.Equal((Quantity)10, bookDepth.Bid[0].Value);
Assert.Equal((Price)8, bookDepth.Bid[1].Key);
Assert.Equal((Quantity)9, bookDepth.Bid[1].Value);
Assert.Equal((Price)10, bookDepth.Ask[0].Key);
Assert.Equal((Quantity)10, bookDepth.Ask[0].Value);
Assert.Equal(9, bookDepth.Bid[0].Key);
Assert.Equal(10, bookDepth.Bid[0].Value);
Assert.Equal(8, bookDepth.Bid[1].Key);
Assert.Equal(9, bookDepth.Bid[1].Value);
Assert.Equal(10, bookDepth.Ask[0].Key);
Assert.Equal(10, bookDepth.Ask[0].Value);
}

[Fact]
Expand All @@ -134,35 +134,35 @@ public void Deserialize_CheckCorrectOnlyBuy()
Assert.Equal(11000, bookDepth.LTP);
Assert.Equal(2, bookDepth.Bid.Count);
Assert.Empty(bookDepth.Ask);
Assert.Equal((Price)9, bookDepth.Bid[0].Key);
Assert.Equal((Quantity)10, bookDepth.Bid[0].Value);
Assert.Equal((Price)8, bookDepth.Bid[1].Key);
Assert.Equal((Quantity)9, bookDepth.Bid[1].Value);
Assert.Equal(9, bookDepth.Bid[0].Key);
Assert.Equal(10, bookDepth.Bid[0].Value);
Assert.Equal(8, bookDepth.Bid[1].Key);
Assert.Equal(9, bookDepth.Bid[1].Value);
}

[Fact]
public void Deserialize_CheckCorrectOnlyAsk()
{
var book = new Book();
book.AddOrderOpenBook(new Order { IsBuy = false, Price = 10, OpenQuantity = 10 });
book.AddOrderOpenBook(new Order { IsBuy = false, Price = 10, OpenQuantity = 10 });
var bytes = BookSerializer.Serialize(book, 9, 11000, DateTime.UtcNow.Ticks);
var messageLength = BitConverter.ToInt32(bytes, 0);
Assert.Equal(67, messageLength);
var bookDepth = BookSerializer.Deserialize(bytes);
Assert.Equal(11000, bookDepth.LTP);
Assert.Empty(bookDepth.Bid);
Assert.Single(bookDepth.Ask);
Assert.Equal((Price)10, bookDepth.Ask[0].Key);
Assert.Equal((Quantity)10, bookDepth.Ask[0].Value);
Assert.Equal(10, bookDepth.Ask[0].Key);
Assert.Equal(10, bookDepth.Ask[0].Value);
}

[Fact]
public void Deserialize_CheckCorrectBidCountAskFullBook()
{
var book = new Book();
book.AddOrderOpenBook(new Order { IsBuy = true, Price = 10, OpenQuantity = 1 });
book.AddOrderOpenBook(new Order { IsBuy = true, Price = 9, OpenQuantity = 2 });
book.AddOrderOpenBook(new Order { IsBuy = true, Price = 8, OpenQuantity = 3 });
book.AddOrderOpenBook(new Order { IsBuy = true, Price = 9, OpenQuantity = 2 });
book.AddOrderOpenBook(new Order { IsBuy = true, Price = 8, OpenQuantity = 3 });
book.AddOrderOpenBook(new Order { IsBuy = true, Price = 7, OpenQuantity = 4 });
book.AddOrderOpenBook(new Order { IsBuy = true, Price = 6, OpenQuantity = 5 });
book.AddOrderOpenBook(new Order { IsBuy = true, Price = 5, OpenQuantity = 6 });
Expand All @@ -171,16 +171,16 @@ public void Deserialize_CheckCorrectBidCountAskFullBook()
book.AddOrderOpenBook(new Order { IsBuy = true, Price = 2, OpenQuantity = 9 });
book.AddOrderOpenBook(new Order { IsBuy = true, Price = 1, OpenQuantity = 10 });

book.AddOrderOpenBook(new Order { IsBuy = false, Price = 11, OpenQuantity = 11 });
book.AddOrderOpenBook(new Order { IsBuy = false, Price = 12, OpenQuantity = 12 });
book.AddOrderOpenBook(new Order { IsBuy = false, Price = 13, OpenQuantity = 13 });
book.AddOrderOpenBook(new Order { IsBuy = false, Price = 14, OpenQuantity = 14 });
book.AddOrderOpenBook(new Order { IsBuy = false, Price = 15, OpenQuantity = 15 });
book.AddOrderOpenBook(new Order { IsBuy = false, Price = 16, OpenQuantity = 16 });
book.AddOrderOpenBook(new Order { IsBuy = false, Price = 11, OpenQuantity = 11 });
book.AddOrderOpenBook(new Order { IsBuy = false, Price = 12, OpenQuantity = 12 });
book.AddOrderOpenBook(new Order { IsBuy = false, Price = 13, OpenQuantity = 13 });
book.AddOrderOpenBook(new Order { IsBuy = false, Price = 14, OpenQuantity = 14 });
book.AddOrderOpenBook(new Order { IsBuy = false, Price = 15, OpenQuantity = 15 });
book.AddOrderOpenBook(new Order { IsBuy = false, Price = 16, OpenQuantity = 16 });
book.AddOrderOpenBook(new Order { IsBuy = false, Price = 17, OpenQuantity = 17 });
book.AddOrderOpenBook(new Order { IsBuy = false, Price = 18, OpenQuantity = 18 });
book.AddOrderOpenBook(new Order { IsBuy = false, Price = 19, OpenQuantity = 19 });
book.AddOrderOpenBook(new Order { IsBuy = false, Price = 20, OpenQuantity = 20 });
book.AddOrderOpenBook(new Order { IsBuy = false, Price = 18, OpenQuantity = 18 });
book.AddOrderOpenBook(new Order { IsBuy = false, Price = 19, OpenQuantity = 19 });
book.AddOrderOpenBook(new Order { IsBuy = false, Price = 20, OpenQuantity = 20 });

var bytes = BookSerializer.Serialize(book, 5, 10, DateTime.UtcNow.Ticks);
var messageLength = BitConverter.ToInt32(bytes, 0);
Expand All @@ -189,28 +189,28 @@ public void Deserialize_CheckCorrectBidCountAskFullBook()
Assert.Equal(10, bookDepth.LTP);
Assert.Equal(5, bookDepth.Bid.Count);

Assert.Equal((Price)10, bookDepth.Bid[0].Key);
Assert.Equal((Quantity)1, bookDepth.Bid[0].Value);
Assert.Equal((Price)9, bookDepth.Bid[1].Key);
Assert.Equal((Quantity)2, bookDepth.Bid[1].Value);
Assert.Equal((Price)8, bookDepth.Bid[2].Key);
Assert.Equal((Quantity)3, bookDepth.Bid[2].Value);
Assert.Equal((Price)7, bookDepth.Bid[3].Key);
Assert.Equal((Quantity)4, bookDepth.Bid[3].Value);
Assert.Equal((Price)6, bookDepth.Bid[4].Key);
Assert.Equal((Quantity)5, bookDepth.Bid[4].Value);


Assert.Equal((Price)11, bookDepth.Ask[0].Key);
Assert.Equal((Quantity)11, bookDepth.Ask[0].Value);
Assert.Equal((Price)12, bookDepth.Ask[1].Key);
Assert.Equal((Quantity)12, bookDepth.Ask[1].Value);
Assert.Equal((Price)13, bookDepth.Ask[2].Key);
Assert.Equal((Quantity)13, bookDepth.Ask[2].Value);
Assert.Equal((Price)14, bookDepth.Ask[3].Key);
Assert.Equal((Quantity)14, bookDepth.Ask[3].Value);
Assert.Equal((Price)15, bookDepth.Ask[4].Key);
Assert.Equal((Quantity)15, bookDepth.Ask[4].Value);
Assert.Equal(10, bookDepth.Bid[0].Key);
Assert.Equal(1, bookDepth.Bid[0].Value);
Assert.Equal(9, bookDepth.Bid[1].Key);
Assert.Equal(2, bookDepth.Bid[1].Value);
Assert.Equal(8, bookDepth.Bid[2].Key);
Assert.Equal(3, bookDepth.Bid[2].Value);
Assert.Equal(7, bookDepth.Bid[3].Key);
Assert.Equal(4, bookDepth.Bid[3].Value);
Assert.Equal(6, bookDepth.Bid[4].Key);
Assert.Equal(5, bookDepth.Bid[4].Value);


Assert.Equal(11, bookDepth.Ask[0].Key);
Assert.Equal(11, bookDepth.Ask[0].Value);
Assert.Equal(12, bookDepth.Ask[1].Key);
Assert.Equal(12, bookDepth.Ask[1].Value);
Assert.Equal(13, bookDepth.Ask[2].Key);
Assert.Equal(13, bookDepth.Ask[2].Value);
Assert.Equal(14, bookDepth.Ask[3].Key);
Assert.Equal(14, bookDepth.Ask[3].Value);
Assert.Equal(15, bookDepth.Ask[4].Key);
Assert.Equal(15, bookDepth.Ask[4].Value);
}
}
}
Loading

0 comments on commit 0d15049

Please sign in to comment.