Lean  $LEAN_TAG$
EventBasedDataQueueHandlerSubscriptionManager.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 
17 using System;
18 using System.Collections.Generic;
19 using System.Linq;
20 using QuantConnect.Logging;
21 
22 namespace QuantConnect.Data
23 {
24  /// <summary>
25  /// Overrides <see cref="DataQueueHandlerSubscriptionManager"/> methods using events
26  /// </summary>
28  {
29  /// <summary>
30  /// Creates an instance of <see cref="EventBasedDataQueueHandlerSubscriptionManager"/> with a single channel name
31  /// </summary>
33 
34  /// <summary>
35  /// Creates an instance of <see cref="EventBasedDataQueueHandlerSubscriptionManager"/>
36  /// </summary>
37  /// <param name="getChannelName">Convert TickType into string</param>
38  public EventBasedDataQueueHandlerSubscriptionManager(Func<TickType, string> getChannelName)
39  {
40  _getChannelName = getChannelName;
41  }
42 
43  /// <summary>
44  /// Subscription method implementation
45  /// </summary>
46  public Func<IEnumerable<Symbol>, TickType, bool> SubscribeImpl { get; set; }
47 
48  /// <summary>
49  /// Unsubscription method implementation
50  /// </summary>
51  public Func<IEnumerable<Symbol>, TickType, bool> UnsubscribeImpl { get; set; }
52 
53  /// <summary>
54  /// Socket channel name
55  /// </summary>
56  private Func<TickType, string> _getChannelName;
57 
58  /// <summary>
59  /// The way Brokerage subscribes to symbol tickers
60  /// </summary>
61  /// <param name="symbols">Symbols to subscribe</param>
62  /// <param name="tickType">Type of tick data</param>
63  /// <returns></returns>
64  protected override bool Subscribe(IEnumerable<Symbol> symbols, TickType tickType)
65  {
66  Log.Trace("EventBasedDataQueueHandlerSubscriptionManager.Subscribe(): {0}", string.Join(",", symbols.Select(x => x.Value)));
67  return SubscribeImpl?.Invoke(symbols, tickType) == true;
68  }
69 
70  /// <summary>
71  /// The way Brokerage unsubscribes from symbol tickers
72  /// </summary>
73  /// <param name="symbols">Symbols to unsubscribe</param>
74  /// <param name="tickType">Type of tick data</param>
75  /// <returns></returns>
76  protected override bool Unsubscribe(IEnumerable<Symbol> symbols, TickType tickType)
77  {
78  Log.Trace("EventBasedDataQueueHandlerSubscriptionManager.Unsubscribe(): {0}", string.Join(",", symbols.Select(x => x.Value)));
79  return UnsubscribeImpl?.Invoke(symbols, tickType) == true;
80  }
81 
82  /// <summary>
83  /// Channel name
84  /// </summary>
85  /// <param name="tickType">Type of tick data</param>
86  /// <returns>Returns Socket channel name corresponding <paramref name="tickType"/></returns>
87  protected override string ChannelNameFromTickType(TickType tickType)
88  {
89  return _getChannelName?.Invoke(tickType);
90  }
91  }
92 }