Lean  $LEAN_TAG$
FillModelPythonWrapper.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;
20 using System.Collections.Generic;
21 
22 namespace QuantConnect.Python
23 {
24  /// <summary>
25  /// Wraps a <see cref="PyObject"/> object that represents a model that simulates order fill events
26  /// </summary>
28  {
29  private readonly BasePythonWrapper<FillModel> _model;
30 
31  /// <summary>
32  /// Constructor for initialising the <see cref="FillModelPythonWrapper"/> class with wrapped <see cref="PyObject"/> object
33  /// </summary>
34  /// <param name="model">Represents a model that simulates order fill events</param>
35  public FillModelPythonWrapper(PyObject model)
36  {
37  _model = new BasePythonWrapper<FillModel>(model, false);
38  using (Py.GIL())
39  {
40  (model as dynamic).SetPythonWrapper(this);
41  }
42  }
43 
44  /// <summary>
45  /// Return an order event with the fill details
46  /// </summary>
47  /// <param name="parameters">A parameters object containing the security and order</param>
48  /// <returns>Order fill information detailing the average price and quantity filled.</returns>
49  public override Fill Fill(FillModelParameters parameters)
50  {
51  Parameters = parameters;
52  return _model.InvokeMethod<Fill>(nameof(Fill), parameters);
53  }
54 
55  /// <summary>
56  /// Limit Fill Model. Return an order event with the fill details.
57  /// </summary>
58  /// <param name="asset">Stock Object to use to help model limit fill</param>
59  /// <param name="order">Order to fill. Alter the values directly if filled.</param>
60  /// <returns>Order fill information detailing the average price and quantity filled.</returns>
61  public override OrderEvent LimitFill(Security asset, LimitOrder order)
62  {
63  return _model.InvokeMethod<OrderEvent>(nameof(LimitFill), asset, order);
64  }
65 
66  /// <summary>
67  /// Limit if Touched Fill Model. Return an order event with the fill details.
68  /// </summary>
69  /// <param name="asset">Asset we're trading this order</param>
70  /// <param name="order"><see cref="LimitIfTouchedOrder"/> Order to Check, return filled if true</param>
71  /// <returns>Order fill information detailing the average price and quantity filled.</returns>
73  {
74  return _model.InvokeMethod<OrderEvent>(nameof(LimitIfTouchedFill), asset, order);
75  }
76 
77  /// <summary>
78  /// Model the slippage on a market order: fixed percentage of order price
79  /// </summary>
80  /// <param name="asset">Asset we're trading this order</param>
81  /// <param name="order">Order to update</param>
82  /// <returns>Order fill information detailing the average price and quantity filled.</returns>
83  public override OrderEvent MarketFill(Security asset, MarketOrder order)
84  {
85  return _model.InvokeMethod<OrderEvent>(nameof(MarketFill), asset, order);
86  }
87 
88  /// <summary>
89  /// Market on Close Fill Model. Return an order event with the fill details
90  /// </summary>
91  /// <param name="asset">Asset we're trading with this order</param>
92  /// <param name="order">Order to be filled</param>
93  /// <returns>Order fill information detailing the average price and quantity filled.</returns>
95  {
96  return _model.InvokeMethod<OrderEvent>(nameof(MarketOnCloseFill), asset, order);
97  }
98 
99  /// <summary>
100  /// Market on Open Fill Model. Return an order event with the fill details
101  /// </summary>
102  /// <param name="asset">Asset we're trading with this order</param>
103  /// <param name="order">Order to be filled</param>
104  /// <returns>Order fill information detailing the average price and quantity filled.</returns>
106  {
107  return _model.InvokeMethod<OrderEvent>(nameof(MarketOnOpenFill), asset, order);
108  }
109 
110  /// <summary>
111  /// Stop Limit Fill Model. Return an order event with the fill details.
112  /// </summary>
113  /// <param name="asset">Asset we're trading this order</param>
114  /// <param name="order">Stop Limit Order to Check, return filled if true</param>
115  /// <returns>Order fill information detailing the average price and quantity filled.</returns>
116  public override OrderEvent StopLimitFill(Security asset, StopLimitOrder order)
117  {
118  return _model.InvokeMethod<OrderEvent>(nameof(StopLimitFill), asset, order);
119  }
120 
121  /// <summary>
122  /// Stop Market Fill Model. Return an order event with the fill details.
123  /// </summary>
124  /// <param name="asset">Asset we're trading this order</param>
125  /// <param name="order">Trailing Stop Order to check, return filled if true</param>
126  /// <returns>Order fill information detailing the average price and quantity filled.</returns>
127  public override OrderEvent StopMarketFill(Security asset, StopMarketOrder order)
128  {
129  return _model.InvokeMethod<OrderEvent>(nameof(StopMarketFill), asset, order);
130  }
131 
132  /// <summary>
133  /// Trailing Stop Fill Model. Return an order event with the fill details.
134  /// </summary>
135  /// <param name="asset">Asset we're trading this order</param>
136  /// <param name="order">Stop Order to Check, return filled if true</param>
137  /// <returns>Order fill information detailing the average price and quantity filled.</returns>
139  {
140  return _model.InvokeMethod<OrderEvent>(nameof(TrailingStopFill), asset, order);
141  }
142 
143  /// <summary>
144  /// Default combo market fill model for the base security class. Fills at the last traded price for each leg.
145  /// </summary>
146  /// <param name="order">Order to fill</param>
147  /// <param name="parameters">Fill parameters for the order</param>
148  /// <returns>Order fill information detailing the average price and quantity filled for each leg. If any of the fills fails, none of the orders will be filled and the returned list will be empty</returns>
149  public override List<OrderEvent> ComboMarketFill(Order order, FillModelParameters parameters)
150  {
151  return _model.InvokeMethod<List<OrderEvent>>(nameof(ComboMarketFill), order, parameters);
152  }
153 
154  /// <summary>
155  /// Default combo limit fill model for the base security class. Fills at the sum of prices for the assets of every leg.
156  /// </summary>
157  /// <param name="order">Order to fill</param>
158  /// <param name="parameters">Fill parameters for the order</param>
159  /// <returns>Order fill information detailing the average price and quantity filled for each leg. If any of the fills fails, none of the orders will be filled and the returned list will be empty</returns>
160  public override List<OrderEvent> ComboLimitFill(Order order, FillModelParameters parameters)
161  {
162  return _model.InvokeMethod<List<OrderEvent>>(nameof(ComboLimitFill), order, parameters);
163  }
164 
165  /// <summary>
166  /// Default combo limit fill model for the base security class. Fills at the limit price for each leg
167  /// </summary>
168  /// <param name="order">Order to fill</param>
169  /// <param name="parameters">Fill parameters for the order</param>
170  /// <returns>Order fill information detailing the average price and quantity filled for each leg. If any of the fills fails, none of the orders will be filled and the returned list will be empty</returns>
171  public override List<OrderEvent> ComboLegLimitFill(Order order, FillModelParameters parameters)
172  {
173  return _model.InvokeMethod<List<OrderEvent>>(nameof(ComboLegLimitFill), order, parameters);
174  }
175 
176  /// <summary>
177  /// Get the minimum and maximum price for this security in the last bar:
178  /// </summary>
179  /// <param name="asset">Security asset we're checking</param>
180  /// <param name="direction">The order direction, decides whether to pick bid or ask</param>
181  protected override Prices GetPrices(Security asset, OrderDirection direction)
182  {
183  return _model.InvokeMethod<Prices>(nameof(GetPrices), asset, direction);
184  }
185 
186  /// <summary>
187  /// Get the minimum and maximum price for this security in the last bar:
188  /// </summary>
189  /// <param name="asset">Security asset we're checking</param>
190  /// <param name="direction">The order direction, decides whether to pick bid or ask</param>
191  /// <remarks>This method was implemented temporarily to help the refactoring of fill models (GH #4567)</remarks>
192  internal Prices GetPricesInternal(Security asset, OrderDirection direction)
193  {
194  return GetPrices(asset, direction);
195  }
196  }
197 }