Beispiel #1
0
        private async Task UpdateInstrumentsCache()
        {
            var instruments = (await _assets.AssetPairGetAllAsync())
                              .ToDictionary(
                a => a.Id,
                a => (IAssetPair) new AssetPair(a.Id, a.Name, a.BaseAssetId, a.QuotingAssetId, a.Accuracy));

            _assetPairsCache.InitInstrumentsCache(instruments);
        }
        private async Task FillMarketDataAsync(Dictionary <string, TemporaryAggregatedData> tempDataByLimitOrderAndDtId)
        {
            var newMarketData = new Dictionary <string, IMarketData>();
            var assetPairs    = await _assetsService.AssetPairGetAllAsync();

            var tempDataValues = tempDataByLimitOrderAndDtId.Values.OrderBy(x => x.Dt);

            var marketProfile = await _marketProfileService.ApiMarketProfileGetAsync();

            var assetPairsHash = marketProfile.Select(i => i.AssetPair).ToHashSet();

            foreach (var record in tempDataValues)
            {
                if (record.Volume1 <= 0)
                {
                    continue;
                }

                try
                {
                    var assetPair = FindPairWithAssets(assetPairs, record.Asset1, record.Asset2);
                    if (assetPair == null || !assetPairsHash.Contains(assetPair.Id))
                    {
                        continue;
                    }

                    var isInverted = IsInvertedTarget(assetPair, record.Asset1);
                    var volume     = isInverted ? record.Volume1 : record.Volume2;

                    if (newMarketData.ContainsKey(assetPair.Id))
                    {
                        newMarketData[assetPair.Id].LastPrice = record.Price;
                        newMarketData[assetPair.Id].Volume   += volume;
                    }
                    else
                    {
                        newMarketData.Add(assetPair.Id, new MarketData
                        {
                            AssetPairId = assetPair.Id,
                            LastPrice   = record.Price,
                            Volume      = volume,
                            Dt          = DateTime.UtcNow
                        });
                    }
                }
                catch (Exception ex)
                {
                    _log.WriteError("FillMarketDataAsync", record.ToJson(), ex);
                }
            }

            await _marketDataRepository.AddOrMergeMarketData(newMarketData.Values);
        }
Beispiel #3
0
        public async Task LoadAssetsAsync()
        {
            IList <AssetPair> assetPairs = await _assetsService.AssetPairGetAllAsync();

            foreach (var assetPair in assetPairs)
            {
                if (!_assetPairs.Contains(assetPair.Id, StringComparer.OrdinalIgnoreCase))
                {
                    continue;
                }

                _assetPairsCache.AddOrUpdate(assetPair.Id.ToUpper(), assetPair, (k, a) => assetPair);
            }
        }
Beispiel #4
0
        public async Task ProcessItemsFromYesterdayAsync(DateTime start, Func <IEnumerable <FeedHistoryEntity>, Task> batchHandler)
        {
            try
            {
                var assetPairs = await _assetsService.AssetPairGetAllAsync();

                _lastAssetPairIds = assetPairs
                                    .Select(i => i.Id)
                                    .Where(i => i.IsValidPartitionOrRowKey())
                                    .ToList();
            }
            catch (Exception ex)
            {
                _log.WriteWarning(nameof(CandlestiсksRepository), start, ex.Message);
            }

            if (_lastAssetPairIds == null || _lastAssetPairIds.Count == 0)
            {
                string queryText = GenerateQueryWithoutPartition(start);
                var    query     = new TableQuery <FeedHistoryEntity>().Where(queryText);
                var    items     = await _storage.WhereAsync(query);
                await batchHandler(items);
            }
            else
            {
                foreach (var assetPairId in _lastAssetPairIds)
                {
                    (string pk1, string pk2) = GetPartiotionsForAssetPair(assetPairId);

                    string queryText = GenerateQueryForPartition(start, pk1);
                    var    query     = new TableQuery <FeedHistoryEntity>().Where(queryText);
                    var    items     = await _storage.WhereAsync(query);

                    if (items.Any())
                    {
                        await batchHandler(items);
                    }

                    queryText = GenerateQueryForPartition(start, pk2);
                    query     = new TableQuery <FeedHistoryEntity>().Where(queryText);
                    items     = await _storage.WhereAsync(query);

                    if (items.Any())
                    {
                        await batchHandler(items);
                    }
                }
            }
        }
        public async Task <IActionResult> GetAssetPairRates()
        {
            var assetPairs = await _assetsService.AssetPairGetAllAsync();

            //var assetPairs = await _assetsService.GetAssetsPairsForClient(new Lykke.Service.Assets.Client.Models.GetAssetPairsForClientRequestModel
            //{
            //    ClientId = _requestContext.ClientId,
            //    IsIosDevice = _requestContext.IsIosDevice,
            //    PartnerId = _requestContext.PartnerId
            //});

            var assetPairsDict = assetPairs.ToDictionary(itm => itm.Id);
            var marketProfile  = await _marketProfileService.ApiMarketProfileGetAsync();

            marketProfile = marketProfile.Where(itm => assetPairsDict.ContainsKey(itm.AssetPair)).ToList();
            return(Ok(AssetPairRatesResponseModel.Create(marketProfile.Select(m => m.ConvertToApiModel()).ToArray())));
        }
        public AssetsLocalCache(
            [NotNull] ILogFactory logFactory,
            [NotNull] IAssetsService assetsService,
            [NotNull] RetryPolicySettings retryPolicySettings,
            [NotNull] ExpirationPeriodsSettings expirationPeriodsSettings)
        {
            ILog log = logFactory.CreateLog(this);

            _assetsCache = new CachedDataDictionary <string, Asset>
                           (
                async() =>
            {
                IList <Asset> assets = await Policy
                                       .Handle <Exception>()
                                       .WaitAndRetryAsync(
                    retryPolicySettings.DefaultAttempts,
                    attempt => TimeSpan.FromSeconds(Math.Pow(2, attempt)),
                    (ex, timestamp) => log.Error("Getting assets dictionary with retry", ex))
                                       .ExecuteAsync(() => assetsService.AssetGetAllAsync(true));

                return(assets.ToDictionary(itm => itm.Id));
            }, expirationPeriodsSettings.AssetsCache);

            _assetPairsCache = new CachedDataDictionary <string, AssetPair>(
                async() =>
            {
                IList <AssetPair> assetPairs = await Policy
                                               .Handle <Exception>()
                                               .WaitAndRetryAsync(
                    retryPolicySettings.DefaultAttempts,
                    attempt => TimeSpan.FromSeconds(Math.Pow(2, attempt)),
                    (ex, timestamp) => log.Error("Getting asset pairs dictionary with retry", ex))
                                               .ExecuteAsync(() => assetsService.AssetPairGetAllAsync());

                return(assetPairs.ToDictionary(itm => itm.Id));
            }, expirationPeriodsSettings.AssetsCache);
        }