// Instrument interface public override void calculate() { Utils.QL_REQUIRE(!discountCurve_.empty(), () => "discounting term structure handle is empty"); results_.value = results_.cash = 0; results_.errorEstimate = null; Date refDate = discountCurve_.link.referenceDate(); Date settlementDate = settlementDate_; if (settlementDate_ == null) { settlementDate = refDate; } else { Utils.QL_REQUIRE(settlementDate >= refDate, () => "settlement date (" + settlementDate + ") before " + "discount curve reference date (" + refDate + ")"); } results_.valuationDate = npvDate_; if (npvDate_ == null) { results_.valuationDate = refDate; } else { Utils.QL_REQUIRE(npvDate_ >= refDate, () => "npv date (" + npvDate_ + ") before " + "discount curve reference date (" + refDate + ")"); } results_.npvDateDiscount = discountCurve_.link.discount(results_.valuationDate); int n = arguments_.legs.Count; results_.legNPV = new InitializedList <double?>(n); results_.legBPS = new InitializedList <double?>(n); results_.startDiscounts = new InitializedList <double?>(n); results_.endDiscounts = new InitializedList <double?>(n); bool includeRefDateFlows = includeSettlementDateFlows_.HasValue ? includeSettlementDateFlows_.Value : Settings.Instance.includeReferenceDateEvents; for (int i = 0; i < n; ++i) { try { YieldTermStructure discount_ref = discountCurve_.currentLink(); double npv = 0, bps = 0; CashFlows.npvbps(arguments_.legs[i], discount_ref, includeRefDateFlows, settlementDate, results_.valuationDate, out npv, out bps); results_.legNPV[i] = npv * arguments_.payer[i]; results_.legBPS[i] = bps * arguments_.payer[i]; if (!arguments_.legs[i].empty()) { Date d1 = CashFlows.startDate(arguments_.legs[i]); if (d1 >= refDate) { results_.startDiscounts[i] = discountCurve_.link.discount(d1); } else { results_.startDiscounts[i] = null; } Date d2 = CashFlows.maturityDate(arguments_.legs[i]); if (d2 >= refDate) { results_.endDiscounts[i] = discountCurve_.link.discount(d2); } else { results_.endDiscounts[i] = null; } } else { results_.startDiscounts[i] = null; results_.endDiscounts[i] = null; } } catch (Exception e) { Utils.QL_FAIL((i + 1) + " leg: " + e.Message); } results_.value += results_.legNPV[i]; } }