Lean  $LEAN_TAG$
FutureSettlementModel.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.Logging;
18 
20 {
21  /// <summary>
22  /// Settlement model which can handle daily profit and loss settlement
23  /// </summary>
25  {
26  private DateTime _lastSettlementDate;
27  private decimal _settledFutureQuantity;
28  private decimal _settlementPrice;
29 
30  /// <summary>
31  /// Applies unsettledContractsTodaysProfit settlement rules
32  /// </summary>
33  /// <param name="applyFundsParameters">The funds application parameters</param>
34  public override void ApplyFunds(ApplyFundsSettlementModelParameters applyFundsParameters)
35  {
36  if(_settledFutureQuantity != 0)
37  {
38  var fill = applyFundsParameters.Fill;
39  var security = applyFundsParameters.Security;
40  var futureHolding = (FutureHolding)security.Holdings;
41 
42  var absoluteQuantityClosed = Math.Min(fill.AbsoluteFillQuantity, security.Holdings.AbsoluteQuantity);
43 
44  var absoluteQuantityClosedSettled = Math.Min(absoluteQuantityClosed, Math.Abs(_settledFutureQuantity));
45  var quantityClosedSettled = Math.Sign(-fill.FillQuantity) * absoluteQuantityClosedSettled;
46 
47  // reduce our settled future quantity proportionally too
48  var factor = quantityClosedSettled / _settledFutureQuantity;
49  _settledFutureQuantity -= quantityClosedSettled;
50 
51  // the passed in cash amount will hold the complete profit/loss of the trade, so we need to substract the settled profit we were given or taken from
52  var removedSettledProfit = factor * futureHolding.SettledProfit;
53  futureHolding.SettledProfit -= removedSettledProfit;
54 
55  applyFundsParameters.CashAmount = new CashAmount(applyFundsParameters.CashAmount.Amount - removedSettledProfit, applyFundsParameters.CashAmount.Currency);
56  }
57 
58  base.ApplyFunds(applyFundsParameters);
59  }
60 
61  /// <summary>
62  /// Scan for pending settlements
63  /// </summary>
64  /// <param name="settlementParameters">The settlement parameters</param>
65  public override void Scan(ScanSettlementModelParameters settlementParameters)
66  {
67  var security = settlementParameters.Security;
68 
69  // In the futures markets, losers pay winners every day. So once a day after the settlement time has passed we will update the cash book to reflect this
70  if (_lastSettlementDate.Date < security.LocalTime.Date)
71  {
72  if ((_lastSettlementDate != default) && security.Invested)
73  {
74  var futureHolding = (FutureHolding)security.Holdings;
75  var futureCache = (FutureCache)security.Cache;
76  _settlementPrice = futureCache.SettlementPrice;
77  _settledFutureQuantity = security.Holdings.Quantity;
78 
79  // We settled the daily P&L, losers pay winners
80  var dailyProfitLoss = futureHolding.TotalCloseProfit(includeFees: false, exitPrice: _settlementPrice) - futureHolding.SettledProfit;
81  if (dailyProfitLoss != 0)
82  {
83  futureHolding.SettledProfit += dailyProfitLoss;
84 
85  settlementParameters.Portfolio.CashBook[security.QuoteCurrency.Symbol].AddAmount(dailyProfitLoss);
86  Log.Trace($"FutureSettlementModel.Scan({security.Symbol}): {security.LocalTime} Daily P&L: {dailyProfitLoss} " +
87  $"Quantity: {_settledFutureQuantity} Settlement: {_settlementPrice} UnrealizedProfit: {futureHolding.UnrealizedProfit}");
88  }
89  }
90  _lastSettlementDate = security.LocalTime.Date;
91  }
92  }
93 
94  /// <summary>
95  /// Set the current datetime in terms of the exchange's local time zone
96  /// </summary>
97  /// <param name="newLocalTime">Current local time</param>
98  public void SetLocalDateTimeFrontier(DateTime newLocalTime)
99  {
100  _lastSettlementDate = newLocalTime.Date;
101  }
102  }
103 }