Lean  $LEAN_TAG$
MarginCallModelPythonWrapper.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 Python.Runtime;
17 using QuantConnect.Orders;
19 using System;
20 using System.Collections.Generic;
21 using System.Linq;
22 
23 namespace QuantConnect.Python
24 {
25  /// <summary>
26  /// Provides a margin call model that wraps a <see cref="PyObject"/> object that represents the model responsible for picking which orders should be executed during a margin call
27  /// </summary>
29  {
30  /// <summary>
31  /// Constructor for initialising the <see cref="MarginCallModelPythonWrapper"/> class with wrapped <see cref="PyObject"/> object
32  /// </summary>
33  /// <param name="model">Represents the model responsible for picking which orders should be executed during a margin call</param>
34  public MarginCallModelPythonWrapper(PyObject model)
35  : base(model)
36  {
37  }
38 
39  /// <summary>
40  /// Executes synchronous orders to bring the account within margin requirements.
41  /// </summary>
42  /// <param name="generatedMarginCallOrders">These are the margin call orders that were generated
43  /// by individual security margin models.</param>
44  /// <returns>The list of orders that were actually executed</returns>
45  public List<OrderTicket> ExecuteMarginCall(IEnumerable<SubmitOrderRequest> generatedMarginCallOrders)
46  {
47  return InvokeMethod<List<OrderTicket>>(nameof(ExecuteMarginCall), generatedMarginCallOrders);
48  }
49 
50  /// <summary>
51  /// Scan the portfolio and the updated data for a potential margin call situation which may get the holdings below zero!
52  /// If there is a margin call, liquidate the portfolio immediately before the portfolio gets sub zero.
53  /// </summary>
54  /// <param name="issueMarginCallWarning">Set to true if a warning should be issued to the algorithm</param>
55  /// <returns>True for a margin call on the holdings.</returns>
56  public List<SubmitOrderRequest> GetMarginCallOrders(out bool issueMarginCallWarning)
57  {
58  issueMarginCallWarning = false;
59  var requests = InvokeMethodWithOutParameters<List<SubmitOrderRequest>>(nameof(GetMarginCallOrders), new[] { typeof(bool) },
60  out var outParameters, issueMarginCallWarning);
61  issueMarginCallWarning = (bool)outParameters[0] || requests.Count > 0;
62 
63  return requests;
64  }
65  }
66 }