/* * QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals. * Lean Algorithmic Trading Engine v2.0. Copyright 2014 QuantConnect Corporation. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ using System; using Python.Runtime; using QuantConnect.Interfaces; using System.Collections.Generic; namespace QuantConnect.Algorithm.Framework.Alphas.Analysis { /// /// Encapsulates the storage of insights. /// public class InsightManager : InsightCollection { private readonly IAlgorithm _algorithm; private IInsightScoreFunction _insightScoreFunction; /// /// Creates a new instance /// /// The associated algorithm instance public InsightManager(IAlgorithm algorithm) { _algorithm = algorithm; } /// /// Process a new time step handling insights scoring /// /// The current utc time public void Step(DateTime utcNow) { _insightScoreFunction?.Score(this, utcNow); } /// /// Sets the insight score function to use /// /// Model that scores insights public void SetInsightScoreFunction(IInsightScoreFunction insightScoreFunction) { _insightScoreFunction = insightScoreFunction; } /// /// Sets the insight score function to use /// /// Model that scores insights public void SetInsightScoreFunction(PyObject insightScoreFunction) { IInsightScoreFunction model; if (insightScoreFunction.TryConvert(out model)) { SetInsightScoreFunction(model); } else { _insightScoreFunction = new InsightScoreFunctionPythonWrapper(insightScoreFunction); } } /// /// Expire the insights of the given symbols /// /// Symbol we want to expire insights for public void Expire(IEnumerable symbols) { if (symbols == null) { return; } foreach (var symbol in symbols) { if (TryGetValue(symbol, out var insights)) { Expire(insights); } } } /// /// Cancel the insights of the given symbols /// /// Symbol we want to cancel insights for public void Cancel(IEnumerable symbols) { Expire(symbols); } /// /// Expire the given insights /// /// Insights to expire public void Expire(IEnumerable insights) { if (insights == null) { return; } var currentUtcTime = _algorithm.UtcTime; foreach (var insight in insights) { insight.Expire(currentUtcTime); } } /// /// Cancel the given insights /// /// Insights to cancel public void Cancel(IEnumerable insights) { Expire(insights); } } }