public MainWindow() { InitializeComponent(); var zeroPrice = new CurrentPrice(); var tickers = new List <string> { "0200.HK", "0941.HK", "2318.HK" }; var viewModel = new ViewModel { ApiKey = "", Stocks = new ObservableCollection <Stock>( tickers.Select(ZeroPrice) ), SelectedHistoricalPrices = new ObservableCollection <HistoricalPrice>() }; this.DataContext = viewModel; // an IoC container would usually be used here. var pricesSource = new AlphaVantageAPI(new AlphaVantageDataSource()); var historicalDatabase = new InMemoryHistoricalDatabase(); new ViewModelUpdater(pricesSource, historicalDatabase, viewModel); }
// GET: Stock Quote public async Task <Stock> GetStockQuote(string symbol) { //symbol = "XOM"; AlphaVantageAPI alphaVantageAPI = new AlphaVantageAPI(MyKeys.ALPHA_VANTAGE_API_KEY); var test = await alphaVantageAPI.GetStockDataAsync(symbol); var jo = JObject.Parse(test); DateTime today = DateTime.Today; DateTime oneMonthAgo = GetLastBusinessDayPreviousMonth(today); DateTime twoMonthsAgo = GetLastBusinessDayPreviousMonth(oneMonthAgo); DateTime threeMonthsAgo = GetLastBusinessDayPreviousMonth(twoMonthsAgo); DateTime fourMonthsAgo = GetLastBusinessDayPreviousMonth(threeMonthsAgo); DateTime fiveMonthsAgo = GetLastBusinessDayPreviousMonth(fourMonthsAgo); DateTime sixMonthsAgo = GetLastBusinessDayPreviousMonth(fiveMonthsAgo); string todayString = today.ToString("yyyy-MM-dd"); string oneMonthAgoString = oneMonthAgo.ToString("yyyy-MM-dd"); string twoMonthsAgoString = twoMonthsAgo.ToString("yyyy-MM-dd"); string threeMonthsAgoString = threeMonthsAgo.ToString("yyyy-MM-dd"); string fourMonthsAgoString = fourMonthsAgo.ToString("yyyy-MM-dd"); string fiveMonthsAgoString = fiveMonthsAgo.ToString("yyyy-MM-dd"); string sixMonthsAgoString = sixMonthsAgo.ToString("yyyy-MM-dd"); var stockPriceToday = jo["Monthly Time Series"][todayString]["4. close"]; var stockPriceOneMonthAgo = jo["Monthly Time Series"][oneMonthAgoString]["4. close"]; var stockPriceTodayTwoMonthsAgo = jo["Monthly Time Series"][twoMonthsAgoString]["4. close"]; var stockPriceTodayThreeMonthsAgo = jo["Monthly Time Series"][threeMonthsAgoString]["4. close"]; var stockPriceTodayFourMonthsAgo = jo["Monthly Time Series"][fourMonthsAgoString]["4. close"]; var stockPriceTodayFiveMonthsAgo = jo["Monthly Time Series"][fiveMonthsAgoString]["4. close"]; var stockPriceTodaySixMonthsAgo = jo["Monthly Time Series"][sixMonthsAgoString]["4. close"]; Stock stock = new System.Web.Script.Serialization.JavaScriptSerializer().Deserialize <Stock>(test); stock.Symbol = symbol; stock.CurrentPrice = stockPriceToday.ToObject <double>(); stock.OneMonthAgoPrice = stockPriceOneMonthAgo.ToObject <double>(); stock.TwoMonthsAgoPrice = stockPriceTodayTwoMonthsAgo.ToObject <double>(); stock.ThreeMonthsAgoPrice = stockPriceTodayThreeMonthsAgo.ToObject <double>(); stock.FourMonthsAgoPrice = stockPriceTodayFourMonthsAgo.ToObject <double>(); stock.FiveMonthsAgoPrice = stockPriceTodayFiveMonthsAgo.ToObject <double>(); stock.SixMonthsAgoPrice = stockPriceTodaySixMonthsAgo.ToObject <double>(); return(stock); }