Skip to content

Commit

Permalink
Merge pull request #1004 from StefanoRaggi/ib-already-connected-fix
Browse files Browse the repository at this point in the history
Fix IB bug causing error "Already Connected"
  • Loading branch information
jaredbroad committed Jul 2, 2017
2 parents 118d9aa + 72ee177 commit a15787f
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 7 deletions.
11 changes: 4 additions & 7 deletions Brokerages/InteractiveBrokers/InteractiveBrokersBrokerage.cs
Expand Up @@ -577,8 +577,6 @@ public override void Connect()
/// </summary>
public override void Disconnect()
{
if (!IsConnected) return;

_client.ClientSocket.eDisconnect();
}

Expand Down Expand Up @@ -1008,7 +1006,6 @@ private void HandleError(object sender, IB.ErrorEventArgs e)
else if (errorCode == 1102 || errorCode == 1101)
{
// we've reconnected
_disconnected1100Fired = false;
OnMessage(BrokerageMessageEvent.Reconnected(errorMsg));

OnMessage(new BrokerageMessageEvent(brokerageMessageType, errorCode, errorMsg));
Expand Down Expand Up @@ -1047,8 +1044,10 @@ private void HandleError(object sender, IB.ErrorEventArgs e)
/// <summary>
/// Restarts the IB Gateway and restores the connection
/// </summary>
private void ResetGatewayConnection()
public void ResetGatewayConnection()
{
_disconnected1100Fired = false;

Log.Trace("InteractiveBrokersBrokerage.ResetGatewayConnection(): Disconnecting...");
Disconnect();

Expand Down Expand Up @@ -1105,8 +1104,6 @@ private void TryWaitForReconnect()
// reset time finished and we're still disconnected, restart IB client
Log.Trace("InteractiveBrokersBrokerage.TryWaitForReconnect(): Reset time finished and still disconnected. Restarting...");

_disconnected1100Fired = false;

// notify the BrokerageMessageHandler before the restart, so it can stop polling
OnMessage(BrokerageMessageEvent.Reconnected(string.Empty));

Expand Down Expand Up @@ -2532,7 +2529,7 @@ private static bool ExistingSessionDetected()
/// <summary>
/// Check if IB Gateway running, restart if not
/// </summary>
private void CheckIbGateway()
public void CheckIbGateway()
{
Log.Trace("InteractiveBrokersBrokerage.CheckIbGateway(): start");
if (!InteractiveBrokersGatewayRunner.IsRunning())
Expand Down
Expand Up @@ -130,6 +130,33 @@ public void ClientConnects()
Assert.IsTrue(ib.IsConnected);
}

[Test]
public void IsConnectedUpdatesCorrectly()
{
var ib = _interactiveBrokersBrokerage;
Assert.IsTrue(ib.IsConnected);

ib.Disconnect();
Assert.IsFalse(ib.IsConnected);

ib.Connect();
Assert.IsTrue(ib.IsConnected);
}

[Test]
public void IsConnectedAfterReset()
{
var ib = _interactiveBrokersBrokerage;
Assert.IsTrue(ib.IsConnected);

ib.ResetGatewayConnection();
Assert.IsTrue(InteractiveBrokersGatewayRunner.IsRunning());
Assert.IsTrue(ib.IsConnected);

ib.CheckIbGateway();
Assert.IsTrue(ib.IsConnected);
}

[Test]
public void PlacedOrderHasNewBrokerageOrderID()
{
Expand Down

0 comments on commit a15787f

Please sign in to comment.