Lean  $LEAN_TAG$
SplitEventProvider.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 QuantConnect.Data;
23 
25 {
26  /// <summary>
27  /// Event provider who will emit <see cref="Split"/> events
28  /// </summary>
30  {
31  // we set the split factor when we encounter a split in the factor file
32  // and on the next trading day we use this data to produce the split instance
33  private decimal? _splitFactor;
34  private decimal _referencePrice;
35  private IFactorFileProvider _factorFileProvider;
36  private MapFile _mapFile;
37 
38  /// <summary>
39  /// The current instance being used
40  /// </summary>
41  protected CorporateFactorProvider FactorFile { get; private set; }
42 
43  /// <summary>
44  /// The associated configuration
45  /// </summary>
46  protected SubscriptionDataConfig Config { get; private set; }
47 
48  /// <summary>
49  /// Initializes this instance
50  /// </summary>
51  /// <param name="config">The <see cref="SubscriptionDataConfig"/></param>
52  /// <param name="factorFileProvider">The factor file provider to use</param>
53  /// <param name="mapFileProvider">The <see cref="Data.Auxiliary.MapFile"/> provider to use</param>
54  /// <param name="startTime">Start date for the data request</param>
55  public void Initialize(
57  IFactorFileProvider factorFileProvider,
58  IMapFileProvider mapFileProvider,
59  DateTime startTime)
60  {
61  Config = config;
62  _factorFileProvider = factorFileProvider;
63  _mapFile = mapFileProvider.ResolveMapFile(Config);
65  }
66 
67  /// <summary>
68  /// Check for new splits
69  /// </summary>
70  /// <param name="eventArgs">The new tradable day event arguments</param>
71  /// <returns>New split event if any</returns>
72  public virtual IEnumerable<BaseData> GetEvents(NewTradableDateEventArgs eventArgs)
73  {
74  if (Config.Symbol == eventArgs.Symbol
75  && FactorFile != null
76  && _mapFile.HasData(eventArgs.Date))
77  {
78  var factor = _splitFactor;
79  if (factor != null)
80  {
81  var close = _referencePrice;
82  if (close == 0)
83  {
84  throw new InvalidOperationException($"Zero reference price for {Config.Symbol} split at {eventArgs.Date}");
85  }
86 
87  _splitFactor = null;
88  _referencePrice = 0;
89  yield return new Split(
90  eventArgs.Symbol,
91  eventArgs.Date,
92  close,
93  factor.Value,
94  SplitType.SplitOccurred);
95  }
96 
97  decimal splitFactor;
98  decimal referencePrice;
99  if (FactorFile.HasSplitEventOnNextTradingDay(eventArgs.Date, out splitFactor, out referencePrice))
100  {
101  _splitFactor = splitFactor;
102  _referencePrice = referencePrice;
103  yield return new Split(
104  eventArgs.Symbol,
105  eventArgs.Date,
106  eventArgs.LastRawPrice ?? 0,
107  splitFactor,
108  SplitType.Warning);
109  }
110  }
111  }
112 
113  /// <summary>
114  /// Initializes the factor file to use
115  /// </summary>
116  protected void InitializeFactorFile()
117  {
118  FactorFile = _factorFileProvider.Get(Config.Symbol) as CorporateFactorProvider;
119  }
120  }
121 }