/// <summary> /// Creates a new watch list. /// </summary> private void CreateWatchList() { // First, display a list of existing watch lists. this.DisplayWatchLists(); // Prompt the user to enter a name for the new watch list. Console.Write("Enter new watch list name ([Blank] to cancel) --> "); var name = Console.ReadLine(); if (string.IsNullOrWhiteSpace(name)) { // The user opted to cancel without entering a name. Console.WriteLine(); return; } // Prompt the user to confirm the inputted name. Console.Write("Create watch list named '" + name + "'? (Y/N) --> "); var isConfirmed = Console.ReadLine(); if (string.IsNullOrWhiteSpace(isConfirmed) || !isConfirmed.ToUpper().Equals("Y")) { // The user did not confirm the name. Console.WriteLine("Watch list not created."); Console.WriteLine(); return; } // Create a new entry in the database using the inputted name. using (var context = new StockScreenerEntities()) { try { context.Watchlists.AddObject(new Watchlist { Name = name }); context.SaveChanges(); Console.WriteLine("Watch list was created."); Console.WriteLine(); } catch (Exception ex) { Console.WriteLine(ex.ToString()); } } }
/// <summary> /// Updates the list of stocks in the database by adding new stocks from the list of current stocks and /// by deleting stocks that are no longer listed in the list of current stocks. /// </summary> /// <param name="currentStocks">The current stocks.</param> public void MakeDbStockListCurrent(List<Stock> currentStocks) { foreach (var stock in currentStocks) { try { using (var context = new StockScreenerEntities()) { if (context.Stocks.Any(s => s.Ticker.Equals(stock.Ticker))) { var stockToUpdate = context.Stocks.First(s => s.Ticker.Equals(stock.Ticker)); if (!stockToUpdate.CompanyName.Equals(stock.CompanyName)) { Console.WriteLine("Updating " + stock.Ticker + ": " + stockToUpdate.CompanyName + " to " + stock.CompanyName); stockToUpdate.CompanyName = stock.CompanyName; context.SaveChanges(); } } else { context.Stocks.AddObject(stock); context.SaveChanges(); Console.WriteLine("Added " + stock.Exchange + ":" + stock.Ticker); } } } catch (UpdateException ex) { if (string.IsNullOrEmpty(ex.InnerException.Message) || !ex.InnerException.Message.Contains("Cannot insert duplicate key")) { throw; } } } this.PruneStocksInDb(currentStocks); }
/// <summary> /// Updates the price history for tickers in a watch list. /// </summary> private void UpdateTickersInWatchList() { // First, print a list of watch lists for the user to choose from. this.DisplayWatchLists(); // Prompt the user to select the watch list that will be updated with new data. Console.Write("Select ID of watch list to update. ([Blank] to cancel.) --> "); var input = Console.ReadLine(); if (string.IsNullOrWhiteSpace(input)) { // The user opted to cancel without entering an ID. Console.WriteLine(); return; } int id; if (!int.TryParse(input, out id)) { // Invalid Input -- non-numeric data!!! Console.WriteLine("Invalid input...no watch list was updated."); Console.WriteLine(); return; } using (var context = new StockScreenerEntities()) { var watchLists = context.Watchlists.ToList(); if (watchLists.Count(list => list.ID == id) <= 0) { // Invalid Input -- invalid Watch List ID!!! Console.WriteLine("Invalid input...no watch list was upated."); Console.WriteLine(); } else { // Valid Input -- Update the selected watch list. var dataUpdater = new DataUpdater(); dataUpdater.UpdateTickerHistories(watchLists.First(list => list.ID == id).Stocks); } } }
/// <summary> /// Prints the contents of a watch list. /// </summary> private void PrintWatchList() { // First, display a list of available watch lists. this.DisplayWatchLists(); // Prompt the user to select the watch list that will be printed. Console.Write("Select ID of watch list to print. ([Blank] to cancel.) --> "); var input = Console.ReadLine(); if (string.IsNullOrWhiteSpace(input)) { // The user opted to cancel without selecting a watch list. Console.WriteLine(); return; } int id; if (!int.TryParse(input, out id)) { // Invalid Input -- non-numeric data!!! Console.WriteLine("Invalid input...no watch list was printed."); Console.WriteLine(); return; } using (var context = new StockScreenerEntities()) { try { var watchLists = context.Watchlists.ToList(); if (watchLists.Count(list => list.ID == id) <= 0) { // Invalid Input -- Invalid Watch List ID!!! Console.WriteLine("Invalid input...no watch list was printed."); Console.WriteLine(); } else { // Print the selected watch list. this.DisplayWatchListStocks(watchLists.First(list => list.ID == id)); Console.Write("Press 'Enter' to continue..."); Console.ReadLine(); Console.WriteLine(); } } catch (Exception ex) { Console.WriteLine(ex.ToString()); } } }
/// <summary> /// Displays a listing of the current watch lists. /// </summary> private void DisplayWatchLists() { Console.WriteLine("----------------------------------------"); Console.WriteLine("Current Watch Lists"); Console.WriteLine("----------------------------------------"); Console.WriteLine(); using (var context = new StockScreenerEntities()) { // Create a list of watch lists from the database. var watchLists = context.Watchlists.ToList(); if (watchLists.Count <= 0) { // The list is empty...no watch lists. Console.WriteLine("No Current Watch Lists"); } // Print each item in the list (if any). foreach (var list in watchLists) { Console.WriteLine(list.ID + "\t" + list.Name); } } Console.WriteLine(); }
/// <summary> /// Deletes a watch list. /// </summary> private void DeleteWatchList() { // First, display a list of available watch lists. this.DisplayWatchLists(); // Prompt the user to enter the watch list that will be deleted. Console.Write("Select ID of watch list to delete. ([Blank] to cancel.) --> "); var input = Console.ReadLine(); if (string.IsNullOrWhiteSpace(input)) { // The user opted to cancel without entering an ID. Console.WriteLine(); return; } int id; if (!int.TryParse(input, out id)) { // Invalid Input -- non-numeric data!!! Console.WriteLine("Invalid input...no watch list was deleted."); Console.WriteLine(); return; } using (var context = new StockScreenerEntities()) { try { var watchLists = context.Watchlists.ToList(); if (watchLists.Count(list => list.ID == id) <= 0) { // Invalid Input -- Invalid watch list ID!!! Console.WriteLine("Invalid input...no watch list was deleted."); Console.WriteLine(); return; } // Delete the selected ID from the database. var listToDelete = watchLists.First(list => list.ID == id); context.Watchlists.DeleteObject(listToDelete); context.SaveChanges(); Console.WriteLine("Watch list was deleted."); Console.WriteLine(); } catch (Exception ex) { Console.WriteLine(ex.ToString()); } } }
/// <summary> /// Prunes the stocks in the DB by removing all stocks that are not in the list of current stocks. /// </summary> /// <param name="currentStocks">The list of current stocks that should remain in the DB.</param> private void PruneStocksInDb(IEnumerable<Stock> currentStocks) { using (var context = new StockScreenerEntities()) { var databaseStocks = context.Stocks.ToList(); foreach (var stock in databaseStocks.Where(stock => currentStocks.Count(list => list.Ticker.Equals(stock.Ticker)) <= 0)) { try { context.Stocks.DeleteObject(stock); context.SaveChanges(); Console.WriteLine("Removed " + stock.Exchange + ":" + stock.Ticker); } catch (Exception ex) { Console.WriteLine(ex); } } } }
/// <summary> /// Updates the daily stock data in database. /// </summary> /// <param name="dailies">The daily data to insert into the database.</param> private static void UpdateDailiesInDatabase(IEnumerable<StockDaily> dailies) { using (var context = new StockScreenerEntities()) { foreach (var daily in dailies) { context.StockDailies.AddObject(daily); } var numRecords = context.SaveChanges(); Console.WriteLine("Inserted " + numRecords + " records."); } }
/// <summary> /// Gets the date of the last record in the database for the ticker symbol. /// </summary> /// <param name="ticker">The ticker symbol to query.</param> /// <returns> /// The date of the last record for the ticker. /// </returns> private static DateTime GetDateOfLastRecord(string ticker) { using (var context = new StockScreenerEntities()) { var history = context.StockDailies.Where(stock => ticker.Equals(stock.Ticker)).ToList(); return history.Count > 0 ? history.Max(stock => stock.Date) : DateTime.Now.Subtract(TimeSpan.FromDays(1826)); } }
/// <summary> /// Updates the price history for all tickers in the database. /// </summary> private void UpdateAllTickers() { List<Stock> stocks; using (var context = new StockScreenerEntities()) { stocks = context.Stocks.ToList(); } var dataUpdater = new DataUpdater(); dataUpdater.UpdateTickerHistories(stocks); Console.ReadLine(); }