Lean  $LEAN_TAG$
MarginInterestRate.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 NodaTime;
19 using System.IO;
20 using QuantConnect.Util;
21 
23 {
24  /// <summary>
25  /// Margin interest rate data source
26  /// </summary>
27  /// <remarks>This is useful to model margin costs</remarks>
29  {
30  /// <summary>
31  /// The interest rate value
32  /// </summary>
33  public decimal InterestRate { get; set; }
34 
35  /// <summary>
36  /// Creates a new instance
37  /// </summary>
39  {
40  DataType = MarketDataType.Auxiliary;
41  }
42 
43  /// <summary>
44  /// 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
45  /// each time it is called. The returned object is assumed to be time stamped in the config.ExchangeTimeZone.
46  /// </summary>
47  /// <param name="config">Subscription data config setup object</param>
48  /// <param name="stream">The data stream</param>
49  /// <param name="date">Date of the requested data</param>
50  /// <param name="isLiveMode">true if we're in live mode, false for backtesting mode</param>
51  /// <returns>Instance of the T:BaseData object generated by this line of the CSV</returns>
52  public override BaseData Reader(SubscriptionDataConfig config, StreamReader stream, DateTime date, bool isLiveMode)
53  {
54  var dateTime = stream.GetDateTime("yyyyMMdd HH:mm:ss");
55  var interestRate = stream.GetDecimal();
56  return new MarginInterestRate {
57  Time = dateTime,
58  InterestRate = Value = interestRate,
59  Symbol = config.Symbol
60  };
61  }
62 
63  /// <summary>
64  /// Return the URL string source of the file. This will be converted to a stream
65  /// </summary>
66  /// <param name="config">Configuration object</param>
67  /// <param name="date">Date of this source file</param>
68  /// <param name="isLiveMode">true if we're in live mode, false for backtesting mode</param>
69  /// <returns>String URL of source file.</returns>
70  public override SubscriptionDataSource GetSource(SubscriptionDataConfig config, DateTime date, bool isLiveMode)
71  {
72  var identifier = config.Symbol.ID;
73  var source = Path.Combine(Globals.DataFolder,
74  identifier.SecurityType.SecurityTypeToLower(),
75  identifier.Market.ToLowerInvariant(),
76  "margin_interest",
77  $"{identifier.Symbol.ToLowerInvariant()}.csv"
78  );
79 
80  return new SubscriptionDataSource(source, SubscriptionTransportMedium.LocalFile, FileFormat.Csv);
81  }
82 
83  /// <summary>
84  /// Specifies the data time zone for this data type. This is useful for custom data types
85  /// </summary>
86  public override DateTimeZone DataTimeZone()
87  {
88  return TimeZones.Utc;
89  }
90 
91  /// <summary>
92  /// Formats a string with the symbol and value.
93  /// </summary>
94  public override string ToString()
95  {
96  return $"{Symbol}: Rate {InterestRate}";
97  }
98  }
99 }