Exemplo n.º 1
0
    public void EnsureBondInMarket(string market_, MongoBond bond_)
    {
      if (bond_ == null) return;

      if (!_cache.ContainsKey(market_))
      {
        // make sure we've tried to get from the server first
        GetBondsForMarket(market_);

        if(!_cache.ContainsKey(market_))
          _cache[market_] = new List<MongoBond>();
      }

      if (!_cache[market_].Contains(bond_))
        _cache[market_].Add(bond_);
    }
    public static IEnumerable<KeyValuePair<string, double>> GetHistoricValues(
        BondMarket market_,
        MongoBond bond_,
        double price_,
        DateTime asOf_,
        CarbonClient client_,
        string pricingSetup_="Sym_LIBOR"
  )
    {
      try
      {
        var baseBondId = getBaseBondID(market_, bond_.Maturity);

        var result = client_.PriceBondAsync(
          baseBondId: (long)baseBondId,
          issueDate: DateConversions.ToNodaLocalDate(bond_.IssueDate),
          maturityDate: DateConversions.ToNodaLocalDate(bond_.Maturity),
          coupon: bond_.Coupon / 100d,
          asof: asOf_,
          pricingSetup: pricingSetup_,
          quantity: 1000000d,
          stubDate: DateConversions.ToNodaLocalDate(bond_.FirstCouponDate),
          price: price_).Result;

        if (result.Results.Any(x => double.IsNaN(x.Value)))
        {
          Logger.Error(
            string.Format("At least one Null value returned from carbon Date={0}, Market={1}", asOf_.ToString("dd-MMM-yyyy"),
              market_), typeof(CarbonHistoricRetriever));
        }

        return result.Results;
      }
      catch (Exception ex_)
      {
        Logger.Error("Error pricing bond", typeof(CarbonHistoricRetriever), ex_);
        return null;
      }
    }
Exemplo n.º 3
0
    private static void stage1_getBondValuesFromCarbon(BondMarket market_, IEnumerable<SI.Data.Bond> bonds_,  DateTime date_, CarbonClient client_)
    {
      foreach (var bond in bonds_)
      {
        var coll = Singleton<Mongo.MongoBondCache>.Instance.GetBondsForMarket(bond.Market);

        MongoBond mBond = null;

        if (coll.Count > 0)
          mBond =
            coll.FirstOrDefault(x => string.Compare(bond.SymmetryCode, x.Isin, StringComparison.OrdinalIgnoreCase) == 0);

        if (mBond == null)
        {
          Logger.Info(string.Format("Adding in bond with isin = {0}", bond.SymmetryCode), typeof (DataBuilder));
          mBond = new MongoBond()
          {
            Coupon = bond.Coupon,
            DisplayName = bond.DisplayName,
            Isin = bond.SymmetryCode,
            Market = bond.Market,
            Maturity = bond.Maturity.Value,
            Term = bond.Term.Value,
            IssueDate = bond.IssueDate ?? DateTime.MinValue,
            FirstCouponDate = bond.FirstCouponDate ?? DateTime.MinValue,
            EffectiveDate=bond.EffectiveDate ?? DateTime.MinValue,
          };

          {
            var refData = BbgTalk.HistoryRequester.GetReferenceData(string.Format("{0} Govt", bond.SymmetryCode),
              "AMT_ISSUED", null);
            if (refData.AllResult.Count > 0)
              mBond.Issuance = refData.GetValue<double>("AMT_ISSUED");
          }
          mBond.Changed = true;
          Singleton<Mongo.MongoBondCache>.Instance.EnsureBondInMarket(bond.Market, mBond);
        }
        else
        {
          // maintain dates
          if (bond.FirstCouponDate.HasValue && bond.FirstCouponDate.Value != mBond.FirstCouponDate)
          {
            mBond.FirstCouponDate = bond.FirstCouponDate.Value;
            mBond.Changed = true;
          }

          if (bond.IssueDate.HasValue && bond.IssueDate.Value != mBond.IssueDate)
          {
            mBond.IssueDate = bond.IssueDate.Value;
            mBond.Changed = true;
          }

          if (bond.EffectiveDate.HasValue && bond.EffectiveDate.Value != mBond.EffectiveDate)
          {
            mBond.EffectiveDate = bond.EffectiveDate.Value;
            mBond.Changed = true;
          }
        }

        //mBond.IsBenchmark = (benchIsins != null && benchIsins.ContainsValue(mBond.Isin.ToUpper()));

        double? price = null;

        // may have to use MLP price if not there
        if (!price.HasValue)
        {
          var prices = bond.GetPrice("MLP", "MLP", 1);
          if (prices != null && prices.HasDate(date_))
            price = prices.ValueOnExactDate(date_);
        }

        // try to get the 'price' value from SYM.LDN 
        if (!price.HasValue)
        {
          var prices = bond.GetPrice("SYM", market_==BondMarket.US ? "NYK" : "LDN", 1);
          if (prices != null && prices.HasDate(date_))
            price = prices.ValueOnExactDate(date_);
        }

        if (!price.HasValue)
        {
          Logger.Error(
            string.Format("Price value not found for bond={0} on date={1}", bond.SymmetryCode,
              date_.ToString("dd-MMM-yyyy")), typeof(DataBuilder));
          continue;
        }

        var flds = CarbonHistoricRetriever.GetHistoricValues(market_, bond, price.Value / 100d, date_.Date.AddHours(23).AddMinutes(59),
          client_);

        if (flds == null)
        {
          Logger.Error(
            string.Format("Failure to get values from Carbon/OT for {0} on {1}", bond.SymmetryCode,
              date_.ToString("dd-MMM-yyyy")), typeof (DataBuilder));
          continue;
        }

        var ts = new TimeSeriesLine {CleanPrice = price.Value};

        foreach (var kvp in flds)
        {
          switch (kvp.Key)
          {
            case CarbonFields.ASWpp:
              ts.ASWpp = kvp.Value;
              break;
            case CarbonFields.ASWyy:
              ts.ASWyy = kvp.Value;
              break;
            case CarbonFields.Yield:
              ts.Yield = kvp.Value;
              break;
            case CarbonFields.ZSpread:
              ts.ZSpread = kvp.Value;
              break;
            case CarbonFields.ZSpread01:
              ts.ZSpread01 = kvp.Value;
              break;
            case CarbonFields.YieldBPV:
              ts.YieldBPV = kvp.Value;
              break;
            case CarbonFields.ModifiedDuration:
              ts.ModDuration = kvp.Value;
              break;
            case CarbonFields.DirtyPrice:
              ts.DirtyPrice = kvp.Value * 100d;
              break;
          }
        }

        if (mBond.TimeSeries == null)
        {
          mBond.TimeSeries = new DatedDataCollectionGen<TimeSeriesLine>(
            new[] { date_ },
            new[] { ts }
            );
        }
        else
        {
          mBond.TimeSeries.SetValueOnDate(date_, ts);
        }

        mBond.Changed = true;

        Singleton<Mongo.MongoBondCache>.Instance.EnsureBondInMarket(bond.Market, mBond);
      }
    }
//    public static IEnumerable<KeyValuePair<string,double>> GetForwardValues(
//        BondMarket markets_,
//        SI.Data.Bond bond_,
//        double cleanPriceInPercent_,
//        CouponPaymentFrequency freq_,
//        DateTime settleDate_,
//        DateTime forwardDate_,
//        double repoRateInPercent_
//)
//    {
//      var fwdPrice = FwdPriceCalculator.GetForwardPrice(bond_.IssueDate.Value, bond_.Maturity.Value, cleanPriceInPercent_,
//        bond_.Coupon, freq_, settleDate_,
//        forwardDate_, repoRateInPercent_);

//      var vals = CarbonHistoricRetriever.GetHistoricValues(
//        markets_: markets_,
//        asOf_: forwardDate_,
//        bond_: bond_,
//        price_: fwdPrice / 100d,
//        client_: CarbonSubscriber.GetInstance().GetCarbonClientInstance());

//      return vals;
//    }

    /// <summary>
    /// Get the forward values given the inputs
    /// </summary>
    /// <param name="market_"></param>
    /// <param name="marketCode_"></param>
    /// <param name="bond_"></param>
    /// <param name="cleanPriceInPercent_">1.01 not 101.00</param>
    /// <param name="freq_"></param>
    /// <param name="settleDate_"></param>
    /// <param name="forwardDate_"></param>
    /// <param name="repoRateInPercent_">in decimals</param>
    /// <returns></returns>
    public static IEnumerable<KeyValuePair<string, double>> GetForwardValues(
    BondMarket market_,
      string marketCode_,
    MongoBond bond_,
    double cleanPriceInPercent_,
    CouponPaymentFrequency freq_,
    DateTime settleDate_,
    DateTime forwardDate_,
    double repoRateInPercent_
)
    {
      var fwdPrice = FwdPriceCalculator.GetForwardPrice(
        effectiveDate_: bond_.IssueDate,
        maturityDate_: bond_.Maturity,
        firstCouponDate_:bond_.FirstCouponDate,
        cleanPriceInPercent_: cleanPriceInPercent_,
        couponInPercent_: bond_.Coupon,
        freq_: freq_,
        settleDate_: settleDate_,
        repoRateInPercent_: repoRateInPercent_,
        market_: market_,
        forwardDate_: forwardDate_);

      var vals = CarbonHistoricRetriever.GetHistoricValues(
        market_: market_,
        asOf_: forwardDate_,
        bond_: bond_,
        price_: fwdPrice / 100d,
        client_: CarbonSubscriber.GetInstance().GetCarbonClientInstance());

      return vals;
    }