Lean  $LEAN_TAG$
BrokerageMultiWebSocketEntry.cs
1 /*
2  * QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals.
3  * Lean Algorithmic Trading Engine v2.0. Copyright 2014 QuantConnect Corporation.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 using System.Collections.Generic;
17 using System.Linq;
18 
20 {
21  /// <summary>
22  /// Helper class for <see cref="BrokerageMultiWebSocketSubscriptionManager"/>
23  /// </summary>
25  {
26  private readonly Dictionary<Symbol, int> _symbolWeights;
27  private readonly List<Symbol> _symbols;
28  private readonly object _locker = new();
29 
30  /// <summary>
31  /// Gets the web socket instance
32  /// </summary>
33  public IWebSocket WebSocket { get; }
34 
35  /// <summary>
36  /// Gets the sum of symbol weights for this web socket
37  /// </summary>
38  public int TotalWeight { get; private set; }
39 
40  /// <summary>
41  /// Gets the number of symbols subscribed
42  /// </summary>
43  public int SymbolCount
44  {
45  get
46  {
47  lock (_locker)
48  {
49  return _symbols.Count;
50  }
51  }
52  }
53 
54  /// <summary>
55  /// Returns whether the symbol is subscribed
56  /// </summary>
57  /// <param name="symbol"></param>
58  /// <returns></returns>
59  public bool Contains(Symbol symbol)
60  {
61  lock (_locker)
62  {
63  return _symbols.Contains(symbol);
64  }
65  }
66 
67  /// <summary>
68  /// Returns the list of subscribed symbols
69  /// </summary>
70  /// <returns></returns>
71  public IReadOnlyCollection<Symbol> Symbols
72  {
73  get
74  {
75  lock (_locker)
76  {
77  return _symbols.ToList();
78  }
79  }
80  }
81 
82  /// <summary>
83  /// Initializes a new instance of the <see cref="BrokerageMultiWebSocketEntry"/> class
84  /// </summary>
85  /// <param name="symbolWeights">A dictionary of symbol weights</param>
86  /// <param name="webSocket">The web socket instance</param>
87  public BrokerageMultiWebSocketEntry(Dictionary<Symbol, int> symbolWeights, IWebSocket webSocket)
88  {
89  _symbolWeights = symbolWeights;
90  _symbols = new List<Symbol>();
91 
92  WebSocket = webSocket;
93  }
94 
95  /// <summary>
96  /// Initializes a new instance of the <see cref="BrokerageMultiWebSocketEntry"/> class
97  /// </summary>
98  /// <param name="webSocket">The web socket instance</param>
100  : this(null, webSocket)
101  {
102  }
103 
104  /// <summary>
105  /// Adds a symbol to the entry
106  /// </summary>
107  /// <param name="symbol">The symbol to add</param>
108  public void AddSymbol(Symbol symbol)
109  {
110  lock (_locker)
111  {
112  _symbols.Add(symbol);
113  }
114 
115  if (_symbolWeights != null && _symbolWeights.TryGetValue(symbol, out var weight))
116  {
117  TotalWeight += weight;
118  }
119  }
120 
121  /// <summary>
122  /// Removes a symbol from the entry
123  /// </summary>
124  /// <param name="symbol">The symbol to remove</param>
125  public void RemoveSymbol(Symbol symbol)
126  {
127  lock (_locker)
128  {
129  _symbols.Remove(symbol);
130  }
131 
132  if (_symbolWeights != null && _symbolWeights.TryGetValue(symbol, out var weight))
133  {
134  TotalWeight -= weight;
135  }
136  }
137  }
138 }