-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathportfolio_maker.py
More file actions
101 lines (88 loc) · 2.53 KB
/
portfolio_maker.py
File metadata and controls
101 lines (88 loc) · 2.53 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
import psycopg2
from For_SQL_chart import create_table
password_sql =
columns = {
"Date": "VARCHAR(50)",
"ticker": "VARCHAR(50)",
"stock_Price": "FLOAT",
"number_of_stock": "INTEGER"
}
# Create Database for Portfolio
create_table("portfolio", columns, "testdb", "postgres", password_sql, host="localhost", port="5432")
# add portfolio
# remove portfolio
def remove_port(ticker):
query = f"""DELETE FROM portfolio
WHERE ticker = '{ticker}';"""
try:
conn = psycopg2.connect(
dbname="testdb",
user="postgres",
password=password_sql,
host="localhost",
port="5432"
)
cur = conn.cursor()
cur.execute(query)
conn.commit()
print(f"Portfolio {ticker} removed.")
except Exception as e:
print("Error: ", e)
finally:
if conn:
cur.close()
conn.close()
remove_port("TSLA")
# Return Purchased price / total investment / number of stock
def get_portfolio(ticker, return_type):
query = f"""
SELECT {return_type}
FROM portfolio
WHERE ticker ILIKE %s;
"""
try:
conn = psycopg2.connect(
dbname="testdb",
user="postgres",
password=password_sql,
host="localhost",
port="5432"
)
cur = conn.cursor()
cur.execute(query, (ticker,))
rows = cur.fetchall()
for row in rows:
return(row[0])
except Exception as e:
print("Error:", e)
finally:
if conn:
cur.close()
conn.close()
def get_all_tickers_and_prices(return_type):
query = f"""
SELECT ticker, {return_type}
FROM portfolio;
"""
try:
conn = psycopg2.connect(
dbname="testdb",
user="postgres",
password=password_sql,
host="localhost",
port="5432"
)
cur = conn.cursor()
cur.execute(query)
rows = cur.fetchall()
for row in rows:
print(f"Ticker: {row[0]}, Price: {row[1]}")
return rows # [("AAPL", 180.3), ("TSLA", 250.5), ...]
except Exception as e:
print("Error:", e)
finally:
if conn:
cur.close()
conn.close()
def get_total_investment(ticker):
return get_portfolio(ticker, stock_price) * get_portfolio(ticker, number_of_stock)