public void DownloadHistoricalStockRecordsUI(ProgramContext programContext) { Console.WriteLine("Which stock would you like to download? Please enter the symbol"); var symbol = Console.ReadLine(); this.DownloadHistoricalStockRecords(programContext, symbol); }
public void CalcHigh52Week(ProgramContext programContext) { var queryList = programContext.DailyRecordList.ToList(); foreach (DailyStockRecord record in programContext.DailyRecordList) { record.High52Week = queryList.Where(x => x.Date > record.Date.AddYears(-1) && x.Date <= record.Date).Select(x => x.High).Max(); } }
public void CalculationsForDailyRecord(ProgramContext programContext) { this.CalcDailyChange(programContext); this.CalcHigh52Week(programContext); this.CalcLow52Week(programContext); this.CalcOverNightChange(programContext); this.CalcVolitilityRating(programContext); this.CalcDividendYield(programContext); }
public void DownloadHistoricalStockRecords(ProgramContext programContext, string symbol) { programContext.JsonDownloader.AlphaVantageDownloader(programContext, symbol); programContext.Calculations.CalculationsForDailyRecord(programContext); programContext.AddStockInfo.SqlForAddingDailyRecord(programContext); programContext.UserInterface.StartUpOptions(programContext); }
public void StartUpOptions(ProgramContext programContext) { string tempUserInput; Console.WriteLine("What would you like to do?"); Console.WriteLine("1) Update database with with all company info"); Console.WriteLine("2) Research daily records for a stock"); Console.WriteLine("3) Create an account"); Console.WriteLine("4) Log into an account"); Console.WriteLine("Please enter the number of your selection"); tempUserInput = Console.ReadLine(); switch (tempUserInput) { case "1": { // update database with all company info programContext.AddStockInfo.AddAllStocksToDataBase(programContext); break; } case "2": { // Research daily records for a stock programContext.DownloadStockHistoricalRecords.DownloadHistoricalStockRecordsUI(programContext); break; } case "3": { // Create an account programContext.UserAccountCreator.CreateUserAccount(programContext); break; } case "4": { // Log into an account programContext.UserLoggin.LogginUser(programContext); break; } default: { // invalid entry Console.WriteLine("Invalid entry, please try again."); this.StartUpOptions(programContext); break; } } this.StartUpOptions(programContext); }
public void CalcDividendYield(ProgramContext programContext) { var queryList = programContext.DailyRecordList.ToList(); foreach (DailyStockRecord record in programContext.DailyRecordList) { var dividendYield = queryList.Where(x => x.Date > record.Date.AddYears(-1) && x.Date <= record.Date).Select(x => x.Dividend).Sum() / record.Close; dividendYield = dividendYield * 100; record.DividendYield = decimal.Round(dividendYield, 2, MidpointRounding.AwayFromZero); } }
public void CalcDailyChange(ProgramContext programContext) { programContext.DailyRecordList.OrderByDescending(x => x.Date); foreach (DailyStockRecord record in programContext.DailyRecordList) { if (programContext.DailyRecordList.IndexOf(record) != programContext.DailyRecordList.Count - 1) { record.DailyChange = record.Close - record.Open; } } }
public void DeletePortfolio(ProgramContext programContext) { string tempPortfolioToDelete; List <Portfolio> tempPortfolioList; Console.WriteLine("Which portfolio would you like to delete? Please type the name"); using (IDbConnection connection = new System.Data.SqlClient.SqlConnection(Helper.CnnVal("StockPortfolio"))) { tempPortfolioList = connection.Query <Portfolio>( $@"declare @UserAcctID int set @UserAcctID = '{programContext.User.AcctID}' Select * from Portfolio where AcctId = @UserAcctID").ToList(); } foreach (Portfolio portfolio in tempPortfolioList) { Console.WriteLine(portfolio.PortfolioName); } tempPortfolioToDelete = Console.ReadLine(); using (IDbConnection connection = new System.Data.SqlClient.SqlConnection(Helper.CnnVal("StockPortfolio"))) { connection.Query( $@"declare @PortfolioToDelete NvarChar(max) declare @UserAcctID int set @PortfolioToDelete = '{tempPortfolioToDelete}' set @UserAcctID = '{programContext.User.AcctID}' DELETE FROM Portfolio WHERE PortfolioName = @PortfolioToDelete and AcctID = @UserAcctID;"); } Console.WriteLine(); Console.WriteLine("New portfolio list"); using (IDbConnection connection = new System.Data.SqlClient.SqlConnection(Helper.CnnVal("StockPortfolio"))) { tempPortfolioList = connection.Query <Portfolio>( $@"declare @UserAcctID int set @UserAcctID = '{programContext.User.AcctID}' Select * from Portfolio where AcctId = @UserAcctID").ToList(); } foreach (Portfolio portfolio in tempPortfolioList) { Console.WriteLine(portfolio.PortfolioName); } }
static void Main(string[] args) { TransactionRecord transactionRecord = new TransactionRecord(); Stock stock = new Stock(); AddStockInfo addStockInfo = new AddStockInfo(); JsonDownloader jsonDownloader = new JsonDownloader(); List <DailyStockRecord> dailyRecordList = new List <DailyStockRecord>(); DailyStockRecord dailyStockRecord = new DailyStockRecord(); Calculations calculations = new Calculations(); DownloadStockHistoricalRecords downloadStockHistoricalRecords = new DownloadStockHistoricalRecords(); UserInterface userInterface = new UserInterface(); UserAccountCreator userAccountCreator = new UserAccountCreator(); UserLoggin userLoggin = new UserLoggin(); List <Portfolio> portfolioList = new List <Portfolio>(); Portfolio portfolio = new Portfolio(); User user = new User(); ProgramContext programContext = new ProgramContext() { TransactionRecord = transactionRecord, Stock = stock, AddStockInfo = addStockInfo, JsonDownloader = jsonDownloader, DailyRecordList = dailyRecordList, DailyStockRecord = dailyStockRecord, Calculations = calculations, DownloadStockHistoricalRecords = downloadStockHistoricalRecords, UserInterface = userInterface, UserAccountCreator = userAccountCreator, PortfolioList = portfolioList, Portfolio = portfolio, UserLoggin = userLoggin, User = user, SourceFile1 = @"c:\users\jflem\source\repos\stockportfolio\stockportfolio\companylist1.txt", SourceFile2 = @"c:\users\jflem\source\repos\stockportfolio\stockportfolio\companylist2.txt", SourceFile3 = @"c:\users\jflem\source\repos\stockportfolio\stockportfolio\companylist3.txt", SourceFileKey = @"C:\Users\jflem\Documents\Notepadstuff\alphavantageapikey.txt" }; programContext.AlphaVantageKey = File.ReadAllText(@"C:\Users\jflem\Documents\Notepadstuff\alphavantageapikey.txt"); // test section //Console.ReadLine(); // end test section userInterface.StartUpOptions(programContext); }
public void AddUserToDataBase(ProgramContext programContext) { using (IDbConnection connection = new System.Data.SqlClient.SqlConnection(Helper.CnnVal("StockPortfolio"))) { connection.Query( $@"declare @username nvarchar(max) declare @password nvarchar(max) set @username = '******' set @password = '******' insert into AccountLoginInfo (Username, Password) values (@username, @password)"); } }
public void AddPortfolioToDatabase(ProgramContext programContext, string enteredPortfolioName) { using (IDbConnection connection = new System.Data.SqlClient.SqlConnection(Helper.CnnVal("StockPortfolio"))) { var ownedPortfolioNames = connection.Query( $@"declare @portfolioName nvarchar(max) declare @AcctID int set @portfolioName = '{enteredPortfolioName}' set @AcctID = '{programContext.User.AcctID}' insert into Portfolio (PortfolioName, AcctID) values (@portfolioName, @AcctID);"); } }
public void DownloadJson(ProgramContext programContext, List <DailyStockRecord> recordList) { using (var client = new WebClient()) { string RawJson = client.DownloadString("https://www.alphavantage.co/query?function=TIME_SERIES_DAILY_ADJUSTED&symbol=" + programContext.Symbol + "&outputsize=full&apikey=" + programContext.AlphaVantageKey); recordList = JsonConvert.DeserializeObject <List <DailyStockRecord> >(RawJson); foreach (DailyStockRecord record in recordList) { Console.WriteLine(record.Date + " " + record.Close); Console.ReadLine(); } } }
public decimal TransactionFeeSelector(ProgramContext programContext, decimal transactionFees, decimal price, decimal quantity) { var tempQuantity = quantity; Console.WriteLine("How are the transaction fees being calculated?"); Console.WriteLine("Enter 1 for a percentage"); Console.WriteLine("Enter 2 for a flat dollar amount"); var selection = Console.ReadLine(); switch (selection) { case "1": { Console.WriteLine("Please enter the fee percentage as a decimal. I.E. 2.5% is .025"); decimal percentage = Convert.ToDecimal(Console.ReadLine()); if (quantity < 0) { tempQuantity = quantity * -1; } transactionFees = price * tempQuantity * percentage; break; } case "2": { Console.WriteLine("Please enter the flat fee as a decimal."); decimal flatFee = Convert.ToDecimal(Console.ReadLine()); if (quantity < 0) { tempQuantity = quantity * -1; } transactionFees = price * tempQuantity + flatFee; break; } default: { Console.WriteLine("Not a valid entry"); this.TransactionFeeSelector(programContext, transactionFees, price, quantity); break; } } return(transactionFees); }
public void CreatePortfolio(ProgramContext programContext) { Console.WriteLine("Please name your portfolio"); var enteredPortfolioName = Console.ReadLine(); if (string.IsNullOrEmpty(enteredPortfolioName)) { Console.WriteLine("Invalid name, Please try again"); this.CreatePortfolio(programContext); } else { using (IDbConnection connection = new System.Data.SqlClient.SqlConnection(Helper.CnnVal("StockPortfolio"))) { var ownedPortfolioNames = connection.Query( $@"declare @enteredPortfolioName nvarchar(max) = '{enteredPortfolioName}' select PortfolioName from Portfolio where PortfolioName = @enteredPortfolioName").ToList(); if (ownedPortfolioNames.Count != 0) { Console.WriteLine("Username is in use please pick another"); programContext.Portfolio.CreatePortfolio(programContext); } else { programContext.Portfolio.PortfolioName = enteredPortfolioName; programContext.Portfolio.AcctID = programContext.User.AcctID; //programContext.PortfolioList.Add(programContext.Portfolio); this.AddPortfolioToDatabase(programContext, enteredPortfolioName); this.UpdatePortfolioList(programContext); } } Console.WriteLine("Your portfolios"); foreach (Portfolio portfolio in programContext.PortfolioList) { Console.WriteLine(portfolio.PortfolioName); } Console.WriteLine(""); programContext.UserInterface.RegisteredUserMenu(programContext); } }
public void CreateUserPassword(ProgramContext programContext) { Console.WriteLine("Please create a password"); var tempUserPassword = Console.ReadLine(); if (string.IsNullOrEmpty(tempUserPassword)) { // invalid entry Console.WriteLine("Invalid entry please try again"); this.CreateUserPassword(programContext); } else { programContext.User.Password = tempUserPassword; } }
public void UpdatePortfolioList(ProgramContext programContext) { using (IDbConnection connection = new System.Data.SqlClient.SqlConnection(Helper.CnnVal("StockPortfolio"))) { var tempPortfolioList = connection.Query <Portfolio>( $@"declare @UserAcctID int set @UserAcctID = '{programContext.User.AcctID}' select * from Portfolio where AcctID = @UserAcctID").ToList(); foreach (var portfolio in tempPortfolioList) { programContext.PortfolioList.Add(portfolio); } } }
public void CalcVolitilityRating(ProgramContext programContext) { List <DailyStockRecord> tempDailyRecordList = new List <DailyStockRecord>(); var baseDailyRecordList = programContext.DailyRecordList.OrderBy(x => x.Date); //dailyRecordList.Sort((x, y) => DateTime.Compare(x.Date, y.Date)); foreach (DailyStockRecord record in baseDailyRecordList) { decimal sum = 0; decimal average = 0; decimal p = 0; decimal variance = 0; decimal sumOfP = 0; tempDailyRecordList.Add(record); foreach (DailyStockRecord tempRecord in tempDailyRecordList) { sum += tempRecord.Close; } average = sum / tempDailyRecordList.Count; foreach (DailyStockRecord tempRecord in tempDailyRecordList) { p = (tempRecord.Close - average) * (tempRecord.Close - average); sumOfP += p; } variance = sumOfP / tempDailyRecordList.Count - 1; var standardDeviation = Math.Sqrt((double)variance); //var volatility = standardDeviation * 100; record.VolitilityRating = standardDeviation; if (Double.IsNaN(record.VolitilityRating)) { record.VolitilityRating = -1; } } }
public void VerifyPassword(ProgramContext programContext, User registeredUser) { Console.WriteLine("Please enter password"); var givenPassword = Console.ReadLine(); if (givenPassword == registeredUser.Password) { // grant access programContext.User.AcctID = registeredUser.AcctID; programContext.User.UserName = registeredUser.UserName; programContext.User.Password = registeredUser.Password; programContext.User.LoggedIn = true; programContext.UserInterface.RegisteredUserMenu(programContext); } else { Console.WriteLine("invalid password please try again"); this.VerifyPassword(programContext, registeredUser); } }
public void LogginUser(ProgramContext programContext) { Console.WriteLine("Please enter your Username"); var givenUserName = Console.ReadLine(); using (IDbConnection connection = new System.Data.SqlClient.SqlConnection(Helper.CnnVal("StockPortfolio"))) { var registeredUser = connection.QuerySingle <User>( $@"declare @givenUserName nvarchar(max) = '{givenUserName}' select * from AccountLoginInfo where username = @givenUserName"); if (givenUserName == registeredUser.UserName) { this.VerifyPassword(programContext, registeredUser); } else { Console.WriteLine("invalid username please try again"); this.LogginUser(programContext); } } }
public void AlphaVantageDownloader(ProgramContext programContext, string symbol) { // Creating the connection object IAvapiConnection connection = AvapiConnection.Instance; // Set up the connection and pass the API_KEY provided by alphavantage.co connection.Connect(programContext.AlphaVantageKey); // Get the TIME_SERIES_DAILY query object Int_TIME_SERIES_DAILY_ADJUSTED time_series_daily_adjusted = connection.GetQueryObject_TIME_SERIES_DAILY_ADJUSTED(); // Perform the TIME_SERIES_DAILY request and get the result IAvapiResponse_TIME_SERIES_DAILY_ADJUSTED time_series_dailyResponse = time_series_daily_adjusted.Query( symbol, Const_TIME_SERIES_DAILY_ADJUSTED.TIME_SERIES_DAILY_ADJUSTED_outputsize.full); // Printout the results Console.WriteLine("******** RAW DATA TIME_SERIES_DAILY ********"); Console.WriteLine(time_series_dailyResponse.RawData); Console.WriteLine("******** STRUCTURED DATA TIME_SERIES_DAILY ********"); var data = time_series_dailyResponse.Data; if (data.Error) { Console.WriteLine(data.ErrorMessage); } else { Console.WriteLine("Information: " + data.MetaData.Information); Console.WriteLine("Symbol: " + data.MetaData.Symbol); Console.WriteLine("LastRefreshed: " + data.MetaData.LastRefreshed); Console.WriteLine("OutputSize: " + data.MetaData.OutputSize); Console.WriteLine("TimeZone: " + data.MetaData.TimeZone); Console.WriteLine("========================"); Console.WriteLine("========================"); //data.TimeSeries.OrderBy(x => x.DateTime); //testing this out foreach (var timeseries in data.TimeSeries) { DailyStockRecord tempDailyStockRecord = new DailyStockRecord(); Console.WriteLine("open: " + timeseries.open); Console.WriteLine("high: " + timeseries.high); Console.WriteLine("low: " + timeseries.low); Console.WriteLine("close: " + timeseries.close); Console.WriteLine("volume: " + timeseries.volume); Console.WriteLine("DateTime: " + timeseries.DateTime); Console.WriteLine("Dividend: " + timeseries.dividendamount); Console.WriteLine("========================"); tempDailyStockRecord.Symbol = data.MetaData.Symbol; tempDailyStockRecord.Open = Convert.ToDecimal(timeseries.open); tempDailyStockRecord.High = Convert.ToDecimal(timeseries.high); tempDailyStockRecord.Low = Convert.ToDecimal(timeseries.low); tempDailyStockRecord.Close = Convert.ToDecimal(timeseries.close); tempDailyStockRecord.Volume = Convert.ToDecimal(timeseries.volume); tempDailyStockRecord.Date = Convert.ToDateTime(timeseries.DateTime); tempDailyStockRecord.Dividend = Convert.ToDecimal(timeseries.dividendamount); tempDailyStockRecord.AdjustedClose = Convert.ToDecimal(timeseries.adjustedclose); programContext.DailyRecordList.Add(tempDailyStockRecord); //tempDailyStockRecord.VolitilityRating = calculations.CalcVolitilityRating(dailyRecordList); } } }
public void AddAllStocksToDataBase(ProgramContext programContext) { this.AddStock(programContext.SourceFile1); this.AddStock(programContext.SourceFile2); this.AddStock(programContext.SourceFile3); }
public void RegisteredUserMenu(ProgramContext programContext) { programContext.UserLoggin.ValidateUserIsLoggedIn(programContext); string tempUserInput; Console.WriteLine("What would you like to do?"); Console.WriteLine("1) Update database with with all company info"); Console.WriteLine("2) Research daily records for a stock"); Console.WriteLine("3) Create a portfolio"); Console.WriteLine("4) Delete a portfolio"); Console.WriteLine("5) Update records for stocks in your portfolio"); Console.WriteLine("6) View portfolio"); Console.WriteLine("7) Buy/Sell stock"); Console.WriteLine("8) View Stocks in your portfolio"); Console.WriteLine("9) Deposit or withdraw cash"); Console.WriteLine("10) View cash account"); Console.WriteLine("11) View cash transaction record"); Console.WriteLine("12) Log out"); Console.WriteLine("Please enter the number of your selection"); tempUserInput = Console.ReadLine(); switch (tempUserInput) { case "1": { // update database with all company info programContext.AddStockInfo.AddAllStocksToDataBase(programContext); break; } case "2": { // Research daily records for a stock programContext.DownloadStockHistoricalRecords.DownloadHistoricalStockRecordsUI(programContext); break; } case "3": { // Create a portfolio programContext.Portfolio.CreatePortfolio(programContext); break; } case "4": { // Delete a portfolio programContext.Portfolio.DeletePortfolio(programContext); break; } case "5": { // Update records for stocks in portfolio programContext.DownloadStockHistoricalRecords.UpdateStocksInPortfolio(programContext); break; } case "6": { // View Portfolio break; } case "7": { // Buy/Sell Stock programContext.TransactionRecord.BuySellStock(programContext); break; } case "8": { // View stocks in portfolio break; } case "9": { // deposit or withdraw cash break; } case "10": { // View cash account break; } case "11": { // view cash transaction record break; } case "12": { // Log out break; } default: { break; } } this.RegisteredUserMenu(programContext); }
public void BuySellStock(ProgramContext programContext) { string portfolioName; int portfolioID; TransactionRecord transactionToBeAdded = new TransactionRecord(); decimal transactionFees = 0; decimal price; DateTime dateTime; decimal quantity; string symbol; string tempStockID; Console.WriteLine("Please enter the symbol for the stock you would like to buy or sell"); symbol = Console.ReadLine(); Console.WriteLine("Please enter the name of the portfolio this stock is in"); using (IDbConnection connection = new System.Data.SqlClient.SqlConnection(Helper.CnnVal("StockPortfolio"))) { var tempPortfolioList = connection.Query <Portfolio>( $@"declare @UserAcctID int set @UserAcctID = '{programContext.User.AcctID}' select * from Portfolio where AcctID = @UserAcctID").ToList(); foreach (Portfolio portfolio in tempPortfolioList) { Console.WriteLine(portfolio.PortfolioName); } } portfolioName = Console.ReadLine(); using (IDbConnection connection = new System.Data.SqlClient.SqlConnection(Helper.CnnVal("StockPortfolio"))) { portfolioID = connection.QuerySingle <int>( $@"declare @PortfolioName nvarchar(max) = '{portfolioName}' Select PortfolioID from Portfolio where PortfolioName = @PortfolioName"); } Console.WriteLine("Please enter the quantity of share you would like to buy or sell"); Console.WriteLine("use a - sign to indicate selling"); quantity = Convert.ToDecimal(Console.ReadLine()); Console.WriteLine("Please enter the date the stock was bought or sold. Use year,Month,Day format"); dateTime = Convert.ToDateTime(Console.ReadLine()); Console.WriteLine("Please enter the price you bought or sold the shares for"); price = Convert.ToDecimal(Console.ReadLine()); transactionToBeAdded.TransactionFees = this.TransactionFeeSelector(programContext, transactionFees, price, quantity); transactionToBeAdded.PortfolioID = portfolioID; using (IDbConnection connection = new System.Data.SqlClient.SqlConnection(Helper.CnnVal("StockPortfolio"))) { tempStockID = connection.QuerySingle <string>( $@"Declare @symbol nvarchar(10) = '{symbol}' Select StockID from Stock where Symbol = @symbol"); } transactionToBeAdded.StockID = Convert.ToInt32(tempStockID); transactionToBeAdded.DateTime = dateTime; transactionToBeAdded.Price = price; transactionToBeAdded.Quantity = quantity; using (IDbConnection connection = new System.Data.SqlClient.SqlConnection(Helper.CnnVal("StockPortfolio"))) { connection.Query( $@"Declare @PortfolioID int = '{transactionToBeAdded.PortfolioID}', @StockID int = '{transactionToBeAdded.StockID}', @DateTime DateTime = '{transactionToBeAdded.DateTime}', @Price decimal(18,6) = '{transactionToBeAdded.Price}', @Quantity decimal(18,6) = '{transactionToBeAdded.Quantity}', @Fees decimal(18,6) = '{transactionToBeAdded.TransactionFees}', @TransactionFee decimal(18,6) = '{transactionToBeAdded.TransactionFees}' Insert Into TransactionRecord (StockID, DateTime, Price, Quantity, Fees, PortfolioID) values (@StockID, @DateTime, @Price, @Quantity, @Fees, @PortfolioID)"); } }
public void ViewPortfolioUI(ProgramContext programContext) { }