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  case BrokerageName.TerminalLink:
199  return new DefaultBrokerageModel(accountType);
200 
201  case BrokerageName.Alpaca:
202  return new AlpacaBrokerageModel();
203 
204  case BrokerageName.InteractiveBrokersBrokerage:
205  return new InteractiveBrokersBrokerageModel(accountType);
206 
207  case BrokerageName.TradierBrokerage:
208  return new TradierBrokerageModel(accountType);
209 
210  case BrokerageName.OandaBrokerage:
211  return new OandaBrokerageModel(accountType);
212 
213  case BrokerageName.FxcmBrokerage:
214  return new FxcmBrokerageModel(accountType);
215 
216  case BrokerageName.Bitfinex:
217  return new BitfinexBrokerageModel(accountType);
218 
219  case BrokerageName.BinanceFutures:
220  return new BinanceFuturesBrokerageModel(accountType);
221 
222  case BrokerageName.BinanceCoinFutures:
223  return new BinanceCoinFuturesBrokerageModel(accountType);
224 
225  case BrokerageName.Binance:
226  return new BinanceBrokerageModel(accountType);
227 
228  case BrokerageName.BinanceUS:
229  return new BinanceUSBrokerageModel(accountType);
230 
231  case BrokerageName.GDAX:
232  return new GDAXBrokerageModel(accountType);
233 
234  case BrokerageName.Coinbase:
235  return new CoinbaseBrokerageModel(accountType);
236 
237  case BrokerageName.AlphaStreams:
238  return new AlphaStreamsBrokerageModel(accountType);
239 
240  case BrokerageName.Zerodha:
241  return new ZerodhaBrokerageModel(accountType);
242 
243  case BrokerageName.Axos:
244  return new AxosClearingBrokerageModel(accountType);
245 
246  case BrokerageName.TradingTechnologies:
247  return new TradingTechnologiesBrokerageModel(accountType);
248 
249  case BrokerageName.Samco:
250  return new SamcoBrokerageModel(accountType);
251 
252  case BrokerageName.Kraken:
253  return new KrakenBrokerageModel(accountType);
254 
255  case BrokerageName.Exante:
256  return new ExanteBrokerageModel(accountType);
257 
258  case BrokerageName.FTX:
259  return new FTXBrokerageModel(accountType);
260 
261  case BrokerageName.FTXUS:
262  return new FTXUSBrokerageModel(accountType);
263 
264  case BrokerageName.Wolverine:
265  return new WolverineBrokerageModel(accountType);
266 
267  case BrokerageName.TDAmeritrade:
268  return new TDAmeritradeBrokerageModel(accountType);
269 
270  case BrokerageName.RBI:
271  return new RBIBrokerageModel(accountType);
272 
273  case BrokerageName.Bybit:
274  return new BybitBrokerageModel(accountType);
275 
276  case BrokerageName.Eze:
277  return new EzeBrokerageModel(accountType);
278 
279  case BrokerageName.TradeStation:
280  return new TradeStationBrokerageModel(accountType);
281 
282  case BrokerageName.CharlesSchwab:
283  return new CharlesSchwabBrokerageModel(accountType);
284 
285  default:
286  throw new ArgumentOutOfRangeException(nameof(brokerage), brokerage, null);
287  }
288  }
289 
290 
291  /// <summary>
292  /// Gets the corresponding <see cref="BrokerageName"/> for the specified <see cref="IBrokerageModel"/>
293  /// </summary>
294  /// <param name="brokerageModel">The brokerage model</param>
295  /// <returns>The <see cref="BrokerageName"/> for the specified brokerage model</returns>
296  public static BrokerageName GetBrokerageName(IBrokerageModel brokerageModel)
297  {
298  var model = brokerageModel;
299  if (brokerageModel is BrokerageModelPythonWrapper)
300  {
301  model = (brokerageModel as BrokerageModelPythonWrapper).GetModel();
302  }
303 
304  // Case order matters to ensure we get the correct brokerage name from the inheritance chain
305  switch (model)
306  {
308  return BrokerageName.Alpaca;
309 
311  return BrokerageName.InteractiveBrokersBrokerage;
312 
313  case TradierBrokerageModel _:
314  return BrokerageName.TradierBrokerage;
315 
316  case OandaBrokerageModel _:
317  return BrokerageName.OandaBrokerage;
318 
319  case FxcmBrokerageModel _:
320  return BrokerageName.FxcmBrokerage;
321 
322  case BitfinexBrokerageModel _:
323  return BrokerageName.Bitfinex;
324 
326  return BrokerageName.BinanceUS;
327 
328  case BinanceBrokerageModel _:
329  return BrokerageName.Binance;
330 
331  case GDAXBrokerageModel _:
332  return BrokerageName.GDAX;
333 
334  case CoinbaseBrokerageModel _:
335  return BrokerageName.Coinbase;
336 
338  return BrokerageName.AlphaStreams;
339 
340  case ZerodhaBrokerageModel _:
341  return BrokerageName.Zerodha;
342 
344  return BrokerageName.Axos;
345 
347  return BrokerageName.TradingTechnologies;
348 
349  case SamcoBrokerageModel _:
350  return BrokerageName.Samco;
351 
352  case KrakenBrokerageModel _:
353  return BrokerageName.Kraken;
354 
355  case ExanteBrokerageModel _:
356  return BrokerageName.Exante;
357 
358  case FTXUSBrokerageModel _:
359  return BrokerageName.FTXUS;
360 
361  case FTXBrokerageModel _:
362  return BrokerageName.FTX;
363 
365  return BrokerageName.Wolverine;
366 
368  return BrokerageName.TDAmeritrade;
369 
370  case RBIBrokerageModel _:
371  return BrokerageName.RBI;
372 
373  case BybitBrokerageModel _:
374  return BrokerageName.Bybit;
375 
376  case EzeBrokerageModel _:
377  return BrokerageName.Eze;
378 
380  return BrokerageName.TradeStation;
381 
383  return BrokerageName.CharlesSchwab;
384 
385  case DefaultBrokerageModel _:
386  return BrokerageName.Default;
387 
388  default:
389  throw new ArgumentOutOfRangeException(nameof(brokerageModel), brokerageModel, null);
390  }
391  }
392  }
393 }