/*
* QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals.
* Lean Algorithmic Trading Engine v2.0. Copyright 2014 QuantConnect Corporation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
using System;
using System.Globalization;
using QuantConnect.Logging;
namespace QuantConnect.Securities.Future
{
///
/// POCO class for modeling margin requirements at given date
///
public class MarginRequirementsEntry
{
///
/// Date of margin requirements change
///
public DateTime Date { get; init; }
///
/// Initial overnight margin for the contract effective from the date of change
///
public decimal InitialOvernight { get; init; }
///
/// Maintenance overnight margin for the contract effective from the date of change
///
public decimal MaintenanceOvernight { get; init; }
///
/// Initial intraday margin for the contract effective from the date of change
///
public decimal InitialIntraday { get; init; }
///
/// Maintenance intraday margin for the contract effective from the date of change
///
public decimal MaintenanceIntraday { get; init; }
///
/// Creates a new instance of from the specified csv line
///
/// The csv line to be parsed
/// A new for the specified csv line
public static MarginRequirementsEntry Create(string csvLine)
{
var line = csvLine.Split(',');
DateTime date;
if (!DateTime.TryParseExact(line[0], DateFormat.EightCharacter, CultureInfo.InvariantCulture, DateTimeStyles.None, out date))
{
Log.Trace($"Couldn't parse date/time while reading future margin requirement file. Line: {csvLine}");
}
decimal initialOvernight;
if (!decimal.TryParse(line[1], out initialOvernight))
{
Log.Trace($"Couldn't parse Initial Overnight margin requirements while reading future margin requirement file. Line: {csvLine}");
}
decimal maintenanceOvernight;
if (!decimal.TryParse(line[2], out maintenanceOvernight))
{
Log.Trace($"Couldn't parse Maintenance Overnight margin requirements while reading future margin requirement file. Line: {csvLine}");
}
// default value, if present in file we try to parse
decimal initialIntraday = initialOvernight * 0.4m;
if (line.Length >= 4
&& !decimal.TryParse(line[3], out initialIntraday))
{
Log.Trace($"Couldn't parse Initial Intraday margin requirements while reading future margin requirement file. Line: {csvLine}");
}
// default value, if present in file we try to parse
decimal maintenanceIntraday = maintenanceOvernight * 0.4m;
if (line.Length >= 5
&& !decimal.TryParse(line[4], out maintenanceIntraday))
{
Log.Trace($"Couldn't parse Maintenance Intraday margin requirements while reading future margin requirement file. Line: {csvLine}");
}
return new MarginRequirementsEntry
{
Date = date,
InitialOvernight = initialOvernight,
MaintenanceOvernight = maintenanceOvernight,
InitialIntraday = initialIntraday,
MaintenanceIntraday = maintenanceIntraday
};
}
}
}