示例#1
0
        public Ativo CotarPrecoAtivo(string ativoString)
        {
            var    config = System.Configuration.ConfigurationManager.AppSettings;
            string apiKey = config.Get("apikeyAV");
            // Creating the connection object
            IAvapiConnection connection = AvapiConnection.Instance;

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

            // Get the TIME_SERIES_DAILY query object
            Int_TIME_SERIES_DAILY time_series_daily =
                connection.GetQueryObject_TIME_SERIES_DAILY();

            ativoString += ".SA";
            // Perform the TIME_SERIES_DAILY request and get the result
            IAvapiResponse_TIME_SERIES_DAILY time_series_dailyResponse =
                time_series_daily.Query(
                    ativoString, //Work SA prices
                    Const_TIME_SERIES_DAILY.TIME_SERIES_DAILY_outputsize.compact);



            var ativo    = MapeamentoServico.MapearParaAtivo(time_series_dailyResponse.Data.MetaData);
            var cotacoes = MapeamentoServico.MapearParaCotacoes(time_series_dailyResponse.Data.TimeSeries);

            ativo.Cotacoes = cotacoes;

            return(ativo);
        }
        private void RetrieveStockData(IAvapiConnection connection)
        {
            Int_TIME_SERIES_DAILY time_series_daily = connection.GetQueryObject_TIME_SERIES_DAILY();

            List <Stock> stocks = _stockRepo.Stocks.ToList();

            foreach (var stock in stocks)
            {
                try
                {
                    IAvapiResponse_TIME_SERIES_DAILY time_series_dailyResponse = time_series_daily.Query(
                        stock.Ticker,
                        Const_TIME_SERIES_DAILY.TIME_SERIES_DAILY_outputsize.compact
                        );

                    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.open ?? d.close));
                    }

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

            HttpContext.Session.SetString("StockDataRetrieved", "true");
        }
示例#3
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);
        }
示例#4
0
        public IActionResult EditStock(StockFormViewModel model)
        {
            if (ModelState.IsValid)
            {
                Boolean shouldAddToSession = model.Stock.ID == 0;

                _stockRepo.Save(model.Stock);
                TempData["message"] = $"{model.Stock.Company} has been saved";

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

                    Int_TIME_SERIES_DAILY time_series_daily = connection.GetQueryObject_TIME_SERIES_DAILY();

                    try
                    {
                        IAvapiResponse_TIME_SERIES_DAILY time_series_dailyResponse = time_series_daily.Query(
                            model.Stock.Ticker,
                            Const_TIME_SERIES_DAILY.TIME_SERIES_DAILY_outputsize.compact
                            );

                        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.open ?? d.close));
                        }

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

                return(RedirectToAction("Stocks"));
            }
            else
            {
                if (model.Stock.ID == 0)
                {
                    ViewBag.FormTitle = "Create Stock";
                }
                else
                {
                    ViewBag.FormTitle = "Edit Stock";
                }

                model.Sectors = new SelectList(_sectorRepo.Sectors.ToList(), "ID", "Name");
                return(View(model));
            }
        }
示例#5
0
        static void Main()
        {
            // Creating the connection object
            IAvapiConnection connection = AvapiConnection.Instance;

            // Set up the connection and pass the API_KEY provided by alphavantage.co
            connection.Connect("391C9AJGB8WP0FO5");

            // Get the TIME_SERIES_DAILY query object
            Int_TIME_SERIES_DAILY time_series_daily =
                connection.GetQueryObject_TIME_SERIES_DAILY();

            // Perform the TIME_SERIES_DAILY request and get the result
            IAvapiResponse_TIME_SERIES_DAILY time_series_dailyResponse =
                time_series_daily.Query(
                    "SPY",
                    Const_TIME_SERIES_DAILY.TIME_SERIES_DAILY_outputsize.compact,
                    Const_TIME_SERIES_DAILY.TIME_SERIES_DAILY_datatype.json);

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

            Console.WriteLine("******** STRUCTURED DATA TIME_SERIES_DAILY ********");
            var data = time_series_dailyResponse.Data;

            if (data.Error)
            {
                Console.WriteLine(data.ErrorMessage);
            }
            else
            {
                Console.WriteLine("Information: " + data.MetaData.Information);
                Console.WriteLine("Symbol: " + data.MetaData.Symbol);
                Console.WriteLine("LastRefreshed: " + data.MetaData.LastRefreshed);
                Console.WriteLine("OutputSize: " + data.MetaData.OutputSize);
                Console.WriteLine("TimeZone: " + data.MetaData.TimeZone);
                Console.WriteLine("========================");
                Console.WriteLine("========================");
                foreach (var timeseries in data.TimeSeries)
                {
                    Console.WriteLine("open: " + timeseries.open);
                    Console.WriteLine("high: " + timeseries.high);
                    Console.WriteLine("low: " + timeseries.low);
                    Console.WriteLine("close: " + timeseries.close);
                    Console.WriteLine("volume: " + timeseries.volume);
                    Console.WriteLine("DateTime: " + timeseries.DateTime);
                    Console.WriteLine("========================");
                }
            }
        }
示例#6
0
        public void TestQueryAlphaVantageApi()
        {
            // Get the TIME_SERIES_DAILY query object
            Int_TIME_SERIES_DAILY time_series_daily =
                connection.GetQueryObject_TIME_SERIES_DAILY();

            // Perform the TIME_SERIES_DAILY request and get the result
            IAvapiResponse_TIME_SERIES_DAILY time_series_dailyResponse =
                time_series_daily.Query(
                    "MSFT",
                    Const_TIME_SERIES_DAILY.TIME_SERIES_DAILY_outputsize.compact);

            // Printout the results
            Console.WriteLine("******** RAW DATA TIME_SERIES_DAILY ********");
            Console.WriteLine(time_series_dailyResponse.RawData);
        }
示例#7
0
        public string GetPrices()
        {
            string csv = string.Empty;

            try
            {
                IAvapiConnection connection = AvapiConnection.Instance;
                connection.Connect(ApiKey);

                Int_TIME_SERIES_DAILY time_series_daily =
                    connection.GetQueryObject_TIME_SERIES_DAILY();

                IAvapiResponse_TIME_SERIES_DAILY time_series_dailyResponse =
                    time_series_daily.Query(
                        Symbol,
                        Const_TIME_SERIES_DAILY.TIME_SERIES_DAILY_outputsize.compact);

                var data = time_series_dailyResponse.Data;
                if (data.Error)
                {
                    Console.WriteLine(data.ErrorMessage);
                }
                else
                {
                    csv = "open;high;low;close;volume;date" + Environment.NewLine;
                    foreach (var timeseries in data.TimeSeries)
                    {
                        csv += timeseries.open + ";" + timeseries.high + ";" + timeseries.low + ";" + timeseries.close + ";" + timeseries.volume + ";" + timeseries.DateTime + Environment.NewLine;
                    }
                }
            }
            catch (Exception ex)
            {
                csv = "ERROR";
                Console.WriteLine("Get Prices API error" + ex.Message);
            }
            return(csv);
        }
示例#8
0
        private ChartValues <DateTimePoint> GetData()
        {
            var values = new ChartValues <DateTimePoint>();
            Int_TIME_SERIES_DAILY time_series_daily =
                _connection.GetQueryObject_TIME_SERIES_DAILY();

            try
            {
                IAvapiResponse_TIME_SERIES_DAILY time_series_dailyResponse =
                    time_series_daily.Query(
                        _args.Symbol,
                        Const_TIME_SERIES_DAILY.TIME_SERIES_DAILY_outputsize.compact);


                var data = time_series_dailyResponse.Data;
                if (data.Error)
                {
                    MessageBox.Show("Failed to fetch data", "Error");
                    SeriesCollection[0].Values = Read("\\init.csv");
                    return((ChartValues <DateTimePoint>)SeriesCollection[0].Values);
                }
                else
                {
                    if (_args.DefaultCurrency != "USD")
                    {
                        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 data", "Error");
                            }
                            else
                            {
                                _exchangeRate = double.Parse(data2.ExchangeRate);

                                foreach (var timeseries in data.TimeSeries)
                                {
                                    values.Add(new DateTimePoint(DateTime.ParseExact(timeseries.DateTime, "yyyy-MM-dd", CultureInfo.InvariantCulture),
                                                                 double.Parse(timeseries.close) * _exchangeRate));
                                }
                                Write("\\init.csv", values);
                            }
                        }
                        catch (NullReferenceException)
                        {
                            MessageBox.Show("Failed to fetch currency exchange rate for chosen currency. Values will be show in USD", "Error");
                            _exchangeRate = 1;
                            foreach (var timeseries in data.TimeSeries)
                            {
                                values.Add(new DateTimePoint(DateTime.ParseExact(timeseries.DateTime, "yyyy-MM-dd", CultureInfo.InvariantCulture),
                                                             double.Parse(timeseries.close) * _exchangeRate));
                            }
                            YFormatter = val => "$" + val.ToString("0.##");
                            Write("\\init.csv", values);
                        }
                    }
                    else
                    {
                        _exchangeRate = 1;
                        foreach (var timeseries in data.TimeSeries)
                        {
                            values.Add(new DateTimePoint(DateTime.ParseExact(timeseries.DateTime, "yyyy-MM-dd", CultureInfo.InvariantCulture),
                                                         double.Parse(timeseries.close) * _exchangeRate));
                        }
                        Write("\\init.csv", values);
                    }
                }
            }
            catch (NullReferenceException)
            {
                MessageBox.Show("Suvise se upita salje serveru za krato vreme, misli da smo spameri. Oladi malo! Prikazuju se podaci koji su dostupni u skladstu podataka.", "Error");
                SeriesCollection[0].Values = Read("\\init.csv");
                return((ChartValues <DateTimePoint>)SeriesCollection[0].Values);
            }

            return(values);
        }