-
Notifications
You must be signed in to change notification settings - Fork 107
/
AlgorithmLoader.cs
369 lines (323 loc) · 13.4 KB
/
AlgorithmLoader.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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
//==============================================================================
// Project: TuringTrader, simulator core
// Name: AlgorithmLoader
// Description: support for dynamic loading of algorithms
// History: 2018ix10, FUB, created
//------------------------------------------------------------------------------
// Copyright: (c) 2011-2023, Bertram Enterprises LLC dba TuringTrader.
// https://www.turingtrader.org
// License: This file is part of TuringTrader, an open-source backtesting
// engine/ trading simulator.
// modify it under the terms of the GNU Affero General Public
// License as published by the Free Software Foundation, either
// version 3 of the License, or (at your option) any later version.
// TuringTrader is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
// GNU Affero General Public License for more details.
// You should have received a copy of the GNU Affero General Public
// License along with TuringTrader. If not, see
// https://www.gnu.org/licenses/agpl-3.0.
//==============================================================================
// LOAD_ALGOS_V2_DLL: with this switch turned on, the algorithm loader
// looks for Algos.v2.dll in the same folder as the TuringTrader binary.
// This is used for convenient testing of the v2 sim core.
//#define LOAD_ALGOS_V2_DLL
#region Libraries
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
#endregion
namespace TuringTrader.Simulator
{
/// <summary>
/// Container for algorithm info
/// </summary>
public class AlgorithmInfo
{
/// <summary>
/// Name of algorithm. This is either the class name, in case the
/// algorithm is contained in a DLL, or the file name w/o path, in
/// case the algorithm is provided as source code.
/// </summary>
public string Name;
/// <summary>
/// Path, as displayed in Algorithm menu. This is either a relative path
/// to the source file containing the algorithm, or the DLL title property
/// </summary>
public List<string> DisplayPath;
/// <summary>
/// True, if algorithm is public. This field is only properly initialized
/// for algorithm contained in DLLs. For algorithms contained in source
/// files, it is always true.
/// </summary>
public bool IsPublic;
/// <summary>
/// Algorithm Type, in case algorithm is contained in DLL
/// </summary>
public Type DllType;
/// <summary>
/// Path to DLL, in case algorithm is contained in DLL
/// </summary>
public string DllPath;
/// <summary>
/// Algorithm source path, in case algorithm is contained in C# source file
/// </summary>
public string SourcePath;
/// <summary>
/// Simulation engine version, true for V2 algorithms
/// </summary>
public bool IsV2Algorithm;
}
/// <summary>
/// Helper class for dynamic algorithm instantiation.
/// </summary>
public class AlgorithmLoader
{
#region internal helpers
private static readonly List<AlgorithmInfo> _staticAlgorithms =
_enumStaticAlgorithms()
.OrderBy(a => a.Name)
.ToList();
private static Assembly _currentSourceAssembly = null;
private static IEnumerable<AlgorithmInfo> _enumAssyAlgorithms(Assembly assembly)
{
Type[] types;
try
{
types = assembly.GetTypes();
}
catch (Exception)
{
// can't load types: ignore
// NOTE: no error message here, as we might look at many DLLs
// that intentionally don't contain algorithms.
//Output.WriteLine("AlgorithmLoader: failed to load {0}, {1}", assembly.FullName, e.Message);
yield break;
}
#if true
// .net core 3
object[] descrAttributes = assembly.GetCustomAttributes(typeof(AssemblyDescriptionAttribute), false);
string title = descrAttributes.Count() > 0
? (descrAttributes[0] as AssemblyDescriptionAttribute).Description
: "n/a";
#else
// .net framework
object[] titleAttributes = assembly.GetCustomAttributes(typeof(AssemblyTitleAttribute), false);
string title = titleAttributes.Count() > 0
? (titleAttributes[0] as AssemblyTitleAttribute).Title
: "n/a";
#endif
foreach (Type type in types)
{
if (!type.IsAbstract
//&& type.IsSubclassOf(typeof(Algorithm))) // NOTE: used until 04/23/2021
&& type.GetInterface(nameof(IAlgorithm)) != null) // NOTE: new 04/23/2021
{
yield return new AlgorithmInfo
{
Name = type.Name,
IsPublic = type.IsPublic,
DllType = type,
DllPath = assembly.Location,
DisplayPath = new List<string>() { title },
IsV2Algorithm = type.IsSubclassOf(typeof(SimulatorV2.Algorithm))
};
}
}
yield break;
}
private static IEnumerable<AlgorithmInfo> _enumDllAlgorithms(string dllPath)
{
#if true
// enumerate algorithms from entry assembly
var entry = Assembly.GetEntryAssembly();
foreach (var type in _enumAssyAlgorithms(entry))
yield return type;
#endif
if (GlobalSettings.LoadAlgoDlls)
{
#if LOAD_ALGOS_V2_DLL
// FIXME: this is only temporary while we are
// kicking of development of the v2 simulator core
var v2Simulator = Path.Combine(
Path.GetDirectoryName(entry.Location),
"Algos.v2.dll");
if (File.Exists(v2Simulator))
{
var assembly = Assembly.LoadFrom(v2Simulator);
foreach (var type in _enumAssyAlgorithms(assembly))
yield return type;
}
#endif
DirectoryInfo dirInfo = new DirectoryInfo(dllPath);
if (!dirInfo.Exists)
yield break;
FileInfo[] files = dirInfo.GetFiles("*.dll");
// see https://msdn.microsoft.com/en-us/library/ms972968.aspx
foreach (FileInfo file in files)
{
Assembly assembly = null;
try
{
assembly = Assembly.LoadFrom(file.FullName);
}
catch
{
continue;
}
foreach (var type in _enumAssyAlgorithms(assembly))
yield return type;
}
}
yield break;
}
private static IEnumerable<AlgorithmInfo> _enumSourceAlgorithms(string path, List<string> displayPath)
{
if (!Directory.Exists(path))
yield break;
DirectoryInfo dirInfo = new DirectoryInfo(path);
FileInfo[] files = dirInfo.GetFiles("*.cs");
foreach (FileInfo file in files)
{
yield return new AlgorithmInfo
{
Name = file.Name,
DisplayPath = displayPath,
IsPublic = true,
SourcePath = file.FullName,
};
}
DirectoryInfo[] dirs = dirInfo.GetDirectories();
foreach (DirectoryInfo dir in dirs)
{
List<string> newDisplayPath = new List<string>(displayPath);
newDisplayPath.Add(dir.Name);
foreach (var a in _enumSourceAlgorithms(dir.FullName, newDisplayPath))
yield return a;
}
yield break;
}
private static IEnumerable<AlgorithmInfo> _enumStaticAlgorithms()
{
#if false
string exePath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
foreach (var algorithm in _enumDllAlgorithms(exePath))
yield return algorithm;
#endif
#if true
foreach (var algorithm in _enumDllAlgorithms(GlobalSettings.AlgorithmPath))
yield return algorithm;
#endif
foreach (var algorithm in _enumSourceAlgorithms(GlobalSettings.AlgorithmPath, new List<string>()))
yield return algorithm;
yield break;
}
#endregion
#region public static List<Type> GetAllAlgorithms()
/// <summary>
/// Return list of all known TuringTrader algorithms
/// </summary>
/// <param name="publicOnly">if true, only return public classes</param>
/// <returns>list of algorithms</returns>
public static List<AlgorithmInfo> GetAllAlgorithms(bool publicOnly = true)
{
var allAlgorithms = new List<AlgorithmInfo>(_staticAlgorithms);
if (_currentSourceAssembly != null)
{
var sourceAlgorithms = _enumAssyAlgorithms(_currentSourceAssembly);
allAlgorithms = allAlgorithms
.Concat(sourceAlgorithms)
.ToList();
}
return allAlgorithms
.Where(t => t.IsPublic == true || publicOnly == false)
.ToList();
}
#endregion
#region public static Algorithm InstantiateAlgorithm(string algorithmName)
/// <summary>
/// Instantiate TuringTrader algorithm
/// </summary>
/// <param name="algorithmName">class name</param>
/// <returns>algorithm instance</returns>
public static IAlgorithm InstantiateAlgorithm(string algorithmName)
{
List<AlgorithmInfo> allAlgorithms = GetAllAlgorithms(false);
List<AlgorithmInfo> matchingAlgorithms = allAlgorithms
.Where(a => a.Name == algorithmName)
.ToList();
if (matchingAlgorithms.Count > 1)
throw new Exception(string.Format("AlgorithmLoader: algorithm {0} is ambiguous", algorithmName));
if (matchingAlgorithms.Count < 1)
throw new Exception(string.Format("AlgorithmLoader: algorithm {0} not found", algorithmName));
return InstantiateAlgorithm(matchingAlgorithms.First());
}
#endregion
#region public static Algorithm InstantiateAlgorithm(AlgorithmInfo algorithmInfo)
/// <summary>
/// Instantiate TuringTrader algorithm
/// </summary>
/// <param name="algorithmInfo">algorithm info</param>
/// <returns>algorithm instance</returns>
public static IAlgorithm InstantiateAlgorithm(AlgorithmInfo algorithmInfo)
{
if (algorithmInfo.DllType != null)
{
// instantiate from DLL
return (IAlgorithm)Activator.CreateInstance(algorithmInfo.DllType);
}
else
{
// compile and instantiate from memory
_currentSourceAssembly = DynamicCompile.CompileSource(algorithmInfo.SourcePath);
if (_currentSourceAssembly == null)
return null;
var publicAlgorithms = _enumAssyAlgorithms(_currentSourceAssembly)
.Where(t => t.IsPublic)
.ToList();
if (publicAlgorithms.Count == 0)
{
Output.WriteLine("AlgorithmLoader: no algorithm found");
return null;
}
if (publicAlgorithms.Count > 1)
{
Output.WriteLine("AlgorithmLoader: multiple algorithms found");
return null;
}
var algo = InstantiateAlgorithm(publicAlgorithms.First());
if (algo != null)
Output.WriteLine("AlgorithmLoader: success!");
return algo;
}
}
#endregion
#region public static void PrintInfo()
/// <summary>
/// Print info about all algorithms found
/// </summary>
public static void PrintInfo()
{
Output.WriteLine("===== AlgorithmLoader Info =====");
int i = 0;
foreach (var info in _staticAlgorithms)
{
if (info.DllType != null)
{
Output.WriteLine("{0}: {1} ({2})", i++, info.Name, info.DllPath);
}
else
{
Output.WriteLine("{0}: {1} ({2})", i++, info.Name, info.SourcePath);
}
}
Output.WriteLine("===== end =====");
}
#endregion
}
}
//==============================================================================
// end of file