예제 #1
0
    public ConstructGen<double> GetWeights(TraderArgs args_)
    {
      generateRawResults(args_);

      // generate all indicators
      List<DateTime> rebalDates = getRebalDates(args_);

      // save them so we can use them later
      args_.RebalDates = rebalDates;

      // get the filter construct first
      ConstructGen<double> filter = getFilters(args_, rebalDates, m_resultsFilter);

      //ConstructDisplay.Show(filter, args_.Products.ToArray(), "Filter Values");

      // get the wts 
      ConstructGen<double>[] wts = getWeights(args_, rebalDates, filter);

      if (wts.Length > 0 && args_.CombineWeightArgs != null)
      {
        // do we want to scale each signal before combining?
        if (args_.CombineWeightArgs.ScaleBeforeCombine)
          for (int i = 0; i < wts.Length; ++i)
            wts[i] = ScaleWeights(wts[i], args_.ScaleToThisVol, args_);

        // do we want to scale the allocation to each signal?
        if (args_.CombineWeightArgs.ScaleWeightSignalsEnabled)
        {
          double currAlloc = args_.CombineWeightArgs.StratWeightStartAlloc;

          for (int i = 0; i < wts.Length; ++i)
          {
            wts[i] = wts[i].MultiplyBy(currAlloc);

            currAlloc += args_.CombineWeightArgs.StratWeightAllocIncrement;
          }
        }
      }

      // combine the different signals
      ConstructGen<double> c = wts[0];
      if (args_.CombineWeightArgs != null && args_.CombineWeightArgs.CombineMethod == StratCombineMethod.MULT)
      {
        c = wts[0];
        for (int i = 1; i < wts.Length; ++i)
          c = multiply(c, wts[i], false);
      }
      else if (args_.CombineWeightArgs != null && args_.CombineWeightArgs.CombineMethod == StratCombineMethod.AGREE)
      {
        c = wts[0];
        for (int i = 1; i < wts.Length; ++i)
          c = agreeSign(c, wts[i]);
      }
      else if (args_.CombineWeightArgs != null && args_.CombineWeightArgs.CombineMethod == StratCombineMethod.ADD)
      {
        c = wts[0];
        for (int i = 1; i < wts.Length; ++i)
          c = c.Plus(wts[i]);
      }
      else if (args_.CombineWeightArgs != null && args_.CombineWeightArgs.CombineMethod == StratCombineMethod.LIST)
      {
        System.Diagnostics.Debug.Assert(args_.CombineWeightArgs.AllocList != null, "Combine method has been set to 'LIST' but no allocation list has been set");

        System.Diagnostics.Debug.Assert(args_.CombineWeightArgs.AllocList.ArrayLength == wts.Length, "Set of weights to combine doesn't match arraylength of ConstructGen<double> that has been set to express the allocations.");

        // get a list of common dates
        List<DateTime> commondates = new List<DateTime>();
        foreach (DateTime date in wts[0].Dates)
        {
          for (int i = 1; i < wts.Length; ++i)
            if (wts[i].Dates.Contains(date) == false)
              continue;

          commondates.Add(date);
        }

        ConstructGen<double> comb = new ConstructGen<double>(wts[0].ArrayLength);

        for (int i = 0; i < wts.Length; ++i)
        {
          foreach (DateTime date in commondates)
          {
            if(comb.Dates.Contains(date)==false)
              comb.SetValues(date,new double[comb.ArrayLength]);

            double alloc = args_.CombineWeightArgs.AllocList.GetValue(date, i);

            for (int j = 0; j < comb.ArrayLength; ++j)
              comb.AddValue(date, j, alloc * wts[i].GetValue(date, j));
          }
        }

        c = comb;

      }
      else if (args_.CombineWeightArgs == null && wts.Length > 1)
        throw new Exception("Don't know how to combine the signals into one.");
      else if (args_.CombineWeightArgs != null)
        throw new Exception("Strategy combine method not implemented");

      // scale to the target vol
      if (args_.ScaleToVol)
        c = ScaleWeights(c, args_.ScaleToThisVol, args_);

      // if we want to 
      if (args_.RebalFrequency == RebalFreq.Trigger)
        processToTrigger(c, args_, this);


      //ConstructDisplay.Show(c, args_.Products.ToArray(), "final wts");

      c.ColumnHeadings = args_.Products.Select<ProductBase, string>(x => x.ToString()).ToArray<string>();

      return c;
    }