Lean  $LEAN_TAG$
CryptoFutureHolding.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 
17 {
18  /// <summary>
19  /// Crypto Future holdings implementation of the base securities class
20  /// </summary>
21  /// <seealso cref="SecurityHolding"/>
23  {
24  /// <summary>
25  /// Crypto Future Holding Class constructor
26  /// </summary>
27  /// <param name="security">The crypto future security being held</param>
28  /// <param name="currencyConverter">A currency converter instance</param>
29  public CryptoFutureHolding(Security security, ICurrencyConverter currencyConverter)
30  : base(security, currencyConverter)
31  {
32  }
33 
34  /// <summary>
35  /// Gets the total value of the specified <paramref name="quantity"/> of shares of this security
36  /// in the account currency
37  /// </summary>
38  /// <param name="quantity">The quantity of shares</param>
39  /// <param name="price">The current price</param>
40  /// <returns>The value of the quantity of shares in the account currency</returns>
41  public override ConvertibleCashAmount GetQuantityValue(decimal quantity, decimal price)
42  {
43  var cryptoFuture = (CryptoFuture)Security;
44 
45  Cash cash;
46  decimal notionalPositionValue;
47  // We could check quote currency or the contract multiplier being 1
48  if (!cryptoFuture.IsCryptoCoinFuture())
49  {
50  // https://www.binance.com/en/support/faq/how-to-calculate-cost-required-to-open-a-position-in-perpetual-futures-contracts-87fa7ee33b574f7084d42bd2ce2e463b
51  // example BTCUSDT: (9,253.30 * 1 BTC) = 9,253.3 USDT
52  notionalPositionValue = price * quantity * cryptoFuture.SymbolProperties.ContractMultiplier;
53 
54  // USDT is the QUOTE currency we will need to convert it into account currency
55  cash = cryptoFuture.QuoteCurrency;
56  }
57  else
58  {
59  // https://www.binance.com/en/support/faq/leverage-and-margin-in-coin-margined-futures-contracts-be2c7d9d95b04a7e8044ed02dd7dfe5c
60  // example BTCUSD: [ (10*100 USD) / 9,800 USD ] = 0.10204 BTC
61  notionalPositionValue = quantity * cryptoFuture.SymbolProperties.ContractMultiplier / price;
62 
63  // BTC is the BASE currency we will need to convert it into account currency
64  cash = cryptoFuture.BaseCurrency;
65  }
66 
67  return new ConvertibleCashAmount(notionalPositionValue, cash);
68  }
69  }
70 }