Lean  $LEAN_TAG$
IBrokerage.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;
19 using QuantConnect.Data;
20 using QuantConnect.Orders;
22 
24 {
25  /// <summary>
26  /// Brokerage interface that defines the operations all brokerages must implement. The IBrokerage implementation
27  /// must have a matching IBrokerageFactory implementation.
28  /// </summary>
29  public interface IBrokerage : IBrokerageCashSynchronizer, IDisposable
30  {
31  /// <summary>
32  /// Event that fires each time the brokerage order id changes
33  /// </summary>
34  event EventHandler<BrokerageOrderIdChangedEvent> OrderIdChanged;
35 
36  /// Event that fires each time the status for a list of orders change
37  /// </summary>
38  event EventHandler<List<OrderEvent>> OrdersStatusChanged;
39 
40  /// <summary>
41  /// Event that fires each time an order is updated in the brokerage side
42  /// </summary>
43  /// <remarks>
44  /// These are not status changes but mainly price changes, like the stop price of a trailing stop order
45  /// </remarks>
46  event EventHandler<OrderUpdateEvent> OrderUpdated;
47 
48  /// <summary>
49  /// Event that fires each time a short option position is assigned
50  /// </summary>
51  event EventHandler<OrderEvent> OptionPositionAssigned;
52 
53  /// <summary>
54  /// Event that fires each time an option position has changed
55  /// </summary>
56  event EventHandler<OptionNotificationEventArgs> OptionNotification;
57 
58  /// <summary>
59  /// Event that fires each time there's a brokerage side generated order
60  /// </summary>
61  event EventHandler<NewBrokerageOrderNotificationEventArgs> NewBrokerageOrderNotification;
62 
63  /// <summary>
64  /// Event that fires each time a delisting occurs
65  /// </summary>
66  /// <remarks>TODO: Wire brokerages to call this event to process delistings</remarks>
67  event EventHandler<DelistingNotificationEventArgs> DelistingNotification;
68 
69  /// <summary>
70  /// Event that fires each time a user's brokerage account is changed
71  /// </summary>
72  event EventHandler<AccountEvent> AccountChanged;
73 
74  /// <summary>
75  /// Event that fires when a message is received from the brokerage
76  /// </summary>
77  event EventHandler<BrokerageMessageEvent> Message;
78 
79  /// <summary>
80  /// Gets the name of the brokerage
81  /// </summary>
82  string Name { get; }
83 
84  /// <summary>
85  /// Returns true if we're currently connected to the broker
86  /// </summary>
87  bool IsConnected { get; }
88 
89  /// <summary>
90  /// Gets all open orders on the account
91  /// </summary>
92  /// <returns>The open orders returned from IB</returns>
93  List<Order> GetOpenOrders();
94 
95  /// <summary>
96  /// Gets all holdings for the account
97  /// </summary>
98  /// <returns>The current holdings from the account</returns>
99  List<Holding> GetAccountHoldings();
100 
101  /// <summary>
102  /// Gets the current cash balance for each currency held in the brokerage account
103  /// </summary>
104  /// <returns>The current cash balance for each currency available for trading</returns>
105  List<CashAmount> GetCashBalance();
106 
107  /// <summary>
108  /// Places a new order and assigns a new broker ID to the order
109  /// </summary>
110  /// <param name="order">The order to be placed</param>
111  /// <returns>True if the request for a new order has been placed, false otherwise</returns>
112  bool PlaceOrder(Order order);
113 
114  /// <summary>
115  /// Updates the order with the same id
116  /// </summary>
117  /// <param name="order">The new order information</param>
118  /// <returns>True if the request was made for the order to be updated, false otherwise</returns>
119  bool UpdateOrder(Order order);
120 
121  /// <summary>
122  /// Cancels the order with the specified ID
123  /// </summary>
124  /// <param name="order">The order to cancel</param>
125  /// <returns>True if the request was made for the order to be canceled, false otherwise</returns>
126  bool CancelOrder(Order order);
127 
128  /// <summary>
129  /// Connects the client to the broker's remote servers
130  /// </summary>
131  void Connect();
132 
133  /// <summary>
134  /// Disconnects the client from the broker's remote servers
135  /// </summary>
136  void Disconnect();
137 
138  /// <summary>
139  /// Specifies whether the brokerage will instantly update account balances
140  /// </summary>
142 
143  /// <summary>
144  /// Returns the brokerage account's base currency
145  /// </summary>
146  string AccountBaseCurrency { get; }
147 
148  /// <summary>
149  /// Gets the history for the requested security
150  /// </summary>
151  /// <param name="request">The historical data request</param>
152  /// <returns>An enumerable of bars covering the span specified in the request</returns>
153  IEnumerable<BaseData> GetHistory(HistoryRequest request);
154  }
155 }