/* * 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.ComponentModel.Composition; using QuantConnect.Configuration; using QuantConnect.Interfaces; using QuantConnect.Lean.Engine.Alpha; using QuantConnect.Lean.Engine.DataFeeds; using QuantConnect.Lean.Engine.RealTime; using QuantConnect.Lean.Engine.Results; using QuantConnect.Lean.Engine.Setup; using QuantConnect.Lean.Engine.TransactionHandlers; using QuantConnect.Logging; using QuantConnect.Util; namespace QuantConnect.Lean.Engine { /// /// Provides a container for the algorithm specific handlers /// public class LeanEngineAlgorithmHandlers : IDisposable { /// /// Gets the result handler used to communicate results from the algorithm /// public IResultHandler Results { get; } /// /// Gets the setup handler used to initialize the algorithm state /// public ISetupHandler Setup { get; } /// /// Gets the data feed handler used to provide data to the algorithm /// public IDataFeed DataFeed { get; } /// /// Gets the transaction handler used to process orders from the algorithm /// public ITransactionHandler Transactions { get; } /// /// Gets the real time handler used to process real time events /// public IRealTimeHandler RealTime { get; } /// /// Gets the map file provider used as a map file source for the data feed /// public IMapFileProvider MapFileProvider { get; } /// /// Gets the map file provider used as a map file source for the data feed /// public IFactorFileProvider FactorFileProvider { get; } /// /// Gets the data file provider used to retrieve security data if it is not on the file system /// public IDataProvider DataProvider { get; } /// /// Gets the alpha handler used to process algorithm generated insights /// public IAlphaHandler Alphas { get; } /// /// Python algorithm worker thread. /// public WorkerThread WorkerThread { get; } /// /// Initializes a new instance of the class from the specified handlers /// /// The result handler for communicating results from the algorithm /// The setup handler used to initialize algorithm state /// The data feed handler used to pump data to the algorithm /// The transaction handler used to process orders from the algorithm /// The real time handler used to process real time events /// The map file provider used to retrieve map files for the data feed /// Map file provider used as a map file source for the data feed /// file provider used to retrieve security data if it is not on the file system /// The alpha handler used to process generated insights public LeanEngineAlgorithmHandlers(IResultHandler results, ISetupHandler setup, IDataFeed dataFeed, ITransactionHandler transactions, IRealTimeHandler realTime, IMapFileProvider mapFileProvider, IFactorFileProvider factorFileProvider, IDataProvider dataProvider, IAlphaHandler alphas ) { if (results == null) { throw new ArgumentNullException(nameof(results)); } if (setup == null) { throw new ArgumentNullException(nameof(setup)); } if (dataFeed == null) { throw new ArgumentNullException(nameof(dataFeed)); } if (transactions == null) { throw new ArgumentNullException(nameof(transactions)); } if (realTime == null) { throw new ArgumentNullException(nameof(realTime)); } if (mapFileProvider == null) { throw new ArgumentNullException(nameof(mapFileProvider)); } if (factorFileProvider == null) { throw new ArgumentNullException(nameof(factorFileProvider)); } if (dataProvider == null) { throw new ArgumentNullException(nameof(dataProvider)); } if (alphas == null) { throw new ArgumentNullException(nameof(alphas)); } Results = results; Setup = setup; DataFeed = dataFeed; Transactions = transactions; RealTime = realTime; MapFileProvider = mapFileProvider; FactorFileProvider = factorFileProvider; DataProvider = dataProvider; Alphas = alphas; //Worker thread for python instances: WorkerThread = new WorkerThread(); Setup.WorkerThread = WorkerThread; } /// /// Creates a new instance of the class from the specified composer using type names from configuration /// /// The composer instance to obtain implementations from /// A fully hydrates instance. /// Throws a CompositionException during failure to load public static LeanEngineAlgorithmHandlers FromConfiguration(Composer composer) { var setupHandlerTypeName = Config.Get("setup-handler", "ConsoleSetupHandler"); var transactionHandlerTypeName = Config.Get("transaction-handler", "BacktestingTransactionHandler"); var realTimeHandlerTypeName = Config.Get("real-time-handler", "BacktestingRealTimeHandler"); var dataFeedHandlerTypeName = Config.Get("data-feed-handler", "FileSystemDataFeed"); var resultHandlerTypeName = Config.Get("result-handler", "BacktestingResultHandler"); var mapFileProviderTypeName = Config.Get("map-file-provider", "LocalDiskMapFileProvider"); var factorFileProviderTypeName = Config.Get("factor-file-provider", "LocalDiskFactorFileProvider"); var dataProviderTypeName = Config.Get("data-provider", "DefaultDataProvider"); var alphaHandlerTypeName = Config.Get("alpha-handler", "DefaultAlphaHandler"); return new LeanEngineAlgorithmHandlers( composer.GetExportedValueByTypeName(resultHandlerTypeName), composer.GetExportedValueByTypeName(setupHandlerTypeName), composer.GetExportedValueByTypeName(dataFeedHandlerTypeName), composer.GetExportedValueByTypeName(transactionHandlerTypeName), composer.GetExportedValueByTypeName(realTimeHandlerTypeName), composer.GetExportedValueByTypeName(mapFileProviderTypeName), composer.GetExportedValueByTypeName(factorFileProviderTypeName), composer.GetExportedValueByTypeName(dataProviderTypeName), composer.GetExportedValueByTypeName(alphaHandlerTypeName) ); } /// /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources. /// /// 2 public void Dispose() { Setup.Dispose(); Log.Trace("LeanEngineAlgorithmHandlers.Dispose(): Disposed of algorithm handlers."); } } }