public async Task RunNotifier() { var notification = new NotificationServer(); List <StockDataPoint> dataPoints = new List <StockDataPoint>(); decimal alertOver = 290; //used to send notifications when passes certain price decimal alertUnder = 284; if (DateTime.Now.TimeOfDay < MarketOpen) // wait for market to open { var waitTime = MarketOpen - DateTime.Now.TimeOfDay; await Task.Delay(waitTime); } while (DateTime.Now.TimeOfDay < MarketClose) { var data = await APIObj.RequestDailyTimeSeriesAsync(Ticker); var latest = data.DataPoints.First(); dataPoints.Add(latest); ///////////////////////////TESTING //notification.Publish("Test", "test notification"); ///////////////////////////TESTING if (DateTime.Now.TimeOfDay.Minutes == 0) { notification.Publish($"Hourly {Ticker} update", $"{latest.ClosingPrice}"); } if (latest.ClosingPrice > alertOver) { notification.Publish($"{Ticker} alert", $"{latest.ClosingPrice} has passed alertOver:{alertOver}"); alertOver = 999; //so that it gets ignored on future passes } if (latest.ClosingPrice < alertUnder) { notification.Publish($"{Ticker} alert", $"{latest.ClosingPrice} has dropped below alertUnder:{alertUnder}"); alertUnder = 0; //so that it gets ignored on future passes } await Task.Delay(TimeSpan.FromSeconds(60)); } //one last check for market closed var lastData = await APIObj.RequestDailyTimeSeriesAsync(Ticker); notification.Publish("Market closed", $"{Ticker} final price: {lastData.DataPoints.First().ClosingPrice}"); }
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); }
public async Task RequestDailyTimeSeriesAsync_Adjusted_Test() { var client = new AlphaVantageStocksClient(ApiKey); var result = await client.RequestDailyTimeSeriesAsync(Symbol, TimeSeriesSize.Compact, adjusted : true); Assert.NotNull(result); Assert.Equal(TimeSeriesType.Daily, result.Type); Assert.Equal(Symbol, result.Symbol); Assert.True(result.IsAdjusted); Assert.NotNull(result.DataPoints); Assert.True(result.DataPoints.All(p => p is StockAdjustedDataPoint)); }
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(); } } }
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(); } }
public async Task CompareOpenPositions() { //how many days open up, how many days open down, and percentages/chance for each, and average amount for both and for all opens together var dayObj = await APIObj.RequestDailyTimeSeriesAsync(Ticker, TimeSeriesSize.Full); var dayArray = dayObj.DataPoints.ToArray(); //average last 10, 20, 50, 1000(4yrs) days int i = 0; decimal avg10, pct10, avg20, pct20, avg50, pct50, avg1000, pct1000; avg10 = pct10 = avg20 = pct20 = avg50 = pct50 = avg1000 = pct1000 = 0; int up10, down10, up20, down20, up50, down50, up1000, down1000; up10 = down10 = up20 = down20 = up50 = down50 = up1000 = down1000 = 0; while (i < 1000) { decimal diff = dayArray[i].OpeningPrice - dayArray[i + 1].ClosingPrice; if (i < 10) { avg10 += diff; if (diff < 0) { down10 += 1; } else if (diff > 0) { up10 += 1; } } if (i < 20) { avg20 += diff; if (diff < 0) { down20 += 1; } else if (diff > 0) { up20 += 1; } } if (i < 50) { avg50 += diff; if (diff < 0) { down50 += 1; } else if (diff > 0) { up50 += 1; } } avg1000 += diff; if (diff < 0) { down1000 += 1; } else if (diff > 0) { up1000 += 1; } i++; } avg10 /= 10; avg20 /= 20; avg50 /= 50; avg1000 /= 1000; pct10 = up10 / 10; pct20 = up20 / 20; pct50 = up50 / 50; pct1000 = up1000 / 1000; using (StreamWriter f = new StreamWriter(Path.Combine(Directory.GetCurrentDirectory(), $"Analysis_{Ticker}_CompareOpenPositions.txt"))) { string header = $"{Ticker} - CompareOpenPositions analysis\n\n"; //Console.WriteLine(header); f.WriteLine(header); f.WriteLine($"Avg last 10\nAVG:{avg10}\nUp:{up10}\tDown:{down10}\nPCT up:{pct10}\n"); f.WriteLine($"Avg last 10\nAVG:{avg20}\nUp:{up20}\tDown:{down20}\nPCT up:{pct20}\n"); f.WriteLine($"Avg last 10\nAVG:{avg50}\nUp:{up50}\tDown:{down50}\nPCT up:{pct50}\n"); f.WriteLine($"Avg last 10\nAVG:{avg1000}\nUp:{up1000}\tDown:{down1000}\nPCT up:{pct1000}\n"); } }
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); } }