Exemplo n.º 1
0
        public static void SumOfFrame()
        {
            var builder = new FrameBuilder.Columns <int, string>();

            builder.Add("column1", new[] { 1, 2, 3, 4 }.ToOrdinalSeries());
            builder.Add("column2", new[] { 1, 2, 3, 4 }.ToOrdinalSeries());
            builder.Add("column3", new[] { 1, 2, 3, 4 }.ToOrdinalSeries());
            builder.Add("column4", new[] { 1, 2, 3, 4 }.ToOrdinalSeries());
            var df  = builder.Frame;
            var sum = df.Sum().GetAllValues().ToArray();

            Assert.AreEqual(10.0, sum[0].Value);
            Assert.AreEqual(10.0, sum[1].Value);
        }
Exemplo n.º 2
0
        public static void FramesOrderedAfterBuild()
        {
            var builder = new FrameBuilder.Columns <int, string>();

            builder.Add("column1", new[] { 1, 2, 3, 4 }.ToOrdinalSeries());
            builder.Add("column2", new[] { 1, 2, 3, 4 }.ToOrdinalSeries());
            builder.Add("column3", new[] { 1, 2, 3, 4 }.ToOrdinalSeries());
            builder.Add("column4", new[] { 1, 2, 3, 4 }.ToOrdinalSeries());
            var frame = builder.Frame;

            Assert.AreEqual(
                new[] { "column1", "column2", "column3", "column4" },
                frame.ColumnKeys.ToArray()
                );
        }
Exemplo n.º 3
0
        //------------------------------------------------------------------------------------------------
        public Frame<DateTime, string> computeFuturesSpreads()
        //------------------------------------------------------------------------------------------------
        {
            Log_.InfoFormat("Computing spreads...");
            CombinedStatic = SpreadSeriesBuilder.combineFuturesAndBondStatic(BondStatic,FuturesStatic);

            var columns = FuturesPrices.Columns;
            
            FrameBuilder.Columns<DateTime, string> frameblder = new FrameBuilder.Columns<DateTime, string>();

            // Iterate over columns for prices
            foreach (var col in FuturesPrices.ColumnKeys)
            {
                var series = columns[col].As<double>();
                //series = series.Select(kvp => calcFuturesMeasure(SpreadMetric_, col, kvp.Value, kvp.Key));

                var dte_idx = series.Keys.ToList();
                SeriesBuilder<DateTime,double> sb = new SeriesBuilder<DateTime, double>();
                Tuple<DateTime, double> prev = null;

                /* THIS ABSOLUTELY HAS TO BE IN ASCENDING ORDER */
                dte_idx.Sort();

                foreach(var dte in dte_idx)
                {
                    var res         = series.TryGet(dte);
                    if (res.HasValue)
                    {
                        double price = res.Value;
                        
                        var staticRow = CombinedStatic.Rows.TryGet(col).ValueOrDefault;
                        
                        /* This section is to account for deficiencies in our database. */
                        // Quit if we don't have static 
                        if (staticRow == null)
                        {
                            sb.Add(dte, double.NaN);
                            continue;
                        }

                        // Quit if we have a bad price
                        if (price == 0.0)
                        {
                            sb.Add(dte, double.NaN);
                            continue;
                        }

                        // Quit if contract isn't even trading
                        var firstTradeDate = staticRow.GetAs<DateTime>("fut_first_trade_dt");
                        var lastTradeDate = staticRow.GetAs<DateTime>("last_tradeable_dt");

                        if (dte > lastTradeDate || dte < firstTradeDate)
                        {
                            sb.Add(dte, double.NaN);
                            continue;
                        }
                        
                        // Get price for AUD
                        if (Conv_.Country == BondAnalytics.Country.AU)
                        {
                            DateTime maturity = TimeSeriesUtilities.LazySafeTryGetAs(staticRow, "Maturity", DateTime.MinValue);
                            int tenor = (int)Math.Round((maturity - dte).Days / 365.25);
                            price = BondAnalytics.CalcAUDFuturesContractValue(price, tenor);
                        }

                        // ASSUMPTION: Our DB seems to front fill by default. Skip days where no data is present
                        /*if (prev != null && price == prev.Item2)
                        {
                            sb.Add(dte, double.NaN);
                            continue;
                        } */

                        

                        double metric = calcFuturesMeasure(SpreadMetric_, col, price, dte, previous: prev);

                        prev = new Tuple<DateTime, double>(dte, price);
                        sb.Add(dte, metric);
                    }
                    else
                    {
                        sb.Add(dte, double.NaN);
                    }                
                }
                frameblder.Add(col, sb.Series);
            }

            var output = frameblder.Frame;
            

            Log_.InfoFormat("Complete:");
            //output.Print();
            
            return output.SortRowsByKey();
        }
Exemplo n.º 4
0
    public static void FramesOrderedAfterBuild()
    {
      var builder = new FrameBuilder.Columns<int, string>();
      builder.Add("column1", new[] { 1, 2, 3, 4 }.ToOrdinalSeries());
      builder.Add("column2", new[] { 1, 2, 3, 4 }.ToOrdinalSeries());
      builder.Add("column3", new[] { 1, 2, 3, 4 }.ToOrdinalSeries());
      builder.Add("column4", new[] { 1, 2, 3, 4 }.ToOrdinalSeries());
      var frame = builder.Frame;

      Assert.AreEqual(
        new[] { "column1", "column2", "column3", "column4" },
        frame.ColumnKeys.ToArray()
        );
    }