-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
308 lines (270 loc) · 7.81 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
#print("Checking for packages...")
import subprocess
import os
import subprocess
import sys
import pip
import time
'''from pip._internal.utils.misc import get_installed_distributions
files = []
for file in get_installed_distributions():
files.append(file)
for i in files:
if i == "":
print("Please hold as we install some packages!")
time.sleep(1.3)
os.system("pip install yahoo_fin")
os.system("pip install -U textblob")
os.system("python -m textblob.download_corpora")
os.system("clear")
'''
from yahoo_fin.stock_info import *
from yahoo_fin import news
from replit import db
from datetime import date
from pprint import pprint
import sys
import re
repl_user = os.environ['REPL_OWNER']
signed_in = False
username = ""
account_shares = username + "shares"
account_prices = username + "prices"
account_port = username + "portfolio"
account_history = username + "history"
account_balance = username + "balance"
account_status = username + "status"
times_home = 0
balance = 5000
owned_stocks = []
pnum_shares = {}
pprice_got = {}
portfolio = [pnum_shares,pprice_got,owned_stocks]
history = []
account_status = username + "status"
if username != "":
db[account_status] == "Online"
def create_account():
acc_name = input("What would you like your username to be?\n> ")
if acc_name not in db:
acc_pswrd = input("Great! What would you like your password to be?\n> ")
db[acc_name] = acc_pswrd
username == acc_name
db[account_balance] = 5000
db[account_port] = []
db[account_history] = []
db[account_shares] = {}
db[account_prices] = {}
home()
def sign_in():
global username
global balance
username = input("What is your username?\n> ")
if username in db:
si_pswrd = input("Account found! What is your password?\n> ")
if si_pswrd == db[username]:
remember = input("Before we log you in, do you want to turn on 'Remember Me' so you won't have to sign in every time? Type 'yes' or 'no'")
if remember == "yes" or "Yes":
print("Okay, enabling 'Remember Me'...")
db["(repl)"+username] = repl_user
print("Great! Signed in!")
account = username + "data"
if si_pswrd != db[username]:
print("Username and password do not match.")
si_pswrd = input("What is your password?\n> ")
time.sleep(1.5)
os.system("clear")
home()
online = {}
offline ={}
controller = {}
def update_custom():
for account in account_status:
if account == "Online":
online.append(account)
if account == "Offline":
offline.append(account)
if account in controller and account in offline:
controller.pop(account)
def dev_stuff():
print("welcome dev")
accountsl = {}
historiesl = {}
pricesl = {}
portfoliosl = {}
sharesl = {}
usersl = {}
item = ""
for item in db:
histories = re.search("^HISTORY", item)
prices = re.search("^PRICE", item)
portfolios = re.search("^PORTFOLIO", item)
shares = re.search("^SHARE", item)
users = re.search("USER", item)
if histories:
item == re.sub("HISTORY","",str(item))
historiesl[item] = db[item]
if prices:
pricesl[item] = db[item]
if portfolios:
portfoliosl[item] = db[item]
if shares:
sharesl[item] = db[item]
if users:
new_item = re.sub("USERNAME","",item)
usersl[new_item] = db[item]
option = input(">")
if option == "users":
print()
for item in usersl:
print(item+":",db[item])
def add_to_history(share_count,user_stock):
today = date.today()
datetoday = today.strftime("%d/%m/%y")
hist_stock = user_stock
history.append(datetoday+": You bought")
history.append(str(share_count)+" shares of")
history.append(hist_stock)
#difference = price - get_premarket_price(user_stock)
#news = news.get_yf_rss(user_stock)
def portfolio():
print(username+"'s Portfolio")
for item in db[account_port]:
print(item,"\n")
def buy():
global balance
price = get_live_price(user_stock)
share_count = int(input("How many shares?\n> "))
cost = price * share_count
if cost < balance:
confirm = input("Are you sure you want to buy "+user_stock+"? It will cost $"+str(round(cost,2))+".\n> ")
if confirm == "yes" or "Yes":
balance = balance - cost
print("Great! You bought",share_count,"shares of",user_stock,"!")
add_to_history(share_count,user_stock)
owned_stocks.append(user_stock)
if user_stock in pprice_got and user_stock in pnum_shares:
find_avg_price = (pprice_got[user_stock]+price) / 2
find_numshares = pnum_shares[user_stock]+share_count
pprice_got.update({user_stock:find_avg_price})
pnum_shares.update({user_stock:find_numshares})
else:
pprice_got.update({user_stock:price})
pnum_shares.update({user_stock:share_count})
else:
print("Okay! Returning to Home...")
else:
print("You don't have enough in your account!")
time.sleep(2)
os.system("clear")
home()
#def sell():
def find_money_change():
global money_change
money_change = 0
for stock in owned_stocks:
ind_money_change = (get_live_price(stock) - pprice_got[stock]) * pnum_shares[stock]
money_change = money_change + ind_money_change
return(money_change)
def save():
db["SHARE"+account_shares] = pnum_shares
db["PRICE"+account_prices] = pprice_got
db["PORTFOLIO"+account_port] = owned_stocks
db["HISTORY"+account_history] = history
db["USERNAME"+username] = db[username]
db["BALANCE"+account_balance] = balance
db[account_shares] = pnum_shares
db[account_prices] = pprice_got
db[account_port] = owned_stocks
db[account_history] = history
db[username] = db[username]
db[account_balance] = round(balance,2)
print("saved")
def home():
global account_shares
global account_prices
global account_port
global account_history
global account_balance
global price
global user_stock
global money_change
global times_home
global balance
global pnum_shares
global pprice_got
global history
global owned_stocks
global portfolio
global account_status
if username != "" and times_home < 1:
balance = db[account_balance]
portfolio = db[account_port]
pnum_shares = db[account_shares]
pprice_got = db[account_prices]
history = db[account_history]
times_home += 1
elif username == "":
balance = 5000
print("___"+username+"___\nBalance: $"+str(round(balance,2)))
#print(db[account])
if times_home >= 1:
save()
user_action = input()
if user_action == "buy":
user_stock = input("What stock?\n> ")
buy()
if user_action == "sign up":
create_account()
home()
if user_action == "sign in":
sign_in()
home()
if user_action == "show":
print(db[account])
if user_action == "history":
word_count = 0
word = []
for item in db[account_history]:
if word_count < 3:
word.append(item)
word_count += 1
if word_count >= 3:
print(' '.join(word))
word.clear()
word_count -= 3
home()
if user_action == "money":
find_money_change()
print(money_change)
if user_action == "portfolio":
portfolio()
if user_action == "view":
user_stock = input("What stock?\n> ")
price = get_live_price(user_stock)
while True:
print(user_stock,":",round(price,2), end = "\r")
print("\n")
ws_action = input("")
if ws_action == "buy":
buy()
break
home()
if user_action == "status":
print(online,"\n",offline)
#print(difference)
'''
def buy():
user_buy = input("Would you like to buy this?")
if user_buy == "yes":
bought_price = price
print(bought_price)'''
def main():
print("Welcome to The Mock Market! Please sign in or create an account to continue...\n\n")
sioca = input("> ")
if sioca == "create account":
create_account()
if sioca == "sign in":
sign_in()
main()
'''while True:'''