/********************************************************
         * 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);
            }
        }
Пример #2
0
        /// <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";
            }
        }
        /// <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);
        }