1000 rem aaii program no. 6 -- irr & npv 1010 rem by robert osterlund of aaii. (c) copyright 1985 by 1020 rem the american association of individual investors. 1030 rem from the september 1985 issue of computerized investing. 2000 rem initialization routine 2010 dim mm%(100), yy%(100) 2020 dim mm(101), td(100), fl(100), dr(100), rd(100) 2030 print chr$(26): rem clear screen 2040 print "you may input up to 100 inflows or" 2050 print "outflows. inflows are entered as" 2060 print "positive amounts (e.g., 500)," 2070 print "outflows negative (e.g., -2500)." 2080 print "enter rates as percents (e.g., 7.5)." 2090 print "dates should be in strict chrono-" 2100 print "logical order and entered as follows:" 2110 print "month (comma) year (e.g., 9,1985)." 2120 print 2130 print "when prompted for the cash flow," 2140 print "enter 'x' to correct the previous" 2150 print "set of entries and 'f' when finished" 2160 print "inputting cases." 2170 print 3000 rem data entry routine 3010 c = 0 3020 print: print "for case #"; c+1; "..." 3030 print "what is the inflow/outflow"; 3040 input a$ 3050 if a$ = "x" then if c > 0 then c = c - 1: goto 3020 3060 if a$ = "f" then gosub 8000: goto 9000 3070 if val(a$) = 0 then if a$ <> "0" then 3030 3080 c = c + 1 3090 fl(c) = val(a$) 3100 j = c: gosub 3500 3110 if c >= 100 then gosub 8000: goto 9000 3120 goto 3020 3500 rem subroutine to input discount rate and date 3510 print "what is the discount rate"; 3520 input dr(j): dr(j) = dr(j)/100: if dr(j) <= -1 then 3510 3530 print "on what month & year"; 3540 input mm%(j), yy%(j) 3550 m% = mm%(j): y% = yy%(j): gosub 8500 3560 if (m < mm(j-1)) or ((m > mm(j+1)) and (mm(j+1) <> 0)) then 3530 3570 mm(j) = m 3580 return 4000 rem case modification routine 4010 gosub 8000: print 4020 cc = 100: gosub 4800: if e% > c then c = e% 4030 for j = b% to e% 4040 print: print "for case #"; j; "..." 4050 print "what is the inflow/outflow"; 4060 input fl(j) 4070 gosub 3500 4080 next j 4090 gosub 8000: goto 9000 4200 rem cash flow modification routine 4210 gosub 8000: print 4220 print "what is the inflow/outflow"; 4230 input f 4240 cc = c: gosub 4800 4250 for j = b% to e% 4260 fl(j) = f 4270 next j 4280 gosub 8000: goto 9000 4400 rem discount rate modification routine 4410 gosub 8000: print 4420 print "what is the discount rate"; 4430 input ds: ds = ds/100: if ds <= -1 then 4420 4440 cc = c: gosub 4800 4450 for j = b% to e% 4460 dr(j) = ds 4470 next j 4480 gosub 8000: goto 9000 4600 rem date modification routine 4610 gosub 8000: print 4620 cc = c: gosub 4800 4630 for j = b% to e% 4640 print 4650 print "case #"; j; "took place ..." 4660 gosub 3530 4670 next j 4680 gosub 8000: goto 9000 4800 rem subroutine to determine case range 4810 print "from which beginning case"; 4820 input b% 4830 if (b% < 1) or (b% > c+abs((cc=100))) then 4810 4840 print "through which ending case"; 4850 input e% 4860 if (e% < b%) or (e% > cc) then 4840 4870 return 5000 rem routine to determine internal rate of return 5010 print: print "what is the estimated rate of" 5020 print "return (%)"; 5030 input re: re = re/100: if re <= -1 then 5010 5040 for j = 1 to c 5050 td(j) = (mm(c) - mm(j))/12 5060 next j 5070 r = log(1+re): k = 0 5080 nv = 0: du = 0 5090 for j = 1 to c 5100 pv = fl(j) * exp(r*td(j)) 5110 nv = nv + pv 5120 du = du + td(j) * pv 5130 next j 5140 if abs(du) < .1 then rd = .03 * sgn(nv) * sgn(du): goto 5160 5150 rd = nv / du 5160 r = r - rd 5170 if abs(rd) < .00005 then if du <> 0 then 5210 5180 k = k + 1 5190 if k >= 100 then print: print "try another estimated rate.": goto 9000 5200 goto 5080 5210 rr = exp(r) - 1 5220 print 5230 print "actual rate of return = "; int(rr*10000+.5)/100; "percent." 5240 goto 9000 6000 rem routine to determine net present value 6010 print: print "what is the present date"; 6020 input m%, y% 6030 gosub 8500 6040 if m > mm(1) then 6010 6050 mm(0) = m 6060 nv = 0 6070 for j = 1 to c 6080 rd(j) = exp(log(1+dr(j))*(mm(j-1)-mm(j))/12) 6090 ds = 1 6100 for k = 1 to j 6110 ds = ds * rd(k) 6120 next k 6130 pv = fl(j) * ds 6140 nv = nv + pv 6150 next j 6160 print: print "net present value = "; 6170 print int(nv*100+.5)/100 6180 mm(0) = 0 6190 goto 9000 7000 rem routine to determine net future value 7010 print: print "what is the future date"; 7020 input m%, y% 7030 gosub 8500 7040 if m < mm(c) then 7010 7050 mm(c+1) = m 7060 nv = 0 7070 for j = c to 1 step -1 7080 rd(j) = exp(log(1+dr(j))*(mm(j+1)-mm(j))/12) 7090 ds = 1 7100 for k = c to j step -1 7110 ds = ds * rd(k) 7120 next k 7130 pv = fl(j) * ds 7140 nv = nv + pv 7150 next j 7160 print: print "net future value = "; 7170 print int(nv*100+.5)/100 7180 mm(c+1) = 0 7190 goto 9000 8000 rem subroutine to output data 8010 rem you might find it necessary to substitute '1' for '2' in the 8020 rem expression 'mid$(str$(...),2)' in lines 8100 & 8110 below. 8030 print chr$(26): rem clear screen 8040 print tab(8) "inflow/"; tab(20) "discount" 8050 print "case"; tab(8) "outflow"; tab(22) "rate"; tab(32) "date" 8060 print 8070 for j = 1 to c 8080 print j; tab(8) int(fl(j)*100+.5)/100; 8090 print tab(21) int(dr(j)*10000+.5)/100; 8100 print tab(32) mid$(str$(mm%(j)),2); "/"; 8110 print mid$(str$(yy%(j)),2) 8120 next j 8130 if s% = 4 then 9000 8140 return 8500 rem subroutine to compute number of months from jan 1900 8510 m = m% + 12*(y%-1900) - 1 8520 return 9000 rem menu routine 9010 print 9020 print "1> find irr 5> modify cases" 9030 print "2> find npv 6> modify flows" 9040 print "3> find nfv 7> modify rates" 9050 print "4> display data 8> modify dates" 9060 print " 9> quit program" 9070 print 9080 print "what is your selection (1-9)"; 9090 input s% 9100 on s% goto 5000, 6000, 7000, 8000, 4000, 4200, 4400, 4600, 9120 9110 goto 9080 9120 end