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
28 changes: 21 additions & 7 deletions stock-simulator.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,13 @@ portfolio = {}
history = []
username = ""

# Restarts the current user portfolio to default values
def restart_user_portfolio(line_num):
global username
global portfolio

new_line = username+":10000.00:\n"

with open('portfolio.txt', 'r') as f:
data = f.readlines()

Expand All @@ -31,17 +32,19 @@ def restart_user_portfolio(line_num):
portfolio = {}
parse_portfolio()

# Remove a given user's history
def erase_user_history():
global username
global history

filename = "history/" + username + ".txt"

if os.path.exists(filename):
os.remove(path)

history = []


# Uses pyplot to show changes in individual stocks and the overall portfolio
def show_graphs():

global username
Expand Down Expand Up @@ -125,6 +128,7 @@ def show_graphs():
else:
print("\nThis stock is too recent to have a graph.\n")

# Rerewites the portfolio file
def write_portfolio(line_num):

global username
Expand All @@ -151,6 +155,7 @@ def write_portfolio(line_num):
with open('portfolio.txt', 'w') as f:
f.writelines(data)

# Gets information from the portfolio file
def parse_portfolio():

global username
Expand All @@ -165,7 +170,7 @@ def parse_portfolio():
if account[0] == username:
break
line_num += 1

portfolio["cash"] = float(account[1])
portfolio["stocks"] = {}

Expand All @@ -180,6 +185,7 @@ def parse_portfolio():

return line_num

# Gets values from the current portfolio to display to the user
def show_portfolio():

print("\nPORTFOLIO\n")
Expand Down Expand Up @@ -219,6 +225,7 @@ def show_portfolio():

print("Cash: {}".format(round(cash,2)))

# Gives users the option to buy stocks
def buy_stock():

print("BUY STOCKS\n")
Expand Down Expand Up @@ -279,6 +286,7 @@ def buy_stock():
else:
print("Invalid Quantity. Transaction Aborted.\n")

# Gives users the option to sell stocks
def sell_stock():

print("SELL STOCKS\n")
Expand Down Expand Up @@ -335,6 +343,7 @@ def sell_stock():
else:
print("Invalid Quantity. Transaction Aborted.\n")

# Get the price of any valid ticker
def get_price():

print("\nCHECK PRICE\n")
Expand All @@ -357,6 +366,7 @@ def get_price():
print("Ticker: "+tck)
print("Price: {}\n".format(price))

# Add to a user history
def write_history():
global history
global username
Expand All @@ -383,10 +393,12 @@ def write_history():

f.write(line)

# Get finance articles
def find_articles():
print("\nCURRENT ARTICLES\n")
scrp.get_articles("Finance")

# Show a user a history of their previous transactions
def show_history():

global username
Expand Down Expand Up @@ -455,6 +467,7 @@ def login():
def segment():
print("\n-----------------------------------------------------------------------------------\n")

# Main interface
def main():

parser = argparse.ArgumentParser(description='Either log in or create a new account. Upon creating a new account, you will start with $10,000.00 of cash to invest. After logging in, you can view your portfolio, buy stocks, sell stocks, check stock prices, view your transaction history, see graphs, or send emails to other users about your progress.')
Expand Down Expand Up @@ -517,6 +530,7 @@ def main():

segment()

# Various option actions based on user selection
if (ans == 1):
show_portfolio()
elif (ans == 2):
Expand Down Expand Up @@ -548,7 +562,7 @@ def main():
else:
print("PLEASE PICK A VALID OPTION.\n")


# Runs the main function
if __name__ == '__main__':

main()
main()
6 changes: 1 addition & 5 deletions stock_saving.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ def update_all():
overall[stock][4] = overall[stock][3]
overall[stock][3] = overall[stock][2]
overall[stock][2] = overall[stock][1]
# Newest price is most recent closing price
overall[stock][1] = scrp.scrape_site(stock)
tempString = stock
for i in range(1,8):
Expand All @@ -92,11 +93,6 @@ def update_all():
with open("stockhistory.txt", "a") as file:
file.write(tempString)

# Ignore this
# AAPL:1:2:3:4:5:6:7
# GM:23:23:23:23:23:23:23
# MSFT:-1:-1:-1:-1:-1:-1:-1

# Will likely need to be called to create pyplots
# Returns the current dictionary
def obtain_current():
Expand Down
13 changes: 1 addition & 12 deletions stockhistory.txt
Original file line number Diff line number Diff line change
@@ -1,12 +1 @@
AAPL:282.97:282.97:1.0:2.0:3.0:4.0:5.0
GM:21.95:21.95:23.0:23.0:23.0:23.0:23.0
MSFT:174.55:-1.0:-1.0:-1.0:-1.0:-1.0:-1.0
TSLA:74.55:-1.0:-1.0:-1.0:-1.0:-1.0:-1.0GOOG:-1:-1:-1:-1:-1:-1:-1
GOOG:-1:-1:-1:-1:-1:-1:-1
YEXT:-1:-1:-1:-1:-1:-1:-1
GOOG:-1:-1:-1:-1:-1:-1:-1
GOOG:-1:-1:-1:-1:-1:-1:-1
GOOG:-1:-1:-1:-1:-1:-1:-1
GOOG:-1:-1:-1:-1:-1:-1:-1
GOOG:-1:-1:-1:-1:-1:-1:-1
GOOG:-1:-1:-1:-1:-1:-1:-1
AAPL:-1:-1:-1:-1:-1:-1:-1