/// <summary> /// Add alphas to the manager from the collection /// </summary> /// <param name="collection">The alpha collection emitted from <see cref="IAlgorithm.AlphasGenerated"/></param> public void AddAlphas(AlphaCollection collection) { foreach (var alpha in collection.Alphas) { var initialValues = _securityValuesProvider.GetValues(alpha.Symbol); var analysisPeriod = alpha.Period + TimeSpan.FromTicks((long)(_extraAnalysisPeriodRatio * alpha.Period.Ticks)); _openAlphaContexts[alpha.Id] = new AlphaAnalysisContext(alpha, initialValues, analysisPeriod); } }
private Insight SetGeneratedAndClosedTimes(Insight insight) { insight.GeneratedTimeUtc = UtcTime; insight.ReferenceValue = _securityValuesProvider.GetValues(insight.Symbol).Get(insight.Type); if (string.IsNullOrEmpty(insight.SourceModel)) { // set the source model name if not already set insight.SourceModel = Alpha.GetModelName(); } TimeSpan barSize; Security security; SecurityExchangeHours exchangeHours; if (Securities.TryGetValue(insight.Symbol, out security)) { exchangeHours = security.Exchange.Hours; barSize = security.Resolution.ToTimeSpan(); } else { barSize = insight.Period.ToHigherResolutionEquivalent(false).ToTimeSpan(); exchangeHours = MarketHoursDatabase.GetExchangeHours(insight.Symbol.ID.Market, insight.Symbol, insight.Symbol.SecurityType); } var localStart = UtcTime.ConvertFromUtc(exchangeHours.TimeZone); barSize = QuantConnect.Time.Max(barSize, QuantConnect.Time.OneMinute); var barCount = (int)(insight.Period.Ticks / barSize.Ticks); insight.CloseTimeUtc = QuantConnect.Time.GetEndTimeForTradeBars(exchangeHours, localStart, barSize, barCount, false).ConvertToUtc(exchangeHours.TimeZone); return(insight); }
/// <summary> /// Add alphas to the manager from the collection /// </summary> /// <param name="collection">The alpha collection emitted from <see cref="IAlgorithm.AlphasGenerated"/></param> public void AddAlphas(AlphaCollection collection) { foreach (var alpha in collection.Alphas) { // save initial security values and deterine analysis period var initialValues = _securityValuesProvider.GetValues(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[alpha.Id] = context; // let everyone know we've received alpha OnAlphaReceived(context); } }
/// <summary> /// Helper class used to set values not required to be set by alpha models /// </summary> /// <param name="insight">The <see cref="Insight"/> to set the values for</param> /// <returns>The same <see cref="Insight"/> instance with the values set</returns> private Insight InitializeInsightFields(Insight insight) { insight.GeneratedTimeUtc = UtcTime; insight.ReferenceValue = _securityValuesProvider.GetValues(insight.Symbol).Get(insight.Type); insight.SourceModel = string.IsNullOrEmpty(insight.SourceModel) ? Alpha.GetModelName() : insight.SourceModel; var exchangeHours = MarketHoursDatabase.GetExchangeHours(insight.Symbol.ID.Market, insight.Symbol, insight.Symbol.SecurityType); insight.SetPeriodAndCloseTime(exchangeHours); return(insight); }
/// <summary> /// Creates a new instance of <see cref="ReadOnlySecurityValuesCollection"/> to hold all <see cref="SecurityValues"/> for /// the specified symbol at the current instant in time /// </summary> /// <param name="securityValuesProvider">Security values provider fetches security values for each symbol</param> /// <param name="symbols">The symbols to get values for</param> /// <returns>A collection of</returns> public static ReadOnlySecurityValuesCollection GetValues(this ISecurityValuesProvider securityValuesProvider, IEnumerable <Symbol> symbols) { var values = new Dictionary <Symbol, SecurityValues>(); foreach (var symbol in symbols.Distinct()) { values[symbol] = securityValuesProvider.GetValues(symbol); } return(new ReadOnlySecurityValuesCollection(values)); }
/// <summary> /// Event invocator for the <see cref="QCAlgorithm.InsightsGenerated"/> event /// </summary> /// <remarks> /// This method is sealed because the framework must be able to force setting of the /// generated and close times before any event handlers are run. Bind directly to the /// <see cref="QCAlgorithm.InsightsGenerated"/> event insead of overriding. /// </remarks> /// <param name="insights">The collection of insights generaed at the current time step</param> protected sealed override void OnInsightsGenerated(IEnumerable <Insight> insights) { // set values not required to be set by alpha models base.OnInsightsGenerated(insights.Select(insight => { insight.GeneratedTimeUtc = UtcTime; insight.ReferenceValue = _securityValuesProvider.GetValues(insight.Symbol).Get(insight.Type); insight.SourceModel = string.IsNullOrEmpty(insight.SourceModel) ? Alpha.GetModelName() : insight.SourceModel; var exchangeHours = MarketHoursDatabase.GetExchangeHours(insight.Symbol.ID.Market, insight.Symbol, insight.Symbol.SecurityType); insight.SetPeriodAndCloseTime(exchangeHours); return(insight); })); }