Exemplo n.º 1
0
        public static List <CryptoCoin> UpdateCoinsCurrentPrice(List <CryptoCoin> coins, List <MarketCoin> apiFetchedCoins, Dictionary <int, HistoricCoinPrice> historicPrices, bool useCombinedDisplay = true, Types.CoinCurrency currency = Types.CoinCurrency.USD)
        {
            MarketCoin btcCoin = apiFetchedCoins.FirstOrDefault(x => x.Symbol == "BTC");
            MarketCoin ethCoin = apiFetchedCoins.FirstOrDefault(x => x.Symbol == "ETH");

            if (btcCoin == null || ethCoin == null)
            {
                return(coins);
            }

            coins = CorrectCryptoCoinSymbols(coins);

            if (useCombinedDisplay)
            {
                coins = ToCoinsCombinedMode(coins);
            }

            foreach (CryptoCoin coin in coins)
            {
                string[] supportedSymbolPrefix = { "BTC-", "ETH-", "USDT-", "USD-" };
                if (!supportedSymbolPrefix.Any(x => coin.Symbol.StartsWith(x)))
                {
                    continue;
                }                                                                             // We do not support anything other than BTC, ETH, USD

                string[] symbol = coin.Symbol.Contains("-") ? coin.Symbol.Split('-') : new[] { "", coin.Symbol };

                coin.CurrentUSDPriceOfBTC = btcCoin.CurrentSymbolPriceUSD;
                coin.CurrentUSDPriceOfETH = ethCoin.CurrentSymbolPriceUSD;

                if (coin.OrderDate != DateTime.MinValue)
                {
                    coin.OrderedDateUSDPriceOfBTC = GetPriceOfETHorBTCOnSpecificDate(historicPrices, coin.OrderDate.ToEpochDayAt12am(), Types.CoinCurrency.BTC);
                    if (currency == Types.CoinCurrency.ETH)
                    {
                        coin.OrderedDateUSDPriceOfETH = GetPriceOfETHorBTCOnSpecificDate(historicPrices, coin.OrderDate.ToEpochDayAt12am(), Types.CoinCurrency.ETH);
                        if (coin.OrderedDateUSDPriceOfETH > 0)
                        {
                            coin.GeneratedPricePerUnitETH = (coin.TotalPricePaidUSD.GetValueOrDefault() / coin.Shares) / coin.OrderedDateUSDPriceOfETH;
                        }
                    }
                }

                MarketCoin fetchedCoin = apiFetchedCoins.FirstOrDefault(x => x.Symbol.Contains(symbol[1]));
                if (fetchedCoin == null)
                {
                    continue;
                }

                coin.MarketCoin.CurrentSymbolPriceBTC = fetchedCoin.CurrentSymbolPriceBTC;
                coin.MarketCoin.CurrentSymbolPriceUSD = fetchedCoin.CurrentSymbolPriceUSD;
                coin.MarketCoin.Name            = fetchedCoin.Name;
                coin.MarketCoin.CoinMarketCapID = fetchedCoin.CoinMarketCapID;
            }

            return(coins);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Gets the latest price of Coin Currency.  For example, if we want the current price of BTC, we pass in BTC as coin currency.
        /// </summary>
        public static decimal GetLatestPriceOfSymbol(string symbol, List <MarketCoin> marketCoins, Types.CoinCurrency currency = Types.CoinCurrency.USD)
        {
            MarketCoin marketCoin = marketCoins.FirstOrDefault(x => x.Symbol == symbol.Split('-')[1].ToUpperInvariant());

            if (marketCoin == null)
            {
                return(0);
            }

            if (currency == Types.CoinCurrency.USD)
            {
                return(marketCoin.CurrentSymbolPriceUSD);
            }
            if (currency == Types.CoinCurrency.BTC)
            {
                return(marketCoin.CurrentSymbolPriceBTC);
            }
            if (currency == Types.CoinCurrency.ETH)
            {
                MarketCoin ethCoin = marketCoins.FirstOrDefault(x => x.Symbol == "ETH");
                return(marketCoin.CurrentSymbolPriceUSD / ethCoin.CurrentSymbolPriceUSD);
            }
            return(0);
        }