public static BondStructureDefinitionItem GetDefinition(BondAnalysisLine line_, double multiplier_)
 {
   return new BondStructureDefinitionItem()
   {
     Coupon=line_.UnderlyingBond.Coupon,
     Market=line_.ParentSource.Market,
     Month=line_.Maturity.Month,
     Year=line_.Maturity.Year,
     Multiplier=multiplier_
   };
 }
Esempio n. 2
0
 public CellClickedArgs(BondAnalysisLine line_, BondField field_, MouseButtons buttons_)
 {
   Line = line_;
   Field = field_;
   Buttons = buttons_;
 }
    public static double[] GetFlyWeightings(BondAnalysisLine A_, BondAnalysisLine B_, BondAnalysisLine C_, FlyWeightingMethod method_ = FlyWeightingMethod.Standard, int history_ = 100)
    {
      switch (method_)
      {
        case FlyWeightingMethod.Standard:
        {
          return new[] {Bfly_Wing, Bfly_Belly, Bfly_Wing};
        }
        case FlyWeightingMethod.Value:
        {
          var Avals =
            A_.GetHistoricValuesForField(BondField.YieldLive)
              .GetEndValues(history_ + 1)
              .GetSubValues<double>(excludeFunction_: double.IsNaN);
          var Bvals =
            B_.GetHistoricValuesForField(BondField.YieldLive)
              .GetEndValues(history_ + 1)
              .GetSubValues(excludeFunction_: double.IsNaN);
          var Cvals =
            C_.GetHistoricValuesForField(BondField.YieldLive)
              .GetEndValues(history_ + 1)
              .GetSubValues(excludeFunction_: double.IsNaN);

          var CminusB = Cvals.Minus(Bvals).ToDifferences().Stdev();
          var BminusA = Bvals.Minus(Avals).ToDifferences().Stdev();

          var ratio = CminusB/BminusA;

          return new double[]
          {
            ratio/(ratio+1d),
            1d,
            1d/(ratio+1d)
          };
        }
        case FlyWeightingMethod.PCAyield:
        case FlyWeightingMethod.PCAasw:
        {
          var Avals =
            A_.GetHistoricValuesForField(method_==FlyWeightingMethod.PCAyield ? BondField.YieldLive : BondField.ASWLive)
              .GetEndValues(history_ + 1)
              .GetSubValues<double>(excludeFunction_: double.IsNaN);
          var Bvals =
            B_.GetHistoricValuesForField(method_ == FlyWeightingMethod.PCAyield ? BondField.YieldLive : BondField.ASWLive)
              .GetEndValues(history_ + 1)
              .GetSubValues(excludeFunction_: double.IsNaN);
          var Cvals =
            C_.GetHistoricValuesForField(method_ == FlyWeightingMethod.PCAyield ? BondField.YieldLive : BondField.ASWLive)
              .GetEndValues(history_ + 1)
              .GetSubValues(excludeFunction_: double.IsNaN);

          var inlineDates = new[] {Avals, Bvals, Cvals}.IntersectDatesKeepSeparate();

          var dblArr = new double[inlineDates[0].Length, 3];
          for(int i=0;i<inlineDates[0].Length;++i)
            for (int j = 0; j < 3; ++j)
              dblArr[i, j] = inlineDates[j].Data[i];

          var pcaWeights = Symmetry.Analytics.PCA.CalculateOptimalHedgeRatios(dblArr, false, 3,
            new double?[] {null, 1d, null});

          return pcaWeights;
        }
      }

      return null;
    }
 public bool SetValuesForLineAndFocus(BondAnalysisLine line_, BondField field_)
 {
   m_allValues = line_.GetHistoricValuesForField(field_);
   m_seriesName= BondAnalysisLineHelper.FindColumnNameForField(field_);
   return reloadInstrumentHistory();
 }
Esempio n. 5
0
 public void BuildStructure(BondAnalysisLine[] bonds_)
 {
   bondStructureComponentsEditor1.applyStructure(bonds_);
 }
Esempio n. 6
0
 public void Dispose()
 {
   m_instantiation = null;
 }
 private bool shouldAddToLocalList(BondAnalysisLine line_)
 {
   return true;
 }
    public static Tuple<int, int> GetCMTTenorWings(BondAnalysisLine line_)
    {
      var toMaturity = line_.GetValue(BondField.TimeToMaturity);

      for (int i = 1; i < _possibleCMTWingsDbls.Length; ++i)
      {
        if (_possibleCMTWingsDbls[i] > toMaturity)
        {
          if (_possibleCMTWingsDbls[i - 1] < toMaturity)
            return new Tuple<int, int>(Convert.ToInt32(_possibleCMTWingsDbls[i - 1]),
              Convert.ToInt32(_possibleCMTWingsDbls[i]));

          break;
        }
      }

      return null;
    }
    public static BondAnalysisLine[] FindBondFromInputString(string input_)
    {
      var matches = m_inputStringRegex.Matches(input_.ToUpper());

      if (matches.Count == 0)
      {
        Singleton<CentralEvents>.Instance.FireStatusMessage(string.Format("Couldn't begin to parse '{0}'", input_));
        return null;
      }

      var arr = new BondAnalysisLine[matches.Count];

      Singleton<CentralEvents>.Instance.FireStatusMessage(string.Empty);

      for(int i=0;i<arr.Length;++i)
      {
        var match = matches[i];

        if (!match.Success)
        {
          Singleton<CentralEvents>.Instance.FireStatusMessage(string.Format("Couldn't begin to parse '{0}'", match.Value));
          continue;
        }

        var mkt = match.Groups["Market"].Value;
        BondMarket market;

        if (!Enum.TryParse<BondMarket>(mkt, out market))
          continue;

        var bondMonth = int.Parse(match.Groups["Month"].Value);
        var bondYear = int.Parse(match.Groups["Year"].Value);
        if (bondYear < 10) { bondYear += (DateTime.Today.Year - (DateTime.Today.Year % 10)); }
        if (bondYear < 2000) { bondYear += 2000; }

        var bonds =
          CountryBondSource.GetInstance(market)
            .Lines.Where(x => x.Maturity.Month == bondMonth && x.Maturity.Year == bondYear);

        double? coupon = null;
        {
          if (match.Groups["Coupon"].Success && !string.IsNullOrEmpty(match.Groups["Coupon"].Value))
            coupon = double.Parse(match.Groups["Coupon"].Value);
        }

        if (coupon.HasValue)
          bonds = bonds.Where(x => Math.Abs(x.UnderlyingBond.Coupon - coupon.Value) < 1e-5);

        arr[i] = bonds.FirstOrDefault();

        if (arr[i] == null)
          Singleton<CentralEvents>.Instance.FireStatusMessage(string.Format("Could not find matching bond for '{0}'",
            match.Value));
      }

      return arr;
    }
    internal void applyStructure(BondAnalysisLine[] result)
    {
      if (result == null || result.Any(x => x == null))
        return;

      SetNumberOfComponents((uint)result.Length);

      switch (result.Length)
      {
        case 1:
          {
            InternalStructure[0].Bond = result[0];
            InternalStructure[0].Multiplier = 1d;
          }
          break;
        case 2: // curve
          {
            InternalStructure[0].Bond = result[0];
            InternalStructure[0].Multiplier = StructureWeightings.Curve_Near;
            InternalStructure[1].Bond = result[1];
            InternalStructure[1].Multiplier = StructureWeightings.Curve_Far;
          }
          break;
        case 3: // butterfly
          {
            InternalStructure[0].Bond = result[0];
            InternalStructure[0].Multiplier = StructureWeightings.Bfly_Wing;
            InternalStructure[1].Bond = result[1];
            InternalStructure[1].Multiplier = StructureWeightings.Bfly_Belly;
            InternalStructure[2].Bond = result[2];
            InternalStructure[2].Multiplier = StructureWeightings.Bfly_Wing;
          }
          break;
        case 4:
          {
            InternalStructure[0].Bond = result[0];
            InternalStructure[0].Multiplier = StructureWeightings.Box_1;
            InternalStructure[1].Bond = result[1];
            InternalStructure[1].Multiplier = StructureWeightings.Box_2;
            InternalStructure[2].Bond = result[2];
            InternalStructure[2].Multiplier = StructureWeightings.Box_3;
            InternalStructure[3].Bond = result[3];
            InternalStructure[3].Multiplier = StructureWeightings.Box_4;
          }
          break;
        case 5:
          {
            for (int i = 0; i < result.Length; ++i)
            {
              InternalStructure[i].Bond = result[i];
              InternalStructure[i].Multiplier = 1d;
            }
          }
          break;
      }
      OnGoClicked(EventArgs.Empty);
    }
    private static BondAnalysisLine findBestBond(BondAnalysisLine bellyBond_, DateTime perfectDate_, IEnumerable<BondAnalysisLine> candidates_, BondFlyWeightMode mode_, int unitGap_, MaturityGapUnit unitType_)
    {
      DateTime fromDate = perfectDate_, toDate = perfectDate_;
      switch (mode_)
      {
        case BondFlyWeightMode.WithinMonthNonWeighted:
          {
            if (unitType_ == MaturityGapUnit.Years && bellyBond_.TimeToMaturity >= 10)
            {
              fromDate = perfectDate_.AddYears(-1);
              toDate = perfectDate_.AddYears(1);
            }
            else
            {
              fromDate = perfectDate_.AddMonths(-1);
              toDate = perfectDate_.AddMonths(1);
            }
          }
          break;
        case BondFlyWeightMode.WithinAThirdWeighted:
        case BondFlyWeightMode.WithinAThirdNonWeighted:
          {
            if (unitType_ == MaturityGapUnit.Years)
            {
              fromDate = perfectDate_.AddMonths(-Convert.ToInt32(unitGap_ * 12 / 3));
              toDate = perfectDate_.AddMonths(Convert.ToInt32(unitGap_ * 12 / 3));
            }
            else if (unitType_ == MaturityGapUnit.Months)
            {
              fromDate = perfectDate_.AddMonths(-Convert.ToInt32(unitGap_/3));
              toDate = perfectDate_.AddMonths(Convert.ToInt32(unitGap_/3));
            }
          }
          break;
      }

      var best2 =
        candidates_.Where(
          x =>
            // within maturity range
            x.Maturity >= fromDate && x.Maturity <= toDate &&
            // not the belly bond
            String.Compare(x.UnderlyingBond.Isin, bellyBond_.UnderlyingBond.Isin, StringComparison.OrdinalIgnoreCase) !=
            0)
          .Select(x => new {Gap = Math.Abs((perfectDate_ - x.Maturity).TotalDays), Candidate = x})
          // ordered by the gap to the perfect date
          .OrderBy(x => x.Gap)
          .Take(2).ToArray();

      if (best2 == null || best2.Length == 0) return null;

      if (best2.Length == 1)
        return best2[0].Candidate;

      // if there are 2 bonds maturing in the same month, then return the lower coupon one 
      if (best2[0].Candidate.Maturity.Year == best2[1].Candidate.Maturity.Year
        && best2[0].Candidate.Maturity.Month == best2[1].Candidate.Maturity.Month)
      {
        return best2.OrderBy(x => x.Candidate.UnderlyingBond.Coupon).First().Candidate;
      }

      return best2[0].Candidate;
    }