-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
431 lines (365 loc) · 12.6 KB
/
app.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
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
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
from flask import (
Flask,
jsonify,
render_template,
request,
redirect,
url_for,
flash,
session,
)
from flask_sqlalchemy import SQLAlchemy
from werkzeug.security import generate_password_hash, check_password_hash
from real_time_price import real_time_price, fetch_real_time, get_prev_closing
from information import stock_name_info
import sqlite3
import pandas as pd
from graph_nifty import main
from top_bottom_3 import top_bottom_3_helper, top_bottom_3
from datetime import date
import pandas as pd
import yfinance as yf
import json
app = Flask(__name__, static_url_path="/static")
app.secret_key = "1234" # Replace with your actual secret key
# Database Configuration
app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite:///users.db"
app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False
db = SQLAlchemy(app)
# User Model
class User(db.Model):
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(100), unique=True, nullable=False)
password_hash = db.Column(db.String(200), nullable=False)
balance = 1000000
# Initialize Database within Application Context
with app.app_context():
db.create_all()
@app.route("/filter")
def filter():
return render_template("filter.html")
@app.route("/")
def index():
return render_template("index_nada.html")
dic = {}
@app.route("/update_data")
def update_data():
# Fetch new data and update the dynamic_data dictionary
dic = fetch_real_time()
return dic
def create_bought_database(user_name):
# Adjust the path and base filename as needed
source_path = "instance/temp.json"
destination_path = f"instance"
new_filename = user_name
with open(source_path, "r") as source_file:
data = json.load(source_file)
# Write the content to the destination with the new filename
with open(f"{destination_path}/{new_filename}.json", "w") as destination_file:
json.dump(data, destination_file, indent=2)
@app.route("/register", methods=["GET", "POST"])
def register():
if request.method == "POST":
username = request.form["username"]
password = request.form["password"]
hashed_password = generate_password_hash(password, method="pbkdf2:sha256")
existing_user = User.query.filter_by(username=username).first()
print(existing_user)
if existing_user:
flash("Username already exists! Please choose a different username.")
error = "Username already exists! Please choose a different username."
return render_template("register.html", error=error)
else:
new_user = User(username=username, password_hash=hashed_password)
db.session.add(new_user)
db.session.commit()
create_bought_database(user_name=username)
flash("Registration successful! Please login.")
return redirect(url_for("login"))
else:
return render_template("register.html")
@app.route("/login", methods=["GET", "POST"])
def login():
error = None
if request.method == "POST":
username = request.form["username"]
password = request.form["password"]
user = User.query.filter_by(username=username).first()
if user and check_password_hash(user.password_hash, password):
session["user_id"] = user.id
session["username"] = user.username
session["balance"] = user.balance
return redirect(url_for("dashboard"))
else:
flash("Invalid username or password")
error = "Invalid username or password!"
# return redirect(url_for("index"))
return render_template("login.html", error=error)
else:
return render_template("login.html")
@app.route("/dashboard")
def dashboard():
if "user_id" in session:
company_name, price, change, percentage_change, pe_ratio = real_time_price(
"NIFTY_50", "INDEXNSE"
)
return render_template(
"welcome_nada.html",
username=session["username"],
price=price,
prev=change,
percentage_change=percentage_change,
)
else:
return redirect(url_for("index"))
@app.route("/profile")
def profile():
if "user_id" in session:
return render_template(
"profile.html",
username=session["username"],
)
else:
return redirect(url_for("index"))
@app.route("/get_balance")
def get_balance():
if "user_id" in session:
username = session["username"]
query = f"SELECT balance FROM user WHERE username = '{username}'"
conn = sqlite3.connect(f"instance/users.db")
cursor = conn.cursor()
cursor.execute(query)
result = cursor.fetchall()
conn.close()
result = round(result[0][0], 3)
return [result]
else:
return None
gb_imp_do_not_change = 0
@app.route("/stock")
def stock():
if "user_id" in session:
company_name, price, change, percentage_change, pe_ratio = real_time_price(
"NIFTY_50", "INDEXNSE"
)
gb_imp_do_not_change = percentage_change
svg = main("NIFTY_50", "INDEXNSE")
return render_template(
"stock.html",
username=session["username"],
price=price,
change=change,
prev=round(price - change, 3),
percentage_change=percentage_change,
svg=svg,
pe_ratio=pe_ratio,
user_name=session["username"],
)
else:
return redirect(url_for("index"))
@app.route("/stocks_list", methods=["GET", "POST"])
def stocks_list():
return render_template("stocks_list.html")
@app.route("/stock_particular", methods=["GET", "POST"])
def stock_particular():
stock_name = request.args.get("stock_name")
picture, info = stock_name_info(stock_name)
df = pd.read_csv("stock_name.csv")
df.set_index("Company Name", inplace=True)
stock_n = stock_name
symbol = df.loc[stock_n, "Symbol"]
company_name, price, change, percentage_change, pe_ratio = real_time_price(
symbol, "NSE"
)
prev = get_prev_closing(symbol, "NSE")
svg = main(symbol, "NSE")
return render_template(
"stock_particular.html",
stock_name=stock_name,
picture=picture,
price=price,
change=change,
percentage_change=percentage_change,
pe_ratio=pe_ratio,
prev=prev,
svg=svg,
info=info,
user_name=session["username"],
)
@app.route("/default_filter", methods=["POST"])
def default_filter():
data = request.json
category = data["category"]
month = int(data["month"])
lowerbound = float(data["lowerBound"])
upperbound = float(data["upperBound"])
company_name = pd.read_csv("stock_name.csv")
result = {}
for name in company_name["Company Name"]:
if name == "Nifty_Fifty" or name == "Sensex":
continue
db_path = f"historical_data/{name}.db"
conn = sqlite3.connect(db_path)
cursor = conn.cursor()
query = f"SELECT AVG({category}) FROM stock_data_table WHERE DATE >= date('now', '-{month} months')"
cursor.execute(query)
average_value = cursor.fetchone()[0]
if average_value >= lowerbound and average_value <= upperbound:
result[name] = average_value
conn.close()
# print(result)
return jsonify(result)
@app.route("/PE_filter", methods=["POST"])
def PE_filter():
data = request.json
month = int(data["month"])
lowerbound = float(data["lowerBound"])
upperbound = float(data["upperBound"])
company_name = pd.read_csv("stock_name.csv")
result = {}
for row in yester_day_data:
if row[1] >= lowerbound and row[1] <= upperbound:
result[row[0]] = row[1]
# print(result)
return jsonify(result)
@app.route("/BUY", methods=["POST"])
def BUY():
data = request.json
quantity = float(data.get("quantity"))
price = float(data.get("price"))
user = data.get("user")
stock = data.get("stock")
print(data.get("balance"))
balance = float(data.get("balance"))
# print(stock,user,type(price),type(quantity),type(balance),balance)
print(user)
if balance < quantity * price:
response_data = {"message": "Not Enough Balance"}
return jsonify(response_data)
else:
query = f"UPDATE user SET balance = {balance} - {quantity*price} WHERE username = '{user}' "
conn = sqlite3.connect(f"instance/users.db")
cursor = conn.cursor()
cursor.execute(query)
conn.commit()
conn.close()
file_path = f"instance/{user}.json"
with open(file_path, "r") as json_file:
data = json.load(json_file)
# Check if a row with the specified name exists
data[stock] = float(data[stock]) + quantity
# Write the updated data back to the JSON file
with open(file_path, "w") as json_file:
json.dump(data, json_file, indent=2)
response_data = {
"message": "BOUGHT SUCCESSFULLY!",
"stock_quantity": data[stock],
}
return jsonify(response_data)
@app.route("/SELL", methods=["POST"])
def SELL():
data = request.json
quantity = float(data.get("quantity"))
price = float(data.get("price"))
user = data.get("user")
stock = data.get("stock")
balance = float(data.get("balance"))
file_path = f"instance/{user}.json"
with open(file_path, "r") as json_file:
data = json.load(json_file)
bought = data[stock]
if quantity > bought:
response_data = {
"message": "Not Enough Stocks in Balance",
"stock_quantity": data[stock],
}
return jsonify(response_data)
else:
query = f"UPDATE user SET balance = {balance} + {quantity*price} WHERE username = '{user}' "
conn = sqlite3.connect(f"instance/users.db")
cursor = conn.cursor()
cursor.execute(query)
conn.commit()
conn.close()
data[stock] = float(data[stock]) - quantity
with open(file_path, "w") as json_file:
json.dump(data, json_file, indent=2)
response_data = {"message": "SOLD SUCCESSFULLY!", "stock_quantity": data[stock]}
return jsonify(response_data)
@app.route("/Profit")
def profit():
user = ""
if "user_id" in session:
user = session["username"]
file_path = f"instance/{user}.json"
with open(file_path, "r") as json_file:
data = json.load(json_file)
pr = {}
temp = {}
for row in yester_day_data:
temp[row[0]] = [row[1], row[2]]
df = pd.read_csv("stock_name.csv")
for name, symbol in zip(df["Company Name"], df["Symbol"]):
if name == "Nifty_Fifty" and data[name] != 0:
pr[name] = []
color = "green" if gb_imp_do_not_change > 0 else "red"
pr[name].append([gb_imp_do_not_change, color])
pr[name].append(data[name])
elif data[name] != 0:
# print(name)
pr[name] = []
# print(temp[symbol])
# print(data[name])
pr[name].append(temp[symbol])
pr[name].append(data[name])
print(pr)
return jsonify(pr)
@app.route("/get_quantity")
def stock_quantity():
stock = request.args.get("stock")
user = session["username"]
file_path = f"instance/{user}.json"
with open(file_path, "r") as json_file:
data = json.load(json_file)
quantity = data.get(stock)
return [quantity]
@app.route("/compare_stock")
def compare_stock():
return render_template("compare_stock.html")
@app.route("/logout")
def logout():
session.pop("user_id", None)
session.pop("username", None)
session.pop()
return redirect(url_for("index"))
@app.route("/query", methods=["POST"])
def query_database():
data = request.json # assuming data is sent in JSON format
startDate = data.get("startDate")
endDate = data.get("endDate")
stock = data.get("stock")
query = f"SELECT DATE(DATE) as date,CLOSE FROM stock_data_table WHERE date BETWEEN '{startDate}' AND '{endDate}'"
conn = sqlite3.connect(f"historical_data/{stock}.db")
cursor = conn.cursor()
cursor.execute(query)
result = cursor.fetchall()
conn.close()
return jsonify(result)
@app.route("/top_3")
def top_3():
# data = top_bottom_3()
top = data[0]
bottom = data[1]
dic = {}
dic[0] = top[0]
dic[1] = top[1]
dic[2] = top[2]
dic[3] = bottom[0]
dic[4] = bottom[1]
dic[5] = bottom[2]
return dic
if __name__ == "__main__":
yester_day_data = top_bottom_3_helper()
data = top_bottom_3(yester_day_data)
app.run(debug=True)
# data = top_bottom_3()