-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmenu.py
More file actions
404 lines (370 loc) · 14.2 KB
/
menu.py
File metadata and controls
404 lines (370 loc) · 14.2 KB
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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
import imp
from itertools import accumulate
from operator import contains
import string
from tokenize import group
from unittest import result
from xml.dom.minidom import Document
from collections import Counter as Count
import matplotlib.pyplot as plt
from numpy import choose
from main import *
from pymongo import *
from termcolor import colored
import finnhub, datetime, requests, os, json
import numpy as np
from bson.objectid import ObjectId
from alpha_vantage.timeseries import TimeSeries
def menu():
"""
Stores users options in dictionay
"""
menu = {
1 : 'Choose Account',
2 : 'View Stock Price',
3 : 'Purchase',
4 : 'Sell',
5 : 'View Holdings',
6 : 'Load Holdings from JSON',
7 : 'Delete Holdings',
8 : 'Force Update Holdings',
9 : 'Historical Prices Graph',
0 : 'Exit'
}
return menu
def PaperT():
"""
ACII art
"""
print(
'''
_______ _______ _______ _______ ______ _______ ______ _______ ______ _______ ______
| || _ || || || _ | | || _ | | _ || | | || _ |
| _ || |_| || _ || ___|| | || |_ _|| | || | |_| || _ || ___|| | ||
| |_| || || |_| || |___ | |_||_ | | | |_||_ | || | | || |___ | |_||_
| ___|| || ___|| ___|| __ | | | | __ || || |_| || ___|| __ |
| | | _ || | | |___ | | | | | | | | | || _ || || |___ | | | |
|___| |__| |__||___| |_______||___| |_| |___| |___| |_||__| |__||______| |_______||___| |_|
'''
)
def clear():
"""
clears the terminal
"""
os.system('cls' if os.name=='nt' else 'clear')
return(" ")
def printMenu():
"""
print s the menu so user can see the options
"""
PaperT()
x = menu()
for i in x:
print(f'{i} {x[i]}')
class stocks():
def __init__() -> None:
pass
def choose_account():
"""
chooses an account
isnt case senstive but does require y/n to start
adds to a user document to store current user
adds to users for reusability
0 to exit
"""
print('Please log in')
print('\npress 0 to go back')
Account = input('Do you have an account: Y/N: ')
yesno = ['y', 'n']
if str.lower(Account) in yesno:
try:
if str.lower(Account) == ('y'):
Account = input('Enter your account name: ')
with open('users.txt') as f:
if Account in f.read():
txt = colored(f'Welcome {Account}', 'green')
clear()
print(txt)
f = open('user.txt', 'w')
f.write(Account)
f.close
return Account
else:
name = colored(Account, 'red')
print(f'Account: {name} not found please try again')
stocks.choose_account()
else:
Account = input('Enter a name to create an account: ')
txt = colored(f'Account created username: {Account}', 'green')
clear()
print(txt)
newuser = open('users.txt', 'a')
newuser.writelines(f'{Account}\n')
newuser.close
user = open('user.txt', 'w')
user.write(f'{Account}')
user.close
return Account,
except Exception as e:
print("An exception occurred ::", e)
elif Account == '0':
clear()
else:
txt = colored(f'\nPlease enter Y or N not {Account}', 'red')
print(txt)
stocks.choose_account()
def addholdings(totalP, ticker, data, quant):
"""
adds a stock purchase to the mongo db
need to add read write to json
reads current user from the user file
"""
try:
client = MongoClient() #connect to the server
db = client.Stonks #returns an object pointing to db test
collection = db.Holdings
Pdate = datetime.datetime.now()
with open('user.txt','r') as f:
lines = f.readlines()
user = lines[0]
if lines == None:
stocks.choose_account()
doc = {
'User': user,
'Ticker': ticker.upper(),
'PricePer': f'{data:.2f}',
'#Shares' : int(quant),
'TotalPrice': f'{totalP:.2f}',
'Date': Pdate
}
doc_id = collection.insert_one(doc).inserted_id
print(f'inserted {doc_id}')
except Exception as e:
print("An exception occurred ::", e)
def view_stockP():
"""
does an api call to the finnhub api to get current stock price
{"c": 261.74,"h": 263.31,"l": 260.68,"o": 261.07,"pc": 259.45,"t": 1582641000}
take the current price and return
"""
try:
ticker = input('Enter stock ticker: ')
data = stocks.get_price(ticker)
if data == 0:
redtxt = colored(f'{ticker} is incorrect', 'red')
print(f'Please enter a correct stock ticker {redtxt}')
stocks.view_stockP()
elif ticker == '0':
clear()
pass
else:
clear()
print(f'\n{ticker.upper()} is ${data:.2f}')
return data
except Exception as e:
print("An exception occurred ::", e)
def get_price(ticker):
try:
ticker = ticker.upper()
finnhub_client = finnhub.Client(api_key="c8tssuiad3i91cikglc0")
data = (finnhub_client.quote(ticker))
data = (data['c'])
return data
except Exception as e:
print("An exception occurred ::", e)
def purchase():
"""
This function allows you to purchase a stock calls get_price for price
then asks user y/n to purchase
if yes then user is asked how many shares if shares is not a number it cancels the order
if they to buy it calls mongoDB func and adds the purchase to the db
"""
clear()
yesno = ['y', 'n']
ticker = input('Enter stock ticker: ')
data = stocks.get_price(ticker)
if data == 0:
redtxt = colored(f'{ticker.upper()} is not a ticker', 'red')
print(f'Please enter a correct stock ticker {redtxt}')
stocks.purchase()
else:
buy = input(f'\n{ticker.upper()} is ${data:.2f} would you like to buy y/n ')
if str.lower(buy) in yesno:
if str.lower(buy) == 'y':
txt = colored('WARNING', 'red')
print(f'{txt} if you dont enter a number your order will be canceled\n')
quant = float(input('How many shares do you want to buy: '))
try:
quant = abs(quant)
totalP = float(quant) * data
clear()
stocks.addholdings(totalP, ticker, data, quant)
print(f'{int(quant)} shares of {ticker.upper()} purchased for {totalP:.2f}')
return totalP
except Exception as e:
print("An exception occurred ::", e)
def remove():
client = MongoClient() #connect to the server
db = client.Stonks #returns an object pointing to db test
collection = db.Holdings
clear()
try:
ticker = input('Enter stock ticker: ')
ticker = ticker.upper()
T_del = {"Ticker": ticker}
trash = collection.delete_many(T_del)
print(f'{trash.deleted_count} documents deleted')
except Exception as e:
print("An exception occurred ::", e)
def sell(ticker):
price = stocks.get_price(ticker)
def holdings():
"""
conects to db calls stonks collection
finds holdings using the user
"""
clear()
tickers = []
try:
with open('user.txt','r') as f:
user = f.readlines()
user = user[0]
if user == None:
print('Please login')
pass
else:
client = MongoClient() #connect to the server
db = client.Stonks #returns an object pointing to db test
clear()
print('=========================================')
for holding in db.Holdings.find({'User': f'{user}'}):
print('\n')
del holding['_id']
tickers.append(holding['Ticker'])
for key in holding:
print(f'{key}: {holding[key]}')
except Exception as e:
print("An exception occurred ::", e)
finally:
print('\nYour holdings contain:\n')
count = Count(tickers)
for key in count:
print(f'{key}: {count[key]}', end=" ")
def loadHoldings():
"""
Loads holdings from the data.json file
checks if list to see if insert many is needed
else just uses insert one
"""
client = MongoClient() #connect to the server
db = client.Stonks #returns an object pointing to db test
collection = db.Holdings
clear()
print('\n=========================================')
try:
with open('data.json') as file:
file_data = json.load(file)
if isinstance(file_data, list):
collection.insert_many(file_data)
x = len(file_data)
print(f'{x} docs inserted')
else:
collection.insert_one(file_data)
print('1 file inserted')
except Exception as e:
print("An exception occurred ::", e)
def updateH():
"""
connects to mongo db
prints all holdings in the db collection
updates a single stock purchase to a different user based on _id
"""
client = MongoClient() #connect to the server
db = client.Stonks #returns an object pointing to db test
collection = db.Holdings
clear()
try:
for holding in db.Holdings.find():
print('\n')
for key in holding:
print(f'{key}: {holding[key]}')
update = input('please enter the _id you want to update: ')
find = {'_id': ObjectId(update)}
new_user = input('please enter the user you want to update: ')
new = {"$set": {'User': new_user}}
collection.update_one(find, new)
changed = collection.find_one({'_id': ObjectId(update)})
print('\n')
clear()
for key in changed:
print(f'{key}: {changed[key]}')
except Exception as e:
print("An exception occurred ::", e)
def sale(ticker):
"""
deletes from the database after a sale
"""
client = MongoClient() #connect to the server
db = client.Stonks #returns an object pointing to db test
collection = db.Holdings
try:
T_del = {"Ticker": ticker}
trash = collection.delete_many(T_del)
print(f'{trash.deleted_count} documents deleted')
except Exception as e:
print("An exception occurred ::", e)
def sell():
"""
funtion to sell stocks
"""
try:
clear()
avg = []
num_shares = []
ticker = input('Enter stock ticker: ')
curr_price = stocks.get_price(ticker)
client = MongoClient() #connect to the server
db = client.Stonks #returns an object pointing to db test
collection = db.Holdings
ticker = ticker.upper()
with open('user.txt','r') as f:
user = f.readlines()
user = user[0]
for holding in collection.find({
'Ticker': f'{ticker}',
'User': f'{user}'
}):
avg.append(holding['PricePer'])
num_shares.append(holding['#Shares'])
avg = np.float_(avg)
num_shares = np.float_(num_shares)
num_shares = sum(num_shares)
avg_Price = sum(avg)/len(avg)
profitPer = curr_price - avg_Price
profit = (num_shares * curr_price) - (num_shares * avg_Price)
print(f'You made : ${profit:.2f}')
stocks.sale(ticker)
ticker = 'USD'
stocks.addholdings(profit, ticker, profitPer, num_shares)
except Exception as e:
print("An exception occurred ::", e)
def s_hist():
try:
ticker = input('Enter stock ticker: ')
ticker = ticker.upper()
stockchart(ticker)
except Exception as e:
print("An exception occurred ::", e)
def stockchart(ticker):
try:
apikey = "NQP7G8T7UX7KC6DQ"
ts2 = TimeSeries(key=apikey, output_format='pandas')
data = ts2.get_intraday(symbol=ticker,interval='1min', outputsize='full')
# print("index:", data.index)
# print(data)
clear()
data[0]['4. close'].plot()
plt.title(f'Intraday Times Series for the {ticker} stock (1 min)')
plt.show()
except Exception as e:
print("An exception occurred ::", e)