diff --git a/QuantConnect.InteractiveBrokersBrokerage/InteractiveBrokersBrokerage.cs b/QuantConnect.InteractiveBrokersBrokerage/InteractiveBrokersBrokerage.cs index 39c51e5..15b29af 100644 --- a/QuantConnect.InteractiveBrokersBrokerage/InteractiveBrokersBrokerage.cs +++ b/QuantConnect.InteractiveBrokersBrokerage/InteractiveBrokersBrokerage.cs @@ -4447,8 +4447,16 @@ private void StartGatewayWeeklyRestartTask() { Log.Trace($"InteractiveBrokersBrokerage.StartGatewayWeeklyRestartTask(): triggering weekly restart manually"); - // stopping the gateway will make the IBAutomater emit the exit event, which will trigger the restart - _ibAutomater?.Stop(); + if (_ibAutomater.IsRunning()) + { + // stopping the gateway will make the IBAutomater emit the exit event, which will trigger the restart + _ibAutomater?.Stop(); + } + else + { + // if the gateway is not running, we start it + CheckIbAutomaterError(_ibAutomater.Start(false)); + } } else { @@ -4480,11 +4488,16 @@ private void OnIbAutomaterExited(object sender, ExitedEventArgs e) _stateManager.Reset(); StopGatewayRestartTask(); - // check if IBGateway was closed because of an IBAutomater error + if (_isDisposeCalled) + { + return; + } + + // check if IBGateway was closed because of an IBAutomater error, die if so var result = _ibAutomater.GetLastStartResult(); CheckIbAutomaterError(result, false); - if (!result.HasError && !_isDisposeCalled) + if (!result.HasError) { // IBGateway was closed by IBAutomater because the auto-restart token expired or it was closed manually (less likely) Log.Trace("InteractiveBrokersBrokerage.OnIbAutomaterExited(): IBGateway close detected, restarting IBAutomater..."); @@ -4519,6 +4532,10 @@ private void OnIbAutomaterExited(object sender, ExitedEventArgs e) } }, CancellationToken.None, TaskContinuationOptions.None, TaskScheduler.Default); } + else + { + OnMessage(new BrokerageMessageEvent(BrokerageMessageType.Error, "IBAutomaterError", result.ErrorMessage)); + } } private TimeSpan GetRestartDelay() @@ -4611,7 +4628,7 @@ public static DateTime ComputeNextWeeklyRestartTimeUtc(TimeSpan weeklyRestartUtc /// private DateTime GetNextWeeklyRestartTimeUtc(DateTime currentDate) { - return ComputeNextWeeklyRestartTimeUtc(_weeklyRestartUtcTime, currentDate); + return ComputeNextWeeklyRestartTimeUtc(_weeklyRestartUtcTime, currentDate); } private void CheckIbAutomaterError(StartResult result, bool throwException = true) diff --git a/QuantConnect.InteractiveBrokersBrokerage/QuantConnect.InteractiveBrokersBrokerage.csproj b/QuantConnect.InteractiveBrokersBrokerage/QuantConnect.InteractiveBrokersBrokerage.csproj index 5f5d6fe..dc58a50 100644 --- a/QuantConnect.InteractiveBrokersBrokerage/QuantConnect.InteractiveBrokersBrokerage.csproj +++ b/QuantConnect.InteractiveBrokersBrokerage/QuantConnect.InteractiveBrokersBrokerage.csproj @@ -31,13 +31,13 @@ - - - analyzers;build - + + + analyzers;build +