Lean  $LEAN_TAG$
BrokerageModelPythonWrapper.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 System.Collections.Generic;
18 using Python.Runtime;
24 using QuantConnect.Orders;
29 
30 namespace QuantConnect.Python
31 {
32  /// <summary>
33  /// Provides an implementation of <see cref="IBrokerageModel"/> that wraps a <see cref="PyObject"/> object
34  /// </summary>
36  {
37  /// <summary>
38  /// Constructor for initialising the <see cref="BrokerageModelPythonWrapper"/> class with wrapped <see cref="PyObject"/> object
39  /// </summary>
40  /// <param name="model">Models brokerage transactions, fees, and order</param>
41  public BrokerageModelPythonWrapper(PyObject model)
42  : base(model)
43  {
44  }
45 
46  /// <summary>
47  /// Gets or sets the account type used by this model
48  /// </summary>
50  {
51  get
52  {
53  return GetProperty<AccountType>(nameof(AccountType));
54  }
55  }
56 
57  /// <summary>
58  /// Gets the brokerages model percentage factor used to determine the required unused buying power for the account.
59  /// From 1 to 0. Example: 0 means no unused buying power is required. 0.5 means 50% of the buying power should be left unused.
60  /// </summary>
61  public decimal RequiredFreeBuyingPowerPercent
62  {
63  get
64  {
65  return GetProperty<decimal>(nameof(RequiredFreeBuyingPowerPercent));
66  }
67  }
68 
69  /// <summary>
70  /// Gets a map of the default markets to be used for each security type
71  /// </summary>
72  public IReadOnlyDictionary<SecurityType, string> DefaultMarkets
73  {
74  get
75  {
76  using (Py.GIL())
77  {
78  var markets = GetProperty(nameof(DefaultMarkets)) as dynamic;
79  if ((markets as PyObject).TryConvert(out IReadOnlyDictionary<SecurityType, string> csharpDic))
80  {
81  return csharpDic;
82  }
83 
84  var dic = new Dictionary<SecurityType, string>();
85  foreach (var item in markets)
86  {
87  using var pyItem = item as PyObject;
88  var market = pyItem.As<SecurityType>();
89  dic[market] = markets[item];
90  }
91 
92  (markets as PyObject).Dispose();
93  return dic;
94  }
95  }
96  }
97 
98  /// <summary>
99  /// Applies the split to the specified order ticket
100  /// </summary>
101  /// <param name="tickets">The open tickets matching the split event</param>
102  /// <param name="split">The split event data</param>
103  public void ApplySplit(List<OrderTicket> tickets, Split split)
104  {
105  InvokeMethod(nameof(ApplySplit), tickets, split);
106  }
107 
108  /// <summary>
109  /// Returns true if the brokerage would be able to execute this order at this time assuming
110  /// market prices are sufficient for the fill to take place. This is used to emulate the
111  /// brokerage fills in backtesting and paper trading. For example some brokerages may not perform
112  /// executions during extended market hours. This is not intended to be checking whether or not
113  /// the exchange is open, that is handled in the Security.Exchange property.
114  /// </summary>
115  /// <param name="security">The security being ordered</param>
116  /// <param name="order">The order to test for execution</param>
117  /// <returns>True if the brokerage would be able to perform the execution, false otherwise</returns>
118  public bool CanExecuteOrder(Security security, Order order)
119  {
120  return InvokeMethod<bool>(nameof(CanExecuteOrder), security, order);
121  }
122 
123  /// <summary>
124  /// Returns true if the brokerage could accept this order. This takes into account
125  /// order type, security type, and order size limits.
126  /// </summary>
127  /// <remarks>
128  /// For example, a brokerage may have no connectivity at certain times, or an order rate/size limit
129  /// </remarks>
130  /// <param name="security">The security being ordered</param>
131  /// <param name="order">The order to be processed</param>
132  /// <param name="message">If this function returns false, a brokerage message detailing why the order may not be submitted</param>
133  /// <returns>True if the brokerage could process the order, false otherwise</returns>
134  public bool CanSubmitOrder(Security security, Order order, out BrokerageMessageEvent message)
135  {
136  message = null;
137  var result = InvokeMethodWithOutParameters<bool>(nameof(CanSubmitOrder), new[] { typeof(BrokerageMessageEvent) },
138  out var outParameters, security, order, message);
139  message = outParameters[0] as BrokerageMessageEvent;
140 
141  return result;
142  }
143 
144  /// <summary>
145  /// Returns true if the brokerage would allow updating the order as specified by the request
146  /// </summary>
147  /// <param name="security">The security of the order</param>
148  /// <param name="order">The order to be updated</param>
149  /// <param name="request">The requested updated to be made to the order</param>
150  /// <param name="message">If this function returns false, a brokerage message detailing why the order may not be updated</param>
151  /// <returns>True if the brokerage would allow updating the order, false otherwise</returns>
152  public bool CanUpdateOrder(Security security, Order order, UpdateOrderRequest request, out BrokerageMessageEvent message)
153  {
154  message = null;
155  var result = InvokeMethodWithOutParameters<bool>(nameof(CanUpdateOrder), new[] { typeof(BrokerageMessageEvent) }, out var outParameters,
156  security, order, request, message);
157  message = outParameters[0] as BrokerageMessageEvent;
158 
159  return result;
160  }
161 
162  /// <summary>
163  /// Get the benchmark for this model
164  /// </summary>
165  /// <param name="securities">SecurityService to create the security with if needed</param>
166  /// <returns>The benchmark for this brokerage</returns>
168  {
169  return InvokeMethodAndWrapResult<IBenchmark>(nameof(GetBenchmark), (pyInstance) => new BenchmarkPythonWrapper(pyInstance), securities);
170  }
171 
172  /// <summary>
173  /// Gets a new fee model that represents this brokerage's fee structure
174  /// </summary>
175  /// <param name="security">The security to get a fee model for</param>
176  /// <returns>The new fee model for this brokerage</returns>
177  public IFeeModel GetFeeModel(Security security)
178  {
179  return InvokeMethodAndWrapResult<IFeeModel>(nameof(GetFeeModel), (pyInstance) => new FeeModelPythonWrapper(pyInstance), security);
180  }
181 
182  /// <summary>
183  /// Gets a new fill model that represents this brokerage's fill behavior
184  /// </summary>
185  /// <param name="security">The security to get fill model for</param>
186  /// <returns>The new fill model for this brokerage</returns>
188  {
189  return InvokeMethodAndWrapResult<IFillModel>(nameof(GetFillModel), (pyInstance) => new FillModelPythonWrapper(pyInstance), security);
190  }
191 
192  /// <summary>
193  /// Gets the brokerage's leverage for the specified security
194  /// </summary>
195  /// <param name="security">The security's whose leverage we seek</param>
196  /// <returns>The leverage for the specified security</returns>
197  public decimal GetLeverage(Security security)
198  {
199  return InvokeMethod<decimal>(nameof(GetLeverage), security);
200  }
201 
202  /// <summary>
203  /// Gets a new settlement model for the security
204  /// </summary>
205  /// <param name="security">The security to get a settlement model for</param>
206  /// <returns>The settlement model for this brokerage</returns>
208  {
209  return InvokeMethodAndWrapResult<ISettlementModel>(nameof(GetSettlementModel),
210  (pyInstance) => new SettlementModelPythonWrapper(pyInstance), security);
211  }
212 
213  /// <summary>
214  /// Gets a new settlement model for the security
215  /// </summary>
216  /// <param name="security">The security to get a settlement model for</param>
217  /// <param name="accountType">The account type</param>
218  /// <returns>The settlement model for this brokerage</returns>
219  [Obsolete("Flagged deprecated and will remove December 1st 2018")]
221  {
222  return InvokeMethod<ISettlementModel>(nameof(GetSettlementModel), security, accountType);
223  }
224 
225  /// <summary>
226  /// Gets a new slippage model that represents this brokerage's fill slippage behavior
227  /// </summary>
228  /// <param name="security">The security to get a slippage model for</param>
229  /// <returns>The new slippage model for this brokerage</returns>
231  {
232  return InvokeMethodAndWrapResult<ISlippageModel>(nameof(GetSlippageModel),
233  (pyInstance) => new SlippageModelPythonWrapper(pyInstance), security);
234  }
235 
236  /// <summary>
237  /// Determine if this symbol is shortable
238  /// </summary>
239  /// <param name="algorithm">The algorithm running</param>
240  /// <param name="symbol">The symbol to short</param>
241  /// <param name="quantity">The amount to short</param>
242  /// <returns></returns>
243  public bool Shortable(IAlgorithm algorithm, Symbol symbol, decimal quantity)
244  {
245  return InvokeMethod<bool>(nameof(Shortable), algorithm, symbol, quantity);
246  }
247 
248  /// <summary>
249  /// Gets a new buying power model for the security, returning the default model with the security's configured leverage.
250  /// For cash accounts, leverage = 1 is used.
251  /// </summary>
252  /// <param name="security">The security to get a buying power model for</param>
253  /// <returns>The buying power model for this brokerage/security</returns>
255  {
256  return InvokeMethodAndWrapResult<IBuyingPowerModel>(nameof(GetBuyingPowerModel),
257  (pyInstance) => new BuyingPowerModelPythonWrapper(pyInstance), security);
258  }
259 
260  /// <summary>
261  /// Gets a new buying power model for the security
262  /// </summary>
263  /// <param name="security">The security to get a buying power model for</param>
264  /// <param name="accountType">The account type</param>
265  /// <returns>The buying power model for this brokerage/security</returns>
266  [Obsolete("Flagged deprecated and will remove December 1st 2018")]
268  {
269  return InvokeMethod<IBuyingPowerModel>(nameof(GetBuyingPowerModel), security, accountType);
270  }
271 
272  /// <summary>
273  /// Gets the shortable provider
274  /// </summary>
275  /// <returns>Shortable provider</returns>
277  {
278  return InvokeMethodAndWrapResult<IShortableProvider>(nameof(GetShortableProvider),
279  (pyInstance) => new ShortableProviderPythonWrapper(pyInstance), security);
280  }
281 
282  /// <summary>
283  /// Convenience method to get the underlying <see cref="IBrokerageModel"/> object from the wrapper.
284  /// </summary>
285  /// <returns>Underlying <see cref="IBrokerageModel"/> object</returns>
287  {
288  using (Py.GIL())
289  {
290  return Instance.As<IBrokerageModel>();
291  }
292  }
293 
294  /// <summary>
295  /// Gets a new margin interest rate model for the security
296  /// </summary>
297  /// <param name="security">The security to get a margin interest rate model for</param>
298  /// <returns>The margin interest rate model for this brokerage</returns>
300  {
301  return InvokeMethodAndWrapResult<IMarginInterestRateModel>(nameof(GetMarginInterestRateModel),
302  (pyInstance) => new MarginInterestRateModelPythonWrapper(pyInstance), security);
303  }
304  }
305 }