Lean  $LEAN_TAG$
IOrderProvider.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 QuantConnect.Orders;
19 
21 {
22  /// <summary>
23  /// Represents a type capable of fetching Order instances by its QC order id or by a brokerage id
24  /// </summary>
25  public interface IOrderProvider
26  {
27  /// <summary>
28  /// Gets the current number of orders that have been processed
29  /// </summary>
30  int OrdersCount { get; }
31 
32  /// <summary>
33  /// Get the order by its id
34  /// </summary>
35  /// <param name="orderId">Order id to fetch</param>
36  /// <returns>A clone of the order with the specified id, or null if no match is found</returns>
37  Order GetOrderById(int orderId);
38 
39  /// <summary>
40  /// Gets the Lean orders by its brokerage id
41  /// </summary>
42  /// <param name="brokerageId">The brokerage id to fetch</param>
43  /// <returns>The orders matching the brokerage id, or null if no match is found</returns>
44  List<Order> GetOrdersByBrokerageId(string brokerageId);
45 
46  /// <summary>
47  /// Gets and enumerable of <see cref="OrderTicket"/> matching the specified <paramref name="filter"/>
48  /// </summary>
49  /// <param name="filter">The filter predicate used to find the required order tickets. If null is specified then all tickets are returned</param>
50  /// <returns>An enumerable of <see cref="OrderTicket"/> matching the specified <paramref name="filter"/></returns>
51  IEnumerable<OrderTicket> GetOrderTickets(Func<OrderTicket, bool> filter = null);
52 
53  /// <summary>
54  /// Gets and enumerable of opened <see cref="OrderTicket"/> matching the specified <paramref name="filter"/>
55  /// </summary>
56  /// <param name="filter">The filter predicate used to find the required order tickets. If null is specified then all tickets are returned</param>
57  /// <returns>An enumerable of opened <see cref="OrderTicket"/> matching the specified <paramref name="filter"/></returns>
58  IEnumerable<OrderTicket> GetOpenOrderTickets(Func<OrderTicket, bool> filter = null);
59 
60  /// <summary>
61  /// Gets the order ticket for the specified order id. Returns null if not found
62  /// </summary>
63  /// <param name="orderId">The order's id</param>
64  /// <returns>The order ticket with the specified id, or null if not found</returns>
65  OrderTicket GetOrderTicket(int orderId);
66 
67  /// <summary>
68  /// Gets all orders matching the specified filter. Specifying null will return an enumerable
69  /// of all orders.
70  /// </summary>
71  /// <param name="filter">Delegate used to filter the orders</param>
72  /// <returns>All orders this order provider currently holds by the specified filter</returns>
73  IEnumerable<Order> GetOrders(Func<Order, bool> filter = null);
74 
75  /// <summary>
76  /// Gets open orders matching the specified filter. Specifying null will return an enumerable
77  /// of all open orders.
78  /// </summary>
79  /// <param name="filter">Delegate used to filter the orders</param>
80  /// <returns>All filtered open orders this order provider currently holds</returns>
81  List<Order> GetOpenOrders(Func<Order, bool> filter = null);
82  }
83 
84  /// <summary>
85  /// Provides extension methods for the <see cref="IOrderProvider"/> interface
86  /// </summary>
87  public static class OrderProviderExtensions
88  {
89  /// <summary>
90  /// Gets the order by its brokerage id
91  /// </summary>
92  /// <param name="orderProvider">The order provider to search</param>
93  /// <param name="brokerageId">The brokerage id to fetch</param>
94  /// <returns>The first order matching the brokerage id, or null if no match is found</returns>
95  public static List<Order> GetOrdersByBrokerageId(this IOrderProvider orderProvider, long brokerageId)
96  {
97  return orderProvider.GetOrdersByBrokerageId(brokerageId.ToStringInvariant());
98  }
99 
100  /// <summary>
101  /// Gets the order by its brokerage id
102  /// </summary>
103  /// <param name="orderProvider">The order provider to search</param>
104  /// <param name="brokerageId">The brokerage id to fetch</param>
105  /// <returns>The first order matching the brokerage id, or null if no match is found</returns>
106  public static List<Order> GetOrdersByBrokerageId(this IOrderProvider orderProvider, int brokerageId)
107  {
108  return orderProvider.GetOrdersByBrokerageId(brokerageId.ToStringInvariant());
109  }
110  }
111 }