Lean  $LEAN_TAG$
MappingContractFactorRow.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.Linq;
19 using Newtonsoft.Json;
20 using System.Collections.Generic;
21 
23 {
24  /// <summary>
25  /// Collection of factors for continuous contracts and their back months contracts for a specific mapping mode <see cref="DataMappingMode"/> and date
26  /// </summary>
28  {
29  /// <summary>
30  /// Gets the date associated with this data
31  /// </summary>
32  public DateTime Date { get; set; }
33 
34  /// <summary>
35  /// Backwards ratio price scaling factors for the front month [index 0] and it's 'i' back months [index 0 + i]
36  /// <see cref="DataNormalizationMode.BackwardsRatio"/>
37  /// </summary>
38  public IReadOnlyList<decimal> BackwardsRatioScale { get; set; } = new List<decimal>();
39 
40  /// <summary>
41  /// Backwards Panama Canal price scaling factors for the front month [index 0] and it's 'i' back months [index 0 + i]
42  /// <see cref="DataNormalizationMode.BackwardsPanamaCanal"/>
43  /// </summary>
44  public IReadOnlyList<decimal> BackwardsPanamaCanalScale { get; set; } = new List<decimal>();
45 
46  /// <summary>
47  /// Forward Panama Canal price scaling factors for the front month [index 0] and it's 'i' back months [index 0 + i]
48  /// <see cref="DataNormalizationMode.ForwardPanamaCanal"/>
49  /// </summary>
50  public IReadOnlyList<decimal> ForwardPanamaCanalScale { get; set; } = new List<decimal>();
51 
52  /// <summary>
53  /// Allows the consumer to specify a desired mapping mode
54  /// </summary>
55  public DataMappingMode? DataMappingMode { get; set; }
56 
57  /// <summary>
58  /// Empty constructor for json converter
59  /// </summary>
61  {
62  }
63 
64  /// <summary>
65  /// Writes factor file row into it's file format
66  /// </summary>
67  /// <remarks>Json formatted</remarks>
68  public string GetFileFormat(string source = null)
69  {
70  return JsonConvert.SerializeObject(this);
71  }
72 
73  /// <summary>
74  /// Parses the lines as factor files rows while properly handling inf entries
75  /// </summary>
76  /// <param name="lines">The lines from the factor file to be parsed</param>
77  /// <param name="factorFileMinimumDate">The minimum date from the factor file</param>
78  /// <returns>An enumerable of factor file rows</returns>
79  public static List<MappingContractFactorRow> Parse(IEnumerable<string> lines, out DateTime? factorFileMinimumDate)
80  {
81  factorFileMinimumDate = null;
82 
83  var rows = new List<MappingContractFactorRow>();
84 
85  // parse factor file lines
86  foreach (var line in lines)
87  {
88  var row = JsonConvert.DeserializeObject<MappingContractFactorRow>(line);
89  if(!row.DataMappingMode.HasValue || Enum.IsDefined(typeof(DataMappingMode), row.DataMappingMode.Value))
90  {
91  rows.Add(row);
92  }
93  }
94 
95  if (rows.Count > 0)
96  {
97  factorFileMinimumDate = rows.Min(ffr => ffr.Date).AddDays(-1);
98  }
99 
100  return rows;
101  }
102  }
103 }