Lean  $LEAN_TAG$
MappingContractFactorProvider.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 System.Collections.Generic;
20 
22 {
23  /// <summary>
24  /// Mapping related factor provider. Factors based on price differences on mapping dates
25  /// </summary>
26  public class MappingContractFactorProvider : FactorFile<MappingContractFactorRow>
27  {
28  /// <summary>
29  ///Creates a new instance
30  /// </summary>
31  public MappingContractFactorProvider(string permtick, IEnumerable<MappingContractFactorRow> data, DateTime? factorFileMinimumDate = null)
32  : base(permtick, data, factorFileMinimumDate)
33  {
34  }
35 
36  /// <summary>
37  /// Gets the price scale factor for the specified search date
38  /// </summary>
39  public override decimal GetPriceFactor(DateTime searchDate, DataNormalizationMode dataNormalizationMode, DataMappingMode? dataMappingMode = null, uint contractOffset = 0)
40  {
41  if (dataNormalizationMode == DataNormalizationMode.Raw)
42  {
43  return 0;
44  }
45 
46  var factor = 1m;
47  if (dataNormalizationMode is DataNormalizationMode.BackwardsPanamaCanal or DataNormalizationMode.ForwardPanamaCanal)
48  {
49  // default value depends on the data mode
50  factor = 0;
51  }
52 
53  for (var i = 0; i < ReversedFactorFileDates.Count; i++)
54  {
55  var factorDate = ReversedFactorFileDates[i];
56  if (factorDate.Date < searchDate.Date)
57  {
58  break;
59  }
60 
61  var factorFileRow = SortedFactorFileData[factorDate];
62  switch (dataNormalizationMode)
63  {
64  case DataNormalizationMode.BackwardsRatio:
65  {
66  var row = factorFileRow.FirstOrDefault(row => row.DataMappingMode == dataMappingMode);
67  if (row != null && row.BackwardsRatioScale.Count > contractOffset)
68  {
69  factor = row.BackwardsRatioScale[(int)contractOffset];
70  }
71  break;
72  }
73  case DataNormalizationMode.BackwardsPanamaCanal:
74  {
75  var row = factorFileRow.FirstOrDefault(row => row.DataMappingMode == dataMappingMode);
76  if (row != null && row.BackwardsPanamaCanalScale.Count > contractOffset)
77  {
78  factor = row.BackwardsPanamaCanalScale[(int)contractOffset];
79  }
80  break;
81  }
82  case DataNormalizationMode.ForwardPanamaCanal:
83  {
84  var row = factorFileRow.FirstOrDefault(row => row.DataMappingMode == dataMappingMode);
85  if (row != null && row.ForwardPanamaCanalScale.Count > contractOffset)
86  {
87  factor = row.ForwardPanamaCanalScale[(int)contractOffset];
88  }
89  break;
90  }
91  default:
92  throw new ArgumentOutOfRangeException(nameof(dataNormalizationMode));
93  }
94  }
95 
96  return factor;
97  }
98  }
99 }