Lean  $LEAN_TAG$
DelistingEventProvider.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 QuantConnect.Data;
19 using QuantConnect.Util;
22 using System.Collections.Generic;
24 
26 {
27  /// <summary>
28  /// Event provider who will emit <see cref="Delisting"/> events
29  /// </summary>
31  {
32  // we'll use these flags to denote we've already fired off the DelistingType.Warning
33  // and a DelistedType.Delisted Delisting object, the _delistingType object is save here
34  // since we need to wait for the next trading day before emitting
35  private bool _delisted;
36  private bool _delistedWarning;
37  private IMapFileProvider _mapFileProvider;
38 
39  /// <summary>
40  /// The delisting date
41  /// </summary>
42  protected ReferenceWrapper<DateTime> DelistingDate { get; set; }
43 
44  /// <summary>
45  /// The current instance being used
46  /// </summary>
47  protected MapFile MapFile { get; private set; }
48 
49  /// <summary>
50  /// The associated configuration
51  /// </summary>
52  protected SubscriptionDataConfig Config { get; private set; }
53 
54  /// <summary>
55  /// Initializes this instance
56  /// </summary>
57  /// <param name="config">The <see cref="SubscriptionDataConfig"/></param>
58  /// <param name="factorFileProvider">The factor file provider to use</param>
59  /// <param name="mapFileProvider">The <see cref="Data.Auxiliary.MapFile"/> provider to use</param>
60  /// <param name="startTime">Start date for the data request</param>
61  public virtual void Initialize(
63  IFactorFileProvider factorFileProvider,
64  IMapFileProvider mapFileProvider,
65  DateTime startTime)
66  {
67  Config = config;
68  _mapFileProvider = mapFileProvider;
69 
71  }
72 
73  /// <summary>
74  /// Check for delistings
75  /// </summary>
76  /// <param name="eventArgs">The new tradable day event arguments</param>
77  /// <returns>New delisting event if any</returns>
78  public virtual IEnumerable<BaseData> GetEvents(NewTradableDateEventArgs eventArgs)
79  {
80  if (Config.Symbol == eventArgs.Symbol)
81  {
82  // we send the delisting warning when we reach the delisting date, here we make sure we compare using the date component
83  // of the delisting date since for example some futures can trade a few hours in their delisting date, else we would skip on
84  // emitting the delisting warning, which triggers us to handle liquidation once delisted
85  if (!_delistedWarning && eventArgs.Date >= DelistingDate.Value.Date)
86  {
87  _delistedWarning = true;
88  var price = eventArgs.LastBaseData?.Price ?? 0;
89  yield return new Delisting(
90  eventArgs.Symbol,
91  DelistingDate.Value.Date,
92  price,
93  DelistingType.Warning);
94  }
95  if (!_delisted && eventArgs.Date > DelistingDate.Value)
96  {
97  _delisted = true;
98  var price = eventArgs.LastBaseData?.Price ?? 0;
99  // delisted at EOD
100  yield return new Delisting(
101  eventArgs.Symbol,
102  DelistingDate.Value.AddDays(1),
103  price,
104  DelistingType.Delisted);
105  }
106  }
107  }
108 
109  /// <summary>
110  /// Initializes the factor file to use
111  /// </summary>
112  protected void InitializeMapFile()
113  {
114  MapFile = _mapFileProvider.ResolveMapFile(Config);
116  }
117  }
118 }