Exemple #1
0
        public void SubSamples()
        {
            var series = new Series {
                Name = "name"
            };
            var reference = DateTime.UtcNow.Date;

            series.AddPoint(reference, 1m);
            series.AddPoint(reference.AddDays(1), 2m);
            series.AddPoint(reference.AddDays(2), 3m);
            series.AddPoint(reference.AddDays(3), 4m);
            series.AddPoint(reference.AddDays(4), 5m);

            var sampler = new SeriesSampler(TimeSpan.FromDays(1));

            var sampled = sampler.Sample(series, reference.AddDays(1), reference.AddDays(2));

            Assert.AreEqual(2, sampled.Values.Count);

            Assert.AreEqual(series.Values[1].x, sampled.Values[0].x);
            Assert.AreEqual(series.Values[1].y, sampled.Values[0].y);

            Assert.AreEqual(series.Values[2].x, sampled.Values[1].x);
            Assert.AreEqual(series.Values[2].y, sampled.Values[1].y);
        }
Exemple #2
0
 public void RespectsMostRecentTimeOnDuplicatePoints()
 {
     var series = new Series();
     series.AddPoint(DateTime.Today, 1m);
     series.AddPoint(DateTime.Today, 2m);
     Assert.AreEqual(1, series.Values.Count);
     Assert.AreEqual(2m, series.Values.Single().y);
 }
Exemple #3
0
        public void RespectsMostRecentTimeOnDuplicatePoints()
        {
            var series = new Series();

            series.AddPoint(DateTime.Today, 1m);
            series.AddPoint(DateTime.Today, 2m);
            Assert.AreEqual(1, series.Values.Count);
            Assert.AreEqual(2m, series.Values.Single().y);
        }
        public void DoesNotSampleScatterPlots()
        {
            var scatter = new Series("scatter", SeriesType.Scatter);
            scatter.AddPoint(DateTime.Today, 1m);
            scatter.AddPoint(DateTime.Today, 3m);
            scatter.AddPoint(DateTime.Today.AddSeconds(1), 1.5m);
            scatter.AddPoint(DateTime.Today.AddSeconds(0.5), 1.5m);

            var sampler = new SeriesSampler(TimeSpan.FromMilliseconds(1));
            var sampled = sampler.Sample(scatter, DateTime.Today, DateTime.Today.AddDays(1));
            foreach (var pair in scatter.Values.Zip<ChartPoint, ChartPoint, Tuple<ChartPoint, ChartPoint>>(sampled.Values, Tuple.Create))
            {
                Assert.AreEqual(pair.Item1.x, pair.Item2.x);
                Assert.AreEqual(pair.Item1.y, pair.Item2.y);
            }
        }
 public void PlotNetPosition(Slice slice)
 {
     _longHoldingsCount.AddPoint(slice.Time, OptionTools.GetHoldingQuantity(this, _primarySymbol, true, false));
     _shortHoldingsCount.AddPoint(slice.Time, -OptionTools.GetHoldingQuantity(this, _primarySymbol, false, true));
     _longOpenOrdersCount.AddPoint(slice.Time, OptionTools.GetOpenOrderQuantity(this, _primarySymbol, true, false));
     _shortOpenOrdersCount.AddPoint(slice.Time, -OptionTools.GetOpenOrderQuantity(this, _primarySymbol, false, true));
 }
Exemple #6
0
        public void DoesNotSampleScatterPlots()
        {
            var scatter = new Series("scatter", SeriesType.Scatter);

            scatter.AddPoint(DateTime.Today, 1m);
            scatter.AddPoint(DateTime.Today, 3m);
            scatter.AddPoint(DateTime.Today.AddSeconds(1), 1.5m);
            scatter.AddPoint(DateTime.Today.AddSeconds(0.5), 1.5m);

            var sampler = new SeriesSampler(TimeSpan.FromMilliseconds(1));
            var sampled = sampler.Sample(scatter, DateTime.Today, DateTime.Today.AddDays(1));

            foreach (var pair in scatter.Values.Zip(sampled.Values, Tuple.Create))
            {
                Assert.AreEqual(pair.Item1.x, pair.Item2.x);
                Assert.AreEqual(pair.Item1.y, pair.Item2.y);
            }
        }
Exemple #7
0
        /// <summary>
        /// Performs processing in sync with the algorithm's time loop to provide consisten reading of data
        /// </summary>
        public virtual void ProcessSynchronousEvents()
        {
            if (_isNotFrameworkAlgorithm)
            {
                return;
            }

            if (Algorithm.UtcTime.Date > _lastAlphaCountSampleDateUtc)
            {
                _lastAlphaCountSampleDateUtc = Algorithm.UtcTime.Date;

                // populate charts with the daily alpha counts per symbol, resetting our storage
                var sumPredictions = PopulateChartWithSeriesPerSymbol(_dailyAlphaCountPerSymbol, _dailyAlphaCountPerSymbolChart, SeriesType.StackedArea);
                _dailyAlphaCountPerSymbol.Clear();

                // add sum of daily alpha counts to the total alpha count series
                _totalAlphaCountSeries.AddPoint(Algorithm.UtcTime.Date, sumPredictions, LiveMode);

                // populate charts with the total alpha counts per symbol, no need to reset
                PopulateChartWithSeriesPerSymbol(_alphaCountPerSymbol, _totalAlphaCountPerSymbolChart, SeriesType.Pie);
            }

            // before updating scores, emit chart points for the previous sample period
            if (Algorithm.UtcTime >= _nextChartSampleAlgorithmTimeUtc)
            {
                try
                {
                    // verify these scores have been computed before taking the first sample
                    if (StatisticsUpdater.RollingAverageIsReady)
                    {
                        // sample the rolling averaged population scores
                        foreach (var scoreType in ScoreTypes)
                        {
                            var score = 100 * RuntimeStatistics.RollingAveragedPopulationScore.GetScore(scoreType);
                            _alphaScoreSeriesByScoreType[scoreType].AddPoint(Algorithm.UtcTime, (decimal)score, LiveMode);
                        }
                        _nextChartSampleAlgorithmTimeUtc = Algorithm.UtcTime + ChartUpdateInterval;
                    }
                }
                catch (Exception err)
                {
                    Log.Error(err);
                }
            }

            try
            {
                // update scores in line with the algo thread to ensure a consistent read of security data
                // this will manage marking alphas as closed as well as performing score updates
                AlphaManager.UpdateScores();
            }
            catch (Exception err)
            {
                Log.Error(err);
            }
        }
Exemple #8
0
        public void SubSamples()
        {
            var series = new Series {Name = "name"};
            var reference = DateTime.UtcNow.Date;
            series.AddPoint(reference, 1m);
            series.AddPoint(reference.AddDays(1), 2m);
            series.AddPoint(reference.AddDays(2), 3m);
            series.AddPoint(reference.AddDays(3), 4m);
            series.AddPoint(reference.AddDays(4), 5m);

            var sampler = new SeriesSampler(TimeSpan.FromDays(1));

            var sampled = sampler.Sample(series, reference.AddDays(1), reference.AddDays(2));
            Assert.AreEqual(2, sampled.Values.Count);

            Assert.AreEqual(series.Values[1].x, sampled.Values[0].x);
            Assert.AreEqual(series.Values[1].y, sampled.Values[0].y);

            Assert.AreEqual(series.Values[2].x, sampled.Values[1].x);
            Assert.AreEqual(series.Values[2].y, sampled.Values[1].y);
        }
Exemple #9
0
        /// <summary>
        /// Invokes the manager at the end of the time step.
        /// Samples and plots insight counts and population score.
        /// </summary>
        /// <param name="frontierTimeUtc">The current frontier time utc</param>
        public void Step(DateTime frontierTimeUtc)
        {
            // Only add our charts to the algorithm when we actually have an insight
            // We will still update our internal charts anyways, but this keeps Alpha charts out of
            // algorithms that don't use the framework.
            if (!_chartsAdded && _dailyInsightCount > 0)
            {
                _algorithm.AddChart(_insightScoreChart);
                _algorithm.AddChart(_totalInsightCountChart);
                _algorithm.AddChart(_totalInsightCountPerSymbolChart);

                _chartsAdded = true;
            }

            // sample insight/symbol counts each utc day change
            if (frontierTimeUtc.Date > _lastInsightCountSampleDateUtc)
            {
                _lastInsightCountSampleDateUtc = frontierTimeUtc.Date;

                // add sum of daily insight counts to the total insight count series
                _totalInsightCountSeries.AddPoint(frontierTimeUtc.Date, _dailyInsightCount);

                // Create the pie chart every minute or so
                PopulateChartWithSeriesPerSymbol(_totalInsightCountPerSymbol, _totalInsightCountPerSymbolChart, SeriesType.Treemap, frontierTimeUtc);

                // Resetting our storage
                _dailyInsightCount = 0;
            }

            // sample average population scores
            if (frontierTimeUtc >= _nextChartSampleAlgorithmTimeUtc)
            {
                try
                {
                    // verify these scores have been computed before taking the first sample
                    if (_statisticsManager.RollingAverageIsReady)
                    {
                        // sample the rolling averaged population scores
                        foreach (var scoreType in InsightManager.ScoreTypes)
                        {
                            var score = 100 * _statisticsManager.Statistics.RollingAveragedPopulationScore.GetScore(scoreType);
                            _insightScoreSeriesByScoreType[scoreType].AddPoint(frontierTimeUtc, score.SafeDecimalCast());
                        }
                        _nextChartSampleAlgorithmTimeUtc = frontierTimeUtc + SampleInterval;
                    }
                }
                catch (Exception err)
                {
                    Log.Error(err);
                }
            }
        }
Exemple #10
0
        public void ReturnsIdentityOnSinglePoint()
        {
            var series = new Series {Name = "name"};
            var reference = DateTime.Now.ToUniversalTime();
            series.AddPoint(reference, 1m);

            var sampler = new SeriesSampler(TimeSpan.FromDays(1));

            var sampled = sampler.Sample(series, reference.AddSeconds(-1), reference.AddSeconds(1));
            Assert.AreEqual(1, sampled.Values.Count);
            Assert.AreEqual(series.Values[0].x, sampled.Values[0].x);
            Assert.AreEqual(series.Values[0].y, sampled.Values[0].y);
        }
Exemple #11
0
        public void SerializeDeserializeReturnsSameValue()
        {
            var date   = new DateTime(2050, 1, 1, 1, 1, 1);
            var series = new Series("Pepito Grillo", SeriesType.Bar, "%", Color.Blue, ScatterMarkerSymbol.Diamond);

            series.AddPoint(date, 1);
            series.AddPoint(date.AddSeconds(1), 2);

            var serializedSeries = JsonConvert.SerializeObject(series);
            var result           = (Series)JsonConvert.DeserializeObject(serializedSeries, typeof(Series));

            Assert.AreEqual(series.Values.Count, result.Values.Count);
            for (var i = 0; i < series.Values.Count; i++)
            {
                Assert.AreEqual(series.Values[i].x, result.Values[i].x);
                Assert.AreEqual(series.Values[i].y, result.Values[i].y);
            }
            Assert.AreEqual(series.Name, result.Name);
            Assert.AreEqual(series.Unit, result.Unit);
            Assert.AreEqual(series.SeriesType, result.SeriesType);
            Assert.AreEqual(series.Color.ToArgb(), result.Color.ToArgb());
            Assert.AreEqual(series.ScatterMarkerSymbol, result.ScatterMarkerSymbol);
        }
Exemple #12
0
        public void SerializedPieWillOnlyHaveOneValue()
        {
            var date   = new DateTime(2050, 1, 1, 1, 1, 1);
            var date2  = date.AddSeconds(1);
            var series = new Series("Pepito Grillo", SeriesType.Pie, "$", Color.Empty, ScatterMarkerSymbol.Diamond);

            series.AddPoint(date, 1);
            series.AddPoint(date2, 2);

            var serializedSeries = JsonConvert.SerializeObject(series);
            var result           = (Series)JsonConvert.DeserializeObject(serializedSeries, typeof(Series));

            var expectedX = Convert.ToInt64(Time.DateTimeToUnixTimeStamp(date2.ToUniversalTime())); // expect last dateTime (date2)

            Assert.AreEqual(1, result.Values.Count);                                                // expect only one value
            Assert.AreEqual(expectedX, result.Values[0].x);
            Assert.AreEqual(3, result.Values[0].y);                                                 // expect sum of values (1 + 2)
            Assert.AreEqual(series.Name, result.Name);
            Assert.AreEqual(series.Unit, result.Unit);
            Assert.AreEqual(series.SeriesType, result.SeriesType);
            Assert.AreEqual(series.Color.ToArgb(), result.Color.ToArgb());
            Assert.AreEqual(series.ScatterMarkerSymbol, result.ScatterMarkerSymbol);
        }
Exemple #13
0
        public void EmitsEmptySeriesWithSinglePointOutsideOfStartStop()
        {
            var series = new Series {
                Name = "name"
            };
            var reference = DateTime.Now;

            series.AddPoint(reference.AddSeconds(-1), 1m);

            var sampler = new SeriesSampler(TimeSpan.FromDays(1));

            var sampled = sampler.Sample(series, reference, reference);

            Assert.AreEqual(0, sampled.Values.Count);
        }
Exemple #14
0
        public void ReturnsIdentityOnSinglePoint()
        {
            var series = new Series {
                Name = "name"
            };
            var reference = DateTime.Now.ToUniversalTime();

            series.AddPoint(reference, 1m);

            var sampler = new SeriesSampler(TimeSpan.FromDays(1));

            var sampled = sampler.Sample(series, reference.AddSeconds(-1), reference.AddSeconds(1));

            Assert.AreEqual(1, sampled.Values.Count);
            Assert.AreEqual(series.Values[0].x, sampled.Values[0].x);
            Assert.AreEqual(series.Values[0].y, sampled.Values[0].y);
        }
        void HandleAction(Data.Market.TradeBar bar)
        {
            if (_lastSlice != null)
            {
                OptionChain chain;
                int         calls = 0; int puts = 0;
                if (_lastSlice.OptionChains.TryGetValue(_option.Symbol.Value, out chain))
                {
                    calls = _lastSlice.OptionChains[_option.Symbol.Value].Select((o) => o.Right == OptionRight.Call).Count();
                    puts  = _lastSlice.OptionChains[_option.Symbol.Value].Select((o) => o.Right == OptionRight.Put).Count();
                }

                _callCount.AddPoint(_lastSlice.Time, calls);
                _putCount.AddPoint(_lastSlice.Time, puts);

                Console.WriteLine("{0}\t{1}, {2}", _lastSlice.Time, calls, puts);
            }
        }
Exemple #16
0
        /// <summary>
        /// Invokes the manager at the end of the time step.
        /// Samples and plots insight counts and population score.
        /// </summary>
        /// <param name="frontierTimeUtc">The current frontier time utc</param>
        public void Step(DateTime frontierTimeUtc)
        {
            // sample insight/symbol counts each utc day change
            if (frontierTimeUtc.Date > _lastInsightCountSampleDateUtc)
            {
                _lastInsightCountSampleDateUtc = frontierTimeUtc.Date;

                // populate charts with the daily insight counts per symbol
                var sumInsights = PopulateChartWithSeriesPerSymbol(_dailyInsightCountPerSymbol, _dailyInsightCountPerSymbolChart, SeriesType.StackedArea, frontierTimeUtc);

                // add sum of daily insight counts to the total insight count series
                _totalInsightCountSeries.AddPoint(frontierTimeUtc.Date, sumInsights);

                // populate charts with the daily insight counts per symbol diff
                PopulateChartWithSeriesPerSymbol(_dailyInsightCountPerSymbol, _totalInsightCountPerSymbolChart, SeriesType.Pie, frontierTimeUtc);

                // Resetting our storage
                _dailyInsightCountPerSymbol.Clear();
            }

            // sample average population scores
            if (frontierTimeUtc >= _nextChartSampleAlgorithmTimeUtc)
            {
                try
                {
                    // verify these scores have been computed before taking the first sample
                    if (_statisticsManager.RollingAverageIsReady)
                    {
                        // sample the rolling averaged population scores
                        foreach (var scoreType in InsightManager.ScoreTypes)
                        {
                            var score = 100 * _statisticsManager.Statistics.RollingAveragedPopulationScore.GetScore(scoreType);
                            _insightScoreSeriesByScoreType[scoreType].AddPoint(frontierTimeUtc, (decimal)score);
                        }
                        _nextChartSampleAlgorithmTimeUtc = frontierTimeUtc + SampleInterval;
                    }
                }
                catch (Exception err)
                {
                    Log.Error(err);
                }
            }
        }
Exemple #17
0
        /// <summary>
        /// Invokes the manager at the end of the time step.
        /// Samples and plots insight counts and population score.
        /// </summary>
        /// <param name="frontierTimeUtc">The current frontier time utc</param>
        public void Step(DateTime frontierTimeUtc)
        {
            // sample insight/symbol counts each utc day change
            if (frontierTimeUtc.Date > _lastInsightCountSampleDateUtc)
            {
                _lastInsightCountSampleDateUtc = frontierTimeUtc.Date;

                // add sum of daily insight counts to the total insight count series
                _totalInsightCountSeries.AddPoint(frontierTimeUtc.Date, _dailyCount);

                // Create the pie chart every minute or so
                PopulateChartWithSeriesPerSymbol(_totalInsightCountPerSymbol, _totalInsightCountPerSymbolChart, SeriesType.Treemap, frontierTimeUtc);

                // Resetting our storage
                _dailyCount = 0;
            }

            // sample average population scores
            if (frontierTimeUtc >= _nextChartSampleAlgorithmTimeUtc)
            {
                try
                {
                    // verify these scores have been computed before taking the first sample
                    if (_statisticsManager.RollingAverageIsReady)
                    {
                        // sample the rolling averaged population scores
                        foreach (var scoreType in InsightManager.ScoreTypes)
                        {
                            var score = 100 * _statisticsManager.Statistics.RollingAveragedPopulationScore.GetScore(scoreType);
                            _insightScoreSeriesByScoreType[scoreType].AddPoint(frontierTimeUtc, score.SafeDecimalCast());
                        }
                        _nextChartSampleAlgorithmTimeUtc = frontierTimeUtc + SampleInterval;
                    }
                }
                catch (Exception err)
                {
                    Log.Error(err);
                }
            }
        }
        public void NoDrawdown(bool hasEquityPoint)
        {
            var strategyEquityChart = new Chart("Strategy Equity");
            var equitySeries        = new Series("Equity");

            strategyEquityChart.AddSeries(equitySeries);

            if (hasEquityPoint)
            {
                equitySeries.AddPoint(new DateTime(2020, 1, 1), 100000);
            }

            var backtest = new BacktestResult
            {
                Charts = new Dictionary <string, Chart> {
                    [strategyEquityChart.Name] = strategyEquityChart
                }
            };

            var normalizedResults = DrawdownCollection.NormalizeResults(backtest, null);

            Assert.AreEqual(0, normalizedResults.KeyCount);
            Assert.AreEqual(0, normalizedResults.ValueCount);
        }
Exemple #19
0
        public void EmitsEmptySeriesWithSinglePointOutsideOfStartStop()
        {
            var series = new Series { Name = "name" };
            var reference = DateTime.Now;
            series.AddPoint(reference.AddSeconds(-1), 1m);

            var sampler = new SeriesSampler(TimeSpan.FromDays(1));

            var sampled = sampler.Sample(series, reference, reference);
            Assert.AreEqual(0, sampled.Values.Count);
        }