Lean  $LEAN_TAG$
Forex.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 QuantConnect.Data;
21 using QuantConnect.Util;
22 
24 {
25  /// <summary>
26  /// FOREX Security Object Implementation for FOREX Assets
27  /// </summary>
28  /// <seealso cref="Security"/>
30  {
31  /// <summary>
32  /// Gets the currency acquired by going long this currency pair
33  /// </summary>
34  /// <remarks>
35  /// For example, the EUR/USD has a base currency of the euro, and as a result
36  /// of going long the EUR/USD a trader is acquiring euros in exchange for US dollars
37  /// </remarks>
38  public Cash BaseCurrency { get; protected set; }
39 
40  /// <summary>
41  /// Constructor for the forex security
42  /// </summary>
43  /// <param name="exchangeHours">Defines the hours this exchange is open</param>
44  /// <param name="quoteCurrency">The cash object that represent the quote currency</param>
45  /// <param name="baseCurrency">The cash object that represent the base currency</param>
46  /// <param name="config">The subscription configuration for this security</param>
47  /// <param name="symbolProperties">The symbol properties for this security</param>
48  /// <param name="currencyConverter">Currency converter used to convert <see cref="CashAmount"/>
49  /// instances into units of the account currency</param>
50  /// <param name="registeredTypes">Provides all data types registered in the algorithm</param>
51  public Forex(SecurityExchangeHours exchangeHours,
52  Cash quoteCurrency,
53  Cash baseCurrency,
55  SymbolProperties symbolProperties,
56  ICurrencyConverter currencyConverter,
58  : base(config,
59  quoteCurrency,
60  symbolProperties,
61  new ForexExchange(exchangeHours),
62  new ForexCache(),
64  new ImmediateFillModel(),
66  NullSlippageModel.Instance,
68  Securities.VolatilityModel.Null,
69  new SecurityMarginModel(50m),
70  new ForexDataFilter(),
72  currencyConverter,
73  registeredTypes,
74  Securities.MarginInterestRateModel.Null
75  )
76  {
77  BaseCurrency = baseCurrency;
78  Holdings = new ForexHolding(this, currencyConverter);
79  }
80 
81  /// <summary>
82  /// Constructor for the forex security
83  /// </summary>
84  /// <param name="symbol">The security's symbol</param>
85  /// <param name="exchangeHours">Defines the hours this exchange is open</param>
86  /// <param name="quoteCurrency">The cash object that represent the quote currency</param>
87  /// <param name="baseCurrency">The cash object that represent the base currency</param>
88  /// <param name="symbolProperties">The symbol properties for this security</param>
89  /// <param name="currencyConverter">Currency converter used to convert <see cref="CashAmount"/>
90  /// instances into units of the account currency</param>
91  /// <param name="registeredTypes">Provides all data types registered in the algorithm</param>
92  /// <param name="securityCache">Cache for storing Security data</param>
93  public Forex(Symbol symbol,
94  SecurityExchangeHours exchangeHours,
95  Cash quoteCurrency,
96  Cash baseCurrency,
97  SymbolProperties symbolProperties,
98  ICurrencyConverter currencyConverter,
100  SecurityCache securityCache)
101  : base(symbol,
102  quoteCurrency,
103  symbolProperties,
104  new ForexExchange(exchangeHours),
105  securityCache,
107  new ImmediateFillModel(),
109  NullSlippageModel.Instance,
111  Securities.VolatilityModel.Null,
112  new SecurityMarginModel(50m),
113  new ForexDataFilter(),
115  currencyConverter,
116  registeredTypes,
117  Securities.MarginInterestRateModel.Null
118  )
119  {
120  BaseCurrency = baseCurrency;
121  Holdings = new ForexHolding(this, currencyConverter);
122  }
123 
124  /// <summary>
125  /// Decomposes the specified currency pair into a base and quote currency provided as out parameters
126  /// </summary>
127  /// <param name="currencyPair">The input currency pair to be decomposed, for example, "EURUSD"</param>
128  /// <param name="baseCurrency">The output base currency</param>
129  /// <param name="quoteCurrency">The output quote currency</param>
130  public static void DecomposeCurrencyPair(string currencyPair, out string baseCurrency, out string quoteCurrency)
131  {
132  if (!CurrencyPairUtil.IsForexDecomposable(currencyPair))
133  {
134  throw new ArgumentException($"Currency pairs must be exactly 6 characters: {currencyPair}");
135  }
136 
137  baseCurrency = currencyPair.Substring(0, 3);
138  quoteCurrency = currencyPair.Substring(3);
139  }
140  }
141 }