Lean  $LEAN_TAG$
DividendEventProvider.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="Dividend"/> events
28  /// </summary>
30  {
31  // we set the price factor ratio when we encounter a dividend in the factor file
32  // and on the next trading day we use this data to produce the dividend instance
33  private decimal? _priceFactorRatio;
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 dividends and returns them
69  /// </summary>
70  /// <param name="eventArgs">The new tradable day event arguments</param>
71  /// <returns>New Dividend 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  if (_priceFactorRatio != null)
79  {
80  if (_referencePrice == 0)
81  {
82  throw new InvalidOperationException($"Zero reference price for {Config.Symbol} dividend at {eventArgs.Date}");
83  }
84 
85  var baseData = Dividend.Create(
86  Config.Symbol,
87  eventArgs.Date,
88  _referencePrice,
89  _priceFactorRatio.Value
90  );
91  // let the config know about it for normalization
92  Config.SumOfDividends += baseData.Distribution;
93  _priceFactorRatio = null;
94  _referencePrice = 0;
95 
96  yield return baseData;
97  }
98 
99  // check the factor file to see if we have a dividend event tomorrow
100  decimal priceFactorRatio;
101  decimal referencePrice;
102  if (FactorFile.HasDividendEventOnNextTradingDay(eventArgs.Date, out priceFactorRatio, out referencePrice))
103  {
104  _priceFactorRatio = priceFactorRatio;
105  _referencePrice = referencePrice;
106  }
107  }
108  }
109 
110  /// <summary>
111  /// Initializes the factor file to use
112  /// </summary>
113  protected void InitializeFactorFile()
114  {
115  FactorFile = _factorFileProvider.Get(Config.Symbol) as CorporateFactorProvider;
116  }
117  }
118 }