/// <summary> /// Refresh the data source, polling the API. /// </summary> private void timerRefresh_Tick(object sender, EventArgs e) { if (_processing) { return; } _processing = true; statusLabel.Text = "Updating Results for Backtest Id: " + _backtestId + "..."; Async.Add(new APIJob(APICommand.BacktestResults, (backtestResult, errors) => { //Handle login and API errors: switch (QuantConnectPlugin.HandleErrors(errors)) { //Handle project specific actions with a login error: case APIErrors.NotLoggedIn: this.SafeInvoke(d => d.ShowLogin(() => { FormOpenProject form = new FormOpenProject(); form.StartPosition = FormStartPosition.CenterScreen; form.Show(); })); this.SafeInvoke(d => d.Close()); return; } //Handle Results Packet: PacketBacktestResult packet = (PacketBacktestResult)backtestResult; this.SafeInvoke(d => d.SetBacktestResult(packet)); }, _backtestId)); // End of Async } // End of timer:
private void OpenProjects_Load(object sender, EventArgs e) { // Load projects buttonOpen.Enabled = false; this.UseWaitCursor = true; List <Project> projectList = new List <Project>(); try { Async.Add(new APIJob(APICommand.ProjectList, (projectfiles, errors) => { //Handle login and API errors: switch (QuantConnectPlugin.HandleErrors(errors)) { //Handle project specific actions with a login error: case APIErrors.NotLoggedIn: this.SafeInvoke(d => d.ShowLogin(() => { FormOpenProject form = new FormOpenProject(); form.StartPosition = FormStartPosition.CenterScreen; form.Show(); })); this.SafeInvoke(d => d.Close()); return; } listViewProjects.SafeInvoke(d => d.Items.Clear()); listViewProjects.SafeInvoke(d => d.BeginUpdate()); foreach (var project in (List <Project>)projectfiles) { ListViewItem item = new ListViewItem(); item.Text = project.Id.ToString(); ListViewItem.ListViewSubItem subitem = new ListViewItem.ListViewSubItem(); subitem.Text = project.Name.ToString(); item.SubItems.Add(subitem); subitem = new ListViewItem.ListViewSubItem(); subitem.Text = project.Modified.ToString(); item.SubItems.Add(subitem); listViewProjects.SafeInvoke(d => d.Items.Add(item)); } listViewProjects.SafeInvoke(d => d.EndUpdate()); buttonOpen.SafeInvoke(d => d.Enabled = true); this.SafeInvoke(d => d.UseWaitCursor = false); this.SafeInvoke(d => d.SizeLastColumn(listViewProjects)); })); } catch { MessageBox.Show("Connection timeout.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } }
private void ButtonSave_Click(object sender, EventArgs e) { buttonLogin.Enabled = false; this.UseWaitCursor = true; Email = textBoxEmail.Text; Password = textBoxPassword.Text; if (checkBoxRememberCredentials.Checked) { QuantConnectPlugin.SaveCredentials(Email, Password); } try { Async.Add(new APIJob(APICommand.Authenticate, (loggedIn, errors) => { buttonLogin.SafeInvoke(d => d.Enabled = true); this.SafeInvoke(d => d.UseWaitCursor = false); if (!(bool)loggedIn) { MessageBox.Show("Wrong user name or password", "QuantConnect Login", MessageBoxButtons.OK, MessageBoxIcon.Error); } else { QuantConnectPlugin.Email = Email; QuantConnectPlugin.Password = Password; this.SafeInvoke(d => { if (d.SuccessCallback != null) { d.SuccessCallback(); } }); this.SafeInvoke(d => d.Close()); } }, Email, Password)); } catch { MessageBox.Show("Connection timeout.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } }
/// <summary> /// Create a new project. /// </summary> public void CreateProject(bool retry = false) { progressBar.Value = 10; labelMessage.Text = "Creating Project on QuantConnect..."; // Create New Project on QC Async.Add(new APIJob(APICommand.NewProject, (projectIdObj, errors) => { // Handle login and API errors: switch (QuantConnectPlugin.HandleErrors(errors)) { // Handle project specific actions with a login error: case APIErrors.NotLoggedIn: this.SafeInvoke(d => d.ShowLogin(() => { FormCreateProject form = new FormCreateProject(); form.StartPosition = FormStartPosition.CenterScreen; form.Show(); })); this.SafeInvoke(d => d.Close()); return; } int projectId = projectIdObj == null ? 0 : Convert.ToInt32(projectIdObj); if (projectId == 0) { //Show the error form labelMessage.SafeInvoke(d => d.Text = "Sorry there was an error with your request. "); pictureError.SafeInvoke(d => d.Visible = true); } else { //Else we have a new id, time to load the template: labelMessage.SafeInvoke(d => d.Text = "Project Created, Opening..."); progressBar.SafeInvoke(d => d.Value = 90); //Open the project and close the loading dialog. QuantConnectPlugin.OpenProject(projectId, ProjectName, () => { this.SafeInvoke(d => d.Close()); QuantConnectPlugin.SetButtonsState(true); }); } }, ProjectName)); }
/// <summary> /// Launch a backtest. /// </summary> public void Backtest(bool retry = false) { int _projectID = QuantConnectPlugin.ProjectID; if (QuantConnectPlugin.LocalCompile()) { progressBar.Value = 33; if (!retry) { labelMessage.Text = "Building on QuantConnect..."; } // Compile on QC Async.Add(new APIJob(APICommand.Compile, (compile, errors) => { // Handle login and API errors: switch (QuantConnectPlugin.HandleErrors(errors)) { // Handle project specific actions with a login error: case APIErrors.NotLoggedIn: this.SafeInvoke(d => d.ShowLogin(() => { FormLoadBacktest form = new FormLoadBacktest(); form.StartPosition = FormStartPosition.CenterScreen; form.Show(); })); this.SafeInvoke(d => d.Close()); return; case APIErrors.CompileTimeout: labelMessage.SafeInvoke(d => d.Text = "Building timedout, retrying..."); this.SafeInvoke(d => d.Backtest(true)); return; case APIErrors.CompileError: MessageBox.Show("There was a build error: " + errors[0], "QuantConnect Build Error", MessageBoxButtons.OK, MessageBoxIcon.Error); this.SafeInvoke(d => d.Close()); return; } progressBar.SafeInvoke(d => d.Value = 66); labelMessage.SafeInvoke(d => d.Text = "Issuing Backtest..."); var packet = (PacketCompile)compile; if (!packet.Success) { pictureError.SafeInvoke(d => d.Visible = true); labelMessage.SafeInvoke(d => d.Text = "QuantConnect build failed. Please see project at QuantConnect.com"); return; } //Sending Backtest... var backtestId = QuantConnectPlugin.GetBacktestID(packet, _projectID); if (backtestId == "") { return; } //Got Id, Open Backtest Form: progressBar.SafeInvoke(d => d.Value = 99); labelMessage.SafeInvoke(d => d.Text = "Backtest Sent Successfully."); pictureCheck.SafeInvoke(d => d.Visible = true); this.SafeInvoke(d => d.LoadResult = backtestId); }, _projectID)); } else { pictureError.Visible = true; labelMessage.Text = "Local build failed. Please see Visual Studio"; } }
/******************************************************** * CLASS METHODS *********************************************************/ public static void Initialize() { // Start thread Thread thread = new Thread(new ThreadStart(Async.Consumer)); thread.Start(); // Set email and password string[] credentials = LoadCredentials(); Email = credentials[0]; Password = credentials[1]; //Create API Object API = new API(Email, Password); //Attempt Login: QuantConnectPlugin.Logged = API.Authenticate(Email, Password); //Check if QCAlgorithm is saved to its last version try { Async.Add(new APIJob(APICommand.CheckQCAlgoVersion, (latestversion, errors) => { if (!(bool)latestversion) { API.DownloadQCAlgorithm(Directory); } }, Directory)); } catch (Exception err) { MessageBox.Show(err.Message); } //Check if QCTemplate exists try { Async.Add(new APIJob(APICommand.UpdateTemplate, (a, b) => { var finished = false; var baseDirectory = Directory + "QCTemplate"; var destination = baseDirectory + @"\template.zip"; // Check if QCTemplate exists if (!System.IO.Directory.Exists(baseDirectory)) { System.IO.Directory.CreateDirectory(baseDirectory); using (System.Net.WebClient client = new System.Net.WebClient()) { try { client.DownloadFile(new Uri("https://www.quantconnect.com/api/v1/QCTemplate.zip"), destination); finished = true; } catch { MessageBox.Show("Sorry there was an error downloading the QCTemplate Project. Please make sure your computer has access to the internet."); } } if (finished) { API.WriteZip(baseDirectory, destination); } } }, Directory)); } catch (Exception err) { MessageBox.Show(err.Message); } }
/// <summary> /// Open your QuantConnect project in Visual Studio /// </summary> /// <param name="selectedProject"></param> /// <param name="api"></param> /// <param name="projectName"></param> public static bool OpenProject(int selectedProject, string projectName, Action enableForm) { // Don't let user open the same project he's working in if (selectedProject == ProjectID) { DialogResult dialogResult = MessageBox.Show("You are currently working on this project.", "Invalid Operation", MessageBoxButtons.OK); } ProjectID = selectedProject; ProjectName = CleanInput(projectName); // Check if file already exists if (!ExistingFileChecker(ProjectName, ProjectID)) { return(false); } Async.Add(new APIJob(APICommand.OpenProject, (files, errors) => { List <File> projectFiles = (List <File>)files; // Create Solution Solution4 soln = (Solution4)QCPluginPackage.ApplicationObject.Solution; soln.Create(Directory + ProjectID + " - " + ProjectName, ProjectName + ".sln"); // Create Project soln.AddFromTemplate(Directory + @"QCTemplate\csQCTemplate.vstemplate", Directory + ProjectID + " - " + ProjectName, ProjectName, false); // Add Files to Project: foreach (EnvDTE.Project proj in soln.Projects) { foreach (var file in proj.ProjectItems) { EnvDTE.ProjectItem item = file as EnvDTE.ProjectItem; if (item.Name == "Class1.cs") { item.Remove(); } } foreach (var file in projectFiles) { System.IO.File.WriteAllText(Directory + ProjectID + " - " + ProjectName + @"\" + file.Name, file.Code); //Add files to project: try { proj.ProjectItems.AddFromFile(Directory + ProjectID + " - " + ProjectName + @"\" + file.Name); } catch (SystemException ex) { MessageBox.Show("ERROR: " + ex); } } //Add References var vsProject = proj.Object as VSProject; if (vsProject == null) { continue; } var commonDLL = Directory + @"QCAlgorithm-master\QuantConnect.Algorithm\bin\Debug\QuantConnect.Common.dll"; if (!System.IO.File.Exists(commonDLL)) { //Download and unzip: API.DownloadQCAlgorithm(Directory); } //Safe to add reference: vsProject.References.Add(commonDLL); vsProject.References.Add(Directory + @"QCAlgorithm-master\QuantConnect.Algorithm\bin\Debug\MathNet.Numerics.dll"); vsProject.References.Add(Directory + @"QCAlgorithm-master\QuantConnect.Algorithm\bin\Debug\QuantConnect.Algorithm.dll"); vsProject.References.Add(Directory + @"QCAlgorithm-master\QuantConnect.Algorithm\bin\Debug\QuantConnect.Algorithm.Interface.dll"); } //Save & open solution soln.SaveAs(Directory + ProjectID + " - " + ProjectName + @"\" + ProjectName + ".sln"); ProjectLoaded = true; if (enableForm != null) { enableForm(); } }, selectedProject)); return(true); }