diff --git a/scripts/StockVisualizer/stock.py b/scripts/StockVisualizer/stock.py index b51a355..9988c76 100644 --- a/scripts/StockVisualizer/stock.py +++ b/scripts/StockVisualizer/stock.py @@ -2,18 +2,32 @@ import pandas as pd import yfinance as yf import matplotlib.pyplot as plt -ticker = input('Stock Symbol: ') -start_date = input('Start Date (YYYY-MM-DD): ') -end_date = input('End Date (YYYY-MM-DD): ') +def get_df(ticker, start_date, end_date): + stock = yf.Ticker(ticker) + df = stock.history(start=start_date, end=end_date) + return df + +def plot(df, ticker): + plt.figure(figsize=(14, 8)) + plt.plot(df['Close']) + plt.title(f'{ticker} Stock Price') + plt.xlabel('Date') + plt.ylabel('Price (USD)') + plt.grid() + plt.show() + +def user_input(): + ticker = input('Stock Symbol: ') + start_date = input('Start Date (YYYY-MM-DD): ') + end_date = input('End Date (YYYY-MM-DD): ') + return ticker, start_date, end_date + +def stock_visualizer(): + ticker, start_date, end_date = user_input() + df = get_df(ticker, start_date, end_date) + plot(df, ticker) + + -stock = yf.Ticker(ticker) -df = stock.history(start=start_date, end=end_date) -plt.figure(figsize=(14, 8)) -plt.plot(df['Close']) -plt.title(f'{ticker} Stock Price') -plt.xlabel('Date') -plt.ylabel('Price (USD)') -plt.grid() -plt.show() diff --git a/scripts/StockVisualizer/tests/tests.py b/scripts/StockVisualizer/tests/tests.py new file mode 100644 index 0000000..0c8a1e4 --- /dev/null +++ b/scripts/StockVisualizer/tests/tests.py @@ -0,0 +1,18 @@ +import yfinance as yf +from stock import ( + get_df, + plot, + user_input, + stock_visualizer +) + +def test_df(): + stock = yf.Ticker("MSFT") + assert (get_df(stock, "2023-01-01", "2023-02-01") != None) + +def test_plot(): + stock = yf.Ticker("MSFT") + df = get_df(stock, "2023-01-01", "2023-02-01") + assert(plot(df, stock) != None) + +