protected ATMVolsRankGroup(FXGroup group_)
    {
      var currencies = Singleton<FXIDs>.Instance.Where(x => x.IsGroup(group_)).ToArray();

      var oneWeek = new ConstructGen<double>(currencies.Select(x => x.Code).ToArray());
      var oneMonth = new ConstructGen<double>(currencies.Select(x => x.Code).ToArray());

      for (int i = 0; i < currencies.Length; ++i)
      {
        {
          var vols = BbgTalk.HistoryRequester.GetHistory(DataConstants.DATA_START, currencies[i].AtTheMoneyVolTicker_1W, "PX_LAST", false);
          oneWeek.SetColumnValues(i, vols);
        }
        {
          var vols = BbgTalk.HistoryRequester.GetHistory(DataConstants.DATA_START, currencies[i].AtTheMoneyVolTicker_1M, "PX_LAST", false);
          oneMonth.SetColumnValues(i, vols);
        }
      }

      {
        oneWeek.SortKeys();
        var avg1W = oneWeek.AvgRows();
        ATM_1W_Avg = avg1W;
        ATM_1W_o6M = avg1W.ToPercentileRanked(126);
        ATM_1W_o1Y = avg1W.ToPercentileRanked(252);
      }

      {
        oneMonth.SortKeys();
        var avg1M = oneMonth.AvgRows();
        ATM_1M_Avg = avg1M;
        ATM_1M_o6M = avg1M.ToPercentileRanked(126);
        ATM_1M_o1Y = avg1M.ToPercentileRanked(252);
      }
    }
    public static ConstructGen<double> GetSmoothCurvesColumnsAreCurvePoints(DateTime valueDate_, uint curveCount_, BondMarket market_, BondField field_, SI.Data.BondCurves curve_, string close_ = "MLP", string source_ = "MLP")
    {
      DateTime date = valueDate_;

      var points = new List<decimal>();

      for (decimal d = 1M; d < 30M; d = d + 0.25M)
        points.Add(d);

      var con = new ConstructGen<double>(points.Select(x => x.ToString()).ToArray());

      for (int i = 0; i < curveCount_; ++i)
      {
        var curve = GetSmoothCurve(date, market_, field_, curve_, close_, source_);
        if (curve == null) continue;

        foreach (var node in curve.GetNodes())
        {
          int index = points.IndexOf(node);

          if (index == -1) continue;

          var point = curve.GetValue(node);

          if(point.HasValue)
            con.SetValue(date, index, point.Value);
        }

        date = MyCalendar.PrevWeekDay(date);
      }

      con.SortKeys();

      return con;
    }
    private ConstructGen<double> getConstructOfInvoiceSpreads()
    {
      var con = new ConstructGen<double>(Configs.Length);

      for (int i = 0; i < con.ArrayLength; ++i)
      {
        for (int j = 0; j < Collections[i].Lines.Count; ++j)
        {
          var date = Collections[i].Lines[j].Date;
          var val = Collections[i].Lines[j].InvoiceSpread;

          con.SetValue(date, i, val ?? double.PositiveInfinity);
        }
      }

      con.SortKeys();

      // feed forward missing values

      double[] before = null;

      foreach (var date in con.Dates)
      {
        var today = con.GetValues(date);
        if (before != null)
        {
          for(int i=0;i<today.Length;++i)
            if (double.IsInfinity(today[i]))
              today[i] = before[i];
        }
        before = today;
      }

      return con;
    }
    public void Refresh()
    {
      // going to get historical values from our database
      ConstructGen<double> hist = new ConstructGen<double>(m_tickers.Count<string>());

      for (int i = 0; i < m_tickers.Length; ++i)
      {
        var histvalues = BbgTalk.HistoryRequester.GetHistory(SI.Data.DataConstants.DATA_START, m_tickers[i],
          "PX_LAST", false, null);

        //histvalues = histvalues.unwind_1d();

        hist.SetColumnValues(i, histvalues);
      }

      hist.SortKeys();

      // initialise 'today's' values to previous day
      hist.SetValues(DateTime.Today, (double[])hist.GetValues(hist.LastDate).Clone());

      double[] avgs = new double[m_windowLength];

      // initialise the avgs array NOTE: today's value is in item with index 0
      for (int i = 0; i < m_windowLength; ++i)
        avgs[i] = hist.GetValues(hist.Dates[hist.Dates.Count - 1 - i]).Average();

      m_avgs = avgs;
      m_liveValues = hist.GetValues(DateTime.Today);
    }
Exemple #5
0
    public static ConstructGen<PairTrade> CalculateWeights(ComID[] commodities_)
    {
      // calculate the weighting
      var con = new ConstructGen<PairTrade>(commodities_.Select(x => x.Name).ToArray());

      for (int i = 0; i < con.ArrayLength; ++i)
        con.SetColumnValues(i, CalculateWeights(commodities_[i]));

      if (con.NeedsToSortKeys())
        con.SortKeys();

      return con;
    }
    public void Create(IList<DataSeriesEvaluator> evals_)
    {
      m_evals = evals_;
      m_pnls = new ConstructGen<double>(evals_.Count);

      for (int evalIndex = 0; evalIndex < evals_.Count; ++evalIndex)
        for (int i = 0; i < m_evals[evalIndex].Daily.Data.Length; ++i)
          m_pnls.SetValue(m_evals[evalIndex].Daily.Dates[i], evalIndex, m_evals[evalIndex].Daily.Data[i]);

      m_pnls.SortKeys();

      m_customStartDate = DateTime.Today.Month == 1
        ? new DateTime(DateTime.Today.Year - 1, 1, 1)
        : new DateTime(DateTime.Today.Year, 1, 1);

      //AsOfDate = MyCalendar.PrevWeekDay(DateTime.Today);
      AsOfDate = m_pnls.LastDate;
    }
    private void btnCombinePnl_Click(object sender, EventArgs e)
    {
      var all = spreadWeightGeneratorCollectionGrid1.ListOfGenerators;

      if (all.Count() == 0) return;

      ConstructGen<double> con = new ConstructGen<double>(all.Count());
      con.ColumnHeadings = new string[con.ArrayLength];

      for (int i = 0; i < con.ArrayLength; ++i)
      {
        var item = all.ElementAt(i);
        con.ColumnHeadings[i] = item.ToString();
        con.SetColumnValues(i, item.GetSimplePnl());
      }

      if (con.NeedsToSortKeys())
        con.SortKeys();

      var eval = new ReturnsEval.DataSeriesEvaluator("Combined", ReturnsEval.DataSeriesType.Returns);
      eval.AddInnerSeries(con.Dates.ToArray(), con.ToArray(), con.ColumnHeadings);

      eval.Display("Combined");
    }
    private void buildData()
    {
      var pxDates = new List<DateTime>();
      var pxValues = new List<double>();

      for (int y = 2003;y <= DateTime.Now.Year; ++y)
      {
        // find the contracts
        var conLon = Long.Underlying.Futures.Where(x => x.Expiry.Year-Long.YearOffset == y && x.Expiry.Month == (int)Long.Month).FirstOrDefault();
        var conShort = Short.Underlying.Futures.Where(x => x.Expiry.Year-Short.YearOffset == y && x.Expiry.Month == (int)Short.Month).FirstOrDefault();

        if (conLon != null && conShort != null)
        {
          m_contractsLongShort.Add(y, new KeyValuePair<ComFutureMeta, ComFutureMeta>(conLon, conShort));

          // last trade of this pair is the earliest lastTrade date of the two
          var lastTrade = (conLon.LastTrade < conShort.LastTrade) ? conLon.LastLastDate : conShort.LastLastDate;
          var dataStart = lastTrade.AddYears(-1);

          if (MyCalendar.IsWeekend(dataStart)) dataStart = MyCalendar.NextWeekDay(dataStart);

          ConstructGen<double> con = new ConstructGen<double>(new string[] { conLon.Ticker, conShort.Ticker, "Diff", "Normalized" });

          con.SetColumnValues((int)dataColumns.Long, conLon.Prices.GetSubValues(dataStart, lastTrade));
          con.SetColumnValues((int)dataColumns.Short, conShort.Prices.GetSubValues(dataStart, lastTrade));

          if (con.NeedsToSortKeys()) con.SortKeys();

          if (con.Dates.Count == 0)
            continue;

          // calculate differences
          foreach (DateTime date in con.Keys)
          {
            double[] d = con.GetValues(date);

            // if we have a value for both contracts on this day
            if (d[(int)dataColumns.Long] != 0d && d[(int)dataColumns.Short] != 0d)
            {
              // save down the difference
              d[(int)dataColumns.Diff] = d[(int)dataColumns.Long] - d[(int)dataColumns.Short];

              if (date.Year == y)
              {
                pxDates.Add(date);
                pxValues.Add(d[2]);
              }
            }
          }

          // normalize differences
          {
            DatedDataCollectionGen<double> diffs = con.GetColumnValuesAsDDC((int)dataColumns.Diff);

            if (diffs==null || diffs.Length == 0)
              continue;

            var min = diffs.Data.Min();
            var max = diffs.Data.Max();

            var normArr = new double[diffs.Length];

            for (int i = 0; i < normArr.Length; ++i)
              normArr[i] = (diffs.Data[i] - min) / (max - min);

            con.SetColumnValues((int)dataColumns.NormalizedDiff, new DatedDataCollectionGen<double>(diffs.Dates, normArr));
          }


          m_yearToPxs.Add(y, con);
        }
      }
      m_hasBuiltData = true;
    }
Exemple #9
0
    public ConstructGen<double> AllProductPrices(bool fillInGapsWithPrevious_ = true)
    {
      // start by generating price series

      ConstructGen<double> prices = new ConstructGen<double>(Products.Select(x => x.Name).ToArray());

      for (int i = 0; i < Products.Count; ++i)
        prices.SetColumnValues(i, Products[i].Prices);

      if (prices.NeedsToSortKeys())
        prices.SortKeys();

      // fill in any missing values (holidays)
      if(fillInGapsWithPrevious_)
      {
        double[] yesterday = null;

        foreach (var date in prices.Dates)
        {
          var today = prices.GetValues(date);

          if (yesterday != null)
          {
            for(int i=0;i<today.Length;++i)
              if (today[i] == 0d)
                today[i] = yesterday[i];
          }

          yesterday = today;
        }
      }

      return prices;
    }
    public void Go()
    {
      var allweights=new ConstructGen<WeightsLine>(Spreads.Length);

      // mark each of the individual spread weight entry/exit points in the construct - could well be different dates per spread...
      for(int i=0;i<Spreads.Length;++i)
      {
        var wts = Spreads[i].GenerateWeights();

        foreach (var wt in wts)
        {
          if (wt.EntryDate <= DateTime.Today) allweights.SetValue(wt.EntryDate, i, wt);
          if (wt.ExitDate <= DateTime.Today) allweights.SetValue(wt.ExitDate, i, wt);
        }
      }

      allweights.SortKeys();


      // on each date, note the positions that are carried over from an earlier trade on the same day, so that we have a
      // full picture of what is in play on that day
      WeightsLine[] prev = null;
      foreach (var date in allweights.Dates)
      {
        var todays = allweights.GetValues(date);
        if (prev != null)
        {
          for (int i = 0; i < todays.Length; ++i)
          {
            if (prev[i] != null && todays[i]==null && date <= prev[i].ExitDate)
              todays[i] = prev[i];
          }
        }

        prev = todays;
      }

      if (allweights.NeedsToSortKeys()) allweights.SortKeys();

      // go through each of the dates to generate a covariance and scale the positions 
      foreach (DateTime date in allweights.Keys)
      {
        var arr = allweights.GetValues(date);

        // build up list of indicies that are live on the current date
        var liveIndicies = new List<int>();

        for (int i = 0; i < arr.Length; ++i)
          if (arr[i] != null && arr[i].ExitDate > date)
            liveIndicies.Add(i);

        if (!liveIndicies.Any()) continue;

        var liveItems = liveIndicies.Select(x => arr[x]).ToArray();

        // for all live items form an array of recent returns over of length 'NumDaysForCovariance'
        var returns = new double[NumDaysForCovariance, liveIndicies.Count()];
        var rawSpreadWeights = new double[liveIndicies.Count()];

        for (int i = 0; i < liveIndicies.Count; ++i)
        {
          var indexReturns = liveItems[i].GetAllSpreadReturns();

          // have prices been updated?
          if (indexReturns.LastDate < date)
            continue;

          int indexOfDate = indexReturns.IndexOfElsePrevious(date);
          --indexOfDate;


          var slice = indexReturns.Data.Slice(indexOfDate - NumDaysForCovariance + 1, NumDaysForCovariance);
          rawSpreadWeights[i] = liveItems[i].SpreadWeight/Statistics.Stdev(slice);
          returns.SetColumn(i, slice);
        }

        // buil the covariance
        var covar = new CovarianceItem(Utils.CalculateCovariance(returns));

        // vol bucketing
        var targetvol = liveItems.Length*0.02;

        // scale the weights
        var newwts = covar.ScaleSeries(rawSpreadWeights, targetvol);

        for (int i = 0; i < newwts.Length; ++i)
          liveItems[i].AddCombineWeight(date, newwts[i]);
      }
    }
    public static void Test()
    {
      const int lookback = 5;
      const WeightGeneratorType genType = WeightGeneratorType.LongestZScoreOverThreshold;

      SpreadWeightGenerator[] arr = new[]
      {
        new SpreadWeightGenerator(
          new WeightGeneratorArgs()
          {
            Lookback = lookback,
            WeightGenerationType = genType,
            MinWindowLength = 50,
            ZScoreThreshold = 1.3d
          },
          new SpreadDefinition(
            new MonthYearOffset(ComIDs.Sugar, 0, MonthCode.V),
            new MonthYearOffset(ComIDs.Sugar, 1, MonthCode.K),
            false)),
        //new SpreadWeightGenerator(
        //  new WeightGeneratorArgs()
        //  {
        //    Lookback = lookback,
        //    WeightGenerationType = genType,
        //    MinWindowLength = 40,
        //    ZScoreThreshold = 1d
        //  },
        //  new SpreadDefinition(
        //    new MonthYearOffset(ComIDs.Sugar, 0, MonthCode.V),
        //    new MonthYearOffset(ComIDs.Sugar, 1, MonthCode.K),
        //    false)),
        //new SpreadWeightGenerator(
        //  new WeightGeneratorArgs()
        //  {
        //    Lookback = lookback,
        //    WeightGenerationType = genType,
        //    MinWindowLength = 40,
        //    ZScoreThreshold = 1.5d
        //  },
        //  new SpreadDefinition(
        //    new MonthYearOffset(ComIDs.NatGas, 0, MonthCode.H),
        //    new MonthYearOffset(ComIDs.NatGas, 0, MonthCode.X),
        //    false)),
        new SpreadWeightGenerator(
          new WeightGeneratorArgs()
          {
            Lookback = lookback,
            WeightGenerationType = genType,
            MinWindowLength = 60,
            ZScoreThreshold = 1.2d
          },
          new SpreadDefinition(
            new MonthYearOffset(ComIDs.NymexGas, 0, MonthCode.H),
            new MonthYearOffset(ComIDs.NymexGas, 0, MonthCode.X),
            false)),
        //new SpreadWeightGenerator(
        //  new WeightGeneratorArgs()
        //  {
        //    Lookback = lookback,
        //    WeightGenerationType = genType,
        //    MinWindowLength = 90,
        //    ZScoreThreshold = 1.7d
        //  },
        //  new SpreadDefinition(
        //    new MonthYearOffset(ComIDs.NatGas, 0, MonthCode.H),
        //    new MonthYearOffset(ComIDs.NatGas, 0, MonthCode.X),
        //    false)),
        //new SpreadWeightGenerator(
        //  new WeightGeneratorArgs()
        //  {
        //    Lookback = lookback,
        //    WeightGenerationType = genType,
        //    MinWindowLength = 40,
        //    ZScoreThreshold = 1.6d
        //  },
        //  new SpreadDefinition(
        //    new MonthYearOffset(ComIDs.Corn, 0, MonthCode.U),
        //    new MonthYearOffset(ComIDs.Corn, 0, MonthCode.Z),
        //    false)),
        new SpreadWeightGenerator(
          new WeightGeneratorArgs()
          {
            Lookback = lookback,
            WeightGenerationType = genType,
            MinWindowLength = 60,
            ZScoreThreshold = 1.3d
          },
          new SpreadDefinition(
            new MonthYearOffset(ComIDs.Corn, 0, MonthCode.U),
            new MonthYearOffset(ComIDs.Corn, 0, MonthCode.Z),
            false)),
        //new SpreadWeightGenerator(
        //  new WeightGeneratorArgs()
        //  {
        //    Lookback = lookback,
        //    WeightGenerationType = genType,
        //    MinWindowLength = 90,
        //    ZScoreThreshold = 0.8d
        //  },
        //  new SpreadDefinition(
        //    new MonthYearOffset(ComIDs.Corn, 0, MonthCode.U),
        //    new MonthYearOffset(ComIDs.Corn, 0, MonthCode.Z),
        //    false)),
        new SpreadWeightGenerator(
          new WeightGeneratorArgs()
          {
            Lookback = lookback,
            WeightGenerationType = genType,
            MinWindowLength = 40,
            ZScoreThreshold = 1.5d
          },
          new SpreadDefinition(
            new MonthYearOffset(ComIDs.Wheat, 0, MonthCode.U),
            new MonthYearOffset(ComIDs.Wheat, 0, MonthCode.Z),
            false)),
        //new SpreadWeightGenerator(
        //  new WeightGeneratorArgs()
        //  {
        //    Lookback = lookback,
        //    WeightGenerationType = genType,
        //    MinWindowLength = 50,
        //    ZScoreThreshold = 1.3d
        //  },
        //  new SpreadDefinition(
        //    new MonthYearOffset(ComIDs.Wheat, 0, MonthCode.U),
        //    new MonthYearOffset(ComIDs.Wheat, 0, MonthCode.Z),
        //    false)),
        //new SpreadWeightGenerator(
        //  new WeightGeneratorArgs()
        //  {
        //    Lookback = lookback,
        //    WeightGenerationType = genType,
        //    MinWindowLength = 70,
        //    ZScoreThreshold = 1.1d
        //  },
        //  new SpreadDefinition(
        //    new MonthYearOffset(ComIDs.Wheat, 0, MonthCode.U),
        //    new MonthYearOffset(ComIDs.Wheat, 0, MonthCode.Z),
        //    false)),
        new SpreadWeightGenerator(
          new WeightGeneratorArgs()
          {
            Lookback = lookback,
            WeightGenerationType = genType,
            MinWindowLength = 50,
            ZScoreThreshold = 1.6d
          },
          new SpreadDefinition(
            new MonthYearOffset(ComIDs.RBOB, 0, MonthCode.J),
            new MonthYearOffset(ComIDs.RBOB, 0, MonthCode.U),
            false)),
      };

      var comb = new SpreadWeightGeneratorCombiner(arr) {NumDaysForCovariance = 42, TargetVol = 0.06};
      comb.Go();

      {
        var combinedPnl = new ConstructGen<double>(arr.Length);

        combinedPnl.ColumnHeadings =
          arr.Select(x => string.Format("{0} / {1} / {2}", x.Spread, x.Args.MinWindowLength, x.Args.ZScoreThreshold))
            .ToArray();

        for (int i = 0; i < arr.Length; ++i)
          combinedPnl.SetColumnValues(i, arr[i].GetCombinedPnl());

        if (combinedPnl.NeedsToSortKeys())
          combinedPnl.SortKeys();


        var eval = new ReturnsEval.DataSeriesEvaluator("Combined", ReturnsEval.DataSeriesType.Returns);
        eval.AddInnerSeries(combinedPnl.Dates.ToArray(), combinedPnl.ToArray(), combinedPnl.ColumnHeadings);
        eval.Display("Combined");

        combinedPnl.SumRows().ToCumulative().DisplayLineChart("combined pnl of scaled weights");
      }

    }
    public void ShowPortfolioPnlProgression()
    {
      var pnl = new ConstructGen<double>(Positions.Select(x=>x.Security).ToArray());

      var flp = new System.Windows.Forms.FlowLayoutPanel();

      var listOfInfraBoxes = new List<Infragistics.Win.Misc.UltraGroupBox>();

      for (int i = 0; i < pnl.ArrayLength; ++i)
      {
        var posPnl = Positions[i].GeneratePnlSinceFix();

        for (int d = 0; d < posPnl.Length; ++d)
        {
          pnl.SetValue(posPnl.Dates[d], i, posPnl.Data[d].Close);
        }

        {
          Infragistics.Win.Misc.UltraGroupBox box = new Infragistics.Win.Misc.UltraGroupBox();
          box.Text = string.Format("{0} {1}", Positions[i].Security, Positions[i].Pnl.ToString("###0.0#;(###0.0#);-"));
          box.Tag = Positions[i].Pnl;
          box.Size = new System.Drawing.Size(250, 250);

          var chart = new SI.Controls.BarDataPointChart();
          chart.SetYAxisFormat("##0.0#");
          chart.Dock = System.Windows.Forms.DockStyle.Fill;
          chart.Create(posPnl);
          box.Controls.Add(chart);
          listOfInfraBoxes.Add(box);
        }
      }

      Infragistics.Win.Misc.UltraGroupBox[] boxArr = listOfInfraBoxes.OrderByDescending(x => (double)x.Tag).ToArray();

      {
        double max = 0d;
        foreach (Infragistics.Win.Misc.UltraGroupBox box in boxArr)
        {
          max = Math.Max(max, ((SI.Controls.BarDataPointChart)box.Controls[0]).YAxisAbsoluteMax);
        }

        foreach (Infragistics.Win.Misc.UltraGroupBox box in boxArr)
        {
          ((SI.Controls.BarDataPointChart)box.Controls[0]).SetMaxMinYAxisRange(max);
        }
      }

      foreach (Infragistics.Win.Misc.UltraGroupBox box in boxArr)
      {
        flp.Controls.Add(box);
      }


      pnl.SortKeys();

      for (int i = 0; i < pnl.ArrayLength; ++i)
      {
        DatedDataCollectionGen<double> col = pnl.GetColumnValuesAsDDC(i);
        double last = col.Data[0];

        for (int j = 1; j < col.Length; ++j)
        {
          double val = col.Data[j];

          if (val == 0d)
          {
            if (last != 0d)
            {
              pnl.SetValue(col.Dates[j], i, last);
            }
          }
          else
            last = val;
        }
      }

      DatedDataCollectionGen<double> total = pnl.SumRows();

      KeyValuePair<string, System.Windows.Forms.Control>[] cons = new KeyValuePair<string, System.Windows.Forms.Control>[3];

      var stack = new Controls.SimpleStackedColumnChart();

      stack.Create<string, string>(
        pnl.Dates.Select(x => x.ToString("HH:mm")).ToArray(),
        Positions.Select(x => x.Security).ToArray(),
        pnl.ToArray());
      cons[0] = new KeyValuePair<string, System.Windows.Forms.Control>("position attributed", stack);


      //stack.DisplayInShowForm(string.Format("{0} pnl progression, position attributed", this.Name));

      var lcdd = new SI.Controls.LineChartDataDisplay();
      lcdd.AddSeries(total.Dates, total.Data, Name, 40, "#0.0#");
      lcdd.SetXAxisFormat("HH:mm");
      //lcdd.DisplayInShowForm(string.Format("{0} total pnl progression", m_p.DisplayName));
      cons[1] = new KeyValuePair<string, Control>("total", lcdd);

      cons[2] = new KeyValuePair<string, Control>("comp", flp);

      cons.DisplayInShowForm(string.Format("{0} pnl progression", Name));

    }
    private SortedDictionary<DateTime, double[]> getRollingVols()
    {
      int[] selIndexes = getSelectedIndixes();

      ConstructGen<double> cum = new ConstructGen<double>(selIndexes.Length);

      DateTime startDate = m_comp.GetDate(m_focus);
      for (int i = 0; i < cum.ArrayLength; ++i)
      {
        ReturnsEval.DataSeriesEvaluator e = m_comp.GetEval(selIndexes[i]);
        if(e.Daily.Dates[0]>startDate)
          e.Evaluate(); 
        e.Daily.GenerateRollingWindowSeries(m_extraArgs.RollingWindowLength);

        for (int j = 0; j < e.Daily.RollingSharpeSeries[m_extraArgs.RollingSeriesType].Dates.Length; ++j)
        {
          DateTime date = e.Daily.RollingSharpeSeries[m_extraArgs.RollingSeriesType].Dates[j];
          if (date < startDate)
            continue;
          double val = e.Daily.RollingSharpeSeries[m_extraArgs.RollingSeriesType].Data[j];

          if(m_extraArgs.RollingSeriesType==SI.ReturnsEval.RollingSeriesType.Vol)
            val /= 100.0;

          cum.SetValue(date, i, val);
        }
      }
      cum.SortKeys();
      return new SortedDictionary<DateTime, double[]>(cum.GetInnerData());
    }
Exemple #14
0
    public static void Go()
    {
      // var is like 'dim'

      // make an array of size 10 - each initalized to the default double value i.e. 0d

      var arr = new double[10];

      // I could 'initialise' the values of this array in one call:

      arr = new double[] {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};

      // an expanding construct is a list

      // make a list that I can only put doubles into

      var list = new List<double>();
      list.Add(1);
      list.Add(2);
      list.Add(3);

      // this is exactly the same as using an intializer:

      list = new List<double> {1, 2, 3};

      // I can use in built stuff to convert this to an array really easily

      double[] arr2 = list.ToArray();

      // dictionaries are lookups or hashmaps with types

      var birthdays = new Dictionary<string, DateTime>();
      birthdays.Add("Ben", new DateTime(1979, 12, 11));
      //or
      birthdays["Jess"] = new DateTime(1985, 1, 19);

      // note, the first method (.Add) will throw an exception if the item already exists, the second method will just overwrite

      // might be better to:
      if (birthdays.ContainsKey("Ben"))
        birthdays.Add("Ben", new DateTime(1979, 12, 11));

      // as we're dealing with time series a lot, I have created some classes that make it easier to work with dates and associated values

      // first of these is DatedDataCollection<T> where T is normally 'double'.

      // these are created with an array of Dates, and an array of 'Ts', which obviously must be of the same length, as the values correspond


      // NOTE: creating array on 1st, 3rd, 5th

      var dtsArray = new DateTime[] {new DateTime(2001, 1, 1), new DateTime(2001, 1, 3), new DateTime(2001, 1, 5)};
      var valuesArray = new double[] {1.21, 1.45, 1.65};

      var ddc = new DatedDataCollectionGen<double>(dtsArray, valuesArray);

      // obviously you wouldn't put normally create ddc like this - it normally gets populated from calls the the database or bbg initially, but we'll 
      // look at that later

      var date4th = new DateTime(2001, 1, 4);

      var value = ddc.ValueOnExactDate(date4th);  // this will fail as I'm insisting on the date being exact and there's nothing for 4th

      var value2 = ddc.ValueOnDate(date4th); // this will give me a value equal to that on the 3rd, since that is value for max date that is before 4th

      var value3 = ddc.ValueOnExactDate_Zero(date4th); // this won't fail but will pass back zero if there isn't an exact date

      // I've extended the classes to make it really easy to plot and see stuff

      ddc.DisplayColumnChart("Values in column chart");
      ddc.DisplayInGrid("values in grid");
      ddc.DisplayLineChart("Line chart");

      // this might be a bit of a leap, but I could very quickly extract EUR values from bloomberg in the following way, and display in a graph

      BbgTalk.HistoryRequester.GetHistory(new DateTime(2001, 1, 1),"EUR Curncy","PX_CLOSE",true)
        .DisplayLineChart("EUR from bbg from 2001");

      // what's this done?

      // BbgTalk.HistoryRequest knows how to connect to bloomberg and pulls out the series as a DatedDataCollection (so long as you're logged into bloomberg)

      DatedDataCollectionGen<double> euroSeries = BbgTalk.HistoryRequester.GetHistory(new DateTime(2001, 1, 1),
        "EUR Curncy", "PX_CLOSE", true);

      // then we displayed in a line chart:

      euroSeries.DisplayLineChart("EUR");

      // what else could we do with this euro series?

      // convert to returns:
      var euroReturns = euroSeries.ToReturns();
      var cumulative = euroReturns.ToCumulative();

      var stdFromMean = euroSeries.ToStdevFromMean(meanWindowLength_: 21, sdWindowLength_: 126);

      // I've also done a load of stuff to transform this series, take a look at HelperMethods.
      
      // often, we don't deal with individual price series, though we need inline data

      // for this I have made something called ConstructGen<T>, where again, T is normally a double

      var firstConstruct = new ConstructGen<double>(9);

      // this has made a construct that is set up to store dated values for 9 different variables

      // it's good to set the columnHeadings on the construct so you know what they refer to

      var headings = new string[] {"AUD", "CAD", "CHF", "EUR", "GBP", "JPY", "NOK", "NZD", "SEK"};
      firstConstruct.ColumnHeadings = headings;

      // (I'll show you how to do this more easily in a bit...

      // let's look at what we can do with construct and how we use it

      DateTime conDate = new DateTime(2014, 1, 1);

      firstConstruct.SetValue(conDate, 0, 100.2);

      // this has set the value for the first column (AUD) on the given Date

      // we get it out by:

      var v1 = firstConstruct.GetValue(conDate, 0);

      // set the second value:

      firstConstruct.SetValue(conDate, 1, 45.6);

      // this has set the value for the given date for 'CAD'

      // we could set all values at once using SetValues rather than SetValue

      firstConstruct.SetValues(conDate, new double[] {1, 2, 3, 4, 5, 6, 7, 8, 9});

      // and we could get them out using:

      double[] allValuesOnDate = firstConstruct.GetValues(conDate);

      // there are lots of methods on Construct<T> to make our life easier when dealing with data

      // we can fill it up randomly using the SetValues, and then just call SortKeys() to ensure teh dates are in order

      firstConstruct.SortKeys();

      // this means that we will be iterate over the dates in order when we go through it

      // e.g.

      foreach (DateTime date in firstConstruct.Dates)
      {
        var datesVAlues = firstConstruct.GetValues(date);

        // here we could process them...
      }

      // there are methods on ConstructGen<T> to make it easy to see what's in it.  e.g.

      firstConstruct.DisplayInGrid("Grid of construct values");
      firstConstruct.DisplayInDataCollectionDisplay("Display each column as a line in a chart");

      // there is also a useful method to get the column of values as a DatedDataCollection<T>

      DatedDataCollectionGen<double> firstColumn = firstConstruct.GetColumnValuesAsDDC(0);

      // this is an expensive operation FYI, so you wouldn't use this iterating over the dates within the ConstructGen<T> , but it is useful

      
      // ok, now, as we have a set universe of ccys, in the way I extract data from the database (prices / weights / carry / etc) I tend to pull
      // out in a standard way, making a ConstructGen<double> with a column for every currency in the universe

      // so, for example, if I wanted the spots from the database from 2013 onwards, I would call this

      SI.Data.FXSpots spotsProvider = new FXSpots();

      ConstructGen<double> spots = spotsProvider.GetData(new DateTime(2013, 1, 1), DateTime.Today);

      // this returns me a ConstructGen<double> with 27 columns, with each row being a date 

      // I can then use the spots values as I wish

      // similarly

      SI.Data.FXForwards1Wk fwdProvider = new FXForwards1Wk();

      ConstructGen<double> fwds = fwdProvider.GetData(new DateTime(2013, 1, 1), DateTime.Today);

      // within these classes, the data is cached, so that I don't call the database again if I don't need to

      // so if I call for the second time:

      var spots2 = spotsProvider.GetData(new DateTime(2013, 1, 1), DateTime.Today);

      // ... this won't have hit the database again, but will get from the cached data

      // but you'll notice that i have to have a reference to spotsProvider to benefit from the cached data.
      // if I was to make the same request from another point in my code, I would have to create a new FXSpots() instance and then call the method on it to get the data

      // it can be useful in this instance to make use of what's known as the 'Singleton' pattern.
      // This basically provides a means of referring to the same instance every time, in this case so that we make use of cached data

      // I have a Singleton<T> wrapper that wraps up a single instance of T so that I know I'm calling methods on the same instance every time

      // so I would usually get spots from the database wrapping FXSpots in this.  like:

      spots = Singleton<FXSpots>.Instance.GetData(new DateTime(2013, 1, 1), DateTime.Today);

      // now I could call the method on Singleton<FXSpots>.Instance from anywhere in my code and I would benefit from the caching

      // I do tend to use most of the classes that retrive from the database Within SI.Data wrapped in a Singleton

      // another example is the class that pulls information about signals

      var signals = Singleton<Signals>.Instance;

      // this is just a list of SI.Data.Signal classes

      

    }
    private void reloadTopRight(IDictionary<string, DataEncapsValue> list)
    {
      if (list.Count == 0 || m_trArgs.Locked)
        return;

      ConstructGen<double> conTopRight = new ConstructGen<double>(list.Count);
      conTopRight.ColumnHeadings = new string[conTopRight.ArrayLength];
      int i = 0;
      foreach (string s in list.Keys)
      {
        conTopRight.ColumnHeadings[i] = s;
        DatedDataCollectionGen<double> coll = list[s].Data;
        double[] series = (cbTopRightCumulative.Checked) ? coll.Data.Cumulative() : coll.Data;
        for (int j = 0; j < series.Length; ++j)
          conTopRight.SetValue(coll.Dates[j], i, series[j]);

        ++i;
      }
      conTopRight.SortKeys();

      StringBuilder titleBuilder=new StringBuilder();
      foreach (string s in list.Values.Select<DataEncapsValue, string>(x => x.Name).Distinct<string>())
        titleBuilder.Append(s).Append(" / ");
      titleBuilder.Length -= 3;
      lbl_TR_title.Text = titleBuilder.ToString();

      dataCollectionDisplay1.Create(new SortedDictionary<DateTime, double[]>(conTopRight.GetInnerData()), conTopRight.ColumnHeadings);
    }
Exemple #16
0
    public static ConstructGen<double> DoBW(ComID[] coms_, ConstructGen<PairTrade> pairTrades_)
    {
      var conRets = new ConstructGen<double>(coms_.Select(x => x.Name).ToArray());

      for (int rebalIndex = 0; rebalIndex < pairTrades_.Dates.Count; ++rebalIndex)
      {
        var rebalDate = pairTrades_.Dates[rebalIndex];
        var tradeEndDate = (rebalIndex < (pairTrades_.Dates.Count - 1) ? pairTrades_.Dates[rebalIndex + 1] : DateTime.Today);

        var pairTradesOnDay = pairTrades_.GetValues(rebalDate);

        for (int i = 0; i < pairTradesOnDay.Length; ++i)
        {
          var pairTrade = pairTradesOnDay[i];

          if (pairTrade == null)
            continue;

          var rets = pairTrade.WeightedBackwardation.GetSubValues(rebalDate.AddDays(1d), tradeEndDate);

          rets = rets.DivideBy(pairTrade.GetStdev(rebalDate, 63) * 3000d);

          conRets.SetColumnValues(i, rets);
        }
      }

      conRets.SortKeys();

      conRets = conRets.ProcessEachCell(x => (double.IsNaN(x) || double.IsInfinity(x) || double.IsNegativeInfinity(x) ? 0d : x));

      return conRets;
    }
    public static void EquityCTAEnviron()
    {
      //var copperIndex = Singleton<ComIDs>.Instance.First(x => x.Name.Equals("Copper")).ArrayIndex;

      //var index =
      //  Singleton<ComIndexPrices>.Instance.GetData(DataConstants.DATA_START, DateTime.Today)
      //    .GetColumnValuesAsDDC(copperIndex);

      //var index = EquityIndexOfSorts();

      //var countries = new[] { "Germany", "US" };
      //var instruments =
      //  Singleton<DBFut_Chains>.Instance.Where(
      //    x => x.AssetClass.Equals("Fixed Income") && countries.Any(y => y.Equals(x.Country))).ToArray();

      var instruments=
        Singleton<DBFut_Chains>.Instance.Where(
          x => x.AssetClass.Equals("Equities")).ToArray();

      foreach (var v in instruments)
      {
        var px = Singleton<DBFut_ChainGenericCache>.Instance.GetGenericSeries(v);
        showCombinedStdevFromMean(px, v.BbgTicker, new[] {21, 42, 63, 89, 100, 126, 150, 189, 252, 512});
      }
      return;

      //var instruments = Singleton<DBFut_Chains>.Instance.Where(x => x.AssetClass.Equals("Equities")).ToArray();

      var con = new ConstructGen<double>(instruments.Select(x => x.BbgTicker).ToArray());

      for(int i=0;i<instruments.Length;++i)
      {
        var index = Singleton<DBFut_ChainGenericCache>.Instance.GetGenericSeries(instruments[i]);
        if (index == null || index.Length == 0) continue;

        var prop = getMADiffPercentiles(index, new PercDiffArgs[]
      {
        //new PercDiffArgs() {MA1 = 21, MA2 = 42, PercWindow = 126},
        //new PercDiffArgs() {MA1 = 21, MA2 = 63, PercWindow = 126},
        //new PercDiffArgs() {MA1 = 21, MA2 = 89, PercWindow = 126},
        new PercDiffArgs() {MA1 = 42, MA2 = 89, PercWindow = 126},
        new PercDiffArgs() {MA1 = 42, MA2 = 126, PercWindow = 126},
        new PercDiffArgs() {MA1 = 63, MA2 = 126, PercWindow = 126},
        new PercDiffArgs() {MA1 = 89, MA2 = 180, PercWindow = 126},
        new PercDiffArgs() {MA1 = 89, MA2 = 252, PercWindow = 126},
        new PercDiffArgs() {MA1 = 126, MA2 = 252, PercWindow = 126},

        //new PercDiffArgs() {MA1 = 21, MA2 = 42, PercWindow = 252},
        //new PercDiffArgs() {MA1 = 21, MA2 = 63, PercWindow = 252},
        //new PercDiffArgs() {MA1 = 21, MA2 = 89, PercWindow = 252},
        new PercDiffArgs() {MA1 = 42, MA2 = 89, PercWindow = 252},
        new PercDiffArgs() {MA1 = 42, MA2 = 126, PercWindow = 252},
        new PercDiffArgs() {MA1 = 63, MA2 = 126, PercWindow = 252},
        new PercDiffArgs() {MA1 = 89, MA2 = 180, PercWindow = 252},
        new PercDiffArgs() {MA1 = 89, MA2 = 252, PercWindow = 252},
        new PercDiffArgs() {MA1 = 126, MA2 = 252, PercWindow = 252},

        new PercDiffArgs() {MA1 = 42, MA2 = 89, PercWindow = 504},
        new PercDiffArgs() {MA1 = 42, MA2 = 126, PercWindow = 504},
        new PercDiffArgs() {MA1 = 63, MA2 = 126, PercWindow = 504},
        new PercDiffArgs() {MA1 = 89, MA2 = 180, PercWindow = 504},
        new PercDiffArgs() {MA1 = 89, MA2 = 252, PercWindow = 504},
        new PercDiffArgs() {MA1 = 126, MA2 = 252, PercWindow = 504},
      });

        //var c = new SI.Controls.LineChartDataDisplay();

        //c.AddSeries(index, "index", 40, "##0.0#");
        //c.AddSeries(dates_: prop.Dates,
        //  values_: prop.Data,
        //  desc_: "prop",
        //  yAxisExtent_: 40,
        //  yLabelFormat_: "##0.0#",
        //  color_: System.Drawing.Color.Orange,
        //  yAxisLeft_: false);

        //c.DisplayInShowForm("blah");

        con.SetColumnValues(i, index.ToReturns().MultiplyBy(prop));
      }
      con.SortKeys();

      var eval = new ReturnsEval.DataSeriesEvaluator("combined", DataSeriesType.Returns);
      eval.AddInnerSeries(con.Dates.ToArray(), con.ToArray(), con.ColumnHeadings);
      eval.Display();

    }
    private static ConstructGen<double> equityFutureComponentPrices()
    {
      var instruments = Singleton<DBFut_Chains>.Instance.Where(x => x.AssetClass.Equals("Equities")).ToArray();
      var con = new ConstructGen<double>(instruments.Select(x => x.BbgTicker).ToArray());

      for (int i = 0; i < instruments.Length; ++i)
      {
        con.SetColumnValues(i, Singleton<DBFut_ChainGenericCache>.Instance.GetGenericSeries(instruments[i]));
      }
      con.SortKeys();
      return con;
    }
    public static void Go()
    {
      var yearOfFile = 2015;

      var ricList = new[] {"ED", "FEI", "FGBL", "FGBM", "FGBS", "TY", "FLG", "FSS", "FGB", "FBTP", "FOAT", "ES", "FDX"};

      var contractMonths = (MonthCode[])Enum.GetValues(typeof(MonthCode));

      var sourceDir = @"E:\futuresData\MarkFiles";
      var markDir = @"e:\futuresData\MarkEllis2";

      var dict = new SortedDictionary<string, DatedDataCollectionGen<double>>();
      var keys = new List<string>();

      foreach (var ricStart in ricList)
      {
        foreach (var contractYear in new[] { 2015, 2016, 2017 })
        {
          foreach (var contractMonth in contractMonths)
          {
            var dates = new List<DateTime>();
            var values = new List<double>();

            foreach (var monthOfFile in new[] { 6, 7, 8 })
            {
              var searchString = string.Format("*{0}-{1}-{2}{3}{4}.csv", yearOfFile, monthOfFile.ToString("00"), ricStart, contractMonth, contractYear - 2010);

              var files = Directory.GetFiles(sourceDir, searchString);

              if (files.Length == 0)
                continue;


              foreach (var item in CsvFile.Read<FuturesIntradaySaver.FuturesLineItem>(files[0]))
              {
                if (item.gmtDate.HasValue && (item.gmtDate.Value.Minute == 32 || item.gmtDate.Value.Minute == 2))
                {
                  item.Date = TimeZoneHelper.ConvertGmtTimeToLondon(item.gmtDate.Value);
                  item.Time = string.Empty;

                  if (item.Date.Hour < 8)
                    continue;

                  if (item.Date.Hour > 17)
                    continue;

                  dates.Add(item.Date);
                  values.Add((item.CloseAsk + item.CloseBid) / 2d);
                }
              }

              //list.ToCsv(string.Join("\\", markDir,
              //  string.Format("{0}-{1}-{2}{3}{4}", yearOfFile, monthOfFile.ToString("00"), UpdateStaticDataForFutures.MAPS[ricStart].Item1, contractMonth, contractYear - 2010)));
            }

            if(dates.Count>0)
            {
              try
              {
                var ticker = string.Format("{0}{1}{2}", UpdateStaticDataForFutures.MAPS[ricStart].Item1, contractMonth, contractYear - 2010);
                keys.Add(ticker);
                dict.Add(ticker, new DatedDataCollectionGen<double>(dates.ToArray(), values.ToArray()));
              }
              catch (Exception ex_)
              {
                Console.WriteLine(ex_.ToString());
              }
            }
          }
        }
      }

      var con = new ConstructGen<double>(keys.ToArray());
      for(int i=0;i<keys.Count;++i)
      {
        con.SetColumnValues(i,dict[keys[i]]);
      }

      if (con.NeedsToSortKeys())
        con.SortKeys();

      con.WriteToCSV(@"e:\futuresData\markDataTo1732.csv");
    }
    protected virtual ConstructGen<double> getData(DateTime startDate_, DateTime endDate_, bool refresh_)
    {
      ConstructGen<double> con = null;

      try
      {
        var ds = Singleton<ConnMngr>.Instance.Execute(queryDBName, getQueryString(startDate_, endDate_));

        if (ConnMngr.HasResult(ds))
        {
          var list = new List<tempRowExtract>();

          foreach (DataRow row in ds.Tables[0].Rows)
          {
            var id = DataRowHelpers.GetInt(row, idColumn);
            var value = DataRowHelpers.GetDouble(row, valueColumn);
            var date = DataRowHelpers.GetDate(row, dateColumn);
            list.Add(new tempRowExtract(id, date, value));
          }

          var colMapping = getConstructIndicies(list);

          con = new ConstructGen<double>(colMapping.Keys.Select(x=>x.ToString()).ToArray());

          foreach (var item in list)
          {
            con.SetValue(item.Date, colMapping[item.ID], item.Value);
          }

          if (con.NeedsToSortKeys())
            con.SortKeys();

          postProcessConstruct(con);
        }
      }
      catch (Exception ex_)
      {
        Logger.Error(string.Format("Error retrieving data (sql={0}). Error:{1}", getQueryString(startDate_, endDate_), ex_.Message), GetType(), ex_);
      }

      return con;
    }
 public ConstructGen<double> GetInnerDailyAsConstruct()
 {
   if (InnerSeries == null) return null;
   if (InnerSeries.Count == 0) return null;
   if (InnerSeries[0].Daily == null) Evaluate();
   ConstructGen<double> ret = new ConstructGen<double>(InnerSeries.Count);
   ret.ColumnHeadings = InnerSeries.Select(x => x.Name).ToArray();
   for (int i = 0; i < InnerSeries.Count; ++i)
     for (int j = 0; j < InnerSeries[i].Daily.Dates.Length; ++j)
       ret.SetValue(InnerSeries[i].Daily.Dates[j], i, InnerSeries[i].Daily.Data[j]);
   ret.SortKeys();
   return ret;
 }
    public ConstructGen<double> AllConstantMaturityPrices()
    {
      if (m_allFuturePrices != null)
        return m_allFuturePrices;

      // get the quarterlyin contracts
      var allIMMs = Underlying.IMM_Contracts();

      // build up the list of prices for all contracts

      ConstructGen<double> subCon;
      {
        var con = new ConstructGen<double>(allIMMs.Select(x => x.SymmetryCode).ToArray());
        {
          for (int i = 0; i < con.ArrayLength; ++i)
            con.SetColumnValues(i, allIMMs[i].GetPrices(marketSnapCode_:MarketSnapCode,quoteSource_:QuoteSourceCode,priceType_:1));

          con.SortKeys();
        }

        var dates = SI.Strategy.RatesSpreads.DateHelper.GetBusinessDates(CurveNames.USD3M);

        subCon = new ConstructGen<double>(con.ColumnHeadings);

        foreach (var date in dates)
          if (con.Dates.Contains(date))
            subCon.SetValues(date, con.GetValues(date));
      }

      // create the construct that will hode the constant maturity prices
      // is NumContracts+1 as first column will be interest rate fixing

      m_allFuturePrices =
        new ConstructGen<double>(
          ExtensionMethods.CreateArrayRep(Underlying.FutureStart, NumContracts+1)
            .Select((x, i) => string.Format("{0}cm{1}", x.ToUpper(), i))
            .ToArray());

      foreach (var date in subCon.Dates)
      {
        // set the fixing
        m_allFuturePrices.SetValue(date, 0, Underlying.FixingInstrmnet.GetPrices().ValueOnDate(date)*100d);

        for (int pointIndex = 0; pointIndex < NumContracts; ++pointIndex)
        {
          var daysForward = Convert.ToDouble(pointIndex + 1)*DaysSpan;
          var forwardDate = date.AddDays(daysForward);

          int beforeIndex=-1, afterIndex=-1;
          for (int i = 0; i < allIMMs.Count-1; ++i)
          {
            if(allIMMs[i].Maturity.Value==forwardDate)
            {
              beforeIndex = i;
              afterIndex = i;
              break;
            }
            else if (allIMMs[i].Maturity.Value < forwardDate && allIMMs[i+1].Maturity.Value > forwardDate)
            {
              beforeIndex = i;
              afterIndex = i + 1;
            }
          }

          // were the indexes of the contract that straddle the forward date found?
          if (beforeIndex >= 0)
          {
            if (beforeIndex == afterIndex)
            {
              m_allFuturePrices.SetValue(date, pointIndex+1, 100d-subCon.GetValue(date, beforeIndex));
            }
            else
            {
              var beforeValue = subCon.GetValue(date, beforeIndex);
              var afterValue = subCon.GetValue(date, afterIndex);

              if (beforeValue == 0d || afterValue == 0d)
                continue;

              var width = allIMMs[afterIndex].Maturity.Value - allIMMs[beforeIndex].Maturity.Value;

              var w1 = forwardDate - allIMMs[beforeIndex].Maturity.Value;

              var propAfter = w1.TotalDays/width.TotalDays;

              var interpValue = (afterValue*propAfter) + (beforeValue*(1d - propAfter));

              m_allFuturePrices.SetValue(date, pointIndex+1, 100d-interpValue);
            }
          }
        }
      }

      return m_allFuturePrices;
    }
    private void reloadBottomLeft(IDictionary<string, DataEncapsValue> list)
    {
      if (list.Count == 0 || m_blArgs.Locked)
        return;

      ConstructGen<double> conBottomLeft = new ConstructGen<double>(list.Count);
      conBottomLeft.ColumnHeadings = new string[conBottomLeft.ArrayLength];
      int i = 0;
      foreach (string s in list.Keys)
      {
        conBottomLeft.ColumnHeadings[i] = s;
        DatedDataCollectionGen<double> coll = list[s].Data;

        DatedDataCollectionGen<double> monthly = coll.ToPeriod(m_blArgs.Period, m_blArgs.Collation);
        for (int j = 0; j < monthly.Length; ++j)
          conBottomLeft.SetValue(monthly.Dates[j], i, monthly.Data[j]);

        ++i;
      }
      conBottomLeft.SortKeys();

      StringBuilder titleBuilder = new StringBuilder();
      foreach (string s in list.Values.Select<DataEncapsValue, string>(x => x.Name).Distinct<string>())
        titleBuilder.Append(s).Append(" / ");
      titleBuilder.Length -= 3;
      lbl_BL_title.Text = titleBuilder.ToString();

      simpleWtsColumnChart2.Create<DateTime, string>(new SortedDictionary<DateTime, double[]>(conBottomLeft.GetInnerData()), conBottomLeft.ColumnHeadings, ExtensionMethods.FormatStringForPeriodicity(m_blArgs.Period));
      //simpleWtsColumnChart2.SetYAxisScaleRange(0d, double.MinValue);
    }
    public static ConstructGen<double> GetInvoiceSpreadsAsConstruct()
    {
      var configs = invoiceSpreadConfigs().ToArray();

      var con = new ConstructGen<double>(
        configs.Select(x => string.Format("{0}_{1}_{2}", x.Future, x.Series + 1, x.Curve.ToString())).ToArray());

      for (int i = 0; i < configs.Length; ++i)
      {
        var spreads = getInvoiceSpreadsCollectionFromMongo(configs[i]);
        con.SetColumnValues(i,
          new DatedDataCollectionGen<double>(spreads.Lines.Select(x => x.Date).ToArray(),
            spreads.Lines.Select(x => x.InvoiceSpread ?? 0d).ToArray()));
      }

      if (con.NeedsToSortKeys())
        con.SortKeys();

      // feed forward missing values
      {
        var values = con.GetValues(con.Dates[0]);

        for (int i = 1; i < con.Dates.Count; ++i)
        {
          var date = con.Dates[i];

          var todayValues = con.GetValues(date);

          for (int j = 0; j < todayValues.Length; ++j)
          {
            if (todayValues[j] == 0d)
              todayValues[j] = values[j];
          }

          values = todayValues;
        }
      }

      return con;
    }
    internal static void Go()
    {
      var listOfTenors = new[]
      {
        "1Y",
        "2Y",
        "3Y",
        "4Y",
        "5Y",
        "6Y",
        "7Y",
        "8Y",
        "9Y",
        "10Y",
        "11Y",
        "12Y",
        "15Y",
        "20Y",
        "25Y",
        "30Y",
        "40Y",
        "1Y1Y",
        "1Y2Y",
        "1Y3Y",
        "1Y4Y",
        "1Y5Y",
        "1Y6Y",
        "1Y7Y",
        "1Y8Y",
        "1Y9Y",
        "1Y10Y",
        "1Y11Y",
        "1Y12Y",
        "1Y15Y",
        "1Y20Y",
        "1Y25Y",
        "1Y30Y",
        "1Y40Y",
        "2Y1Y",
        "2Y2Y",
        "2Y3Y",
        "2Y4Y",
        "2Y5Y",
        "2Y6Y",
        "2Y7Y",
        "2Y8Y",
        "2Y9Y",
        "2Y10Y",
        "2Y11Y",
        "2Y12Y",
        "2Y15Y",
        "2Y20Y",
        "2Y25Y",
        "2Y30Y",
        "2Y40Y",
        "3Y1Y",
        "3Y2Y",
        "3Y3Y",
        "3Y4Y",
        "3Y5Y",
        "3Y6Y",
        "3Y7Y",
        "3Y8Y",
        "3Y9Y",
        "3Y10Y",
        "3Y11Y",
        "3Y12Y",
        "3Y15Y",
        "3Y20Y",
        "3Y25Y",
        "3Y30Y",
        "3Y40Y",
        "4Y1Y",
        "4Y2Y",
        "4Y3Y",
        "4Y4Y",
        "4Y5Y",
        "4Y6Y",
        "4Y7Y",
        "4Y8Y",
        "4Y9Y",
        "4Y10Y",
        "4Y12Y",
        "4Y15Y",
        "4Y20Y",
        "4Y25Y",
        "4Y30Y",
        "4Y40Y",
        "5Y1Y",
        "5Y2Y",
        "5Y3Y",
        "5Y4Y",
        "5Y5Y",
        "5Y6Y",
        "5Y7Y",
        "5Y8Y",
        "5Y9Y",
        "5Y10Y",
        "5Y12Y",
        "5Y15Y",
        "5Y20Y",
        "5Y25Y",
        "5Y30Y",
        "5Y40Y",
        "6Y1Y",
        "6Y2Y",
        "6Y3Y",
        "6Y4Y",
        "6Y5Y",
        "6Y6Y",
        "6Y7Y",
        "6Y8Y",
        "6Y9Y",
        "6Y10Y",
        "6Y12Y",
        "6Y15Y",
        "6Y20Y",
        "6Y25Y",
        "6Y30Y",
        "7Y1Y",
        "7Y2Y",
        "7Y3Y",
        "7Y4Y",
        "7Y5Y",
        "7Y6Y",
        "7Y7Y",
        "7Y8Y",
        "7Y9Y",
        "7Y10Y",
        "7Y11Y",
        "7Y12Y",
        "7Y15Y",
        "7Y20Y",
        "7Y25Y",
        "7Y30Y",
        "8Y1Y",
        "8Y2Y",
        "8Y3Y",
        "8Y4Y",
        "8Y5Y",
        "8Y6Y",
        "8Y7Y",
        "8Y8Y",
        "8Y9Y",
        "8Y10Y",
        "8Y12Y",
        "8Y15Y",
        "8Y20Y",
        "8Y25Y",
        "8Y30Y",
        "9Y1Y",
        "9Y2Y",
        "9Y3Y",
        "9Y4Y",
        "9Y5Y",
        "9Y6Y",
        "9Y7Y",
        "9Y8Y",
        "9Y9Y",
        "9Y10Y",
        "9Y11Y",
        "9Y12Y",
        "9Y15Y",
        "9Y20Y",
        "9Y25Y",
        "9Y30Y",
        "10Y1Y",
        "10Y2Y",
        "10Y3Y",
        "10Y4Y",
        "10Y5Y",
        "10Y10Y",
        "10Y15Y",
        "10Y20Y",
        "10Y30Y",
        "11Y1Y",
        "11Y2Y",
        "11Y3Y",
        "11Y4Y",
        "11Y5Y",
        "11Y6Y",
        "11Y7Y",
        "11Y10Y",
        "11Y15Y",
        "11Y20Y",
        "11Y25Y",
        "11Y30Y",
        "12Y1Y",
        "12Y2Y",
        "12Y3Y",
        "12Y4Y",
        "12Y5Y",
        "12Y8Y",
        "12Y10Y",
        "12Y20Y",
        "12Y25Y",
        "15Y1Y",
        "15Y2Y",
        "15Y3Y",
        "15Y4Y",
        "15Y5Y",
        "15Y6Y",
        "15Y7Y",
        "15Y8Y",
        "15Y9Y",
        "15Y10Y",
        "15Y15Y",
        "20Y5Y",
        "20Y10Y",
        "25Y5Y",
        "30Y10Y",

      };

      ProductList.RegisterMongoClasses();

      foreach (var curve in new[] { CurveNames.USD3M, CurveNames.EUR6M} )
      {
        var con = new ConstructGen<double>(listOfTenors);

        for (int i = 0; i < listOfTenors.Length; ++i)
        {
          var split = listOfTenors[i].Trim('Y').Split('Y');

          int start=0, foward=0;

          if (split.Length == 1)
          {
            foward = int.Parse(split[0]);
          }
          else
          {
            start = int.Parse(split[0]);
            foward = int.Parse(split[1]);
          }

          var series = xYyY.Get(curve, start, foward);

          con.SetColumnValues(i, series.A_Prices);
        }

        if (con.NeedsToSortKeys())
          con.SortKeys();

        con.WriteToCSV(string.Format(@"e:\temp\Mark_Swaps_{0}.csv", curve));
      }
      
    }
    public static void Compare(string indexStart_, string suffix_)
    {
      var closePrices = GetData(indexStart_, suffix_);
      var closePrices2 = GetData2(indexStart_, suffix_);

      var con = new ConstructGen<double>(new string[] { "First", "Second" });

      con.SetColumnValues(0, closePrices);
      con.SetColumnValues(1, closePrices2);

      if (con.NeedsToSortKeys())
        con.SortKeys();

      con.DisplayInGrid("Compare");

    }