Skip to content

Commit

Permalink
LEAN Exits Gracefully If The Algorithm Adds Invalid Symbol (#13)
Browse files Browse the repository at this point in the history
* 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 QuantConnect/Lean#7493
  • Loading branch information
AlexCatarino authored Oct 12, 2023
1 parent 2c22c61 commit 14ea8aa
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
15 changes: 12 additions & 3 deletions QuantConnect.OandaBrokerage/OandaRestApiBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -419,9 +419,18 @@ private bool Refresh()
/// <param name="symbolsToSubscribe">The list of symbols to subscribe</param>
protected void SubscribeSymbols(IEnumerable<Symbol> symbolsToSubscribe)
{
var instruments = symbolsToSubscribe
.Select(symbol => SymbolMapper.GetBrokerageSymbol(symbol))
.ToList();
var instruments = new List<string>();

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);

Expand Down

0 comments on commit 14ea8aa

Please sign in to comment.