Lean  $LEAN_TAG$
Split.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 Newtonsoft.Json;
19 using ProtoBuf;
20 using static QuantConnect.StringExtensions;
21 
23 {
24  /// <summary>
25  /// Split event from a security
26  /// </summary>
27  [ProtoContract(SkipConstructor = true)]
28  public class Split : BaseData
29  {
30  /// <summary>
31  ///Gets the type of split event, warning or split.
32  /// </summary>
33  [JsonProperty]
34  [ProtoMember(10)]
35  public SplitType Type
36  {
37  get; private set;
38  }
39 
40  /// <summary>
41  /// Gets the split factor
42  /// </summary>
43  [JsonProperty]
44  [ProtoMember(11)]
45  public decimal SplitFactor
46  {
47  get; private set;
48  }
49 
50  /// <summary>
51  /// Gets the price at which the split occurred
52  /// This is typically the previous day's closing price
53  /// </summary>
54  [ProtoMember(12)]
55  public decimal ReferencePrice
56  {
57  get { return Value; }
58  set { Value = value; }
59  }
60 
61  /// <summary>
62  /// Initializes a new instance of the Split class
63  /// </summary>
64  public Split()
65  {
66  Type = SplitType.SplitOccurred;
67  DataType = MarketDataType.Auxiliary;
68  }
69 
70  /// <summary>
71  /// Initializes a new instance of the Split class
72  /// </summary>
73  /// <param name="symbol">The symbol</param>
74  /// <param name="date">The date</param>
75  /// <param name="price">The price at the time of the split</param>
76  /// <param name="splitFactor">The split factor to be applied to current holdings</param>
77  /// <param name="type">The type of split event, warning or split occurred</param>
78  public Split(Symbol symbol, DateTime date, decimal price, decimal splitFactor, SplitType type)
79  : this()
80  {
81  Type = type;
82  Time = date;
83  Symbol = symbol;
84  ReferencePrice = price;
85  SplitFactor = splitFactor;
86  }
87 
88  /// <summary>
89  /// 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
90  /// each time it is called.
91  /// </summary>
92  /// <param name="config">Subscription data config setup object</param>
93  /// <param name="line">Line of the source document</param>
94  /// <param name="date">Date of the requested data</param>
95  /// <param name="isLiveMode">true if we're in live mode, false for backtesting mode</param>
96  /// <returns>Instance of the T:BaseData object generated by this line of the CSV</returns>
97  public override BaseData Reader(SubscriptionDataConfig config, string line, DateTime date, bool isLiveMode)
98  {
99  // this is implemented in the SubscriptionDataReader.CheckForSplit
100  throw new NotImplementedException("This method is not supposed to be called on the Split type.");
101  }
102 
103  /// <summary>
104  /// Return the URL string source of the file. This will be converted to a stream
105  /// </summary>
106  /// <param name="config">Configuration object</param>
107  /// <param name="date">Date of this source file</param>
108  /// <param name="isLiveMode">true if we're in live mode, false for backtesting mode</param>
109  /// <returns>String URL of source file.</returns>
110  public override SubscriptionDataSource GetSource(SubscriptionDataConfig config, DateTime date, bool isLiveMode)
111  {
112  // this data is derived from map files and factor files in backtesting
113  return null;
114  }
115 
116  /// <summary>
117  /// Formats a string with the symbol and value.
118  /// </summary>
119  /// <returns>string - a string formatted as SPY: 167.753</returns>
120  public override string ToString()
121  {
122  var type = Type == SplitType.Warning ? "Split Warning" : "Split";
123  return Invariant($"{type}: {Symbol}: {SplitFactor} | {ReferencePrice}");
124  }
125 
126  /// <summary>
127  /// Return a new instance clone of this object, used in fill forward
128  /// </summary>
129  /// <remarks>
130  /// This base implementation uses reflection to copy all public fields and properties
131  /// </remarks>
132  /// <returns>A clone of the current object</returns>
133  public override BaseData Clone()
134  {
135  return new Split(Symbol, Time, Price, SplitFactor, Type);
136  }
137  }
138 }