Ejemplo n.º 1
0
    public static List<HoldingPeriod> GenerateHoldingPeriods(ConstructGen<double> historicalWeights_, double? trimDistributionsBy_=null)
    {
      var ret = new List<HoldingPeriod>();

      for (int colIndex = 0; colIndex < historicalWeights_.ArrayLength; ++colIndex)
      {
        var colVals = historicalWeights_.GetColumnValuesAsDDC(colIndex);

        if (colVals.Data.SumAbs() == 0d)
          continue;

        Dictionary<WtState, List<double>> dict = new Dictionary<WtState, List<double>>();

        WtState currentState = WtState.None;
        DateTime stateStart = DateTime.MinValue;

        for (int i = 0; i < colVals.Length; ++i)
        {
          var wt = colVals.Data[i];

          WtState newState = (wt > 0d) ? WtState.Long : (wt < 0d) ? WtState.Short : WtState.Flat;

          if (currentState == WtState.None)
          {
            stateStart = colVals.Dates[i];
            currentState = newState;
          }
          else if (newState == currentState)
          {
            // do nothing
          }
          else // we have a new state
          {
            var ts = colVals.Dates[i] - stateStart;

            if (!dict.ContainsKey(currentState)) dict[currentState] = new List<double>();


            dict[currentState].Add(MyCalendar.NumBusinessDaysBetween(stateStart,colVals.Dates[i]));

            currentState = newState;
            stateStart = colVals.Dates[i];
          }
        }

        // commit the last values

        {

        }

        var hp = new HoldingPeriod()
        {
          Ccy=historicalWeights_.ColumnHeadings[colIndex],
          State=currentState,
          Held=MyCalendar.NumBusinessDaysBetween(stateStart,DateTime.Today),
          Wt=colVals.Data.Last()
        };

        foreach (var key in dict.Keys)
        {
          if (trimDistributionsBy_.HasValue && trimDistributionsBy_.Value >0d && trimDistributionsBy_ <1d)
          {
            var series = dict[key].OrderBy(x => x);

            int numberInTail = Convert.ToInt32(series.Count() * trimDistributionsBy_.Value);

            //var subset = series.TakeWhile<double>(
          }

          hp.SetValues(key, dict[key]);


        }

        ret.Add(hp);
      }

      return ret;
    }