-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfinancial_functions.py
More file actions
88 lines (69 loc) · 2.15 KB
/
financial_functions.py
File metadata and controls
88 lines (69 loc) · 2.15 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
#!/usr/bin/python
# -*- coding: utf-8 -*-
#Library of common functions
import json, urllib2, re, time, datetime, sys, cgi, os, string, random, math
#to enable debugging
import cgitb
# cgitb.enable()
# ########################## ##################
# ########################## ##################
#
# Financial functions
#
# ########################## ##################
def ff_xirr(cf=[],dt=[]):
#module to estimate X_IRR
# inputs are a cash flow list, datelist
# Model parameters
low_rate = 0.01
high_rate = 0.75
max_iter = 1000
prec_req = 0.00000001
num_flows = len(cf)
#ptest(( cf,dt,num_flows))
# Initialize values
i = 0
j = 0
m = 0.0
xold = 0.00
xnew = 0.00
old_guess_rate = low_rate
new_guess_rate = low_rate
guess_rate = low_rate
low_guess_rate = low_rate
high_guess_rate = high_rate
npv = 0.0
denom = 0.0
for i in range(0, max_iter):
npv = 0.00
for j in range(0,num_flows):
#denom = math.pow((1+guess_rate),j)
if j==0:
time_interval = 0
else:
time_interval = (dt[j]-dt[0]).days / 365.0
denom = math.pow((1+guess_rate),time_interval)
npv = npv + (cf[j]/denom)
#print i,j,time_interval,denom,npv
# Stop checking once the required precision is achieved
if ((npv > 0) and (npv < prec_req)): break
if (xold == 0):
xold = npv
else:
xold = xnew
xnew = npv
if (i > 0):
if(xold < xnew):
if(xold < 0 and xnew < 0):
high_guess_rate = new_guess_rate
else:
low_guess_rate = new_guess_rate
else:
if(xold > 0 and xnew > 0):
low_guess_rate = new_guess_rate
else:
high_guess_rate = new_guess_rate
old_guess_rate = guess_rate
guess_rate = (low_guess_rate + high_guess_rate) / 2.0
new_guess_rate = guess_rate
return guess_rate