public override void DoWeights(ConstructGen<double> signalResults_, ConstructGen<double> fullSignalResults_, ConstructGen<double> wts_, List<ProductBase> products_, ConstructGen<double> filters_)
    {
      for (int i = 0; i < products_.Count; ++i)
      {
        double avg = 0;
        for (int j = 0; j < signalResults_.Dates.Count; ++j)
        {
          if(j<WindowLength)
          {
            wts_.SetValue(signalResults_.Dates[j],i,0);
            continue;
          }

          // if the product is not valid on this date then the weight is zero.
          if (double.IsNaN(filters_.GetValue(signalResults_.Dates[j],i)))
          {
            wts_.SetValue(signalResults_.Dates[j], i, 0.0);
          }
          else
          {
            for (int y = 0; y < WindowLength; ++y)
              avg += signalResults_.GetValue(signalResults_.Dates[j - y], i);
            avg /= WindowLength;

            double val = signalResults_.GetValue(signalResults_.Dates[j], i) / avg;

            wts_.MultValue(signalResults_.Dates[j], i, val, false);
          }
        }
      }
    }
    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 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;
    }
Beispiel #4
0
    protected override ReturnsEval.DataSeriesEvaluator doPnl(TraderArgs args_, ConstructGen<double> wts_)
    {
      var priceReturns =
        args_.AllProductPrices(fillInGapsWithPrevious_: true)
          .ToReturns(args_.Products.Select(x => x.Convention).ToArray());


      var stratReturns = new ConstructGen<double>(priceReturns.ColumnHeadings);

      double[] appliedWeights = null;

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

        var priceReturnsArr = priceReturns.GetValues(date);

        if (appliedWeights != null)
        {
          for (int j = 0; j < priceReturnsArr.Length; ++j)
            stratReturns.SetValue(date, j, appliedWeights[j]*priceReturnsArr[j]);
        }

        if (wts_.Dates.Contains(date))
        {
          appliedWeights = wts_.GetValues(date);
        }
      }

      var eval = new ReturnsEval.DataSeriesEvaluator("Gen pnl from weights", ReturnsEval.DataSeriesType.Returns);
      eval.AddInnerSeries(stratReturns.Dates.ToArray(), stratReturns.ToArray(), stratReturns.ColumnHeadings);

      return eval;
    }
Beispiel #5
0
    public override void DoWeights(ConstructGen<double> signalResults_, ConstructGen<double> fullSignalResults_, ConstructGen<double> wts_, List<ProductBase> products_, ConstructGen<double> filters_)
    {
      foreach (DateTime date in signalResults_.Dates)
      {
        for (int i = 0; i < signalResults_.ArrayLength; ++i)
        {
          double val = signalResults_.GetValue(date, i);

          if (val < 0 && GoShort == false)
            wts_.SetValue(date, i, 0.0);
          else if (val > 0 && GoLong == false)
            wts_.SetValue(date, i, 0.0);
          else
            wts_.MultValue(date, i, val, false);
        }
      }
    }
Beispiel #6
0
    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;
    }
Beispiel #7
0
    public static void GoMulti()
    {
      var data = DataRetriever.GetData(indexStart_: "ES", suffix_: "Index", contractIndex_: 1);

      var listOfEvals = new List<ReturnsEval.DataSeriesEvaluator>();

      foreach (var firstWindow in new[] {5, 10, 15, 20, 25, 50, })
      {
        var indic = new SI.Research.Technicals.MACross(firstWindow, firstWindow * 2);

        var signals = indic.GenerateWeightSeries(data, null);

        for (int i = 0; i < signals.Length; ++i)
          signals.Data[i] = signals.Data[i] < 0d ? -1d : 1d;

        signals = CollapseToChanges(signals);

        if (false)
        {
          var con = new ConstructGen<double>(new[] { "Signal", "CleanPrice" });
          con.SetColumnValues(0, signals);

          foreach (var date in con.Dates)
            con.SetValue(date, 1, data.ValueOnExactDate(date));

          con.DisplayInGrid("changes with price levels");
        }

        //signals.DisplayInGrid("changes");


        var pnl = GeneratePnl(data, signals);

        var eval = new ReturnsEval.DataSeriesEvaluator(pnl.Dates, pnl.Data, string.Format("ES_{0}", firstWindow), ReturnsEval.DataSeriesType.Returns);
        listOfEvals.Add(eval);

      }
      listOfEvals.Display("blah");
    }
Beispiel #8
0
    public override void DoWeights(ConstructGen<double> signalResults_, ConstructGen<double> fullSignalResults_, ConstructGen<double> wts_, List<ProductBase> products_, ConstructGen<double> filters_)
    {
      foreach (DateTime date in signalResults_.Dates)
        for (int i = 0; i < products_.Count; ++i)
        {
          double v = signalResults_.GetValue(date, i);

          v = (v == 0) ? 0.0 : (v < 0) ? -1.0 : 1.0;
          v = (m_reverse) ? -v : v;

          if (m_scaleSignDifferently && v != 0)
          {
            v = (v < 0) ? (v * m_negScale) : (v * m_posScale);

            wts_.MultValue(date, i, v, v < 0);
          }
          else
          {
            wts_.SetValue(date, i, v);
            //wts_.SetToSign(date, i, v);
          }
        }
    }
    protected override ConstructGen<double> getData(DateTime startDate_, DateTime endDate_, bool forceRefresh_)
    {
      ConstructGen<double> con = null;
      var ds = Singleton<ConnMngr>.Instance.Execute(queryDBName, getQueryString(startDate_, endDate_));

      if (ConnMngr.HasResult(ds))
      {
        con = new ConstructGen<double>(Singleton<FXIDs>.Instance.ColumnHeadings);
        foreach (DataRow row in ds.Tables[0].Rows)
        {
          var ccyID = Convert.ToInt32(row[idColumn]);
          var value = Convert.ToDouble(row[valueColumn]);
          var date = Convert.ToDateTime(row[dateColumn]);

          {
            var hour = (int)row["hour"];
            var minute = (int)row["minute"];

            date = date.AddHours(hour).AddMinutes(minute);
          }

          Currency ccy = Singleton<FXIDs>.Instance.GetFromId(ccyID);

          con.SetValue(date, ccy.ArrayIndex, value);
        }
      }
      return con;
    }
Beispiel #10
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

      

    }
Beispiel #11
0
      protected override void handleclick(EventArgs e_)
      {
        try
        {
          DatedDataCollectionGen<double> hist = null;
          hist = m_signal.GetSignalGridProvider().GetHistoryForCcyPair(m_ccy1, m_ccy2);

          if (hist == null) hist = m_signal.GetSignalGridProvider().GetHistoryForCcyPair(m_ccy2, m_ccy1, -1d);

          ConstructGen<double> con = new ConstructGen<double>(Singleton<FXIDs>.Instance.ColumnHeadings);

          for (int i = 0; i < hist.Dates.Length; ++i)
          {
            con.SetValue(hist.Dates[i], m_ccy1.ArrayIndex, hist.Data[i]);
            con.SetValue(hist.Dates[i], m_ccy2.ArrayIndex, -hist.Data[i]);
          }
          var rets = ReturnsFromFXWeights.DoIt_DailyWeights(dailyWts_: con);

          var eval = new ReturnsEval.DataSeriesEvaluator(m_signal.Name, ReturnsEval.DataSeriesType.Returns);
          eval.AddInnerSeries(rets.CombinedPnl.Dates.ToArray(), rets.CombinedPnl.ToArray(), rets.CombinedPnl.ColumnHeadings);
          eval.Evaluate();
          eval.Display(string.Format("{0}: {1}/{2} signal performance",m_signal.Name,m_ccy1,m_ccy2)).Icon = Util.BmpToIcon(Properties.Resources.money);
        }
        catch (Exception ex_)
        {
          Logger.Error(string.Format("Error generating and showing pnl for signal grid ccy pair ({0}/{1})", m_ccy1.Code, m_ccy2.Code), typeof(SignalPnlOverTime), ex_);
        }
      }
      protected override void OnClick(EventArgs e)
      {
        ConstructGen<double> shortBasket = new ConstructGen<double>(m_analyzer.SourceWts.ColumnHeadings);
        ConstructGen<double> longBasket = new ConstructGen<double>(m_analyzer.SourceWts.ColumnHeadings);

        foreach (DateTime date in m_analyzer.SourceWts.Dates)
        {
          double[] fullWts = m_analyzer.SourceWts.GetValues(date);

          shortBasket.EnsureDate(date);
          longBasket.EnsureDate(date);

          for (int i = 0; i < fullWts.Length; ++i)
            if (fullWts[i] > 0d)
              longBasket.SetValue(date, i, fullWts[i]);
            else if (fullWts[i] < 0d)
              shortBasket.SetValue(date, i, fullWts[i]);
        }

        var allResult = ReturnsFromFXWeights.DoIt_DailyWeights(m_analyzer.SourceWts);
        var longREsult = ReturnsFromFXWeights.DoIt_DailyWeights(longBasket);
        var shortResult = ReturnsFromFXWeights.DoIt_DailyWeights(shortBasket);

        List<ReturnsEval.DataSeriesEvaluator> evals = new List<ReturnsEval.DataSeriesEvaluator>();

        //evals.Add(allResult.GetEvaluatorCombinedSpotAndCarry());
        //evals.Last().Name = m_analyzer.Name;

        //evals.Add(longREsult.GetEvaluatorCombinedSpotAndCarry());
        //evals.Last().Name = "LongBasket";

        //evals.Add(shortResult.GetEvaluatorCombinedSpotAndCarry());
        //evals.Last().Name = "ShortBasket";

        evals.Display(string.Format("{0} split into long/short baskets", m_analyzer.Name));
        
      }
    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);
    }
Beispiel #14
0
    public static ConstructGen<double> TransformToCon(DataAroundEvent[] data_, int numberAroundEvents_, DataAroundEventField field_)
    {
      ConstructGen<double> ret = new ConstructGen<double>((numberAroundEvents_ * 2) + 1);
        ret.ColumnHeadings = new string[ret.ArrayLength];
        ret.ColumnHeadings[numberAroundEvents_] = "0";
        for (int i = 1; i <= numberAroundEvents_; ++i)
        {
          ret.ColumnHeadings[numberAroundEvents_ - i] = (-i).ToString();
          ret.ColumnHeadings[numberAroundEvents_ + i] = i.ToString();
        }
        var headings = new List<string>(ret.ColumnHeadings);

        if(data_!=null)
          foreach (var eve in data_.OrderBy(x => x.EventDate))
          {
            // initialise to NaN
            ret.SetValues(eve.EventDate, Utils.GetArrayOfValue<double>(double.NaN, ret.ArrayLength));
            var subData = eve[field_];
            if(subData!=null)
              foreach (var point in subData.Data)
              {
                if (point == null) continue;
                var index = headings.IndexOf((point.Index - data_.First().Offset).ToString());
                ret.SetValue(eve.EventDate, index, point.Value);
              }
          }
      return ret;
    }
Beispiel #15
0
    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));

    }
Beispiel #16
0
    private ConstructGen<double>[] getWeights(TraderArgs args_, List<DateTime> rebalDates_, ConstructGen<double> filter_)
    {
      if (m_resultsWts.Count == 0)
        return null;

      ConstructGen<double>[] allWts = new ConstructGen<double>[args_.WtIndicators.Count];

      // go through all wtIndicators - will normally only be one
      for (int i = 0; i < args_.WtIndicators.Count; ++i)
      {
        ConstructGen<double> all_c,rebal_c;

        int length = args_.Products.Count;

        all_c = new ConstructGen<double>(length);
        rebal_c = new ConstructGen<double>(length);

        for (int j = 0; j < args_.Products.Count; ++j)
        {
          // extract the signal series for this wtIndicator and product
          DatedDataCollectionGen<double> dataForProductAndIndicator = m_resultsWts[args_.Products[j]][i];

          // put the data in the contruct that represents the signal at every point
          for (int y = 0; y < dataForProductAndIndicator.Length; ++y)
            all_c.SetValue(
              dataForProductAndIndicator.Dates[y],
              j,
              dataForProductAndIndicator.Data[y]);

          // put the data in the construct that represents the signal just on rebal days
          // NOTE: is offset by 1 day so not in sample
          foreach (DateTime d in rebalDates_)
          {
            //if (d == new DateTime(2010, 3, 19))
            //  System.Diagnostics.Debugger.Break();
            if (args_.Products[j].IsValid(d))
              rebal_c.SetValue(d, j, dataForProductAndIndicator.ValueOnDate(d, args_.DayOffset));
          }
        }

        // c is now a representation of the signal for all products on all days

        // do we want the product of the weights to determine which products can have positions on different days?
        if (args_.UseWeightsForFilter)
        {
          foreach (DateTime d in rebalDates_)
          {
            for (int ii = 0; ii < rebal_c.ArrayLength; ++ii)
              if (double.IsNaN(rebal_c.GetValue(d, ii)))
                filter_.SetValue(d, ii, double.NaN);
          }
        }

        // multiply it by the filter to get rid of non-eligible days

        rebal_c = multiply(rebal_c, filter_,false);

        //ConstructDisplay.Show(rebal_c, args_.Products.ToArray(), "raw signal * filter");

        // now need to scale the weights based on the weighting scheme supplied on the signal indicator 

        // create a construct which will be the multiple

        foreach (SI.Research.Backtest.Builder.Model.WtScheme transform in args_.WtIndicators[i].TransformList)
        {
          ConstructGen<double> wts = new ConstructGen<double>(length);
          wts.InitialiseToValue(rebalDates_,1d);

          transform.DoWeights(rebal_c, all_c, wts, args_.Products,filter_);

          wts = multiply(wts, filter_, true);

          rebal_c = wts;

          //ConstructDisplay.Show(rebal_c, args_.Products.ToArray(), "weights");
        }

        //ConstructDisplay.Show(rebal_c, new object[] { "wts" }, "transformed weights");

        allWts[i] = rebal_c;
      }

      return allWts;
    }
Beispiel #17
0
    public static ConstructGen<double> getFilters(TraderArgs args_, List<DateTime> rebalDates_, IDictionary<ProductBase,List<DatedDataCollectionGen<double>>> resultsFilter_)
    {
      ConstructGen<double> ret = new ConstructGen<double>(args_.Products.Count);

      if (resultsFilter_==null || resultsFilter_.Count == 0)
      {
        ret.InitialiseToValue(rebalDates_, 1d);
      }
      else
      {
        for (int j = 0; j < args_.Products.Count; ++j)
        {
          DatedDataCollectionGen<double> dataForProductAndFilter = resultsFilter_[args_.Products[j]][0];

          foreach (DateTime date in rebalDates_)
          {
            ret.SetValue(
              date,
              j,
              dataForProductAndFilter.ValueOnDate(date, args_.DayOffset));
          }
        }
      }

      // use this opportunity to count out those products that are not valid on this date
      for (int j = 0; j < args_.Products.Count; ++j)
      {
        foreach (DateTime date in rebalDates_)
        {
          if (args_.Products[j].IsValid(date) == false)
            ret.SetValue(date, j, double.NaN);
        }
      }

      // ConstructDisplay.Show(ret, args_.Products.ToArray(), "Filter series");

      return ret;
    }
Beispiel #18
0
    private ConstructGen<double> agreeSign(ConstructGen<double> a_, ConstructGen<double> b_)
    {
      ConstructGen<double> ret = new ConstructGen<double>(a_.ArrayLength);

      foreach (DateTime d in a_.Dates)
      {
        for (int i = 0; i < ret.ArrayLength; ++i)
        {
          double valA = a_.GetValue(d, i);
          double valB = b_.GetValue(d, i);

          if (double.IsNaN(valA)
            || double.IsNaN(valB)
            || ((valA > 0) && (valB < 0))
            || ((valA < 0) && (valB > 0))
            || ((valA==0) && (valB !=0))
            || ((valB==0) && (valA!=0)))
          {
            ret.SetValue(d, i, 0.0);
          }
          else
          {
            ret.SetValue(d, i, valA + valB);
          }
        }
      }
      return ret;
    }
Beispiel #19
0
    private ConstructGen<double> multiply(ConstructGen<double> a_, ConstructGen<double> b_, bool convertNAN_)
    {
      ConstructGen<double> ret = new ConstructGen<double>(a_.ArrayLength);

      foreach (DateTime d in a_.Dates)
      {
        for (int i = 0; i < ret.ArrayLength; ++i)
        {
          double val = a_.GetValue(d, i) * b_.GetValue(d, i);

          if (double.IsNaN(val) && convertNAN_)
            val = 0.0;

          ret.SetValue(d, i, val);
        }
      }

      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 static void populateIntradayBarData(Event eventObj_, ConstructGen<double> populateThis_)
    {
      foreach (var msg in eventObj_)
      {
        if (msg.MessageType.Equals(Name.GetName("IntradayBarResponse")))
        {
          if (msg.HasElement(RESPONSE_ERROR))
          {
            var error = msg.GetElement(RESPONSE_ERROR);

            if (msg.NumElements == 1)
            {
              Logger.Error("Error getting intraday bar data" + error.GetElementAsString(MESSAGE),
                typeof (HistoryRequester));

              return;
            }
          }

          var barDataArray = msg.GetElement("barData");

          foreach (var barData in barDataArray.Elements)
          {
            if (barData.Name.ToString() == "barTickData")
            {
              int numberOfBars = barData.NumValues;
              for (int pointIndex = 0; pointIndex < numberOfBars; ++pointIndex)
              {
                var fields = barData.GetValueAsElement(pointIndex);
                Datetime time = null;

                if (fields.HasElement("time"))
                  time = fields.GetElementAsDatetime("time");

                if (time == null) continue;

                var dt = time.ToSystemDateTime();

                if (fields.HasElement("open"))
                  populateThis_.SetValue(dt, (int)BarDataField.Open, fields.GetElementAsFloat64("open"));
                if (fields.HasElement("high"))
                  populateThis_.SetValue(dt, (int)BarDataField.High, fields.GetElementAsFloat64("high"));
                if (fields.HasElement("low"))
                  populateThis_.SetValue(dt, (int)BarDataField.Low, fields.GetElementAsFloat64("low"));
                if (fields.HasElement("close"))
                  populateThis_.SetValue(dt, (int)BarDataField.Close, fields.GetElementAsFloat64("close"));
                if (fields.HasElement("volume"))
                  populateThis_.SetValue(dt, (int) BarDataField.Volume,
                    Convert.ToDouble(fields.GetElementAsInt64("volume")));
              }
            }
          }
        }
      }
    }
 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 static ConstructGen<double> GetSmoothCurvesColumnsAreValueDates(DateTime valueDate_, uint curveCount_, BondMarket market_, BondField field_, SI.Data.BondCurves curve_, string close_ = "MLP", string source_ = "MLP")
    {
      var tempDict = new SortedDictionary<DateTime, RateCurve>();

      var date = valueDate_;

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

        if (curve == null) continue;

        tempDict.Add(date, curve);

        date = MyCalendar.PrevWeekDay(date);
      }

      var keys = tempDict.Keys.ToArray();

      var con = new ConstructGen<double>(keys.Select(x => x.ToString("dd MMM")).ToArray());

      for (int i = 0; i < keys.Length; ++i)
      {
        var curve = tempDict[keys[i]];

        foreach (var node in curve.GetNodes())
        {
          var time = valueDate_.AddDays(Convert.ToDouble(node)*365.25).Date;

          con.SetValue(time, i, curve.GetValue(node).Value*100d);
        }
      }

      return con;
    }
    public static ConstructGen<double> GetSumFlow(ConstructGen<double> fxWts_)
    {
      var ret = new ConstructGen<double>(fxWts_.ColumnHeadings);

      for (int i = 0; i < fxWts_.ArrayLength; ++i)
      {
        ret.SetValue(DateTime.Today, i, fxWts_.GetColumnValues(i).ToDiffs().ToAbs().Sum());
      }

      return ret;
    }
Beispiel #25
0
    public static Tuple<ConstructGen<double>, int, DateTime> TransformToCompoChartDataAndShift(DataAroundEvent[] data_, int numberAroundEvents_, DataAroundEventField field_)
    {
        ConstructGen<double> ret = new ConstructGen<double>(numberAroundEvents_  + 1);
        ret.ColumnHeadings = new string[ret.ArrayLength];
        //ret.ColumnHeadings[0] = DateTime.Today.ToString("dd-MMM-yyyy");
        for (int i = 0; i <= numberAroundEvents_; ++i)
        {            
            ret.ColumnHeadings[i] = i.ToString();
        }
        var headings = new List<string>(ret.ColumnHeadings);


        int dayShift=0;
        DateTime closestEvent=DateTime.MinValue;
        if (data_ != null)
        {
            var eventdates = data_.Select(d => d.EventDate).ToArray();
            // first find out any events falls within next 15 days
            var maxAllowEventDate = DateTime.Today.AddDays(numberAroundEvents_);
            var includeEvents = eventdates
                .Where(newEvent => newEvent >= DateTime.Today && newEvent <= maxAllowEventDate);

            
            if (includeEvents.Any())
            {
                closestEvent = includeEvents.OrderBy(e => e).First();
                dayShift = (int)(closestEvent -DateTime.Today).TotalDays;
                int firstDataIndex = numberAroundEvents_ - dayShift;


                foreach (var eve in data_.OrderBy(x => x.EventDate))
                {
                    // initialise to NaN
                    ret.SetValues(eve.EventDate, Utils.GetArrayOfValue<double>(double.NaN, ret.ArrayLength));
                    var subData = eve[field_];
                    if (subData != null)
                        for (int i = 0; i <= numberAroundEvents_; i++)
                        {
                            if (firstDataIndex + i > subData.Data.Length -1) continue;
                            if (subData.Data[firstDataIndex+i] == null) continue;
                            ret.SetValue(eve.EventDate, i, subData.Data[firstDataIndex+i].Value);
                        }
                }
            }

            
        }
        return new Tuple<ConstructGen<double>, int, DateTime>(ret, dayShift, closestEvent);
    }
    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]);
      }
    }
    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);
    }
Beispiel #28
0
    public static void Go_multiMA3Complex_MR(string futureStart_, string suffix_, int contractIndex_)
    {
      //var data = DataRetriever.GetData("ES", "Index", contractIndex_);
      var data = DataRetriever.GetData(futureStart_, suffix_, contractIndex_);
      DataRetriever.ChartData(futureStart_, suffix_, contractIndex_);

      //data = Singleton<SI.Data.FXSpots>.Instance.GetData(new DateTime(2003, 1, 1), DateTime.Today).GetColumnValuesAsDDC(SI.Data.Currency.TWD.ArrayIndex);
      //data = BbgTalk.HistoryRequester.GetHistory(new DateTime(2009, 1, 1), "ES1 Index", "PX_LAST", true);

      var listOfEvals = new List<ReturnsEval.DataSeriesEvaluator>();

      var mas = new int[] {5, 10, 15, 20, 25, 30, 35, 40, 45, 50};

      mas = new int[] {15};

      foreach (var firstWindow in mas )
      {
        var args = new SI.Research.Technicals.MA3ComplexArgs()
        {
          MA1Win = firstWindow,
          MA2Win = firstWindow*2,
          MA3Win = firstWindow*4,
          Long = -1d,
          Long_1MA = -0.5d,
          Long_2MA = 0.5d,
          Short = 1d,
          Short_1MA = 0.5d,
          Short_2MA = -0.5d
        };

        var indic = new SI.Research.Technicals.MA3ComplexIndicator(args);

        var signals = indic.GenerateWeightSeries(data, null);

        signals = CollapseToChanges(signals);

        if (true)
        {
          var con = new ConstructGen<double>(new[] { "Signal", "CleanPrice" });
          con.SetColumnValues(0, signals);

          foreach (var date in con.Dates)
            con.SetValue(date, 1, data.ValueOnExactDate(date));

          con.DisplayInGrid("changes with price levels");
        }

        //signals.DisplayInGrid("changes");


        var pnl = GeneratePnl(data, signals);

        var eval = new ReturnsEval.DataSeriesEvaluator(pnl.Dates, pnl.Data, string.Format("{2}_{0}_{1}", contractIndex_, args.ToString(),futureStart_), ReturnsEval.DataSeriesType.Returns);
        listOfEvals.Add(eval);
        //eval.Display(eval.Name);

        //pnl.ToCumulative().DisplayLineChart("ES");
      }
      listOfEvals.Display("blah");
    }
Beispiel #29
0
    public static DatedDataCollectionGen<double> GetIndicator(IndicType type_)
    {
      DatedDataCollectionGen<double> ret = null;

      switch (type_)
      {
        case IndicType.ATM1WVol_actual:
          ret = Singleton<ATMVolsRank>.Instance.ATM_1W_Avg;
          break;
        case IndicType.ATM1WVols_pr126: 
          ret = Singleton<ATMVolsRank>.Instance.ATM_1W_o6M;
          break;
        case IndicType.ATM1WVols_pr252: 
          ret = Singleton<ATMVolsRank>.Instance.ATM_1W_o1Y; 
          break;
        case IndicType.ATM1WVol_actual_vol21: 
          ret = HelperMethods.GetRollingStdev(Singleton<ATMVolsRank>.Instance.ATM_1W_o1Y, 21); 
          break;

        case IndicType.ATM1MVol_actual:
          ret = Singleton<ATMVolsRank>.Instance.ATM_1M_Avg;
          break;
        case IndicType.ATM1MVols_pr126:
          ret = Singleton<ATMVolsRank>.Instance.ATM_1M_o6M;
          break;
        case IndicType.ATM1MVols_pr252: 
          ret = Singleton<ATMVolsRank>.Instance.ATM_1M_o1Y; 
          break;

        case IndicType.ATMDelivs1MVols_pr252:
          ret = Singleton<ATMVolsRankDelivs>.Instance.ATM_1M_o1Y;
          break;

        case IndicType.ATM_EMs_1MVols_actual_pr252:
          ret = Singleton<ATMVolsRankEMs>.Instance.ATM_1M_o1Y;
          break;

        case IndicType.EWMA_Vol_pr252:
        {
          ret = new SI.Data.FXEWMAVariances(0.94, 252).Values.SumRows()
            .ToPercentileRanked(252);
        }
          break;

        case IndicType.CommodsMultiUniverse:
        {
          ret = Singleton<MultiCommodsIndicator.CommodsUniverseScore_21>.Instance.Pxs;
        }
          break;
        case IndicType.CommodsMultiAgs:
          ret = Singleton<MultiCommodsIndicator.CommodsGrainsScore_21>.Instance.Pxs;
          break;

        case IndicType.CommodsMultiBase:
          ret = Singleton<MultiCommodsIndicator.CommodsBaseScore_21>.Instance.Pxs;
          break;

        case IndicType.CcyMultiUniverse:
          ret = Singleton<MultiCcyIndicator.CurrencyUniverseScore_21>.Instance.Pxs;
          break;

        case IndicType.CcyMultAsiaIncG10:
          ret = Singleton<MultiCcyIndicator.CurrencyAsiaIncG10_21>.Instance.Pxs;
          break;

        case IndicType.CcyMultCEEMEA:
          ret = Singleton<MultiCcyIndicator.CurrencyCEEMEAScore_21>.Instance.Pxs;
          break;

        case IndicType.CcyMultAmericas:
          ret = Singleton<MultiCcyIndicator.CurrencyAmericasScore_21>.Instance.Pxs;
          break;

        case IndicType.CcyMultiG10:
          ret = Singleton<MultiCcyIndicator.CurrencyG10Score_21>.Instance.Pxs;
          break;

        case IndicType.FI_Futures:
          ret = Singleton<Futures.Breakout.FutChainOptArgs>.Instance.Index;
          break;

        case IndicType.CcyMultiEM:
          ret = Singleton<MultiCcyIndicator.CurrencyEMScore_21>.Instance.Pxs;
          break;

        case IndicType.DXY_sc21: ret = Singleton<DXYScore_21>.Instance.Scores; break;

        case IndicType.DXY_sc21_contextMA252:
        {
          var rawScores = (DatedDataCollectionGen<double>)Singleton<DXYScore_21>.Instance.Scores.Clone();

          var pxsSeries = Singleton<DXYScore_21>.Instance.Pxs;
          var maSeries = HelperMethods.GetMA(pxsSeries, 252);

          for (int i = 0; i < maSeries.Length; ++i)
          {
            var date = maSeries.Dates[i];
            var px = pxsSeries.ValueOnExactDate(date);
            var ma = maSeries.Data[i];

            var score = rawScores.ValueOnExactDate(date);

            if (score > 0d && px < ma)
            {
              rawScores.Data[rawScores.IndexOf(date)] = 0d;
            }
            else if (score < 0d && px > ma)
            {
              rawScores.Data[rawScores.IndexOf(date)] = 0d;
            }
          }

          return rawScores;
        }
        case IndicType.DXY_sc42_contextMA252:
          {
            var pxsSeries = Singleton<DXYScore_21>.Instance.Pxs;
            var rawScores = HelperMethods.CalculateStdevFromMean(pxsSeries, 42, 42);
            var maSeries = HelperMethods.GetMA(pxsSeries, 252);

            for (int i = 0; i < maSeries.Length; ++i)
            {
              var date = maSeries.Dates[i];
              var px = pxsSeries.ValueOnExactDate(date);
              var ma = maSeries.Data[i];

              var score = rawScores.ValueOnExactDate(date);

              if (score > 0d && px < ma)
              {
                rawScores.Data[rawScores.IndexOf(date)] = 0d;
              }
              else if (score < 0d && px > ma)
              {
                rawScores.Data[rawScores.IndexOf(date)] = 0d;
              }
            }

            return rawScores;
          }
        case IndicType.DXY_sc63_contextMA252:
          {
            var pxsSeries = Singleton<DXYScore_21>.Instance.Pxs;
            var rawScores = HelperMethods.CalculateStdevFromMean(pxsSeries, 63, 63);
            var maSeries = HelperMethods.GetMA(pxsSeries, 252);

            for (int i = 0; i < maSeries.Length; ++i)
            {
              var date = maSeries.Dates[i];
              var px = pxsSeries.ValueOnExactDate(date);
              var ma = maSeries.Data[i];

              var score = rawScores.ValueOnExactDate(date);

              if (score > 0d && px < ma)
              {
                rawScores.Data[rawScores.IndexOf(date)] = 0d;
              }
              else if (score < 0d && px > ma)
              {
                rawScores.Data[rawScores.IndexOf(date)] = 0d;
              }
            }

            return rawScores;
          }
        //case IndicType.CustomDXY_sc21: ret = Singleton<CurrencyUniverseScore_21>.Instance.Scores; break;
        case IndicType.MoveIndex_pr252:
          ret = Singleton<MOVEScore_21>.Instance.Pxs.ToPercentileRanked(252);
          break;
        case IndicType.MoveIndex_pr100:
          ret = Singleton<MOVEScore_21>.Instance.Pxs.ToPercentileRanked(100);
          break;
        case IndicType.VIX_sc: ret = Singleton<VIXScore_21>.Instance.Scores; break;
        case IndicType.VIX_actual:
          {
            DatedDataCollectionGen<double> dd = BbgTalk.Core.Instance.GetHistory(Environment.UserName, DataConstants.DATA_START, "VIX INDEX", "PX_LAST", true);
            ret = new DatedDataCollectionGen<double>(dd.Dates, dd.Data);
          }
          break;
        //case IndicType.AllVsDow_actual:
        //  {
        //    DatedDataCollectionGen<double> dd = BbgTalk.Core.Instance.GetHistory(Environment.UserName, DataConstants.DATA_START, ".ALLVSDOW INDEX", "PX_LAST", true);
        //    ret = new DatedDataCollectionGen<double>(dd.Dates, dd.Data);
        //  }
        //  break;
        case IndicType.DXY_actual:
          {
            DatedDataCollectionGen<double> dd = BbgTalk.Core.Instance.GetHistory(Environment.UserName, DataConstants.DATA_START, "DXY CURNCY", "PX_LAST", true);
            ret = new DatedDataCollectionGen<double>(dd.Dates, dd.Data);
          }
          break;
        case IndicType.VIX_pr20:
          {
            DatedDataCollectionGen<double> dd = BbgTalk.Core.Instance.GetHistory(Environment.UserName, DataConstants.DATA_START, "VIX INDEX", "PX_LAST", true);
            ret = new DatedDataCollectionGen<double>(dd.Dates, dd.Data).ToPercentileRanked(20);
          }
          break;
        case IndicType.VIX_pr252:
          {
            DatedDataCollectionGen<double> dd = BbgTalk.Core.Instance.GetHistory(Environment.UserName, DataConstants.DATA_START, "VIX INDEX", "PX_LAST", true);
            ret = new DatedDataCollectionGen<double>(dd.Dates, dd.Data).ToPercentileRanked(126);
          }
          break;
        //case IndicType.GoldCopper_pr20:
        //  {
        //    DatedDataCollectionGen<double> dd = BbgTalk.Core.Instance.GetHistory(Environment.UserName, DataConstants.DATA_START, ".GLDCOP INDEX", "PX_LAST", true);
        //    ret = new DatedDataCollectionGen<double>(dd.Dates, dd.Data).ToPercentileRanked(20);
        //  }
        //  break;
        //case IndicType.GoldCopper_pr126:
        //  {
        //    DatedDataCollectionGen<double> dd = BbgTalk.Core.Instance.GetHistory(Environment.UserName, DataConstants.DATA_START, ".GLDCOP INDEX", "PX_LAST", true);
        //    ret = new DatedDataCollectionGen<double>(dd.Dates, dd.Data).ToPercentileRanked(126);
        //  }
        //  break;
        //case IndicType.GoldCopper_pr252:
        //  {
        //    DatedDataCollectionGen<double> dd = BbgTalk.Core.Instance.GetHistory(Environment.UserName, DataConstants.DATA_START, ".GLDCOP INDEX", "PX_LAST", true);
        //    ret = new DatedDataCollectionGen<double>(dd.Dates, dd.Data).ToPercentileRanked(252);
        //  }
        //  break;
        //case IndicType.GoldGoldEqu_pr252:
        //  {
        //    DatedDataCollectionGen<double> dd = BbgTalk.Core.Instance.GetHistory(Environment.UserName, DataConstants.DATA_START, ".GLDGLDEQ INDEX", "PX_LAST", true);
        //    ret = new DatedDataCollectionGen<double>(dd.Dates, dd.Data).ToPercentileRanked(252);
        //  }
        //  break;
        //case IndicType.SPXRUSS_pr20:
        //  {
        //    DatedDataCollectionGen<double> dd = BbgTalk.Core.Instance.GetHistory(Environment.UserName, DataConstants.DATA_START, ".SPXRTY INDEX", "PX_LAST", true);
        //    ret = new DatedDataCollectionGen<double>(dd.Dates, dd.Data).ToPercentileRanked(20);
        //  }
        //  break;
        //case IndicType.SPXRUSS_pr252:
        //  {
        //    DatedDataCollectionGen<double> dd = BbgTalk.Core.Instance.GetHistory(Environment.UserName, DataConstants.DATA_START, ".SPXRTY INDEX", "PX_LAST", true);
        //    ret = new DatedDataCollectionGen<double>(dd.Dates, dd.Data).ToPercentileRanked(252);
        //  }
        //  break;
        //case IndicType.TEDSpread_pr:
        //  {
        //    DatedDataCollectionGen<double> dd = BbgTalk.Core.Instance.GetHistory(Environment.UserName, DataConstants.DATA_START, ".TED INDEX", "PX_LAST", true);
        //    ret = new DatedDataCollectionGen<double>(dd.Dates, dd.Data).ToPercentileRanked(252);
        //  }
        //  break;
        //case IndicType.VIXVDAX_pr252:
        //  {
        //    DatedDataCollectionGen<double> dd = BbgTalk.Core.Instance.GetHistory(Environment.UserName, DataConstants.DATA_START, ".VV INDEX", "PX_LAST", true);
        //    ret = new DatedDataCollectionGen<double>(dd.Dates, dd.Data).ToPercentileRanked(252);
        //  }
        //  break;
        //case IndicType.CVIXVIX_pr252:
        //  {
        //    DatedDataCollectionGen<double> dd = BbgTalk.Core.Instance.GetHistory(Environment.UserName, DataConstants.DATA_START, ".VCV INDEX", "PX_LAST", true);
        //    ret = new DatedDataCollectionGen<double>(dd.Dates, dd.Data).ToPercentileRanked(252);
        //  }
        //  break;
        //case IndicType.LIBORoisSpread_pr20:
        //  {
        //    DatedDataCollectionGen<double> dd = BbgTalk.Core.Instance.GetHistory(Environment.UserName, DataConstants.DATA_START, ".LOISS INDEX", "PX_LAST", true);
        //    ret = new DatedDataCollectionGen<double>(dd.Dates, dd.Data).ToPercentileRanked(20);
        //  }
        //  break;
        //case IndicType.LIBORoisSpread_pr252:
        //  {
        //    DatedDataCollectionGen<double> dd = BbgTalk.Core.Instance.GetHistory(Environment.UserName, DataConstants.DATA_START, ".LOISS INDEX", "PX_LAST", true);
        //    ret = new DatedDataCollectionGen<double>(dd.Dates, dd.Data).ToPercentileRanked(252);
        //  }
        //  break;
        //case IndicType.IGCS_pr252:
        //  {
        //    DatedDataCollectionGen<double> dd = BbgTalk.Core.Instance.GetHistory(Environment.UserName, DataConstants.DATA_START, ".IGCS INDEX", "PX_LAST", true);
        //    ret = new DatedDataCollectionGen<double>(dd.Dates, dd.Data).ToPercentileRanked(252);
        //  }
        //  break;
        //case IndicType.FSR_pr20:
        //  {
        //    DatedDataCollectionGen<double> dd = BbgTalk.Core.Instance.GetHistory(Environment.UserName, DataConstants.DATA_START, ".FSRE INDEX", "PX_LAST", true);
        //    ret = new DatedDataCollectionGen<double>(dd.Dates, dd.Data).ToPercentileRanked(20);
        //  }
        //  break;
        //case IndicType.FSR_pr252:
        //  {
        //    DatedDataCollectionGen<double> dd = BbgTalk.Core.Instance.GetHistory(Environment.UserName, DataConstants.DATA_START, ".FSRE INDEX", "PX_LAST", true);
        //    ret = new DatedDataCollectionGen<double>(dd.Dates, dd.Data).ToPercentileRanked(252);
        //  }
        //  break;
        //case IndicType.VolSlope_pr252:
        //  {
        //    DatedDataCollectionGen<double> dd = BbgTalk.Core.Instance.GetHistory(Environment.UserName, DataConstants.DATA_START, ".VSL INDEX", "PX_LAST", true);
        //    ret = new DatedDataCollectionGen<double>(dd.Dates, dd.Data).ToPercentileRanked(252);
        //  }
        //  break;
        case IndicType.AvgCarry_actual:
          {
            var spots = Singleton<FXSpots>.Instance.GetData(DataConstants.DATA_START, DateTime.Today, true);
            var fwds = Singleton<FXForwards1Wk>.Instance.GetData(DataConstants.DATA_START, DateTime.Today, true);

            Currency[] ccys = Singleton<FXIDs>.Instance.ToArray();
            ConstructGen<double> con = new ConstructGen<double>(ccys.Select(x=>x.Code).ToArray());

            for (int d = 0; d < spots.Dates.Count; ++d)
            {
              DateTime date = spots.Dates[d];
              double ccyCarry = 0d;
              for (int c = 0; c < ccys.Length; ++c)
              {
                if (ccys[c].IsValid(date) == false)
                  continue;

                if (ccys[c].Convention == 1)
                  ccyCarry = ((spots.GetValue(date, c) / fwds.GetValue(date, c)) - 1d) * (252d / 5d);
                else
                  ccyCarry = ((fwds.GetValue(date, c) / spots.GetValue(date, c)) - 1d) * (252d / 5d);

                con.SetValue(date, c, ccyCarry);
              }
            }

            ret = con.AvgRows();

            for (int i = 1; i < ret.Length; ++i)
            {
              if (double.IsInfinity(ret.Data[i]))
                ret.Data[i] = ret.Data[i - 1];

              ret.Data[i] = Math.Max(ret.Data[i], -0.01);
            }
          }
          break;
        //case IndicType.sigNumRebals21:
        //  {
        //    DateTime[] allDates = Singleton<FXSpots>.Instance.GetData(DataConstants.DATA_START, DateTime.Today, true).Dates;
        //    double[] rebalInd = new double[allDates.Length];
        //    for (int i = 0; i < allDates.Length; ++i)
        //      if (sigWeights_.Dates.Contains(allDates[i]))
        //        rebalInd[i] = 1d;

        //    ret = new DatedDataCollectionGen<double>(allDates, rebalInd).ToRollingSum(21);
        //  }
        //  break;
        //case IndicType.sigTurnover21:
        //  {
        //    List<DateTime> allDates = new List<DateTime>(Singleton<FXSpots>.Instance.GetData(DataConstants.DATA_START, DateTime.Today, true).Dates);
        //    double[] tOver = new double[allDates.Count];

        //    double[] prevweights = sigWeights_.GetValues(sigWeights_.Dates[0]);

        //    for (int i = 0; i < sigWeights_.Dates.Count; ++i)
        //    {
        //      DateTime date = sigWeights_.Dates[i];
        //      int index = allDates.IndexOf(date);

        //      if (index == -1)
        //        continue;

        //      double[] todayWts = sigWeights_.GetValues(date);

        //      double totalTurnover = 0d;
        //      for (int j = 0; j < todayWts.Length; ++j)
        //      {
        //        double diff = todayWts[j] - prevweights[j];
        //        if (diff == 0d) continue;
        //        if (prevweights[j] == 0d) continue;
        //        totalTurnover += (Math.Abs(diff) / Math.Abs(prevweights[j]));
        //      }
        //      tOver[index] = totalTurnover;
        //    }
        //    ret = new DatedDataCollectionGen<double>(allDates.ToArray(), tOver).ToRollingSum(21);
        //  }
        //  break;
        //case IndicType.sigReturn21:
        //  {
        //    ret = sigDailyReturns_.ToRollingSum(21);
        //  }
        //  break;
        //case IndicType.sigTOverReturn21:
        //  {
        //    ret = GetIndicator(IndicType.sigReturn21, sigWeights_, sigDailyReturns_).MultiplyBy(GetIndicator(IndicType.sigTurnover21, sigWeights_, sigDailyReturns_));
        //  }
        //  break;
        //case IndicType.sigPnlVsMA100:
        //  {
        //    ret = GetDiffVsMA(sigDailyReturns_, 100);
        //  }
        //  break;
        //case IndicType.sigPnlVsMA50:
        //  {
        //    ret = GetDiffVsMA(sigDailyReturns_, 50);
        //  }
        //  break;
        case IndicType.ccyRankPers21to5:
          {
            ret = getPersistenceOfCurrencyReturns(42, 5, FXGroup.EM);
          }
          break;
        case IndicType.EmVolAvg21_actual:
          {
            ret = getRollingAvgVol(FXGroup.EM, 21);
          }
          break;
        case IndicType.G10VolAvg21_actual:
          {
            ret = getRollingAvgVol(FXGroup.G10, 21);
          }
          break;
        case IndicType.EMVolAvg21_pr126:
          {
            ret = getRollingAvgVol(FXGroup.EM, 21).ToPercentileRanked(126);
          }
          break;
        //case IndicType.G4SwpSprd_actual:
        //  {
        //    ret = Singleton<GXSwapRates>.Instance.G4;
        //  }
        //  break;
        //case IndicType.G4SwpSprd_pr20:
        //  {
        //    ret = Singleton<GXSwapRates>.Instance.G4.ToPercentileRanked(20);
        //  }
        //  break;
        //case IndicType.G4SwpSprd_pr252:
        //  {
        //    ret = Singleton<GXSwapRates>.Instance.G4.ToPercentileRanked(252);
        //  }
        //  break;
        //case IndicType.G10SwpSprd_actual:
        //  {
        //    ret = Singleton<GXSwapRates>.Instance.G10;
        //  }
        //  break;
        //case IndicType.G10SwpSprd_pr20:
        //  {
        //    ret = Singleton<GXSwapRates>.Instance.G10.ToPercentileRanked(20);
        //  }
        //  break;
        //case IndicType.G10SwpSprd_pr126:
        //  {
        //    ret = Singleton<GXSwapRates>.Instance.G10.ToPercentileRanked(126);
        //  }
        //  break;
        //case IndicType.G10SwpSprd_pr252:
        //  {
        //    ret = Singleton<GXSwapRates>.Instance.G10.ToPercentileRanked(252);
        //  }
        //  break;
        case IndicType.RVOL_sc21:
        {
          ret = FXStrat.BacktestHelper.getWE(FXStrat.Strat.RVOL).Value.Daily.ToStdevFromMean(21, 21);
        }
          break;
      }

      return ret;
    }
Beispiel #30
0
    public static void Go_ES_NthContract(int contractIndex_ = 1)
    {
      var data = DataRetriever.GetData("ES", "Index", contractIndex_);

      var args = new SI.Research.Technicals.MA3ComplexArgs()
      {
        MA1Win = 10,
        MA2Win = 20,
        MA3Win = 40,
        Long = -1d,
        Long_1MA = -0.5d,
        Long_2MA = 0.5d,
        Short = 1d,
        Short_1MA = 0.5d,
        Short_2MA = -0.5d
      };

      var indic = new SI.Research.Technicals.MA3ComplexIndicator(args);

      var signals = indic.GenerateWeightSeries(data, null);

      signals = CollapseToChanges(signals);

      {
        var con = new ConstructGen<double>(new[] { "Signal", "CleanPrice" });
        con.SetColumnValues(0, signals);

        foreach (var date in con.Dates)
          con.SetValue(date, 1, data.ValueOnExactDate(date));

        //con.DisplayInGrid("changes with price levels");
      }

      //signals.DisplayInGrid("changes");


      var pnl = GeneratePnl(data, signals);

      var chart = pnl.ToCumulative().DisplayLineChart("ES");
      chart.AddSeries(data, "FuturePrice", 80, "#,##0.0");

      //var percentilRankedVol = HelperMethods.GetRollingStat(data, 48, (x) => Statistics.Stdev(x)).ToPercentileRanked(252);

      //for (int i = 0; i < percentilRankedVol.Length; ++i)
      //  percentilRankedVol.Data[i] = percentilRankedVol.Data[i] < 0.5 ? 0d : 1d;

      //chart.AddSeries(percentilRankedVol, "Rolling vol", 120, "#,##0.0%");

    }