Lean  $LEAN_TAG$
EzeBrokerageModel.cs
1 /*
2 * QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals.
3 * Lean Algorithmic Trading Engine v2.0. Copyright 2023 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 QuantConnect.Orders;
19 using System;
20 using System.Collections.Generic;
21 
23 {
24  /// <summary>
25  /// Provides Eze specific properties
26  /// </summary>
28  {
29  /// <summary>
30  /// Array's Eze supports security types
31  /// </summary>
32  private readonly HashSet<SecurityType> _supportSecurityTypes = new(
33  new[]
34  {
35  SecurityType.Equity,
36  SecurityType.Option,
37  SecurityType.Future,
38  SecurityType.FutureOption,
39  SecurityType.Index,
40  SecurityType.IndexOption
41  });
42 
43  /// <summary>
44  /// Array's Eze supports order types
45  /// </summary>
46  private readonly HashSet<OrderType> _supportOrderTypes = new(
47  new[]
48  {
49  OrderType.Market,
50  OrderType.Limit,
51  OrderType.StopMarket,
52  OrderType.StopLimit,
53  OrderType.MarketOnOpen,
54  OrderType.MarketOnClose,
55  });
56 
57  /// <summary>
58  /// Constructor for Eze brokerage model
59  /// </summary>
60  /// <param name="accountType">Cash or Margin</param>
61  public EzeBrokerageModel(AccountType accountType = AccountType.Margin) : base(accountType)
62  {
63  if (accountType == AccountType.Cash)
64  {
65  throw new NotSupportedException($"Eze brokerage can only be used with a {AccountType.Margin} account type");
66  }
67  }
68 
69  /// <summary>
70  /// Provides Eze fee model
71  /// </summary>
72  /// <param name="security">Security</param>
73  /// <returns>Eze Fee model</returns>
74  public override IFeeModel GetFeeModel(Security security)
75  {
76  return new EzeFeeModel();
77  }
78 
79  /// <summary>
80  /// Returns true if the brokerage could accept this order. This takes into account
81  /// order type, security type, and order size limits.
82  /// </summary>
83  /// <remarks>
84  /// For example, a brokerage may have no connectivity at certain times, or an order rate/size limit
85  /// </remarks>
86  /// <param name="security">The security of the order</param>
87  /// <param name="order">The order to be processed</param>
88  /// <param name="message">>If this function returns false, a brokerage message detailing why the order may not be submitted</param>
89  /// <returns>True if the brokerage could process the order, false otherwise</returns>
90  public override bool CanSubmitOrder(Security security, Order order, out BrokerageMessageEvent message)
91  {
92  if (!_supportSecurityTypes.Contains(security.Type))
93  {
94  message = new BrokerageMessageEvent(BrokerageMessageType.Warning, "NotSupported",
96 
97  return false;
98  }
99 
100  if (!_supportOrderTypes.Contains(order.Type))
101  {
102  message = new BrokerageMessageEvent(BrokerageMessageType.Warning, "NotSupported",
103  Messages.DefaultBrokerageModel.UnsupportedOrderType(this, order, _supportOrderTypes));
104 
105  return false;
106  }
107 
108  if (order.AbsoluteQuantity % 1 != 0)
109  {
110  message = new BrokerageMessageEvent(BrokerageMessageType.Warning, "NotSupported",
111  $"Order Quantity must be Integer, but provided {order.AbsoluteQuantity}.");
112 
113  return false;
114  }
115 
116  return base.CanSubmitOrder(security, order, out message);
117  }
118 
119  /// <summary>
120  /// Returns true if the brokerage could accept this order update. This takes into account
121  /// order type, security type, and order size limits.
122  /// </summary>
123  /// <param name="security">The security of the order</param>
124  /// <param name="order">The order to be updated</param>
125  /// <param name="request">The requested update to be made to the order</param>
126  /// <param name="message">If this function returns false, a brokerage message detailing why the order may not be updated</param>
127  /// <returns>True if the brokerage could update the order, false otherwise</returns>
128  /// <remarks>
129  /// The Eze supports update:
130  /// - quantity <see cref="Order.Quantity"/>
131  /// - LimitPrice <see cref="LimitOrder.LimitPrice"/>
132  /// - StopPrice <see cref="StopLimitOrder.StopPrice"/>
133  /// - OrderType <seealso cref="OrderType"/>
134  /// - Time In Force <see cref="Order.TimeInForce"/>
135  /// </remarks>
136  public override bool CanUpdateOrder(Security security, Order order, UpdateOrderRequest request, out BrokerageMessageEvent message)
137  {
138  message = null;
139  return true;
140  }
141  }
142 }