Lean  $LEAN_TAG$
AlpacaBrokerageModel.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 using QuantConnect.Orders;
19 using System.Collections.Generic;
20 
22 {
23  /// <summary>
24  /// Provides an implementation of the <see cref="DefaultBrokerageModel"/> specific to Alpaca brokerage.
25  /// </summary>
27  {
28  /// <summary>
29  /// A dictionary that maps each supported <see cref="SecurityType"/> to an array of <see cref="OrderType"/> supported by Alpaca brokerage.
30  /// </summary>
31  private readonly Dictionary<SecurityType, HashSet<OrderType>> _supportOrderTypeBySecurityType = new()
32  {
33  { SecurityType.Equity, new HashSet<OrderType> { OrderType.Market, OrderType.Limit, OrderType.StopMarket, OrderType.StopLimit,
34  OrderType.TrailingStop, OrderType.MarketOnOpen, OrderType.MarketOnClose } },
35  // Market and limit order types see https://docs.alpaca.markets/docs/options-trading-overview
36  { SecurityType.Option, new HashSet<OrderType> { OrderType.Market, OrderType.Limit } },
37  { SecurityType.Crypto, new HashSet<OrderType> { OrderType.Market, OrderType.Limit, OrderType.StopLimit }}
38  };
39 
40  /// <summary>
41  /// Initializes a new instance of the <see cref="AlpacaBrokerageModel"/> class
42  /// </summary>
43  /// <remarks>All Alpaca accounts are set up as margin accounts</remarks>
45  {
46  }
47 
48  /// <summary>
49  /// Gets a new fee model that represents this brokerage's fee structure
50  /// </summary>
51  /// <param name="security">The security to get a fee model for</param>
52  /// <returns>The new fee model for this brokerage</returns>
53  public override IFeeModel GetFeeModel(Security security)
54  {
55  return new AlpacaFeeModel();
56  }
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  public override bool CanSubmitOrder(Security security, Order order, out BrokerageMessageEvent message)
70  {
71  if (!_supportOrderTypeBySecurityType.TryGetValue(security.Type, out var supportOrderTypes))
72  {
73  message = new BrokerageMessageEvent(BrokerageMessageType.Warning, "NotSupported",
75  return false;
76  }
77 
78  if (!supportOrderTypes.Contains(order.Type))
79  {
80  message = new BrokerageMessageEvent(BrokerageMessageType.Warning, "NotSupported",
81  Messages.DefaultBrokerageModel.UnsupportedOrderType(this, order, supportOrderTypes));
82  return false;
83  }
84 
85  return base.CanSubmitOrder(security, order, out message);
86  }
87 
88  /// <summary>
89  /// Returns true if the brokerage would allow updating the order as specified by the request
90  /// </summary>
91  /// <param name="security">The security of the order</param>
92  /// <param name="order">The order to be updated</param>
93  /// <param name="request">The requested updated to be made to the order</param>
94  /// <param name="message">If this function returns false, a brokerage message detailing why the order may not be updated</param>
95  /// <returns>True if the brokerage would allow updating the order, false otherwise</returns>
96  public override bool CanUpdateOrder(Security security, Order order, UpdateOrderRequest request, out BrokerageMessageEvent message)
97  {
98  message = null;
99  return true;
100  }
101  }
102 }