/* * QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals. * Lean Algorithmic Trading Engine v2.0. Copyright 2014 QuantConnect Corporation. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ using System; using System.Linq; using QuantConnect.Algorithm.Framework.Alphas; using QuantConnect.Algorithm.Framework.Alphas.Analysis; using QuantConnect.Algorithm.Framework.Alphas.Analysis.Providers; using QuantConnect.Algorithm.Framework.Execution; using QuantConnect.Algorithm.Framework.Portfolio; using QuantConnect.Algorithm.Framework.Risk; using QuantConnect.Algorithm.Framework.Selection; using QuantConnect.Data; using QuantConnect.Data.UniverseSelection; using QuantConnect.Securities; namespace QuantConnect.Algorithm.Framework { /// /// Algorithm framework base class that enforces a modular approach to algorithm development /// public partial class QCAlgorithmFramework : QCAlgorithm { private readonly ISecurityValuesProvider _securityValuesProvider; /// /// Returns true since algorithms derived from this use the framework /// public override bool IsFrameworkAlgorithm => true; /// /// Gets or sets the portfolio selection model. /// public IPortfolioSelectionModel PortfolioSelection { get; set; } /// /// Gets or sets the alpha model /// public IAlphaModel Alpha { get; set; } /// /// Gets or sets the portoflio construction model /// public IPortfolioConstructionModel PortfolioConstruction { get; set; } /// /// Gets or sets the execution model /// public IExecutionModel Execution { get; set; } /// /// Gets or sets the risk management model /// public IRiskManagementModel RiskManagement { get; set; } /// /// Initializes a new instance of the class /// public QCAlgorithmFramework() { _securityValuesProvider = new AlgorithmSecurityValuesProvider(this); // set model defaults Execution = new ImmediateExecutionModel(); RiskManagement = new NullRiskManagementModel(); } /// /// Called by setup handlers after Initialize and allows the algorithm a chance to organize /// the data gather in the Initialize method /// public override void PostInitialize() { CheckModels(); foreach (var universe in PortfolioSelection.CreateUniverses(this)) { AddUniverse(universe); } base.PostInitialize(); } /// /// Used to send data updates to algorithm framework models /// /// The current data slice 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); } /// /// Used to send security changes to algorithm framework models /// /// Security additions/removals for this time step public sealed override void OnFrameworkSecuritiesChanged(SecurityChanges changes) { Alpha.OnSecuritiesChanged(this, changes); PortfolioConstruction.OnSecuritiesChanged(this, changes); Execution.OnSecuritiesChanged(this, changes); RiskManagement.OnSecuritiesChanged(this, changes); } /// /// Sets the portfolio selection model /// /// Model defining universes for the algorithm public void SetPortfolioSelection(IPortfolioSelectionModel portfolioSelection) { PortfolioSelection = portfolioSelection; } /// /// Sets the alpha model /// /// Model that generates alpha public void SetAlpha(IAlphaModel alpha) { Alpha = alpha; } /// /// Sets the portfolio construction model /// /// Model defining how to build a portoflio from insights public void SetPortfolioConstruction(IPortfolioConstructionModel portfolioConstruction) { PortfolioConstruction = portfolioConstruction; } /// /// Sets the execution model /// /// Model defining how to execute trades to reach a portfolio target public void SetExecution(IExecutionModel execution) { Execution = execution; } /// /// Sets the risk management model /// /// Model defining public void SetRiskManagement(IRiskManagementModel riskManagement) { RiskManagement = riskManagement; } private Insight SetGeneratedAndClosedTimes(Insight insight) { insight.GeneratedTimeUtc = UtcTime; insight.ReferenceValue = _securityValuesProvider.GetValues(insight.Symbol).Get(insight.Type); TimeSpan barSize; Security security; SecurityExchangeHours exchangeHours; if (Securities.TryGetValue(insight.Symbol, out security)) { exchangeHours = security.Exchange.Hours; barSize = security.Resolution.ToTimeSpan(); } else { barSize = insight.Period.ToHigherResolutionEquivalent(false).ToTimeSpan(); exchangeHours = MarketHoursDatabase.GetExchangeHours(insight.Symbol.ID.Market, insight.Symbol, insight.Symbol.SecurityType); } var localStart = UtcTime.ConvertFromUtc(exchangeHours.TimeZone); barSize = QuantConnect.Time.Max(barSize, QuantConnect.Time.OneMinute); var barCount = (int) (insight.Period.Ticks / barSize.Ticks); insight.CloseTimeUtc = QuantConnect.Time.GetEndTimeForTradeBars(exchangeHours, localStart, barSize, barCount, false).ConvertToUtc(exchangeHours.TimeZone); return insight; } private void CheckModels() { if (PortfolioSelection == null) { throw new Exception("Framework algorithms must specify a portfolio selection model using the 'PortfolioSelection' property."); } if (Alpha == null) { throw new Exception("Framework algorithms must specify a alpha model using the 'Alpha' property."); } if (PortfolioConstruction == null) { throw new Exception("Framework algorithms must specify a portfolio construction model using the 'PortfolioConstruction' property"); } if (Execution == null) { throw new Exception("Framework algorithms must specify an execution model using the 'Execution' property."); } if (RiskManagement == null) { throw new Exception("Framework algorithms must specify an risk management model using the 'RiskManagement' property."); } } } }