Lean  $LEAN_TAG$
IRiskFreeInterestRateModel.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.Collections.Generic;
18 using System.Linq;
19 
20 namespace QuantConnect.Data
21 {
22  /// <summary>
23  /// Represents a model that provides risk free interest rate data
24  /// </summary>
25  public interface IRiskFreeInterestRateModel
26  {
27  /// <summary>
28  /// Get interest rate by a given date
29  /// </summary>
30  /// <param name="date">The date</param>
31  /// <returns>Interest rate on the given date</returns>
32  decimal GetInterestRate(DateTime date);
33  }
34 
35  /// <summary>
36  /// Provide extension and static methods for <see cref="IRiskFreeInterestRateModel"/>
37  /// </summary>
39  {
40  /// <summary>
41  /// Gets the average risk free annual return rate
42  /// </summary>
43  /// <param name="model">The interest rate model</param>
44  /// <param name="startDate">Start date to calculate the average</param>
45  /// <param name="endDate">End date to calculate the average</param>
46  public static decimal GetRiskFreeRate(this IRiskFreeInterestRateModel model, DateTime startDate, DateTime endDate)
47  {
48  return model.GetAverageRiskFreeRate(Time.EachDay(startDate, endDate));
49  }
50 
51  /// <summary>
52  /// Gets the average Risk Free Rate from the interest rate of the given dates
53  /// </summary>
54  /// <param name="model">The interest rate model</param>
55  /// <param name="dates">
56  /// Collection of dates from which the interest rates will be computed and then the average of them
57  /// </param>
58  public static decimal GetAverageRiskFreeRate(this IRiskFreeInterestRateModel model, IEnumerable<DateTime> dates)
59  {
60  var interestRates = dates.Select(x => model.GetInterestRate(x)).DefaultIfEmpty(0);
61  return interestRates.Average();
62  }
63  }
64 }