Skip to content

Commit

Permalink
Merge pull request #60 from ArjunVachhani/better-types
Browse files Browse the repository at this point in the history
refactored types
  • Loading branch information
ArjunVachhani committed Jun 30, 2023
2 parents fdbdf11 + ce8dbe6 commit 53b4685
Show file tree
Hide file tree
Showing 8 changed files with 59 additions and 11 deletions.
12 changes: 7 additions & 5 deletions OrderMatcher/OrderMatcher.Performance/MatchingEngineBenchmark.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@
[MinColumn, MaxColumn, MeanColumn, MedianColumn, AllStatisticsColumn]
public class MatchingEngineBenchmark
{
private readonly static Quantity stepSize = new Quantity(0.00000001m);

[Benchmark]
public void CreateMatchingEngine()
{
MatchingEngine engine = new MatchingEngine(null, null, 0.00000001m, 8);
MatchingEngine engine = new MatchingEngine(null, null, stepSize, 8);
}

[Benchmark]
Expand Down Expand Up @@ -36,7 +38,7 @@ public void CreateOrder()
[Benchmark]
public void AddOrder()
{
MatchingEngine engine = new MatchingEngine(null, null, 0.00000001m, 8);
MatchingEngine engine = new MatchingEngine(null, null, stepSize, 8);
Order order = new Order()
{
CancelOn = 0,
Expand All @@ -63,7 +65,7 @@ public void AddOrder()
[Benchmark]
public void AddAndCancelOrder()
{
MatchingEngine engine = new MatchingEngine(null, null, 0.00000001m, 8);
MatchingEngine engine = new MatchingEngine(null, null, stepSize, 8);
Order order = new Order()
{
CancelOn = 0,
Expand Down Expand Up @@ -95,7 +97,7 @@ public void AddAndCancelOrder()
[Benchmark]
public void TenAddOrder()
{
MatchingEngine engine = new MatchingEngine(new FakeTradeListener(), new FakeFeeProvider(), 0.00000001m, 8);
MatchingEngine engine = new MatchingEngine(new FakeTradeListener(), new FakeFeeProvider(), stepSize, 8);
for (var i = 0; i < 10; i++)
{
Order order = new Order()
Expand Down Expand Up @@ -124,7 +126,7 @@ public void TenAddOrder()
[Benchmark]
public void TenAddAndCancel()
{
MatchingEngine engine = new MatchingEngine(new FakeTradeListener(), new FakeFeeProvider(), 0.00000001m, 8);
MatchingEngine engine = new MatchingEngine(new FakeTradeListener(), new FakeFeeProvider(), stepSize, 8);
for (var i = 1; i <= 10; i++)
{
Order order = new Order()
Expand Down
30 changes: 30 additions & 0 deletions OrderMatcher/OrderMatcher.Types/Amount.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,36 @@ public override int GetHashCode()
return 92834 + _amount.GetHashCode();
}

public static Amount operator -(Amount left, Amount right)
{
return new Amount(left._amount - right._amount);
}

public static Amount operator +(Amount left, Amount right)
{
return new Amount(left._amount + right._amount);
}

public static Quantity operator /(Amount amount, Price price)
{
return new Quantity(amount._amount / price.GetPrice());
}

public static Amount operator /(Amount amount, int d)
{
return new Amount(amount._amount / d);
}

public static Amount operator *(Amount amount, decimal d)
{
return new Amount(amount._amount * d);
}

public Amount Round(int decimalPlaces)
{
return Math.Round(_amount, decimalPlaces);
}

public static bool operator <(Amount left, Amount right)
{
return left._amount < right._amount;
Expand Down
6 changes: 6 additions & 0 deletions OrderMatcher/OrderMatcher.Types/Price.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ public Price(decimal price)
return c._price;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal decimal GetPrice()
{
return _price;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool operator >(Price a, Price b)
{
Expand Down
10 changes: 10 additions & 0 deletions OrderMatcher/OrderMatcher.Types/Quantity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ public Quantity(decimal quantity)
_quantity = quantity;
}

public static Amount operator *(Quantity q, Price p)
{
return new Amount(q._quantity * p.GetPrice());
}

public static Quantity operator -(Quantity a, Quantity b)
{
return a._quantity - b._quantity;
Expand All @@ -21,6 +26,11 @@ public Quantity(decimal quantity)
return a._quantity + b._quantity;
}

public static Quantity operator %(Quantity a, Quantity b)
{
return a._quantity % b._quantity;
}

public static implicit operator Quantity(decimal quantity)
{
return new Quantity(quantity);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public static void Serialize(OrderId orderId, UserId userId, Quantity quantityTo
Write(bytes.Slice(versionOffset), version);
Write(bytes.Slice(orderIdOffset), orderId);
Write(bytes.Slice(userIdOffset), userId);
Write(bytes.Slice(quantityToDecrementOffset), quantityToDecrement);
quantityToDecrement.WriteBytes(bytes.Slice(quantityToDecrementOffset));
Write(bytes.Slice(messageSequenceOffset), messageSequence);
}

Expand Down
2 changes: 1 addition & 1 deletion OrderMatcher/OrderMatcher/Book.cs
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ internal bool CheckCanFillOrder(bool isBuy, Quantity requestedQuantity, Price li
return side.CheckCanBeFilled(requestedQuantity, limitPrice);
}

internal bool CheckCanFillMarketOrderAmount(bool isBuy, Quantity orderAmount)
internal bool CheckCanFillMarketOrderAmount(bool isBuy, Amount orderAmount)
{
var side = isBuy ? _asks : _bids;
return side.CheckMarketOrderAmountCanBeFilled(orderAmount);
Expand Down
6 changes: 3 additions & 3 deletions OrderMatcher/OrderMatcher/MatchingEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -379,13 +379,13 @@ static void CancelIncomingOrder(Order incomingOrder, ITradeListener? tradeListen
throw new OrderMatcherException(Constant.NOT_EXPECTED);
}

var cost = Math.Round(maxQuantity * matchPrice, _quoteCurrencyDecimalPlaces);
var cost = (maxQuantity * matchPrice).Round(_quoteCurrencyDecimalPlaces);
restingOrder.Cost += cost;
incomingOrder.Cost += cost;
var incomingFee = _feeProvider.GetFee(incomingOrder.FeeId);
var restingFee = _feeProvider.GetFee(restingOrder.FeeId);
restingOrder.Fee += Math.Round((cost * restingFee.MakerFee) / 100, _quoteCurrencyDecimalPlaces);
incomingOrder.Fee += Math.Round((cost * incomingFee.TakerFee) / 100, _quoteCurrencyDecimalPlaces);
restingOrder.Fee += ((cost * restingFee.MakerFee) / 100).Round(_quoteCurrencyDecimalPlaces);
incomingOrder.Fee += ((cost * incomingFee.TakerFee) / 100).Round(_quoteCurrencyDecimalPlaces);
bool orderFilled = _book.FillOrder(restingOrder, maxQuantity);
bool isRestingTipAdded = false;
if (orderFilled && restingOrder.IsTip)
Expand Down
2 changes: 1 addition & 1 deletion OrderMatcher/OrderMatcher/Side.cs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ public bool CheckCanBeFilled(Quantity requestedQuantity, Price limitPrice)
return false;
}

public bool CheckMarketOrderAmountCanBeFilled(Quantity orderAmount)
public bool CheckMarketOrderAmountCanBeFilled(Amount orderAmount)
{
Amount cummulativeOrderAmount = 0;
foreach (var priceLevel in _priceLevels)
Expand Down

0 comments on commit 53b4685

Please sign in to comment.