Ejemplo n.º 1
0
    public static void Go(DateTime date_)
    {
      LookFeel.SetupNoirModerneLookFeel();

      double thresholdForFlagging = 0.5d;

      var controlDict=new Dictionary<string,SimpleWtsColumnChart>();

      var listOfItems = Retriever.Retrieve(date_);

      var listOfAnomolies = new List<FlagListItem>();

      foreach (var distinctCurve in listOfItems.Select(x => x.CurveName).Distinct())
      {
        var curveSubset = listOfItems.Where(x => x.CurveName.Equals(distinctCurve));

        foreach (var distinctTenor in curveSubset.Select(x => x.Tenor).Distinct().OrderBy(x=>x))
        {
          var tenorAndCurveSubset = curveSubset.Where(x => x.Tenor == distinctTenor);

          var isins = new List<string>();
          var changeInMMS = new List<double>();

          foreach (var isin in tenorAndCurveSubset.OrderBy(x=>x.Maturity).Select(x => x.ISIN).Distinct())
          {
            var isinItems = tenorAndCurveSubset.Where(x => x.ISIN.Equals(isin)).OrderByDescending(x => x.ValueDate).ToArray();

            if (isinItems.Count() > 1)
            {
              isins.Add(isin);
              changeInMMS.Add((isinItems[0].MMS - isinItems[1].MMS) * 10000d);
            }
          }

          if (isins.Count() == 0)
            continue;

          for (int i = 1; i < isins.Count - 1; ++i)
          {
            var neighbourAverage = (changeInMMS[i - 1] + changeInMMS[i + 1]) / 2d;
            var diff = changeInMMS[i] - neighbourAverage;

            var bond = listOfItems.Where(x => string.Compare(isins[i], x.ISIN, true) == 0).First();

            if (Math.Abs(diff) >= thresholdForFlagging)
            {
              var timeToMaturity = bond.Maturity - DateTime.Today;

              // ignore bonds with less than one yeat to maturity
              if (timeToMaturity.Days < 252)
                continue;

              listOfAnomolies.Add(new FlagListItem()
              {
                MarkDate = date_,
                Curve = distinctCurve,
                Tenor = distinctTenor,
                Isin = isins[i],
                ChangeInMMS = changeInMMS[i],
                LeftBond = listOfItems.Where(x => string.Compare(isins[i-1], x.ISIN, true) == 0).First().Bond,
                ChangeInMMSLeft = changeInMMS[i - 1],
                RightBond = listOfItems.Where(x => string.Compare(isins[i+1], x.ISIN, true) == 0).First().Bond,
                ChangeInMMSRight = changeInMMS[i + 1],
                Bond=bond.Bond
              });
            }
          }

          var chart = new Controls.SimpleWtsColumnChart();
          chart.SetYAxisFormat("##0.000#");

          chart.Create(isins.ToArray(), changeInMMS.ToArray());
          chart.SetXAxisExtent(100);

          controlDict.Add(string.Format("{0}_{1}", distinctCurve, distinctTenor), chart);
        }
      }


      if (listOfAnomolies.Count <= 0) return;

      MailMessage msg = new MailMessage("*****@*****.**", "*****@*****.**", "beta: MMS change opportunity / change report", "body");
      msg.To.Clear();
      msg.To.Add("*****@*****.**");
      msg.To.Add("*****@*****.**");
      msg.IsBodyHtml = true;

      var builder = new StringBuilder();

      builder.AppendLine(string.Format("<h3>Unusual bond MMS moves on {0}</h3>", date_.ToString("dd-MMM-yyyy")));
      builder.AppendLine("<p>(Criteria is 0.5 bps difference from the average of the bonds either side)</p>");

      var grid = listOfAnomolies.DisplayInBoundGrid("blah", false);

      builder.AppendLine(grid.ToHTML());

      var resourceDict=new Dictionary<string,string>();

      foreach (string chartKey in listOfAnomolies.Select(x => string.Format("{0}_{1}", x.Curve, x.Tenor)).Distinct())
      {
        builder.AppendLine(string.Format("<h4>{0} - bp change in MMS per bond</h4>", chartKey));
        builder.AppendLine(string.Format("<img src=cid:{0}>", chartKey));

        var path = string.Format("{0}\\{1}_{2}.bmp", Environment.GetFolderPath(Environment.SpecialFolder.InternetCache), chartKey, date_.ToString("ddMMyyyy"));
        controlDict[chartKey].Chart.DataBind();
        LookFeel.ProcessChart(controlDict[chartKey]);
        controlDict[chartKey].Chart.SaveTo(path, System.Drawing.Imaging.ImageFormat.Bmp);
        resourceDict.Add(chartKey, path);
      }

      AlternateView htmlView = AlternateView.CreateAlternateViewFromString(builder.ToString(), null, "text/html");
      foreach (var k in resourceDict.Keys)
      {
        LinkedResource lr = new LinkedResource(resourceDict[k]);
        lr.ContentId = k;
        htmlView.LinkedResources.Add(lr);
      }

      msg.AlternateViews.Add(htmlView);


      try
      {
        Emailer.GetClient().Send(msg);
      }
      catch (Exception ex_)
      {
        Logger.Error("Error sending email", typeof (Analyzer), ex_);
      }

    }
Ejemplo n.º 2
0
    private void rebind()
    {
      var finestGranulatiry = 
        m_evaluator.Daily.Length > (2*m_evaluator.Weekly.Length)
          ? Period.Daily
          : m_evaluator.Weekly.Length > (2*m_evaluator.Monthly.Length) 
            ? Period.Weekly 
            : Period.Monthly;

      lblCumReturn.Bind(m_evaluator.Daily, "CumulativeReturn", new Validators.DoubleValidator("#0.00%"));
      lblAvgReturnPA.Bind(m_evaluator.Daily, "AverageReturnsPerAnnum", new Validators.DoubleValidator("#0.00%"));


      lblAnnualSharpe.Bind(m_evaluator[finestGranulatiry], "AnnualSharpeRatio", new Validators.DoubleValidator("#0.00"));
      lblRealisedVolPA.Bind(m_evaluator[finestGranulatiry], "RealisedVolPerAnnum", new Validators.DoubleValidator("#0.00%"));


      lblYTD.Bind(m_evaluator.Daily, "YTD", new Validators.DoubleValidator("#0.00%"));
      lblSortino.Bind(m_evaluator.Daily, "AnnualSortinoRatio", new Validators.DoubleValidator("#0.00"));

      // periodic returns
      dailyReturnsDisplay.Bind(m_evaluator.Daily);
      weeklyReturnsDisplay.Bind(m_evaluator.Weekly);
      monthlyReturnsDisplay.Bind(m_evaluator.Monthly);

      monthlyReturnsChart.Bind(m_evaluator, ReturnsEval.Period.Monthly, "MMM-yy");

      // drawdowns
      for (int i = 0; i < m_drawdownControls.Length; ++i)
      {
        // ReSharper disable once AssignmentInConditionalExpression
        if (m_drawdownControls[i].Visible = (i < m_evaluator.Daily.DrawDowns.Length))
          m_drawdownControls[i].Bind(m_evaluator, i, true);
      }
      drawdownAnalysisControl1.Bind(m_evaluator);
      cumulativeChart.Bind(m_evaluator, ReturnsEval.Period.Daily);

      if (m_evaluator.InnerSeries.Count > 0)
      {
        #region Add overall attribution

        if (m_overallAttributionChart == null)
        {
          Infragistics.Win.UltraWinTabControl.UltraTab tab = ultraTabControl1.Tabs.Add();
          tab.Text = "Attribution";
          m_overallAttributionChart = new Controls.SimpleWtsColumnChart();
          m_overallAttributionChart.Dock = DockStyle.Fill;
          m_overallAttributionChart.SetXAxisExtent(80);
          tab.TabPage.Controls.Add(m_overallAttributionChart);
        }

        m_overallAttributionChart.Create(m_evaluator.InnerSeries.Select(x => x.Name).ToArray(), m_evaluator.InnerSeries.Select(x => x.Daily.CumulativeReturn).ToArray());
        m_overallAttributionChart.SetXAxisExtent(80);

        #endregion

        #region Add Monthly Attribution

        //if (m_datedReturnsChartAttributed == null)
        //{
        //  Infragistics.Win.UltraWinTabControl.UltraTab tab = ultraTabControl1.Tabs.Add();
        //  tab.Text = "Monthly Attribution";
        //  m_datedReturnsChartAttributed = new DatedReturnsChartAttributed();
        //  m_datedReturnsChartAttributed.Dock = DockStyle.Fill;
        //  tab.TabPage.Controls.Add(m_datedReturnsChartAttributed);
        //}

        //if(m_datedReturnsChartAttributed!=null)
        //  m_datedReturnsChartAttributed.Bind(m_evaluator, ReturnsEval.Period.Monthly, "MMM yy");

        #endregion

        #region Add Cumulative returns comparison

        //if (m_cumulativeWithInnerAttributedChart == null)
        //{
        //  Infragistics.Win.UltraWinTabControl.UltraTab tab = ultraTabControl1.Tabs.Add();
        //  tab.Text = "Cumulative Returns Attribution";
        //  m_cumulativeWithInnerAttributedChart = new CumulativeWithInnerAttributedChart();
        //  m_cumulativeWithInnerAttributedChart.Dock = DockStyle.Fill;
        //  tab.TabPage.Controls.Add(m_cumulativeWithInnerAttributedChart);
        //}

        //if(m_cumulativeWithInnerAttributedChart!=null)
        //  m_cumulativeWithInnerAttributedChart.Bind(m_evaluator);

        #endregion

      }

      bindRW();
    }