示例#1
0
        /// <summary>
        /// Helper class that can be used by the different <see cref="IPortfolioConstructionModel"/>
        /// implementations to filter <see cref="Insight"/> instances with an invalid
        /// <see cref="Insight.Magnitude"/> value based on the <see cref="IAlgorithmSettings"/>
        /// </summary>
        /// <param name="algorithm">The algorithm instance</param>
        /// <param name="insights">The insight collection to filter</param>
        /// <returns>Returns a new array of insights removing invalid ones</returns>
        public static Insight[] FilterInvalidInsightMagnitude(QCAlgorithm algorithm, Insight[] insights)
        {
            var result = insights.Where(insight =>
            {
                if (!insight.Magnitude.HasValue || insight.Magnitude == 0)
                {
                    return(true);
                }

                var absoluteMagnitude = Math.Abs(insight.Magnitude.Value);
                if (absoluteMagnitude > (double)algorithm.Settings.MaxAbsolutePortfolioTargetPercentage ||
                    absoluteMagnitude < (double)algorithm.Settings.MinAbsolutePortfolioTargetPercentage)
                {
                    algorithm.Error("PortfolioConstructionModel.FilterInvalidInsightMagnitude():" +
                                    $"The insight target Magnitude: {insight.Magnitude}, will not comply with the current " +
                                    $"'Algorithm.Settings' 'MaxAbsolutePortfolioTargetPercentage': {algorithm.Settings.MaxAbsolutePortfolioTargetPercentage}" +
                                    $" or 'MinAbsolutePortfolioTargetPercentage': {algorithm.Settings.MinAbsolutePortfolioTargetPercentage}. Skipping insight."
                                    );
                    return(false);
                }

                return(true);
            });

            return(result.ToArray());
        }
        /// <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(QCAlgorithm algorithm, SecurityChanges changes)
        {
            // added
            foreach (var added in changes.AddedSecurities)
            {
                SymbolData symbolData;
                if (!_symbolDataBySymbol.TryGetValue(added.Symbol, out symbolData))
                {
                    // add symbol/symbolData pair to collection
                    symbolData = new SymbolData(_rangePeriod, _consolidatorTimeSpan)
                    {
                        Symbol = added.Symbol,
                        K1     = _k1,
                        K2     = _k2
                    };

                    _symbolDataBySymbol[added.Symbol] = symbolData;

                    //register consolidator
                    algorithm.SubscriptionManager.AddConsolidator(added.Symbol, symbolData.GetConsolidator());
                }
            }

            // removed
            foreach (var removed in changes.RemovedSecurities)
            {
                SymbolData symbolData;
                if (_symbolDataBySymbol.TryGetValue(removed.Symbol, out symbolData))
                {
                    // unsibscribe consolidator from data updates
                    algorithm.SubscriptionManager.RemoveConsolidator(removed.Symbol, symbolData.GetConsolidator());

                    // remove item from dictionary collection
                    if (!_symbolDataBySymbol.Remove(removed.Symbol))
                    {
                        algorithm.Error("Unable to remove data from collection: DualThrustAlphaModel");
                    }
                }
            }
        }
 /// <summary>
 /// Send an error message for the algorithm
 /// </summary>
 /// <param name="message">String message</param>
 public void Error(string message)
 {
     _baseAlgorithm.Error(message);
 }
示例#4
0
 /// <summary>
 /// Error handler override assing messages via static method to the console.
 /// </summary>
 /// <param name="errorMessage">String message to send.</param>
 /// <seealso cref="WriteLine"/>
 public static void Error(string errorMessage)
 {
     _algorithmNamespace.Error(errorMessage);
 }