Skip to content

Commit

Permalink
Remove excesive locking
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin-Molinero committed Oct 18, 2023
1 parent c28110e commit 963d926
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 139 deletions.
75 changes: 41 additions & 34 deletions QuantConnect.KrakenBrokerage/KrakenBrokerage.DataQueueHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -269,24 +269,28 @@ private void ProcessOrderBookSnapshot(Symbol symbol, JToken book)
else
{
orderBook.BestBidAskUpdated -= OnBestBidAskUpdated;
orderBook.Clear();
}

foreach (var entry in book["as"])
{
var bidAsk = entry.ToObject<KrakenBidAsk>();
orderBook.UpdateAskRow(bidAsk.Price, bidAsk.Volume);
}

foreach (var entry in book["bs"])
lock(orderBook)
{
var bidAsk = entry.ToObject<KrakenBidAsk>();
orderBook.UpdateBidRow(bidAsk.Price, bidAsk.Volume);
}
orderBook.Clear();

foreach (var entry in book["as"])
{
var bidAsk = entry.ToObject<KrakenBidAsk>();
orderBook.UpdateAskRow(bidAsk.Price, bidAsk.Volume);
}

foreach (var entry in book["bs"])
{
var bidAsk = entry.ToObject<KrakenBidAsk>();
orderBook.UpdateBidRow(bidAsk.Price, bidAsk.Volume);
}

orderBook.BestBidAskUpdated += OnBestBidAskUpdated;
orderBook.BestBidAskUpdated += OnBestBidAskUpdated;

EmitQuoteTick(symbol, orderBook.BestBidPrice, orderBook.BestBidSize, orderBook.BestAskPrice, orderBook.BestAskSize);
EmitQuoteTick(symbol, orderBook.BestBidPrice, orderBook.BestBidSize, orderBook.BestAskPrice, orderBook.BestAskSize);
}
}
catch (Exception e)
{
Expand All @@ -306,34 +310,37 @@ private void ProcessOrderBookUpdate(Symbol symbol, JToken book)
{
var orderBook = _orderBooks[symbol];

if (book["a"] != null)
lock(orderBook)
{
foreach (var rawAsk in book["a"])
if (book["a"] != null)
{
var ask = rawAsk.ToObject<KrakenBidAsk>();
if (ask.Volume == 0)
{
orderBook.RemoveAskRow(ask.Price);
}
else
foreach (var rawAsk in book["a"])
{
orderBook.UpdateAskRow(ask.Price, ask.Volume);
var ask = rawAsk.ToObject<KrakenBidAsk>();
if (ask.Volume == 0)
{
orderBook.RemoveAskRow(ask.Price);
}
else
{
orderBook.UpdateAskRow(ask.Price, ask.Volume);
}
}
}
}

if (book["b"] != null)
{
foreach (var rawBid in book["b"])

if (book["b"] != null)
{
var bid = rawBid.ToObject<KrakenBidAsk>();
if (bid.Volume == 0)
{
orderBook.RemoveBidRow(bid.Price);
}
else
foreach (var rawBid in book["b"])
{
orderBook.UpdateBidRow(bid.Price, bid.Volume);
var bid = rawBid.ToObject<KrakenBidAsk>();
if (bid.Volume == 0)
{
orderBook.RemoveBidRow(bid.Price);
}
else
{
orderBook.UpdateBidRow(bid.Price, bid.Volume);
}
}
}
}
Expand Down
155 changes: 50 additions & 105 deletions QuantConnect.KrakenBrokerage/KrakenOrderBook.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ public class KrakenOrderBook : IOrderBookUpdater<decimal, decimal>
private decimal _bestBidSize;
private decimal _bestAskPrice;
private decimal _bestAskSize;
private readonly object _locker = new object();
private readonly Symbol _symbol;

/// <summary>
Expand All @@ -51,58 +50,22 @@ public class KrakenOrderBook : IOrderBookUpdater<decimal, decimal>
/// <summary>
/// The best bid price
/// </summary>
public decimal BestBidPrice
{
get
{
lock (_locker)
{
return _bestBidPrice;
}
}
}
public decimal BestBidPrice => _bestBidPrice;

/// <summary>
/// The best bid size
/// </summary>
public decimal BestBidSize
{
get
{
lock (_locker)
{
return _bestBidSize;
}
}
}
public decimal BestBidSize => _bestBidSize;

/// <summary>
/// The best ask price
/// </summary>
public decimal BestAskPrice
{
get
{
lock (_locker)
{
return _bestAskPrice;
}
}
}
public decimal BestAskPrice => _bestAskPrice;

/// <summary>
/// The best ask size
/// </summary>
public decimal BestAskSize
{
get
{
lock (_locker)
{
return _bestAskSize;
}
}
}
public decimal BestAskSize => _bestAskSize;

private int _depth;

Expand All @@ -121,16 +84,13 @@ public KrakenOrderBook(Symbol symbol, int depth)
/// </summary>
public void Clear()
{
lock (_locker)
{
_bestBidPrice = 0;
_bestBidSize = 0;
_bestAskPrice = 0;
_bestAskSize = 0;
_bestBidPrice = 0;
_bestBidSize = 0;
_bestAskPrice = 0;
_bestAskSize = 0;

Bids.Clear();
Asks.Clear();
}
Bids.Clear();
Asks.Clear();
}

/// <summary>
Expand All @@ -140,22 +100,19 @@ public void Clear()
/// <param name="size">The new size at the bid price level</param>
public void UpdateBidRow(decimal price, decimal size)
{
lock (_locker)
{
Bids[price] = size;
Bids[price] = size;

if (Bids.Count > _depth)
{
Bids.Remove(Bids.First().Key);
}
if (Bids.Count > _depth)
{
Bids.Remove(Bids.First().Key);
}

if (_bestBidPrice == 0 || price >= _bestBidPrice)
{
_bestBidPrice = price;
_bestBidSize = size;
if (_bestBidPrice == 0 || price >= _bestBidPrice)
{
_bestBidPrice = price;
_bestBidSize = size;

BestBidAskUpdated?.Invoke(this, new BestBidAskUpdatedEventArgs(_symbol, _bestBidPrice, _bestBidSize, _bestAskPrice, _bestAskSize));
}
BestBidAskUpdated?.Invoke(this, new BestBidAskUpdatedEventArgs(_symbol, _bestBidPrice, _bestBidSize, _bestAskPrice, _bestAskSize));
}
}

Expand All @@ -166,22 +123,19 @@ public void UpdateBidRow(decimal price, decimal size)
/// <param name="size">The new size at the ask price level</param>
public void UpdateAskRow(decimal price, decimal size)
{
lock (_locker)
{
Asks[price] = size;
Asks[price] = size;

if (Asks.Count > _depth)
{
Asks.Remove(Asks.Last().Key);
}
if (Asks.Count > _depth)
{
Asks.Remove(Asks.Last().Key);
}

if (_bestAskPrice == 0 || price <= _bestAskPrice)
{
_bestAskPrice = price;
_bestAskSize = size;
if (_bestAskPrice == 0 || price <= _bestAskPrice)
{
_bestAskPrice = price;
_bestAskSize = size;

BestBidAskUpdated?.Invoke(this, new BestBidAskUpdatedEventArgs(_symbol, _bestBidPrice, _bestBidSize, _bestAskPrice, _bestAskSize));
}
BestBidAskUpdated?.Invoke(this, new BestBidAskUpdatedEventArgs(_symbol, _bestBidPrice, _bestBidSize, _bestAskPrice, _bestAskSize));
}
}

Expand All @@ -191,18 +145,15 @@ public void UpdateAskRow(decimal price, decimal size)
/// <param name="price">The bid price level to be removed</param>
public void RemoveBidRow(decimal price)
{
lock (_locker)
{
Bids.Remove(price);
Bids.Remove(price);

if (price == _bestBidPrice)
{
var priceLevel = Bids.LastOrDefault();
_bestBidPrice = priceLevel.Key;
_bestBidSize = priceLevel.Value;
if (price == _bestBidPrice)
{
var priceLevel = Bids.LastOrDefault();
_bestBidPrice = priceLevel.Key;
_bestBidSize = priceLevel.Value;

BestBidAskUpdated?.Invoke(this, new BestBidAskUpdatedEventArgs(_symbol, _bestBidPrice, _bestBidSize, _bestAskPrice, _bestAskSize));
}
BestBidAskUpdated?.Invoke(this, new BestBidAskUpdatedEventArgs(_symbol, _bestBidPrice, _bestBidSize, _bestAskPrice, _bestAskSize));
}
}

Expand All @@ -212,18 +163,15 @@ public void RemoveBidRow(decimal price)
/// <param name="price">The ask price level to be removed</param>
public void RemoveAskRow(decimal price)
{
lock (_locker)
{
Asks.Remove(price);
Asks.Remove(price);

if (price == _bestAskPrice)
{
var priceLevel = Asks.FirstOrDefault();
_bestAskPrice = priceLevel.Key;
_bestAskSize = priceLevel.Value;
if (price == _bestAskPrice)
{
var priceLevel = Asks.FirstOrDefault();
_bestAskPrice = priceLevel.Key;
_bestAskSize = priceLevel.Value;

BestBidAskUpdated?.Invoke(this, new BestBidAskUpdatedEventArgs(_symbol, _bestBidPrice, _bestBidSize, _bestAskPrice, _bestAskSize));
}
BestBidAskUpdated?.Invoke(this, new BestBidAskUpdatedEventArgs(_symbol, _bestBidPrice, _bestBidSize, _bestAskPrice, _bestAskSize));
}
}

Expand All @@ -233,16 +181,13 @@ public void RemoveAskRow(decimal price)
/// <param name="priceLevel"></param>
public void RemovePriceLevel(decimal priceLevel)
{
lock (_locker)
if (Asks.ContainsKey(priceLevel))
{
RemoveAskRow(priceLevel);
}
else if (Bids.ContainsKey(priceLevel))
{
if (Asks.ContainsKey(priceLevel))
{
RemoveAskRow(priceLevel);
}
else if (Bids.ContainsKey(priceLevel))
{
RemoveBidRow(priceLevel);
}
RemoveBidRow(priceLevel);
}
}
}
Expand Down

0 comments on commit 963d926

Please sign in to comment.