forked from QuantConnect/Lean
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolutionExplorerMenuCommand.cs
342 lines (300 loc) · 13.6 KB
/
SolutionExplorerMenuCommand.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
/*
* 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.Design;
using System.Globalization;
using Microsoft.VisualStudio.Shell;
using Microsoft.VisualStudio.Shell.Interop;
using EnvDTE80;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using System.Threading;
using System.Diagnostics;
namespace QuantConnect.VisualStudioPlugin
{
/// <summary>
/// Command handler for QuantConnect Solution Explorer menu buttons
/// </summary>
internal sealed class SolutionExplorerMenuCommand
{
/// <summary>
/// Command IDs for Solution Explorer menu buttons
/// </summary>
public const int SendForBacktestingCommandId = 0x0100;
public const int SaveToQuantConnectCommandId = 0x0110;
/// <summary>
/// Command menu group (command set GUID).
/// </summary>
public static readonly Guid CommandSet = new Guid("00ce2ccb-74c7-42f4-bf63-52c573fc1532");
/// <summary>
/// VS Package that provides this command, not null.
/// </summary>
private readonly QuantConnectPackage _package;
private DTE2 _dte2;
private ProjectFinder _projectFinder;
private LogInCommand _logInCommand;
/// <summary>
/// Initializes a new instance of the <see cref="SolutionExplorerMenuCommand"/> class.
/// Adds our command handlers for menu (commands must exist in the command table file)
/// </summary>
/// <param name="package">Owner package, not null.</param>
private SolutionExplorerMenuCommand(Package package)
{
if (package == null)
{
throw new ArgumentNullException("package");
}
_package = package as QuantConnectPackage;
_dte2 = ServiceProvider.GetService(typeof(SDTE)) as DTE2;
_logInCommand = CreateLogInCommand();
var commandService = this.ServiceProvider.GetService(typeof(IMenuCommandService)) as OleMenuCommandService;
if (commandService != null)
{
RegisterSendForBacktesting(commandService);
RegisterSaveToQuantConnect(commandService);
}
}
private ProjectFinder CreateProjectFinder()
{
return new ProjectFinder(PathUtils.GetSolutionFolder(_dte2));
}
private LogInCommand CreateLogInCommand()
{
return new LogInCommand();
}
private void RegisterSendForBacktesting(OleMenuCommandService commandService)
{
var menuCommandID = new CommandID(CommandSet, SendForBacktestingCommandId);
var oleMenuItem = new OleMenuCommand(new EventHandler(SendForBacktestingCallback), menuCommandID);
commandService.AddCommand(oleMenuItem);
}
private void RegisterSaveToQuantConnect(OleMenuCommandService commandService)
{
var menuCommandID = new CommandID(CommandSet, SaveToQuantConnectCommandId);
var oleMenuItem = new OleMenuCommand(new EventHandler(SaveToQuantConnectCallback), menuCommandID);
commandService.AddCommand(oleMenuItem);
}
/// <summary>
/// Gets the instance of the command.
/// </summary>
public static SolutionExplorerMenuCommand Instance
{
get;
private set;
}
/// <summary>
/// Gets the service provider from the owner package.
/// </summary>
private IServiceProvider ServiceProvider
{
get
{
return _package;
}
}
// Lazily create ProjectFinder only when we have an opened solution
private ProjectFinder ProjectFinder
{
get {
if (_projectFinder == null)
{
_projectFinder = CreateProjectFinder();
}
return _projectFinder;
}
}
/// <summary>
/// Initializes the singleton instance of the command.
/// </summary>
/// <param name="package">Owner package, not null.</param>
public static void Initialize(Package package)
{
Instance = new SolutionExplorerMenuCommand(package);
}
private void SendForBacktestingCallback(object sender, EventArgs e)
{
ExecuteOnProject(sender, (selectedProjectId, selectedProjectName, files) =>
{
var fileNames = files.Select(f => f.FileName).ToList();
VsUtils.DisplayInStatusBar(this.ServiceProvider, "Uploading files to server ...");
UploadFilesToServer(selectedProjectId, files);
VsUtils.DisplayInStatusBar(this.ServiceProvider, "Compiling project ...");
var compileStatus = CompileProjectOnServer(selectedProjectId);
if (compileStatus.State == Api.CompileState.BuildError)
{
VsUtils.DisplayInStatusBar(this.ServiceProvider, "Compile error.");
VsUtils.ShowMessageBox(this.ServiceProvider, "Compile Error", "Error when compiling project.");
return;
}
VsUtils.DisplayInStatusBar(this.ServiceProvider, "Backtesting project ...");
Api.Backtest backtest = BacktestProjectOnServer(selectedProjectId, compileStatus.CompileId);
// Errors are not being transfered in response, so client can't tell if the backtest failed or not.
// This response error handling code will not work but should.
/* if (backtest.Errors.Count != 0) {
VsUtils.DisplayInStatusBar(this.ServiceProvider, "Backtest error.");
showMessageBox("Backtest Error", "Error when backtesting project.");
return;
}*/
VsUtils.DisplayInStatusBar(this.ServiceProvider, "Backtest complete.");
var projectUrl = string.Format(
CultureInfo.CurrentCulture,
"https://www.quantconnect.com/terminal/#open/{0}",
selectedProjectId
);
Process.Start(projectUrl);
});
}
private void SaveToQuantConnectCallback(object sender, EventArgs e)
{
ExecuteOnProject(sender, (selectedProjectId, selectedProjectName, files) =>
{
var fileNames = files.Select(f => f.FileName).ToList();
VsUtils.DisplayInStatusBar(this.ServiceProvider, "Uploading files to server ...");
UploadFilesToServer(selectedProjectId, files);
VsUtils.DisplayInStatusBar(this.ServiceProvider, "Files upload complete.");
});
}
private void UploadFilesToServer(int selectedProjectId, List<SelectedItem> files)
{
var api = AuthorizationManager.GetInstance().GetApi();
foreach (var file in files)
{
api.DeleteProjectFile(selectedProjectId, file.FileName);
var fileContent = File.ReadAllText(file.FilePath);
api.AddProjectFile(selectedProjectId, file.FileName, fileContent);
}
}
private Api.Compile CompileProjectOnServer(int projectId)
{
var api = AuthorizationManager.GetInstance().GetApi();
var compileStatus = api.CreateCompile(projectId);
var compileId = compileStatus.CompileId;
while (compileStatus.State == Api.CompileState.InQueue)
{
Thread.Sleep(5000);
compileStatus = api.ReadCompile(projectId, compileId);
}
return compileStatus;
}
private Api.Backtest BacktestProjectOnServer(int projectId, string compileId)
{
var api = AuthorizationManager.GetInstance().GetApi();
var backtestStatus = api.CreateBacktest(projectId, compileId, "My New Backtest");
var backtestId = backtestStatus.BacktestId;
while (!backtestStatus.Completed)
{
Thread.Sleep(5000);
backtestStatus = api.ReadBacktest(projectId, backtestId);
}
return backtestStatus;
}
private void ExecuteOnProject(object sender, Action<int, string, List<SelectedItem>> onProject)
{
if (_logInCommand.DoLogIn(this.ServiceProvider, _package.DataPath, explicitLogin: false))
{
var api = AuthorizationManager.GetInstance().GetApi();
var projects = api.ListProjects().Projects;
var projectNames = projects.Select(p => Tuple.Create(p.ProjectId, p.Name)).ToList();
var files = GetSelectedFiles(sender);
var fileNames = files.Select(tuple => tuple.FileName).ToList();
var suggestedProjectName = ProjectFinder.ProjectNameForFiles(fileNames);
var projectNameDialog = new ProjectNameDialog(projectNames, suggestedProjectName);
VsUtils.DisplayDialogWindow(projectNameDialog);
if (projectNameDialog.ProjectNameProvided())
{
var selectedProjectName = projectNameDialog.GetSelectedProjectName();
var selectedProjectId = projectNameDialog.GetSelectedProjectId();
ProjectFinder.AssociateProjectWith(selectedProjectName, fileNames);
if (!selectedProjectId.HasValue)
{
var newProjectLanguage = PathUtils.DetermineProjectLanguage(files.Select(f => f.FilePath).ToList());
if (!newProjectLanguage.HasValue)
{
VsUtils.ShowMessageBox(this.ServiceProvider, "Failed to determine project language",
$"Failed to determine programming laguage for a project");
return;
}
selectedProjectId = CreateQuantConnectProject(selectedProjectName, newProjectLanguage.Value);
if (!selectedProjectId.HasValue)
{
VsUtils.ShowMessageBox(this.ServiceProvider, "Failed to create a project", $"Failed to create a project {selectedProjectName}");
}
onProject.Invoke(selectedProjectId.Value, selectedProjectName, files);
}
else
{
onProject.Invoke(selectedProjectId.Value, selectedProjectName, files);
}
}
}
}
private int? CreateQuantConnectProject(string projectName, Language projectLanguage)
{
var api = AuthorizationManager.GetInstance().GetApi();
var projectResponse = api.CreateProject(projectName, projectLanguage);
if (!projectResponse.Success)
{
return null;
}
return projectResponse.Projects[0].ProjectId;
}
private List<SelectedItem> GetSelectedFiles(object sender)
{
var myCommand = sender as OleMenuCommand;
var selectedFiles = new List<SelectedItem>();
var selectedItems = (object[])_dte2.ToolWindows.SolutionExplorer.SelectedItems;
foreach (EnvDTE.UIHierarchyItem selectedUIHierarchyItem in selectedItems)
{
if (selectedUIHierarchyItem.Object is EnvDTE.ProjectItem)
{
var item = selectedUIHierarchyItem.Object as EnvDTE.ProjectItem;
var filePath = item.Properties.Item("FullPath").Value.ToString();
var selectedItem = new SelectedItem
{
FileName = item.Name,
FilePath = filePath
};
selectedFiles.Add(selectedItem);
}
}
// Check if the user selected a folder, and include files in directories otherwise language inference breaks.
// Also to maintain child folder structure on webclient tweak the filename to contain folders expressed in URI format.
if (selectedFiles.Count == 1 &&
string.IsNullOrEmpty(Path.GetExtension(selectedFiles.First().FilePath)))
{
var basePath = selectedFiles.First().FilePath;
var nonFolders = Directory.GetFiles(selectedFiles.First().FilePath, "*", SearchOption.AllDirectories);
selectedFiles = nonFolders.Select(c => new SelectedItem
{
FileName = "/" + c.Replace(basePath, string.Empty).Replace("\\", "/"),
FilePath = c
}).ToList();
}
return selectedFiles;
}
}
class SelectedItem
{
public string FileName
{
get; set;
}
public string FilePath
{
get; set;
}
}
}