Commit 72881f90 authored by Robert's avatar Robert
Browse files

query and insert, still need update/delete

parent c820d893
import mysql.connector
COMMAND_INDIVIDUAL = "individual"
COMMAND_INDIVIDUAL_SEARCH = "search "
COMMAND_INDIVIDUAL_IPO = "ipo"
COMMAND_INDIVIDUAL_FINANCIALS = "financials "
COMMAND_INDIVIDUAL_PRICING = "pricing "
COMMAND_INDIVIDUAL_ARTICLE = "article "
COMMAND_INDIVIDUAL_COMMENT = "comment "
COMMAND_INDIVIDUAL_BACK = "back"
COMMAND_INDIVIDUAL_HELP = "help"
INDIVIDUAL_MISSING_TICKER = "Please select a ticker by using the \"search <ticker>\" command"
SEPARATOR = "-------------------------"
def individual(cursor):
print(SEPARATOR + "\nEntered individual mode\nType \"help\" for additional instructions\n" + SEPARATOR)
active_ticker = None
while True:
print("Active ticker: <" + ("NONE SELECTED" if active_ticker is None else active_ticker) + ">")
user_input = input().lower()
if user_input == COMMAND_INDIVIDUAL_HELP:
individual_help()
elif user_input.startswith(COMMAND_INDIVIDUAL_SEARCH):
ticker = user_input.removeprefix(COMMAND_INDIVIDUAL_SEARCH).upper()
cursor.execute("select symbol from Stock where symbol = '" + ticker + "'")
valid = True if len(cursor.fetchall()) > 0 else False # Check if the ticker is valid by referencing the database
if valid:
active_ticker = ticker
else:
print("Ticker <" + ticker + "> is invalid.")
elif user_input == COMMAND_INDIVIDUAL_IPO:
if active_ticker is None:
print(INDIVIDUAL_MISSING_TICKER)
else:
cursor.execute("select * from IPODetails where symbol = '" + active_ticker + "'")
result = cursor.fetchone()
if result is None:
print("No IPO data available for " + active_ticker)
else:
print("")
print("Symbol: " + str(result["symbol"]))
print("Date: " + str(result["day"]))
print("Days better than S&P during year: " + str(result["daysBetterThanSP"]))
print("Days green during year: " + str(result["daysGreen"]))
print("Initial month trend: " + str(result["marketMonthTrend"]))
print("Initial 3-month trend: " + str(result["market3MonthTrend"]))
print("Initial 6-month trend: " + str(result["market6MonthTrend"]))
print("Initial year trend: " + str(result["marketYearTrend"]))
print("USA company: " + str(result["usaCompany"]))
print("")
elif user_input.startswith(COMMAND_INDIVIDUAL_FINANCIALS):
if active_ticker is None:
print (INDIVIDUAL_MISSING_TICKER)
else:
year = user_input.removeprefix(COMMAND_INDIVIDUAL_FINANCIALS)
cursor.execute("select * from Financials where symbol = '" + active_ticker + "' and year ='" + year + "'")
result = cursor.fetchone()
if result is None:
print("No financial data available for " + active_ticker + " during the year of " + year)
else:
print("")
print("Symbol: " + str(result["symbol"]))
print("Year: " + str(result["year"]))
print("Revenue: " + str(result["revenue"]))
print("Revenue growth: " + str(result["revenueGrowth"]))
print("Cost of revenue: " + str(result["costOfRevenue"]))
print("Gross profit: " + str(result["grossProfit"]))
print("R&D expenses: " + str(result["rndExpenses"]))
print("EPS: " + str(result["eps"]))
print("EPS diluted: " + str(result["epsDiluted"]))
print("Dividend per share: " + str(result["dividendPerShare"]))
print("EBITDA: " + str(result["ebitda"]))
print("EBIT: " + str(result["ebit"]))
print("Profit margin: " + str(result["profitMargin"]))
print("Cash and Equivalents: " + str(result["cashAndEquivalent"]))
print("Debt growth: " + str(result["debtGrowth"]))
print("")
elif user_input.startswith(COMMAND_INDIVIDUAL_PRICING):
if active_ticker is None:
print(INDIVIDUAL_MISSING_TICKER)
else:
date = user_input.removeprefix(COMMAND_INDIVIDUAL_PRICING)
splitDate = date.split("-")
day = splitDate[0]
month = splitDate[1]
year = splitDate[2]
cursor.execute("select * from StockPrice where symbol = '" + active_ticker + "' and day(date) = '" + day + "' and month(date) = '" + month + "' and year(date) = '" + year + "'")
result = cursor.fetchone()
if result is None:
print("No pricing data available for " + active_ticker + " at " + day + "-" + month + "-" + year + " (dd-mm-yyyy)")
else:
print("")
print("Symbol: " + str(result["symbol"]))
print("Date: " + str(result["date"]))
print("Volume: " + str(result["volume"]))
print("Open: " + str(result["open"]))
print("High: " + str(result["high"]))
print("Low: " + str(result["low"]))
print("Close: " + str(result["close"]))
print("Adjusted close: " + str(result["adjClose"]))
print("")
elif user_input.startswith(COMMAND_INDIVIDUAL_ARTICLE):
if active_ticker is None:
print(INDIVIDUAL_MISSING_TICKER)
else:
specifiers = user_input.removeprefix(COMMAND_INDIVIDUAL_ARTICLE).split(' ')
date1 = specifiers[1]
date2 = specifiers[2]
if specifiers[0].lower() == "partner":
cursor.execute("select * from Article a inner join PartnerArticle p on a.articleID = p.articleID "
+ " where a.date >= str_to_date('" + date1 + "', '%d-%m-%Y') and a.date <= str_to_date('" + date2 + "', '%d-%m-%Y') "
+ " and a.symbol = '" + active_ticker + "' order by a.date")
print("")
for row in cursor.fetchall():
print("Date: " + str(row["date"]))
print("Partner: " + str(row["partner"]))
print("Headline: " + str(row["headline"]))
print("URL: " + str(row["url"]))
print("")
elif specifiers[0].lower() == "analyst":
cursor.execute("select * from Article a inner join AnalystArticle an on a.articleID = an.articleID "
+ " where a.date >= str_to_date('" + date1 + "', '%d-%m-%Y') and a.date <= str_to_date('" + date2 + "', '%d-%m-%Y') "
+ " and a.symbol = '" + active_ticker + "' order by a.date")
print("")
for row in cursor.fetchall():
print("Date: " + str(row["date"]))
print("Analyst: " + str(row["analyst"]))
print("Headline: " + str(row["headline"]))
print("URL: " + str(row["url"]))
print("")
else:
print("Invalid article type selected")
elif user_input.startswith(COMMAND_INDIVIDUAL_COMMENT):
if active_ticker is None:
print(INDIVIDUAL_MISSING_TICKER)
else:
specifiers = user_input.removeprefix(COMMAND_INDIVIDUAL_ARTICLE).split(' ')
date1 = specifiers[1]
date2 = specifiers[2]
cursor.execute("select * from StockComment where date >= str_to_date('" + date1 + "', '%d-%m-%Y') and date <= str_to_date('" + date2 + "', '%d-%m-%Y') " +
" and symbol = '" + active_ticker + "' order by date")
print("")
for row in cursor.fetchall():
print("Date: " + str(row["date"]))
print("Comment: " + str(row["text"]))
print("")
elif user_input == COMMAND_INDIVIDUAL_BACK:
print(SEPARATOR + "\nLeaving \"individual\" mode\n" + SEPARATOR)
break
else:
print("Unknown command, type \"help\" for instructions")
def individual_help():
print("Individual help")
\ No newline at end of file
import mysql.connector
from datetime import datetime
COMMAND_INSERT = "insert"
COMMAND_INSERT_STOCK = "stock"
COMMAND_INSERT_BACK = "back"
COMMAND_INSERT_ARTICLE = "article"
COMMAND_INSERT_COMMENT = "comment"
COMMAND_INSERT_PRICE = "price"
COMMAND_INSERT_IPO = "ipo"
COMMAND_INSERT_FINANCIALS = "financials"
SEPARATOR = "-------------------------"
INSERT_DATA = "Operation completed."
def insert(db, cursor):
print(SEPARATOR + "\nEntered insert mode\nType \"help\" for additional instructions\n" + SEPARATOR)
while True:
user_input = input().lower()
if user_input == COMMAND_INSERT_STOCK:
user_input = input("Stock symbol: ")
insert_symbol(db, cursor, user_input)
print(INSERT_DATA)
elif user_input == COMMAND_INSERT_ARTICLE:
symbol = input("Stock symbol: ")
url = input("URL: ")
headline = input("Headline: ")
date = input("Date (dd-mm-yyyy): ")
articleType = input("Type (analyst or partner): ").lower()
authorType = "Analyst: " if articleType == "analyst" else "Partner: "
author = input(authorType)
# Insert symbol to strong entity just in case it's new
insert_symbol(db, cursor, symbol)
# Insert into main article table
sql = "insert into Article (symbol, url, headline, date) values (%s, %s, %s, %s)"
val = (symbol, url, headline, create_datetime(date))
cursor.execute(sql , val)
articleID = cursor.lastrowid
# Insert into specialized tables
if articleType == "analyst":
sql = "insert into AnalystArticle (articleID, analyst) values (%s, %s)"
val = (articleID, author)
cursor.execute(sql, val)
else:
sql = "insert into PartnerArticle (articleID, partner) values (%s, %s)"
val = (articleID, author)
cursor.execute(sql, val)
db.commit()
print(INSERT_DATA)
elif user_input == COMMAND_INSERT_COMMENT:
symbol = input("Stock symbol: ")
text = input("Text: ")
# Insert symbol to strong entity just in case it's new
insert_symbol(db, cursor, symbol)
sql = "insert into StockComment (symbol, date, text) values (%s, %s, %s)"
val = (symbol, datetime.now(), text)
cursor.execute(sql, val)
db.commit()
print(INSERT_DATA)
elif user_input == COMMAND_INSERT_PRICE:
symbol = input("Stock symbol: ")
date = input("Date (dd-mm-yyyy): ")
volume = input("Volume: ")
open = input("Open: ")
high = input("High: ")
low = input("Low: ")
close = input("Close: ")
adjClose = input("Adjusted close: ")
# Insert symbol to strong entity just in case it's new
insert_symbol(db, cursor, symbol)
sql = "insert into StockPrice (symbol, date, volume, open, high, low, close, adjClose) values (%s, %s, %s, %s, %s, %s, %s, %s)"
val = (symbol, create_datetime(date), volume, open, high, low, close, adjClose)
cursor.execute(sql, val)
db.commit()
print(INSERT_DATA)
elif user_input == COMMAND_INSERT_IPO:
symbol = input("Stock symbol: ")
date = input("Date (dd-mm-yyyy): ")
daysBetterThanSP = input("Days better than S&P: ")
daysGreen = input("Days green: ")
monthTrend = input("First month trend: ")
month3Trend = input("First 3-month trend: ")
month6Trend = input("First 6-month trend: ")
yearTrend = input("First year trend: ")
usaCompany = input("Usa company: (YES or NO): ")
# Insert symbol to strong entity just in case it's new
insert_symbol(db, cursor, symbol)
sql = "insert into IPODetails (symbol, day, daysBetterThanSP, daysGreen, marketMonthTrend, market3MonthTrend, market6MonthTrend, marketYearTrend, usaCompany) values (%s, %s, %s, %s, %s, %s, %s, %s, %s)"
val = (symbol, create_datetime(date), daysBetterThanSP, daysGreen, monthTrend, month3Trend, month6Trend, yearTrend, usaCompany)
cursor.execute(sql, val)
db.commit()
print(INSERT_DATA)
elif user_input == COMMAND_INSERT_FINANCIALS:
symbol = input("Stock symbol: ")
year = input("Year: ")
revenue = input("Revenue: ")
revenueGrowth = input("Revenue growth: ")
costOfRevenue = input("Cost of revenue: ")
grossProfit = input("Gross profit: ")
rndExpenses = input("R&D expenses: ")
eps = input("EPS: ")
epsDiluted = input("EPS diluted: ")
dividendPerShare = input("Divident per share: ")
ebitda = input("ebitda: ")
ebit = input("ebit: ")
profitMargin = input("Profit margin: ")
cashAndEquivalent = input("Cash and equivalents: ")
debtGrowth = input("Debt growth: ")
# Insert symbol to strong entity just in case it's new
insert_symbol(db, cursor, symbol)
sql = str("insert into Financials (symbol, year, revenue, revenueGrowth, costOfRevenue, grossProfit, rndExpenses, eps, epsDiluted, dividendPerShare, ebitda, ebit, profitMargin, cashAndEquivalent, debtGrowth) "
+ " values (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)")
val = (symbol, year, revenue, revenueGrowth, costOfRevenue, grossProfit, rndExpenses, eps, epsDiluted, dividendPerShare, ebitda, ebit, profitMargin, cashAndEquivalent, debtGrowth)
cursor.execute(sql, val)
db.commit()
print(INSERT_DATA)
elif user_input == COMMAND_INSERT_BACK:
print(SEPARATOR + "\nLeaving \"insert\" mode\n" + SEPARATOR)
break
else:
print("Unknown command, type \"help\" for instructions")
def insert_symbol(db, cursor, symbol):
sql = "insert ignore into Stock (symbol) values (%s)"
val = [(symbol.upper())]
cursor.execute(sql, val)
db.commit()
def create_datetime(dateString):
split = dateString.split('-')
day = int(split[0])
month = int(split[1])
year = int(split[2])
return datetime(year, month, day)
\ No newline at end of file
import mysql.connector
from individual import *
from insert import *
from delete import *
SEPARATOR = "-------------------------"
COMMAND_HELP = "help"
COMMAND_EXIT = "exit"
COMMAND_INDIVIDUAL = "individual"
COMMAND_INDIVIDUAL_SEARCH = "search "
COMMAND_INDIVIDUAL_IPO = "ipo"
COMMAND_INDIVIDUAL_PRICING = "pricing "
COMMAND_INDIVIDUAL_BACK = "back"
COMMAND_INDIVIDUAL_HELP = "help"
INDIVIDUAL_MISSING_TICKER = "Please select a ticker by using the \"search <ticker>\" command"
def main():
......@@ -25,6 +22,8 @@ def main():
print("Successful connection")
break
cursor = db.cursor(buffered = True, dictionary = True)
# Run the "interface" loop
print("Type \"help\" for instructions")
while True:
......@@ -32,7 +31,11 @@ def main():
if user_input == COMMAND_HELP:
help()
elif user_input == COMMAND_INDIVIDUAL:
individual()
individual(cursor)
elif user_input == COMMAND_INSERT:
insert(db, cursor)
elif user_input == COMMAND_DELETE:
delete(cursor)
elif user_input == COMMAND_EXIT:
print("Closing connection...")
db.close()
......@@ -43,75 +46,28 @@ def main():
def help():
print("Main help")
def individual():
print(SEPARATOR + "\nEntered individual mode\nType \"help\" for additional instructions\n" + SEPARATOR)
active_ticker = None
while True:
print("Active ticker: <" + ("NONE SELECTED" if active_ticker is None else active_ticker) + ">")
user_input = input().lower()
if user_input == COMMAND_INDIVIDUAL_HELP:
individual_help()
elif user_input.startswith(COMMAND_INDIVIDUAL_SEARCH):
ticker = user_input.removeprefix(COMMAND_INDIVIDUAL_SEARCH).upper()
valid = True # TODO - Check if the ticker is valid by referencing the database
if valid:
active_ticker = ticker
else:
print("Ticker <" + ticker + "> is invalid.")
elif user_input == COMMAND_INDIVIDUAL_IPO:
if active_ticker is None:
print(INDIVIDUAL_MISSING_TICKER)
else:
print("ipo data for " + active_ticker) # TODO - get actual data from the database
elif user_input == COMMAND_INDIVIDUAL_REVENUE:
if active_ticker is None:
print (INDIVIDUAL_MISSING_TICKER)
else:
print ("revenue data for " + active_ticker) # TODO - get actual revenue data from the database
elif user_input.startswith(COMMAND_INDIVIDUAL_PRICING):
if active_ticker is None:
print(INDIVIDUAL_MISSING_TICKER)
else:
print("parse date and print pricing data for " + active_ticker) # TODO - get actual pricing data from the database
elif user_input == COMMAND_INDIVIDUAL_BACK:
print(SEPARATOR + "\nLeaving \"individual\" mode\n" + SEPARATOR)
break
else:
print("Unknown command, type \"help\" for instructions")
def individual_help():
print("Individual help")
def login():
host = "marmoset04.shoshin.uwaterloo.ca" #input("Host: ")
host = "marmoset03.shoshin.uwaterloo.ca" #input("Host: ")
user = input("Username: ")
password = input("Password: ")
database = input("Database: ")
try:
return mysql.connector.connect(
host = host,
user = user,
password = password
password = password,
database = database
)
except:
return None
def help():
print("Main help")
if __name__ == "__main__":
main()
\ No newline at end of file
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment