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); } }
public bool TryGetHistoricalPrices(string ticker, Exchange?exchange, DateTime?from, DateTime?to, PriceInterval priceInterval, out PriceList prices, out string errorMessage) { double fromValue; if (from.HasValue) { fromValue = (from.Value - DATE_1970).TotalSeconds; } else { fromValue = (FIRST_DATE - DATE_1970).TotalSeconds; } double toValue; if (to.HasValue) { toValue = (to.Value - DATE_1970).TotalSeconds; } else { toValue = (DateTime.Now - DATE_1970).TotalSeconds; } string content = YahooApiCaller.GetHistoricalPrices(ticker, fromValue, toValue, priceInterval); string[] lines = content.Split('\n'); prices = new PriceList(); for (int i = 1; i < lines.Length; i++) { HistoricalPrice p = HistoricalPrice.From(lines[i]); prices.Add(p); } errorMessage = "ok"; return(true); }
public bool TryGetAssetType(string ticker, out AssetClass assetType) { //Example 1 //https://query2.finance.yahoo.com/v7/finance/quote?formatted=true&lang=en-US®ion=US&symbols=AAPL /* * { * "quoteResponse": { * "result": [ * { * ... * "symbol": "AAPL", * "quoteType": "EQUITY", * "longName": "Apple Inc." * ... * } * ], * "error": null * } * } */ //Example 2 //https://query2.finance.yahoo.com/v7/finance/quote?formatted=true&lang=en-US®ion=US&symbols=TQQQ /* * { * "quoteResponse": { * "result": [ * { * "symbol": "TQQQ", * "shortName": "ProShares UltraPro QQQ", * "quoteType": "ETF", * "longName": "ProShares UltraPro QQQ" * } * ], * "error": null * } * } */ bool ok = YahooApiCaller.GetQuoteData(ticker, out HttpStatusCode statusCode, out YahooQuoteResponse yahooResponse, out string jsonResponse, out string message); if (ok) { var result = yahooResponse.quoteResponse.result.Where(r => r.symbol == ticker).SingleOrDefault(); if (result == null) { assetType = AssetClass.Unknown; return(false); } if (result.quoteType == "ETF") { assetType = AssetClass.ETF; return(true); } else if (result.quoteType == "EQUITY") { assetType = AssetClass.Stock; return(true); } else { assetType = AssetClass.Unknown; return(false); } } else { assetType = AssetClass.Unknown; return(false); } }