From beb41979525191aab6aabac5ec226e9846274d41 Mon Sep 17 00:00:00 2001 From: Yusuf Salman Ahmed Date: Wed, 22 Oct 2025 23:42:39 +0100 Subject: [PATCH] Updated to use the latest yfinance and mpl libaries, along with additional improvements. --- requirements.txt | 5 +++ stock_visualizer.py | 102 ++++++++++++++++++++++++-------------------- 2 files changed, 61 insertions(+), 46 deletions(-) create mode 100644 requirements.txt diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..02f4081 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,5 @@ +tkcalendar +pandas +matplotlib +yfinance +mplfinance \ No newline at end of file diff --git a/stock_visualizer.py b/stock_visualizer.py index 75df3ec..ee1d68c 100644 --- a/stock_visualizer.py +++ b/stock_visualizer.py @@ -1,67 +1,77 @@ ''' NEURALNINE (c) 2019 -Stock Visualizer v0.1 Alpha - -This is the very first prototype and the code is not very clean -Also there may be a couple of bugs -A lot of exceptions are not handled +Stock Visualizer v0.2 +Updated to use latest yfinance and mplfinance libraries, adding support for multiple tickers. ''' # Imports from tkinter import * from tkcalendar import DateEntry import datetime as dt - -import pandas_datareader as web +import yfinance as yf import matplotlib.pyplot as plt import matplotlib.dates as mdates -from mpl_finance import candlestick_ohlc +from mplfinance.original_flavor import candlestick_ohlc +import matplotlib +matplotlib.use('TkAgg') -''' -Function for visualizing stock data -Using Candlestick Charts -''' -def visualize(): +plt.rcParams['font.family'] = 'Consolas' - # Get Dates From DateEntry and Convert It To Datetime +# Function for visualizing stock data using Candlestick Charts +def visualize(): + # Get Dates From DateEntry and Convert to Datetime from_date = cal_from.get_date() to_date = cal_to.get_date() - start = dt.datetime(from_date.year, from_date.month, from_date.day) end = dt.datetime(to_date.year, to_date.month, to_date.day) - # Load Ticker From Entry And Download Data - ticker = text_ticker.get() - data = web.DataReader(ticker, 'yahoo', start, end) - - # Restructure Data Into OHLC Format - data = data[['Open', 'High', 'Low', 'Close']] - - # Reset Index And Convert Dates Into Numerical Format - data.reset_index(inplace=True) - data['Date'] = data['Date'].map(mdates.date2num) - - # Adjust Style Of The Plot - ax = plt.subplot() - ax.grid(True) - ax.set_axisbelow(True) - ax.set_title('{} Share Price'.format(ticker), color='white') - ax.figure.canvas.set_window_title('NeuralNine Stock Visualizer v0.1 Alpha') - ax.set_facecolor('black') - ax.figure.set_facecolor('#121212') - ax.tick_params(axis='x', colors='white') - ax.tick_params(axis='y', colors='white') - ax.xaxis_date() - - # Plot The Candlestick Chart - candlestick_ohlc(ax, data.values, width=0.5, colorup='#00ff00') - plt.show() + # Load Tickers From Entry (comma-separated) And Download Data + tickers = [t.strip().upper() for t in text_ticker.get().split(',')] + if not tickers: + return + + # Create one figure with len(tickers) subplots + fig, axes = plt.subplots(len(tickers), 1, figsize=(10, 4*len(tickers)), squeeze=False) + axes = axes.flatten() # ensure it's a 1D array even if only 1 ticker + + for ax, ticker in zip(axes, tickers): + try: + # Download data + data = yf.download(ticker, start=start, end=end) + data = data[['Open', 'High', 'Low', 'Close']] + data.reset_index(inplace=True) + data['Date'] = data['Date'].map(mdates.date2num) + + # Style the subplot + ax.grid(True, color='lightgray') + ax.set_axisbelow(True) + ax.set_title(f'{ticker} Share Price', color='black') + for spine in ax.spines.values(): + spine.set_visible(False) + ax.set_facecolor('white') + ax.tick_params(axis='x', colors='black') + ax.tick_params(axis='y', colors='black') + ax.xaxis_date() + + # Plot candlestick chart + candlestick_ohlc(ax, data.values, width=0.5, colorup='#2ECC71', colordown='#E74C3C') + except Exception as e: + ax.text(0.5, 0.5, f'Error fetching {ticker}\n{str(e)}', + horizontalalignment='center', verticalalignment='center', + transform=ax.transAxes, color='red', fontsize=12) + + fig.suptitle('Stock Visualiser', fontsize=16) + plt.tight_layout() + plt.show(block=False) + plt.pause(15) + plt.close(fig) + # Define Main Window root = Tk() -root.title("NeuralNine Stock Visualizer v0.1 Alpha") +root.title("NeuralNine Stock Visualiser v0.2") -# Add Components And Link Function +# Add Components and Link Function label_from = Label(root, text="From:") label_from.pack() cal_from = DateEntry(root, width=50, year=2010, month=1, day=1) @@ -72,12 +82,12 @@ def visualize(): cal_to = DateEntry(root, width=50) cal_to.pack(padx=10, pady=10) -label_ticker = Label(root, text="Ticker Symbol:") +label_ticker = Label(root, text="Ticker Symbol(s) (comma-separated):") label_ticker.pack() text_ticker = Entry(root) text_ticker.pack() -btn_visualize = Button(root, text="Visualize", command=visualize) +btn_visualize = Button(root, text="Visualise", command=visualize) btn_visualize.pack() -root.mainloop() \ No newline at end of file +root.mainloop()