/// <summary>
        /// Create portfolio targets from the specified insights
        /// </summary>
        /// <param name="algorithm">The algorithm instance</param>
        /// <param name="insights">The insights to create portoflio targets from</param>
        /// <returns>An enumerable of portoflio targets to be sent to the execution model</returns>
        public override IEnumerable <IPortfolioTarget> CreateTargets(QCAlgorithmFramework algorithm, Insight[] insights)
        {
            _insightCollection.AddRange(insights);

            var targets = new List <IPortfolioTarget>();

            if (_removedSymbols != null)
            {
                // zero out securities removes from the universe
                targets.AddRange(_removedSymbols.Select(s => new PortfolioTarget(s, 0)));
                _removedSymbols = null;
            }

            if (insights.Length == 0)
            {
                return(targets);
            }

            // Get symbols that have emit insights, are still in the universe, and insigths haven't expired
            var symbols = _insightCollection
                          .Where(x => x.CloseTimeUtc > algorithm.UtcTime)
                          .Select(x => x.Symbol).Distinct().ToList();

            if (symbols.Count == 0)
            {
                return(targets);
            }

            // give equal weighting to each security
            var percent = 1m / symbols.Count;

            foreach (var symbol in symbols)
            {
                List <Insight> activeInsights;
                if (_insightCollection.TryGetValue(symbol, out activeInsights))
                {
                    var direction = activeInsights.Last().Direction;
                    targets.Add(PortfolioTarget.Percent(algorithm, symbol, (int)direction * percent));
                }
            }

            return(targets);
        }
示例#2
0
        /// <summary>
        /// Event fired each time the we add/remove securities from the data feed
        /// </summary>
        /// <param name="algorithm">The algorithm instance that experienced the change in securities</param>
        /// <param name="changes">The security additions and removals from the algorithm</param>
        public override void OnSecuritiesChanged(QCAlgorithmFramework algorithm, SecurityChanges changes)
        {
            // save securities removed so we can zero out our holdings
            _removedSymbols = changes.RemovedSecurities.Select(x => x.Symbol).ToList();

            // remove the insights of the removed symbol from the collection
            foreach (var removedSymbol in _removedSymbols)
            {
                List <Insight> insights;
                if (_insightCollection.TryGetValue(removedSymbol, out insights))
                {
                    foreach (var insight in insights.ToList())
                    {
                        _insightCollection.Remove(insight);
                    }
                }
            }
        }