Post Reply 
(Plus42) Cash Flow Analysis: NPV, IRR%, MIR%, FMR%
02-17-2022, 12:41 PM
Post: #15
RE: (Plus42) Cash Flow Analysis: NPV, IRR%, MIR%, FMR%
My current version:
  • NPV, IRR%, MIR%, FMR% without bracket amount (coming, but perhaps not soon..)
  • Compatible with legacy solvers (17B,19B), or so I think (if you take out the parameter list in the first line).
  • Direct solution for all but IRR%
  • Single sweep over the CashFlows
  • Separate code for Nj=1
  • CF0 may be positive, and N0 does not need to be equal to 1

Code:
CFA(CF:IRR%:NPV:SF%:RI%:MIR%:FMR%):
IF(S(CF) OR S(SF%) OR S(RI%):0:
L(P:0)*(
  IF(S(MIR%) OR S(FMR%):
    L(F:0)+
    L(S:1+L(SF:SF%/100))+
    L(R:1+RI%/100)
  :
    L(S:1+IRR%/100)
  )+
  L(NN:-1+Σ(I:SIZEC(CF):0:-1:L(Nj:#T(CF:I))+
    0*(
      L(C:FLOW(CF:I))+
      IF(S(FMR%):
        IF(G(Nj)=1:
          IF(L(P:G(P)-G(C))<0:
            L(F:G(F)-G(P))+L(P:0):
            L(P:G(P)/G(S))
          )+
          L(F:G(F)/G(R))
        :
          L(N1:L(N2:L(N3:0)))+
          IF(G(C)>0:
            IF(G(P)=0:L(N3:G(Nj)):
              L(N1:MIN(G(Nj):IP(LNP1(G(SF)*G(P)/G(C))/LNP1(G(SF)))))+
              L(N2:G(Nj)-G(N1))+
              L(N3:G(N2)-1)
            )
          :
            L(N1:G(Nj))
          )+
          IF(G(N1)>0:
            L(F:G(F)*SPPV(RI%:G(N1)))+
            L(P:G(P)*SPPV(SF%:G(N1))-G(C)*USPV(SF%:G(N1)))
          :0)+
          IF(G(N2)>0:
            L(F:(G(F)-G(P)+G(C))/G(R))+
            L(P:0)
          :0)+
          IF(G(N3)>0:
            L(F:G(F)*SPPV(RI%:G(N3))+G(C)*USPV(RI%:G(N3)))
          :0)
        )
      :
        IF(S(MIR%):
          IF(G(Nj)=1:
            IF(G(C)<0:L(P:G(P)-G(C)):L(F:G(F)+G(C)))+
            L(P:G(P)/G(S))+L(F:G(F)/G(R))
          :
            L(P:G(P)*SPPV(SF%:G(Nj)))+
            L(F:G(F)*SPPV(RI%:G(Nj)))+
            IF(G(C)<0:
              L(P:G(P)-G(C)*USPV(SF%:G(Nj))):
              L(F:G(F)+G(C)*USPV(RI%:G(Nj)))
            )
          )
        :
          IF(G(Nj)=1:
            L(P:(G(P)+G(C))/G(S)):
            L(P:G(P)*SPPV(IRR%:G(Nj))+G(C)*USPV(IRR%:G(Nj)))
          )
        )
      )
  )))
)+
IF(S(MIR%) OR S(FMR%):
  ((G(F)*G(R)/G(P)/G(S))^(1/G(NN))*G(R)-1)*100-IF(S(MIR%):MIR%:FMR%)
:
  G(P)*G(S)-NPV
))

Cut-and-Paste:
Code:
CFA(CF:IRR%:NPV:SF%:RI%:MIR%:FMR%):IF(S(CF) OR S(SF%) OR S(RI%):0:L(P:0)*(IF(S(MIR%) OR S(FMR%):L(F:0)+L(S:1+L(SF:SF%/100))+L(R:1+RI%/100):L(S:1+IRR%/100))+L(NN:-1+Σ(I:SIZEC(CF):0:-1:L(Nj:#T(CF:I))+0*(L(C:FLOW(CF:I))+IF(S(FMR%):IF(G(Nj)=1:IF(L(P:G(P)-G(C))<0:L(F:G(F)-G(P))+L(P:0):L(P:G(P)/G(S)))+L(F:G(F)/G(R)):L(N1:L(N2:L(N3:0)))+IF(G(C)>0:IF(G(P)=0:L(N3:G(Nj)):L(N1:MIN(G(Nj):IP(LNP1(G(SF)*G(P)/G(C))/LNP1(G(SF)))))+L(N2:G(Nj)-G(N1))+L(N3:G(N2)-1)):L(N1:G(Nj)))+IF(G(N1)>0:L(F:G(F)*SPPV(RI%:G(N1)))+L(P:G(P)*SPPV(SF%:G(N1))-G(C)*USPV(SF%:G(N1))):0)+IF(G(N2)>0:L(F:(G(F)-G(P)+G(C))/G(R))+L(P:0):0)+IF(G(N3)>0:L(F:G(F)*SPPV(RI%:G(N3))+G(C)*USPV(RI%:G(N3))):0)):IF(S(MIR%):IF(G(Nj)=1:IF​(G(C)<0:L(P:G(P)-G(C)):L(F:G(F)+G(C)))+L(P:G(P)/G(S))+L(F:G(F)/G(R)):L(P:G(P)*SPPV(SF%:G(Nj)))+L(F:G(F)*SPPV(RI%:G(Nj)))+IF(G(C)<0:L(P:G(P)-G(C)*USPV(SF%:G(Nj))):L(F:G(F)+G(C)*USPV(RI%:G(Nj))))):IF(G(Nj)=1:L(P:(G(P)+G(C)​)/G(S)):L(P:G(P)*SPPV(IRR%:G(Nj))+G(C)*USPV(IRR%:G(Nj))))))))))+IF(S(MIR%) OR S(FMR%):((G(F)*G(R)/G(P)/G(S))^(1/G(NN))*G(R)-1)*100-IF(S(MIR%):MIR%:FMR%):G(P)*G(S)-NPV))

Cheers, Werner

41CV†,42S,48GX,49G,DM42,DM41X,17BII,15CE,DM15L,12C,16CE
Find all posts by this user
Quote this message in a reply
Post Reply 


Messages In This Thread
RE: (Plus42) Cash Flow Analysis: NPV, IRR%, MIR%, FMR% - Werner - 02-17-2022 12:41 PM



User(s) browsing this thread: 2 Guest(s)