Lean  $LEAN_TAG$
IBrokerageModel.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;
18 using System.Collections.Generic;
22 using QuantConnect.Orders;
27 using QuantConnect.Python;
28 
30 {
31  /// <summary>
32  /// Models brokerage transactions, fees, and order
33  /// </summary>
34  public interface IBrokerageModel
35  {
36  /// <summary>
37  /// Gets the account type used by this model
38  /// </summary>
40  {
41  get;
42  }
43 
44  /// <summary>
45  /// Gets the brokerages model percentage factor used to determine the required unused buying power for the account.
46  /// 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.
47  /// </summary>
49  {
50  get;
51  }
52 
53  /// <summary>
54  /// Gets a map of the default markets to be used for each security type
55  /// </summary>
56  IReadOnlyDictionary<SecurityType, string> DefaultMarkets { get; }
57 
58  /// <summary>
59  /// Returns true if the brokerage could accept this order. This takes into account
60  /// order type, security type, and order size limits.
61  /// </summary>
62  /// <remarks>
63  /// For example, a brokerage may have no connectivity at certain times, or an order rate/size limit
64  /// </remarks>
65  /// <param name="security">The security being ordered</param>
66  /// <param name="order">The order to be processed</param>
67  /// <param name="message">If this function returns false, a brokerage message detailing why the order may not be submitted</param>
68  /// <returns>True if the brokerage could process the order, false otherwise</returns>
69  bool CanSubmitOrder(Security security, Order order, out BrokerageMessageEvent message);
70 
71  /// <summary>
72  /// Returns true if the brokerage would allow updating the order as specified by the request
73  /// </summary>
74  /// <param name="security">The security of the order</param>
75  /// <param name="order">The order to be updated</param>
76  /// <param name="request">The requested updated to be made to the order</param>
77  /// <param name="message">If this function returns false, a brokerage message detailing why the order may not be updated</param>
78  /// <returns>True if the brokerage would allow updating the order, false otherwise</returns>
79  bool CanUpdateOrder(Security security, Order order, UpdateOrderRequest request, out BrokerageMessageEvent message);
80 
81  /// <summary>
82  /// Returns true if the brokerage would be able to execute this order at this time assuming
83  /// market prices are sufficient for the fill to take place. This is used to emulate the
84  /// brokerage fills in backtesting and paper trading. For example some brokerages may not perform
85  /// executions during extended market hours. This is not intended to be checking whether or not
86  /// the exchange is open, that is handled in the Security.Exchange property.
87  /// </summary>
88  /// <param name="security">The security being ordered</param>
89  /// <param name="order">The order to test for execution</param>
90  /// <returns>True if the brokerage would be able to perform the execution, false otherwise</returns>
91  bool CanExecuteOrder(Security security, Order order);
92 
93  /// <summary>
94  /// Applies the split to the specified order ticket
95  /// </summary>
96  /// <param name="tickets">The open tickets matching the split event</param>
97  /// <param name="split">The split event data</param>
98  void ApplySplit(List<OrderTicket> tickets, Split split);
99 
100  /// <summary>
101  /// Gets the brokerage's leverage for the specified security
102  /// </summary>
103  /// <param name="security">The security's whose leverage we seek</param>
104  /// <returns>The leverage for the specified security</returns>
105  decimal GetLeverage(Security security);
106 
107  /// <summary>
108  /// Get the benchmark for this model
109  /// </summary>
110  /// <param name="securities">SecurityService to create the security with if needed</param>
111  /// <returns>The benchmark for this brokerage</returns>
113 
114  /// <summary>
115  /// Gets a new fill model that represents this brokerage's fill behavior
116  /// </summary>
117  /// <param name="security">The security to get fill model for</param>
118  /// <returns>The new fill model for this brokerage</returns>
119  IFillModel GetFillModel(Security security);
120 
121  /// <summary>
122  /// Gets a new fee model that represents this brokerage's fee structure
123  /// </summary>
124  /// <param name="security">The security to get a fee model for</param>
125  /// <returns>The new fee model for this brokerage</returns>
126  IFeeModel GetFeeModel(Security security);
127 
128  /// <summary>
129  /// Gets a new slippage model that represents this brokerage's fill slippage behavior
130  /// </summary>
131  /// <param name="security">The security to get a slippage model for</param>
132  /// <returns>The new slippage model for this brokerage</returns>
134 
135  /// <summary>
136  /// Gets a new settlement model for the security
137  /// </summary>
138  /// <param name="security">The security to get a settlement model for</param>
139  /// <returns>The settlement model for this brokerage</returns>
141 
142  /// <summary>
143  /// Gets a new margin interest rate model for the security
144  /// </summary>
145  /// <param name="security">The security to get a margin interest rate model for</param>
146  /// <returns>The margin interest rate model for this brokerage</returns>
148 
149  /// <summary>
150  /// Gets a new settlement model for the security
151  /// </summary>
152  /// <param name="security">The security to get a settlement model for</param>
153  /// <param name="accountType">The account type</param>
154  /// <returns>The settlement model for this brokerage</returns>
155  [Obsolete("Flagged deprecated and will remove December 1st 2018")]
157 
158  /// <summary>
159  /// Gets a new buying power model for the security
160  /// </summary>
161  /// <param name="security">The security to get a buying power model for</param>
162  /// <returns>The buying power model for this brokerage/security</returns>
164 
165  /// <summary>
166  /// Gets a new buying power model for the security
167  /// </summary>
168  /// <param name="security">The security to get a buying power model for</param>
169  /// <param name="accountType">The account type</param>
170  /// <returns>The buying power model for this brokerage/security</returns>
171  [Obsolete("Flagged deprecated and will remove December 1st 2018")]
173 
174  /// <summary>
175  /// Gets the shortable provider
176  /// </summary>
177  /// <returns>Shortable provider</returns>
179  }
180 
181  /// <summary>
182  /// Provides factory method for creating an <see cref="IBrokerageModel"/> from the <see cref="BrokerageName"/> enum
183  /// </summary>
184  public static class BrokerageModel
185  {
186  /// <summary>
187  /// Creates a new <see cref="IBrokerageModel"/> for the specified <see cref="BrokerageName"/>
188  /// </summary>
189  /// <param name="orderProvider">The order provider</param>
190  /// <param name="brokerage">The name of the brokerage</param>
191  /// <param name="accountType">The account type</param>
192  /// <returns>The model for the specified brokerage</returns>
193  public static IBrokerageModel Create(IOrderProvider orderProvider, BrokerageName brokerage, AccountType accountType)
194  {
195  switch (brokerage)
196  {
197  case BrokerageName.Default:
198  return new DefaultBrokerageModel(accountType);
199 
200  case BrokerageName.InteractiveBrokersBrokerage:
201  return new InteractiveBrokersBrokerageModel(accountType);
202 
203  case BrokerageName.TradierBrokerage:
204  return new TradierBrokerageModel(accountType);
205 
206  case BrokerageName.OandaBrokerage:
207  return new OandaBrokerageModel(accountType);
208 
209  case BrokerageName.FxcmBrokerage:
210  return new FxcmBrokerageModel(accountType);
211 
212  case BrokerageName.Bitfinex:
213  return new BitfinexBrokerageModel(accountType);
214 
215  case BrokerageName.BinanceFutures:
216  return new BinanceFuturesBrokerageModel(accountType);
217 
218  case BrokerageName.BinanceCoinFutures:
219  return new BinanceCoinFuturesBrokerageModel(accountType);
220 
221  case BrokerageName.Binance:
222  return new BinanceBrokerageModel(accountType);
223 
224  case BrokerageName.BinanceUS:
225  return new BinanceUSBrokerageModel(accountType);
226 
227  case BrokerageName.GDAX:
228  return new GDAXBrokerageModel(accountType);
229 
230  case BrokerageName.Coinbase:
231  return new CoinbaseBrokerageModel(accountType);
232 
233  case BrokerageName.AlphaStreams:
234  return new AlphaStreamsBrokerageModel(accountType);
235 
236  case BrokerageName.Zerodha:
237  return new ZerodhaBrokerageModel(accountType);
238 
239  case BrokerageName.Axos:
240  return new AxosClearingBrokerageModel(accountType);
241 
242  case BrokerageName.TradingTechnologies:
243  return new TradingTechnologiesBrokerageModel(accountType);
244 
245  case BrokerageName.Samco:
246  return new SamcoBrokerageModel(accountType);
247 
248  case BrokerageName.Kraken:
249  return new KrakenBrokerageModel(accountType);
250 
251  case BrokerageName.Exante:
252  return new ExanteBrokerageModel(accountType);
253 
254  case BrokerageName.FTX:
255  return new FTXBrokerageModel(accountType);
256 
257  case BrokerageName.FTXUS:
258  return new FTXUSBrokerageModel(accountType);
259 
260  case BrokerageName.Wolverine:
261  return new WolverineBrokerageModel(accountType);
262 
263  case BrokerageName.TDAmeritrade:
264  return new TDAmeritradeBrokerageModel(accountType);
265 
266  case BrokerageName.RBI:
267  return new RBIBrokerageModel(accountType);
268 
269  case BrokerageName.Bybit:
270  return new BybitBrokerageModel(accountType);
271 
272  case BrokerageName.Eze:
273  return new EzeBrokerageModel(accountType);
274 
275  default:
276  throw new ArgumentOutOfRangeException(nameof(brokerage), brokerage, null);
277  }
278  }
279 
280 
281  /// <summary>
282  /// Gets the corresponding <see cref="BrokerageName"/> for the specified <see cref="IBrokerageModel"/>
283  /// </summary>
284  /// <param name="brokerageModel">The brokerage model</param>
285  /// <returns>The <see cref="BrokerageName"/> for the specified brokerage model</returns>
286  public static BrokerageName GetBrokerageName(IBrokerageModel brokerageModel)
287  {
288  var model = brokerageModel;
289  if (brokerageModel is BrokerageModelPythonWrapper)
290  {
291  model = (brokerageModel as BrokerageModelPythonWrapper).GetModel();
292  }
293 
294  // Case order matters to ensure we get the correct brokerage name from the inheritance chain
295  switch (model)
296  {
298  return BrokerageName.InteractiveBrokersBrokerage;
299 
300  case TradierBrokerageModel _:
301  return BrokerageName.TradierBrokerage;
302 
303  case OandaBrokerageModel _:
304  return BrokerageName.OandaBrokerage;
305 
306  case FxcmBrokerageModel _:
307  return BrokerageName.FxcmBrokerage;
308 
309  case BitfinexBrokerageModel _:
310  return BrokerageName.Bitfinex;
311 
313  return BrokerageName.BinanceUS;
314 
315  case BinanceBrokerageModel _:
316  return BrokerageName.Binance;
317 
318  case GDAXBrokerageModel _:
319  return BrokerageName.GDAX;
320 
321  case CoinbaseBrokerageModel _:
322  return BrokerageName.Coinbase;
323 
325  return BrokerageName.AlphaStreams;
326 
327  case ZerodhaBrokerageModel _:
328  return BrokerageName.Zerodha;
329 
331  return BrokerageName.Axos;
332 
334  return BrokerageName.TradingTechnologies;
335 
336  case SamcoBrokerageModel _:
337  return BrokerageName.Samco;
338 
339  case KrakenBrokerageModel _:
340  return BrokerageName.Kraken;
341 
342  case ExanteBrokerageModel _:
343  return BrokerageName.Exante;
344 
345  case FTXUSBrokerageModel _:
346  return BrokerageName.FTXUS;
347 
348  case FTXBrokerageModel _:
349  return BrokerageName.FTX;
350 
352  return BrokerageName.Wolverine;
353 
355  return BrokerageName.TDAmeritrade;
356 
357  case RBIBrokerageModel _:
358  return BrokerageName.RBI;
359 
360  case BybitBrokerageModel _:
361  return BrokerageName.Bybit;
362 
363  case EzeBrokerageModel _:
364  return BrokerageName.Eze;
365 
366  case DefaultBrokerageModel _:
367  return BrokerageName.Default;
368 
369  default:
370  throw new ArgumentOutOfRangeException(nameof(brokerageModel), brokerageModel, null);
371  }
372  }
373  }
374 }