コード例 #1
0
    // NPV of swap
    public static double NPV(SwapStyle BB, IRateCurve c, bool PayOrRec)
    {
        #region FixLeg
        // fixed leg data
        double[] yfFixLeg = BB.scheduleLeg1.GetYFVect(BB.swapLeg1.DayCount);     // fixed is leg 1

        // dfs array of fixed lag
        Date[] dfDates = BB.scheduleLeg1.payDates;     // serial date of fixed lag (each dates we should find df)

        // # of fixed cash flows
        int n_fix = dfDates.Length;

        double NPV_fix = 0.0;
        // calculate df
        for (int i = 0; i < n_fix; i++)
        {
            NPV_fix += c.Df(dfDates[i]) * yfFixLeg[i] * BB.rateValue;      // df*yf
        }
        #endregion

        #region FloatLeg
        // floating leg data
        double[] yfFloatLeg = BB.scheduleLeg2.GetYFVect(BB.swapLeg2.DayCount);     // float is leg 2

        // dfs array of fixed lag
        Date[] dfDatesFloat = BB.scheduleLeg2.payDates;     // serial date of float leg (each dates we should find df)

        Date[] FromDateFloat = BB.scheduleLeg2.fromDates;

        // # of floating cash flows
        int n_float = dfDatesFloat.Length;

        double[] fwd = new double[n_float];     // fwd rates container

        // getting fwd rates
        for (int i = 0; i < n_float; i++)
        {
            fwd[i] = c.Fwd(FromDateFloat[i]);
        }

        double NPV_float = 0.0;
        // calculate df
        for (int i = 0; i < n_float; i++)
        {
            NPV_float += c.Df(dfDatesFloat[i]) * yfFloatLeg[i] * fwd[i];      // df*yf
        }

        #endregion
        if (!PayOrRec)
        {
            return(-NPV_fix + NPV_float);
        }                                // Receiver Swap
        return(NPV_fix - NPV_float);     // NPV Payer Swap
    }
コード例 #2
0
    public static double CapBlack(string Tenor, double strike, double N, IRateCurve curve, BilinearInterpolator VolCapletMatrix)
    {
        Date      refDate = curve.RefDate();
        SwapStyle y       = (SwapStyle) new BuildingBlockFactory().CreateBuildingBlock(refDate, 0, Tenor, curve.GetSwapStyle().buildingBlockType);

        double[] yf       = y.scheduleLeg2.GetYFVect(Dc._Act_360);
        int      toRemove = yf.Length - 1;

        yf = yf.Where((val, inx) => inx != toRemove).ToArray();

        List <Date> ToDate = y.scheduleLeg2.toDates.ToList();

        ToDate.RemoveAt(ToDate.Count - 1);      // remove last element
        double[] T = (from c in ToDate
                      select refDate.YF_365(c)).ToArray();

        // df- getting relevant dates
        Date[] dfDate  = y.scheduleLeg2.payDates;
        int    Ncaplet = yf.Length;   // number of caplets

        double[] df = new double[Ncaplet];
        // fwd rate
        double[] fwd     = new double[Ncaplet];
        Date[]   fwdDate = y.scheduleLeg2.fromDates;

        for (int i = 0; i < Ncaplet; i++) // Note the loop start from 1
        {                                 // first discount factor is on first payment date of caplet (first caplet skipped)
            df[i]  = curve.Df(dfDate[i + 1]);
            fwd[i] = curve.Fwd(fwdDate[i + 1]);
        }

        double[] sigma = (from t in T
                          select VolCapletMatrix.Solve(t, strike)).ToArray();
        return(CapBlack(T, yf, N, strike, sigma, df, fwd));
    }
コード例 #3
0
    public static double Swaption(double N, double K, string Start, string SwapTenor, bool isPayer, double sigma, IRateCurve Curve)
    {
        Date refDate = Curve.RefDate();                         // curve ref date
        // false/true with fwd swap matrix
        Date      startDate = refDate.add_period(Start, false); // swap start 2 business days after the expiry
        Date      expDate   = startDate.add_workdays(-2);       // expiry of swaption
        Date      today     = refDate.add_workdays(-2);
        double    T         = today.YF_365(expDate);
        Period    p         = new Period(SwapTenor); // should be in year 1Y, 2Y (not 3m,...)
        SwapStyle y         = (SwapStyle) new BuildingBlockFactory().CreateBuildingBlock(startDate, 0, SwapTenor, Curve.GetSwapStyle().buildingBlockType);

        double[] yf = y.scheduleLeg1.GetYFVect(Dc._30_360);
        double[] df = (from payDay in y.scheduleLeg1.payDates
                       select Curve.Df(payDay)).ToArray();

        return(Swaption(N, Curve.SwapFwd(startDate, SwapTenor), K, sigma, T, isPayer, yf, df));
    }
コード例 #4
0
    // constructor
    public CapletMatrixVolBuilder(string[] Tenor, IRateCurve Curve, double[] strike, List <double[]> VolSilos)
    {
        #region preparing data

        // yf of longer cap
        SwapStyle y        = (SwapStyle) new BuildingBlockFactory().CreateBuildingBlock(Curve.RefDate(), 0, Tenor.Last(), Curve.GetSwapStyle().buildingBlockType);
        double[]  yf       = y.scheduleLeg2.GetYFVect(Dc._Act_360);
        int       toRemove = yf.Length - 1;
        yf = yf.Where((val, inx) => inx != toRemove).ToArray();

        // T all tenor needed. They are more than input
        Date        refDate = Curve.RefDate();
        List <Date> ToDate  = y.scheduleLeg2.toDates.ToList();
        ToDate.RemoveAt(ToDate.Count - 1);    // remove last element
        double[] T = (from c in ToDate
                      select refDate.YF_365(c)).ToArray();

        // available T from input
        double[] Tquoted = new double[Tenor.Length];
        for (int i = 0; i < Tquoted.Length; i++)
        {        // calculate yf of each available T as it was a swap
            SwapStyle myS    = (SwapStyle) new BuildingBlockFactory().CreateBuildingBlock(Curve.RefDate(), 0, Tenor[i], Curve.GetSwapStyle().buildingBlockType);
            int       L      = myS.scheduleLeg2.toDates.Count() - 1;
            Date[]    toDate = myS.scheduleLeg2.toDates;
            Tquoted[i] = refDate.YF_365(toDate[L - 1]);
        }

        // df- getting relevant dates
        Date[]   dfDate  = y.scheduleLeg2.payDates;
        int      Ncaplet = yf.Length; // number of caplets
        double[] df      = new double[Ncaplet];
        // fwd rate
        double[] fwd     = new double[Ncaplet];
        Date[]   fwdDate = y.scheduleLeg2.fromDates;

        for (int i = 0; i < Ncaplet; i++) // Note the loop start from 1
        {                                 // first discount factor is on first payment date of caplet (first caplet skipped)
            df[i]  = Curve.Df(dfDate[i + 1]);
            fwd[i] = Curve.Fwd(fwdDate[i + 1]);
        }

        #endregion

        Ini(T, df, fwd, yf, Tquoted, VolSilos, strike);
    }
コード例 #5
0
    // used in constructor
    private void Ini(string tenor, IRateCurve curve, BilinearInterpolator capletVolMatrix, double nominal)
    {
        stringTenor = tenor;
        rateCurve   = curve;
        volMatrix   = capletVolMatrix;
        N           = nominal;

        // yf of longer cap
        SwapStyle y = (SwapStyle) new BuildingBlockFactory().CreateBuildingBlock(curve.RefDate(), 0, tenor, curve.GetSwapStyle().buildingBlockType);

        yf          = y.scheduleLeg2.GetYFVect(Dc._Act_360);
        capSchedule = y.scheduleLeg2;
        int toRemove = yf.Length - 1;

        yf = yf.Where((val, inx) => inx != toRemove).ToArray();

        // T all tenor needed. They are more than input
        Date        refDate = curve.RefDate();
        List <Date> ToDate  = y.scheduleLeg2.toDates.ToList();

        ToDate.RemoveAt(ToDate.Count - 1);  // remove last element
        T = (from c in ToDate
             select refDate.YF_365(c)).ToArray();
        // df- getting relevant dates
        Date[] dfDate  = y.scheduleLeg2.payDates;
        int    Ncaplet = yf.Length; // number of caplets

        df = new double[Ncaplet];
        // fwd rate
        fwd = new double[Ncaplet];
        Date[] fwdDate = y.scheduleLeg2.fromDates;

        for (int i = 0; i < Ncaplet; i++) // Note the loop start from 1
        {                                 // first discount factor is on first payment date of caplet (first caplet skipped)
            df[i]  = curve.Df(dfDate[i + 1]);
            fwd[i] = curve.Fwd(fwdDate[i + 1]);
        }
    }
コード例 #6
0
    public static double FwdBasis(Date StartDate, string SwapTenor, IRateCurve Curve1, IRateCurve Curve2)
    {
        SwapStyle S1 = (SwapStyle) new BuildingBlockFactory().CreateBuildingBlock(StartDate, 0.0, SwapTenor, Curve1.GetSwapStyle().buildingBlockType);
        SwapStyle S2 = (SwapStyle) new BuildingBlockFactory().CreateBuildingBlock(StartDate, 0.0, SwapTenor, Curve2.GetSwapStyle().buildingBlockType);

        double[] yfFloatLeg1 = S1.scheduleLeg2.GetYFVect(S1.swapLeg2.DayCount);

        // using LINQ syntax
        double[] dfFloatLeg1 = (from d in S1.scheduleLeg2.payDates
                                select Curve1.Df(d)).ToArray <double>();
        double[] fwdFloatLeg1 = (from d in S1.scheduleLeg2.fromDates
                                 select Curve1.Fwd(d)).ToArray <double>();

        double[] yfFloatLeg2 = S2.scheduleLeg2.GetYFVect(S2.swapLeg2.DayCount);
        // using LINQ syntax
        double[] dfFloatLeg2 = (from d in S2.scheduleLeg2.payDates
                                select Curve2.Df(d)).ToArray <double>();

        double[] fwdFloatLeg2 = (from d in S2.scheduleLeg2.fromDates
                                 select Curve2.Fwd(d)).ToArray <double>();

        return(SpreadBasisFormula(yfFloatLeg1, dfFloatLeg1, fwdFloatLeg1,
                                  yfFloatLeg2, dfFloatLeg2, fwdFloatLeg2));
    }