Lean  $LEAN_TAG$
CharlesSchwabBrokerageModel.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 QuantConnect.Orders;
20 using System.Collections.Generic;
21 
23 {
24  /// <summary>
25  /// Represents a brokerage model specific to Charles Schwab.
26  /// </summary>
28  {
29  /// <summary>
30  /// HashSet containing the security types supported by TradeStation.
31  /// </summary>
32  private readonly HashSet<SecurityType> _supportSecurityTypes = new(
33  new[]
34  {
35  SecurityType.Equity,
36  SecurityType.Option,
37  SecurityType.IndexOption
38  });
39 
40  /// <summary>
41  /// HashSet containing the order types supported by the <see cref="CanSubmitOrder"/> operation in TradeStation.
42  /// </summary>
43  private readonly HashSet<OrderType> _supportOrderTypes = new(
44  new[]
45  {
46  OrderType.Market,
47  OrderType.Limit,
48  OrderType.StopMarket
49  });
50 
51  /// <summary>
52  /// Constructor for Charles Schwab brokerage model
53  /// </summary>
54  /// <param name="accountType">Cash or Margin</param>
56  : base(accountType)
57  {
58  }
59 
60  /// <summary>
61  /// Provides TradeStation fee model
62  /// </summary>
63  /// <param name="security">Security</param>
64  /// <returns>TradeStation fee model</returns>
65  public override IFeeModel GetFeeModel(Security security)
66  {
67  return new CharlesSchwabFeeModel();
68  }
69 
70  /// <summary>
71  /// Returns true if the brokerage could accept this order. This takes into account
72  /// order type, security type, and order size limits.
73  /// </summary>
74  /// <remarks>
75  /// For example, a brokerage may have no connectivity at certain times, or an order rate/size limit
76  /// </remarks>
77  /// <param name="security">The security of the order</param>
78  /// <param name="order">The order to be processed</param>
79  /// <param name="message">If this function returns false, a brokerage message detailing why the order may not be submitted</param>
80  /// <returns>True if the brokerage could process the order, false otherwise</returns>
81  public override bool CanSubmitOrder(Security security, Order order, out BrokerageMessageEvent message)
82  {
83  message = default;
84 
85  if (!_supportSecurityTypes.Contains(security.Type))
86  {
87  message = new BrokerageMessageEvent(BrokerageMessageType.Warning, "NotSupported",
89 
90  return false;
91  }
92 
93  if (!_supportOrderTypes.Contains(order.Type))
94  {
95  message = new BrokerageMessageEvent(BrokerageMessageType.Warning, "NotSupported", Messages.DefaultBrokerageModel.UnsupportedOrderType(this, order, _supportOrderTypes));
96  return false;
97  }
98 
99  return base.CanSubmitOrder(security, order, out message);
100  }
101  }
102 }