Lean  $LEAN_TAG$
TradingTechnologiesBrokerageModel.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;
17 using System.Collections.Generic;
18 using System.Linq;
20 using QuantConnect.Orders;
24 using QuantConnect.Util;
25 
27 {
28  /// <summary>
29  /// Provides properties specific to Trading Technologies
30  /// </summary>
32  {
33  /// <summary>
34  /// The default markets for Trading Technologies
35  /// </summary>
36  public new static readonly IReadOnlyDictionary<SecurityType, string> DefaultMarketMap = new Dictionary<SecurityType, string>
37  {
38  {SecurityType.Future, Market.CME}
39  }.ToReadOnlyDictionary();
40 
41  private readonly Type[] _supportedTimeInForces =
42  {
44  typeof(DayTimeInForce)
45  };
46 
47  private readonly HashSet<OrderType> _supportedOrderTypes = new()
48  {
49  OrderType.Limit,
50  OrderType.Market,
51  OrderType.StopMarket,
52  OrderType.StopLimit
53  };
54 
55  /// <summary>
56  /// Initializes a new instance of the <see cref="TradingTechnologiesBrokerageModel"/> class
57  /// </summary>
58  /// <param name="accountType">The type of account to be modelled, defaults to
59  /// <see cref="AccountType.Margin"/></param>
61  : base(accountType)
62  {
63  }
64 
65  /// <summary>
66  /// Gets a map of the default markets to be used for each security type
67  /// </summary>
68  public override IReadOnlyDictionary<SecurityType, string> DefaultMarkets => DefaultMarketMap;
69 
70  /// <summary>
71  /// Get the benchmark for this model
72  /// </summary>
73  /// <param name="securities">SecurityService to create the security with if needed</param>
74  /// <returns>The benchmark for this brokerage</returns>
75  public override IBenchmark GetBenchmark(SecurityManager securities)
76  {
77  // Equivalent to no benchmark
78  return new FuncBenchmark(x => 0);
79  }
80 
81  /// <summary>
82  /// Gets a new fee model that represents this brokerage's fee structure
83  /// </summary>
84  /// <param name="security">The security to get a fee model for</param>
85  /// <returns>The new fee model for this brokerage</returns>
86  public override IFeeModel GetFeeModel(Security security)
87  {
88  return new ConstantFeeModel(0);
89  }
90 
91  /// <summary>
92  /// Returns true if the brokerage could accept this order. This takes into account
93  /// order type, security type, and order size limits.
94  /// </summary>
95  /// <remarks>
96  /// For example, a brokerage may have no connectivity at certain times, or an order rate/size limit
97  /// </remarks>
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 (security.Type != SecurityType.Future)
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 time in force
125  if (!_supportedTimeInForces.Contains(order.TimeInForce.GetType()))
126  {
127  message = new BrokerageMessageEvent(BrokerageMessageType.Warning, "NotSupported",
128  Messages.DefaultBrokerageModel.UnsupportedTimeInForce(this, order));
129 
130  return false;
131  }
132 
133  // validate stop orders prices
134  var stopMarket = order as StopMarketOrder;
135  if (stopMarket != null)
136  {
137  return IsValidOrderPrices(security, OrderType.StopMarket, stopMarket.Direction, stopMarket.StopPrice, security.Price, ref message);
138  }
139 
140  var stopLimit = order as StopLimitOrder;
141  if (stopLimit != null)
142  {
143  return IsValidOrderPrices(security, OrderType.StopMarket, stopLimit.Direction, stopLimit.StopPrice, stopLimit.LimitPrice, ref message);
144  }
145 
146  return true;
147  }
148 
149  /// <summary>
150  /// Returns true if the brokerage would allow updating the order as specified by the request
151  /// </summary>
152  /// <param name="security">The security of the order</param>
153  /// <param name="order">The order to be updated</param>
154  /// <param name="request">The requested update to be made to the order</param>
155  /// <param name="message">If this function returns false, a brokerage message detailing why the order may not be updated</param>
156  /// <returns>True if the brokerage would allow updating the order, false otherwise</returns>
157  public override bool CanUpdateOrder(Security security, Order order, UpdateOrderRequest request, out BrokerageMessageEvent message)
158  {
159  message = null;
160 
161  return true;
162  }
163 
164  /// <summary>
165  /// Returns true if the brokerage would be able to execute this order at this time assuming
166  /// market prices are sufficient for the fill to take place. This is used to emulate the
167  /// brokerage fills in backtesting and paper trading. For example some brokerages may not perform
168  /// executions during extended market hours. This is not intended to be checking whether or not
169  /// the exchange is open, that is handled in the Security.Exchange property.
170  /// </summary>
171  /// <param name="security"></param>
172  /// <param name="order">The order to test for execution</param>
173  /// <returns>True if the brokerage would be able to perform the execution, false otherwise</returns>
174  public override bool CanExecuteOrder(Security security, Order order)
175  {
176  return order.SecurityType == SecurityType.Future;
177  }
178 
179  /// <summary>
180  /// Validates stopmarket/stoplimit order prices, pass security.Price for limit/stop if n/a
181  /// </summary>
182  private static bool IsValidOrderPrices(
183  Security security,
184  OrderType orderType,
185  OrderDirection orderDirection,
186  decimal stopPrice,
187  decimal limitPrice,
188  ref BrokerageMessageEvent message
189  )
190  {
191  // validate stop market order prices
192  if (orderType == OrderType.StopMarket &&
193  (orderDirection == OrderDirection.Buy && stopPrice <= security.Price ||
194  orderDirection == OrderDirection.Sell && stopPrice >= security.Price))
195  {
196  message = new BrokerageMessageEvent(BrokerageMessageType.Warning, "NotSupported",
197  Messages.TradingTechnologiesBrokerageModel.InvalidStopMarketOrderPrice);
198 
199  return false;
200  }
201 
202  // validate stop limit order prices
203  if (orderType == OrderType.StopLimit)
204  {
205  if (orderDirection == OrderDirection.Buy && stopPrice <= security.Price ||
206  orderDirection == OrderDirection.Sell && stopPrice >= security.Price)
207  {
208  message = new BrokerageMessageEvent(BrokerageMessageType.Warning, "NotSupported",
209  Messages.TradingTechnologiesBrokerageModel.InvalidStopLimitOrderPrice);
210 
211  return false;
212  }
213 
214  if (orderDirection == OrderDirection.Buy && limitPrice < stopPrice ||
215  orderDirection == OrderDirection.Sell && limitPrice > stopPrice)
216  {
217  message = new BrokerageMessageEvent(BrokerageMessageType.Warning, "NotSupported",
218  Messages.TradingTechnologiesBrokerageModel.InvalidStopLimitOrderLimitPrice);
219 
220  return false;
221  }
222  }
223 
224  return true;
225  }
226  }
227 }