public CorrelationAnalyzer(DatedDataCollectionGen<double> series1_, DatedDataCollectionGen<double> series2_, Period p_, int numOfPeriods_)
    {
      m_series1 = series1_;
      m_series2 = series2_;
      m_period = p_;
      m_numPeriod = numOfPeriods_;

      // find common dates

      List<DateTime> commonDates = new List<DateTime>();
      List<DateTime> stratDates = new List<DateTime>(m_series1.Dates);
      foreach (DateTime date in m_series2.Dates)
        if (stratDates.Contains(date))
          commonDates.Add(date);

      QuickSort.Sort<DateTime>(commonDates);

      List<double> first = new List<double>();
      List<double> second = new List<double>();

      // get the values for each of the common dates

      foreach (DateTime date in commonDates)
      {
        first.Add(m_series1.ValueOnDate(date));
        second.Add(m_series2.ValueOnDate(date));
      }

      m_series1 = new DatedDataCollectionGen<double>(commonDates.ToArray(), first.ToArray());
      m_series2 = new DatedDataCollectionGen<double>(commonDates.ToArray(), second.ToArray());

      recalc();
    }
Example #2
0
    internal override DatedDataCollectionGen<double> GetTrades(DatedDataCollectionGen<double> levels_)
    {
      var ma = HelperMethods.GetMA(levels_, MA);

      var trades = ma.Dates.Select((date, index) => levels_.ValueOnDate(date) > ma.Data[index] ? 1d : -1d).ToArray();

      return new DatedDataCollectionGen<double>(ma.Dates, trades);
    }
    public DatedDataCollectionGen<double> GenerateWeightSeries(DatedDataCollectionGen<double> prices_, double convention_, DateTime? minDate_)
    {
      var periodReturns = getPeriodReturns(prices_, convention_);
      var zScores = getZScores(prices_, convention_);
      var contextMAs = HelperMethods.GetRollingStat(prices_, Context, x => x.Average());

      double lastScore = 0d;

      var dates = new[]
      {
        periodReturns.Dates,
        zScores.Dates,
        contextMAs.Dates
      }.OrderBy(x => x.Length)
        .First();


      var scores = new double[dates.Length];

      for (int i = 1; i < dates.Length; ++i)
      {
        var date = dates[i];

        var zScore = zScores.ValueOnDate(date);
        var ret = periodReturns.ValueOnDate(date);
        var price = prices_.ValueOnDate(date);
        var contextMA = contextMAs.ValueOnDate(date);

        var targetScore = 
          Math.Abs(zScore) < ZScoreThreshold
          ? 0d
          : ret > 0d ? 1d : -1d;

        // matches context?
        if (targetScore == 1)
        {
          targetScore = (price >= contextMA) ? targetScore : 0d;
        }
        else if (targetScore == -1)
        {
          targetScore = (price <= contextMA) ? targetScore : 0d;
        }

        var prevScore = scores[i - 1];

        var shift = Math.Min(Math.Abs(targetScore - prevScore), ScoreShift);

        if (targetScore < prevScore)
          scores[i] = prevScore - shift;
        else
          scores[i] = prevScore + shift;
      }

      return new DatedDataCollectionGen<double>(dates, scores);
    }
Example #4
0
    private static DatedDataCollectionGen<double> combineAllocation(DatedDataCollectionGen<double> a_, DatedDataCollectionGen<double> b_)
    {
      // this should only be used when at the top level, when the allocation will be assumed to be '1' 
      if (a_ == null) return b_;
      if (b_ == null) return a_;

      var distinctDates = a_.Dates.AppendWith(b_.Dates).Distinct().OrderBy(x => x).ToArray();
      var allocs = distinctDates.Select(date => a_.ValueOnDate(date)*b_.ValueOnDate(date)).ToArray();

      return new DatedDataCollectionGen<double>(distinctDates, allocs);
    }
Example #5
0
    public void Create(DatedDataCollectionGen<double> values_, DatedDataCollectionGen<double> zoneLookup_, IList<ZoneDefinition> zones_)
    {
      while (ultraChart.CompositeChart.ChartAreas.Count > 1)
        ultraChart.CompositeChart.ChartAreas.RemoveAt(1);

      ultraChart.Dock = DockStyle.None;
      ultraChart.Anchor=AnchorStyles.Bottom|AnchorStyles.Top|AnchorStyles.Right|AnchorStyles.Left;
      ultraChart.Location=new Point(0,0);
      ultraChart.Size=new Size(Size.Width,Size.Height-40);

      if (flp == null)
      {
        flp = new FlowLayoutPanel();
        flp.Size = new Size(Width, 40);
        flp.Dock = DockStyle.Bottom;
        Controls.Add(flp);
      }

      flp.Controls.Clear();

      ultraChart.CompositeChart.ChartLayers.Clear();
      ultraChart.CompositeChart.Series.Clear();
      ultraChart.CompositeChart.Legends[0].ChartLayers.Clear();
      dt.Rows.Clear();
      dt.Columns.Clear();

      // add columns into datatable
      dt.Columns.Add("Date", typeof(DateTime));
      for (int i = 0; i < zones_.Count; ++i)
      {
        dt.Columns.Add(zones_[i].Name, typeof(double));
        zones_[i].Index = i;
      }

      // add first row
      DataRow row = dt.NewRow();
      row[0] = zoneLookup_.Dates[0];
      dt.Rows.Add(row);

      double prevPrice = values_.ValueOnDate(zoneLookup_.Dates[0]);
      double low = double.MaxValue;
      double high = double.MinValue;

      for (int d = 1; d < zoneLookup_.Length; ++d)
      {
        DateTime date = zoneLookup_.Dates[d];
        DataRow newRow = dt.NewRow();
        newRow[0] = date;
        double px = values_.ValueOnDate(date);
        double score = zoneLookup_.ValueOnDate(date);

        low = Math.Min(low, px);
        high = Math.Max(high, px);

        for (int i = 0; i < zones_.Count; ++i)
        {
          if (zones_[i].Matches(score))
          {
            newRow[i + 1] = px;
            dt.Rows[dt.Rows.Count - 1][i + 1] = prevPrice;
            break;
          }
        }
        prevPrice = px;
        dt.Rows.Add(newRow);
      }
      


      // create the layers for each heading

      for(int j=0;j<zones_.Count;++j)
      {
        string heading = zones_[j].Name;
        NumericTimeSeries nts = new NumericTimeSeries();
        nts.Key = string.Format("Series{0}",heading);
        nts.Label = heading;
        nts.PEs.Add(new PaintElement(zones_[j].LineColour));

        ultraChart.CompositeChart.Series.Add(nts);

        nts.Data.TimeValueColumn = "Date";
        nts.Data.ValueColumn = heading;

        LineChartAppearance lca = new LineChartAppearance();
        ChartLayerAppearance cla = new ChartLayerAppearance();

        cla.AxisXKey = "axis1";
        cla.AxisYKey = "axis2";
        cla.ChartAreaKey = "area1";
        cla.ChartType = ChartType.LineChart;
        lca.EndStyle = m_capStyle;
        lca.StartStyle = m_capStyle;
        lca.NullHandling=NullHandling.DontPlot;
        lca.Thickness = LineThickness;
        cla.ChartTypeAppearance = lca;
        cla.Key = string.Format("chartLayer{0}",j.ToString());
        cla.SeriesList = nts.Key;
        

        ultraChart.CompositeChart.ChartLayers.Add(cla);

        AxisItem ai = ultraChart.CompositeChart.ChartAreas[0].Axes.FromKey("axis2");
        {
          ai.RangeType = AxisRangeType.Custom;
          ai.RangeMin = low;
          ai.RangeMax = high;
        }

        nts.Data.DataSource = dt;
        nts.DataBind();

        System.Windows.Forms.CheckBox cb = new System.Windows.Forms.CheckBox();
        cb.Text = heading;
        cb.AutoSize = true;
        cb.Margin = new Padding(1, 1, 1, 1);
        cb.Tag = j;
        flp.Controls.Add(cb);
        cb.Checked = true;
        cb.CheckedChanged += handleCheckedChanged;
        cb.ForeColor = zones_[j].LineColour;
        cb.BackColor = Color.FromArgb(15,15,15);

        //ultraChart.CompositeChart.ChartLayers.Add(cla);
        ultraChart.CompositeChart.Legends[0].ChartLayers.Add(cla);
      }

      ultraChart.InvalidateLayers();
    }
Example #6
0
    private static DatedDataCollectionGen<double> getPnl(DatedDataCollectionGen<double> prices_,
      DatedDataCollectionGen<double> wts_)
    {
      var dates = new List<DateTime>();
      var returns = new List<double>();

      var instrumentReturns = prices_.ToReturns();

      var currentWt = 0d;

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

        dates.Add(date);
        returns.Add(currentWt*instrumentReturns.Data[i]);

        if (wts_.HasDate(date))
          currentWt = wts_.ValueOnDate(date);
      }

      return new DatedDataCollectionGen<double>(dates.ToArray(), returns.ToArray());
    }
Example #7
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

      

    }
Example #8
0
    public SysTimeSeries MultiplyOut(DatedDataCollectionGen<double> allocation_, int offset_)
    {
      if (!HasData || allocation_==null) return null;

      var con = new ConstructGenGen<DateTime, double>(Data.ColumnHeadings);

      foreach (var date in Data.Keys)
        con.SetValues(date, Data.GetValues(date).MultiplyBy(allocation_.ValueOnDate(date,offset_)));

      return new SysTimeSeries {Data = con};
    }
Example #9
0
    private void btnLoad_Click(object sender, EventArgs e)
    {
      var date = m_args.GetDate();

      if (date == null) return;

      var rollDay = Setups.GetSet(m_args.RollSet).FirstOrDefault(x => x.Rolldate == date.Value.Date);

      if (rollDay == null) return;

      rollDay.ReadFromCSV();

      lblDescription.Text = rollDay.Description;

      simpleWtsColumnChart1.ClearData();
      lineChartDataDisplay1.ClearSeries();

      var series = new DatedDataCollectionGen<double>(rollDay.Lines.Dates,
        cleanData(rollDay.Lines.Data.Select(x => x.SeriesValue).ToArray()));

      //var minutVolume = new DatedDataCollectionGen<double>(setup.Lines.Dates,
      //  setup.Lines.Data.Select(x => x.NewContractVolume + x.PriorContractVolume + x.SpreadVolume).ToArray());

      //var cumulativeVolume = minutVolume.ToCumulative();

      //var hourlyVolume = minutVolume.ToGenPeriod(
      //  periodDateFunction_: x => x,
      //  areSamePeriodFunction_: (x, y) => x.Hour == y.Hour,
      //  collationFunction_: x => x.Sum());

      {

        var priorSeries = new DatedDataCollectionGen<double>(rollDay.Lines.Dates,
          rollDay.Lines.Data.Select(x => x.PriorContractVolume).ToArray()).ToGenPeriod(
            periodDateFunction_: x => x,
            areSamePeriodFunction_: rollDay.FunctionForVolumeBucketing,
            collationFunction_: x => x.Sum());

        var newSeries = new DatedDataCollectionGen<double>(rollDay.Lines.Dates,
          rollDay.Lines.Data.Select(x => x.NewContractVolume).ToArray()).ToGenPeriod(
            periodDateFunction_: x => x,
            areSamePeriodFunction_: rollDay.FunctionForVolumeBucketing,
            collationFunction_: x => x.Sum());

        var spreadSeries = new DatedDataCollectionGen<double>(rollDay.Lines.Dates,
          rollDay.Lines.Data.Select(x => x.SpreadVolume).ToArray()).ToGenPeriod(
            periodDateFunction_: x => x,
            areSamePeriodFunction_: rollDay.FunctionForVolumeBucketing,
            collationFunction_: x => x.Sum());

        var labels = datesRelativeToDate(priorSeries.Dates, priorSeries.LastDate);

        var dict = new Dictionary<string, double[]>();
        for (int i = 0; i < labels.Length; ++i)
        {
          dict.Add(labels[i],
            new[]
            {
              priorSeries.ValueOnDate(priorSeries.Dates[i]),
              newSeries.ValueOnDate(priorSeries.Dates[i]),
              spreadSeries.ValueOnDate(priorSeries.Dates[i])
            });
        }

        //simpleWtsColumnChart1.Create(dict,new[] {"From","To","Spread"});
        simpleWtsColumnChart1.Create(labels, spreadSeries.Data);

        simpleWtsColumnChart1.SetYAxisFormat("#,###");
        simpleWtsColumnChart1.SetXAxisExtent(60);
      }


      var chart = lineChartDataDisplay1.AddSeries(
              xAxisValues_: datesRelativeToDate(series.Dates, series.LastDate),
              values_: series.Data,
              desc_: "Price Action",
              yAxisExtent_: 40,
              yLabelFormat_: "##0.0##",
              color_: Color.White,
              lineThickness_: 3);
      chart.AxisX.TickmarkStyle = Infragistics.UltraChart.Shared.Styles.AxisTickStyle.Smart;
      chart.AxisX.Extent = 60;
      chart.AxisX.TickmarkInterval = 10;
      chart.AxisX.TickmarkPercentage = 0.1;
      chart.AxisX.MajorGridLines.Visible = false;
    }