Skip to content

Commit

Permalink
order matcher now calculates fee for maker and taker (#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
ArjunVachhani authored Jun 8, 2020
1 parent 9023481 commit d20c0b6
Show file tree
Hide file tree
Showing 17 changed files with 420 additions and 317 deletions.
31 changes: 17 additions & 14 deletions OrderMatcher/OrderMatcher.Tests/CancelledOrderSerializerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ public class CancelledOrderSerializerTests
[Fact]
public void Serialize_Doesnotthrowexception_Min()
{
var bytes = CancelledOrderSerializer.Serialize(new CancelledOrder { OrderId = OrderId.MinValue, Timestamp = int.MinValue, RemainingQuantity = int.MinValue, CancelReason = CancelReason.UserRequested, Cost = Quantity.MinValue });
var bytes = CancelledOrderSerializer.Serialize(new CancelledOrder { OrderId = OrderId.MinValue, Timestamp = int.MinValue, RemainingQuantity = int.MinValue, CancelReason = CancelReason.UserRequested, Cost = Quantity.MinValue, Fee = Quantity.MinValue });
}

[Fact]
public void Serialize_Doesnotthrowexception_Max()
{
var bytes = CancelledOrderSerializer.Serialize(new CancelledOrder { OrderId = OrderId.MaxValue, Timestamp = int.MaxValue, RemainingQuantity = int.MaxValue, CancelReason = CancelReason.ValidityExpired, Cost = Quantity.MaxValue });
var bytes = CancelledOrderSerializer.Serialize(new CancelledOrder { OrderId = OrderId.MaxValue, Timestamp = int.MaxValue, RemainingQuantity = int.MaxValue, CancelReason = CancelReason.ValidityExpired, Cost = Quantity.MaxValue, Fee = Quantity.MaxValue });
}

[Fact]
Expand All @@ -34,31 +34,31 @@ public void Deserialize_ThrowsExecption_IfNullPassed()
[Fact]
public void Deserialize_ThrowsExecption_IfMessageIsLessThan35Bytes()
{
var bytes = new byte[51];
var bytes = new byte[67];
Exception ex = Assert.Throws<Exception>(() => CancelledOrderSerializer.Deserialize(bytes));
Assert.Equal("Canceled Order Message must be of Size : 52", ex.Message);
Assert.Equal("Canceled Order Message must be of Size : 68", ex.Message);
}

[Fact]
public void Deserialize_ThrowsExecption_IfMessageIsGreaterThan35Bytes()
{
var bytes = new byte[53];
var bytes = new byte[69];
Exception ex = Assert.Throws<Exception>(() => CancelledOrderSerializer.Deserialize(bytes));
Assert.Equal("Canceled Order Message must be of Size : 52", ex.Message);
Assert.Equal("Canceled Order Message must be of Size : 68", ex.Message);
}

[Fact]
public void Deserialize_ThrowsExecption_IfMessageIsNothaveValidType()
{
var bytes = new byte[52];
var bytes = new byte[68];
Exception ex = Assert.Throws<Exception>(() => CancelledOrderSerializer.Deserialize(bytes));
Assert.Equal("Invalid Message", ex.Message);
}

[Fact]
public void Deserialize_ThrowsExecption_IfVersionIsNotSet()
{
var bytes = new byte[52];
var bytes = new byte[68];
bytes[4] = (byte)MessageType.Cancel;
Exception ex = Assert.Throws<Exception>(() => CancelledOrderSerializer.Deserialize(bytes));
Assert.Equal("version mismatch", ex.Message);
Expand All @@ -67,44 +67,47 @@ public void Deserialize_ThrowsExecption_IfVersionIsNotSet()
[Fact]
public void Deserialize_Doesnotthrowexception_Min()
{
var bytes = CancelledOrderSerializer.Serialize(new CancelledOrder { OrderId = OrderId.MinValue, Timestamp = int.MinValue, RemainingQuantity = int.MinValue, CancelReason = CancelReason.UserRequested, Cost = Quantity.MinValue });
var bytes = CancelledOrderSerializer.Serialize(new CancelledOrder { OrderId = OrderId.MinValue, Timestamp = int.MinValue, RemainingQuantity = int.MinValue, CancelReason = CancelReason.UserRequested, Cost = Quantity.MinValue, Fee = Quantity.MinValue });
var messageLength = BitConverter.ToInt32(bytes, 0);
Assert.Equal(52, messageLength);
Assert.Equal(68, messageLength);
var cancelledOrder = CancelledOrderSerializer.Deserialize(bytes);
Assert.Equal(OrderId.MinValue, cancelledOrder.OrderId);
Assert.Equal(int.MinValue, cancelledOrder.RemainingQuantity);
Assert.Equal(int.MinValue, cancelledOrder.Timestamp);
Assert.Equal(CancelReason.UserRequested, cancelledOrder.CancelReason);
Assert.Equal(Quantity.MinValue, cancelledOrder.Cost);
Assert.Equal(Quantity.MinValue, cancelledOrder.Fee);
}

[Fact]
public void Deserialize_Doesnotthrowexception_Max()
{
var bytes = CancelledOrderSerializer.Serialize(new CancelledOrder { OrderId = OrderId.MaxValue, Timestamp = int.MaxValue, RemainingQuantity = int.MaxValue, CancelReason = CancelReason.ValidityExpired, Cost = Quantity.MaxValue });
var bytes = CancelledOrderSerializer.Serialize(new CancelledOrder { OrderId = OrderId.MaxValue, Timestamp = int.MaxValue, RemainingQuantity = int.MaxValue, CancelReason = CancelReason.ValidityExpired, Cost = Quantity.MaxValue, Fee = Quantity.MaxValue });

var messageLength = BitConverter.ToInt32(bytes, 0);
Assert.Equal(52, messageLength);
Assert.Equal(68, messageLength);
var cancelledOrder = CancelledOrderSerializer.Deserialize(bytes);
Assert.Equal(OrderId.MaxValue, cancelledOrder.OrderId);
Assert.Equal(int.MaxValue, cancelledOrder.RemainingQuantity);
Assert.Equal(int.MaxValue, cancelledOrder.Timestamp);
Assert.Equal(CancelReason.ValidityExpired, cancelledOrder.CancelReason);
Assert.Equal(Quantity.MaxValue, cancelledOrder.Cost);
Assert.Equal(Quantity.MaxValue, cancelledOrder.Fee);
}

[Fact]
public void Deserialize_Doesnotthrowexception()
{
var bytes = CancelledOrderSerializer.Serialize(new CancelledOrder { OrderId = 12345678, RemainingQuantity = 56789, Timestamp = 404, CancelReason = CancelReason.ValidityExpired, Cost = 12.13m });
var bytes = CancelledOrderSerializer.Serialize(new CancelledOrder { OrderId = 12345678, RemainingQuantity = 56789, Timestamp = 404, CancelReason = CancelReason.ValidityExpired, Cost = 12.13m, Fee = 92.005m });
var messageLength = BitConverter.ToInt32(bytes, 0);
Assert.Equal(52, messageLength);
Assert.Equal(68, messageLength);
var cancelledOrder = CancelledOrderSerializer.Deserialize(bytes);
Assert.Equal((OrderId)12345678, cancelledOrder.OrderId);
Assert.Equal(56789, cancelledOrder.RemainingQuantity);
Assert.Equal(404, cancelledOrder.Timestamp);
Assert.Equal(CancelReason.ValidityExpired, cancelledOrder.CancelReason);
Assert.Equal((Quantity)12.13m, cancelledOrder.Cost);
Assert.Equal((Quantity)92.005m, cancelledOrder.Fee);
}
}
}
46 changes: 28 additions & 18 deletions OrderMatcher/OrderMatcher.Tests/FillSerializerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ public class FillSerializerTests
[Fact]
public void Serialize_Doesnotthrowexception_Min()
{
var bytes = FillSerializer.Serialize(new Fill { MakerOrderId = OrderId.MinValue, TakerOrderId = OrderId.MinValue, Timestamp = int.MinValue, MatchQuantity = int.MinValue, MatchRate = int.MinValue, BidCost = Quantity.MinValue, AskRemainingQuantity = Quantity.MinValue });
var bytes = FillSerializer.Serialize(new Fill { MakerOrderId = OrderId.MinValue, TakerOrderId = OrderId.MinValue, Timestamp = int.MinValue, MatchQuantity = int.MinValue, MatchRate = int.MinValue, BidCost = Quantity.MinValue, BidFee = Quantity.MinValue, AskRemainingQuantity = Quantity.MinValue, AskFee = Quantity.MinValue });
}

[Fact]
public void Serialize_Doesnotthrowexception_Max()
{
var bytes = FillSerializer.Serialize(new Fill { MakerOrderId = OrderId.MaxValue, TakerOrderId = OrderId.MaxValue, Timestamp = int.MaxValue, MatchQuantity = int.MaxValue, MatchRate = int.MaxValue, BidCost = Quantity.MaxValue, AskRemainingQuantity = Quantity.MaxValue });
var bytes = FillSerializer.Serialize(new Fill { MakerOrderId = OrderId.MaxValue, TakerOrderId = OrderId.MaxValue, Timestamp = int.MaxValue, MatchQuantity = int.MaxValue, MatchRate = int.MaxValue, BidCost = Quantity.MaxValue, BidFee = Quantity.MaxValue, AskRemainingQuantity = Quantity.MaxValue, AskFee = Quantity.MaxValue });
}

[Fact]
Expand All @@ -34,31 +34,31 @@ public void Deserialize_ThrowsExecption_IfNullPassed()
[Fact]
public void Deserialize_ThrowsExecption_IfMessageIsLessThan35Bytes()
{
var bytes = new byte[92];
var bytes = new byte[126];
Exception ex = Assert.Throws<Exception>(() => FillSerializer.Deserialize(bytes));
Assert.Equal("Fill Message must be of Size : 93", ex.Message);
Assert.Equal("Fill Message must be of Size : 127", ex.Message);
}

[Fact]
public void Deserialize_ThrowsExecption_IfMessageIsGreaterThan35Bytes()
{
var bytes = new byte[94];
var bytes = new byte[128];
Exception ex = Assert.Throws<Exception>(() => FillSerializer.Deserialize(bytes));
Assert.Equal("Fill Message must be of Size : 93", ex.Message);
Assert.Equal("Fill Message must be of Size : 127", ex.Message);
}

[Fact]
public void Deserialize_ThrowsExecption_IfMessageIsNothaveValidType()
{
var bytes = new byte[93];
var bytes = new byte[127];
Exception ex = Assert.Throws<Exception>(() => FillSerializer.Deserialize(bytes));
Assert.Equal("Invalid Message", ex.Message);
}

[Fact]
public void Deserialize_ThrowsExecption_IfVersionIsNotSet()
{
var bytes = new byte[93];
var bytes = new byte[127];
bytes[4] = (byte)MessageType.Fill;
Exception ex = Assert.Throws<Exception>(() => FillSerializer.Deserialize(bytes));
Assert.Equal("version mismatch", ex.Message);
Expand All @@ -67,25 +67,27 @@ public void Deserialize_ThrowsExecption_IfVersionIsNotSet()
[Fact]
public void Deserialize_Doesnotthrowexception_Min()
{
var bytes = FillSerializer.Serialize(new Fill { MakerOrderId = OrderId.MinValue, TakerOrderId = OrderId.MinValue, Timestamp = int.MinValue, MatchQuantity = int.MinValue, MatchRate = int.MinValue, BidCost = Quantity.MinValue, AskRemainingQuantity = Quantity.MinValue });
var bytes = FillSerializer.Serialize(new Fill { MakerOrderId = OrderId.MinValue, TakerOrderId = OrderId.MinValue, Timestamp = int.MinValue, MatchQuantity = int.MinValue, MatchRate = int.MinValue, BidCost = Quantity.MinValue, BidFee = Quantity.MinValue, AskRemainingQuantity = Quantity.MinValue, AskFee = Quantity.MinValue });
var messageLength = BitConverter.ToInt32(bytes, 0);
Assert.Equal(93, messageLength);
Assert.Equal(127, messageLength);
var fill = FillSerializer.Deserialize(bytes);
Assert.Equal(OrderId.MinValue, fill.MakerOrderId);
Assert.Equal(OrderId.MinValue, fill.TakerOrderId);
Assert.Equal(int.MinValue, fill.MatchRate);
Assert.Equal(int.MinValue, fill.MatchQuantity);
Assert.Equal(int.MinValue, fill.Timestamp);
Assert.Equal(Quantity.MinValue, fill.BidCost);
Assert.Equal(Quantity.MinValue, fill.BidFee);
Assert.Equal(Quantity.MinValue, fill.AskRemainingQuantity);
Assert.Equal(Quantity.MinValue, fill.AskFee);
}

[Fact]
public void Deserialize_Doesnotthrowexception_Max()
{
var bytes = FillSerializer.Serialize(new Fill { MakerOrderId = OrderId.MaxValue, TakerOrderId = OrderId.MaxValue, Timestamp = int.MaxValue, MatchQuantity = int.MaxValue, MatchRate = int.MaxValue, BidCost = Quantity.MaxValue, AskRemainingQuantity = Quantity.MaxValue });
var bytes = FillSerializer.Serialize(new Fill { MakerOrderId = OrderId.MaxValue, TakerOrderId = OrderId.MaxValue, Timestamp = int.MaxValue, MatchQuantity = int.MaxValue, MatchRate = int.MaxValue, BidCost = Quantity.MaxValue, BidFee = Quantity.MaxValue, AskRemainingQuantity = Quantity.MaxValue, AskFee = Quantity.MaxValue });
var messageLength = BitConverter.ToInt32(bytes, 0);
Assert.Equal(93, messageLength);
Assert.Equal(127, messageLength);
var fill = FillSerializer.Deserialize(bytes);
Assert.Equal(OrderId.MaxValue, fill.MakerOrderId);
Assert.Equal(OrderId.MaxValue, fill.TakerOrderId);
Expand All @@ -94,54 +96,62 @@ public void Deserialize_Doesnotthrowexception_Max()
Assert.Equal(int.MaxValue, fill.Timestamp);
Assert.Equal(Quantity.MaxValue, fill.AskRemainingQuantity);
Assert.Equal(Quantity.MaxValue, fill.BidCost);
Assert.Equal(Quantity.MaxValue, fill.AskFee);
Assert.Equal(Quantity.MaxValue, fill.BidFee);
}

[Fact]
public void Deserialize_Doesnotthrowexception()
{
var bytes = FillSerializer.Serialize(new Fill { MakerOrderId = 12345678, TakerOrderId = 56789, Timestamp = 404, MatchQuantity = 2356, MatchRate = 9534, BidCost = 4347, AskRemainingQuantity = 87135 });
var bytes = FillSerializer.Serialize(new Fill { MakerOrderId = 12345678, TakerOrderId = 56789, Timestamp = 404, MatchQuantity = 2356, MatchRate = 9534, BidCost = 4347, BidFee = 76157, AskRemainingQuantity = 87135, AskFee = 12103 });
var messageLength = BitConverter.ToInt32(bytes, 0);
Assert.Equal(93, messageLength);
Assert.Equal(127, messageLength);
var fill = FillSerializer.Deserialize(bytes);
Assert.Equal((OrderId)12345678, fill.MakerOrderId);
Assert.Equal((OrderId)56789, fill.TakerOrderId);
Assert.Equal(9534, fill.MatchRate);
Assert.Equal(2356, fill.MatchQuantity);
Assert.Equal(404, fill.Timestamp);
Assert.Equal(4347, fill.BidCost);
Assert.Equal(76157, fill.BidFee);
Assert.Equal(87135, fill.AskRemainingQuantity);
Assert.Equal(12103, fill.AskFee);
}

[Fact]
public void Deserialize_Doesnotthrowexception_BidCostNull()
{
var bytes = FillSerializer.Serialize(new Fill { MakerOrderId = 12345678, TakerOrderId = 56789, Timestamp = 404, MatchQuantity = 2356, MatchRate = 9534, BidCost = null, AskRemainingQuantity = 87135 });
var bytes = FillSerializer.Serialize(new Fill { MakerOrderId = 12345678, TakerOrderId = 56789, Timestamp = 404, MatchQuantity = 2356, MatchRate = 9534, BidCost = null, BidFee = null, AskRemainingQuantity = 87135, AskFee = 5434 });
var messageLength = BitConverter.ToInt32(bytes, 0);
Assert.Equal(93, messageLength);
Assert.Equal(127, messageLength);
var fill = FillSerializer.Deserialize(bytes);
Assert.Equal((OrderId)12345678, fill.MakerOrderId);
Assert.Equal((OrderId)56789, fill.TakerOrderId);
Assert.Equal(9534, fill.MatchRate);
Assert.Equal(2356, fill.MatchQuantity);
Assert.Equal(404, fill.Timestamp);
Assert.Null(fill.BidCost);
Assert.Null(fill.BidFee);
Assert.Equal(87135, fill.AskRemainingQuantity);
Assert.Equal(5434, fill.AskFee);
}

[Fact]
public void Deserialize_Doesnotthrowexception_AskRemainingQuantityNull()
{
var bytes = FillSerializer.Serialize(new Fill { MakerOrderId = 12345678, TakerOrderId = 56789, Timestamp = 404, MatchQuantity = 2356, MatchRate = 9534, BidCost = 4347, AskRemainingQuantity = null });
var bytes = FillSerializer.Serialize(new Fill { MakerOrderId = 12345678, TakerOrderId = 56789, Timestamp = 404, MatchQuantity = 2356, MatchRate = 9534, BidCost = 4347, BidFee = 891434, AskRemainingQuantity = null, AskFee = null });
var messageLength = BitConverter.ToInt32(bytes, 0);
Assert.Equal(93, messageLength);
Assert.Equal(127, messageLength);
var fill = FillSerializer.Deserialize(bytes);
Assert.Equal((OrderId)12345678, fill.MakerOrderId);
Assert.Equal((OrderId)56789, fill.TakerOrderId);
Assert.Equal(9534, fill.MatchRate);
Assert.Equal(2356, fill.MatchQuantity);
Assert.Equal(404, fill.Timestamp);
Assert.Equal(4347, fill.BidCost);
Assert.Equal(891434, fill.BidFee);
Assert.Null(fill.AskRemainingQuantity);
Assert.Null(fill.AskFee);
}
}
}
Loading

0 comments on commit d20c0b6

Please sign in to comment.