public override CompileResult Execute(IEnumerable <FunctionArgument> arguments, ParsingContext context) { ValidateArguments(arguments, 5); var settlementDate = System.DateTime.FromOADate(ArgToInt(arguments, 0)); var maturityDate = System.DateTime.FromOADate(ArgToInt(arguments, 1)); if (settlementDate >= maturityDate) { return(CreateResult(eErrorType.Num)); } var issueDate = System.DateTime.FromOADate(ArgToInt(arguments, 2)); var rate = ArgToDecimal(arguments, 3); if (rate < 0) { return(CreateResult(eErrorType.Num)); } var price = ArgToDecimal(arguments, 4); if (price <= 0) { return(CreateResult(eErrorType.Num)); } var basis = 0; if (arguments.Count() > 5) { basis = ArgToInt(arguments, 5); if (basis < 0 || basis > 4) { return(CreateResult(eErrorType.Num)); } } var yearFracProvider = new YearFracProvider(context); var yf1 = yearFracProvider.GetYearFrac(issueDate, maturityDate, (DayCountBasis)basis); var yf2 = yearFracProvider.GetYearFrac(issueDate, settlementDate, (DayCountBasis)basis); var yf3 = yearFracProvider.GetYearFrac(settlementDate, maturityDate, (DayCountBasis)basis); var result = 1d + yf1 * rate; result /= price / 100d + yf2 * rate; result = --result / yf3; return(CreateResult(result, DataType.Decimal)); }
public override CompileResult Execute(IEnumerable <FunctionArgument> arguments, ParsingContext context) { ValidateArguments(arguments, 4); var settlementNum = ArgToDecimal(arguments, 0); var maturityNum = ArgToDecimal(arguments, 1); var settlement = System.DateTime.FromOADate(settlementNum); var maturity = System.DateTime.FromOADate(maturityNum); var pr = ArgToDecimal(arguments, 2); var redemption = ArgToDecimal(arguments, 3); int basis = 0; if (arguments.Count() > 4) { basis = ArgToInt(arguments, 4); } if (maturity <= settlement || pr <= 0 || redemption <= 0 || (basis < 0 || basis > 4)) { return(CreateResult(eErrorType.Num)); } var yearFrac = new YearFracProvider(context); var result = (1d - pr / redemption) / yearFrac.GetYearFrac(settlement, maturity, (DayCountBasis)basis); return(CreateResult(result, DataType.Decimal)); }