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);
            }
        }
Exemplo n.º 2
0
        public static void Main(string[] args)
        {
            try
            {
                const string strategyId    = "Client:/Optimizer/TAXTEST";
                const string accountId     = "BENCH:SP50";
                const string riskModelDate = "09/01/2020";
                const string backtestDate  = "09/01/2020";
                var          axpStrategy   = new AxiomaEquityOptimizerStrategy(null, strategyId);
                var          axpAccount    = new OptimizerAccount(accountId);
                var          optimization  = new Optimization(riskModelDate, backtestDate);
                var          tradesList    = new OptimizerTradesList(OptimizerTradesList.IdentifierTypeEnum.SedolChk, false);
                var          outputTypes   = new OptimizerOutputTypes(tradesList);

                var axpCalculationParameters =
                    new AxiomaEquityOptimizationParameters(axpStrategy, axpAccount, optimization, outputTypes);
                var axpCalculationParameterRoot = new AxiomaEquityOptimizationParametersRoot(axpCalculationParameters);

                var axpOptimizerApi = new AXPOptimizerApi(GetApiConfiguration());

                var calculationResponse = axpOptimizerApi.PostAndOptimizeWithHttpInfo(null, null, axpCalculationParameterRoot);
                // 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 = axpOptimizerApi.PostAndOptimizeWithHttpInfo(null, cacheControl, axpCalculationParameterRoot);

                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 =
                        axpOptimizerApi.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 =
                                axpOptimizerApi.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;
            }
        }