Lean  $LEAN_TAG$
ConstantCurrencyConversion.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 System.Collections.Generic;
19 using System.Linq;
20 
22 {
23  /// <summary>
24  /// Provides an implementation of <see cref="ICurrencyConversion"/> with a fixed conversion rate
25  /// </summary>
27  {
28  private decimal _conversionRate;
29 
30  /// <summary>
31  /// Event fired when the conversion rate is updated
32  /// </summary>
33  public event EventHandler<decimal> ConversionRateUpdated;
34 
35  /// <summary>
36  /// The currency this conversion converts from
37  /// </summary>
38  public string SourceCurrency { get; }
39 
40  /// <summary>
41  /// The currency this conversion converts to
42  /// </summary>
43  public string DestinationCurrency { get; }
44 
45  /// <summary>
46  /// The current conversion rate
47  /// </summary>
48  public decimal ConversionRate
49  {
50  get
51  {
52  return _conversionRate;
53  }
54  set
55  {
56  if (_conversionRate != value)
57  {
58  // only update if there was actually one
59  _conversionRate = value;
60  ConversionRateUpdated?.Invoke(this, value);
61  }
62  }
63  }
64 
65  /// <summary>
66  /// The securities which the conversion rate is based on
67  /// </summary>
68  public IEnumerable<Security> ConversionRateSecurities => Enumerable.Empty<Security>();
69 
70  /// <summary>
71  /// Initializes a new instance of the <see cref="ConstantCurrencyConversion"/> class.
72  /// </summary>
73  /// <param name="sourceCurrency">The currency this conversion converts from</param>
74  /// <param name="destinationCurrency">The currency this conversion converts to</param>
75  /// <param name="conversionRate">The conversion rate between the currencies</param>
76  public ConstantCurrencyConversion(string sourceCurrency, string destinationCurrency, decimal conversionRate = 1m)
77  {
78  SourceCurrency = sourceCurrency;
79  DestinationCurrency = destinationCurrency;
80  ConversionRate = conversionRate;
81  }
82 
83  /// <summary>
84  /// Marks the conversion rate as potentially outdated, needing an update based on the latest data
85  /// </summary>
86  /// <remarks>This conversion is not based on securities, so we don't really need an update</remarks>
87  public void Update()
88  {
89  }
90 
91  /// <summary>
92  /// Creates a new identity conversion, where the conversion rate is set to 1 and the source and destination currencies might the same
93  /// </summary>
94  /// <param name="sourceCurrency">The currency this conversion converts from</param>
95  /// <param name="destinationCurrency">The currency this conversion converts to. If null, the destination and source currencies are the same</param>
96  /// <returns>The identity currency conversion</returns>
97  public static ConstantCurrencyConversion Identity(string sourceCurrency, string destinationCurrency = null)
98  {
99  return new ConstantCurrencyConversion(sourceCurrency, destinationCurrency ?? sourceCurrency);
100  }
101 
102  /// <summary>
103  /// Returns an instance of <see cref="ConstantCurrencyConversion"/> that represents a null conversion
104  /// </summary>
105  public static ConstantCurrencyConversion Null(string sourceCurrency, string destinationCurrency)
106  {
107  return new ConstantCurrencyConversion(sourceCurrency, destinationCurrency, 0m);
108  }
109  }
110 }