Beispiel #1
0
        private async Task RebuildTopAssets()
        {
            _log.Info("Rebuild top asset....");

            var settings = Settings;

            // Get top 100 market caps
            List <AssetMarketCap> allMarketCaps;

            lock (_sync)
                allMarketCaps = _allMarketCaps.ToList();

            // Get white list supplies
            var whiteListSupplies = new Dictionary <string, decimal>();

            settings.Assets.ForEach(x => whiteListSupplies.Add(x, allMarketCaps.Single(mk => mk.Asset == x).CirculatingSupply));

            // Get white list prices
            var sources              = settings.Sources.ToList();
            var whiteListAssets      = whiteListSupplies.Select(x => x.Key).ToList();
            var assetsPrices         = _tickPricesService.GetAssetPrices(sources);
            var assetsSettings       = settings.AssetsSettings;
            var whiteListUsingPrices = GetAssetsUsingPrices(whiteListAssets, assetsPrices, assetsSettings);

            var areAllPricesPresent = await ArePricesPresentForAllAssets(whiteListAssets, whiteListUsingPrices);

            if (!areAllPricesPresent)
            {
                return;
            }

            // Calculate white list market caps
            var whiteListMarketCaps = CalculateMarketCaps(whiteListAssets, whiteListSupplies, whiteListUsingPrices);

            // Calculate white list weights
            var whiteListWeights = CalculateWeightsOrderedByDesc(whiteListMarketCaps);

            // Get top weights
            var topWeights = whiteListWeights
                             .Take(Settings.TopCount)
                             .ToDictionary();

            lock (_sync)
            {
                // Refresh top assets
                _topAssets.Clear();
                _topAssets.AddRange(topWeights.Keys);

                _lastRebuild = DateTime.UtcNow.Date;

                _rebuildNeeded = false;

                _log.Info($"Finished rebuilding top assets, count - {_topAssets.Count}.");
            }
        }
        public async Task <IReadOnlyList <string> > GetSourcesAsync()
        {
            var prices = _tickPricesService.GetAssetPrices();

            var result = prices.SelectMany(x => x.Value)
                         .Select(x => x.Source)
                         .Distinct()
                         .OrderBy(x => x).ToList();

            return(result);
        }
        public async Task <IReadOnlyList <AssetInfo> > GetAllAsync()
        {
            var settings = await _settingsService.GetAsync();

            var marketCaps = _indexCalculator.GetAllAssetsMarketCaps();
            var prices     = _tickPricesService.GetAssetPrices(settings.Sources.ToList());

            var result = new List <AssetInfo>();

            foreach (var asset in settings.Assets)
            {
                if (!marketCaps.ContainsKey(asset))
                {
                    continue;
                }

                var marketCap = marketCaps[asset];

                if (!prices.ContainsKey(asset))
                {
                    continue;
                }

                var allAssetPrices = prices[asset];

                var sources = allAssetPrices.Select(x => x.Source).Distinct().ToList();

                var assetPrices = new Dictionary <string, decimal>();

                foreach (var source in sources)
                {
                    var assetSourcePrices = allAssetPrices.Where(x => x.Source == source).ToList();

                    var middlePrice = Utils.GetMiddlePrice(asset, assetSourcePrices);

                    assetPrices.Add(source, middlePrice);
                }

                var assetInfo = new AssetInfo
                {
                    Asset     = asset,
                    MarketCap = marketCap,
                    Prices    = assetPrices
                };

                result.Add(assetInfo);
            }

            return(result);
        }