Files
quantconnect--lean/Brokerages/BrokerageMultiWebSocketEntry.cs
T
Stefano Raggi 0c4e577885
Regression Tests / build (push) Has been cancelled
Build & Test Lean / build (push) Has been cancelled
BitfinexBrokerage updates (#5787)
* Update BinanceBrokerage to handle more than 512 symbols

* Address review

- fetch symbol weights only if required
- remove code duplication

* Add rate limiting for new connections

* Update WebSocketMessage to include the websocket instance

* Handle resubscriptions on reconnect

* Address review

* Address review

* Remove unnecessary locking

* WebSocketClientWrapper updates

- remove allocation of receive buffer on each message
- add missing lock in Close method
- log message data when message type is Close
- fix race condition after unexpected websocket close

* Set WebSocketClientWrapper task to LongRunning

* Add missing check in GetHistory

* Fix exceptions with Binance downloader

- closes #5794

* Update Bitfinex symbols in symbol properties database

* Update BitfinexBrokerage to use BrokerageMultiWebSocketSubscriptionManager

* Address review

* Remove unnecessary locking

* Remove old channels on resubscription
2021-07-28 11:10:12 -03:00

139 lines
4.1 KiB
C#

/*
* QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals.
* Lean Algorithmic Trading Engine v2.0. Copyright 2014 QuantConnect Corporation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
using System.Collections.Generic;
using System.Linq;
namespace QuantConnect.Brokerages
{
/// <summary>
/// Helper class for <see cref="BrokerageMultiWebSocketSubscriptionManager"/>
/// </summary>
public class BrokerageMultiWebSocketEntry
{
private readonly Dictionary<Symbol, int> _symbolWeights;
private readonly List<Symbol> _symbols;
private readonly object _locker = new();
/// <summary>
/// Gets the web socket instance
/// </summary>
public IWebSocket WebSocket { get; }
/// <summary>
/// Gets the sum of symbol weights for this web socket
/// </summary>
public int TotalWeight { get; private set; }
/// <summary>
/// Gets the number of symbols subscribed
/// </summary>
public int SymbolCount
{
get
{
lock (_locker)
{
return _symbols.Count;
}
}
}
/// <summary>
/// Returns whether the symbol is subscribed
/// </summary>
/// <param name="symbol"></param>
/// <returns></returns>
public bool Contains(Symbol symbol)
{
lock (_locker)
{
return _symbols.Contains(symbol);
}
}
/// <summary>
/// Returns the list of subscribed symbols
/// </summary>
/// <returns></returns>
public IReadOnlyCollection<Symbol> Symbols
{
get
{
lock (_locker)
{
return _symbols.ToList();
}
}
}
/// <summary>
/// Initializes a new instance of the <see cref="BrokerageMultiWebSocketEntry"/> class
/// </summary>
/// <param name="symbolWeights">A dictionary of symbol weights</param>
/// <param name="webSocket">The web socket instance</param>
public BrokerageMultiWebSocketEntry(Dictionary<Symbol, int> symbolWeights, IWebSocket webSocket)
{
_symbolWeights = symbolWeights;
_symbols = new List<Symbol>();
WebSocket = webSocket;
}
/// <summary>
/// Initializes a new instance of the <see cref="BrokerageMultiWebSocketEntry"/> class
/// </summary>
/// <param name="webSocket">The web socket instance</param>
public BrokerageMultiWebSocketEntry(IWebSocket webSocket)
: this(null, webSocket)
{
}
/// <summary>
/// Adds a symbol to the entry
/// </summary>
/// <param name="symbol">The symbol to add</param>
public void AddSymbol(Symbol symbol)
{
lock (_locker)
{
_symbols.Add(symbol);
}
if (_symbolWeights != null && _symbolWeights.TryGetValue(symbol, out var weight))
{
TotalWeight += weight;
}
}
/// <summary>
/// Removes a symbol from the entry
/// </summary>
/// <param name="symbol">The symbol to remove</param>
public void RemoveSymbol(Symbol symbol)
{
lock (_locker)
{
_symbols.Remove(symbol);
}
if (_symbolWeights != null && _symbolWeights.TryGetValue(symbol, out var weight))
{
TotalWeight -= weight;
}
}
}
}