Lean  $LEAN_TAG$
FakeHistoryProvider.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 System.Linq;
18 using QuantConnect.Data;
21 using System.Collections.Generic;
24 
26 {
27  /// <summary>
28  /// Provides FAKE implementation of <see cref="IHistoryProvider"/> used for testing. <see cref="FakeDataQueue"/>
29  /// </summary>
31  {
32  private int _historyCount;
33 
34  /// <summary>
35  /// Gets the total number of data points emitted by this history provider
36  /// </summary>
37  public override int DataPointCount => _historyCount;
38 
39  /// <summary>
40  /// Initializes this history provider to work for the specified job
41  /// </summary>
42  /// <param name="parameters">The initialization parameters</param>
43  public override void Initialize(HistoryProviderInitializeParameters parameters)
44  {
45  }
46 
47  /// <summary>
48  /// Gets the history for the requested securities
49  /// </summary>
50  /// <param name="requests">The historical data requests</param>
51  /// <param name="sliceTimeZone">The time zone used when time stamping the slice instances</param>
52  /// <returns>An enumerable of the slices of data covering the span specified in each request</returns>
53  public override IEnumerable<Slice> GetHistory(IEnumerable<HistoryRequest> requests, DateTimeZone sliceTimeZone)
54  {
55  var single = requests.FirstOrDefault();
56  if (single == null)
57  {
58  yield break;
59  }
60 
61  var currentLocalTime = single.StartTimeLocal;
62  while (currentLocalTime < single.EndTimeLocal)
63  {
64  if (single.ExchangeHours.IsOpen(currentLocalTime, single.IncludeExtendedMarketHours))
65  {
66  _historyCount++;
67 
68  BaseData data;
69  if (single.DataType == typeof(TradeBar))
70  {
71  data = new TradeBar
72  {
73  Symbol = single.Symbol,
74  Time = currentLocalTime,
75  Open = _historyCount,
76  Low = _historyCount,
77  High = _historyCount,
78  Close = _historyCount,
79  Volume = _historyCount,
80  Period = single.Resolution.ToTimeSpan()
81  };
82  }
83  else if (single.DataType == typeof(QuoteBar))
84  {
85  data = new QuoteBar
86  {
87  Symbol = single.Symbol,
88  Time = currentLocalTime,
89  Ask = new Bar(_historyCount, _historyCount, _historyCount, _historyCount),
90  Bid = new Bar(_historyCount, _historyCount, _historyCount, _historyCount),
91  Period = single.Resolution.ToTimeSpan()
92  };
93  }
94  else if (single.DataType == typeof(ZipEntryName))
95  {
96  if (single.Symbol.SecurityType == SecurityType.Future)
97  {
98  data = new ZipEntryName
99  {
100  Symbol = Symbol.CreateFuture(single.Symbol.ID.Symbol, single.Symbol.ID.Market, currentLocalTime.AddDays(20)),
101  Time = currentLocalTime
102  };
103  }
104  else if (single.Symbol.SecurityType.IsOption())
105  {
106  data = new ZipEntryName
107  {
108  Symbol = Symbol.CreateOption(single.Symbol.Underlying.ID.Symbol,
109  single.Symbol.ID.Market,
110  single.Symbol.Underlying.SecurityType.DefaultOptionStyle(),
111  default(OptionRight),
112  0m,
113  currentLocalTime.AddDays(20)),
114  Time = currentLocalTime
115  };
116  }
117  else
118  {
119  yield break;
120  }
121  }
122  else
123  {
124  yield break;
125  }
126 
127  yield return new Slice(data.EndTime, new BaseData[] { data }, data.EndTime.ConvertFromUtc(single.ExchangeHours.TimeZone));
128  }
129 
130  currentLocalTime = currentLocalTime.Add(single.Resolution.ToTimeSpan());
131  }
132  }
133  }
134 }