Lean  $LEAN_TAG$
TDAmeritradeBrokerageModel.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  /// TDAmeritrade
26  /// </summary>
28  {
29  /// <summary>
30  /// Array's TD Ameritrade supports security types
31  /// </summary>
32  private readonly HashSet<SecurityType> _supportSecurityTypes = new (new [] { SecurityType.Equity });
33 
34  /// <summary>
35  /// Array's TD Ameritrade supports order types
36  /// </summary>
37  private readonly HashSet<OrderType> _supportOrderTypes = new(new [] { OrderType.Market, OrderType.Limit, OrderType.StopMarket, OrderType.StopLimit });
38 
39  /// <summary>
40  /// Constructor for TDAmeritrade brokerage model
41  /// </summary>
42  /// <param name="accountType">Cash or Margin</param>
43  public TDAmeritradeBrokerageModel(AccountType accountType = AccountType.Margin) : base(accountType)
44  {
45 
46  }
47 
48  /// <summary>
49  /// Returns true if the brokerage could accept this order. This takes into account
50  /// order type, security type, and order size limits.
51  /// </summary>
52  /// <remarks>
53  /// For example, a brokerage may have no connectivity at certain times, or an order rate/size limit
54  /// </remarks>
55  /// <param name="security">The security of the order</param>
56  /// <param name="order">The order to be processed</param>
57  /// <param name="message">If this function returns false, a brokerage message detailing why the order may not be submitted</param>
58  /// <returns>True if the brokerage could process the order, false otherwise</returns>
59  public override bool CanSubmitOrder(Security security, Order order, out BrokerageMessageEvent message)
60  {
61  if (!IsValidOrderSize(security, order.Quantity, out message))
62  {
63  return false;
64  }
65 
66  message = null;
67 
68  if (!_supportSecurityTypes.Contains(security.Type))
69  {
70  message = new BrokerageMessageEvent(BrokerageMessageType.Warning, "NotSupported",
71  Messages.DefaultBrokerageModel.UnsupportedSecurityType(this, security));
72 
73  return false;
74  }
75 
76  if (!_supportOrderTypes.Contains(order.Type))
77  {
78  message = new BrokerageMessageEvent(BrokerageMessageType.Warning, "NotSupported",
79  Messages.DefaultBrokerageModel.UnsupportedOrderType(this, order, _supportOrderTypes));
80 
81  return false;
82  }
83 
84  return base.CanSubmitOrder(security, order, out message);
85  }
86 
87  /// <summary>
88  /// TDAmeritrade support Update Order
89  /// </summary>
90  /// <param name="security">Security</param>
91  /// <param name="order">Order that should be updated</param>
92  /// <param name="request">Update request</param>
93  /// <param name="message">Outgoing message</param>
94  /// <returns>True if the brokerage would allow updating the order, false otherwise</returns>
95  public override bool CanUpdateOrder(Security security, Order order, UpdateOrderRequest request, out BrokerageMessageEvent message)
96  {
97  message = null;
98  return true;
99  }
100 
101  /// <summary>
102  /// Provides TDAmeritrade fee model
103  /// </summary>
104  /// <param name="security">Security</param>
105  /// <returns>TDAmeritrade fee model</returns>
106  public override IFeeModel GetFeeModel(Security security)
107  {
108  return new TDAmeritradeFeeModel();
109  }
110  }
111 }