Lean  $LEAN_TAG$
MarginRequirementsEntry.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 using System;
17 using System.Globalization;
18 using QuantConnect.Logging;
19 
21 {
22  /// <summary>
23  /// POCO class for modeling margin requirements at given date
24  /// </summary>
26  {
27  /// <summary>
28  /// Date of margin requirements change
29  /// </summary>
30  public DateTime Date { get; init; }
31 
32  /// <summary>
33  /// Initial overnight margin for the contract effective from the date of change
34  /// </summary>
35  public decimal InitialOvernight { get; init; }
36 
37  /// <summary>
38  /// Maintenance overnight margin for the contract effective from the date of change
39  /// </summary>
40  public decimal MaintenanceOvernight { get; init; }
41 
42  /// <summary>
43  /// Initial intraday margin for the contract effective from the date of change
44  /// </summary>
45  public decimal InitialIntraday { get; init; }
46 
47  /// <summary>
48  /// Maintenance intraday margin for the contract effective from the date of change
49  /// </summary>
50  public decimal MaintenanceIntraday { get; init; }
51 
52  /// <summary>
53  /// Creates a new instance of <see cref="MarginRequirementsEntry"/> from the specified csv line
54  /// </summary>
55  /// <param name="csvLine">The csv line to be parsed</param>
56  /// <returns>A new <see cref="MarginRequirementsEntry"/> for the specified csv line</returns>
57  public static MarginRequirementsEntry Create(string csvLine)
58  {
59  var line = csvLine.Split(',');
60 
61  DateTime date;
62  if (!DateTime.TryParseExact(line[0], DateFormat.EightCharacter, CultureInfo.InvariantCulture, DateTimeStyles.None, out date))
63  {
64  Log.Trace($"Couldn't parse date/time while reading future margin requirement file. Line: {csvLine}");
65  }
66 
67  decimal initialOvernight;
68  if (!decimal.TryParse(line[1], out initialOvernight))
69  {
70  Log.Trace($"Couldn't parse Initial Overnight margin requirements while reading future margin requirement file. Line: {csvLine}");
71  }
72 
73  decimal maintenanceOvernight;
74  if (!decimal.TryParse(line[2], out maintenanceOvernight))
75  {
76  Log.Trace($"Couldn't parse Maintenance Overnight margin requirements while reading future margin requirement file. Line: {csvLine}");
77  }
78 
79  // default value, if present in file we try to parse
80  decimal initialIntraday = initialOvernight * 0.4m;
81  if (line.Length >= 4
82  && !decimal.TryParse(line[3], out initialIntraday))
83  {
84  Log.Trace($"Couldn't parse Initial Intraday margin requirements while reading future margin requirement file. Line: {csvLine}");
85  }
86 
87  // default value, if present in file we try to parse
88  decimal maintenanceIntraday = maintenanceOvernight * 0.4m;
89  if (line.Length >= 5
90  && !decimal.TryParse(line[4], out maintenanceIntraday))
91  {
92  Log.Trace($"Couldn't parse Maintenance Intraday margin requirements while reading future margin requirement file. Line: {csvLine}");
93  }
94 
95  return new MarginRequirementsEntry
96  {
97  Date = date,
98  InitialOvernight = initialOvernight,
99  MaintenanceOvernight = maintenanceOvernight,
100  InitialIntraday = initialIntraday,
101  MaintenanceIntraday = maintenanceIntraday
102  };
103  }
104  }
105 }