Lean  $LEAN_TAG$
SineHistoryProvider.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 NodaTime;
17 using QuantConnect.Data;
22 using System;
23 using System.Collections.Generic;
24 using System.Linq;
26 
28 {
29  /// <summary>
30  /// Implements a History provider that always return a IEnumerable of Slice with prices following a sine function
31  /// </summary>
33  {
34  private readonly SecurityChanges _securityChanges = SecurityChanges.None;
35  private readonly SecurityManager _securities;
36 
37  /// <summary>
38  /// Gets the total number of data points emitted by this history provider
39  /// </summary>
40  public override int DataPointCount => 0;
41 
42  /// <summary>
43  /// Initializes a new instance of the <see cref="SineHistoryProvider"/> class
44  /// </summary>
45  /// <param name="securities">Collection of securities that a history request can return</param>
47  {
48  _securities = securities;
49  }
50 
51  /// <summary>
52  /// Initializes this history provider to work for the specified job
53  /// </summary>
54  /// <param name="parameters">The initialization parameters</param>
55  public override void Initialize(HistoryProviderInitializeParameters parameters)
56  {
57  }
58 
59  /// <summary>
60  /// Gets the history for the requested securities
61  /// </summary>
62  /// <param name="requests">The historical data requests</param>
63  /// <param name="sliceTimeZone">The time zone used when time stamping the slice instances</param>
64  /// <returns>An enumerable of the slices of data covering the span specified in each request</returns>
65  public override IEnumerable<Slice> GetHistory(IEnumerable<HistoryRequest> requests, DateTimeZone sliceTimeZone)
66  {
67  var configsByDateTime = GetSubscriptionDataConfigByDateTime(requests);
68  var count = configsByDateTime.Count;
69  var i = 0;
70  var timeSliceFactory = new TimeSliceFactory(sliceTimeZone);
71  foreach (var kvp in configsByDateTime)
72  {
73  var utcDateTime = kvp.Key;
74  var configs = kvp.Value;
75  var last = Convert.ToDecimal(100 + 10 * Math.Sin(Math.PI * (360 - count + i) / 180.0));
76  var high = last * 1.005m;
77  var low = last / 1.005m;
78 
79  var packets = new List<DataFeedPacket>();
80 
81  foreach (var config in configs)
82  {
83  Security security;
84  if (!_securities.TryGetValue(config.Symbol, out security))
85  {
86  continue;
87  }
88 
89  var period = config.Resolution.ToTimeSpan();
90  var time = (utcDateTime - period).ConvertFromUtc(config.DataTimeZone);
91  var data = new TradeBar(time, config.Symbol, last, high, last, last, 1000, period);
92  security.SetMarketPrice(data);
93  packets.Add(new DataFeedPacket(security, config, new List<BaseData> { data }));
94  }
95 
96  i++;
97  yield return timeSliceFactory.Create(utcDateTime, packets, _securityChanges, new Dictionary<Universe, BaseDataCollection>()).Slice;
98  }
99  }
100 
101  private Dictionary<DateTime, List<SubscriptionDataConfig>> GetSubscriptionDataConfigByDateTime(
102  IEnumerable<HistoryRequest> requests)
103  {
104  var dictionary = new Dictionary<DateTime, List<SubscriptionDataConfig>>();
105 
106  var barSize = requests.Select(x => x.Resolution.ToTimeSpan()).Min();
107  var startUtc = requests.Min(x => x.StartTimeUtc);
108  var endUtc = requests.Max(x => x.EndTimeUtc);
109 
110  for (var utcDateTime = startUtc; utcDateTime < endUtc; utcDateTime += barSize)
111  {
112  var subscriptionDataConfig = new List<SubscriptionDataConfig>();
113 
114  foreach (var request in requests)
115  {
116  var exchange = request.ExchangeHours;
117  var extendedMarket = request.IncludeExtendedMarketHours;
118  var localDateTime = utcDateTime.ConvertFromUtc(exchange.TimeZone);
119  if (!exchange.IsOpen(localDateTime, extendedMarket))
120  {
121  continue;
122  }
123 
124  var config = request.ToSubscriptionDataConfig();
125  subscriptionDataConfig.Add(config);
126  }
127 
128  if (subscriptionDataConfig.Count > 0)
129  {
130  dictionary.Add(utcDateTime.Add(barSize), subscriptionDataConfig);
131  }
132  }
133 
134  return dictionary;
135  }
136  }
137 }