Lean  $LEAN_TAG$
BinanceFutureMarginInterestRateModel.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;
19 
21 {
22  /// <summary>
23  /// The responsability of this model is to apply future funding rate cash flows to the portfolio based on open positions
24  /// </summary>
26  {
27  private DateTime _nextFundingRateApplication = DateTime.MaxValue;
28 
29  /// <summary>
30  /// Apply margin interest rates to the portfolio
31  /// </summary>
32  /// <param name="marginInterestRateParameters">The parameters to use</param>
33  public void ApplyMarginInterestRate(MarginInterestRateParameters marginInterestRateParameters)
34  {
35  var security = marginInterestRateParameters.Security;
36  var time = marginInterestRateParameters.Time;
37  var cryptoFuture = (CryptoFuture)security;
38  if (!cryptoFuture.Invested)
39  {
40  // nothing to do
41  _nextFundingRateApplication = DateTime.MaxValue;
42  return;
43  }
44  else if (_nextFundingRateApplication == DateTime.MaxValue)
45  {
46  // we opened a new position
47  _nextFundingRateApplication = GetNextFundingRateApplication(time);
48  }
49 
50  var marginInterest = cryptoFuture.Cache.GetData<MarginInterestRate>();
51  if(marginInterest == null)
52  {
53  return;
54  }
55 
56  while(time >= _nextFundingRateApplication)
57  {
58  // When the funding rate is positive, the price of the perpetual contract is higher than the mark price,
59  // thus, traders who are long pay for short positions. Conversely, a negative funding rate indicates that perpetual
60  // prices are below the mark price, which means that short positions pay for longs.
61  // Funding Amount = Nominal Value of Positions * Funding Rate
62 
63  var holdings = cryptoFuture.Holdings;
64 
65  var positionValue = cryptoFuture.Holdings.GetQuantityValue(holdings.Quantity);
66 
67  var funding = marginInterest.InterestRate * positionValue.Amount;
68 
69  funding *= -1;
70  // '* -1' because:
71  // - we pay when 'funding' positive:
72  // long position & positive rate
73  // short position & negative rate
74  // - we ear when 'funding' negative:
75  // long position & negative rate
76  // short position & positive rate
77  positionValue.Cash.AddAmount(funding);
78 
79  _nextFundingRateApplication = GetNextFundingRateApplication(_nextFundingRateApplication);
80  }
81  }
82 
83  private static DateTime GetNextFundingRateApplication(DateTime currentTime)
84  {
85  if(currentTime.Hour >= 16)
86  {
87  // tomorrow 00:00
88  return currentTime.Date.AddDays(1);
89  }
90  else if (currentTime.Hour >= 8)
91  {
92  return currentTime.Date.AddHours(16);
93  }
94  else
95  {
96  return currentTime.Date.AddHours(8);
97  }
98  }
99  }
100 }