IBAutomater uses auto-restarts instead of auto-logoff to allow users with 2FA to authenticate only once per week. You must handle two specific events to maintain connectivity:
Restarted: Triggered during a 'soft restart' (daily auto-restart). No authentication is required. You should use this event to reconnect your client API connection.Exited: Triggered when IB Gateway is closed because full authentication is required (e.g., weekly reset) or an error occurred. You are responsible for calling Start() to restart the gateway.
Note on Weekend Maintenance: During IB weekend maintenance (typically Friday 23:00 ET to Sunday), login attempts will fail. If your application runs through the weekend, handle the Exited event and delay restart attempts until Sunday at 4:00 PM ET.
private void OnIbAutomaterExited(object sender, ExitedEventArgs e)
{
var result = _ibAutomater.GetLastStartResult();
if (!result.HasError)
{
// IB Gateway was closed because auto-restart token expired
Thread.Sleep(TimeSpan.FromSeconds(10));
Disconnect();
_ibAutomater.Start(false);
Connect();
}
}
private void OnIbAutomaterRestarted(object sender, EventArgs e)
{
var result = _ibAutomater.GetLastStartResult();
if (!result.HasError)
{
// Soft restart detected, reconnect client API
Disconnect();
Connect();
}
}
private void CheckIbAutomaterError(StartResult result)
{
if (result.HasError)
{
Console.WriteLine($"{result.ErrorCode} - {result.ErrorMessage}");
}
}