/* * 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.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.Util; namespace QuantConnect.Lean.Engine { /// /// Provides a container for the algorithm specific handlers /// public class LeanEngineAlgorithmHandlers : IDisposable { private readonly IDataFeed _dataFeed; private readonly ISetupHandler _setup; private readonly IResultHandler _results; private readonly IRealTimeHandler _realTime; private readonly ITransactionHandler _transactions; /// /// Gets the result handler used to communicate results from the algorithm /// public IResultHandler Results { get { return _results; } } /// /// Gets the setup handler used to initialize the algorithm state /// public ISetupHandler Setup { get { return _setup; } } /// /// Gets the data feed handler used to provide data to the algorithm /// public IDataFeed DataFeed { get { return _dataFeed; } } /// /// Gets the transaction handler used to process orders from the algorithm /// public ITransactionHandler Transactions { get { return _transactions; } } /// /// Gets the real time handler used to process real time events /// public IRealTimeHandler RealTime { get { return _realTime; } } /// /// 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 public LeanEngineAlgorithmHandlers(IResultHandler results, ISetupHandler setup, IDataFeed dataFeed, ITransactionHandler transactions, IRealTimeHandler realTime) { if (results == null) { throw new ArgumentNullException("results"); } if (setup == null) { throw new ArgumentNullException("setup"); } if (dataFeed == null) { throw new ArgumentNullException("dataFeed"); } if (transactions == null) { throw new ArgumentNullException("transactions"); } if (realTime == null) { throw new ArgumentNullException("realTime"); } _results = results; _setup = setup; _dataFeed = dataFeed; _transactions = transactions; _realTime = realTime; } /// /// 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", "ConsoleResultHandler"); return new LeanEngineAlgorithmHandlers( composer.GetExportedValueByTypeName(resultHandlerTypeName), composer.GetExportedValueByTypeName(setupHandlerTypeName), composer.GetExportedValueByTypeName(dataFeedHandlerTypeName), composer.GetExportedValueByTypeName(transactionHandlerTypeName), composer.GetExportedValueByTypeName(realTimeHandlerTypeName) ); } /// /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources. /// /// 2 public void Dispose() { Setup.Dispose(); } } }