예제 #1
0
        /// <summary>
        /// Create portfolio targets from the specified insights
        /// </summary>
        /// <param name="algorithm">The algorithm instance</param>
        /// <param name="insights">The insights to create portfolio targets from</param>
        /// <returns>An enumerable of portfolio targets to be sent to the execution model</returns>
        public virtual IEnumerable <IPortfolioTarget> CreateTargets(QCAlgorithm algorithm, Insight[] insights)
        {
            Algorithm = algorithm;

            // always add new insights
            if (insights.Length > 0)
            {
                // Validate we should create a target for this insight
                InsightCollection.AddRange(insights
                                           .Where(insight => PythonWrapper?.ShouldCreateTargetForInsight(insight)
                                                  ?? ShouldCreateTargetForInsight(insight)));
            }

            if (!(PythonWrapper?.IsRebalanceDue(insights, algorithm.UtcTime)
                  ?? IsRebalanceDue(insights, algorithm.UtcTime)))
            {
                return(Enumerable.Empty <IPortfolioTarget>());
            }

            var targets = new List <IPortfolioTarget>();

            // Create flatten target for each security that was removed from the universe unless RebalanceOnSecurityChanges is False
            if (_removedSymbols != null)
            {
예제 #2
0
        /// <summary>
        /// Create portfolio targets from the specified insights
        /// </summary>
        /// <param name="algorithm">The algorithm instance</param>
        /// <param name="insights">The insights to create portfolio targets from</param>
        /// <returns>An enumerable of portfolio targets to be sent to the execution model</returns>
        public virtual IEnumerable <IPortfolioTarget> CreateTargets(QCAlgorithm algorithm, Insight[] insights)
        {
            Algorithm = algorithm;

            // always add new insights
            if (insights.Length > 0)
            {
                // Validate we should create a target for this insight
                InsightCollection.AddRange(insights
                                           .Where(insight => PythonWrapper?.ShouldCreateTargetForInsight(insight)
                                                  ?? ShouldCreateTargetForInsight(insight)));
            }

            if (!(PythonWrapper?.IsRebalanceDue(insights, algorithm.UtcTime)
                  ?? IsRebalanceDue(insights, algorithm.UtcTime)))
            {
                return(Enumerable.Empty <IPortfolioTarget>());
            }

            var targets = new List <IPortfolioTarget>();

            // Create flatten target for each security that was removed from the universe
            if (_removedSymbols != null)
            {
                var universeDeselectionTargets = _removedSymbols.Select(symbol => new PortfolioTarget(symbol, 0));
                targets.AddRange(universeDeselectionTargets);
                _removedSymbols = null;
            }

            var lastActiveInsights = PythonWrapper?.GetTargetInsights()
                                     ?? GetTargetInsights();

            var errorSymbols = new HashSet <Symbol>();

            // Determine target percent for the given insights
            var percents = PythonWrapper?.DetermineTargetPercent(lastActiveInsights)
                           ?? DetermineTargetPercent(lastActiveInsights);

            foreach (var insight in lastActiveInsights)
            {
                double percent;
                if (!percents.TryGetValue(insight, out percent))
                {
                    continue;
                }

                var target = PortfolioTarget.Percent(algorithm, insight.Symbol, percent);
                if (target != null)
                {
                    targets.Add(target);
                }
                else
                {
                    errorSymbols.Add(insight.Symbol);
                }
            }

            // Get expired insights and create flatten targets for each symbol
            var expiredInsights = InsightCollection.RemoveExpiredInsights(algorithm.UtcTime);

            var expiredTargets = from insight in expiredInsights
                                 group insight.Symbol by insight.Symbol into g
                                 where !InsightCollection.HasActiveInsights(g.Key, algorithm.UtcTime) && !errorSymbols.Contains(g.Key)
                                 select new PortfolioTarget(g.Key, 0);

            targets.AddRange(expiredTargets);

            return(targets);
        }