コード例 #1
0
ファイル: InsightManager.cs プロジェクト: zukobronja/Lean
        /// <summary>
        /// Steps the manager forward in time, accepting new state information and potentialy newly generated insights
        /// </summary>
        /// <param name="frontierTimeUtc">The frontier time of the insight analysis</param>
        /// <param name="securityValuesCollection">Snap shot of the securities at the frontier time</param>
        /// <param name="generatedInsights">Any insight generated by the algorithm at the frontier time</param>
        public void Step(DateTime frontierTimeUtc, ReadOnlySecurityValuesCollection securityValuesCollection, GeneratedInsightsCollection generatedInsights)
        {
            lock (_lock)
            {
                if (generatedInsights != null && generatedInsights.Insights.Count > 0)
                {
                    foreach (var insight in generatedInsights.Insights)
                    {
                        // save initial security values and deterine analysis period
                        var initialValues  = securityValuesCollection[insight.Symbol];
                        var analysisPeriod = insight.Period + TimeSpan.FromTicks((long)(_extraAnalysisPeriodRatio * insight.Period.Ticks));

                        // set this as an open analysis context
                        var context = new InsightAnalysisContext(insight, initialValues, analysisPeriod);
                        _openInsightContexts.Add(context);

                        // let everyone know we've received an insight
                        _extensions.ForEach(e => e.OnInsightGenerated(context));
                    }
                }

                UpdateScores(securityValuesCollection);

                foreach (var extension in _extensions)
                {
                    extension.Step(frontierTimeUtc);
                }
            }
        }
コード例 #2
0
ファイル: AlphaManager.cs プロジェクト: yang127/Lean
        /// <summary>
        /// Steps the manager forward in time, accepting new state information and potentialy newly generated alphas
        /// </summary>
        /// <param name="frontierTimeUtc">The frontier time of the alpha analysis</param>
        /// <param name="securityValuesCollection">Snap shot of the securities at the frontier time</param>
        /// <param name="generatedAlphas">Any alpha generated by the algorithm at the frontier time</param>
        public void Step(DateTime frontierTimeUtc, ReadOnlySecurityValuesCollection securityValuesCollection, AlphaCollection generatedAlphas)
        {
            if (generatedAlphas != null && generatedAlphas.Alphas.Count > 0)
            {
                foreach (var alpha in generatedAlphas.Alphas)
                {
                    // save initial security values and deterine analysis period
                    var initialValues  = securityValuesCollection[alpha.Symbol];
                    var analysisPeriod = alpha.Period + TimeSpan.FromTicks((long)(_extraAnalysisPeriodRatio * alpha.Period.Ticks));

                    // set this as an open analysis context
                    var context = new AlphaAnalysisContext(alpha, initialValues, analysisPeriod);
                    _openAlphaContexts.Add(context);

                    // let everyone know we've received alpha
                    OnAlphaReceived(context);
                }
            }

            UpdateScores(securityValuesCollection);

            foreach (var extension in _extensions)
            {
                extension.Step(frontierTimeUtc);
            }
        }
コード例 #3
0
        /// <summary>
        /// Updates all open insight scores
        /// </summary>
        private void UpdateScores(ReadOnlySecurityValuesCollection securityValuesCollection)
        {
            var removals = new List <InsightAnalysisContext>();

            foreach (var context in _openInsightContexts)
            {
                // was this insight period closed before we update the times?
                var previouslyClosed = context.InsightPeriodClosed;

                // update the security values: price/volatility
                context.SetCurrentValues(securityValuesCollection[context.Symbol]);

                // update scores for each score type
                var currentTimeUtc = context.CurrentValues.TimeUtc;
                foreach (var scoreType in ScoreTypes)
                {
                    if (!context.ShouldAnalyze(scoreType))
                    {
                        // not all insights can receive every score type, for example, insight.Magnitude==null, not point in doing magnitude scoring
                        continue;
                    }

                    // resolve and evaluate the scoring function, storing the result in the context
                    var function = _scoreFunctionProvider.GetScoreFunction(context.Insight.Type, scoreType);
                    var score    = function.Evaluate(context, scoreType);
                    context.Score.SetScore(scoreType, score, currentTimeUtc);
                }

                // it wasn't closed and now it is closed, fire the event.
                if (!previouslyClosed && context.InsightPeriodClosed)
                {
                    _extensions.ForEach(e => e.OnInsightClosed(context));
                }

                // if this score has been finalized, remove it from the open set
                if (currentTimeUtc >= context.AnalysisEndTimeUtc)
                {
                    context.Score.Finalize(currentTimeUtc);

                    // set the last value used for scoring
                    context.Insight.ReferenceValueFinal = context.CurrentValues.Get(context.Insight.Type);

                    _extensions.ForEach(e => e.OnInsightAnalysisCompleted(context));

                    var id = context.Insight.Id;
                    _closedInsightContexts[id] = context;

                    removals.Add(context);
                }

                // mark the context as having been updated
                _updatedInsightContexts.Add(context);
            }

            _openInsightContexts.RemoveWhere(removals.Contains);
        }