public async Task PostInstanceDataForAlgo()
        {
            UploadStringDTO metadataForUploadedBinary = await UploadStringAlgo();

            string algoID = metadataForUploadedBinary.AlgoId;

            GetPopulatedInstanceDataDTO getinstanceAlgo = new GetPopulatedInstanceDataDTO();

            InstanceDataDTO instanceForAlgo = getinstanceAlgo.returnInstanceDataDTO(algoID);

            string url = ApiPaths.ALGO_STORE_ALGO_INSTANCE_DATA;

            var postInstanceDataResponse = await this.Consumer.ExecuteRequest(url, Helpers.EmptyDictionary, JsonUtils.SerializeObject(instanceForAlgo), Method.POST);

            Assert.That(postInstanceDataResponse.Status == HttpStatusCode.OK);

            InstanceDataDTO postInstanceData = JsonUtils.DeserializeJson <InstanceDataDTO>(postInstanceDataResponse.ResponseJson);

            Assert.That(postInstanceData.AlgoId, Is.EqualTo((instanceForAlgo.AlgoId)));
            Assert.That(postInstanceData.AlgoClientId, Is.EqualTo(instanceForAlgo.AlgoClientId));
            Assert.That(postInstanceData.AlgoId, Is.EqualTo(instanceForAlgo.AlgoId));
            Assert.That(postInstanceData.InstanceName, Is.EqualTo(instanceForAlgo.InstanceName));


            ClientInstanceEntity instanceDataEntityExists = await ClientInstanceRepository.TryGetAsync(t => t.Id == postInstanceData.InstanceId) as ClientInstanceEntity;

            Assert.NotNull(instanceDataEntityExists);
        }
        public async Task GetAllClientInstanceData()
        {
            MetaDataResponseDTO metadataForUploadedBinary = await UploadBinaryAlgoAndGetResponceDTO();

            string algoID = metadataForUploadedBinary.Id;

            InstanceDataDTO instanceForAlgo = new InstanceDataDTO()
            {
                AlgoId      = algoID,
                HftApiKey   = "key",
                AssetPair   = "BTCUSD",
                TradedAsset = "USD",
                Margin      = "1",
                Volume      = "1"
            };

            string url = ApiPaths.ALGO_STORE_ALGO_INSTANCE_DATA;

            var postInstanceDataResponse = await this.Consumer.ExecuteRequest(url, Helpers.EmptyDictionary, JsonUtils.SerializeObject(instanceForAlgo), Method.POST);

            Assert.That(postInstanceDataResponse.Status == HttpStatusCode.OK);

            InstanceDataDTO postInstanceData = JsonUtils.DeserializeJson <InstanceDataDTO>(postInstanceDataResponse.ResponseJson);

            Assert.That(postInstanceData.AlgoId, Is.EqualTo(instanceForAlgo.AlgoId));
            Assert.That(postInstanceData.AssetPair, Is.EqualTo(instanceForAlgo.AssetPair));
            Assert.That(postInstanceData.HftApiKey, Is.EqualTo(instanceForAlgo.HftApiKey));
            Assert.That(postInstanceData.TradedAsset, Is.EqualTo(instanceForAlgo.TradedAsset));
            Assert.AreEqual((int)Convert.ToDouble(postInstanceData.Volume), (int)Convert.ToDouble(instanceForAlgo.Volume));
            Assert.AreEqual((int)Convert.ToDouble(postInstanceData.Margin), (int)Convert.ToDouble(instanceForAlgo.Margin));
            Assert.NotNull(postInstanceData.InstanceId);

            ClientInstanceEntity instanceDataEntityExists = await ClientInstanceRepository.TryGetAsync(t => t.Id == postInstanceData.InstanceId) as ClientInstanceEntity;

            Assert.NotNull(instanceDataEntityExists);

            url = ApiPaths.ALGO_STORE_ALGO_GET_ALL_INSTANCE_DATA;

            Dictionary <string, string> queryParmas = new Dictionary <string, string>()
            {
                { "algoId", algoID }
            };

            var responceAllClientInstance = await this.Consumer.ExecuteRequest(url, queryParmas, null, Method.GET);

            Assert.That(responceAllClientInstance.Status, Is.EqualTo(HttpStatusCode.OK));
            Object responceClientGetAll = JsonUtils.DeserializeJson(responceAllClientInstance.ResponseJson);
            List <InstanceDataDTO> listAllClinetObjects = Newtonsoft.Json.JsonConvert.DeserializeObject <List <InstanceDataDTO> >(responceClientGetAll.ToString());
            int mathcedKeysCounter = 0;

            foreach (InstanceDataDTO currentData in listAllClinetObjects)
            {
                bool Exists = currentData.InstanceId.Equals(postInstanceData.InstanceId);
                if (Exists)
                {
                    mathcedKeysCounter++;
                }
            }
            Assert.That(mathcedKeysCounter, Is.EqualTo(1));
        }
        public async Task CheckTailLogsForStoppedInstances(AlgoInstanceType algoInstanceType)
        {
            // Create an algo
            AlgoDataDTO algoData = await CreateAlgo();

            // Create algo instance
            var instanceData = await SaveInstance(algoData, algoInstanceType);

            // Wait up to 3 minutes for the instance to start
            await WaitAlgoInstanceToStart(instanceData.InstanceId);

            // Stop the instance
            await StopAlgoInstance(instanceData);

            //Get the instance
            ClientInstanceEntity instanceDataFromDB = await ClientInstanceRepository.TryGetAsync($"algo_{algoData.Id}", instanceData.InstanceId) as ClientInstanceEntity;

            Assert.That(instanceDataFromDB.AlgoInstanceStatus, Is.EqualTo(AlgoInstanceStatus.Stopped));

            // Wait 30 seconds
            Wait.ForPredefinedTime(30 * 1000);
            var tailLog = await GetInstanceTailLogFromApi(instanceData);

            Assert.That(tailLog, Is.Not.Null);
        }
        public async Task GetInstanceData()
        {
            MetaDataResponseDTO metadataForUploadedBinary = await UploadBinaryAlgoAndGetResponceDTO();

            string algoID = metadataForUploadedBinary.Id;

            InstanceDataDTO instanceForAlgo = new InstanceDataDTO()
            {
                AlgoId      = algoID,
                HftApiKey   = "key",
                AssetPair   = "BTCUSD",
                TradedAsset = "USD",
                Margin      = "1",
                Volume      = "1"
            };

            string url = ApiPaths.ALGO_STORE_ALGO_INSTANCE_DATA;

            var postInstanceDataResponse = await this.Consumer.ExecuteRequest(url, Helpers.EmptyDictionary, JsonUtils.SerializeObject(instanceForAlgo), Method.POST);

            Assert.That(postInstanceDataResponse.Status, Is.EqualTo(HttpStatusCode.OK));

            InstanceDataDTO postInstanceData = JsonUtils.DeserializeJson <InstanceDataDTO>(postInstanceDataResponse.ResponseJson);

            Assert.That(postInstanceData.AlgoId, Is.EqualTo((instanceForAlgo.AlgoId)));
            Assert.That(postInstanceData.AssetPair, Is.EqualTo((instanceForAlgo.AssetPair)));
            Assert.That(postInstanceData.HftApiKey, Is.EqualTo((instanceForAlgo.HftApiKey)));
            Assert.That(postInstanceData.TradedAsset, Is.EqualTo((instanceForAlgo.TradedAsset)));
            Assert.AreEqual((int)Convert.ToDouble(postInstanceData.Volume), (int)Convert.ToDouble(instanceForAlgo.Volume));
            Assert.AreEqual((int)Convert.ToDouble(postInstanceData.Margin), (int)Convert.ToDouble(instanceForAlgo.Margin));
            Assert.NotNull(postInstanceData.InstanceId);

            ClientInstanceEntity instanceDataEntityExists = await ClientInstanceRepository.TryGetAsync(t => t.Id == postInstanceData.InstanceId) as ClientInstanceEntity;

            Assert.NotNull(instanceDataEntityExists);

            Dictionary <string, string> queryParmas = new Dictionary <string, string>()
            {
                { "algoId", algoID },
                { "instanceId", postInstanceData.InstanceId }
            };

            var instanceDataResponse = await this.Consumer.ExecuteRequest(url, queryParmas, null, Method.GET);

            Assert.That(instanceDataResponse.Status, Is.EqualTo(HttpStatusCode.OK));

            InstanceDataDTO returnedClinetInstanceData = JsonUtils.DeserializeJson <InstanceDataDTO>(instanceDataResponse.ResponseJson);

            Assert.That(returnedClinetInstanceData.AlgoId, Is.EqualTo((postInstanceData.AlgoId)));
            Assert.That(returnedClinetInstanceData.AssetPair, Is.EqualTo((postInstanceData.AssetPair)));
            Assert.That(returnedClinetInstanceData.HftApiKey, Is.EqualTo((postInstanceData.HftApiKey)));
            Assert.That(returnedClinetInstanceData.TradedAsset, Is.EqualTo((postInstanceData.TradedAsset)));
            Assert.AreEqual((int)Convert.ToDouble(postInstanceData.Volume), (int)Convert.ToDouble(postInstanceData.Volume));
            Assert.AreEqual((int)Convert.ToDouble(postInstanceData.Margin), (int)Convert.ToDouble(postInstanceData.Margin));
            Assert.That(returnedClinetInstanceData.InstanceId, Is.EqualTo((postInstanceData.InstanceId)));
        }
        public async Task GetAllClientInstanceData()
        {
            UploadStringDTO metadataForUploadedBinary = await UploadStringAlgo();

            string algoID = metadataForUploadedBinary.AlgoId;

            GetPopulatedInstanceDataDTO getinstanceAlgo = new GetPopulatedInstanceDataDTO();

            InstanceDataDTO instanceForAlgo = getinstanceAlgo.returnInstanceDataDTO(algoID);

            string url = ApiPaths.ALGO_STORE_ALGO_INSTANCE_DATA;

            var postInstanceDataResponse = await this.Consumer.ExecuteRequest(url, Helpers.EmptyDictionary, JsonUtils.SerializeObject(instanceForAlgo), Method.POST);

            Assert.That(postInstanceDataResponse.Status == HttpStatusCode.OK);

            InstanceDataDTO postInstanceData = JsonUtils.DeserializeJson <InstanceDataDTO>(postInstanceDataResponse.ResponseJson);

            Assert.That(postInstanceData.AlgoClientId, Is.EqualTo(instanceForAlgo.AlgoClientId));
            Assert.That(postInstanceData.AlgoId, Is.EqualTo(instanceForAlgo.AlgoId));
            Assert.That(postInstanceData.InstanceName, Is.EqualTo(instanceForAlgo.InstanceName));

            ClientInstanceEntity instanceDataEntityExists = await ClientInstanceRepository.TryGetAsync(t => t.Id == postInstanceData.InstanceId) as ClientInstanceEntity;

            Assert.NotNull(instanceDataEntityExists);

            url = ApiPaths.ALGO_STORE_ALGO_GET_ALL_INSTANCE_DATA;

            Dictionary <string, string> queryParmas = new Dictionary <string, string>()
            {
                { "algoId", algoID }
            };

            var responceAllClientInstance = await this.Consumer.ExecuteRequest(url, queryParmas, null, Method.GET);

            Assert.That(responceAllClientInstance.Status, Is.EqualTo(HttpStatusCode.OK));
            Object responceClientGetAll = JsonUtils.DeserializeJson(responceAllClientInstance.ResponseJson);
            List <InstanceDataDTO> listAllClinetObjects = Newtonsoft.Json.JsonConvert.DeserializeObject <List <InstanceDataDTO> >(responceClientGetAll.ToString());
            int mathcedKeysCounter = 0;

            foreach (InstanceDataDTO currentData in listAllClinetObjects)
            {
                bool Exists = currentData.InstanceId.Equals(postInstanceData.InstanceId);
                if (Exists)
                {
                    mathcedKeysCounter++;
                }
            }
            Assert.That(mathcedKeysCounter, Is.EqualTo(1));
        }
        public async Task BackTestWithLongFunctionPeriod(AlgoInstanceType algoInstanceType)
        {
            WalletDTO walletDTO = null;

            if (algoInstanceType == AlgoInstanceType.Live)
            {
                walletDTO = await GetExistingWallet();
            }

            // Create algo
            var algoData = await CreateAlgo();

            // Build days offset
            DaysOffsetDTO daysOffsetDTO = BuildDaysOffsetByInstanceType(algoInstanceType);

            // Set Instance start date to be three years ago and end date to be 10 days ago
            int start = 365 * -3;
            int end   = -10;

            daysOffsetDTO.AlgoStartOffset     = start;
            daysOffsetDTO.AlgoEndOffset       = end;
            daysOffsetDTO.SmaShortStartOffset = start;
            daysOffsetDTO.SmaShortEndOffset   = end;
            daysOffsetDTO.SmaShortStartOffset = start;
            daysOffsetDTO.SmaLongEndOffset    = end;

            // Build InstanceParameters
            InstanceParameters instanceParameters = InstanceConfig.ValidMetaData;

            // Build instance request payload
            var instanceForAlgo = InstanceDataBuilder.BuildInstanceData(algoData, walletDTO, algoInstanceType, instanceParameters, daysOffsetDTO);

            // Build save instance url
            var url = algoInstanceType == AlgoInstanceType.Live ? ApiPaths.ALGO_STORE_SAVE_ALGO_INSTANCE : ApiPaths.ALGO_STORE_FAKE_TRADING_INSTANCE_DATA;

            var postInstanceDataResponse = await Consumer.ExecuteRequest(url, Helpers.EmptyDictionary, JsonUtils.SerializeObject(instanceForAlgo), Method.POST);

            AlgoErrorDTO postInstanceDataResponseDTO = JsonUtils.DeserializeJson <AlgoErrorDTO>(postInstanceDataResponse.ResponseJson);

            // Get instance data from DB
            ClientInstanceEntity instanceDataFromDB = await ClientInstanceRepository.TryGetAsync(t => t.AlgoId == algoData.Id) as ClientInstanceEntity;

            Assert.Multiple(() =>
            {
                Assert.That(postInstanceDataResponse.Status, Is.EqualTo(HttpStatusCode.InternalServerError));
                Assert.That(postInstanceDataResponseDTO.ErrorMessage, Does.StartWith("Code:511-InitialWalletBalanceNotCalculated Message:Initial wallet balance could not be calculated. Could not get history price for"));
                // Verify intance is not created in DB
                Assert.That(instanceDataFromDB, Is.Null);
            });
        }
Ejemplo n.º 7
0
        public static async Task WaitAlgoToStart(GenericRepository <ClientInstanceEntity, IClientInstance> clientInstanceRepository, InstanceDataDTO postInstanceData)
        {
            ClientInstanceEntity instanceDataEntityExists = await clientInstanceRepository.TryGetAsync(t => t.Id == postInstanceData.InstanceId) as ClientInstanceEntity;

            Assert.NotNull(instanceDataEntityExists);

            // Wait up to 3 minutes for the algo to be started
            int count = 45;

            while (instanceDataEntityExists.AlgoInstanceStatusValue != "Started" && count > 1) // TODO: Update when a health check endpoint is created
            {
                Wait.ForPredefinedTime(5000);                                                  // Wait for five secodns before getting the algo instance data again
                instanceDataEntityExists = await clientInstanceRepository.TryGetAsync(t => t.Id == postInstanceData.InstanceId) as ClientInstanceEntity;

                count--;
            }
        }
        public async Task WaitAlgoInstanceToStart(string instanceId)
        {
            ClientInstanceEntity instanceDataEntityExists = await ClientInstanceRepository.TryGetAsync(t => t.Id == instanceId && t.PartitionKey != "StoppingEntity") as ClientInstanceEntity;

            Assert.NotNull(instanceDataEntityExists);

            // Wait up to 3 minutes for the algo to be started
            int count = 45;

            while (instanceDataEntityExists.AlgoInstanceStatus != AlgoInstanceStatus.Started && count > 1) // TODO: Update when a health check endpoint is created
            {
                Wait.ForPredefinedTime(5000);                                                              // Wait for five seconds before getting the algo instance data again
                instanceDataEntityExists = await ClientInstanceRepository.TryGetAsync(t => t.Id == instanceId) as ClientInstanceEntity;

                count--;
            }

            Wait.ForPredefinedTime(30000); // Wait for half a minute more so that the deploy can finish successfully
        }
        public async Task AttemptToDeleteActiveAlgoInstance(AlgoInstanceType algoInstanceType)
        {
            // Create an algo
            AlgoDataDTO algoData = await CreateAlgo();

            // Start an instance
            var instanceData = await SaveInstance(algoData, algoInstanceType);

            // Build the body for the request
            var model = new DeleteAlgoInstanceDTO
            {
                AlgoId       = instanceData.AlgoId,
                InstanceId   = instanceData.InstanceId,
                AlgoClientId = instanceData.AlgoClientId
            };

            // Send the request
            var deleteAlgoInstance = await Consumer.ExecuteRequest(ApiPaths.ALGO_STORE_DELETE_INSTANCE,
                                                                   Helpers.EmptyDictionary, JsonUtils.SerializeObject(model), Method.DELETE);

            Assert.That(deleteAlgoInstance.Status, Is.EqualTo(HttpStatusCode.BadRequest));

            // Get instance data from DB
            ClientInstanceEntity instanceDataFromDB = await ClientInstanceRepository.TryGetAsync($"algo_{algoData.Id}", instanceData.InstanceId) as ClientInstanceEntity;

            Assert.That(instanceDataFromDB, Is.Not.Null);

            // Wait up to 3 minutes for the instance to start
            await WaitAlgoInstanceToStart(instanceData.InstanceId);

            // Send the request
            deleteAlgoInstance = await Consumer.ExecuteRequest(ApiPaths.ALGO_STORE_DELETE_INSTANCE,
                                                               Helpers.EmptyDictionary, JsonUtils.SerializeObject(model), Method.DELETE);

            Assert.That(deleteAlgoInstance.Status, Is.EqualTo(HttpStatusCode.BadRequest));

            // Get instance data from DB
            instanceDataFromDB = await ClientInstanceRepository.TryGetAsync($"algo_{algoData.Id}", instanceData.InstanceId) as ClientInstanceEntity;

            Assert.That(instanceDataFromDB, Is.Not.Null);
        }
        public async Task DeleteCreatedInstances()
        {
            foreach (var instanceToDelete in instancesList)
            {
                // Get instance Data
                ClientInstanceEntity instanceDataFromDB = await ClientInstanceRepository.TryGetAsync(t => t.Id == instanceToDelete.InstanceId) as ClientInstanceEntity;

                var authToken = instanceDataFromDB.AuthToken;

                // Delete instance
                await DeleteAlgoInstance(instanceToDelete);

                // Delete instance pod if not already deleted
                var podExists = await InstancePodExists(instanceToDelete.InstanceId, authToken);

                if (podExists)
                {
                    await DeleteInstancePod(instanceToDelete.InstanceId, authToken);
                    await DeleteAlgoInstance(instanceToDelete);
                }
            }
        }
Ejemplo n.º 11
0
        public async Task EditInstanceDataForAlgo()
        {
            UploadStringDTO metadataForUploadedBinary = await UploadStringAlgo();

            string algoID = metadataForUploadedBinary.AlgoId;

            //InstanceDataDTO instanceForAlgo = GetPopulatedInstanceDataDTO.returnInstanceDataDTO(algoID);

            var postInstanceDataResponse = await this.Consumer.ExecuteRequest(algoInstanceDataPath, Helpers.EmptyDictionary, JsonUtils.SerializeObject(instanceForAlgo), Method.POST);

            Assert.That(postInstanceDataResponse.Status, Is.EqualTo(HttpStatusCode.OK));

            InstanceDataDTO postInstanceData = JsonUtils.DeserializeJson <InstanceDataDTO>(postInstanceDataResponse.ResponseJson);

            Assert.That(postInstanceData.AlgoClientId, Is.EqualTo(instanceForAlgo.AlgoClientId));
            Assert.That(postInstanceData.AlgoId, Is.EqualTo(instanceForAlgo.AlgoId));
            Assert.That(postInstanceData.InstanceName, Is.EqualTo(instanceForAlgo.InstanceName));

            ClientInstanceEntity instanceDataEntityExists = await ClientInstanceRepository.TryGetAsync(t => t.Id == postInstanceData.InstanceId) as ClientInstanceEntity;

            Assert.NotNull(instanceDataEntityExists);
            InstanceDataDTO instanceForAlgoEdit = postInstanceData;

            postInstanceData.InstanceName = "EditedTest";

            var postInstanceDataResponseEdit = await this.Consumer.ExecuteRequest(algoInstanceDataPath, Helpers.EmptyDictionary, JsonUtils.SerializeObject(instanceForAlgoEdit), Method.POST);

            Assert.That(postInstanceDataResponseEdit.Status, Is.EqualTo(HttpStatusCode.OK));
            InstanceDataDTO postInstanceDataEdit = JsonUtils.DeserializeJson <InstanceDataDTO>(postInstanceDataResponseEdit.ResponseJson);

            Assert.That(postInstanceDataEdit.AlgoClientId, Is.EqualTo(instanceForAlgo.AlgoClientId));
            Assert.That(postInstanceDataEdit.AlgoId, Is.EqualTo(instanceForAlgo.AlgoId));
            Assert.That(postInstanceDataEdit.InstanceId, Is.EqualTo(instanceDataEntityExists.Id));
            Assert.That(postInstanceDataEdit.InstanceName, Is.EqualTo("EditedTest"));

            ClientInstanceEntity instanceDataEntityExistsEdit = await ClientInstanceRepository.TryGetAsync(t => t.Id == postInstanceDataEdit.InstanceId) as ClientInstanceEntity;

            Assert.NotNull(instanceDataEntityExistsEdit);
        }
Ejemplo n.º 12
0
        public async Task GetInstanceData()
        {
            UploadStringDTO metadataForUploadedBinary = await UploadStringAlgo();

            string algoID = metadataForUploadedBinary.AlgoId;

            //InstanceDataDTO instanceForAlgo = GetPopulatedInstanceDataDTO.returnInstanceDataDTO(algoID);

            var postInstanceDataResponse = await this.Consumer.ExecuteRequest(algoInstanceDataPath, Helpers.EmptyDictionary, JsonUtils.SerializeObject(instanceForAlgo), Method.POST);

            Assert.That(postInstanceDataResponse.Status, Is.EqualTo(HttpStatusCode.OK));

            InstanceDataDTO postInstanceData = JsonUtils.DeserializeJson <InstanceDataDTO>(postInstanceDataResponse.ResponseJson);

            Assert.That(postInstanceData.AlgoClientId, Is.EqualTo(instanceForAlgo.AlgoClientId));
            Assert.That(postInstanceData.AlgoId, Is.EqualTo(instanceForAlgo.AlgoId));
            Assert.That(postInstanceData.InstanceName, Is.EqualTo(instanceForAlgo.InstanceName));

            ClientInstanceEntity instanceDataEntityExists = await ClientInstanceRepository.TryGetAsync(t => t.Id == postInstanceData.InstanceId) as ClientInstanceEntity;

            Assert.NotNull(instanceDataEntityExists);

            Dictionary <string, string> queryParmas = new Dictionary <string, string>()
            {
                { "algoId", algoID },
                { "instanceId", postInstanceData.InstanceId }
            };

            var instanceDataResponse = await this.Consumer.ExecuteRequest(algoInstanceDataPath, queryParmas, null, Method.GET);

            Assert.That(instanceDataResponse.Status, Is.EqualTo(HttpStatusCode.OK));

            InstanceDataDTO returnedClinetInstanceData = JsonUtils.DeserializeJson <InstanceDataDTO>(instanceDataResponse.ResponseJson);

            Assert.That(postInstanceData.AlgoClientId, Is.EqualTo(instanceForAlgo.AlgoClientId));
            Assert.That(postInstanceData.AlgoId, Is.EqualTo(instanceForAlgo.AlgoId));
            Assert.That(postInstanceData.InstanceName, Is.EqualTo(instanceForAlgo.InstanceName));
        }
        public async Task CheckTcBuildData(AlgoInstanceType algoInstanceType)
        {
            // Create an algo
            AlgoDataDTO algoData = await CreateAlgo();

            // Create an instance
            var instanceData = await SaveInstance(algoData, algoInstanceType);

            // Wait up to 3 minutes for the instance to start
            await WaitAlgoInstanceToStart(instanceData.InstanceId);

            // Get the Instance
            ClientInstanceEntity instanceDataFromDB = await ClientInstanceRepository.TryGetAsync($"algo_{algoData.Id}", instanceData.InstanceId) as ClientInstanceEntity;

            Assert.That(instanceDataFromDB, Is.Not.Null);

            // Get TcBuild log
            TcBuildEntity tcBuildDataFromDB = await TcBuildRepository.TryGetAsync("TcBuildEntity", instanceDataFromDB.TcBuildId) as TcBuildEntity;

            Assert.That(tcBuildDataFromDB.InstanceId, Is.EqualTo(instanceDataFromDB.Id));
            Assert.That(tcBuildDataFromDB.ClientId, Is.EqualTo(instanceDataFromDB.ClientId));
            Assert.That(tcBuildDataFromDB.TcBuildId, Is.EqualTo(instanceDataFromDB.TcBuildId));
        }
        public async Task CheckHistoryCandles(AlgoInstanceType algoInstanceType)
        {
            int daysBack = -2;

            // Create an algo
            AlgoDataDTO algoData = await CreateAlgo();

            // Start an instance
            var instanceData = await SaveInstance(algoData, algoInstanceType);

            // Wait up to 3 minutes for the instance to start
            await WaitAlgoInstanceToStart(instanceData.InstanceId);

            // Build History Api candles url
            var historyApiCandlesUrl = $"{BaseUrl.AlgoStoreHistoryApiBaseUrl}{ApiPaths.ALGO_STORE_HISTORY_API_CANDLES}";

            // Build params dictionary
            Dictionary <string, string> historyApiCandlesRequestParams = new Dictionary <string, string>
            {
                { "StartFrom", InstanceDataBuilder.FunctionsDictionary[FunctionType.SMA_Short.ToString()].StartingDate.ToString(GlobalConstants.ISO_8601_DATE_FORMAT) },
                { "EndOn", InstanceDataBuilder.FunctionsDictionary[FunctionType.SMA_Long.ToString()].EndingDate.AddDays(daysBack).ToString(GlobalConstants.ISO_8601_DATE_FORMAT) },
                { "IndicatorName", FunctionType.SMA_Short.ToString() }
            };

            // Get instance data from DB
            ClientInstanceEntity instanceDataFromDB = await ClientInstanceRepository.TryGetAsync(t => t.Id == instanceData.InstanceId) as ClientInstanceEntity;

            // Get instance history candles
            var histiryApiCandles = await Consumer.ExecuteRequestCustomEndpoint(historyApiCandlesUrl, historyApiCandlesRequestParams, null, Method.GET, instanceDataFromDB.AuthToken);

            Assert.That(histiryApiCandles.Status, Is.EqualTo(HttpStatusCode.OK));
            List <CandleDTO> actualResult = JsonUtils.DeserializeJson <List <CandleDTO> >(histiryApiCandles.ResponseJson);

            // Build Api v2 candles url
            var apiV2CandlesUrl = $"{BaseUrl.ApiV2BaseUrl}{ApiPaths.API_V2_CANDLES_HISTORY}";

            // Build params dictionary
            Dictionary <string, string> apiV2CandlesRequestParams = new Dictionary <string, string>
            {
                { "Type", "Spot" },
                { "PriceType", "Mid" },
                { "AssetPairId", InstanceDataBuilder.FunctionsDictionary[FunctionType.SMA_Short.ToString()].AssetPair },
                { "TimeInterval", InstanceDataBuilder.FunctionsDictionary[FunctionType.SMA_Short.ToString()].CandleTimeInterval.ToString() },
                { "FromMoment", InstanceDataBuilder.FunctionsDictionary[FunctionType.SMA_Long.ToString()].StartingDate.ToString(GlobalConstants.ISO_8601_DATE_FORMAT) },
                { "ToMoment", InstanceDataBuilder.FunctionsDictionary[FunctionType.SMA_Long.ToString()].EndingDate.AddDays(daysBack).ToString(GlobalConstants.ISO_8601_DATE_FORMAT) }
            };

            // Get expected history candles
            var apiV2Candles = await Consumer.ExecuteRequestCustomEndpoint(apiV2CandlesUrl, apiV2CandlesRequestParams, null, Method.GET);

            Assert.That(apiV2Candles.Status, Is.EqualTo(HttpStatusCode.OK));
            ApiV2HistoryCandles apiV2HistoryCandles = JsonUtils.DeserializeJson <ApiV2HistoryCandles>(apiV2Candles.ResponseJson);

            // Get expected result
            List <CandleDTO> expectedResult = apiV2HistoryCandles.History.Select(c => new CandleDTO()
            {
                DateTime = c.DateTime,
                Open     = c.Open,
                Close    = c.Close,
                High     = c.High,
                Low      = c.Low
            }).ToList();

            // Sort actual and expected result by DateTime
            actualResult.Sort((x, y) => x.DateTime.CompareTo(y.DateTime));
            expectedResult.Sort((x, y) => x.DateTime.CompareTo(y.DateTime));

            // Assert actual result equals expected result
            Assert.That(actualResult.Count, Is.EqualTo(expectedResult.Count));
            Assert.Multiple(() =>
            {
                for (int i = 0; i < actualResult.Count; i++)
                {
                    Assert.Multiple(() =>
                    {
                        Assert.That(actualResult[i].DateTime, Is.EqualTo(expectedResult[i].DateTime));
                        Assert.That(actualResult[i].Open, Is.EqualTo(expectedResult[i].Open));
                        Assert.That(actualResult[i].Close, Is.EqualTo(expectedResult[i].Close));
                        Assert.That(actualResult[i].High, Is.EqualTo(expectedResult[i].High));
                        Assert.That(actualResult[i].Low, Is.EqualTo(expectedResult[i].Low));
                    });
                }
            });
        }
        public async Task <string> GetInstanceAuthToken(string instanceId)
        {
            ClientInstanceEntity instanceDataFromDB = await ClientInstanceRepository.TryGetAsync(t => t.Id == instanceId) as ClientInstanceEntity;

            return(instanceDataFromDB.AuthToken);
        }
Ejemplo n.º 16
0
        public async Task CheckSummaryRowUpdatedWhenAlogIsStopped()
        {
            ClientInstanceEntity instanceDataEntityExists = await ClientInstanceRepository.TryGetAsync(t => t.Id == postInstanceData.InstanceId) as ClientInstanceEntity;

            Assert.NotNull(instanceDataEntityExists);

            Dictionary <string, string> queryParmas = new Dictionary <string, string>()
            {
                { "algoId", postInstanceData.AlgoId },
                { "instanceId", postInstanceData.InstanceId }
            };

            var instanceDataResponse = await this.Consumer.ExecuteRequest(algoInstanceDataPath, queryParmas, null, Method.GET);

            Assert.That(instanceDataResponse.Status, Is.EqualTo(HttpStatusCode.OK));

            // Get initial statistics values from Azure
            AlgoInstanceStatisticsEntity initialAlgoInstanceStatisticsEntity = await AlgoInstanceStaticsticsRepository.TryGetAsync(t => t.InstanceId == postInstanceData.InstanceId && t.Id == "Summary") as AlgoInstanceStatisticsEntity;

            // Assert LastTradedAssetBalance and LastAssetTwoBalance equal InitialTradedAssetBalance and InitialAssetTwoBalance
            // Before an algo is started, LastTradedAssetBalance and LastAssetTwoBalance should be equal to InitialTradedAssetBalance and InitialAssetTwoBalance
            Assert.That(initialAlgoInstanceStatisticsEntity.InitialTradedAssetBalance, Is.EqualTo(initialAlgoInstanceStatisticsEntity.LastTradedAssetBalance));
            Assert.That(initialAlgoInstanceStatisticsEntity.InitialAssetTwoBalance, Is.EqualTo(initialAlgoInstanceStatisticsEntity.LastAssetTwoBalance));

            // Wait up to 3 minutes for the algo to be started
            await AlgoStoreCommonSteps.WaitAlgoToStart(ClientInstanceRepository, postInstanceData);

            // Get actual values from statistics endpoint 6 times within 1 minute
            StatisticsDTO statistics;

            for (int i = 0; i < 6; i++)
            {
                statistics = await AlgoStoreCommonSteps.GetStatisticsResponseAsync(Consumer, postInstanceData);
            }

            // Wait for 5 seconds before getting statistics values from Azure
            Wait.ForPredefinedTime(5000);

            // Get updated statistics values from Azure
            AlgoInstanceStatisticsEntity updatedAlgoInstanceStatisticsEntity = await AlgoInstanceStaticsticsRepository.TryGetAsync(t => t.InstanceId == postInstanceData.InstanceId && t.Id == "Summary") as AlgoInstanceStatisticsEntity;

            // Assert updated LastTradedAssetBalance and LastAssetTwoBalance does not equal InitialTradedAssetBalance and InitialAssetTwoBalance
            // In algo is not stopped, invoking statistics endpoint should update LastTradedAssetBalance and LastAssetTwoBalance
            Assert.That(updatedAlgoInstanceStatisticsEntity.LastTradedAssetBalance, Is.Not.EqualTo(updatedAlgoInstanceStatisticsEntity.InitialTradedAssetBalance));
            Assert.That(updatedAlgoInstanceStatisticsEntity.LastAssetTwoBalance, Is.Not.EqualTo(updatedAlgoInstanceStatisticsEntity.InitialAssetTwoBalance));
            // Assert InitialTradedAssetBalance and InitialAssetTwoBalance are not updated after invoking statistics endpoint
            Assert.That(updatedAlgoInstanceStatisticsEntity.InitialTradedAssetBalance, Is.EqualTo(initialAlgoInstanceStatisticsEntity.InitialTradedAssetBalance));
            Assert.That(updatedAlgoInstanceStatisticsEntity.InitialAssetTwoBalance, Is.EqualTo(initialAlgoInstanceStatisticsEntity.InitialAssetTwoBalance));

            // Stop the algo instance
            await AlgoStoreCommonSteps.StopAlgoInstance(Consumer, postInstanceData);

            // Get statistics values from Azure after the algo is stopped and before calling statistics endpoint
            AlgoInstanceStatisticsEntity afterStoppingAlgoInstanceStatisticsEntity = await AlgoInstanceStaticsticsRepository.TryGetAsync(t => t.InstanceId == postInstanceData.InstanceId && t.Id == "Summary") as AlgoInstanceStatisticsEntity;

            // Assert after stopping LastTradedAssetBalance and LastAssetTwoBalance does not equal updated LastTradedAssetBalance and LastAssetTwoBalance
            // After stopping an algo, statistics should be updated with the current values
            Assert.That(afterStoppingAlgoInstanceStatisticsEntity.LastTradedAssetBalance, Is.Not.EqualTo(updatedAlgoInstanceStatisticsEntity.LastTradedAssetBalance));
            Assert.That(afterStoppingAlgoInstanceStatisticsEntity.LastAssetTwoBalance, Is.Not.EqualTo(updatedAlgoInstanceStatisticsEntity.LastAssetTwoBalance));
            // Assert InitialTradedAssetBalance and InitialAssetTwoBalance are not updated after stoppong the algo
            Assert.That(afterStoppingAlgoInstanceStatisticsEntity.InitialTradedAssetBalance, Is.EqualTo(initialAlgoInstanceStatisticsEntity.InitialTradedAssetBalance));
            Assert.That(afterStoppingAlgoInstanceStatisticsEntity.InitialAssetTwoBalance, Is.EqualTo(initialAlgoInstanceStatisticsEntity.InitialAssetTwoBalance));

            // Wait for a minute and invoke statistics endpoint again
            statistics = await AlgoStoreCommonSteps.GetStatisticsResponseAsync(Consumer, postInstanceData, 60000);

            // Wait for 5 seconds before getting statistics values from Azure
            Wait.ForPredefinedTime(5000);

            // Get statistics values from Azure after calling statistics endpoint
            AlgoInstanceStatisticsEntity finalAlgoInstanceStatisticsEntity = await AlgoInstanceStaticsticsRepository.TryGetAsync(t => t.InstanceId == postInstanceData.InstanceId && t.Id == "Summary") as AlgoInstanceStatisticsEntity;

            // Assert final LastTradedAssetBalance and LastAssetTwoBalance equal LastTradedAssetBalance and LastAssetTwoBalance from after stopping the algo
            // After stopping an algo, invoking statistics endpoint should not update LastTradedAssetBalance and LastAssetTwoBalance
            Assert.That(finalAlgoInstanceStatisticsEntity.LastTradedAssetBalance, Is.EqualTo(afterStoppingAlgoInstanceStatisticsEntity.LastTradedAssetBalance));
            Assert.That(finalAlgoInstanceStatisticsEntity.LastAssetTwoBalance, Is.EqualTo(afterStoppingAlgoInstanceStatisticsEntity.LastAssetTwoBalance));
            // Assert InitialTradedAssetBalance and InitialAssetTwoBalance are not updated after stoppong the algo
            Assert.That(finalAlgoInstanceStatisticsEntity.InitialTradedAssetBalance, Is.EqualTo(initialAlgoInstanceStatisticsEntity.InitialTradedAssetBalance));
            Assert.That(finalAlgoInstanceStatisticsEntity.InitialAssetTwoBalance, Is.EqualTo(initialAlgoInstanceStatisticsEntity.InitialAssetTwoBalance));
        }
Ejemplo n.º 17
0
        public async Task StopAlgo()
        {
            string url = ApiPaths.ALGO_STORE_METADATA;

            MetaDataDTO metadata = new MetaDataDTO()
            {
                Name        = Helpers.RandomString(13),
                Description = Helpers.RandomString(13)
            };

            var response = await this.Consumer.ExecuteRequest(url, Helpers.EmptyDictionary, JsonUtils.SerializeObject(metadata), Method.POST);

            MetaDataResponseDTO responceMetaData = JsonUtils.DeserializeJson <MetaDataResponseDTO>(response.ResponseJson);

            url = ApiPaths.ALGO_STORE_UPLOAD_STRING;

            UploadStringDTO stringDTO = new UploadStringDTO()
            {
                AlgoId = responceMetaData.Id,
                Data   = this.CSharpAlgoString
            };

            var responsetemp = await this.Consumer.ExecuteRequest(url, Helpers.EmptyDictionary, JsonUtils.SerializeObject(stringDTO), Method.POST);

            Assert.That(responsetemp.Status, Is.EqualTo(HttpStatusCode.NoContent));

            //InstanceDataDTO instanceForAlgo = GetPopulatedInstanceDataDTO.returnInstanceDataDTO(stringDTO.AlgoId);

            url = ApiPaths.ALGO_STORE_ALGO_INSTANCE_DATA;

            var postInstanceDataResponse = await this.Consumer.ExecuteRequest(url, Helpers.EmptyDictionary, JsonUtils.SerializeObject(instanceForAlgo), Method.POST);

            Assert.That(postInstanceDataResponse.Status, Is.EqualTo(HttpStatusCode.OK));

            InstanceDataDTO postInstanceData = JsonUtils.DeserializeJson <InstanceDataDTO>(postInstanceDataResponse.ResponseJson);

            url = ApiPaths.ALGO_STORE_DEPLOY_BINARY;

            DeployBinaryDTO deploy = new DeployBinaryDTO()
            {
                AlgoId     = stringDTO.AlgoId,
                InstanceId = postInstanceData.InstanceId,
            };

            var deployBynaryResponse = await this.Consumer.ExecuteRequest(url, Helpers.EmptyDictionary, JsonUtils.SerializeObject(deploy), Method.POST);

            Assert.That(postInstanceDataResponse.Status, Is.EqualTo(HttpStatusCode.OK));

            url = ApiPaths.ALGO_STORE_ALGO_STOP;
            int retryCounter = 0;

            StopBinaryDTO stopAlgo = new StopBinaryDTO()
            {
                AlgoId     = postInstanceData.AlgoId,
                InstanceId = postInstanceData.InstanceId
            };
            var responceCascadeDelete = await this.Consumer.ExecuteRequest(url, Helpers.EmptyDictionary, JsonUtils.SerializeObject(stopAlgo), Method.POST);

            StopBinaryResponseDTO stopAlgoResponce = JsonUtils.DeserializeJson <StopBinaryResponseDTO>(responceCascadeDelete.ResponseJson);;

            while ((stopAlgoResponce.Status.Equals("Deploying") || stopAlgoResponce.Status.Equals("Started")) && retryCounter <= 30)
            {
                System.Threading.Thread.Sleep(10000);
                responceCascadeDelete = await this.Consumer.ExecuteRequest(url, Helpers.EmptyDictionary, JsonUtils.SerializeObject(stopAlgo), Method.POST);

                stopAlgoResponce = JsonUtils.DeserializeJson <StopBinaryResponseDTO>(responceCascadeDelete.ResponseJson);

                retryCounter++;
            }

            Assert.That(responceCascadeDelete.Status, Is.EqualTo(HttpStatusCode.OK));

            Assert.That(stopAlgoResponce.Status, Is.EqualTo("Stopped"));

            ClientInstanceEntity algoInstanceEntitiy = await ClientInstanceRepository.TryGetAsync(t => t.PartitionKey == "algo_" + stringDTO.AlgoId) as ClientInstanceEntity;

            Assert.That(algoInstanceEntitiy.AlgoInstanceStatusValue, Is.EqualTo("Stopped"));
        }