/// <summary> /// Gets the execution details matching the filter /// </summary> /// <returns>A list of executions matching the filter</returns> public List <IB.ExecDetailsEventArgs> GetExecutions(string symbol, IB.SecurityType?type, string exchange, DateTime?timeSince, IB.ActionSide?side) { var filter = new IB.ExecutionFilter { AcctCode = _account, ClientId = _clientID, Exchange = exchange, SecurityType = type ?? IB.SecurityType.Undefined, Symbol = symbol, Time = timeSince ?? DateTime.MinValue, Side = side ?? IB.ActionSide.Undefined }; var details = new List <IB.ExecDetailsEventArgs>(); using (var client = new IB.IBClient()) { client.Connect(_host, _port, IncrementClientID()); var manualResetEvent = new ManualResetEvent(false); int requestID = GetNextRequestID(); // define our event handlers EventHandler <IB.ExecutionDataEndEventArgs> clientOnExecutionDataEnd = (sender, args) => { if (args.RequestId == requestID) { manualResetEvent.Set(); } }; EventHandler <IB.ExecDetailsEventArgs> clientOnExecDetails = (sender, args) => { if (args.RequestId == requestID) { details.Add(args); } }; client.ExecDetails += clientOnExecDetails; client.ExecutionDataEnd += clientOnExecutionDataEnd; // no need to be fancy with request id since that's all this client does is 1 request client.RequestExecutions(requestID, filter); if (!manualResetEvent.WaitOne(5000)) { throw new TimeoutException("InteractiveBrokersBrokerage.GetExecutions(): Operation took longer than 1 second."); } // remove our event handlers client.ExecDetails -= clientOnExecDetails; client.ExecutionDataEnd -= clientOnExecutionDataEnd; } return(details); }
/// <summary> /// Gets the execution details matching the filter /// </summary> /// <returns>A list of executions matching the filter</returns> public List<IB.ExecDetailsEventArgs> GetExecutions(string symbol, IB.SecurityType? type, string exchange, DateTime? timeSince, IB.ActionSide? side) { var filter = new IB.ExecutionFilter { AcctCode = _account, ClientId = _clientID, Exchange = exchange, SecurityType = type ?? IB.SecurityType.Undefined, Symbol = symbol, Time = timeSince ?? DateTime.MinValue, Side = side ?? IB.ActionSide.Undefined }; var details = new List<IB.ExecDetailsEventArgs>(); using (var client = new IB.IBClient()) { client.Connect(_host, _port, IncrementClientID()); var manualResetEvent = new ManualResetEvent(false); int requestID = GetNextRequestID(); // define our event handlers EventHandler<IB.ExecutionDataEndEventArgs> clientOnExecutionDataEnd = (sender, args) => { if (args.RequestId == requestID) manualResetEvent.Set(); }; EventHandler<IB.ExecDetailsEventArgs> clientOnExecDetails = (sender, args) => { if (args.RequestId == requestID) details.Add(args); }; client.ExecDetails += clientOnExecDetails; client.ExecutionDataEnd += clientOnExecutionDataEnd; // no need to be fancy with request id since that's all this client does is 1 request client.RequestExecutions(requestID, filter); if (!manualResetEvent.WaitOne(5000)) { throw new TimeoutException("InteractiveBrokersBrokerage.GetExecutions(): Operation took longer than 1 second."); } // remove our event handlers client.ExecDetails -= clientOnExecDetails; client.ExecutionDataEnd -= clientOnExecutionDataEnd; } return details; }
/// <summary> /// Connects the client to the IB gateway /// </summary> public override void Connect() { if (IsConnected) { return; } // set up event handlers _client.UpdatePortfolio += HandlePortfolioUpdates; _client.OrderStatus += HandleOrderStatusUpdates; _client.UpdateAccountValue += HandleUpdateAccountValue; _client.Error += HandleError; // we need to wait until we receive the next valid id from the server var manualResetEvent = new ManualResetEvent(false); _client.NextValidId += (sender, e) => { // only grab this id when we initialize, and we'll manually increment it here to avoid threading issues if (_nextValidID == 0) { _nextValidID = e.OrderId; manualResetEvent.Set(); } Log.Trace("InteractiveBrokersBrokerage.HandleNextValidID(): " + e.OrderId); }; int attempt = 1; while (true) { try { Log.Trace("InteractiveBrokersBrokerage.Connect(): Attempting to connect (" + attempt + "/10) ..."); // we're going to try and connect several times, if successful break _client.Connect(_host, _port, _clientID); break; } catch (Exception err) { // max out at 10 attempts to connect if (attempt++ < 10) { Thread.Sleep(5000); continue; } // we couldn't connect after several attempts, log the error and throw an exception Log.Error("InteractiveBrokersBrokerage.Connect(): " + err.Message); throw; } } // pause for a moment to receive next valid ID message from gateway if (!manualResetEvent.WaitOne(1000)) { // we timed out our wait for next valid id... we'll just log it Log.Error("InteractiveBrokersBrokerage.Connect(): Timed out waiting for next valid ID event to fire."); } _client.RequestAccountUpdates(true, _account); }