Lean  $LEAN_TAG$
IMarginCallModel.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.Collections.Generic;
18 using QuantConnect.Orders;
19 
21 {
22  /// <summary>
23  /// Represents the model responsible for picking which orders should be executed during a margin call
24  /// </summary>
25  public interface IMarginCallModel
26  {
27  /// <summary>
28  /// Scan the portfolio and the updated data for a potential margin call situation which may get the holdings below zero!
29  /// If there is a margin call, liquidate the portfolio immediately before the portfolio gets sub zero.
30  /// </summary>
31  /// <param name="issueMarginCallWarning">Set to true if a warning should be issued to the algorithm</param>
32  /// <returns>True for a margin call on the holdings.</returns>
33  List<SubmitOrderRequest> GetMarginCallOrders(out bool issueMarginCallWarning);
34 
35  /// <summary>
36  /// Executes synchronous orders to bring the account within margin requirements.
37  /// </summary>
38  /// <param name="generatedMarginCallOrders">These are the margin call orders that were generated
39  /// by individual security margin models.</param>
40  /// <returns>The list of orders that were actually executed</returns>
41  List<OrderTicket> ExecuteMarginCall(IEnumerable<SubmitOrderRequest> generatedMarginCallOrders);
42  }
43 
44  /// <summary>
45  /// Provides access to a null implementation for <see cref="IMarginCallModel"/>
46  /// </summary>
47  public static class MarginCallModel
48  {
49  /// <summary>
50  /// Gets an instance of <see cref="IMarginCallModel"/> that will always
51  /// return an empty list of executed orders.
52  /// </summary>
53  public static readonly IMarginCallModel Null = new NullMarginCallModel();
54 
55  private sealed class NullMarginCallModel : IMarginCallModel
56  {
57  public List<SubmitOrderRequest> GetMarginCallOrders(out bool issueMarginCallWarning)
58  {
59  issueMarginCallWarning = false;
60  return new List<SubmitOrderRequest>();
61  }
62 
63  public List<OrderTicket> ExecuteMarginCall(IEnumerable<SubmitOrderRequest> generatedMarginCallOrders)
64  {
65  return new List<OrderTicket>();
66  }
67  }
68  }
69 }