|
| 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 | + |
0 commit comments