public async System.Threading.Tasks.Task <List <DataBar> > GetHistoricalDataAsync(SecurityType securityType, string ticker, BarSize barSize, DateTime?endDate = null)
        {
            var             client = new AlphaVantageStocksClient(Constants.AlphaVantageApiKey);
            var             HistoricalBarCollection = new List <DataBar>();
            StockTimeSeries timeSeries;

            switch (barSize)
            {
            case BarSize.DAY:
                timeSeries = await client.RequestDailyTimeSeriesAsync(ticker, TimeSeriesSize.Full, adjusted : false);

                break;

            case BarSize.WEEK:
                timeSeries = await client.RequestWeeklyTimeSeriesAsync(ticker, adjusted : false);

                break;

            case BarSize.MONTH:
                timeSeries = await client.RequestMonthlyTimeSeriesAsync(ticker, adjusted : false);

                break;

            default:
                timeSeries = await client.RequestIntradayTimeSeriesAsync(ticker, (IntradayInterval)barSize, TimeSeriesSize.Full);

                break;
            }

            ((List <StockDataPoint>)timeSeries.DataPoints).ForEach(bar => HistoricalBarCollection.Add(new DataBar(bar)));
            return(HistoricalBarCollection);
        }
Example #2
0
        public async Task <List <KeyValuePair <string, double> > > GetStockPrices(List <KeyValuePair <string, int> > stocks)
        {
            string apiKey = "O8UW3NE3Z3CEYSVB"; // API KEY

            var client = new AlphaVantageStocksClient(apiKey);


            //This list will contain the stocks' symbols and the prices
            List <KeyValuePair <string, double> > stocksAndPrice = new List <KeyValuePair <string, double> >();

            string[] stockSymbols = new string[stocks.Count];

            for (int i = 0; i < stocks.Count; i++)
            {
                stockSymbols[i] = stocks[i].Key;
            }


            // retrieve stocks batch quotes for Apple Inc. and Facebook Inc.:
            ICollection <StockQuote> batchQuotes = await client.RequestBatchQuotesAsync(stockSymbols);

            foreach (var stockQuote in batchQuotes)
            {
                stocksAndPrice.Add(new KeyValuePair <string, double>(stockQuote.Symbol, (double)stockQuote.Price));
                Console.WriteLine($"{stockQuote.Symbol}: {stockQuote.Price}");
            }

            return(stocksAndPrice);
        }
Example #3
0
 public AlphaVantageDownloader(string apiKey)
 {
     _symbols = new List <string>()
     {
         "AAPL",
     };
     _client = new AlphaVantageStocksClient(apiKey);
 }
Example #4
0
        public async Task RequestSymbolSearchAsync_Test()
        {
            var client = new AlphaVantageStocksClient(ApiKey);

            var result = await client.RequestSearchAsync("RJZ");

            Assert.NotNull(result);

            Assert.True(result.Count > 0);
        }
Example #5
0
        public async Task RequestFxDailyTimeSeriesAsync_Test()
        {
            var client = new AlphaVantageStocksClient(ApiKey);

            var result = await client.RequestFxDailyTimeSeriesAsync("USD", "RUB");

            Assert.NotNull(result);

            Assert.True(result.DataPoints.Count > 0);
        }
Example #6
0
        public async void PrintSample()
        {
            var test = new AlphaVantageStocksClient(APIKey.key);
            var data = await test.RequestIntradayTimeSeriesAsync(Ticker, AlphaVantage.Net.Stocks.TimeSeries.IntradayInterval.OneMin, AlphaVantage.Net.Stocks.TimeSeries.TimeSeriesSize.Full);

            using (StreamWriter f = new StreamWriter("SPY.txt"))
            {
                foreach (var d in data.DataPoints)
                {
                    Console.WriteLine($"{Ticker} H:{d.HighestPrice} L:{d.LowestPrice} Diff:{d.HighestPrice - d.LowestPrice} Volume: {d.Volume}");
                    //f.WriteLine($"{Ticker} {d.Time}  Open:{d.OpeningPrice}  H:{d.HighestPrice}  L:{d.LowestPrice}  Diff:{d.HighestPrice - d.LowestPrice}  Volume: {d.Volume}");
                }
            }
        }
Example #7
0
        public async void SaveMinOpenSample()
        {
            var test = new AlphaVantageStocksClient(APIKey.key);
            var data = await test.RequestIntradayTimeSeriesAsync(Ticker, AlphaVantage.Net.Stocks.TimeSeries.IntradayInterval.OneMin, AlphaVantage.Net.Stocks.TimeSeries.TimeSeriesSize.Full);

            using (StreamWriter f = new StreamWriter(Path.Combine(Directory.GetCurrentDirectory(), "SPYopen.txt")))
            {
                foreach (var d in data.DataPoints)
                {
                    //Console.WriteLine($"{Ticker} H:{d.HighestPrice} L:{d.LowestPrice} Diff:{d.HighestPrice - d.LowestPrice} Volume: {d.Volume}");
                    f.WriteLine($"{d.OpeningPrice}");
                }
            }
        }
Example #8
0
        public async static Task <List <StockDataPoint> > GetIntraDayInfo(string symbol, int numDataPoints)
        {
            try
            {
                var avClient = new AlphaVantageStocksClient(API_KEY_ALPHA_VANTAGE);
                var info     = await avClient.RequestIntradayTimeSeriesAsync(symbol, IntradayInterval.OneMin);

                return(info.DataPoints.Take(numDataPoints).ToList());
            }
            catch (Exception e)
            {
                throw;
            }
        }
Example #9
0
        public async Task RequestWeeklyTimeSeriesAsync_Adjusted_Test()
        {
            var client = new AlphaVantageStocksClient(ApiKey);

            var result =
                await client.RequestStockWeeklyTimeSeriesAsync(Symbol, adjusted : true);

            Assert.NotNull(result);
            Assert.Equal(TimeSeriesType.Weekly, result.Type);
            Assert.Equal(Symbol, result.Symbol);
            Assert.True(result.IsAdjusted);
            Assert.NotNull(result.DataPoints);
            Assert.True(result.DataPoints.All(p => p is AdjustedDataPoint));
        }
Example #10
0
        public async Task RequestBatchQuotesAsync_Test()
        {
            var client = new AlphaVantageStocksClient(ApiKey);

            var result =
                await client.RequestBatchQuotesAsync(new [] { "AAPL", "FB", "MSFT" });

            Assert.NotNull(result);
            Assert.Equal(3, result.Count);
            Assert.True(
                result.Any(r => r.Symbol == "MSFT") &&
                result.Any(r => r.Symbol == "FB") &&
                result.Any(r => r.Symbol == "AAPL"));
        }
Example #11
0
        public async Task RequestIntradayTimeSeriesAsync_Test()
        {
            var client = new AlphaVantageStocksClient(ApiKey);

            var result =
                await client.RequestStockIntradayTimeSeriesAsync(Symbol, IntradayInterval.OneMin, TimeSeriesSize.Compact);

            Assert.NotNull(result);
            Assert.Equal(TimeSeriesType.Intraday, result.Type);
            Assert.Equal(Symbol, result.Symbol);
            Assert.NotNull(result.DataPoints);
            Assert.True(result.DataPoints.All(p =>
                                              p.GetType() == typeof(DataPoint) &&
                                              p.GetType() != typeof(AdjustedDataPoint)));
        }
Example #12
0
        public async Task RequestDailyTimeSeriesAsync_NotAdjusted_Test()
        {
            var client = new AlphaVantageStocksClient(ApiKey);

            var result =
                await client.RequestStockDailyTimeSeriesAsync(Symbol, TimeSeriesSize.Compact, adjusted : false);

            Assert.NotNull(result);
            Assert.Equal(TimeSeriesType.Daily, result.Type);
            Assert.Equal(Symbol, result.Symbol);
            Assert.False(result.IsAdjusted);
            Assert.NotNull(result.DataPoints);
            Assert.True(result.DataPoints.All(p =>
                                              p.GetType() == typeof(DataPoint) &&
                                              p.GetType() != typeof(AdjustedDataPoint)));
        }
        async Task GetIntraData()
        {
            IntraDataDict.Clear();
            var             client     = new AlphaVantageStocksClient(APIKey);
            StockTimeSeries timeSeries = await client.RequestIntradayTimeSeriesAsync("GE", IntradayInterval.FiveMin, TimeSeriesSize.Compact);    // client.RequestDailyTimeSeriesAsync("GE", TimeSeriesSize.Compact, false);

            foreach (var dataPoint in timeSeries.DataPoints)
            {
                if (DateTime.Now.Date == dataPoint.Time.Date)
                {
                    decimal[] tempDecimal = new decimal[4];
                    tempDecimal[0] = dataPoint.HighestPrice;
                    tempDecimal[1] = dataPoint.LowestPrice;
                    tempDecimal[2] = dataPoint.OpeningPrice;
                    tempDecimal[3] = dataPoint.ClosingPrice;
                    IntraDataDict[dataPoint.Time] = tempDecimal;
                }
            }
        }
Example #14
0
        public async Task GetStocksToday(String ticker)
        {
            string apiKey = "UI2LXNQXDODSOR3L";

            var             client     = new AlphaVantageStocksClient(apiKey);
            StockTimeSeries timeSeries = await client.RequestDailyTimeSeriesAsync($"{ticker}", TimeSeriesSize.Compact, true);

            int perceptronId = 0;

            foreach (var perceptron in context.Perceptrons)
            {
                if (perceptron.Stock == ticker)
                {
                    perceptronId = perceptron.PerceptronId;
                }
            }
            if (perceptronId == 0)
            {
                Perceptron perceptron = new Perceptron();
                perceptron.Stock = ticker;

                context.Perceptrons.Add(perceptron);
                perceptronId = context.Perceptrons.Find(perceptron).PerceptronId;
                await context.SaveChangesAsync();
            }
            foreach (var dataPoint in timeSeries.DataPoints)
            {
                if (dataPoint.Time.Date == DateTime.Today.AddDays(-1))
                {
                    MarketData DataPoint = new MarketData();
                    DataPoint.Date          = dataPoint.Time.Date;
                    DataPoint.Ticker        = ticker;
                    DataPoint.PercentChange = (double)(dataPoint.OpeningPrice / dataPoint.ClosingPrice);


                    context.MarketDataPoints.Add(DataPoint);
                    await context.SaveChangesAsync();
                }
            }
        }
Example #15
0
        private async Task InitAPIObj()
        {
            APIObj   = new AlphaVantageStocksClient(APIKey.key);
            StockObj = await APIObj.RequestIntradayTimeSeriesAsync(Ticker, AlphaVantage.Net.Stocks.TimeSeries.IntradayInterval.OneMin, AlphaVantage.Net.Stocks.TimeSeries.TimeSeriesSize.Full);

            ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
            //debugging, dump data to file

            /*string dumpFile = $"SPYDUMP_5-30-2020_11-02am.txt";
             * if (!File.Exists(dumpFile))
             * {
             *  using (StreamWriter f = new StreamWriter(Path.Combine(Directory.GetCurrentDirectory(), dumpFile)))
             *  {
             *      foreach (var sdp in StockObj.DataPoints)
             *      {
             *          string temp = $"Time:{sdp.Time}\tOpen:{sdp.OpeningPrice}\tClose:{sdp.ClosingPrice}\tVolume:{sdp.Volume}";
             *          f.WriteLine(temp);
             *      }
             *  }
             * }*/
            ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        }
Example #16
0
        public async Task GetStocksAll(String ticker)
        {
            string apiKey = "UI2LXNQXDODSOR3L";

            var client = new AlphaVantageStocksClient(apiKey);

            StockTimeSeries timeSeries = await client.RequestDailyTimeSeriesAsync($"{ticker}", TimeSeriesSize.Full, true);

            int perceptronId = 0;

            //foreach (var perceptron in context.Perceptrons)
            //{
            //    if (perceptron.Stock == ticker)
            //    {
            //        perceptronId = perceptron.PerceptronId;
            //    }
            //}
            //if (perceptronId == 0)
            //{
            //    Perceptron perceptron = new Perceptron();
            //    perceptron.Stock = ticker;

            //    context.Perceptrons.Add(perceptron);
            //    perceptronId = context.Perceptrons.Find(perceptron.PerceptronId).PerceptronId;
            //    await context.SaveChangesAsync();
            //}
            foreach (var dataPoint in timeSeries.DataPoints)
            {
                MarketData DataPoint = new MarketData();
                DataPoint.Date          = dataPoint.Time.Date;
                DataPoint.Ticker        = ticker;
                DataPoint.PercentChange = (double)(dataPoint.OpeningPrice / dataPoint.ClosingPrice);


                context.MarketDataPoints.Add(DataPoint);
                await context.SaveChangesAsync();
            }
        }
Example #17
0
        public async Task <List <int> > GetVolumeSample() //Get the volume for the last 5 trading days by minute?  And reverse it so it's in order from oldest to newest
        {
            Console.WriteLine($"GetVolumeSample: {DateTime.Now}");
            var outList = new List <int>();
            var APIObj  = new AlphaVantageStocksClient(APIKey.key);
            var data    = await APIObj.RequestIntradayTimeSeriesAsync(Ticker, AlphaVantage.Net.Stocks.TimeSeries.IntradayInterval.OneMin, AlphaVantage.Net.Stocks.TimeSeries.TimeSeriesSize.Full);

            Console.WriteLine($"After data retrieved: {DateTime.Now}");

            //outList = data.DataPoints.Select(x => x.Volume);
            foreach (var min in data.DataPoints)
            {
                outList.Add((int)min.Volume);
            }

            Console.WriteLine($"After converted to list: {DateTime.Now}");

            outList.Reverse();

            Console.WriteLine($"After reversed: {DateTime.Now}");

            return(outList);
        }
Example #18
0
        public async static Task <IEnumerable <StockQuote> > GetQuotes(params string[] symbols)
        {
            var avClient = new AlphaVantageStocksClient(API_KEY_ALPHA_VANTAGE);

            return(await avClient.RequestBatchQuotesAsync(symbols));
        }
Example #19
0
 public Stock(string tick)
 {
     Ticker = tick;
     APIObj = new AlphaVantageStocksClient(APIKey.key);
 }
Example #20
0
        public override async Task <MarketPriceRes> Execute(MarketPriceReq request)
        {
            try
            {
                string          apiKey = plcalc.Common.Properties.Settings.Default.AlphaVantageApiKey;; // enter your API key here
                var             client = new AlphaVantageStocksClient(apiKey);
                StockTimeSeries timeSeries;
                StockDataPoint  data = null;
                switch (request.ApiFunction)
                {
                case ApiFunction.TIME_SERIES_DAILY:
                    timeSeries =
                        await client.RequestDailyTimeSeriesAsync(request.Ticker, TimeSeriesSize.Compact, adjusted : false);

                    if (request.getPreviousClose)
                    {
                        var prevDate = DateTime.Now.DayOfWeek == DayOfWeek.Monday
                                ? DateTime.Now.AddDays(-3).Date
                                : DateTime.Now.Date.AddDays(-1).Date;
                        data = timeSeries.DataPoints.OrderByDescending(a => a.Time)
                               .FirstOrDefault(a => a.Time.Date == prevDate);
                    }
                    else
                    {
                        data = timeSeries.DataPoints.OrderByDescending(a => a.Time).FirstOrDefault();
                    }

                    if (data == null)
                    {
                        return(null);
                    }

                    return(new MarketPriceRes
                    {
                        Ticker = request.Ticker,
                        Date = data.Time,
                        Price = data.ClosingPrice
                    });

                case ApiFunction.TIME_SERIES_INTRADAY:
                    timeSeries =
                        await client.RequestIntradayTimeSeriesAsync(request.Ticker, IntradayInterval.OneMin, TimeSeriesSize.Compact);

                    data = timeSeries.DataPoints.OrderByDescending(a => a.Time).FirstOrDefault();
                    if (data == null)
                    {
                        return(null);
                    }

                    return(new MarketPriceRes
                    {
                        Ticker = request.Ticker,
                        Date = data.Time,
                        Price = data.ClosingPrice
                    });

                default:
                    throw new plcalcException("Invalid apifunction");
                }
            }
            catch (Exception ex)
            {
                //log error
                return(null);
            }
        }