public bool TryGetLastPrice(string ticker, Exchange?exchange, AssetClass assetType, out HistoricalPrice lastPrice, out string message) { //Last two months??? //https://query1.finance.yahoo.com/v8/finance/chart/AAPL?region=US&lang=en-US&includePrePost=false&interval=2m&range=1d&corsDomain=finance.yahoo.com&.tsrc=finance //Daily, last day //https://query1.finance.yahoo.com/v8/finance/chart/AAPL?lang=en-US&includePrePost=false&interval=1d&range=1d //Daily, last 5 days //https://query1.finance.yahoo.com/v8/finance/chart/AAPL?lang=en-US&includePrePost=false&interval=1d&range=5d int days = 1; YahooChartInterval interval = YahooChartInterval.OneHour; bool ok = YahooApiCaller.GetDailyPrices(ticker, interval, days, out HttpStatusCode statusCode, out YahooChartResponse yahooResponse, out string jsonResponse, out message); if (ok && statusCode == HttpStatusCode.OK && yahooResponse != null && yahooResponse.Chart != null && yahooResponse.Chart.Result != null && yahooResponse.Chart.Result.Length > 0) { var result = yahooResponse.Chart.Result.Last(); int length = result.TimeStampsAsLong.Length; decimal?close = null; int i = length - 1; while (close == null && i > 0) { if (result.Indicators.Quote[0].Close[i].HasValue) { close = result.Indicators.Quote[0].Close[i].Value; } else { i--; } } int index = i; long seconds = result.TimeStampsAsLong[index]; DateTime dt = DATE_1970.AddSeconds(seconds); decimal price = result.Indicators.Quote[0].Close[index].Value; ulong volume = result.Indicators.Quote[0].Volume[index].Value; lastPrice = new HistoricalPrice() { Date = dt, Close = price, Volume = volume, }; return(true); } else { lastPrice = null; return(false); } }
internal static bool GetDailyPrices(string ticker, YahooChartInterval interval, int days, out HttpStatusCode statusCode, out YahooChartResponse yahooResponse, out string jsonResponse, out string message) { //Last two months??? //https://query1.finance.yahoo.com/v8/finance/chart/AAPL?region=US&lang=en-US&includePrePost=false&interval=2m&range=1d&corsDomain=finance.yahoo.com&.tsrc=finance //Daily, last day //https://query1.finance.yahoo.com/v8/finance/chart/AAPL?lang=en-US&includePrePost=false&interval=1d&range=1d //Daily, last 5 days //https://query1.finance.yahoo.com/v8/finance/chart/AAPL?lang=en-US&includePrePost=false&interval=1d&range=5d //Wrong parameter //https://query1.finance.yahoo.com/v8/finance/chart/AAPL?region=US&lang=en-US&includePrePost=false&interval=5h&range=1d //{"chart":{"result":null,"error":{"code":"Bad Request","description":"Invalid input - interval=5h is not supported. Valid intervals: [1m, 2m, 5m, 15m, 30m, 60m, 90m, 1h, 1d, 5d, 1wk, 1mo, 3mo]"}}} //Daily, every 1 minutes //https://query1.finance.yahoo.com/v8/finance/chart/AAPL?region=US&lang=en-US&includePrePost=false&interval=1m&range=1d //Daily, every 15 minutes //https://query1.finance.yahoo.com/v8/finance/chart/AAPL?region=US&lang=en-US&includePrePost=false&interval=15m&range=1d string uri = $"{httpClientChartPrices.BaseAddress}/{ticker}?lang=en-US&includePrePost=false&interval={interval}&range={days}d"; HttpResponseMessage responseMessage = httpClientChartPrices.GetAsync(uri).Result; string content = responseMessage.Content.ReadAsStringAsync().Result; statusCode = responseMessage.StatusCode; if (responseMessage.StatusCode == HttpStatusCode.OK) { jsonResponse = content; yahooResponse = JsonConvert.DeserializeObject <YahooChartResponse>(jsonResponse); message = "OK"; return(true); } else { dynamic error = new { HttpStatusCode = responseMessage.StatusCode.ToString(), ReasonPhrase = responseMessage.ReasonPhrase, ContentResponse = content, }; jsonResponse = JsonConvert.SerializeObject(error); yahooResponse = null; message = responseMessage.ReasonPhrase; return(false); } }