Skip to content

Commit 2de770c

Browse files
authored
Add files via upload
1 parent cbb2e86 commit 2de770c

File tree

16 files changed

+975
-0
lines changed

16 files changed

+975
-0
lines changed

balance.py

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
import pickle
2+
import tkinter as tk
3+
from tkinter import messagebox
4+
5+
6+
class Portfolio:
7+
def __init__(self, username, balance):
8+
self.username = username
9+
self.balance = balance
10+
self.stock_list = {}
11+
12+
13+
def add_balance_util(a, b):
14+
s = int(b.get())
15+
a.balance += s
16+
17+
filename = 'user_data/' + a.username + '.pkl'
18+
pickle_out = open(filename, 'wb')
19+
pickle.dump(a, pickle_out)
20+
pickle_out.close()
21+
22+
messagebox.showinfo("Confirmation Message", str(s) + " was added to your account.")
23+
24+
25+
def add_balance(x):
26+
add_balance_window = tk.Tk()
27+
add_balance_window.title('Adding balance to your account!')
28+
add_balance_window.geometry('400x200')
29+
30+
add_balance_frame = tk.Frame(add_balance_window)
31+
add_balance_frame.pack(side='top', fill='x')
32+
33+
add_balance_label = tk.Label(add_balance_frame, text='Enter the amount to add ')
34+
add_balance_label.pack(side='left')
35+
36+
add_balance_entry = tk.Entry(add_balance_frame)
37+
add_balance_entry.pack(side='left')
38+
39+
add_balance_button = tk.Button(add_balance_window, text='Ok',
40+
command=lambda a=x, b=add_balance_entry: add_balance_util(a, b))
41+
add_balance_button.pack(side='bottom', fill='x')
42+
43+
44+
#Main function of this module starts from here hence the name start LOL. Username passed as user from home function
45+
def start(user):
46+
temp = Portfolio('', 0)
47+
filename = 'user_data/' + user + '.pkl'
48+
file = open(filename, 'rb')
49+
temp = pickle.load(file)
50+
51+
#closed the original loaded pickle user file.
52+
file.close()
53+
54+
# temp has temporary variables extracted from pickled files. Successfully printed values.
55+
# print(temp.username)
56+
# print(temp.balance)
57+
# print(temp.stock_list)
58+
59+
#Opening a new window layout
60+
balance_window = tk.Tk()
61+
balance_window.title('Your Account information!')
62+
balance_window.geometry('300x300')
63+
64+
exit_button = tk.Button(balance_window, text='Exit', command=balance_window.destroy)
65+
exit_button.pack(side='bottom', fill='x')
66+
67+
#Passing temp object as an argument (x) to permanently modify and write new balance value.
68+
balance_button = tk.Button(balance_window, text='Add Balance', command=lambda x=temp: add_balance(x))
69+
balance_button.pack(side='bottom', fill='x')
70+
71+
balance_label = tk.Label(balance_window, text='Your current balance is Rs ' + str(temp.balance))
72+
balance_label.pack(side='bottom', fill='x')
73+
74+
balance_window.mainloop()
75+
76+

buy.py

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
import pickle
2+
import tkinter as tk
3+
import requests
4+
from bs4 import BeautifulSoup
5+
from tkinter import messagebox
6+
7+
8+
class Portfolio:
9+
def __init__(self, username, balance):
10+
self.username = username
11+
self.balance = balance
12+
self.stock_list = {}
13+
14+
list_stock = ['ADANIPORTS', 'ASIANPAINT', 'AXISBANK', 'BAJAJ-AUTO', 'BAJFINANCE', 'BAJAJFINSV', 'BPCL',
15+
'BHARTIARTL', 'INFRATEL', 'CIPLA', 'COALINDIA', 'DRREDDY', 'EICHERMOT', 'GAIL', 'GRASIM',
16+
'HCLTECH', 'HDFCBANK', 'HEROMOTOCO', 'HINDALCO', 'HINDPETRO', 'HINDUNILVR', 'HDFC', 'ITC',
17+
'ICICIBANK', 'IBULHSGFIN', 'IOC', 'INDUSINDBK', 'INFY', 'KOTAKBANK', 'LT', 'LUPIN', 'M&M',
18+
'MARUTI', 'NTPC', 'ONGC', 'POWERGRID', 'RELIANCE', 'SBIN', 'SUNPHARMA', 'TCS', 'TATAMOTORS',
19+
'TATASTEEL', 'TECHM', 'TITAN', 'UPL', 'ULTRACEMCO', 'VEDL', 'WIPRO', 'YESBANK', 'ZEEL']
20+
21+
22+
def on_buy_util(a, b, c, d):
23+
unit = int(b.get())
24+
25+
#Balance update
26+
a.balance = a.balance - (unit * c)
27+
28+
#Portfolio update
29+
a.stock_list[d] = unit
30+
31+
filename = 'user_data/' + a.username + '.pkl'
32+
file = open(filename, 'wb')
33+
34+
#Storing updated values in pickled database.
35+
pickle.dump(a, file)
36+
file.close()
37+
38+
success_text = 'Congratulations! You have bought ' + str(unit) + ' of ' + d
39+
messagebox.showinfo(success_text)
40+
41+
42+
def on_buy_func(user, y):
43+
my_stock = y.get()
44+
45+
temp = Portfolio('', 0)
46+
filename = 'user_data/' + user + '.pkl'
47+
file = open(filename, 'rb')
48+
temp = pickle.load(file)
49+
50+
print(temp.balance)
51+
52+
try:
53+
url = 'https://www.screener.in/company/' + my_stock
54+
response = requests.get(url)
55+
data = response.text
56+
57+
except requests.exceptions.RequestException as exp:
58+
print(exp)
59+
60+
soup = BeautifulSoup(data, 'lxml')
61+
62+
data_items = soup.findAll('b')[0:4]
63+
stock_data = [item.text for item in data_items]
64+
65+
current_price = stock_data[1]
66+
current_price = current_price.replace(',','')
67+
68+
current_price = int(float(current_price))
69+
70+
if current_price > temp.balance:
71+
messagebox.showinfo("You cannot buy this stock due to low balance.")
72+
73+
else:
74+
quantity = temp.balance // current_price
75+
msg = 'You can buy ' + str(quantity) + ' of this stock.'
76+
77+
#So far so good. Now call the on_buy_util function, pass quantity, user
78+
on_buy_window = tk.Tk()
79+
on_buy_window.geometry('400x200')
80+
on_buy_window.title('Confirmation for buying ' + my_stock)
81+
82+
t1 = 'The current price of ' + my_stock + ' is ' + str(current_price)
83+
l1 = tk.Label(on_buy_window, text=t1)
84+
l1.pack(side='top')
85+
86+
t2 = 'Your current balance is ' + str(temp.balance)
87+
l2 = tk.Label(on_buy_window, text=t2)
88+
l2.pack(side='top')
89+
90+
l3 = tk.Label(on_buy_window, text=msg)
91+
l3.pack(side='top')
92+
93+
on_buy_frame = tk.Frame(on_buy_window)
94+
quantity_label = tk.Label(on_buy_frame, text='Select quantity')
95+
quantity_label.pack(side='left')
96+
97+
on_buy_entry = tk.Entry(on_buy_frame)
98+
on_buy_entry.pack(side='left')
99+
100+
on_buy_frame.pack(side='top')
101+
102+
on_buy_button = tk.Button(on_buy_window, text='Buy now',
103+
command=lambda a=temp, b=on_buy_entry, c=current_price, d=my_stock: on_buy_util(a, b, c, d))
104+
on_buy_button.pack(side='bottom')
105+
106+
on_buy_window.mainloop()
107+
108+
109+
def start(user):
110+
buy_window = tk.Tk()
111+
buy_window.title('Buy a stock!')
112+
buy_window.geometry('450x200')
113+
114+
buy_frame = tk.Frame(buy_window)
115+
buy_label = tk.Label(buy_frame, text='Enter a stock to buy')
116+
buy_label.pack(side='left')
117+
118+
buy_entry = tk.Entry(buy_frame)
119+
buy_entry.pack(side='left')
120+
121+
buy_frame.pack(side='top')
122+
123+
buy_button = tk.Button(buy_window, text='Buy this stock', command=lambda x=user, y=buy_entry: on_buy_func(x, y))
124+
buy_button.pack(side='bottom')
125+
126+
buy_window.mainloop()
127+

datastore.db

24 KB
Binary file not shown.

0 commit comments

Comments
 (0)