Skip to content

Commit

Permalink
Improve unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Marinovsky committed Jan 2, 2024
1 parent e7fb0ef commit b8231e7
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 10 deletions.
17 changes: 10 additions & 7 deletions Engine/Setup/BrokerageSetupHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,7 @@ private bool LoadCashBalance(IBrokerage brokerage, IAlgorithm algorithm)
return true;
}

private bool LoadExistingHoldingsAndOrders(IBrokerage brokerage, IAlgorithm algorithm, SetupHandlerParameters parameters)
protected bool LoadExistingHoldingsAndOrders(IBrokerage brokerage, IAlgorithm algorithm, SetupHandlerParameters parameters)
{
Log.Trace("BrokerageSetupHandler.Setup(): Fetching open orders from brokerage...");
try
Expand Down Expand Up @@ -417,9 +417,10 @@ private bool LoadExistingHoldingsAndOrders(IBrokerage brokerage, IAlgorithm algo
Log.Trace("BrokerageSetupHandler.Setup(): Has existing holding: " + holding);

// verify existing holding security type
if (!algorithm.Securities.TryGetValue(holding.Symbol, out var security))
Security security;
if (!algorithm.Securities.TryGetValue(holding.Symbol, out security))
{
if (!AddUnrequestedSecurity(algorithm, holding.Symbol, holding.Type))
if (!AddUnrequestedSecurity(algorithm, holding.Symbol, holding.Type, out security))
{
continue;
}
Expand Down Expand Up @@ -461,15 +462,16 @@ private bool LoadExistingHoldingsAndOrders(IBrokerage brokerage, IAlgorithm algo
return true;
}

private bool AddUnrequestedSecurity(IAlgorithm algorithm, Symbol symbol, SecurityType securityType)
private bool AddUnrequestedSecurity(IAlgorithm algorithm, Symbol symbol, SecurityType securityType, out Security security)
{
if (!algorithm.Securities.TryGetValue(symbol, out Security security))
if (!algorithm.Securities.TryGetValue(symbol, out security))
{
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));
security = null;
return false;
}

Expand Down Expand Up @@ -525,9 +527,10 @@ protected void GetOpenOrders(IAlgorithm algorithm, IResultHandler resultHandler,
foreach (var order in openOrders.OrderByDescending(x => x.SecurityType))
{
// verify existing holding security type
if (!algorithm.Securities.TryGetValue(order.Symbol, out var security))
Security security;
if (!algorithm.Securities.TryGetValue(order.Symbol, out security))
{
if (!AddUnrequestedSecurity(algorithm, order.Symbol, order.SecurityType))
if (!AddUnrequestedSecurity(algorithm, order.Symbol, order.SecurityType, out security))
{
// keep aggregating these errors
continue;
Expand Down
9 changes: 6 additions & 3 deletions Tests/Engine/Setup/BrokerageSetupHandlerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -631,9 +631,12 @@ public void HasErrorWithZeroTotalPortfolioValue(bool hasCashBalance, bool hasHol
new object[] {
new Func<List<Holding>>(() => new List<Holding> { new Holding { Symbol = new Symbol(
SecurityIdentifier.GenerateBase(typeof(CustomDataBitcoinAlgorithm.Bitcoin), "BTC", Market.USA, false), "BTC"), Quantity = 1 }}),
new Func<List<Order>>(() => new List<Order> { new LimitOrder(new Symbol(
SecurityIdentifier.GenerateBase(typeof(CustomDataBitcoinAlgorithm.Bitcoin), "BTC", Market.USA, false), "BTC"), 1, 1, DateTime.UtcNow)}),
true }
new Func<List<Order>>(() => new List<Order>()),
true },
new object[] {
new Func<List<Holding>>(() => new List<Holding> { new Holding { Symbol = Symbols.SPY, Quantity = 1 }}),
new Func<List<Order>>(() => new List<Order>()),
true }
};

private static TestCaseData[] GetExistingHoldingsAndOrdersTestCaseData()
Expand Down

0 comments on commit b8231e7

Please sign in to comment.