Example #1
0
        public static ConstructGen<double> getOHLCSeriesFromBbg(DateTime firstDate_, string ticker_, bool insertNan_ = true)
        {
            var ret = new ConstructGen<double>(new string[] { "PX_OPEN", "PX_HIGH", "PX_LOW", "PX_CLOSE_1D" });
            DateTime[] bbgDates = null;
            for (int i = 0; i < ret.ColumnHeadings.Length; ++i)
            {
                DateTime start;
                if (i == 3)
                    start = firstDate_.AddDays(1);
                else
                    start = firstDate_;

                var b = BbgTalk.HistoryRequester.GetHistory(start, ticker_, ret.ColumnHeadings[i], false);

                if (b == null || b.Length == 0)
                {
                    throw new Exception(string.Format("No data retrieved for given ticker '{0}'", ticker_.ToUpper()));
                }
                else
                    b = HelperMethods.ensureAllWeekdays(b, insertNan_);

                if (i == 3)
                {
                    var closeData = bbgDates.Length > b.Dates.Length ? b.Data : b.Data.Skip(1).ToArray();
                    ret.SetColumnValues(i, bbgDates.SkipLast().ToArray(), closeData);
                }
                else
                {
                    ret.SetColumnValues(i, b.Dates, b.Data);
                    bbgDates = b.Dates;
                }
            }
            
            foreach (var d in ret.Dates.OrderByDescending(d => d))
            {
                if (ret.GetValue(d, 3) == 0)
                {
                    ret.RemoveValues(d);
                }
                else break;                
            }

            return ret;
        }
Example #2
0
    public static void processToTrigger(ConstructGen<double> wts_, TraderArgs args_, Trader trader_)
    {
      if (wts_.Dates.Count == 0)
        return;

      if (args_.TriggerArgs == null)
        return;

      List<int> datesIndicesToRemove=new List<int>();
      List<int> varContTopIndices = new List<int>();
      List<int> topWtPos = new List<int>();
      double[] before = wts_.GetValues(wts_.Dates[0]);
      double[] varContBefore = (args_.TriggerArgs.DoVarFlipsTrigger) ? trader_.GetContToVar(before, wts_.Dates[0], args_) : null;
      
      if (args_.TriggerArgs.DoVarFlipsTrigger)
        getTopVarIndicies(varContTopIndices, varContBefore, args_.TriggerArgs.VarFlipsTrigger.ConsiderTopVarNumber);
      if (args_.TriggerArgs.DoTopInSizeTrigger)
        getTopPosSizeIndicies(topWtPos, before, args_.TriggerArgs.TopInSizeTrigger.ConsiderTopInSizeNumber);

      List<DateTime> removeTheseDates = new List<DateTime>();
      int daysSinceRebal = 0;

      List<DateTime> includeTheseDates = new List<DateTime>();
      if (args_.TriggerArgs.AlwayRebalFrequencyEnabled)
      {
        // get the dates of rebal the rebalfrequency implies
        includeTheseDates = GenerateRebalDates(args_.TriggerArgs.AlwaysRebalFrequency, wts_.Dates[0]);
      }

      for (int i = 1; i < wts_.Dates.Count; ++i)
      {
        double[] wtsToday = wts_.GetValues(wts_.Dates[i]);
        bool remove=true;

        ++daysSinceRebal;

        // have we exceeded the maximum number of specified days without a rebal?
        if (remove && args_.TriggerArgs.MaxDaysWithoutRebalEnabled && args_.TriggerArgs.MaxDaysWithoutRebal <= daysSinceRebal)
        {
          remove = false;
        }

        // do we want to always rebal on a particular day of the week
        if (remove && args_.TriggerArgs.AlwaysRebalOnDayOfWeekEnabled && args_.TriggerArgs.AlwayRebalOnThisDayOfWeek == wts_.Dates[i].DayOfWeek)
        {
          remove = false;
        }

        // is the date in the list of dates that we always want to maintain
        if (remove && includeTheseDates.Contains(wts_.Dates[i]))
        {
          remove = false;
        }

        // positions that have changed signs must be greater than NUM_CHANGE_FREQ in order for the day to be kept

        if (remove && (args_.TriggerArgs.ChangeSignEnabled || args_.TriggerArgs.ChangeSignOnProportionEnabled))
        {
          int countChanged = 0;
          for (int j = 0; j < wts_.ArrayLength; ++j)
          {
            // change sign in position means keep
            if (Statistics.AreSameSign(before[j], wtsToday[j]) == false)
              ++countChanged;
          }

          int changeOnThisNumber = int.MaxValue;

          // if have decided to change on number of positions that have changed sign...
          if (args_.TriggerArgs.ChangeSignEnabled)
            changeOnThisNumber = Math.Min(changeOnThisNumber, args_.TriggerArgs.MinNumberOfItemsToChange);

          // if have decided to change on proportion of tradeable products to change sign
          if (args_.TriggerArgs.ChangeSignOnProportionEnabled)
          {
            int validCount = numValidProductsOnDate(wts_.Dates[i], args_);

            int numProp = Convert.ToInt32(Convert.ToDouble(validCount) * args_.TriggerArgs.ChangeSignOnThisProportion);

            changeOnThisNumber = Math.Min(changeOnThisNumber, numProp);
          }

          if (countChanged >= changeOnThisNumber)
            remove = false;
        }

        // proportion of portfolio turnover

        if (remove && args_.TriggerArgs.TurnoverEnabled)
        {
          double sumOfTurnover = 0.0;
          double sumOfPositions = 0.0;

          for (int j = 0; j < wts_.ArrayLength; ++j)
          {
            sumOfTurnover += Math.Abs(wtsToday[j] - (before[j]));
            sumOfPositions += Math.Abs(before[j]);
          }

          double propChanged = sumOfTurnover / sumOfPositions;

          if (propChanged >= args_.TriggerArgs.TurnoverThreshold/* TAG:MA32DaysMin && daysSinceRebal>1*/)
            remove = false;
        }

        // trade the flipped positions but don't re-scale everything else

        if (remove==false &&
          args_.TriggerArgs.DontTopLevelScaleIntraWeek == true &&
          args_.TriggerArgs.AlwaysRebalOnDayOfWeekEnabled && args_.TriggerArgs.AlwayRebalOnThisDayOfWeek != wts_.Dates[i].DayOfWeek)
        {
          double[] newArray = new double[before.Length];

          for (int j = 0; j < wts_.ArrayLength; ++j)
          {
            if (Statistics.AreSameSign(before[j], wtsToday[j]))
              newArray[j] = before[j];
            else
              newArray[j] = wtsToday[j];
          }

          wts_.SetValues(wts_.Dates[i], newArray);
          wtsToday = newArray;
        }

        // var flips
        if (remove &&
          args_.TriggerArgs.DoVarFlipsTrigger)
        {
          int numFlipped = 0;

          foreach (int index in varContTopIndices)
            if (Statistics.AreSameSign(before[index], wtsToday[index]) == false)
              ++numFlipped;

          if (numFlipped >= args_.TriggerArgs.VarFlipsTrigger.NumFlipsTrigger)
            remove = false;
        }

        // top in size flips
        if (remove &&
          args_.TriggerArgs.DoTopInSizeTrigger)
        {
          int numFlipped = 0;

          foreach (int index in topWtPos)
            if (Statistics.AreSameSign(before[index], wtsToday[index]) == false)
              ++numFlipped;

          if (numFlipped >= args_.TriggerArgs.TopInSizeTrigger.NumFlipsTrigger)
            remove = false;
        }

        if (remove)
        {
          removeTheseDates.Add(wts_.Dates[i]);
          ++daysSinceRebal;
        }
        else
        {
          before = wtsToday;
          //varContBefore = (trader_ == null || args_.TriggerArgs.VarFlipsTrigger == null || args_.TriggerArgs.VarFlipsTrigger.Enabled == false) ? null : trader_.GetContToVar(before, wts_.Dates[i], args_);
          daysSinceRebal = 0;
          if (args_.TriggerArgs.DoVarFlipsTrigger)
          {
            varContBefore = trader_.GetContToVar(before, wts_.Dates[i], args_);
            getTopVarIndicies(varContTopIndices, varContBefore, args_.TriggerArgs.VarFlipsTrigger.ConsiderTopVarNumber);
          }

          if (args_.TriggerArgs.DoTopInSizeTrigger)
            getTopPosSizeIndicies(topWtPos, before, args_.TriggerArgs.TopInSizeTrigger.ConsiderTopInSizeNumber);
        }
      }

      foreach (DateTime date in removeTheseDates)
        wts_.RemoveValues(date);
    }
 private void removeDate(ConstructGen<double> con_, DateTime date_)
 {
   if (con_ != null)
     con_.RemoveValues(date_);
 }