Skip to content

Commit

Permalink
Fix bugs in unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Marinovsky committed Jan 2, 2024
1 parent b8231e7 commit b9dcf08
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 124 deletions.
27 changes: 9 additions & 18 deletions Engine/Setup/BrokerageSetupHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public class BrokerageSetupHandler : ISetupHandler
// saves ref to algo so we can call quit if runtime error encountered
private IBrokerageFactory _factory;
private IBrokerage _dataQueueHandlerBrokerage;
private HashSet<SecurityType> _supportedSecurityTypes = new()
protected virtual HashSet<SecurityType> SupportedSecurityTypes => new()
{
SecurityType.Equity,
SecurityType.Forex,
Expand Down Expand Up @@ -394,7 +394,7 @@ protected bool LoadExistingHoldingsAndOrders(IBrokerage brokerage, IAlgorithm al
Log.Trace("BrokerageSetupHandler.Setup(): Fetching open orders from brokerage...");
try
{
GetOpenOrders(algorithm, parameters.ResultHandler, parameters.TransactionHandler, brokerage, _supportedSecurityTypes);
GetOpenOrders(algorithm, parameters.ResultHandler, parameters.TransactionHandler, brokerage);
}
catch (Exception err)
{
Expand All @@ -418,12 +418,9 @@ protected bool LoadExistingHoldingsAndOrders(IBrokerage brokerage, IAlgorithm al

// verify existing holding security type
Security security;
if (!algorithm.Securities.TryGetValue(holding.Symbol, out security))
if (!algorithm.Securities.TryGetValue(holding.Symbol, out security) && !AddUnrequestedSecurity(algorithm, holding.Symbol, holding.Type, out security))
{
if (!AddUnrequestedSecurity(algorithm, holding.Symbol, holding.Type, out security))
{
continue;
}
continue;
}

var exchangeTime = utcNow.ConvertFromUtc(security.Exchange.TimeZone);
Expand Down Expand Up @@ -466,11 +463,11 @@ private bool AddUnrequestedSecurity(IAlgorithm algorithm, Symbol symbol, Securit
{
if (!algorithm.Securities.TryGetValue(symbol, out security))
{
if (!_supportedSecurityTypes.Contains((SecurityType)securityType))
if (!SupportedSecurityTypes.Contains((SecurityType)securityType))
{
Log.Error("BrokerageSetupHandler.Setup(): Unsupported security type: " + securityType + "-" + symbol.Value);
AddInitializationError("Found unsupported security type in existing brokerage holdings: " + securityType + ". " +
"QuantConnect currently supports the following security types: " + string.Join(",", _supportedSecurityTypes));
"QuantConnect currently supports the following security types: " + string.Join(",", SupportedSecurityTypes));
security = null;
return false;
}
Expand Down Expand Up @@ -516,9 +513,7 @@ private bool AddUnrequestedSecurity(IAlgorithm algorithm, Symbol symbol, Securit
/// <param name="resultHandler">The configured result handler</param>
/// <param name="transactionHandler">The configurated transaction handler</param>
/// <param name="brokerage">Brokerage output instance</param>
/// <param name="supportedSecurityTypes">The list of supported security types</param>
protected void GetOpenOrders(IAlgorithm algorithm, IResultHandler resultHandler, ITransactionHandler transactionHandler, IBrokerage brokerage,
HashSet<SecurityType> supportedSecurityTypes)
protected void GetOpenOrders(IAlgorithm algorithm, IResultHandler resultHandler, ITransactionHandler transactionHandler, IBrokerage brokerage)
{
// populate the algorithm with the account's outstanding orders
var openOrders = brokerage.GetOpenOrders();
Expand All @@ -528,13 +523,9 @@ protected void GetOpenOrders(IAlgorithm algorithm, IResultHandler resultHandler,
{
// verify existing holding security type
Security security;
if (!algorithm.Securities.TryGetValue(order.Symbol, out security))
if (!algorithm.Securities.TryGetValue(order.Symbol, out security) && !AddUnrequestedSecurity(algorithm, order.Symbol, order.SecurityType, out security))
{
if (!AddUnrequestedSecurity(algorithm, order.Symbol, order.SecurityType, out security))
{
// keep aggregating these errors
continue;
}
continue;
}

transactionHandler.AddOpenOrder(order, algorithm);
Expand Down
Loading

0 comments on commit b9dcf08

Please sign in to comment.