public async Task Trade() { var stopwatch = Stopwatch.StartNew(); _logger.LogInformation("Start trading"); var balance = await _stockExchange.GetBalance(); // var baseCurrency = GetBaseCurrency(balance); var baseCurrency = "BTC"; if (baseCurrency == null) { _logger.LogWarning("No money!"); return; } _logger.LogInformation($"Balance is {balance.Total[baseCurrency]} {baseCurrency}"); var currencyPairs = (await _stockExchange.GetCurrencyPairs()).ToArray(); var availablePairs = currencyPairs .Where(cp => cp.BaseCurrency == baseCurrency || cp.TargetCurrency == baseCurrency) .Select(cp => cp.BaseCurrency == baseCurrency ? cp.TargetCurrency : cp.BaseCurrency) .ToHashSet(); var usdPairs = FilterPairsByCurrency(currencyPairs, "USD").ToArray(); _logger.LogInformation("USD pairs are: {@Pairs}", usdPairs); var actualToUsdRates = (await _stockExchange.GetActualRatesAsync(usdPairs, DateTime.UtcNow.AddSeconds(-60))) .ToDictionary( x => x.BaseCurrency == "USD" ? x.TargetCurrency : x.BaseCurrency, x => x.BaseCurrency == "USD" ? x.AverageRate != 0 ? 1 / x.AverageRate : 0 : x.AverageRate); // var actualToBaseRates = (await _stockExchange.GetActualRatesAsync(basePairs, DateTime.UtcNow.AddSeconds(-60))) // .Where(x => x.AverageRate != 0) // .ToDictionary( // x => x.BaseCurrency == baseCurrency ? x.TargetCurrency : x.BaseCurrency, // x => x.BaseCurrency == baseCurrency ? 1 / x.AverageRate : x.AverageRate); _logger.LogInformation("Current rates: {@Rates}", actualToUsdRates); var previousToUsdRates = _currencyRatesStore.GetLast(); if (previousToUsdRates != null) { var predictedToUsdRates = actualToUsdRates.ToDictionary( x => x.Key, x => CalculatePredictedRate(previousToUsdRates[x.Key], x.Value)); var(recommendedCurrency, conversionType) = GetRecommendedCurrency(availablePairs, actualToUsdRates, predictedToUsdRates, baseCurrency); if (recommendedCurrency == baseCurrency) { _logger.LogCritical("NO CONVERSION"); } else { _logger.LogCritical($"CONVERT FROM '{baseCurrency}' TO {recommendedCurrency}"); } _logger.LogInformation("Recommended conversion: {From} - {To}", baseCurrency, recommendedCurrency); } _currencyRatesStore.Store(actualToUsdRates); }