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();
58  _dataPermissionManager = parameters.DataPermissionManager;
59  }
60 
61  /// <summary>
62  /// Gets the history for the requested securities
63  /// </summary>
64  /// <param name="requests">The historical data requests</param>
65  /// <param name="sliceTimeZone">The time zone used when time stamping the slice instances</param>
66  /// <returns>An enumerable of the slices of data covering the span specified in each request</returns>
67  public override IEnumerable<Slice> GetHistory(IEnumerable<HistoryRequest> requests, DateTimeZone sliceTimeZone)
68  {
69  // create subscription objects from the configs
70  var subscriptions = new List<Subscription>();
71  foreach (var request in requests)
72  {
73  var history = _brokerage.GetHistory(request);
74  if (history == null)
75  {
76  // doesn't support this history request, that's okay
77  continue;
78  }
79  var subscription = CreateSubscription(request, history);
80 
81  _dataPermissionManager.AssertConfiguration(subscription.Configuration, request.StartTimeLocal, request.EndTimeLocal);
82 
83  subscriptions.Add(subscription);
84  }
85 
86  if (subscriptions.Count == 0)
87  {
88  return null;
89  }
90  return CreateSliceEnumerableFromSubscriptions(subscriptions, sliceTimeZone);
91  }
92  }
93 }