Lean  $LEAN_TAG$
BrokerageHistoryProvider.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;
17 using System.Collections.Generic;
18 using NodaTime;
19 using QuantConnect.Data;
23 
25 {
26  /// <summary>
27  /// Provides an implementation of <see cref="IHistoryProvider"/> that relies on
28  /// a brokerage connection to retrieve historical data
29  /// </summary>
31  {
32  private IDataPermissionManager _dataPermissionManager;
33  private IBrokerage _brokerage;
34  private bool _initialized;
35 
36  /// <summary>
37  /// Sets the brokerage to be used for historical requests
38  /// </summary>
39  /// <param name="brokerage">The brokerage instance</param>
40  public void SetBrokerage(IBrokerage brokerage)
41  {
42  _brokerage = brokerage;
43  }
44 
45  /// <summary>
46  /// Initializes this history provider to work for the specified job
47  /// </summary>
48  /// <param name="parameters">The initialization parameters</param>
49  public override void Initialize(HistoryProviderInitializeParameters parameters)
50  {
51  if (_initialized)
52  {
53  // let's make sure no one tries to change our parameters values
54  throw new InvalidOperationException("BrokerageHistoryProvider can only be initialized once");
55  }
56  _initialized = true;
57  _brokerage.Connect();
59  _dataPermissionManager = parameters.DataPermissionManager;
60  }
61 
62  /// <summary>
63  /// Gets the history for the requested securities
64  /// </summary>
65  /// <param name="requests">The historical data requests</param>
66  /// <param name="sliceTimeZone">The time zone used when time stamping the slice instances</param>
67  /// <returns>An enumerable of the slices of data covering the span specified in each request</returns>
68  public override IEnumerable<Slice> GetHistory(IEnumerable<HistoryRequest> requests, DateTimeZone sliceTimeZone)
69  {
70  // create subscription objects from the configs
71  var subscriptions = new List<Subscription>();
72  foreach (var request in requests)
73  {
74  var history = _brokerage.GetHistory(request);
75  if (history == null)
76  {
77  // doesn't support this history request, that's okay
78  continue;
79  }
80  var subscription = CreateSubscription(request, history);
81  subscriptions.Add(subscription);
82  }
83 
84  if (subscriptions.Count == 0)
85  {
86  return null;
87  }
88  return CreateSliceEnumerableFromSubscriptions(subscriptions, sliceTimeZone);
89  }
90  }
91 }