forked from QuantConnect/Lean
-
Notifications
You must be signed in to change notification settings - Fork 0
/
LeanEngineAlgorithmHandlers.cs
226 lines (207 loc) · 10.1 KB
/
LeanEngineAlgorithmHandlers.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
/*
* 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
{
/// <summary>
/// Provides a container for the algorithm specific handlers
/// </summary>
public class LeanEngineAlgorithmHandlers : IDisposable
{
/// <summary>
/// Gets the result handler used to communicate results from the algorithm
/// </summary>
public IResultHandler Results { get; }
/// <summary>
/// Gets the setup handler used to initialize the algorithm state
/// </summary>
public ISetupHandler Setup { get; }
/// <summary>
/// Gets the data feed handler used to provide data to the algorithm
/// </summary>
public IDataFeed DataFeed { get; }
/// <summary>
/// Gets the transaction handler used to process orders from the algorithm
/// </summary>
public ITransactionHandler Transactions { get; }
/// <summary>
/// Gets the real time handler used to process real time events
/// </summary>
public IRealTimeHandler RealTime { get; }
/// <summary>
/// Gets the map file provider used as a map file source for the data feed
/// </summary>
public IMapFileProvider MapFileProvider { get; }
/// <summary>
/// Gets the map file provider used as a map file source for the data feed
/// </summary>
public IFactorFileProvider FactorFileProvider { get; }
/// <summary>
/// Gets the data file provider used to retrieve security data if it is not on the file system
/// </summary>
public IDataProvider DataProvider { get; }
/// <summary>
/// Gets the alpha handler used to process algorithm generated insights
/// </summary>
public IAlphaHandler Alphas { get; }
/// <summary>
/// Gets the object store used for persistence
/// </summary>
public IObjectStore ObjectStore { get; }
/// <summary>
/// Entity in charge of handling data permissions
/// </summary>
public IDataPermissionManager DataPermissionsManager { get; }
/// <summary>
/// Initializes a new instance of the <see cref="LeanEngineAlgorithmHandlers"/> class from the specified handlers
/// </summary>
/// <param name="results">The result handler for communicating results from the algorithm</param>
/// <param name="setup">The setup handler used to initialize algorithm state</param>
/// <param name="dataFeed">The data feed handler used to pump data to the algorithm</param>
/// <param name="transactions">The transaction handler used to process orders from the algorithm</param>
/// <param name="realTime">The real time handler used to process real time events</param>
/// <param name="mapFileProvider">The map file provider used to retrieve map files for the data feed</param>
/// <param name="factorFileProvider">Map file provider used as a map file source for the data feed</param>
/// <param name="dataProvider">file provider used to retrieve security data if it is not on the file system</param>
/// <param name="alphas">The alpha handler used to process generated insights</param>
/// <param name="objectStore">The object store used for persistence</param>
/// <param name="dataPermissionsManager">The data permission manager to use</param>
public LeanEngineAlgorithmHandlers(IResultHandler results,
ISetupHandler setup,
IDataFeed dataFeed,
ITransactionHandler transactions,
IRealTimeHandler realTime,
IMapFileProvider mapFileProvider,
IFactorFileProvider factorFileProvider,
IDataProvider dataProvider,
IAlphaHandler alphas,
IObjectStore objectStore,
IDataPermissionManager dataPermissionsManager
)
{
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));
}
if (objectStore == null)
{
throw new ArgumentNullException(nameof(objectStore));
}
if (dataPermissionsManager == null)
{
throw new ArgumentNullException(nameof(dataPermissionsManager));
}
Results = results;
Setup = setup;
DataFeed = dataFeed;
Transactions = transactions;
RealTime = realTime;
MapFileProvider = mapFileProvider;
FactorFileProvider = factorFileProvider;
DataProvider = dataProvider;
Alphas = alphas;
ObjectStore = objectStore;
DataPermissionsManager = dataPermissionsManager;
}
/// <summary>
/// Creates a new instance of the <see cref="LeanEngineAlgorithmHandlers"/> class from the specified composer using type names from configuration
/// </summary>
/// <param name="composer">The composer instance to obtain implementations from</param>
/// <returns>A fully hydrates <see cref="LeanEngineSystemHandlers"/> instance.</returns>
/// <exception cref="CompositionException">Throws a CompositionException during failure to load</exception>
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");
var objectStoreTypeName = Config.Get("object-store", "LocalObjectStore");
var dataPermissionManager = Config.Get("data-permission-manager", "DataPermissionManager");
return new LeanEngineAlgorithmHandlers(
composer.GetExportedValueByTypeName<IResultHandler>(resultHandlerTypeName),
composer.GetExportedValueByTypeName<ISetupHandler>(setupHandlerTypeName),
composer.GetExportedValueByTypeName<IDataFeed>(dataFeedHandlerTypeName),
composer.GetExportedValueByTypeName<ITransactionHandler>(transactionHandlerTypeName),
composer.GetExportedValueByTypeName<IRealTimeHandler>(realTimeHandlerTypeName),
composer.GetExportedValueByTypeName<IMapFileProvider>(mapFileProviderTypeName),
composer.GetExportedValueByTypeName<IFactorFileProvider>(factorFileProviderTypeName),
composer.GetExportedValueByTypeName<IDataProvider>(dataProviderTypeName),
composer.GetExportedValueByTypeName<IAlphaHandler>(alphaHandlerTypeName),
composer.GetExportedValueByTypeName<IObjectStore>(objectStoreTypeName),
composer.GetExportedValueByTypeName<IDataPermissionManager>(dataPermissionManager)
);
}
/// <summary>
/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
/// </summary>
/// <filterpriority>2</filterpriority>
public void Dispose()
{
Log.Trace("LeanEngineAlgorithmHandlers.Dispose(): start...");
Setup.DisposeSafely();
ObjectStore.DisposeSafely();
Log.Trace("LeanEngineAlgorithmHandlers.Dispose(): Disposed of algorithm handlers.");
}
}
}