public void UpdateCallsDisposeOnDisposableInstances() { var disposable = new Disposable(Symbols.SPY); var dictionary = new Dictionary <Symbol, Disposable> { [Symbols.SPY] = disposable }; var SPY = new Equity( Symbols.SPY, SecurityExchangeHours.AlwaysOpen(TimeZones.NewYork), new Cash("USD", 1m, 1m), SymbolProperties.GetDefault("USD"), ErrorCurrencyConverter.Instance, new RegisteredSecurityDataTypesProvider(), new SecurityCache() ); var changes = SecurityChangesTests.RemovedNonInternal(SPY); NotifiedSecurityChanges.UpdateDictionary(dictionary, changes, security => security.Symbol, security => new Disposable(security.Symbol) ); Assert.IsTrue(disposable.Disposed); }
public void DelistedSecurityEmitsFlatTargetWithNewInsights(Language language, InsightDirection direction) { SetPortfolioConstruction(language, _algorithm); var insights = new[] { GetInsight(Symbols.SPY, InsightDirection.Down, _algorithm.UtcTime) }; var targets = _algorithm.PortfolioConstruction.CreateTargets(_algorithm, insights).ToList(); Assert.AreEqual(1, targets.Count); // Removing SPY should clear the key in the insight collection var changes = SecurityChangesTests.RemovedNonInternal(_algorithm.Securities[Symbols.SPY]); _algorithm.PortfolioConstruction.OnSecuritiesChanged(_algorithm, changes); var amount = _algorithm.Portfolio.TotalPortfolioValue * (decimal)Confidence; var expectedTargets = _algorithm.Securities.Select(x => { // Expected target quantity for SPY is zero, since it will be removed var quantity = x.Key.Value == "SPY" ? 0 : (int)direction * Math.Floor(amount * (1 - _algorithm.Settings.FreePortfolioValuePercentage) / x.Value.Price); return(new PortfolioTarget(x.Key, quantity)); }); // Do no include SPY in the insights insights = _algorithm.Securities.Keys.Where(x => x.Value != "SPY") .Select(x => GetInsight(x, direction, _algorithm.UtcTime)).ToArray(); // Create target from an empty insights array var actualTargets = _algorithm.PortfolioConstruction.CreateTargets(_algorithm, insights); AssertTargets(expectedTargets, actualTargets); }
public override void DelistedSecurityEmitsFlatTargetWithNewInsights(Language language, InsightDirection direction) { SetPortfolioConstruction(language); var insights = new[] { GetInsight(Symbols.SPY, InsightDirection.Down, Algorithm.UtcTime) }; var targets = Algorithm.PortfolioConstruction.CreateTargets(Algorithm, insights).ToList(); Assert.AreEqual(1, targets.Count); // Removing SPY should clear the key in the insight collection var changes = SecurityChangesTests.RemovedNonInternal(Algorithm.Securities[Symbols.SPY]); Algorithm.PortfolioConstruction.OnSecuritiesChanged(Algorithm, changes); // Since we have 3 B, 2 T and 1 X, each security in each sector will get // B => .166%, T => .25, X => 0% (removed) var sectorCount = 2; var groupedBySector = Algorithm.Securities .Where(pair => pair.Value.Fundamentals?.CompanyReference?.IndustryTemplateCode != null) .GroupBy(pair => pair.Value.Fundamentals.CompanyReference.IndustryTemplateCode); var expectedTargets = new List <PortfolioTarget>(); foreach (var securities in groupedBySector) { var list = securities.ToList(); var amount = Algorithm.Portfolio.TotalPortfolioValue / list.Count / sectorCount * (1 - Algorithm.Settings.FreePortfolioValuePercentage); expectedTargets.AddRange(list .Select(x => new PortfolioTarget(x.Key, x.Key.Value == "SPY" ? 0 : (int)direction * Math.Floor(amount / x.Value.Price)))); } // Do no include SPY in the insights insights = Algorithm.Securities.Keys.Where(x => x.Value != "SPY") .Select(x => GetInsight(x, direction, Algorithm.UtcTime)).ToArray(); // Create target from an empty insights array var actualTargets = Algorithm.PortfolioConstruction.CreateTargets(Algorithm, insights); Assert.Greater(Algorithm.Securities.Count, expectedTargets.Count); AssertTargets(expectedTargets, actualTargets); }
public override void DelistedSecurityEmitsFlatTargetWithNewInsights(Language language, InsightDirection direction) { SetPortfolioConstruction(language); if (PortfolioBias != PortfolioBias.LongShort && (int)direction != (int)PortfolioBias) { direction = InsightDirection.Flat; } var insights = new[] { GetInsight(Symbols.SPY, InsightDirection.Down, Algorithm.UtcTime) }; var targets = Algorithm.PortfolioConstruction.CreateTargets(Algorithm, insights).ToList(); Assert.AreEqual(1, targets.Count); // Removing SPY should clear the key in the insight collection var changes = SecurityChangesTests.RemovedNonInternal(Algorithm.Securities[Symbols.SPY]); Algorithm.PortfolioConstruction.OnSecuritiesChanged(Algorithm, changes); // Equity will be divided by all securities minus 1, since SPY is already invested and we want to remove it var amount = Algorithm.Portfolio.TotalPortfolioValue / (decimal)(1 / Weight - 1) * (1 - Algorithm.Settings.FreePortfolioValuePercentage); var expectedTargets = Algorithm.Securities.Select(x => { // Expected target quantity for SPY is zero, since it will be removed var quantity = x.Key.Value == "SPY" ? 0 : (int)direction * Math.Floor(amount / x.Value.Price); return(new PortfolioTarget(x.Key, quantity)); }); // Do no include SPY in the insights insights = Algorithm.Securities.Keys.Where(x => x.Value != "SPY") .Select(x => GetInsight(x, direction, Algorithm.UtcTime)).ToArray(); // Create target from an empty insights array var actualTargets = Algorithm.PortfolioConstruction.CreateTargets(Algorithm, insights); AssertTargets(expectedTargets, actualTargets); }
public void DelistedSecurityEmitsFlatTargetWithoutNewInsights(Language language) { SetPortfolioConstruction(language, _algorithm); var insights = new[] { GetInsight(Symbols.SPY, InsightDirection.Down, _algorithm.UtcTime) }; var targets = _algorithm.PortfolioConstruction.CreateTargets(_algorithm, insights).ToList(); Assert.AreEqual(1, targets.Count); var changes = SecurityChangesTests.RemovedNonInternal(_algorithm.Securities[Symbols.SPY]); _algorithm.PortfolioConstruction.OnSecuritiesChanged(_algorithm, changes); var expectedTargets = new List <IPortfolioTarget> { new PortfolioTarget(Symbols.SPY, 0) }; // Create target from an empty insights array var actualTargets = _algorithm.PortfolioConstruction.CreateTargets(_algorithm, new Insight[0]); AssertTargets(expectedTargets, actualTargets); }