示例#1
0
        // ***********************************************************
        // EXAMPLE METHODS
        // ***********************************************************
        /// <summary>
        /// Example of how to value a bond. </summary>
        public static int exampleValuation()
        {
            Date pvdate = new Date(2000, 1, 1);
            Date mdate = new Date(2030, 1, 1);

            Console.WriteLine("pvdate: " + pvdate.Libdate() + " mdate: " + mdate.Libdate());
            /* timing variables */
            long start = 0;

            InterestRateModel model = new InterestRateModel();
            if (!model.SetVolatility(vol))
            {
                Console.Write("Warning: invalid volatility '{0:F}', using 0\n", vol);
            }
            if (!model.SetRate(.5, rate))
            {
                Console.Write("Warning: invalid input rate '{0:F}', using 2%\n", rate);
                rate = 2;
                model.SetRate(.5, rate);
            }
            if (ctype == CurveType.LINEAR)
            {
                model.SetRate(1, rate + .01);
                model.SetRate(30, rate + .3);
            }
            else if (ctype == CurveType.ASYM)
            {
                double[] terms = new double[] {1, 3, 5, 7, 10, 15, 30};
                for (int i = 0; i < terms.Length; i++)
                {
                    model.SetRate(terms[i], rate + 2 * (1 - 1.0 / terms[i]));
                }
            }
            if (quiet == false)
            {
                Console.Write("Making a par rate curve with {0:g2} volatility", vol);
                Console.Write(", {0:F1} yr = {1:F2}%", 1.0, model.GetRate(1));
                Console.Write(", {0:F1} yr = {1:F2}%", 30.0, model.GetRate(30));
            }

            if (timing)
            {
                start = CurrentUnixTimeMillis();
            }
            model.Solve();
            if (timing)
            {
                start = CurrentUnixTimeMillis() - start;
                Console.Write("Seconds to fit the base curve = {0:F2}\n", INSECS(start));
            }
            if (!msgs(model))
            {
                return model.Error();
            }

            // make the bond
            if (quiet == false)
            {
                Console.Write("\nMaking a 30 year {0:g10}% bond maturing on {1:D}", coupon, mdate.Libdate());
            }
            Date idate = new Date(mdate.YearOf() - 30, mdate.MonthOf(), mdate.DayOf());
            Bond bond = new Bond("example", idate, mdate, coupon);
            if (!msgs(bond))
            {
                return bond.Error();
            }

            if (!bullet)
            {
                Date cdate = new Date(idate.YearOf() + 5, idate.MonthOf(), idate.DayOf());
                if (quiet == false)
                {
                    Console.Write(" callable {0:D} at par", cdate.Libdate());
                }
                if (!bond.SetCall(cdate, 100))
                {
                    Console.Write("failed to add call at {0:D}\n", cdate.Libdate());
                }
            }
            if (quiet == false)
            {
                Console.Write("\n\n");
            }

            if (quiet == false)
            {
                string underline = "--------------------";
                string fmt = "%10.10s %8.8s %8.8s %8.8s %8.8s %8.8s";
                Console.Write(fmt, "pvdate  ", fromoas ? "price" : "oas", "accrued", "optval", "duration", "convex.");
                Console.Write("\n");
                Console.Write(fmt, underline, underline, underline, underline, underline, underline);
                Console.Write("\n");
            }

            Value value = new Value(bond, model, pvdate);
            if (!msgs(value))
            {
                return value.Error();
            }

            // loop through pvdates
            start = CurrentUnixTimeMillis();
            int cnt = 0;
            for (cnt = 0; pvdate.IsLT(mdate) && cnt < days; pvdate.PlusEqual(1), cnt++)
            {
                if (cnt > 0)
                {
                    value.Reset(bond, pvdate);
                    if (!msgs(value))
                    {
                        break;
                    }
                }
                double oas = fromoas ? quote : value.Oas(quote);
                double price = fromoas ? value.Price(quote) : quote;
                if (!msgs(value) || oas == Value.BadValue || price == Value.BadValue)
                {
                    break;
                }

                if (quiet == false)
                {
                    Duration duration = value.EffectiveDuration(oas);
                    Console.Write("{0:D2}/{1:D2}/{2:D4} {3,8:F3} {4,8:F3} {5,8:F3} {6,8:F3} {7,8:F3}", pvdate.MonthOf(), pvdate.DayOf(), pvdate.YearOf(), fromoas ? price : oas, value.Accrued(), value.OptionValue(oas), duration.duration, duration.convexity);
                    Console.Write("\n");
                }
            }
            if (timing)
            {
                Console.Write("\nSeconds to value the bond for {0:D} pvdates = {1:F2}\n", cnt, INSECS(CurrentUnixTimeMillis() - start));
            }
            return 0;
        }
示例#2
0
 public double YearsTo(Date other, Bond.DAYCOUNT arg1)
 {
     double ret = AkaApiPINVOKE.Date_YearsTo(swigCPtr, Date.getCPtr(other), (int)arg1);
     if (AkaApiPINVOKE.SWIGPendingException.Pending) throw AkaApiPINVOKE.SWIGPendingException.Retrieve();
     return ret;
 }
示例#3
0
 public Date(Date arg0, double yrs, Bond.DAYCOUNT arg2)
     : this(AkaApiPINVOKE.new_Date__SWIG_6(Date.getCPtr(arg0), yrs, (int)arg2), true)
 {
     if (AkaApiPINVOKE.SWIGPendingException.Pending) throw AkaApiPINVOKE.SWIGPendingException.Retrieve();
 }
示例#4
0
 public bool SetYieldMethod(Bond.YIELD_METHOD arg0)
 {
     bool ret = AkaApiPINVOKE.Bond_SetYieldMethod(swigCPtr, (int)arg0);
     return ret;
 }
示例#5
0
 internal static HandleRef getCPtr(Bond obj)
 {
     return (obj == null) ? new HandleRef(null, IntPtr.Zero) : obj.swigCPtr;
 }
示例#6
0
 public Bond.NotifyBehavior SetNotifyBehavior(Bond.NotifyBehavior arg0)
 {
     Bond.NotifyBehavior ret = (Bond.NotifyBehavior)AkaApiPINVOKE.Bond_SetNotifyBehavior(swigCPtr, (int)arg0);
     return ret;
 }
示例#7
0
 public bool SetSinkOption(bool allow_delivery, double acceleration, Bond.ALLOCATION allocation)
 {
     bool ret = AkaApiPINVOKE.Bond_SetSinkOption(swigCPtr, allow_delivery, acceleration, (int)allocation);
     return ret;
 }
示例#8
0
 public bool SetFrequency(Bond.FREQUENCY arg0)
 {
     bool ret = AkaApiPINVOKE.Bond_SetFrequency(swigCPtr, (int)arg0);
     return ret;
 }
示例#9
0
 public Bond(Bond arg0)
     : this(AkaApiPINVOKE.new_Bond__SWIG_1(Bond.getCPtr(arg0)), true)
 {
     if (AkaApiPINVOKE.SWIGPendingException.Pending) throw AkaApiPINVOKE.SWIGPendingException.Retrieve();
 }
示例#10
0
 public Value(Bond arg0, InterestRateModel arg1, SinkingFundStatus arg2, Date pvdate, Date tradedate)
     : this(AkaApiPINVOKE.new_Value__SWIG_6(Bond.getCPtr(arg0), InterestRateModel.getCPtr(arg1), SinkingFundStatus.getCPtr(arg2), Date.getCPtr(pvdate), Date.getCPtr(tradedate)), true)
 {
     if (AkaApiPINVOKE.SWIGPendingException.Pending) throw AkaApiPINVOKE.SWIGPendingException.Retrieve();
 }
示例#11
0
 public bool SetDaycount(Bond.DAYCOUNT arg0)
 {
     bool ret = AkaApiPINVOKE.Bond_SetDaycount(swigCPtr, (int)arg0);
     return ret;
 }
示例#12
0
 public int Reset(Bond arg0, InterestRateModel arg1, SinkingFundStatus arg2, Date pvdate, Date tradedate)
 {
     int ret = AkaApiPINVOKE.Value_Reset__SWIG_5(swigCPtr, Bond.getCPtr(arg0), InterestRateModel.getCPtr(arg1), SinkingFundStatus.getCPtr(arg2), Date.getCPtr(pvdate), Date.getCPtr(tradedate));
     if (AkaApiPINVOKE.SWIGPendingException.Pending) throw AkaApiPINVOKE.SWIGPendingException.Retrieve();
     return ret;
 }
示例#13
0
 public int Reset(Bond arg0, InterestRateModel arg1, Date pvdate)
 {
     int ret = AkaApiPINVOKE.Value_Reset__SWIG_2(swigCPtr, Bond.getCPtr(arg0), InterestRateModel.getCPtr(arg1), Date.getCPtr(pvdate));
     if (AkaApiPINVOKE.SWIGPendingException.Pending) throw AkaApiPINVOKE.SWIGPendingException.Retrieve();
     return ret;
 }
示例#14
0
 public int Reset(Bond arg0, Date pvdate, Date tradedate)
 {
     int ret = AkaApiPINVOKE.Value_Reset__SWIG_1(swigCPtr, Bond.getCPtr(arg0), Date.getCPtr(pvdate), Date.getCPtr(tradedate));
     if (AkaApiPINVOKE.SWIGPendingException.Pending) throw AkaApiPINVOKE.SWIGPendingException.Retrieve();
     return ret;
 }
示例#15
0
 public Value(Bond arg0, InterestRateModel arg1, Date pvdate)
     : this(AkaApiPINVOKE.new_Value__SWIG_3(Bond.getCPtr(arg0), InterestRateModel.getCPtr(arg1), Date.getCPtr(pvdate)), true)
 {
     if (AkaApiPINVOKE.SWIGPendingException.Pending) throw AkaApiPINVOKE.SWIGPendingException.Retrieve();
 }
示例#16
0
 public Value(Bond arg0, Date pvdate, Date tradedate)
     : this(AkaApiPINVOKE.new_Value__SWIG_2(Bond.getCPtr(arg0), Date.getCPtr(pvdate), Date.getCPtr(tradedate)), true)
 {
     if (AkaApiPINVOKE.SWIGPendingException.Pending) throw AkaApiPINVOKE.SWIGPendingException.Retrieve();
 }