Skip to content

Commit

Permalink
[Requested changes]
Browse files Browse the repository at this point in the history
- rename protected fields to follow coding style
- rename iterface IOrderBook -> IOrderBookUpdater
  • Loading branch information
AdalyatNazirov committed Jul 26, 2019
1 parent 6387e75 commit 048c535
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 18 deletions.
38 changes: 23 additions & 15 deletions Brokerages/DefaultOrderBook.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,20 @@ namespace QuantConnect.Brokerages
/// It contains prices and order sizes for each bid and ask level.
/// The best bid and ask prices are also kept up to date.
/// </summary>
public class DefaultOrderBook : IOrderBook<decimal, decimal>
public class DefaultOrderBook : IOrderBookUpdater<decimal, decimal>
{
private readonly object _locker = new object();
private readonly Symbol _symbol;
protected readonly SortedDictionary<decimal, decimal> _bids = new SortedDictionary<decimal, decimal>();
protected readonly SortedDictionary<decimal, decimal> _asks = new SortedDictionary<decimal, decimal>();

/// <summary>
/// Represents bid prices and sizes
/// </summary>
protected readonly SortedDictionary<decimal, decimal> Bids = new SortedDictionary<decimal, decimal>();

/// <summary>
/// Represents ask prices and sizes
/// </summary>
protected readonly SortedDictionary<decimal, decimal> Asks = new SortedDictionary<decimal, decimal>();

/// <summary>
/// Event fired each time <see cref="BestBidPrice"/> or <see cref="BestAskPrice"/> are changed
Expand Down Expand Up @@ -72,8 +80,8 @@ public void Clear()
{
lock (_locker)
{
_bids.Clear();
_asks.Clear();
Bids.Clear();
Asks.Clear();
}

BestBidPrice = 0;
Expand All @@ -91,7 +99,7 @@ public void UpdateBidRow(decimal price, decimal size)
{
lock (_locker)
{
_bids[price] = size;
Bids[price] = size;
}

if (BestBidPrice == 0 || price >= BestBidPrice)
Expand All @@ -112,7 +120,7 @@ public void UpdateAskRow(decimal price, decimal size)
{
lock (_locker)
{
_asks[price] = size;
Asks[price] = size;
}

if (BestAskPrice == 0 || price <= BestAskPrice)
Expand All @@ -132,15 +140,15 @@ public void RemoveBidRow(decimal price)
{
lock (_locker)
{
_bids.Remove(price);
Bids.Remove(price);
}

if (price == BestBidPrice)
{
lock (_locker)
{
BestBidPrice = _bids.Keys.LastOrDefault();
BestBidSize = BestBidPrice > 0 ? _bids[BestBidPrice] : 0;
BestBidPrice = Bids.Keys.LastOrDefault();
BestBidSize = BestBidPrice > 0 ? Bids[BestBidPrice] : 0;
}

BestBidAskUpdated?.Invoke(this, new BestBidAskUpdatedEventArgs(_symbol, BestBidPrice, BestBidSize, BestAskPrice, BestAskSize));
Expand All @@ -155,15 +163,15 @@ public void RemoveAskRow(decimal price)
{
lock (_locker)
{
_asks.Remove(price);
Asks.Remove(price);
}

if (price == BestAskPrice)
{
lock (_locker)
{
BestAskPrice = _asks.Keys.FirstOrDefault();
BestAskSize = BestAskPrice > 0 ? _asks[BestAskPrice] : 0;
BestAskPrice = Asks.Keys.FirstOrDefault();
BestAskSize = BestAskPrice > 0 ? Asks[BestAskPrice] : 0;
}

BestBidAskUpdated?.Invoke(this, new BestBidAskUpdatedEventArgs(_symbol, BestBidPrice, BestBidSize, BestAskPrice, BestAskSize));
Expand All @@ -176,11 +184,11 @@ public void RemoveAskRow(decimal price)
/// <param name="priceLevel"></param>
public void RemovePriceLevel(decimal priceLevel)
{
if (_asks.ContainsKey(priceLevel))
if (Asks.ContainsKey(priceLevel))
{
RemoveAskRow(priceLevel);
}
else if (_bids.ContainsKey(priceLevel))
else if (Bids.ContainsKey(priceLevel))
{
RemoveBidRow(priceLevel);
}
Expand Down
5 changes: 3 additions & 2 deletions Brokerages/IOrderBook.cs → Brokerages/IOrderBookUpdater.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,12 @@
namespace QuantConnect.Brokerages
{
/// <summary>
/// Represents an order book interface for a security.
/// Represents an orderbook updater interface for a security.
/// Provides the ability to update orderbook price level and to be alerted about updates
/// </summary>
/// <typeparam name="K">Price level identifier</typeparam>
/// <typeparam name="V">Size at the price level</typeparam>
public interface IOrderBook<K, V>
public interface IOrderBookUpdater<K, V>
{
/// <summary>
/// Event fired each time <see cref="BestBidPrice"/> or <see cref="BestAskPrice"/> are changed
Expand Down
2 changes: 1 addition & 1 deletion Brokerages/QuantConnect.Brokerages.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@
<Compile Include="Bitfinex\BitfinexWebSocketWrapper.cs" />
<Compile Include="Bitfinex\Messages.cs" />
<Compile Include="Bitfinex\BitfinexSymbolMapper.cs" />
<Compile Include="IOrderBook.cs" />
<Compile Include="IOrderBookUpdater.cs" />
<Compile Include="DefaultOrderBook.cs" />
<Compile Include="Brokerage.cs" />
<Compile Include="BrokerageException.cs" />
Expand Down

0 comments on commit 048c535

Please sign in to comment.