コード例 #1
0
ファイル: Value.cs プロジェクト: radekwyrwas/akalib
 internal static HandleRef getCPtr(Value obj)
 {
     return (obj == null) ? new HandleRef(null, IntPtr.Zero) : obj.swigCPtr;
 }
コード例 #2
0
ファイル: AkaLibExample.cs プロジェクト: radekwyrwas/akalib
        // ***********************************************************
        // 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;
        }
コード例 #3
0
ファイル: Value.cs プロジェクト: radekwyrwas/akalib
 public bool FlowType(int idx, Value.FLOWTYPE t)
 {
     bool ret = AkaApiPINVOKE.Value_FlowType__SWIG_1(swigCPtr, idx, (int)t);
     return ret;
 }