Beispiel #1
0
        private async Task <List <TradeSummaryData> > GetOrCacheBaseSummary(string baseCurrency)
        {
            var cacheResult = await CacheService.GetOrSetHybridAsync(CacheKey.ExchangeSummary(baseCurrency), TimeSpan.FromMinutes(5), async() =>
            {
                var tradeHistory     = await GetOrCacheTradeHistory().ConfigureAwait(false);
                var tradePairs       = await TradePairReader.GetTradePairs().ConfigureAwait(false);
                var basepairs        = tradePairs.Where(x => x.BaseSymbol == baseCurrency).ToList();
                var ids              = basepairs.Select(x => x.TradePairId);
                var tradeHistoryData = tradeHistory.Where(tp => ids.Contains(tp.TradePairId)).ToList();

                var results = new List <TradeSummaryData>();
                foreach (var x in basepairs)
                {
                    var data = tradeHistoryData.Where(t => t.TradePairId == x.TradePairId);
                    results.Add(new TradeSummaryData
                    {
                        CurrencyId  = x.CurrencyId,
                        TradePairId = x.TradePairId,
                        Name        = x.Name,
                        Symbol      = x.Symbol,
                        Change      = GetChangePercent(data.FirstOrDefault()?.Rate ?? x.LastTrade, x.LastTrade).ToString("F2"),
                        BaseVolume  = data.Sum(t => t.Amount *t.Rate).ToString("F8"),
                        Volume      = data.Sum(t => t.Amount).ToString("F8"),
                        Low         = (data.OrderBy(t => t.Rate).FirstOrDefault()?.Rate ?? x.LastTrade).ToString("F8"),
                        High        = (data.OrderByDescending(t => t.Rate).FirstOrDefault()?.Rate ?? x.LastTrade).ToString("F8"),
                        Last        = x.LastTrade.ToString("F8"),
                        BaseSymbol  = x.BaseSymbol
                    });
                }

                return(results.ToList());
            }).ConfigureAwait(false);

            return(cacheResult);
        }
Beispiel #2
0
        public async Task <ActionResult> Index()
        {
            var tradePairs = await TradePairReader.GetTradePairs();

            var balances = await BalanceReader.GetBalances(User.Id());

            return(View(new FaucetViewModel
            {
                TradePairs = new List <TradePairModel>(tradePairs),
                Balances = new List <BalanceModel>(balances),
            }));
        }
Beispiel #3
0
        public async Task <ActionResult> Index(string area = "Deposit", string coin = null)
        {
            var tradePairs = await TradePairReader.GetTradePairs();

            var balances = await BalanceReader.GetBalances(User.Id());

            return(View(new HistoryViewModel
            {
                Currency = coin,
                Section = area,
                TradePairs = new List <TradePairModel>(tradePairs),
                Balances = new List <BalanceModel>(balances)
            }));
        }
        public async Task <ActionResult> Index()
        {
            var tradePairs = await TradePairReader.GetTradePairs();

            var balances = await BalanceReader.GetBalances(User.Id());

            var currencyStatus = await CurrencyReader.GetCurrencyStatus();

            return(View(new StatusViewModel
            {
                TradePairs = new List <TradePairModel>(tradePairs),
                Balances = new List <BalanceModel>(balances),
                CurrencyStatus = new List <CurrencyStatusModel>(currencyStatus)
            }));
        }
        public async Task <ActionResult> Index(string section = "Account")
        {
            ViewBag.Section = section;
            var tradePairs = await TradePairReader.GetTradePairs();

            var balances = await BalanceReader.GetBalances(User.Id());

            return(View(new UserSettingsModel
            {
                SecurityModel = new UserSecurityModel(),
                UserProfile = new UserProfileModel(),
                TradePairs = new List <TradePairModel>(tradePairs),
                Balances = new List <BalanceModel>(balances)
            }));
        }
        public async Task <ApiTradePairResult> GetTradePairs()
        {
            var tradepairs = await TradePairReader.GetTradePairs().ConfigureAwait(false);

            if (!tradepairs.Any())
            {
                return new ApiTradePairResult {
                           Error = "No currencies found", Success = true,
                }
            }
            ;

            var results = tradepairs.Select(x => new ApiTradePair
            {
                BaseCurrency     = x.BaseName,
                BaseSymbol       = x.BaseSymbol,
                Currency         = x.Name,
                Label            = x.Market.Replace('_', '/'),
                MaximumBaseTrade = 100000000m,
                MaximumPrice     = 100000000m,
                MaximumTrade     = 100000000m,
                MinimumBaseTrade = x.BaseMinTrade,
                MinimumPrice     = 0.00000001m,
                MinimumTrade     = 0.00000001m,
                Status           = x.Status.ToString(),
                StatusMessage    = x.StatusMessage,
                Symbol           = x.Symbol,
                Id       = x.TradePairId,
                TradeFee = x.BaseFee
            });

            return(new ApiTradePairResult
            {
                Success = true,
                Data = new List <ApiTradePair>(results)
            });
        }
Beispiel #7
0
        public async Task <ExchangeSummary> GetExchangeSummary()
        {
            var cacheResult = await CacheService.GetOrSetHybridAsync(CacheKey.ExchangeSummary(), TimeSpan.FromSeconds(20), async() =>
            {
                var tradeHistoryData = await GetOrCacheTradeHistory().ConfigureAwait(false);
                var tradePairs       = await TradePairReader.GetTradePairs().ConfigureAwait(false);
                var currencies       = await CurrencyReader.GetCurrencies().ConfigureAwait(false);
                var baseCurrencies   = await CurrencyReader.GetBaseCurrencies().ConfigureAwait(false);
                var summaryData      = new List <ExchangeSummaryModel>();

                foreach (var x in tradePairs)
                {
                    var data = tradeHistoryData.Where(t => t.TradePairId == x.TradePairId);
                    summaryData.Add(new ExchangeSummaryModel
                    {
                        TradePairId = x.TradePairId,
                        Symbol      = x.Symbol,
                        BaseSymbol  = x.BaseSymbol,
                        TotalTrades = data.Count(),
                        Low         = (data.OrderBy(t => t.Rate).FirstOrDefault()?.Rate ?? x.LastTrade),
                        High        = (data.OrderByDescending(t => t.Rate).FirstOrDefault()?.Rate ?? x.LastTrade),
                        Volume      = data.Sum(t => t.Amount),
                        TotalBase   = data.Sum(t => t.Amount *t.Rate),
                        Change      = GetChangePercent(data.FirstOrDefault()?.Rate ?? x.LastTrade, x.LastTrade)
                    });
                }

                var now         = DateTime.UtcNow;
                var baseData    = summaryData.GroupBy(x => x.BaseSymbol);
                var totalVolume = baseData.Select(x => new { Symbol = x.Key, Total = x.Sum(u => u.TotalBase) }).ToList();
                var topMarket   = baseData.Select(x => x.MaxBy(u => u.TotalBase)).OrderBy(x => x.BaseSymbol).ToList();
                var featured    = currencies.Any(x => x.FeaturedExpires > now)
                                        ? currencies.Where(x => x.FeaturedExpires > now).ToList()
                                        : currencies.Where(x => x.CurrencyId == Constant.DOTCOIN_ID).ToList();

                var baseCurrencyData = new List <ExchangeSummaryBaseCurrency>();
                foreach (var currency in baseCurrencies)
                {
                    var totalVol = totalVolume.FirstOrDefault(x => x.Symbol == currency.Symbol);
                    baseCurrencyData.Add(new ExchangeSummaryBaseCurrency
                    {
                        CurrencyId  = currency.CurrencyId,
                        Name        = currency.Name,
                        Symbol      = currency.Symbol,
                        TotalVolume = totalVol != null
                                                        ? totalVol.Total
                                                        : 0,
                        Rank = currency.Rank
                    });
                }

                return(new ExchangeSummary
                {
                    TopMarkets = topMarket,
                    BaseCurrencies = baseCurrencyData,
                    TotalMarkets = tradePairs.Count,
                    TotalTrades = summaryData.Sum(x => x.TotalTrades),
                    Featured = featured.Select(x => new FeaturedCurrency
                    {
                        Name = x.Name,
                        Symbol = x.Symbol,
                        Website = x.Website,
                        Summary = x.Summary
                    }).ToList()
                });
            }).ConfigureAwait(false);

            return(cacheResult);
        }
        public async Task <ApiMarketsResult> GetMarkets(string baseMarket, int hours)
        {
            hours = Math.Min(hours, 168);
            var cacheResult = await CacheService.GetOrSetHybridAsync(CacheKey.ApiMarkets(baseMarket, hours), TimeSpan.FromSeconds(20), async() =>
            {
                var lastTime   = DateTime.UtcNow.AddHours(-hours);
                var tradePairs = await TradePairReader.GetTradePairs().ConfigureAwait(false);
                if (!tradePairs.Any())
                {
                    return new ApiMarketsResult {
                        Error = $"Markets not found", Success = true,
                    }
                }
                ;

                var markets = string.IsNullOrEmpty(baseMarket) ? tradePairs : tradePairs.Where(x => x.BaseSymbol == baseMarket).ToList();
                using (var context = ExchangeDataContextFactory.CreateReadOnlyContext())
                {
                    var allTrades = await context.Trade
                                    .AsNoTracking()
                                    .Where(x => (x.Status == Enums.TradeStatus.Partial || x.Status == Enums.TradeStatus.Pending))
                                    .Select(x => new
                    {
                        TradePairId = x.TradePairId,
                        Type        = x.Type,
                        Rate        = x.Rate,
                        Amount      = x.Amount
                    }).ToListNoLockAsync().ConfigureAwait(false);

                    var allTradeHistory = await context.TradeHistory
                                          .AsNoTracking()
                                          .Where(x => x.Timestamp > lastTime)
                                          .Select(x => new
                    {
                        Id          = x.Id,
                        TradePairId = x.TradePairId,
                        Rate        = x.Rate,
                        Amount      = x.Amount
                    })
                                          .OrderBy(x => x.Id)
                                          .ToListNoLockAsync().ConfigureAwait(false);

                    var results = new ConcurrentBag <ApiMarket>();
                    foreach (var tradePair in markets)
                    {
                        var trades = allTrades
                                     .Where(x => x.TradePairId == tradePair.TradePairId)
                                     .Select(x => new
                        {
                            Type   = x.Type,
                            Rate   = x.Rate,
                            Amount = x.Amount
                        }).ToList();

                        var tradeHistory = allTradeHistory
                                           .Where(x => x.TradePairId == tradePair.TradePairId)
                                           .Select(x => new
                        {
                            Id     = x.Id,
                            Rate   = x.Rate,
                            Amount = x.Amount
                        })
                                           .OrderBy(x => x.Id)
                                           .ToList();

                        var ask            = 0m;
                        var bid            = 0m;
                        var buyVolume      = 0m;
                        var sellVolume     = 0m;
                        var buyBaseVolume  = 0m;
                        var sellBaseVolume = 0m;
                        if (trades.Any())
                        {
                            var sellOrders = trades.Where(x => x.Type == Enums.TradeHistoryType.Sell).OrderBy(x => x.Rate).ToList();
                            if (sellOrders.Any())
                            {
                                ask            = sellOrders.First().Rate;
                                sellVolume     = Math.Round(sellOrders.Sum(x => x.Amount), 8);
                                sellBaseVolume = Math.Round(sellOrders.Sum(x => x.Amount *x.Rate), 8);
                            }
                            var buyOrders = trades.Where(x => x.Type == Enums.TradeHistoryType.Buy).OrderByDescending(x => x.Rate).ToList();
                            if (buyOrders.Any())
                            {
                                bid           = buyOrders.First().Rate;
                                buyVolume     = Math.Round(buyOrders.Sum(x => x.Amount), 8);
                                buyBaseVolume = Math.Round(buyOrders.Sum(x => x.Amount *x.Rate), 8);
                            }
                        }

                        var change     = 0m;
                        var volume     = 0m;
                        var baseVolume = 0m;
                        var open       = tradePair.LastTrade;
                        var close      = tradePair.LastTrade;
                        var high       = tradePair.LastTrade;
                        var low        = tradePair.LastTrade;
                        var last       = tradePair.LastTrade;
                        if (tradeHistory.Any())
                        {
                            var firstTrade = tradeHistory.FirstOrDefault();
                            var lastTrade  = tradeHistory.LastOrDefault();

                            high       = tradeHistory.Max(x => x.Rate);
                            low        = tradeHistory.Min(x => x.Rate);
                            open       = firstTrade.Rate;
                            close      = lastTrade.Rate;
                            last       = lastTrade.Rate;
                            change     = open.GetPercentChanged(close);
                            volume     = tradeHistory.Sum(x => x.Amount);
                            baseVolume = Math.Round(tradeHistory.Sum(x => x.Amount *x.Rate), 8);
                        }

                        results.Add(new ApiMarket
                        {
                            Label       = tradePair.Market.Replace("_", "/"),
                            TradePairId = tradePair.TradePairId,
                            Change      = open.GetPercentChanged(close),
                            AskPrice    = ask,
                            BidPrice    = bid,
                            BuyVolume   = buyVolume,
                            SellVolume  = sellVolume,
                            LastPrice   = last,
                            High        = high,
                            Low         = low,
                            Volume      = volume,

                            Open           = open,
                            Close          = close,
                            BaseVolume     = baseVolume,
                            BuyBaseVolume  = buyBaseVolume,
                            SellBaseVolume = sellBaseVolume
                        });
                    }

                    return(new ApiMarketsResult
                    {
                        Success = true,
                        Data = results.OrderBy(x => x.Label).ToList()
                    });
                }
            });