public override void OnSecuritiesChanged(QCAlgorithmFramework algorithm, SecurityChanges changes) { foreach (var security in changes.RemovedSecurities) { if (_symbolDataBySymbol.ContainsKey(security.Symbol)) { _symbolDataBySymbol.Remove(security.Symbol); } } // Retrieve price history for all securities in the security universe // and update the indicators in the SymbolData object var symbols = changes.AddedSecurities.Select(x => x.Symbol); var history = algorithm.History(symbols, 1, _resolution); if (symbols.Count() > 0 && history.Count() == 0) { algorithm.Debug($"No data on {algorithm.Time}"); } history.PushThrough(bar => { SymbolData symbolData; if (!_symbolDataBySymbol.TryGetValue(bar.Symbol, out symbolData)) { symbolData = new SymbolData(bar.Symbol, _predictionInterval); } symbolData.Update(bar.EndTime, bar.Price); _symbolDataBySymbol[bar.Symbol] = symbolData; }); }
/// <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) { NotifiedSecurityChanges.UpdateCollection(Securities, changes); var symbols = Securities.Select(x => x.Symbol).ToArray(); var history = algorithm.History(symbols, _lookback, _resolution); var vectors = GetPriceVectors(history); if (vectors.LongLength == 0) { algorithm.Debug($"PearsonCorrelationPairsTradingAlphaModel.OnSecuritiesChanged(): The requested historical data does not have series of prices with the same date/time. Please consider increasing the looback period. Current lookback: {_lookback}"); } else { var pearsonMatrix = Correlation.PearsonMatrix(vectors).UpperTriangle(); var maxValue = pearsonMatrix.Enumerate().Where(x => Math.Abs(x) < 1).Max(); if (maxValue >= _minimumCorrelation) { var maxTuple = pearsonMatrix.Find(x => x == maxValue); _bestPair = Tuple.Create(symbols[maxTuple.Item1], symbols[maxTuple.Item2]); } } base.OnSecuritiesChanged(algorithm, changes); }
public override void OnSecuritiesChanged(QCAlgorithmFramework algorithm, SecurityChanges changes) { foreach (var removed in changes.RemovedSecurities) { if (_symbolDataBySymbol.ContainsKey(removed.Symbol)) { _symbolDataBySymbol[removed.Symbol].RemoveConsolidators(algorithm); _symbolDataBySymbol.Remove(removed.Symbol); } } // Initialize data for added securities var symbols = changes.AddedSecurities.Select(x => x.Symbol); var dailyHistory = algorithm.History(symbols, _historyDays + 1, Resolution.Daily); if (symbols.Count() > 0 && dailyHistory.Count() == 0) { algorithm.Debug($"{algorithm.Time} :: No daily data"); } dailyHistory.PushThrough(bar => { SymbolData symbolData; if (!_symbolDataBySymbol.TryGetValue(bar.Symbol, out symbolData)) { symbolData = new SymbolData(algorithm, bar.Symbol, _historyDays, _lookback, _resolution); _symbolDataBySymbol.Add(bar.Symbol, symbolData); } // Update daily rate of change indicator symbolData.UpdateDailyRateOfChange(bar); }); algorithm.History(symbols, _lookback, _resolution).PushThrough(bar => { // Update rate of change indicator with given resolution if (_symbolDataBySymbol.ContainsKey(bar.Symbol)) { _symbolDataBySymbol[bar.Symbol].UpdateRateOfChange(bar); } }); }