This repository was archived by the owner on Apr 28, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathportfolio.py
More file actions
executable file
·139 lines (113 loc) · 4.36 KB
/
portfolio.py
File metadata and controls
executable file
·139 lines (113 loc) · 4.36 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
#!/usr/bin/env python
import sys
import pandas as pd
from pandas_datareader.data import get_quote_yahoo
import reports
if len(sys.argv) == 1:
print("No args given, using example data...")
data = pd.read_csv("example_data.csv")
else:
if sys.argv[1].endswith(".csv"):
data = pd.read_csv(sys.argv[1])
elif sys.argv[1].endswith(".gpg"):
from encryption import getDataFromEncryptedFile as GDFEF
decryptedData = GDFEF(sys.argv[1])
data = pd.read_csv(decryptedData)
else:
print("Invalid filetype")
def getQuotes(data):
tickers = data["Ticker"]
print("Getting current prices from Yahoo Finance...")
balances = []
for i in range(0, len(tickers)):
ac = data["AC"][i]
# If Asset Class is cash, then get amount only
if ac == "CASH":
balances.append(data["Amount"][i])
else:
try:
currPrice = get_quote_yahoo(tickers[i])["price"][0]
amount = data["Amount"][i]
balances.append(round(currPrice*amount, 2))
except Exception:
print("No quote for this ticker")
data["Balance"] = balances
return data
def getTotalBalance():
totalBalance = 0.0
for i in range(0, len(data)):
totalBalance += data["Balance"][i]
return totalBalance
def getAssetAllocation():
labels = "Stocks", "Bonds", "Commodities", "Gold", "Crypto", "Cash"
stocks, bonds, commodities, gold, crypto, cash = 0, 0, 0, 0, 0, 0
for i in range(0, len(data)):
ac = data["AC"][i]
if ac == "S":
stocks += data["Balance"][i]
elif ac == "B":
bonds += data["Balance"][i]
elif ac == "C":
commodities += data["Balance"][i]
elif ac == "G":
gold += data["Balance"][i]
elif ac == "CRYPTO":
crypto += data["Balance"][i]
elif ac == "CASH":
cash += data["Balance"][i]
return labels, (stocks, bonds, commodities, gold, crypto, cash)
def getAllocationForClass(c):
cdata = data.loc[data['AC'] == c]
labels = []
ticker_balances = []
for i in range(0, len(cdata)):
labels.append(cdata["Name"].iloc[i])
ticker_balances.append(cdata["Balance"].iloc[i])
return labels, ticker_balances
def analyzeAC():
stocks, bonds, commodities, gold, crypto, cash = getAssetAllocation()[1]
totalBalance = getTotalBalance()
pctStocks = stocks/totalBalance * 100
pctBonds = bonds/totalBalance * 100
pctCommodities = commodities/totalBalance * 100
pctGold = gold/totalBalance * 100
pctCrypto = crypto/totalBalance * 100
pctCash = cash/totalBalance * 100
print("Percentage of stocks: " + str(round(pctStocks, 2))+"%")
print("Percentage of bonds: " + str(round(pctBonds, 2))+"%")
print("Percentage of commodities: " + str(round(pctCommodities, 2))+"%")
print("Percentage of gold: " + str(round(pctGold, 2))+"%")
print("Percentage of crypto: " + str(round(pctCrypto, 2))+"%")
print("Percentage of cash: " + str(round(pctCash, 2))+"%")
print("================================================================")
def getDataFrame(filename):
data = pd.read_csv(filename)
df = getQuotes(data)
return df
def main():
getQuotes(data)
cmdOptions = [
'\033[1m' + '(a)'+'\033[0m'+'nalyze', # analyze portfolio (e.g. for market conditions)
'\033[1m' + '(b)'+'\033[0m'+'alance', # show balance
'\033[1m' + '(r)'+'\033[0m'+'eports', # show reports
'\033[1m' + '(q)'+'\033[0m'+'uit' # quit
]
greetingText = '\033[1m' + "Hello, what do you want to do?" + '\033[0m' + " (Choose option and hit Enter)\n"
for co in cmdOptions:
greetingText += " " + co+"\n"
greetingText += ">"
cmd = ""
while cmd != "q":
cmd = input(greetingText)
print("================================================================")
if cmd == "a":
analyzeAC()
elif cmd == "b":
print(data)
print("Your total balance is: " +
str(round(getTotalBalance(), 2))+"EUR")
print("================================================================")
elif cmd == "r":
reports.plotReports(getAssetAllocation(), getAllocationForClass("S"), getAllocationForClass("CRYPTO"))
if __name__ == "__main__":
main()