Lean  $LEAN_TAG$
Dividend.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 ProtoBuf;
19 using static QuantConnect.StringExtensions;
20 
22 {
23  /// <summary>
24  /// Dividend event from a security
25  /// </summary>
26  [ProtoContract(SkipConstructor = true)]
27  public class Dividend : BaseData
28  {
29  /// <summary>
30  /// Gets the dividend payment
31  /// </summary>
32  [ProtoMember(10)]
33  public decimal Distribution
34  {
35  get { return Value; }
36  set { Value = value; }
37  }
38 
39  /// <summary>
40  /// Gets the price at which the dividend occurred.
41  /// This is typically the previous day's closing price
42  /// </summary>
43  [ProtoMember(11)]
44  public decimal ReferencePrice
45  {
46  get;
47  set;
48  }
49 
50  /// <summary>
51  /// Initializes a new instance of the Dividend class
52  /// </summary>
53  public Dividend()
54  {
55  DataType = MarketDataType.Auxiliary;
56  }
57 
58  /// <summary>
59  /// Initializes a new instance of the Dividend class
60  /// </summary>
61  /// <param name="symbol">The symbol</param>
62  /// <param name="date">The date</param>
63  /// <param name="distribution">The dividend amount</param>
64  /// <param name="referencePrice">The previous day's closing price</param>
65  public Dividend(Symbol symbol, DateTime date, decimal distribution, decimal referencePrice)
66  : this()
67  {
68  Symbol = symbol;
69  Time = date;
70  Distribution = distribution;
71  ReferencePrice = referencePrice;
72  }
73 
74  /// <summary>
75  /// Initializes a new instance of the Dividend class
76  /// </summary>
77  /// <param name="symbol">The symbol</param>
78  /// <param name="date">The date</param>
79  /// <param name="referencePrice">The previous day's closing price</param>
80  /// <param name="priceFactorRatio">The ratio of the price factors, pf_i/pf_i+1</param>
81  /// <param name="decimalPlaces">The number of decimal places to round the dividend's distribution to, defaulting to 2</param>
82  public static Dividend Create(Symbol symbol, DateTime date, decimal referencePrice, decimal priceFactorRatio, int decimalPlaces = 2)
83  {
84  var distribution = ComputeDistribution(referencePrice, priceFactorRatio, decimalPlaces);
85  return new Dividend(symbol, date, distribution, referencePrice);
86  }
87 
88  /// <summary>
89  /// Computes the price factor ratio given the previous day's closing price and the p
90  /// </summary>
91  /// <param name="close">Previous day's closing price</param>
92  /// <param name="priceFactorRatio">Price factor ratio pf_i/pf_i+1</param>
93  /// <param name="decimalPlaces">The number of decimal places to round the result to, defaulting to 2</param>
94  /// <returns>The distribution rounded to the specified number of decimal places, defaulting to 2</returns>
95  public static decimal ComputeDistribution(decimal close, decimal priceFactorRatio, int decimalPlaces)
96  {
97  return Math.Round(close - close * priceFactorRatio, decimalPlaces);
98  }
99 
100  /// <summary>
101  /// Reader converts each line of the data source into BaseData objects. Each data type creates its own factory method, and returns a new instance of the object
102  /// each time it is called.
103  /// </summary>
104  /// <param name="config">Subscription data config setup object</param>
105  /// <param name="line">Line of the source document</param>
106  /// <param name="date">Date of the requested data</param>
107  /// <param name="isLiveMode">true if we're in live mode, false for backtesting mode</param>
108  /// <returns>Instance of the T:BaseData object generated by this line of the CSV</returns>
109  public override BaseData Reader(SubscriptionDataConfig config, string line, DateTime date, bool isLiveMode)
110  {
111  // this is implemented in the SubscriptionDataReader.CheckForDividend
112  throw new NotImplementedException("This method is not supposed to be called on the Dividend type.");
113  }
114 
115  /// <summary>
116  /// Return the URL string source of the file. This will be converted to a stream
117  /// </summary>
118  /// <param name="config">Configuration object</param>
119  /// <param name="date">Date of this source file</param>
120  /// <param name="isLiveMode">true if we're in live mode, false for backtesting mode</param>
121  /// <returns>String URL of source file.</returns>
122  public override SubscriptionDataSource GetSource(SubscriptionDataConfig config, DateTime date, bool isLiveMode)
123  {
124  // this data is derived from map files and factor files in backtesting
125  return null;
126  }
127 
128  /// <summary>
129  /// Return a new instance clone of this object, used in fill forward
130  /// </summary>
131  /// <remarks>
132  /// This base implementation uses reflection to copy all public fields and properties
133  /// </remarks>
134  /// <returns>A clone of the current object</returns>
135  public override BaseData Clone()
136  {
137  return new Dividend
138  {
139  Time = Time,
140  Value = Value,
141  Symbol = Symbol,
142  EndTime = EndTime,
143  DataType = DataType,
146  };
147  }
148 
149  /// <summary>
150  /// Formats a string with the symbol and value.
151  /// </summary>
152  /// <returns>string - a string formatted as SPY: 167.753</returns>
153  public override string ToString()
154  {
155  return Invariant($"Dividend: {Symbol}: {Distribution} | {ReferencePrice}");
156  }
157  }
158 }