/*
* 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 NodaTime;
using System.IO;
using QuantConnect.Util;
namespace QuantConnect.Data.Market
{
///
/// Margin interest rate data source
///
/// This is useful to model margin costs
public class MarginInterestRate : BaseData
{
///
/// The interest rate value
///
public decimal InterestRate { get; set; }
///
/// Creates a new instance
///
public MarginInterestRate()
{
DataType = MarketDataType.Auxiliary;
}
///
/// 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
/// each time it is called. The returned object is assumed to be time stamped in the config.ExchangeTimeZone.
///
/// Subscription data config setup object
/// The data stream
/// Date of the requested data
/// true if we're in live mode, false for backtesting mode
/// Instance of the T:BaseData object generated by this line of the CSV
[StubsIgnore]
public override BaseData Reader(SubscriptionDataConfig config, StreamReader stream, DateTime date, bool isLiveMode)
{
var dateTime = stream.GetDateTime("yyyyMMdd HH:mm:ss");
var interestRate = stream.GetDecimal();
return new MarginInterestRate {
Time = dateTime,
InterestRate = Value = interestRate,
Symbol = config.Symbol
};
}
///
/// Return the URL string source of the file. This will be converted to a stream
///
/// Configuration object
/// Date of this source file
/// true if we're in live mode, false for backtesting mode
/// String URL of source file.
public override SubscriptionDataSource GetSource(SubscriptionDataConfig config, DateTime date, bool isLiveMode)
{
var identifier = config.Symbol.ID;
var source = Path.Combine(Globals.DataFolder,
identifier.SecurityType.SecurityTypeToLower(),
identifier.Market.ToLowerInvariant(),
"margin_interest",
$"{identifier.Symbol.ToLowerInvariant()}.csv"
);
return new SubscriptionDataSource(source, SubscriptionTransportMedium.LocalFile, FileFormat.Csv);
}
///
/// Specifies the data time zone for this data type. This is useful for custom data types
///
public override DateTimeZone DataTimeZone()
{
return TimeZones.Utc;
}
///
/// Formats a string with the symbol and value.
///
public override string ToString()
{
return $"{Symbol}: Rate {InterestRate}";
}
}
}