/// <summary> /// Used to send data updates to algorithm framework models /// </summary> /// <param name="slice">The current data slice</param> public sealed override void OnFrameworkData(Slice slice) { // generate, timestamp and emit insights var insights = Alpha.Update(this, slice) .Select(SetGeneratedAndClosedTimes) .ToList(); // only fire insights generated event if we actually have insights if (insights.Count != 0) { // debug printing of generated insights if (DebugMode) { Log($"{Time}: ALPHA: {string.Join(" | ", insights.Select(i => i.ToString()).OrderBy(i => i))}"); } OnInsightsGenerated(insights); } // construct portfolio targets from insights var targets = PortfolioConstruction.CreateTargets(this, insights).ToList(); // set security targets w/ those generated via portfolio construction module foreach (var target in targets) { var security = Securities[target.Symbol]; security.Holdings.Target = target; } if (DebugMode) { // debug printing of generated targets if (targets.Any()) { Log($"{Time}: PORTFOLIO: {string.Join(" | ", targets.Select(t => t.ToString()).OrderBy(t => t))}"); } } var riskTargetOverrides = RiskManagement.ManageRisk(this).ToList(); // override security targets w/ those generated via risk management module foreach (var target in riskTargetOverrides) { var security = Securities[target.Symbol]; security.Holdings.Target = target; } if (DebugMode) { // debug printing of generated risk target overrides if (riskTargetOverrides.Any()) { Log($"{Time}: RISK: {string.Join(" | ", riskTargetOverrides.Select(t => t.ToString()).OrderBy(t => t))}"); } } // execute on the targets, overriding targets for symbols w/ risk targets Execution.Execute(this, riskTargetOverrides.Concat(targets).DistinctBy(pt => pt.Symbol)); }
/// <summary> /// Used to send data updates to algorithm framework models /// </summary> /// <param name="slice">The current data slice</param> public sealed override void OnFrameworkData(Slice slice) { // generate, timestamp and emit alphas var alphas = Alpha.Update(this, slice) .Select(alpha => { alpha.GeneratedTimeUtc = UtcTime; return(alpha); }) .ToList(); if (alphas.Count != 0) { // only fire alphas generated event if we actually have alphas OnAlphasGenerated(alphas); } // construct portfolio targets from alphas var targets = PortfolioConstruction.CreateTargets(this, alphas); // execute on the targets and manage risk Execution.Execute(this, targets); RiskManagement.ManageRisk(this); }
/// <summary> /// Used to send data updates to algorithm framework models /// </summary> /// <param name="slice">The current data slice</param> public sealed override void OnFrameworkData(Slice slice) { // generate, timestamp and emit insights var insights = Alpha.Update(this, slice) .Select(SetGeneratedAndClosedTimes) .ToList(); if (insights.Count != 0) { // only fire insights generated event if we actually have insights OnInsightsGenerated(insights); } // construct portfolio targets from insights var targets = PortfolioConstruction.CreateTargets(this, insights); // execute on the targets and manage risk Execution.Execute(this, targets); RiskManagement.ManageRisk(this); }
/// <summary> /// Used to send data updates to algorithm framework models /// </summary> /// <param name="slice">The current data slice</param> public sealed override void OnFrameworkData(Slice slice) { // generate, timestamp and emit insights var insights = Alpha.Update(this, slice) .Select(SetGeneratedAndClosedTimes) .ToList(); if (insights.Count != 0) { // only fire insights generated event if we actually have insights OnInsightsGenerated(insights); } // construct portfolio targets from insights var targets = PortfolioConstruction.CreateTargets(this, insights); var riskTargetOverrides = RiskManagement.ManageRisk(this); // execute on the targets, overriding targets for symbols w/ risk targets Execution.Execute(this, riskTargetOverrides.Concat(targets).DistinctBy(pt => pt.Symbol)); }
/// <summary> /// They different framework models will process the new provided insight. /// The <see cref="IPortfolioConstructionModel"/> will create targets, /// the <see cref="IRiskManagementModel"/> will adjust the targets /// and the <see cref="IExecutionModel"/> will execute the <see cref="IPortfolioTarget"/> /// </summary> /// <param name="insights">The insight to process</param> private void ProcessInsights(Insight[] insights) { // construct portfolio targets from insights var targetsEnumerable = PortfolioConstruction.CreateTargets(this, insights); // for performance only call 'ToArray' if not empty enumerable (which is static) var targets = targetsEnumerable == Enumerable.Empty <IPortfolioTarget>() ? new IPortfolioTarget[] {} : targetsEnumerable.ToArray(); // set security targets w/ those generated via portfolio construction module foreach (var target in targets) { var security = Securities[target.Symbol]; security.Holdings.Target = target; } if (DebugMode) { // debug printing of generated targets if (targets.Length > 0) { Log($"{Time}: PORTFOLIO: {string.Join(" | ", targets.Select(t => t.ToString()).OrderBy(t => t))}"); } } var riskTargetOverridesEnumerable = RiskManagement.ManageRisk(this, targets); // for performance only call 'ToArray' if not empty enumerable (which is static) var riskTargetOverrides = riskTargetOverridesEnumerable == Enumerable.Empty <IPortfolioTarget>() ? new IPortfolioTarget[] { } : riskTargetOverridesEnumerable.ToArray(); // override security targets w/ those generated via risk management module foreach (var target in riskTargetOverrides) { var security = Securities[target.Symbol]; security.Holdings.Target = target; } if (DebugMode) { // debug printing of generated risk target overrides if (riskTargetOverrides.Length > 0) { Log($"{Time}: RISK: {string.Join(" | ", riskTargetOverrides.Select(t => t.ToString()).OrderBy(t => t))}"); } } IPortfolioTarget[] riskAdjustedTargets; // for performance we check the length before if (riskTargetOverrides.Length != 0 || targets.Length != 0) { // execute on the targets, overriding targets for symbols w/ risk targets riskAdjustedTargets = riskTargetOverrides.Concat(targets).DistinctBy(pt => pt.Symbol).ToArray(); } else { riskAdjustedTargets = new IPortfolioTarget[] { }; } if (DebugMode) { // only log adjusted targets if we've performed an adjustment if (riskTargetOverrides.Length > 0) { Log($"{Time}: RISK ADJUSTED TARGETS: {string.Join(" | ", riskAdjustedTargets.Select(t => t.ToString()).OrderBy(t => t))}"); } } Execution.Execute(this, riskAdjustedTargets); }
/// <summary> /// They different framework models will process the new provided insight. /// The <see cref="IPortfolioConstructionModel"/> will create targets, /// the <see cref="IRiskManagementModel"/> will adjust the targets /// and the <see cref="IExecutionModel"/> will execute the <see cref="IPortfolioTarget"/> /// </summary> /// <param name="insights">The insight to process</param> protected void ProcessInsights(Insight[] insights) { // construct portfolio targets from insights var targetsEnumerable = PortfolioConstruction.CreateTargets(this, insights); // for performance only call 'ToArray' if not empty enumerable (which is static) var targets = targetsEnumerable == Enumerable.Empty <IPortfolioTarget>() ? new IPortfolioTarget[] {} : targetsEnumerable.ToArray(); // set security targets w/ those generated via portfolio construction module foreach (var target in targets) { var security = Securities[target.Symbol]; security.Holdings.Target = target; } if (DebugMode) { // debug printing of generated targets if (targets.Length > 0) { Log($"{Time}: PORTFOLIO: {string.Join(" | ", targets.Select(t => t.ToString()).OrderBy(t => t))}"); } } var riskTargetOverridesEnumerable = RiskManagement.ManageRisk(this, targets); // for performance only call 'ToArray' if not empty enumerable (which is static) var riskTargetOverrides = riskTargetOverridesEnumerable == Enumerable.Empty <IPortfolioTarget>() ? new IPortfolioTarget[] { } : riskTargetOverridesEnumerable.ToArray(); // override security targets w/ those generated via risk management module foreach (var target in riskTargetOverrides) { var security = Securities[target.Symbol]; security.Holdings.Target = target; } if (DebugMode) { // debug printing of generated risk target overrides if (riskTargetOverrides.Length > 0) { Log($"{Time}: RISK: {string.Join(" | ", riskTargetOverrides.Select(t => t.ToString()).OrderBy(t => t))}"); } } IPortfolioTarget[] riskAdjustedTargets; // for performance we check the length before if (riskTargetOverrides.Length != 0 || targets.Length != 0) { // execute on the targets, overriding targets for symbols w/ risk targets riskAdjustedTargets = riskTargetOverrides.Concat(targets).DistinctBy(pt => pt.Symbol).ToArray(); } else { riskAdjustedTargets = new IPortfolioTarget[] { }; } if (DebugMode) { // only log adjusted targets if we've performed an adjustment if (riskTargetOverrides.Length > 0) { Log($"{Time}: RISK ADJUSTED TARGETS: {string.Join(" | ", riskAdjustedTargets.Select(t => t.ToString()).OrderBy(t => t))}"); } } if (riskAdjustedTargets.Length > 0 && Execution.GetType() != typeof(NullExecutionModel) && BrokerageModel.AccountType == AccountType.Cash) { throw new InvalidOperationException($"Non null {nameof(IExecutionModel)} and {nameof(IPortfolioConstructionModel)} are currently unsuitable for Cash Modeled brokerages (e.g. GDAX) and may result in unexpected trades." + " To prevent possible user error we've restricted them to Margin trading. You can select margin account types with" + $" SetBrokerage( ... AccountType.Margin). Or please set them to {nameof(NullExecutionModel)}, {nameof(NullPortfolioConstructionModel)}"); } Execution.Execute(this, riskAdjustedTargets); }
/// <summary> /// Used to send data updates to algorithm framework models /// </summary> /// <param name="slice">The current data slice</param> public sealed override void OnFrameworkData(Slice slice) { if (UtcTime >= UniverseSelection.GetNextRefreshTimeUtc()) { var universes = UniverseSelection.CreateUniverses(this).ToDictionary(u => u.Configuration.Symbol); // remove deselected universes by symbol foreach (var ukvp in UniverseManager) { var universeSymbol = ukvp.Key; var qcUserDefined = UserDefinedUniverse.CreateSymbol(ukvp.Value.SecurityType, ukvp.Value.Market); if (universeSymbol.Equals(qcUserDefined)) { // prevent removal of qc algorithm created user defined universes continue; } Universe universe; if (!universes.TryGetValue(universeSymbol, out universe)) { if (ukvp.Value.DisposeRequested) { UniverseManager.Remove(universeSymbol); } // mark this universe as disposed to remove all child subscriptions ukvp.Value.Dispose(); } } // add newly selected universes foreach (var ukvp in universes) { // note: UniverseManager.Add uses TryAdd, so don't need to worry about duplicates here UniverseManager.Add(ukvp); } } // we only want to run universe selection if there's no data available in the slice if (!slice.HasData) { return; } // insight timestamping handled via InsightsGenerated event handler var insights = Alpha.Update(this, slice).ToArray(); // only fire insights generated event if we actually have insights if (insights.Length != 0) { // debug printing of generated insights if (DebugMode) { Log($"{Time}: ALPHA: {string.Join(" | ", insights.Select(i => i.ToString()).OrderBy(i => i))}"); } OnInsightsGenerated(insights); } // construct portfolio targets from insights var targets = PortfolioConstruction.CreateTargets(this, insights).ToArray(); // set security targets w/ those generated via portfolio construction module foreach (var target in targets) { var security = Securities[target.Symbol]; security.Holdings.Target = target; } if (DebugMode) { // debug printing of generated targets if (targets.Length > 0) { Log($"{Time}: PORTFOLIO: {string.Join(" | ", targets.Select(t => t.ToString()).OrderBy(t => t))}"); } } var riskTargetOverrides = RiskManagement.ManageRisk(this, targets).ToArray(); // override security targets w/ those generated via risk management module foreach (var target in riskTargetOverrides) { var security = Securities[target.Symbol]; security.Holdings.Target = target; } if (DebugMode) { // debug printing of generated risk target overrides if (riskTargetOverrides.Length > 0) { Log($"{Time}: RISK: {string.Join(" | ", riskTargetOverrides.Select(t => t.ToString()).OrderBy(t => t))}"); } } // execute on the targets, overriding targets for symbols w/ risk targets var riskAdjustedTargets = riskTargetOverrides.Concat(targets).DistinctBy(pt => pt.Symbol).ToArray(); if (DebugMode) { // only log adjusted targets if we've performed an adjustment if (riskTargetOverrides.Length > 0) { Log($"{Time}: RISK ADJUSTED TARGETS: {string.Join(" | ", riskAdjustedTargets.Select(t => t.ToString()).OrderBy(t => t))}"); } } Execution.Execute(this, riskAdjustedTargets); }