public void OptimizerWorkflow_Should_Succeed_When_GivenValidInputs()
        {
            const string strategyId    = "CLIENT:/Aapi/Optimizers/BPMAPISIMPLE";
            const string accountId     = "BENCH:SP50";
            const string riskModelDate = "09/01/2008";
            var          bpmStrategy   = new BPMOptimizerStrategy(null, strategyId);
            var          bpmAccount    = new OptimizerAccount(accountId);
            var          optimization  = new BPMOptimization(riskModelDate, backtestDate: "09/01/2008");
            var          tradesList    = new OptimizerTradesList(OptimizerTradesList.IdentifierTypeEnum.Asset, false);
            var          outputTypes   = new OptimizerOutputTypes(tradesList);

            var bpmCalculationParameters     = new BPMOptimizationParameters(bpmStrategy, optimization, bpmAccount, outputTypes);
            var bpmCalculationParametersRoot = new BPMOptimizationParametersRoot(bpmCalculationParameters);

            Console.WriteLine(JsonConvert.SerializeObject(bpmCalculationParametersRoot));

            try
            {
                var postAndOptimizeHttpResponse =
                    bpmOptimizerApi.PostAndOptimizeWithHttpInfo(bPMOptimizationParametersRoot: bpmCalculationParametersRoot);

                postAndOptimizeHttpResponse.StatusCode
                .Should()
                .Match <HttpStatusCode>(c => c == HttpStatusCode.Created || c == HttpStatusCode.Accepted);

                switch (postAndOptimizeHttpResponse.StatusCode)
                {
                case HttpStatusCode.Accepted:
                    var optimizationId =
                        postAndOptimizeHttpResponse.Headers["X-Factset-Api-Calculation-Id"].First();
                    bool shouldPoll;
                    do
                    {
                        var pollingResponse = bpmOptimizerApi.GetOptimizationStatusByIdWithHttpInfo(optimizationId);
                        pollingResponse.StatusCode
                        .Should()
                        .Match <HttpStatusCode>(c =>
                                                c == HttpStatusCode.Created || c == HttpStatusCode.Accepted);

                        if (pollingResponse.StatusCode == HttpStatusCode.Created)
                        {
                            break;
                        }

                        shouldPoll = pollingResponse.StatusCode == HttpStatusCode.Accepted;

                        if (pollingResponse.Headers.ContainsKey("Cache-Control"))
                        {
                            var maxAge = pollingResponse.Headers["Cache-Control"][0];
                            if (string.IsNullOrWhiteSpace(maxAge))
                            {
                                Console.WriteLine("Sleeping for 2 seconds");
                                // Sleep for at least 2 seconds.
                                Thread.Sleep(2000);
                            }
                            else
                            {
                                var age = int.Parse(maxAge.Replace("max-age=", ""));
                                Console.WriteLine($"Sleeping for {age} seconds");
                                Thread.Sleep(age * 1000);
                            }
                        }
                    } while (shouldPoll);

                    var getResultResponse = bpmOptimizerApi.GetOptimizationResultWithHttpInfo(optimizationId);
                    getResultResponse.StatusCode.Should().Be(HttpStatusCode.OK);
                    getResultResponse.Data.Should().NotBeNull();
                    break;

                case HttpStatusCode.Created:
                    break;
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
        }
Esempio n. 2
0
        public static void Main(string[] args)
        {
            try
            {
                const string strategyId  = "CLIENT:/Aapi/BPMAPISIMPLE";
                var          bpmStrategy = new BPMOptimizerStrategy(null, strategyId);
                var          tradesList  = new OptimizerTradesList(OptimizerTradesList.IdentifierTypeEnum.Asset, false);
                var          outputTypes = new OptimizerOutputTypes(tradesList);

                var bpmCalculationParameters =
                    new BPMOptimizationParameters(strategy: bpmStrategy, outputTypes: outputTypes);
                var bpmCalculationParameterRoot = new BPMOptimizationParametersRoot(bpmCalculationParameters);

                var bpmOptimizerApi = new BPMOptimizerApi(GetApiConfiguration());

                var calculationResponse = bpmOptimizerApi.PostAndOptimizeWithHttpInfo(null, null, bpmCalculationParameterRoot);
                // Comment the above line and uncomment the below lines to add cache control configuration. Results are by default cached for 12 hours; Setting max-stale=300 will fetch a cached result which is at max 5 minutes older.
                //var cacheControl = "max-stale=300";
                //var calculationResponse = bpmOptimizerApi.PostAndOptimizeWithHttpInfo(null, cacheControl, bpmCalculationParameterRoot);

                switch (calculationResponse.StatusCode)
                {
                case HttpStatusCode.Created:
                    var result = (ObjectRoot)calculationResponse.Data;
                    PrintResult(result);
                    return;

                case HttpStatusCode.Accepted:
                    var calculationStatus = (CalculationInfoRoot)calculationResponse.Data;
                    var optimizationId    = calculationStatus.Data.CalculationId;
                    Console.WriteLine("Optimization Id: " + optimizationId);
                    var optimizationStatusResponse =
                        bpmOptimizerApi.GetOptimizationStatusByIdWithHttpInfo(optimizationId);
                    var continuePolling = optimizationStatusResponse.StatusCode == HttpStatusCode.Accepted;
                    while (continuePolling)
                    {
                        if (optimizationStatusResponse.StatusCode == HttpStatusCode.Accepted)
                        {
                            if (optimizationStatusResponse.Headers.ContainsKey("Cache-Control"))
                            {
                                var maxAge = optimizationStatusResponse.Headers["Cache-Control"][0];
                                if (string.IsNullOrWhiteSpace(maxAge))
                                {
                                    Console.WriteLine("Sleeping for 2 seconds");
                                    // Sleep for at least 2 seconds.
                                    Thread.Sleep(2000);
                                }
                                else
                                {
                                    var age = int.Parse(maxAge.Replace("max-age=", ""));
                                    Console.WriteLine($"Sleeping for {age} seconds");
                                    Thread.Sleep(age * 1000);
                                }
                            }

                            optimizationStatusResponse =
                                bpmOptimizerApi.GetOptimizationStatusByIdWithHttpInfo(optimizationId);
                        }
                        else
                        {
                            continuePolling = false;
                        }
                    }

                    if (optimizationStatusResponse.StatusCode == HttpStatusCode.Created)
                    {
                        Console.WriteLine("Optimization Id: " + optimizationId + " succeeded!");
                        PrintResult((ObjectRoot)optimizationStatusResponse.Data);
                    }
                    else
                    {
                        Console.WriteLine("Optimization Id: " + optimizationId + " failed!");
                        Console.WriteLine("Error message: " + optimizationStatusResponse.ErrorText);
                    }

                    break;
                }
            }
            catch (ApiException e)
            {
                Console.WriteLine($"Status Code: {e.ErrorCode}");
                Console.WriteLine($"Reason : {e.Message}");
                Console.WriteLine(e.StackTrace);
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                throw;
            }
        }