Lean  $LEAN_TAG$
AxosClearingBrokerageModel.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 System.Linq;
17 using System.Collections.Generic;
21 using QuantConnect.Orders;
25 using QuantConnect.Util;
26 
28 {
29  /// <summary>
30  /// Provides the Axos clearing brokerage model specific properties
31  /// </summary>
33  {
34  private readonly HashSet<OrderType> _supportedOrderTypes = new()
35  {
36  OrderType.Limit,
37  OrderType.Market,
38  OrderType.MarketOnClose
39  };
40 
41  /// <summary>
42  /// The default markets for Trading Technologies
43  /// </summary>
44  public new static readonly IReadOnlyDictionary<SecurityType, string> DefaultMarketMap = new Dictionary<SecurityType, string>
45  {
46  {SecurityType.Equity, Market.USA}
47  }.ToReadOnlyDictionary();
48 
49  /// <summary>
50  /// Creates a new instance
51  /// </summary>
52  public AxosClearingBrokerageModel(AccountType accountType = AccountType.Margin) : base(accountType)
53  {
54  }
55 
56  /// <summary>
57  /// Gets a map of the default markets to be used for each security type
58  /// </summary>
59  public override IReadOnlyDictionary<SecurityType, string> DefaultMarkets => DefaultMarketMap;
60 
61  /// <summary>
62  /// Provides Axos fee model
63  /// </summary>
64  /// <param name="security">The security to get a fee model for</param>
65  /// <returns>The new fee model for this brokerage</returns>
66  public override IFeeModel GetFeeModel(Security security)
67  {
68  return new AxosFeeModel();
69  }
70 
71  /// <summary>
72  /// Gets the shortable provider
73  /// </summary>
74  /// <returns>Shortable provider</returns>
76  {
77  if(security.Type == SecurityType.Equity)
78  {
79  return new LocalDiskShortableProvider("axos");
80  }
81  return base.GetShortableProvider(security);
82  }
83 
84  /// <summary>
85  /// Get the benchmark for this model
86  /// </summary>
87  /// <param name="securities">SecurityService to create the security with if needed</param>
88  /// <returns>The benchmark for this brokerage</returns>
89  public override IBenchmark GetBenchmark(SecurityManager securities)
90  {
91  // Equivalent to no benchmark
92  return new FuncBenchmark(x => 0);
93  }
94 
95  /// <summary>
96  /// Returns true if the brokerage could accept this order.
97  /// </summary>
98  /// <param name="security">The security being ordered</param>
99  /// <param name="order">The order to be processed</param>
100  /// <param name="message">If this function returns false, a brokerage message detailing why the order may not be submitted</param>
101  /// <returns>True if the brokerage could process the order, false otherwise</returns>
102  public override bool CanSubmitOrder(Security security, Order order, out BrokerageMessageEvent message)
103  {
104  message = null;
105 
106  // validate security type
107  if (!DefaultMarketMap.ContainsKey(security.Type))
108  {
109  message = new BrokerageMessageEvent(BrokerageMessageType.Warning, "NotSupported",
110  Messages.DefaultBrokerageModel.UnsupportedSecurityType(this, security));
111 
112  return false;
113  }
114 
115  // validate order type
116  if (!_supportedOrderTypes.Contains(order.Type))
117  {
118  message = new BrokerageMessageEvent(BrokerageMessageType.Warning, "NotSupported",
119  Messages.DefaultBrokerageModel.UnsupportedOrderType(this, order, _supportedOrderTypes));
120 
121  return false;
122  }
123 
124  // validate orders quantity
125  if (order.AbsoluteQuantity % 1 != 0)
126  {
127  message = new BrokerageMessageEvent(BrokerageMessageType.Warning, "NotSupported",
128  Messages.AxosBrokerageModel.NonIntegerOrderQuantity(order));
129 
130  return false;
131  }
132 
133  return true;
134  }
135 
136  /// <summary>
137  /// Returns true if the brokerage would allow updating the order as specified by the request
138  /// </summary>
139  /// <param name="security">The security of the order</param>
140  /// <param name="order">The order to be updated</param>
141  /// <param name="request">The requested update to be made to the order</param>
142  /// <param name="message">If this function returns false, a brokerage message detailing why the order may not be updated</param>
143  /// <returns>True if the brokerage would allow updating the order, false otherwise</returns>
144  public override bool CanUpdateOrder(Security security, Order order, UpdateOrderRequest request, out BrokerageMessageEvent message)
145  {
146  message = null;
147 
148  return true;
149  }
150  }
151 }