コード例 #1
0
ファイル: AssetSwap.cs プロジェクト: OpenDerivatives/QLCore
        // other
        public override void setupArguments(IPricingEngineArguments args)
        {
            base.setupArguments(args);

            AssetSwap.Arguments arguments = args as AssetSwap.Arguments;

            if (arguments == null) // it's a swap engine...
            {
                return;
            }

            List <CashFlow> fixedCoupons = bondLeg();

            arguments.fixedResetDates = arguments.fixedPayDates = new List <Date>(fixedCoupons.Count);
            arguments.fixedCoupons    = new List <double>(fixedCoupons.Count);

            for (int i = 0; i < fixedCoupons.Count; ++i)
            {
                FixedRateCoupon coupon = fixedCoupons[i] as FixedRateCoupon;

                arguments.fixedPayDates[i]   = coupon.date();
                arguments.fixedResetDates[i] = coupon.accrualStartDate();
                arguments.fixedCoupons[i]    = coupon.amount();
            }

            List <CashFlow> floatingCoupons = floatingLeg();

            arguments.floatingResetDates      = arguments.floatingPayDates =
                arguments.floatingFixingDates = new List <Date>(floatingCoupons.Count);
            arguments.floatingAccrualTimes    = new List <double>(floatingCoupons.Count);
            arguments.floatingSpreads         = new List <double>(floatingCoupons.Count);

            for (int i = 0; i < floatingCoupons.Count; ++i)
            {
                FloatingRateCoupon coupon = floatingCoupons[i] as FloatingRateCoupon;

                arguments.floatingResetDates[i]   = coupon.accrualStartDate();
                arguments.floatingPayDates[i]     = coupon.date();
                arguments.floatingFixingDates[i]  = coupon.fixingDate();
                arguments.floatingAccrualTimes[i] = coupon.accrualPeriod();
                arguments.floatingSpreads[i]      = coupon.spread();
            }
        }
コード例 #2
0
        public override void setupArguments(IPricingEngineArguments args)
        {
            CapFloor.Arguments arguments = args as CapFloor.Arguments;

            if (arguments == null)
            {
                throw new ArgumentException("wrong argument type");
            }


            int n = floatingLeg_.Count;

            arguments.startDates   = new InitializedList <Date>(n);
            arguments.fixingDates  = new InitializedList <Date>(n);
            arguments.endDates     = new InitializedList <Date>(n);
            arguments.accrualTimes = new InitializedList <double>(n);
            arguments.forwards     = new InitializedList <double?>(n);
            arguments.nominals     = new InitializedList <double>(n);
            arguments.gearings     = new InitializedList <double>(n);
            arguments.capRates     = new InitializedList <double?>(n);
            arguments.floorRates   = new InitializedList <double?>(n);
            arguments.spreads      = new InitializedList <double>(n);

            arguments.type = type_;

            Date today = Settings.Instance.evaluationDate();

            for (int i = 0; i < n; ++i)
            {
                FloatingRateCoupon coupon = floatingLeg_[i] as FloatingRateCoupon;

                if (coupon == null)
                {
                    throw new ArgumentException("non-FloatingRateCoupon given");
                }

                arguments.startDates[i]  = coupon.accrualStartDate();
                arguments.fixingDates[i] = coupon.fixingDate();
                arguments.endDates[i]    = coupon.date();

                // this is passed explicitly for precision
                arguments.accrualTimes[i] = coupon.accrualPeriod();

                // this is passed explicitly for precision...
                if (arguments.endDates[i] >= today)
                {
                    // ...but only if needed
                    arguments.forwards[i] = coupon.adjustedFixing;
                }
                else
                {
                    arguments.forwards[i] = null;
                }

                arguments.nominals[i] = coupon.nominal();
                double spread  = coupon.spread();
                double gearing = coupon.gearing();
                arguments.gearings[i] = gearing;
                arguments.spreads[i]  = spread;

                if (type_ == CapFloorType.Cap || type_ == CapFloorType.Collar)
                {
                    arguments.capRates[i] = (capRates_[i] - spread) / gearing;
                }
                else
                {
                    arguments.capRates[i] = null;
                }

                if (type_ == CapFloorType.Floor || type_ == CapFloorType.Collar)
                {
                    arguments.floorRates[i] = (floorRates_[i] - spread) / gearing;
                }
                else
                {
                    arguments.floorRates[i] = null;
                }
            }
        }
コード例 #3
0
        // other
        public override void setupArguments(IPricingEngineArguments args)
        {
            base.setupArguments(args);

            Arguments arguments = args as Arguments;

            Utils.QL_REQUIRE(arguments != null, () => "argument type does not match");

            arguments.type     = type_;
            arguments.nominal1 = nominal1_;
            arguments.nominal2 = nominal2_;
            arguments.index1   = index1_;
            arguments.index2   = index2_;

            List <CashFlow> leg1Coupons = leg1();
            List <CashFlow> leg2Coupons = leg2();

            arguments.leg1ResetDates      = arguments.leg1PayDates =
                arguments.leg1FixingDates = new InitializedList <Date>(leg1Coupons.Count);
            arguments.leg2ResetDates      = arguments.leg2PayDates =
                arguments.leg2FixingDates = new InitializedList <Date>(leg2Coupons.Count);

            arguments.leg1Spreads      = arguments.leg1AccrualTimes =
                arguments.leg1Gearings = new InitializedList <double>(leg1Coupons.Count);
            arguments.leg2Spreads      = arguments.leg2AccrualTimes =
                arguments.leg2Gearings = new InitializedList <double>(leg2Coupons.Count);

            arguments.leg1Coupons = new InitializedList <double?>(leg1Coupons.Count, null);
            arguments.leg2Coupons = new InitializedList <double?>(leg2Coupons.Count, null);

            arguments.leg1IsRedemptionFlow = new InitializedList <bool>(leg1Coupons.Count, false);
            arguments.leg2IsRedemptionFlow = new InitializedList <bool>(leg2Coupons.Count, false);

            arguments.leg1CappedRates = arguments.leg1FlooredRates =
                new InitializedList <double?>(leg1Coupons.Count, null);
            arguments.leg2CappedRates = arguments.leg2FlooredRates =
                new InitializedList <double?>(leg2Coupons.Count, null);

            for (int i = 0; i < leg1Coupons.Count; ++i)
            {
                FloatingRateCoupon coupon = leg1Coupons[i] as FloatingRateCoupon;
                if (coupon != null)
                {
                    arguments.leg1AccrualTimes[i] = coupon.accrualPeriod();
                    arguments.leg1PayDates[i]     = coupon.date();
                    arguments.leg1ResetDates[i]   = coupon.accrualStartDate();
                    arguments.leg1FixingDates[i]  = coupon.fixingDate();
                    arguments.leg1Spreads[i]      = coupon.spread();
                    arguments.leg1Gearings[i]     = coupon.gearing();
                    try
                    {
                        arguments.leg1Coupons[i] = coupon.amount();
                    }
                    catch (Exception)
                    {
                        arguments.leg1Coupons[i] = null;
                    }
                    CappedFlooredCoupon cfcoupon = leg1Coupons[i] as CappedFlooredCoupon;
                    if (cfcoupon != null)
                    {
                        arguments.leg1CappedRates[i]  = cfcoupon.cap();
                        arguments.leg1FlooredRates[i] = cfcoupon.floor();
                    }
                }
                else
                {
                    CashFlow cashflow = leg1Coupons[i] as CashFlow;
                    int      j        = arguments.leg1PayDates.FindIndex(x => x == cashflow.date());
                    Utils.QL_REQUIRE(j != -1, () =>
                                     "nominal redemption on " + cashflow.date() + "has no corresponding coupon");
                    int jIdx = j; // Size jIdx = j - arguments->leg1PayDates.begin();
                    arguments.leg1IsRedemptionFlow[i] = true;
                    arguments.leg1Coupons[i]          = cashflow.amount();
                    arguments.leg1ResetDates[i]       = arguments.leg1ResetDates[jIdx];
                    arguments.leg1FixingDates[i]      = arguments.leg1FixingDates[jIdx];
                    arguments.leg1AccrualTimes[i]     = 0.0;
                    arguments.leg1Spreads[i]          = 0.0;
                    arguments.leg1Gearings[i]         = 1.0;
                    arguments.leg1PayDates[i]         = cashflow.date();
                }
            }

            for (int i = 0; i < leg2Coupons.Count; ++i)
            {
                FloatingRateCoupon coupon = leg2Coupons[i] as FloatingRateCoupon;
                if (coupon != null)
                {
                    arguments.leg2AccrualTimes[i] = coupon.accrualPeriod();
                    arguments.leg2PayDates[i]     = coupon.date();
                    arguments.leg2ResetDates[i]   = coupon.accrualStartDate();
                    arguments.leg2FixingDates[i]  = coupon.fixingDate();
                    arguments.leg2Spreads[i]      = coupon.spread();
                    arguments.leg2Gearings[i]     = coupon.gearing();
                    try
                    {
                        arguments.leg2Coupons[i] = coupon.amount();
                    }
                    catch (Exception)
                    {
                        arguments.leg2Coupons[i] = null;
                    }
                    CappedFlooredCoupon cfcoupon = leg2Coupons[i] as CappedFlooredCoupon;
                    if (cfcoupon != null)
                    {
                        arguments.leg2CappedRates[i]  = cfcoupon.cap();
                        arguments.leg2FlooredRates[i] = cfcoupon.floor();
                    }
                }
                else
                {
                    CashFlow cashflow = leg2Coupons[i] as CashFlow;
                    int      j        = arguments.leg2PayDates.FindIndex(x => x == cashflow.date());
                    Utils.QL_REQUIRE(j != -1, () =>
                                     "nominal redemption on " + cashflow.date() + "has no corresponding coupon");
                    int jIdx = j; // j - arguments->leg2PayDates.begin();
                    arguments.leg2IsRedemptionFlow[i] = true;
                    arguments.leg2Coupons[i]          = cashflow.amount();
                    arguments.leg2ResetDates[i]       = arguments.leg2ResetDates[jIdx];
                    arguments.leg2FixingDates[i]      =
                        arguments.leg2FixingDates[jIdx];
                    arguments.leg2AccrualTimes[i] = 0.0;
                    arguments.leg2Spreads[i]      = 0.0;
                    arguments.leg2Gearings[i]     = 1.0;
                    arguments.leg2PayDates[i]     = cashflow.date();
                }
            }
        }
コード例 #4
0
        //! Constructors
        //! general constructor
        public DigitalCoupon(FloatingRateCoupon underlying,
                             double?callStrike              = null,
                             Position.Type callPosition     = Position.Type.Long,
                             bool isCallATMIncluded         = false,
                             double?callDigitalPayoff       = null,
                             double?putStrike               = null,
                             Position.Type putPosition      = Position.Type.Long,
                             bool isPutATMIncluded          = false,
                             double?putDigitalPayoff        = null,
                             DigitalReplication replication = null)
            : base(underlying.date(), underlying.nominal(), underlying.accrualStartDate(), underlying.accrualEndDate(), underlying.fixingDays, underlying.index(), underlying.gearing(), underlying.spread(), underlying.referencePeriodStart, underlying.referencePeriodEnd, underlying.dayCounter(), underlying.isInArrears())
        {
            if (replication == null)
            {
                replication = new DigitalReplication();
            }

            underlying_          = underlying;
            callCsi_             = 0.0;
            putCsi_              = 0.0;
            isCallATMIncluded_   = isCallATMIncluded;
            isPutATMIncluded_    = isPutATMIncluded;
            isCallCashOrNothing_ = false;
            isPutCashOrNothing_  = false;
            callLeftEps_         = replication.gap() / 2.0;
            callRightEps_        = replication.gap() / 2.0;
            putLeftEps_          = replication.gap() / 2.0;
            putRightEps_         = replication.gap() / 2.0;
            hasPutStrike_        = false;
            hasCallStrike_       = false;
            replicationType_     = replication.replicationType();


            Utils.QL_REQUIRE(replication.gap() > 0.0, () => "Non positive epsilon not allowed");

            if (putStrike == null)
            {
                Utils.QL_REQUIRE(putDigitalPayoff == null, () => "Put Cash rate non allowed if put strike is null");
            }

            if (callStrike == null)
            {
                Utils.QL_REQUIRE(callDigitalPayoff == null, () => "Call Cash rate non allowed if call strike is null");
            }

            if (callStrike != null)
            {
                Utils.QL_REQUIRE(callStrike >= 0.0, () => "negative call strike not allowed");

                hasCallStrike_ = true;
                callStrike_    = callStrike.GetValueOrDefault();
                Utils.QL_REQUIRE(callStrike_ >= replication.gap() / 2.0, () => "call strike < eps/2");

                switch (callPosition)
                {
                case Position.Type.Long:
                    callCsi_ = 1.0;
                    break;

                case Position.Type.Short:
                    callCsi_ = -1.0;
                    break;

                default:
                    Utils.QL_FAIL("unsupported position type");
                    break;
                }
                if (callDigitalPayoff != null)
                {
                    callDigitalPayoff_   = callDigitalPayoff.GetValueOrDefault();
                    isCallCashOrNothing_ = true;
                }
            }
            if (putStrike != null)
            {
                Utils.QL_REQUIRE(putStrike >= 0.0, () => "negative put strike not allowed");
                hasPutStrike_ = true;
                putStrike_    = putStrike.GetValueOrDefault();
                switch (putPosition)
                {
                case Position.Type.Long:
                    putCsi_ = 1.0;
                    break;

                case Position.Type.Short:
                    putCsi_ = -1.0;
                    break;

                default:
                    Utils.QL_FAIL("unsupported position type");
                    break;
                }
                if (putDigitalPayoff != null)
                {
                    putDigitalPayoff_   = putDigitalPayoff.GetValueOrDefault();
                    isPutCashOrNothing_ = true;
                }
            }

            switch (replicationType_)
            {
            case Replication.Type.Central:
                // do nothing
                break;

            case Replication.Type.Sub:
                if (hasCallStrike_)
                {
                    switch (callPosition)
                    {
                    case Position.Type.Long:
                        callLeftEps_  = 0.0;
                        callRightEps_ = replication.gap();
                        break;

                    case Position.Type.Short:
                        callLeftEps_  = replication.gap();
                        callRightEps_ = 0.0;
                        break;

                    default:
                        Utils.QL_FAIL("unsupported position type");
                        break;
                    }
                }
                if (hasPutStrike_)
                {
                    switch (putPosition)
                    {
                    case Position.Type.Long:
                        putLeftEps_  = replication.gap();
                        putRightEps_ = 0.0;
                        break;

                    case Position.Type.Short:
                        putLeftEps_  = 0.0;
                        putRightEps_ = replication.gap();
                        break;

                    default:
                        Utils.QL_FAIL("unsupported position type");
                        break;
                    }
                }
                break;

            case Replication.Type.Super:
                if (hasCallStrike_)
                {
                    switch (callPosition)
                    {
                    case Position.Type.Long:
                        callLeftEps_  = replication.gap();
                        callRightEps_ = 0.0;
                        break;

                    case Position.Type.Short:
                        callLeftEps_  = 0.0;
                        callRightEps_ = replication.gap();
                        break;

                    default:
                        Utils.QL_FAIL("unsupported position type");
                        break;
                    }
                }
                if (hasPutStrike_)
                {
                    switch (putPosition)
                    {
                    case Position.Type.Long:
                        putLeftEps_  = 0.0;
                        putRightEps_ = replication.gap();
                        break;

                    case Position.Type.Short:
                        putLeftEps_  = replication.gap();
                        putRightEps_ = 0.0;
                        break;

                    default:
                        Utils.QL_FAIL("unsupported position type");
                        break;
                    }
                }
                break;

            default:
                Utils.QL_FAIL("unsupported position type");
                break;
            }

            underlying.registerWith(update);
        }