Esempio n. 1
0
        /// <summary>
        /// Finds the right Currency by the official naming. The most API's should match ISO 4217. If not it is possible to add another alternative key here
        /// </summary>
        /// <param name="key">The currency naming in three letters most likely ISO 4217 e.g. USD, EUR, CNY</param>
        /// <returns>The currency which matches the three letter naming</returns>
        /// <exception cref="CurrencyNotSupportedException"></exception>
        public CurrencyBase GetCurrency(String key)
        {
            key = key.ToUpperInvariant();
            // I have implemented two keys, because crypto currencies are not official yet and there are some inconsistencies e.g. Kraken (XBT) differs from Bitfinex (BTC)
            var currency = SupportedCurrencies.FirstOrDefault(x => x.Key == key || x.AlternativeKey == key);

            if (default(CurrencyBase) == currency)
            {
                throw new CurrencyNotSupportedException {
                          CurrencyKey = key
                };
            }
            return(currency);
        }
        public SupportedCurrency GetCurrentCurrency()
        {
            // Check if the custom currency header is present
            var isHeaderPresent = httpContextAccessor.HttpContext.Request.Headers.TryGetValue(HEADER_CURRENCY, out StringValues stringValues);

            // Check if the currency is supported
            if (isHeaderPresent && SupportedCurrencies.Any(x => x.Code == stringValues.FirstOrDefault()))
            {
                return(SupportedCurrencies.FirstOrDefault(x => x.Code == stringValues.FirstOrDefault()));
            }
            else
            {
                return(SupportedCurrencies
                       .Where(x => x.IsDefault)
                       .FirstOrDefault());
            }
        }
Esempio n. 3
0
        /*public double GetEarningsInUsd(ICurrency currency)
         * {
         *  Dictionary<DateTime, Dictionary<ICurrency, double>> balanceHistory = BalanceHistory;
         *
         *
         *  double balance = 0.0;
         *  double previousBalance = 0.0;
         *  foreach (KeyValuePair<DateTime, Dictionary<ICurrency, double>> balanceHistoricalEntry in balanceHistory)
         *  {
         *      if (!balanceHistoricalEntry.Value.ContainsKey(currency))
         *      {
         *        continue;
         *      }
         *
         *      IDictionary<ICurrency, double> entry = balanceHistoricalEntry.Value;
         *      if (Math.Abs(previousBalance - entry[currency]) > double.Epsilon)
         *      {
         *          IExchangeRate exchangeRate = _exchangeRates.First(exrat => exrat.ReferenceCurrency == currency);
         *          double price = exchangeRate.GetPriceInUsd(balanceHistoricalEntry.Key, currency);
         *
         *
         *      }
         *      previousBalance = balanceEntry[currency];
         *  }
         * }*/
        public void RefreshHoldings()
        {
            AccountInfo accountInfo = BinanceDataPool.GetAccountInfo();

            Dictionary <ICurrency, double> balances = new Dictionary <ICurrency, double>();

            foreach (AccountInfo.Balance balance in accountInfo.balances)
            {
                double balanceD = double.Parse(balance.free);

                ICurrency currency = SupportedCurrencies.FirstOrDefault(curr => curr.Symbol == balance.asset);
                if (currency != null)
                {
                    balances.Add(currency, balanceD);
                }
            }

            Dictionary <DateTime, Dictionary <ICurrency, double> > balanceHistory = _balanceHistory.Where(pair => pair.Key > DateTime.Now - MaxHistory)
                                                                                    .ToDictionary(pair => pair.Key, pair => pair.Value);

            balanceHistory.Add(DateTime.Now, balances);
            _balanceHistory = balanceHistory;
        }