Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
tkcalendar
pandas
matplotlib
yfinance
mplfinance
102 changes: 56 additions & 46 deletions stock_visualizer.py
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -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()
root.mainloop()