private void EnrichWithMetadata([NotNull] JProperty metadataJson, [NotNull] StockTimeSeries timeSeries) { var metadatas = metadataJson.Children().Single(); foreach (var metadataItem in metadatas) { var metadataProperty = (JProperty)metadataItem; var metadataItemName = metadataProperty.Name; var metadataItemValue = metadataProperty.Value.ToString(); if (metadataItemName.Contains(MetaDataJsonTokens.InformationToken)) { timeSeries.Type = GetTimeSeriesType(metadataItemValue); timeSeries.IsAdjusted = IsAdjusted(metadataItemValue); } else if (metadataItemName.Contains(MetaDataJsonTokens.RefreshTimeToken)) { var refreshTime = DateTime.Parse(metadataItemValue); timeSeries.LastRefreshed = DateTime.SpecifyKind(refreshTime, DateTimeKind.Local); } else if (metadataItemName.Contains(MetaDataJsonTokens.SymbolToken)) { timeSeries.Symbol = metadataItemValue; } } }
public static async Task StocksDemo() { // use your AlphaVantage API key string apiKey = "6FQOAVODM8ZFCE3T"; // there are 5 more constructors available using var client = new AlphaVantageClient(apiKey); using var stocksClient = client.Stocks(); StockTimeSeries stockTs = await stocksClient.GetTimeSeriesAsync("AAPL", Interval.Daily, OutputSize.Compact, isAdjusted : true); GlobalQuote globalQuote = await stocksClient.GetGlobalQuoteAsync("AAPL"); ICollection <SymbolSearchMatch> searchMatches = await stocksClient.SearchSymbolAsync("BA"); }
//don't use? save actual day object instead? public async Task UpdateDayFiles() //move to Stock.cs eventually { StockObj = await APIObj.RequestIntradayTimeSeriesAsync(Ticker, AlphaVantage.Net.Stocks.TimeSeries.IntradayInterval.OneMin, AlphaVantage.Net.Stocks.TimeSeries.TimeSeriesSize.Full); DateTime lastDate = DateTime.Now.Date;//StockObj.DataPoints.First().Time.Date; foreach (var sdp in StockObj.DataPoints) { if (sdp.Time.Date != lastDate) { lastDate = sdp.Time.Date; //if (JSONDay) } } }
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; } } }
public Analysis(string ticker) { //Initialize API object Ticker = ticker; StockObj = null; Task.Run(async() => await InitAPIObj()); while (StockObj == null) { Thread.Sleep(1000); } //DaysCount = StockObj.DataPoints.Count() / 390; //this assumes every trading day is a full 390 points, no early closures //Break API object in to days list APIData = new List <List <StockDataPoint> >(); Days = new List <Day>(); var dataPoints = StockObj.DataPoints; string tempDate = dataPoints.First().Time.Date.ToString(); var tempList = new List <StockDataPoint>(); foreach (var min in dataPoints) { if (min.Time.Date.ToString() != tempDate) //if a new day is reached, add the tempList and start a new one { Days.Add(new Day(Ticker, tempList)); tempList = new List <StockDataPoint>(); tempDate = min.Time.Date.ToString(); } tempList.Add(min); if (min == dataPoints.Last()) //handles adding the last day since it was cut off with it being in the first if statement { Days.Add(new Day(Ticker, tempList)); } } foreach (var day in Days) { day.DebugDump(); day.SaveJSON(); } }
private StockHistory TransformSeries(StockTimeSeries series) { List <StockDataPoint> history = series.DataPoints.ToList(); if (DateTimeOffset.UtcNow.Subtract(history.First().Time).TotalDays < 1) { history = history.Skip(1).ToList(); } return(new StockHistory { Symbol = series.Symbol, History = history.Select((dp, i) => new StockHistoryItem { ClosingDate = dp.Time, Volume = dp.Volume, AdjustedClose = dp.ClosingPrice, AdjustedPercentChanged = i == history.Count - 1 ? 0 : dp.ClosingPrice / history[i + 1].ClosingPrice }).ToList() }); }
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(); } } }
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); * } * } * }*/ //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// }
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 StockTimeSeries ParseTimeSeries(JObject jObject) { if (jObject == null) { throw new ArgumentNullException(nameof(jObject)); } try { var properties = jObject.Children().Select(ch => (JProperty)ch).ToArray(); var metadataJson = properties.FirstOrDefault(p => p.Name == MetaDataJsonTokens.MetaDataHeader); var timeSeriesJson = properties.FirstOrDefault(p => p.Name.Contains(TimeSeriesJsonTokens.TimeSeriesHeader)); if (metadataJson == null || timeSeriesJson == null) { throw new StocksParsingException("Unable to parse time-series json"); } var result = new StockTimeSeries(); EnrichWithMetadata(metadataJson, result); result.DataPoints = GetStockDataPoints(timeSeriesJson); return(result); } catch (StocksParsingException) { throw; } catch (Exception ex) { throw new StocksParsingException("Unable to parse data. See the inner exception for details", ex); } }