forked from Jay-Jay-D/LeanOptimization
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRunner.cs
114 lines (93 loc) · 3.81 KB
/
Runner.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
using QuantConnect.Configuration;
using QuantConnect.Lean.Engine;
using QuantConnect.Lean.Engine.Results;
using QuantConnect.Logging;
using QuantConnect.Packets;
using QuantConnect.Util;
using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.IO;
using System.Linq;
namespace Optimization
{
public class Runner : MarshalByRefObject
{
private BacktestingResultHandler _resultsHandler;
IOptimizerConfiguration _config;
public Dictionary<string, string> Run(Dictionary<string, object> items)
{
string plain = string.Join(",", items.Select(s => s.Value));
Dictionary<string, Dictionary<string, string>> results = AppDomainManager.GetResults(AppDomain.CurrentDomain);
_config = AppDomainManager.GetConfig(AppDomain.CurrentDomain);
if (results.ContainsKey(plain))
{
return results[plain];
}
foreach (var pair in items)
{
Config.Set(pair.Key, pair.Value.ToString());
}
LaunchLean();
results.Add(plain, _resultsHandler.FinalStatistics);
AppDomainManager.SetResults(AppDomain.CurrentDomain, results);
return _resultsHandler.FinalStatistics;
}
private void LaunchLean()
{
Config.Set("environment", "backtesting");
if (!string.IsNullOrEmpty(_config.AlgorithmTypeName))
{
Config.Set("algorithm-type-name", _config.AlgorithmTypeName);
}
if (!string.IsNullOrEmpty(_config.AlgorithmLocation))
{
Config.Set("algorithm-location", Path.GetFileName(_config.AlgorithmLocation));
}
if (!string.IsNullOrEmpty(_config.DataFolder))
{
Config.Set("data-folder", _config.DataFolder);
}
if (_config.StartDate.HasValue)
{
Config.Set("startDate", _config.StartDate.Value.ToString("O"));
}
if (_config.EndDate.HasValue)
{
Config.Set("endDate", _config.EndDate.Value.ToString("O"));
}
var systemHandlers = LeanEngineSystemHandlers.FromConfiguration(Composer.Instance);
systemHandlers.Initialize();
var logFileName = "log_" + Guid.NewGuid().ToString() + ".txt";
var logHandlers = new ILogHandler[] { new FileLogHandler(logFileName, true) };
using (Log.LogHandler = new CompositeLogHandler(logHandlers))
{
LeanEngineAlgorithmHandlers leanEngineAlgorithmHandlers;
try
{
leanEngineAlgorithmHandlers = LeanEngineAlgorithmHandlers.FromConfiguration(Composer.Instance);
_resultsHandler = (BacktestingResultHandler)leanEngineAlgorithmHandlers.Results;
}
catch (CompositionException compositionException)
{
Log.Error("Engine.Main(): Failed to load library: " + compositionException);
throw;
}
string algorithmPath;
AlgorithmNodePacket job = systemHandlers.JobQueue.NextJob(out algorithmPath);
try
{
var _engine = new Engine(systemHandlers, leanEngineAlgorithmHandlers, Config.GetBool("live-mode"));
_engine.Run(job, algorithmPath);
}
finally
{
Log.Trace("Engine.Main(): Packet removed from queue: " + job.AlgorithmId);
// clean up resources
systemHandlers.Dispose();
leanEngineAlgorithmHandlers.Dispose();
}
}
}
}
}