Lean  $LEAN_TAG$
Crypto.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 QuantConnect.Data;
22 using System;
23 
25 {
26  /// <summary>
27  /// Crypto Security Object Implementation for Crypto Assets
28  /// </summary>
29  /// <seealso cref="Security"/>
31  {
32  /// <summary>
33  /// Gets the currency acquired by going long this currency pair
34  /// </summary>
35  /// <remarks>
36  /// For example, the EUR/USD has a base currency of the euro, and as a result
37  /// of going long the EUR/USD a trader is acquiring euros in exchange for US dollars
38  /// </remarks>
39  public Cash BaseCurrency { get; protected set; }
40 
41  /// <summary>
42  /// Constructor for the Crypto security
43  /// </summary>
44  /// <param name="exchangeHours">Defines the hours this exchange is open</param>
45  /// <param name="quoteCurrency">The cash object that represent the quote currency</param>
46  /// <param name="baseCurrency">The cash object that represent the base currency</param>
47  /// <param name="config">The subscription configuration for this security</param>
48  /// <param name="symbolProperties">The symbol properties for this security</param>
49  /// <param name="currencyConverter">Currency converter used to convert <see cref="CashAmount"/>
50  /// instances into units of the account currency</param>
51  /// <param name="registeredTypes">Provides all data types registered in the algorithm</param>
52  public Crypto(SecurityExchangeHours exchangeHours,
53  Cash quoteCurrency,
54  Cash baseCurrency,
56  SymbolProperties symbolProperties,
57  ICurrencyConverter currencyConverter,
59  : base(config,
60  quoteCurrency,
61  symbolProperties,
62  new CryptoExchange(exchangeHours),
63  new ForexCache(),
65  new ImmediateFillModel(),
66  new GDAXFeeModel(),
67  NullSlippageModel.Instance,
69  Securities.VolatilityModel.Null,
71  new ForexDataFilter(),
73  currencyConverter,
74  registeredTypes,
75  Securities.MarginInterestRateModel.Null
76  )
77  {
78  BaseCurrency = baseCurrency;
79  Holdings = new CryptoHolding(this, currencyConverter);
80  }
81 
82  /// <summary>
83  /// Constructor for the Crypto security
84  /// </summary>
85  /// <param name="symbol">The security's symbol</param>
86  /// <param name="exchangeHours">Defines the hours this exchange is open</param>
87  /// <param name="quoteCurrency">The cash object that represent the quote currency</param>
88  /// <param name="baseCurrency">The cash object that represent the base currency</param>
89  /// <param name="symbolProperties">The symbol properties for this security</param>
90  /// <param name="currencyConverter">Currency converter used to convert <see cref="CashAmount"/>
91  /// instances into units of the account currency</param>
92  /// <param name="registeredTypes">Provides all data types registered in the algorithm</param>
93  /// <param name="securityCache">Cache to store <see cref="Security"/> data</param>
94  public Crypto(Symbol symbol,
95  SecurityExchangeHours exchangeHours,
96  Cash quoteCurrency,
97  Cash baseCurrency,
98  SymbolProperties symbolProperties,
99  ICurrencyConverter currencyConverter,
100  IRegisteredSecurityDataTypesProvider registeredTypes,
101  SecurityCache securityCache)
102  : base(symbol,
103  quoteCurrency,
104  symbolProperties,
105  new CryptoExchange(exchangeHours),
106  securityCache,
108  new ImmediateFillModel(),
109  new GDAXFeeModel(),
110  NullSlippageModel.Instance,
112  Securities.VolatilityModel.Null,
113  new CashBuyingPowerModel(),
114  new ForexDataFilter(),
116  currencyConverter,
117  registeredTypes,
118  Securities.MarginInterestRateModel.Null
119  )
120  {
121  BaseCurrency = baseCurrency;
122  Holdings = new CryptoHolding(this, currencyConverter);
123  }
124 
125  /// <summary>
126  /// Get the current value of the security.
127  /// </summary>
128  public override decimal Price => Cache.GetData<TradeBar>()?.Close ?? Cache.Price;
129 
130  /// <summary>
131  /// Decomposes the specified currency pair into a base and quote currency provided as out parameters
132  /// </summary>
133  /// <param name="symbol">The input symbol to be decomposed</param>
134  /// <param name="symbolProperties">The symbol properties for this security</param>
135  /// <param name="baseCurrency">The output base currency</param>
136  /// <param name="quoteCurrency">The output quote currency</param>
137  public static void DecomposeCurrencyPair(Symbol symbol, SymbolProperties symbolProperties, out string baseCurrency, out string quoteCurrency)
138  {
139  quoteCurrency = symbolProperties.QuoteCurrency;
140  if (symbol.Value.EndsWith(quoteCurrency))
141  {
142  baseCurrency = symbol.Value.RemoveFromEnd(quoteCurrency);
143  }
144  else
145  {
146  throw new InvalidOperationException($"symbol doesn't end with {quoteCurrency}");
147  }
148  }
149  }
150 }