Пример #1
0
        public void LiveForexAlgorithms_CanBeUsedWithFXCM_Successfully()
        {
            var user     = Config.Get("fxcm-user-name");
            var password = Config.Get("fxcm-password");
            var account  = Config.Get("fxcm-account-id");
            var file     = new List <ProjectFile>
            {
                new ProjectFile {
                    Name = "main.cs", Code = File.ReadAllText("../../../Algorithm.CSharp/BasicTemplateForexAlgorithm.cs")
                }
            };

            // Create a new project
            var project = _api.CreateProject("Test project - " + DateTime.Now, Language.CSharp);

            Assert.IsTrue(project.Success);

            // Update Project
            var update = _api.UpdateProject(project.ProjectId, file);

            Assert.IsTrue(update.Success);

            // Create compile
            var compile = _api.CreateCompile(project.ProjectId);

            Assert.IsTrue(compile.Success);

            // Create default algorithm settings
            var settings = new FXCMLiveAlgorithmSettings(user,
                                                         password,
                                                         BrokerageEnvironment.Paper,
                                                         account);

            // Wait for project to compile
            Thread.Sleep(10000);

            // Create live default algorithm
            var createLiveAlgorithm = _api.CreateLiveAlgorithm(project.ProjectId, compile.CompileId, "server512", settings);

            Assert.IsTrue(createLiveAlgorithm.Success);

            if (stopLiveAlgos)
            {
                // Liquidate live algorithm
                var liquidateLive = _api.LiquidateLiveAlgorithm(project.ProjectId);
                Assert.IsTrue(liquidateLive.Success);

                // Stop live algorithm
                var stopLive = _api.StopLiveAlgorithm(project.ProjectId);
                Assert.IsTrue(stopLive.Success);

                // Delete the project
                var deleteProject = _api.DeleteProject(project.ProjectId);
                Assert.IsTrue(deleteProject.Success);
            }
        }
Пример #2
0
        /// <summary>
        /// Runs the algorithm with the given settings and file
        /// </summary>
        /// <param name="settings">Settings for Lean</param>
        /// <param name="file">File to run</param>
        private void RunLiveAlgorithm(BaseLiveAlgorithmSettings settings, ProjectFile file)
        {
            // Create a new project
            var project = _api.CreateProject($"Test project - {DateTime.Now.ToStringInvariant()}", Language.CSharp);

            // Add Project Files
            var addProjectFile = _api.AddProjectFile(project.Projects.First().ProjectId, file.Name, file.Code);

            Assert.IsTrue(addProjectFile.Success);

            // Create compile
            var compile = _api.CreateCompile(project.Projects.First().ProjectId);

            Assert.IsTrue(compile.Success);

            // Wait at max 30 seconds for project to compile
            Compile compileCheck = WaitForCompilerResponse(project.Projects.First().ProjectId, compile.CompileId, 30);

            Assert.IsTrue(compileCheck.Success);
            Assert.IsTrue(compileCheck.State == CompileState.BuildSuccess);

            // Get a live node to launch the algorithm on
            var nodes = _api.ReadNodes(_testOrganization);

            Assert.IsTrue(nodes.Success);
            var freeNode = nodes.LiveNodes.Where(x => x.Busy == false);

            Assert.IsNotEmpty(freeNode, "No free Live Nodes found");

            // Create live default algorithm
            var createLiveAlgorithm = _api.CreateLiveAlgorithm(project.Projects.First().ProjectId, compile.CompileId, freeNode.FirstOrDefault().Id, settings);

            Assert.IsTrue(createLiveAlgorithm.Success);

            if (stopLiveAlgos)
            {
                // Liquidate live algorithm; will also stop algorithm
                var liquidateLive = _api.LiquidateLiveAlgorithm(project.Projects.First().ProjectId);
                Assert.IsTrue(liquidateLive.Success);

                // Delete the project
                var deleteProject = _api.DeleteProject(project.Projects.First().ProjectId);
                Assert.IsTrue(deleteProject.Success);
            }
        }