private void RetrieveCryptoData(IAvapiConnection connection)
        {
            Int_DIGITAL_CURRENCY_DAILY time_series_daily = connection.GetQueryObject_DIGITAL_CURRENCY_DAILY();

            List <CryptoCurrency> stocks = _cryptoRepo.CryptoCurrencies.ToList();

            foreach (var stock in stocks)
            {
                try
                {
                    IAvapiResponse_DIGITAL_CURRENCY_DAILY time_series_dailyResponse = time_series_daily.QueryPrimitive(
                        stock.Ticker,
                        "USD"
                        );

                    Dictionary <DateTime, double> stockData = new Dictionary <DateTime, double>();

                    foreach (var d in time_series_dailyResponse.Data.TimeSeries)
                    {
                        stockData.Add(Convert.ToDateTime(d.DateTime), Convert.ToDouble(d.OpenUSD ?? d.CloseUSD));
                    }

                    string stockDataString = JsonConvert.SerializeObject(stockData);
                    HttpContext.Session.SetString(stock.Ticker, stockDataString);
                }
                catch (Exception e)
                {
                }
            }

            HttpContext.Session.SetString("CryptoDataRetrieved", "true");
        }
Пример #2
0
        public IViewComponentResult Invoke()
        {
            double netWorth = _saveRepo.Accounts.Sum(x => x.Amount);
            netWorth -= _payRepo.PaymentMethods.Where(x => x.IsCredit).Sum(x => x.CreditBalance.Amount);

            IAvapiConnection connection = AvapiConnection.Instance;
            connection.Connect("BXGO930UI9P053HT");

            Int_TIME_SERIES_DAILY time_series_daily = connection.GetQueryObject_TIME_SERIES_DAILY();
            List<Share> shares = _sharesRepo.Shares.ToList();
            List<Stock> stocks = _stockRepo.Stocks.ToList();

            try
            {
                foreach (var stock in stocks)
                {
                    Dictionary<DateTime, double> stockData =
                        JsonConvert.DeserializeObject<Dictionary<DateTime, double>>(HttpContext.Session.GetString(stock.Ticker));

                    var sData = stockData.First();

                    List<Share> listOfShares = shares.Where(e => e.StockID == stock.ID).ToList();
                    int totalShares = listOfShares.Select(e => e.NumOfShares).Sum();
                    netWorth += Convert.ToDouble(sData.Value) * (double)totalShares;
                }
            }
            catch(Exception e)
            {

            }

            Int_DIGITAL_CURRENCY_DAILY crypto_series_daily = connection.GetQueryObject_DIGITAL_CURRENCY_DAILY();

            List<CryptoCurrency> cryptos = _cryptoRepo.CryptoCurrencies.ToList();
            List<Coin> coins = _coinRepo.Coins.ToList();

            try
            {
                foreach (var crypto in cryptos)
                {
                    Dictionary<DateTime, double> cryptoData =
                        JsonConvert.DeserializeObject<Dictionary<DateTime, double>>(HttpContext.Session.GetString(crypto.Ticker));

                    var sData = cryptoData.First();

                    List<Coin> coinList = coins.Where(e => e.CryptoCurrencyID == crypto.ID).ToList();
                    decimal totalCoins = coinList.Select(e => e.NumOfCoins).Sum();
                    netWorth += Convert.ToDouble(sData.Value) * (double)totalCoins;
                }
            }
            catch (Exception e)
            {

            }

            netWorth = Math.Round(netWorth);

            return View(netWorth);
        }
Пример #3
0
        private void testLib()
        {
            // Creating the connection object
            IAvapiConnection connection = AvapiConnection.Instance;

            // Set up the connection and pass the API_KEY provided by alphavantage.co
            connection.Connect(API_KEYS.AVAPI_API_KEY);

            Int_DIGITAL_CURRENCY_DAILY digital_cur_daily = connection.GetQueryObject_DIGITAL_CURRENCY_DAILY();

            IAvapiResponse_DIGITAL_CURRENCY_DAILY apiResponse = digital_cur_daily.QueryPrimitive("BTC", "CNY");


            // Printout the results
            Console.WriteLine("******** RAW DATA TIME_SERIES_DAILY ********");
            Console.WriteLine(apiResponse.RawData);

            /*Console.WriteLine("******** STRUCTURED DATA TIME_SERIES_DAILY ********");
             * var data = apiResponse.Data;
             * if (data.Error)
             * {
             *  Console.WriteLine(data.ErrorMessage);
             * }
             * else
             * {
             *  Console.WriteLine("Information: " + data.MetaData.Information);
             *  Console.WriteLine("Symbol: " + data.MetaData.DigitalCurrencyCode);
             *  Console.WriteLine("LastRefreshed: " + data.MetaData.LastRefreshed);
             *  Console.WriteLine("OutputSize: " + data.MetaData.DigitalCurrencyName);
             *  Console.WriteLine("TimeZone: " + data.MetaData.TimeZone);
             *  Console.WriteLine("========================");
             *  Console.WriteLine("========================");
             *  foreach (var timeseries in data.TimeSeries)
             *  {
             *
             *      Console.WriteLine("DateTime: " + timeseries.DateTime);
             *      Console.WriteLine("========================");
             *  }
             * }*/
        }
        private ChartValues <DateTimePoint> GetData()
        {
            var values = new ChartValues <DateTimePoint>();


            Int_DIGITAL_CURRENCY_DAILY digital_currency_daily =
                _connection.GetQueryObject_DIGITAL_CURRENCY_DAILY();

            try
            {
                IAvapiResponse_DIGITAL_CURRENCY_DAILY digital_currency_dailyResponse =
                    digital_currency_daily.QueryPrimitive(_args.Symbol, _args.DefaultCurrency);

                var data = digital_currency_dailyResponse.Data;
                if (data.Error && _args.DefaultCurrency == "USD")
                {
                    MessageBox.Show("Failed to fetch data", "Error");
                    Read("\\" + _args.Symbol + ".csv");
                    try
                    {
                        return((ChartValues <DateTimePoint>)SeriesCollection[0].Values);
                    }
                    catch
                    {
                    }
                }
                else if (data.Error)
                {
                    digital_currency_dailyResponse = digital_currency_daily.QueryPrimitive(_args.Symbol, "USD");
                    var data1 = digital_currency_dailyResponse.Data;
                    if (data1.Error)
                    {
                        MessageBox.Show("Failed to fetch data", "Error");
                        values = Read("\\" + _args.Symbol + ".csv");
                        try
                        {
                            return((ChartValues <DateTimePoint>)SeriesCollection[0].Values);
                        }
                        catch (Exception)
                        {
                        }
                    }
                    else
                    {
                        try
                        {
                            Int_CURRENCY_EXCHANGE_RATE            currency_exchange_rate         = _connection.GetQueryObject_CURRENCY_EXCHANGE_RATE();
                            IAvapiResponse_CURRENCY_EXCHANGE_RATE currency_exchange_rateResponse =
                                currency_exchange_rate.QueryPrimitive("USD", _args.DefaultCurrency);
                            var data2 = currency_exchange_rateResponse.Data;
                            if (data2.Error)
                            {
                                MessageBox.Show("Failed to fetch exchange rate data for " + _args, "Error");
                            }

                            else
                            {
                                _exchangeRate = double.Parse(data2.ExchangeRate);
                                DateTime offset = DateTime.Now.AddYears(-1);
                                DateTime temp;
                                foreach (var timeseries in data1.TimeSeries)
                                {
                                    temp = DateTime.ParseExact(timeseries.DateTime, "yyyy-MM-dd", CultureInfo.InvariantCulture);
                                    if (temp > offset)
                                    {
                                        values.Add(new DateTimePoint(temp, double.Parse(timeseries.Close) * _exchangeRate));
                                    }
                                }
                                Write("\\" + _args.Symbol + ".csv", values);
                            }
                        }
                        catch (NullReferenceException)
                        {
                            MessageBox.Show("Failed to fetch currency exchange rate for " + _args.DefaultCurrency + " currency. Values will be show in USD", "Error");
                            DateTime offset = DateTime.Now.AddYears(-1);
                            DateTime temp;
                            _exchangeRate = 1;
                            foreach (var timeseries in data1.TimeSeries)
                            {
                                temp = DateTime.ParseExact(timeseries.DateTime, "yyyy-MM-dd", CultureInfo.InvariantCulture);
                                if (temp > offset)
                                {
                                    values.Add(new DateTimePoint(temp, double.Parse(timeseries.Close)));
                                }
                            }
                            YFormatter = val => "$" + val.ToString("0.##");
                            Write("\\" + _args.Symbol + ".csv", values);
                        }
                    }
                }
                else
                {
                    DateTime offset = DateTime.Now.AddYears(-1);
                    DateTime temp;
                    _exchangeRate = 1;
                    foreach (var timeseries in data.TimeSeries)
                    {
                        temp = DateTime.ParseExact(timeseries.DateTime, "yyyy-MM-dd", CultureInfo.InvariantCulture);
                        if (temp > offset)
                        {
                            values.Add(new DateTimePoint(temp, double.Parse(timeseries.Close)));
                        }
                    }
                    Write("\\" + _args.Symbol + ".csv", values);
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                values = Read("\\" + _args.Symbol + ".csv");
            }

            //SeriesCollection[0].Title = data.MetaData.DigitalCurrencyName;
            return(values);
        }