Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Financial Function
Message
General information
Forum:
Visual FoxPro
Category:
Forms & Form designer
Miscellaneous
Thread ID:
00096470
Message ID:
00099923
Views:
14
>Thanks Juan, for providing me routine for calculating IRR, but when I run these function , it gives me domain error. What wrong I am doing ? For your convinience I am providing U parameter i.e. initial Amt is - 100000 , Case start Date is 04/05/98, Monthly Amt is 5000 and case period is 24 months . Please go thru it and tell me about my mistake . Please excuse my english.
>
>Thanks
*** Hi,

*** I have modified the message so you can create and use the IRR function correctly.
*** You have to create 2 files: MYPROG.PRG AND TIR.PRG

*** From the command line type "do myprog.prg", it will solve your example.
***

*** Notice that i am using 365-years, that's why the answer you get is 19.68%
*** If you want to use 360-years you have to modify theses variables:
*** tiempo, fx1, fx2 (i think you will get a 20.00%)
*** If you need help, just let me know.


*** THE FIRST SECTION MAY BE A PART OF ANY PROGRAM WHERE YOU WANT TO CALL
*** THE IRR FUNCTION, I NAMED IT "MYPROG.PRG"


* Create a table with two fields (Date and value)


create table flujo ;
(tirfflujo d ,;
tirvflujo n(15,2) )




*
*
* Write your code in order to generate the cash flow in the table (Dates and values).
* Remember: Investment must be a negative value.
*

* if we want to develope your example, we have to use several Insert statements:
* (It's obvious that your software has to generate the flow more automatically using
* some data source)

set date british * dd/mm/aa
set century on * years >= 2000


insert into flujo (tirfflujo, tirvflujo) values (ctod("04/05/1998"),-100000)
insert into flujo (tirfflujo, tirvflujo) values (ctod("04/06/1998"),5000)
insert into flujo (tirfflujo, tirvflujo) values (ctod("04/07/1998"),5000)
insert into flujo (tirfflujo, tirvflujo) values (ctod("04/08/1998"),5000)
insert into flujo (tirfflujo, tirvflujo) values (ctod("04/09/1998"),5000)
insert into flujo (tirfflujo, tirvflujo) values (ctod("04/10/1998"),5000)
insert into flujo (tirfflujo, tirvflujo) values (ctod("04/11/1998"),5000)
insert into flujo (tirfflujo, tirvflujo) values (ctod("04/12/1998"),5000)
insert into flujo (tirfflujo, tirvflujo) values (ctod("04/01/1999"),5000)
insert into flujo (tirfflujo, tirvflujo) values (ctod("04/02/1999"),5000)
insert into flujo (tirfflujo, tirvflujo) values (ctod("04/03/1999"),5000)
insert into flujo (tirfflujo, tirvflujo) values (ctod("04/04/1999"),5000)
insert into flujo (tirfflujo, tirvflujo) values (ctod("04/05/1999"),5000)
insert into flujo (tirfflujo, tirvflujo) values (ctod("04/06/1999"),5000)
insert into flujo (tirfflujo, tirvflujo) values (ctod("04/07/1999"),5000)
insert into flujo (tirfflujo, tirvflujo) values (ctod("04/08/1999"),5000)
insert into flujo (tirfflujo, tirvflujo) values (ctod("04/09/1999"),5000)
insert into flujo (tirfflujo, tirvflujo) values (ctod("04/10/1999"),5000)
insert into flujo (tirfflujo, tirvflujo) values (ctod("04/11/1999"),5000)
insert into flujo (tirfflujo, tirvflujo) values (ctod("04/12/1999"),5000)
insert into flujo (tirfflujo, tirvflujo) values (ctod("04/01/2000"),5000)
insert into flujo (tirfflujo, tirvflujo) values (ctod("04/02/2000"),5000)
insert into flujo (tirfflujo, tirvflujo) values (ctod("04/03/2000"),5000)
insert into flujo (tirfflujo, tirvflujo) values (ctod("04/04/2000"),5000)
insert into flujo (tirfflujo, tirvflujo) values (ctod("04/05/2000"),5000)


* Call the irr function

? tir (ctod("04/05/98"), 30)

*******************************************************************************


*** THE NEXT SECTION HAS TO BE A SEPARATE .PRG FILE (TIR.PRG) , BECAUSE YOU WANT TO CALL
*** THIS FUNCTION FROM ANY PROGRAM

function tir
parameters xfecha_ini, xtasa_efec

* xtasa_efec = An estimated initial value for IRR.
* xfecha_ini = Initial date.



* Description:
* This function calculates two NPV using two initial values (rates x1 and x2)
* Each NPV (fx1 and fx2) is the basis for estimating the next value (x3)
* The loop is repeated until you get a NPV close to 0.0 or until it tries
* 50 times and returns 0

* Normally, i get the solution during the first 6 - 10 loops.



* tirvtir = IRR (The value you are looking for)

go top

tirvtir = 0
x1 = xtasa_efec / 100 - 0.02
x2 = xtasa_efec / 100 + 0.02
x3 = 0
fx1 = 1
fx2 = 0

if eof()
use
return 0
endif

intentos = 0

do while abs(fx1) > 0.09

intentos = intentos + 1
if intentos = 50
use
return 0
endif

go top
fx1 = 0
fx2 = 0
scan
tiempo = tirfflujo - xfecha_ini
fx1 = fx1 + tirvflujo / (1+x1)^(tiempo/365)
fx2 = fx2 + tirvflujo / (1+x2)^(tiempo/365)
endscan

x3 = round(x2 - (x1-x2) * fx2 / (fx1 - fx2),11)
x1 = x2
x2 = x3

enddo

tirvtir = x1 * 100

use

return tirvtir
Previous
Reply
Map
View

Click here to load this message in the networking platform