-
Notifications
You must be signed in to change notification settings - Fork 1
/
calc-return.py
executable file
·51 lines (45 loc) · 1.75 KB
/
calc-return.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
#!/usr/bin/env python
#
# calc-return.py Calculate the annualised return of an investment.
#
# Copyright (C) 2024 Michael Davies <[email protected]>
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation; either version 2 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
# 02111-1307, USA.
#
import datetime
def get_date(prompt):
no_date = True
while no_date:
try:
d = input(prompt)
if d == 'today':
parsed_date = datetime.datetime.now()
else:
parsed_date = datetime.datetime.strptime(d, "%d/%m/%Y")
no_date = False
except ValueError:
print("Sorry, wrong date format. Try again.")
return parsed_date
start_date = get_date("Enter starting date (dd/mm/yyyy): ")
end_date = get_date("Enter ending date (dd/mm/yy or today): ")
# Flip the dates for user error
if start_date > end_date:
start_date, end_date = end_date, start_date
delta = end_date - start_date
tot = float(input("Enter total return (0.3 = 30%): "))
annual_return = ((1 + tot)**(365/delta.days)) - 1
print(f"Time period is {delta.days} days, which is {delta.days/365:.2f} years")
print(f"Annualised return is {(annual_return*100):.2f}%")