From 14ea8aa1c5375c24a2d754b19c13e397abcfbec4 Mon Sep 17 00:00:00 2001 From: Alexandre Catarino Date: Thu, 12 Oct 2023 15:26:15 +0100 Subject: [PATCH] LEAN Exits Gracefully If The Algorithm Adds Invalid Symbol (#13) * LEAN Exits Gracefully If The Algorithm Adds Invalid Symbol If the algorithm adds an invalid symbol, e.g. GPBEUR, an exception is raised. However, LEAN doesn't exit because the operation is not done in the main thread, but stops getting data if there are valid symbols. We added a brokerage error message to exit LEAN. * Fixes Existing Unit Test API change from https://github.com/QuantConnect/Lean/pull/7493 --- .../OandaBrokerageHistoryProviderTests.cs | 2 +- QuantConnect.OandaBrokerage/OandaRestApiBase.cs | 15 ++++++++++++--- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/QuantConnect.OandaBrokerage.Tests/OandaBrokerageHistoryProviderTests.cs b/QuantConnect.OandaBrokerage.Tests/OandaBrokerageHistoryProviderTests.cs index 965bcc5..291f2c6 100644 --- a/QuantConnect.OandaBrokerage.Tests/OandaBrokerageHistoryProviderTests.cs +++ b/QuantConnect.OandaBrokerage.Tests/OandaBrokerageHistoryProviderTests.cs @@ -73,7 +73,7 @@ public void GetsHistory(Symbol symbol, Resolution resolution, TimeSpan period, b var historyProvider = new BrokerageHistoryProvider(); historyProvider.SetBrokerage(brokerage); - historyProvider.Initialize(new HistoryProviderInitializeParameters(null, null, null, null, null, null, null, false, new DataPermissionManager())); + historyProvider.Initialize(new HistoryProviderInitializeParameters(null, null, null, null, null, null, null, false, new DataPermissionManager(), null)); var now = DateTime.UtcNow; diff --git a/QuantConnect.OandaBrokerage/OandaRestApiBase.cs b/QuantConnect.OandaBrokerage/OandaRestApiBase.cs index 3971af0..b898e4a 100644 --- a/QuantConnect.OandaBrokerage/OandaRestApiBase.cs +++ b/QuantConnect.OandaBrokerage/OandaRestApiBase.cs @@ -419,9 +419,18 @@ private bool Refresh() /// The list of symbols to subscribe protected void SubscribeSymbols(IEnumerable symbolsToSubscribe) { - var instruments = symbolsToSubscribe - .Select(symbol => SymbolMapper.GetBrokerageSymbol(symbol)) - .ToList(); + var instruments = new List(); + + try + { + instruments.AddRange(symbolsToSubscribe + .Select(SymbolMapper.GetBrokerageSymbol)); + } + catch (Exception e) + { + // GetBrokerageSymbol will raise an exception if at least one invalid symbol, e.g. GPBEUR, is requested + OnMessage(new BrokerageMessageEvent(BrokerageMessageType.Error, -1, e.Message)); + } PricingConnectionHandler.EnableMonitoring(false);