示例#1
0
        private void Login_Click(object sender, RoutedEventArgs e)
        {
            VSActivityLog.Info("Login button clicked");
            try
            {
                // Disable textbox passwordBox and login button
                userIdBox.IsReadOnly     = true;
                accessTokenBox.IsEnabled = false;
                logInButton.IsEnabled    = false;

                var userId      = userIdBox.Text;
                var accessToken = accessTokenBox.Password;
                var credentials = new Credentials(userId, accessToken);
                if (_authorizationManager.Login(credentials))
                {
                    VSActivityLog.Info("Logged in successfully");
                    VsUtils.DisplayInStatusBar(_serviceProvider, "Logged into QuantConnect");
                    _credentials = new Credentials(userId, accessToken);
                    Close();
                    return;
                }
            }
            catch (Exception ex)
            {
                VsUtils.ShowMessageBox(_serviceProvider, "QuantConnect Login Exception", ex.ToString());
                VSActivityLog.Error(ex.ToString());
            }
            VsUtils.DisplayInStatusBar(_serviceProvider, "Failed to login");
            userIdBox.BorderBrush      = Brushes.Red;
            accessTokenBox.BorderBrush = Brushes.Red;
            // Re enable button and textbox
            userIdBox.IsReadOnly     = false;
            accessTokenBox.IsEnabled = true;
            logInButton.IsEnabled    = true;
        }
        /// <summary>
        /// Perform QuantConnect authentication
        /// </summary>
        /// <param name="serviceProvider">Visual Studio services provider</param>
        /// <param name="explicitLogin">User explicitly clicked Log In button</param>
        /// <returns>true if user logged into QuantConnect, false otherwise</returns>
        public bool DoLogIn(IServiceProvider serviceProvider, string dataFolderPath, bool explicitLogin)
        {
            _log.Info("Logging in");

            if (!PathUtils.DataFolderPathValid(dataFolderPath))
            {
                VsUtils.ShowMessageBox(serviceProvider, "Incorrect data folder",
                                       $"Incorrect data folder path: {dataFolderPath}\nGo to Tools -> Settings -> QuantConnect to set it");
                return(false);
            }

            var authorizationManager = AuthorizationManager.GetInstance();

            if (authorizationManager.IsLoggedIn())
            {
                _log.Info("Already logged in");
                return(true);
            }

            var previousCredentials = _credentialsManager.GetLastCredential();

            if (!explicitLogin && LoggedInWithLastStorredPassword(previousCredentials, dataFolderPath))
            {
                _log.Info("Logged in with previously storred credentials");
                return(true);
            }

            return(LogInWithDialog(serviceProvider, previousCredentials, dataFolderPath));
        }
示例#3
0
        private async void ExecuteOnProjectAsync(object sender, Action <int, string, List <SelectedItem> > onProject)
        {
            if (await _authenticationCommand.Login(_serviceProvider, false))
            {
                var projects = await System.Threading.Tasks.Task.Run(() =>
                {
                    var api = AuthorizationManager.GetInstance().GetApi();
                    return(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 = _lazyProjectFinder.ProjectNameForFiles(fileNames);
                var projectNameDialog    = new ProjectNameDialog(projectNames, suggestedProjectName);
                VsUtils.DisplayDialogWindow(projectNameDialog);

                if (projectNameDialog.ProjectNameProvided)
                {
                    var selectedProjectName = projectNameDialog.SelectedProjectName;
                    var selectedProjectId   = projectNameDialog.SelectedProjectId;
                    _lazyProjectFinder.AssociateProjectWith(selectedProjectName, fileNames);

                    if (!selectedProjectId.HasValue)
                    {
                        var newProjectLanguage = PathUtils.DetermineProjectLanguage(files.Select(f => f.FilePath).ToList());
                        if (!newProjectLanguage.HasValue)
                        {
                            VsUtils.ShowMessageBox(_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(_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 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 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);
            });
        }