Esempio n. 1
0
    protected void getCalcs()
    {
      var conf = CountryConfig.GetCountryConfig(Market);

      if (Values == null)
        Values =
          new ConstructGen<double>(
            ((ColPosition[]) Enum.GetValues(typeof (ColPosition))).Select(x => x.ToString()).ToArray());

      var crv = Singleton<CMTCurveCache>.Instance.Get(
        market_: Market,
        series_: CMTSeriesNumber,
        curve_: conf.OISCurve,
        focus_: Focus.ASW,
        createIfNotThere_: true);

      if (crv == null || crv.Data == null) return;

      // need to find the indexof of the tenor points in the data construct
      int pointIndex = -1;

      for (int i = 0; i < crv.Data.ArrayLength && (pointIndex == -1); ++i)
      {
        var asDbl = double.Parse(crv.Data.ColumnHeadings[i]);

        if (Math.Abs(asDbl - Tenor) < 1e-7)
          pointIndex = i;
      }

      var startDate = new DateTime(2010, 1, 1);


      Values.SetColumnValues((int)ColPosition.CMT_OIS, crv.Data.GetColumnValuesAsDDC(pointIndex).GetValuesBetweenDates(startDate, DateTime.Today).MultiplyBy(-100d));
      Values.SetColumnValues((int)ColPosition.GC, BbgTalk.HistoryRequester.GetHistory(startDate, conf.CollateralTicker, "PX_LAST", false));
      Values.SetColumnValues((int)ColPosition.OIS, BbgTalk.HistoryRequester.GetHistory(startDate, conf.OISTicker, "PX_LAST", false));

      Values.SortKeys();

      using (var cc = new CarbonClient("PRD"))
      {
        var fillCols = new[] { ColPosition.CMT_OIS, ColPosition.GC, ColPosition.OIS };

        for (int i = 0; i < Values.Dates.Count; ++i)
        {
          var date = Values.Dates[i];
          var vals = Values.GetValues(Values.Dates[i]);

          // fill in zeros as unlikely all series will line up
          if (i > 0)
          {
            foreach (var col in fillCols)
            {
              if (vals[(int)col].IsZero())
                vals[(int)col] = Values.GetValues(Values.Dates[i - 1])[(int)col];
            }
          }

          vals[(int)ColPosition.GCminusOIS] = vals[(int)ColPosition.GC] - vals[(int)ColPosition.OIS];


          bool needToMakeCall = true;

          {
            var currentValue = Values.GetValue(date.Date, (int) ColPosition.SwapValue);
            if (!currentValue.IsZero())
              needToMakeCall = false;
          }

          if (needToMakeCall)
          {
            Logger.Info(string.Format("Making calls for {0}.{1}.{2} carry calc on {3}",Market,Tenor,CMTSeriesNumber, date.ToString("dd-MMM-yyyy")),
              typeof(CMTCarry));


            var settleDate = cc.RollDateAsync(date.ToNodaLocalDate(), 2, Symmetry.Carbon.Model.DateUnit.Bd,
              Symmetry.Carbon.Model.BusinessDayConvention.Following, conf.HolidayCalendar).Result;

            var swapDate =
              cc.RollDateAsync(settleDate, Tenor, Symmetry.Carbon.Model.DateUnit.Y,
                Symmetry.Carbon.Model.BusinessDayConvention.Following, conf.HolidayCalendar).Result;

            var swapResult = cc.PriceSwapAsync(
              curveName: conf.OTSwapName,
              startDate: settleDate,
              endDate: swapDate,
              asof: date.Date.AddHours(23),
              pricingSetup: "Symmetry").Result;

            if (swapResult == null) continue;

            foreach (var v in swapResult.Results)
            {
              switch (v.Key)
              {
                case "BreakEven Rate":
                  vals[(int)ColPosition.SwapValue] = v.Value;
                  break;
                case "PV01":
                  vals[(int)ColPosition.SwapDuration] = v.Value;
                  break;
              }
            }
            m_changed = true;
          }
        }

        if (Values.NeedsToSortKeys())
          Values.SortKeys();
        // 20 day moving average of difference
        {
          var series = Values.GetColumnValuesAsDDC((int)ColPosition.GCminusOIS);
          Values.SetColumnValues((int)ColPosition.GCminusOIS_MA20, HelperMethods.GetMA(series, 20));
        }

        // calculate carry
        {
          var mustNotBeZero = new[]
          {
            ColPosition.CMT_OIS,
            ColPosition.GCminusOIS_MA20,
            ColPosition.SwapDuration
          };

          foreach (var date in Values.Dates)
          {
            var series = Values.GetValues(date);

            foreach(var col in mustNotBeZero)
              if (series[(int) col].IsZero())
                continue;

            series[(int) ColPosition.Carry] = (series[(int) ColPosition.CMT_OIS] - series[(int) ColPosition.GCminusOIS_MA20])/
                                         series[(int) ColPosition.SwapDuration]*10000d;
          }
        }
      }
    }
    internal static BondSpreadResult GetSpreads(
      OTBond bondType_, 
      NodaTime.LocalDate maturity_, 
      NodaTime.LocalDate issueDate_, 
      NodaTime.LocalDate firstCouponDate_,
      QuoteValueType priceType_, 
      double priceValue_, 
      double coupon_, 
      CurveMappings.Mapping pricingSetup_, 
      DateTime asOf_, 
      CarbonClient client_, 
      ThrowBehavior behavior_=ThrowBehavior.DontThrow )
    {
      var ret = new BondSpreadResult();
      ret.Spreads = new BondSpread();

      try
      {
        SLog.log.DebugFormat(
          "Calling PriceBondAsync with params: baseBondID={0} issueDate={1} maturity={2} coupon={3} asOf={4} pricingSetup={5} stubDate={6} priceValue={7}, priceType_={8}",
          ((long) bondType_).ToString(), issueDate_, maturity_, coupon_, asOf_, pricingSetup_.OT_BondPricingSetup,
          firstCouponDate_, priceValue_, priceType_);


        switch (priceType_)
        {
          case QuoteValueType.Price:
            {
              ret.Price = priceValue_;

              var result = client_.PriceBondAsync(
                baseBondId: (long)bondType_,
                issueDate: issueDate_,
                maturityDate: maturity_,
                coupon: coupon_,
                asof: asOf_,
                pricingSetup: pricingSetup_.OT_BondPricingSetup,
                quantity: 100000d,
                stubDate: firstCouponDate_,
                price: ret.Price.Value).Result;

              ret.Spreads.Spread = -result.Results[ServiceConstants.KEY_ASW_YY];
              ret.Spreads.TrueSpread = -result.Results[ServiceConstants.KEY_ZSpread];
              ret.Spreads.Yield = result.Results[ServiceConstants.KEY_Yield];
            }

            break;
          case QuoteValueType.Yield:
            {
              var result = client_.GetBondPriceFromYieldAsync(
                baseBondId: (long)bondType_,
                issueDate: issueDate_,
                maturityDate: maturity_,
                coupon: coupon_,
                asof: asOf_,
                pricingSetup: pricingSetup_.OT_BondPricingSetup,
                quantity: 100000d,
                yield: priceValue_,
                stubDate: firstCouponDate_).Result;

              ret.Price = result.Results[ServiceConstants.KEY_Price];

              ret.Spreads.Spread = -result.Results[ServiceConstants.KEY_ASW_YY];
              ret.Spreads.TrueSpread = -result.Results[ServiceConstants.KEY_ZSpread];
              ret.Spreads.Yield = priceValue_;
            }


            break;
          default:
            SLog.log.ErrorFormat("Cannot call Carbon to get spreads with priceType {0}", priceType_);
            break;
        }

        SLog.log.DebugFormat(
          "Calling PriceSwapAsync with params: curveName={0} startDate={1} endDate={2} asOf={3} pricingSetup={4}",
          pricingSetup_.OT_Swap, issueDate_, maturity_, asOf_, pricingSetup_.OT_BondPricingSetup);

        var mmsResult = client_.PriceSwapAsync(
          curveName: pricingSetup_.OT_Swap,
          startDate: asOf_.ToNodaLocalDate(),
          endDate: maturity_,
          asof: asOf_,
          pricingSetup: pricingSetup_.OT_BondPricingSetup).Result;

        ret.Spreads.MMS = mmsResult.Results[ServiceConstants.KEY_MMS];

        SLog.log.DebugFormat("Result: y={0} m={1} s={2} t={3}", ret.Spreads.Yield, ret.Spreads.MMS, ret.Spreads.Spread, ret.Spreads.TrueSpread);

        postProcess(bondType_, ret.Spreads, pricingSetup_);
      }
      catch (Exception ex_)
      {
        Exceptions.Rethrow("GetCurves", behavior_, ex_);
      }

      return ret;
    }