A Financial Database for Beginners

def merge_tickers():
nasdaqtxt = ‘tickerlist/nasdaq.txt’
nysetxt = ‘tickerlist/nyse.txt’
tickers = {}

with open(nasdaqtxt,’r’) as file:
companies = file.readlines()
for company in companies:
company_data = company.split(‘|’)
tickers[company_data[0]] = {
‘ticker’ : company_data[0],
‘company’ : company_data[1],
‘exchange’ : ‘Nasdaq’,
‘ETF’ : False }
with open(nysetxt,’r’) as file:
def exchange_type(type):
if type in (‘A’,’N’,’P’): return ‘NYSE’
else: return ‘OTHER’
def is_etf(flag):
if flag == ‘Y’: return True
else: return False

companies = file.readlines()

for company in companies:
company_data = company.split(‘|’)
if '.' not in company_data[0] and '$' not in company_data[0]:
tickers[company_data[0]] = {
‘ticker’ : company_data[0],
‘company’ : company_data[1],
‘exchange’ : exchange_type(company_data[2]),
‘ETF’ : is_etf(company_data[4]) }
return tickers
import jsondef write_to_json(data):
file_location = 'tickerlist/tickers.json'
json_string = json.dumps(data)
with open(file_location,'w+') as file:
file.write(json_string)
companies = merge_tickers()
write_to_json(companies)
import yfinance as yf"""
for this demo we are going to focus on end of day (C) pricing for these companies
"""
def get_tickers():
file_location = 'tickerlist/tickers.json'
with open(file_location,'r') as file:
string = file.read()
data = json.loads(string)
tickers = [ticker for ticker in data]
return tickers
def initialize_db(tickers):
for ticker in tickers:
try:
data = yf.download(
tickers = ticker,
period='max',
interval='1d',
auto_adjust=True,
prepost=True,
proxy=None )

d = {}
for index, row in data.iterrows():
d[str(index).split(' ')[0]] = {
'O' : row['Open'],
'H' : row['High'],
'L' : row['Low'],
'C' : row['Close'],
'V' : row['Volume'] }

json_string = json.dumps(d)
file_location = 'dailydata/{}.json'.format(ticker)
with open(file_location,'w+') as file:
file.write(json_string)

except:
continue
tickers = get_tickers()
initialize_db(tickers)
import seaborn as sns
import pandas as pd
import matplotlib.pyplot as plt
import json
def plot(ticker):
# retrieve the data
file_location = ‘dailydata/{}.json’.format(ticker)
with open(file_location, ‘r’) as file:
json_string = file.read()
data = json.loads(json_string)

# format the data
try:
stock_prices = [ data[date][‘C’] for date in data ]
dates = [ date for date in data ]
assert len(stock_prices) == len(dates)
except AssertionError:
print(‘Error in retrieving company data’)
return

if len(stock_prices) > 2000:
stock_prices = stock_prices[-2000:]
dates = dates[-2000:]

data = { ‘Dates’ : dates,
‘Price’ : stock_price }

df = pd.DataFrame.from_dict(data)

# change the dates parameter so they are of type date
df[‘Dates’] = pd.to_datetime(df[‘Dates’], format = ‘%Y-%m-%d’)

# plot the data
sns.lineplot(data=df,x=’Dates’,y=’Price’)
plt.title(ticker)
plt.show()
plot(‘TSLA’)
plot('GME')

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store