-
Notifications
You must be signed in to change notification settings - Fork 1
/
mainV2.py
220 lines (189 loc) · 5.67 KB
/
mainV2.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
import datetime
import pickle
import requests
import sys
import os
import time
import json
today=datetime.datetime.today().date()
class Portfolio:
import matplotlib.pyplot
plt=matplotlib.pyplot
dayTotals={}
oldestDay=None
db={}
profit=0
totalDividendsDict={}
apiKey=None
def strDay(self,day):
return datetime.datetime.strftime(day,'%Y-%m-%d')
def fullCWD(self):
cwd=os.getcwd()
if '//' in cwd:
return f'{cwd}//'
return f'{cwd}/'
def finaliseData(self):
from moneyChamber.prices import addPrices
apiKey=self.apiKey
#Download and process stock prices
for stock in self.db:
addPrices(self,stock,apiKey)
#Count daily totals for graph
self.updateDayTotals()
def showGraph(self):
from moneyChamber.graph import showGraphV2
showGraphV2(self)
def showDepositGraph(self):
from moneyChamber.depositGraph import showDepositGraph
showDepositGraph(self)
def __init__(self,statementPath):
self.cwd=self.fullCWD()
self.depositsData=[0]
self.dividendData=[0]
self.loadStatement(statementPath)
def countYeld(self):
class MonthlyYeld:
def __init__(self,divIncome,total):
self.divIncome=round(divIncome,2)
self.total=round(total,2)
self.yeld=round((divIncome/total)*100,3)
def nextMonth(date):
date-=datetime.timedelta(32)
try:
return (datetime.datetime(date.year,date.month,today.day).date())
except ValueError:
return (datetime.datetime(date.year,date.month,today.day-3).date())
def getNewestDay():
d=list(self.dayTotals.keys())[-1]
return (datetime.datetime.strptime(d,'%Y-%m-%d')).date()
newestDay=getNewestDay()
day=newestDay
while day > self.oldestDay:
totalIncome=0
totalWorth=0
for s in self.db:
path=self.db[s].history
if day in path:
amount=path[day].amount
bPrice=path[day].basePrice
total=amount*bPrice
totalIncome+=(total*self.db[s].yeld)
totalWorth+=total
o=MonthlyYeld(totalIncome,totalWorth)
print(day,o.divIncome,o.total,o.yeld)
day=nextMonth(day)
def countYearlyDivYelds(self):
def oldestNewestDay():
newestDay=list(path.keys())[-1]
yearAgo=newestDay-datetime.timedelta(365)
if yearAgo not in path:
yearAgo=self.db[s].date
return yearAgo,newestDay
for s in self.db:
path=self.db[s].history
yearAgo,newestDay=oldestNewestDay()
processedDay=yearAgo
dividend=0
totalYeld=0
while processedDay != newestDay:
stringDay=self.strDay(processedDay)
if stringDay in self.dividendData[s]:
dividend=self.dividendData[s][stringDay]
yeld=dividend/(path[processedDay].amount*path[processedDay].basePrice)
totalYeld+=yeld
processedDay+=datetime.timedelta(1)
prettyYeld=round(totalYeld,3)
if self.db[s].etf == False:
prettyYeld/=100
self.db[s].yeld=prettyYeld
def loadDeposits(self,filename):
path=self.cwd+filename
try:
with open(path,'r') as file:
self.depositsData=json.load(file)
except FileNotFoundError:
print(f'Cannot find file {path}')
self.dividendData={}
def loadDividends(self,filename):
path=self.cwd+filename
try:
with open(path,'r') as file:
self.dividendData=json.load(file)
except FileNotFoundError:
print(f'Cannot find file {path}')
self.dividendData={}
def createOrders(self):
""" Create orders data from statement """
d=self.db
data={d[s].name:{self.strDay(d[s].date):[d[s].amount,d[s].price]} for s in d}
#adjust prices for non ETF
for stock in data.keys():
etfs=self.Stock.__etfs__
if stock not in etfs:
for date in data[stock].keys():
secondPart=data[stock][date]
secondPart[1]*=100
return data
def loadOrders(self,filename):
path=self.cwd+filename
try:
with open(path,'r') as file:
self.ordersData=json.load(file)
except FileNotFoundError:
print(f'Cannot find {filename}')
print(f'Your working directory is {os.getcwd()}')
sys.exit()
def updateDayTotals(self):
totalDeposits=0
totalDividends=0
for day in range((today-self.oldestDay).days):
stringDay=self.strDay(self.oldestDay+datetime.timedelta(day))
processedDay=(self.oldestDay+datetime.timedelta(day))
totalForDay=0
if stringDay in self.depositsData:
totalDeposits+=self.depositsData[stringDay]
for stockName in self.db:
if processedDay in self.db[stockName].history:
totalForDay+=self.db[stockName].history[processedDay].profit
if stockName in self.dividendData:
if stringDay in self.dividendData[stockName]:
totalDividends+=self.dividendData[stockName][stringDay]
self.dayTotals[stringDay]=totalForDay
self.totalDividendsDict[stringDay]=totalDividends
class Stock:
__etfs__=['VUKE','VMID']
def __init__ (self,l):
self.date=self.processDate(l)
self.buySell=l[3]
self.amount=float(l[4])
self.name=l[5]
self.price=float(l[6])
buyRange=(today-self.date).days
self.history={(self.date+datetime.timedelta(d)):0 for d in range(buyRange)}
self.updateETF()
self.dividendTotal=0
def processDate(self,l):
return datetime.datetime.strptime(l[2],'%Y.%m.%d %H:%M').date()
def updateETF(self):
if self.name in self.__etfs__:
self.etf=True
else:
self.etf=False
self.price/=100
def loadStatement(self,filename):
def loadStatementFile(filename):
path=self.cwd+filename
try:
data=open(path,'r')
return [l.split('\t') for l in data]
except FileNotFoundError:
print(f'Cannot find {url}')
print(f'Your working directory is {os.getcwd()}')
sys.exit()
statementFile=loadStatementFile(filename)
# process each line in statement as separate stock
for d in statementFile:
self.db[d[5]]=self.Stock(d)
self.ordersData=self.createOrders()
self.oldestDay=min([self.db[stock].date for stock in self.db])
pass