public void TickResolutionHistoryRequest() { _algorithm = new QCAlgorithm(); _algorithm.SubscriptionManager.SetDataManager(new DataManagerStub(_algorithm)); _algorithm.HistoryProvider = new SubscriptionDataReaderHistoryProvider(); var zipCacheProvider = new ZipDataCacheProvider(_dataProvider); _algorithm.HistoryProvider.Initialize(new HistoryProviderInitializeParameters( null, null, _dataProvider, zipCacheProvider, _mapFileProvider, _factorFileProvider, null, false, new DataPermissionManager())); _algorithm.SetStartDate(2013, 10, 08); var start = new DateTime(2013, 10, 07); // Trades and quotes var result = _algorithm.History(new [] { Symbols.SPY }, start.AddHours(9.8), start.AddHours(10), Resolution.Tick).ToList(); // Just Trades var result2 = _algorithm.History <Tick>(Symbols.SPY, start.AddHours(9.8), start.AddHours(10), Resolution.Tick).ToList(); zipCacheProvider.DisposeSafely(); Assert.IsNotEmpty(result); Assert.IsNotEmpty(result2); Assert.IsTrue(result2.All(tick => tick.TickType == TickType.Trade)); // (Trades and quotes).Count > Trades * 2 Assert.Greater(result.Count, result2.Count * 2); }
public void TickResolutionSubscriptionHistoryRequestOtherResolution(Resolution resolution) { var start = new DateTime(2013, 10, 07); _algorithm = GetAlgorithm(start.AddDays(1)); _algorithm.AddEquity(Symbols.SPY, Resolution.Tick); // Trades and quotes var result = _algorithm.History(new [] { Symbols.SPY }, start, _algorithm.Time, resolution).ToList(); Assert.IsNotEmpty(result); Assert.IsTrue(result.All(slice => { foreach (var bar in slice.Bars.Values) { return((bar.EndTime - bar.Time) == resolution.ToTimeSpan()); } foreach (var bar in slice.QuoteBars.Values) { return((bar.EndTime - bar.Time) == resolution.ToTimeSpan()); } return(false); })); }
public void TickResolutionOpenInterestHistoryRequestIsNotFilteredWhenRequestedExplicitly() { _algorithm = new QCAlgorithm(); _algorithm.SubscriptionManager.SetDataManager(new DataManagerStub(_algorithm)); _algorithm.HistoryProvider = new SubscriptionDataReaderHistoryProvider(); var zipCacheProvider = new ZipDataCacheProvider(_dataProvider); _algorithm.HistoryProvider.Initialize(new HistoryProviderInitializeParameters( null, null, _dataProvider, zipCacheProvider, _mapFileProvider, _factorFileProvider, null, false, new DataPermissionManager())); var start = new DateTime(2014, 6, 05); _algorithm.SetStartDate(start); _algorithm.SetDateTime(start.AddDays(2)); _algorithm.UniverseSettings.FillForward = false; var optionSymbol = Symbol.CreateOption("TWX", Market.USA, OptionStyle.American, OptionRight.Call, 23, new DateTime(2015, 1, 17)); var openInterests = _algorithm.History <OpenInterest>(optionSymbol, start, start.AddDays(2), Resolution.Minute).ToList(); zipCacheProvider.DisposeSafely(); Assert.IsNotEmpty(openInterests); Assert.AreEqual(2, openInterests.Count); Assert.AreEqual(new DateTime(2014, 06, 05, 6, 32, 0), openInterests[0].Time); Assert.AreEqual(optionSymbol, openInterests[0].Symbol); Assert.AreEqual(new DateTime(2014, 06, 06, 6, 32, 0), openInterests[1].Time); Assert.AreEqual(optionSymbol, openInterests[1].Symbol); }
/// <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) { // Get removed symbol and invalidate them in the insight collection _removedSymbols = changes.RemovedSecurities.Select(x => x.Symbol).ToList(); _insightCollection.Clear(_removedSymbols.ToArray()); foreach (var symbol in _removedSymbols) { if (_symbolDataDict.ContainsKey(symbol)) { _symbolDataDict[symbol].Reset(); _symbolDataDict.Remove(symbol); } } // initialize data for added securities var addedSymbols = changes.AddedSecurities.Select(x => x.Symbol).ToList(); algorithm.History(addedSymbols, _lookback * _period, _resolution) .PushThrough(bar => { ReturnsSymbolData symbolData; if (!_symbolDataDict.TryGetValue(bar.Symbol, out symbolData)) { symbolData = new ReturnsSymbolData(bar.Symbol, _lookback, _period); _symbolDataDict.Add(bar.Symbol, symbolData); } symbolData.Update(bar.EndTime, bar.Value); }); }
/// <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) { base.OnSecuritiesChanged(algorithm, changes); foreach (var symbol in changes.RemovedSecurities.Select(x => x.Symbol)) { if (_symbolDataDict.ContainsKey(symbol)) { _symbolDataDict[symbol].Reset(); _symbolDataDict.Remove(symbol); } } // initialize data for added securities var addedSymbols = changes.AddedSecurities.ToDictionary(x => x.Symbol, x => x.Exchange.TimeZone); algorithm.History(addedSymbols.Keys, _lookback * _period, _resolution) .PushThrough(bar => { ReturnsSymbolData symbolData; if (!_symbolDataDict.TryGetValue(bar.Symbol, out symbolData)) { symbolData = new ReturnsSymbolData(bar.Symbol, _lookback, _period); _symbolDataDict.Add(bar.Symbol, symbolData); } // Convert the data timestamp to UTC var utcTime = bar.EndTime.ConvertToUtc(addedSymbols[bar.Symbol]); symbolData.Update(utcTime, bar.Value); }); }
/// <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) { 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(QCAlgorithm algorithm, SecurityChanges changes) { // Clean up data for removed securities foreach (var removed in changes.RemovedSecurities) { SymbolData symbolData; if (_symbolDataBySymbol.TryGetValue(removed.Symbol, out symbolData)) { symbolData.RemoveConsolidators(algorithm); _symbolDataBySymbol.Remove(removed.Symbol); } } // Initialize data for added securities var symbols = changes.AddedSecurities.Select(x => x.Symbol); var history = algorithm.History(symbols, _lookback, _resolution); if (symbols.Count() == 0 && history.Count() == 0) { return; } history.PushThrough(bar => { SymbolData symbolData; if (!_symbolDataBySymbol.TryGetValue(bar.Symbol, out symbolData)) { symbolData = new SymbolData(algorithm, bar.Symbol, _lookback, _resolution); _symbolDataBySymbol[bar.Symbol] = symbolData; } symbolData.WarmUpIndicators(bar); }); }
public void TickResolutionOpenInterestHistoryRequestIsFilteredByDefault_SingleSymbol() { _algorithm = new QCAlgorithm(); _algorithm.SubscriptionManager.SetDataManager(new DataManagerStub(_algorithm)); _algorithm.HistoryProvider = new SubscriptionDataReaderHistoryProvider(); var dataProvider = new DefaultDataProvider(); var zipCacheProvider = new ZipDataCacheProvider(dataProvider); _algorithm.HistoryProvider.Initialize(new HistoryProviderInitializeParameters( null, null, dataProvider, zipCacheProvider, new LocalDiskMapFileProvider(), new LocalDiskFactorFileProvider(), null, false, new DataPermissionManager())); var start = new DateTime(2014, 6, 05); _algorithm.SetStartDate(start); _algorithm.SetDateTime(start.AddDays(2)); var optionSymbol = Symbol.CreateOption("TWX", Market.USA, OptionStyle.American, OptionRight.Call, 23, new DateTime(2015, 1, 17)); var result = _algorithm.History(new[] { optionSymbol }, start, start.AddDays(2), Resolution.Minute, fillForward: false).ToList(); zipCacheProvider.DisposeSafely(); Assert.IsNotEmpty(result); Assert.IsTrue(result.Any(slice => slice.ContainsKey(optionSymbol))); var openInterests = result.Select(slice => slice.Get(typeof(OpenInterest)) as DataDictionary <OpenInterest>).Where(dataDictionary => dataDictionary.Count > 0).ToList(); Assert.AreEqual(0, openInterests.Count); }
public override void OnSecuritiesChanged(QCAlgorithm 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; }); }
private void WarmUpIndicators(QCAlgorithm algorithm) { // Make a history call and update the indicators algorithm.History(new[] { _mortgageRate }, _indicatorPeriod, _resolution).PushThrough(bar => { _mortgageRateSma.Update(bar.EndTime, bar.Value); _mortgageRateStd.Update(bar.EndTime, bar.Value); }); }
public void SingleAlphaSinglePosition(Language language) { SetPortfolioConstruction(language); var alpha = _algorithm.AddData <AlphaStreamsPortfolioState>("9fc8ef73792331b11dbd5429a").Symbol; var data = _algorithm.History <AlphaStreamsPortfolioState>(alpha, TimeSpan.FromDays(2)).Last(); _algorithm.SetCurrentSlice(new Slice(_algorithm.UtcTime, new List <BaseData> { data })); var targets = _algorithm.PortfolioConstruction.CreateTargets(_algorithm, Array.Empty <Insight>()).ToList(); Assert.AreEqual(1, targets.Count); var position = data.PositionGroups.Single().Positions.Single(); Assert.AreEqual(position.Symbol, targets.Single().Symbol); Assert.AreEqual(position.Quantity, targets.Single().Quantity); }
/// <summary> /// Helper method to perform history checks on different data normalization modes /// </summary> private static void CheckHistoryResultsForDataNormalizationModes(QCAlgorithm algorithm, Symbol symbol, DateTime start, DateTime end, Resolution resolution, DataNormalizationMode[] dataNormalizationModes) { var historyResults = dataNormalizationModes .Select(x => algorithm.History(new [] { symbol }, start, end, resolution, dataNormalizationMode: x).ToList()) .ToList(); CheckThatHistoryResultsHaveEqualBarCount(historyResults); CheckThatHistoryResultsHaveDifferentClosePrices(historyResults, dataNormalizationModes.Length, $"History results close prices should have been different for each data normalization mode at each time"); }
public void TickResolutionHistoryRequest() { var start = new DateTime(2013, 10, 07); _algorithm = GetAlgorithm(start.AddDays(1)); // Trades and quotes var result = _algorithm.History(new [] { Symbols.SPY }, start.AddHours(9.8), start.AddHours(10), Resolution.Tick).ToList(); // Just Trades var result2 = _algorithm.History <Tick>(Symbols.SPY, start.AddHours(9.8), start.AddHours(10), Resolution.Tick).ToList(); Assert.IsNotEmpty(result); Assert.IsNotEmpty(result2); Assert.IsTrue(result2.All(tick => tick.TickType == TickType.Trade)); // (Trades and quotes).Count > Trades * 2 Assert.Greater(result.Count, result2.Count * 2); }
public override void OnSecuritiesChanged(QCAlgorithm 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); } }); }
private double[] GetReturns(QCAlgorithm algorithm, Symbol symbol) { var window = new RollingWindow <double>(period); var rateOfChange = new RateOfChange(1); rateOfChange.Updated += (s, item) => window.Add((double)item.Value); foreach (var bar in algorithm.History(symbol, period, Resolution.Daily)) { rateOfChange.Update(bar.EndTime, bar.Close); } return(window.ToArray()); }
public void HistoryDoesNotThrowForSupportedDataNormalizationMode_Future(DataNormalizationMode dataNormalizationMode) { _algorithm = GetAlgorithmWithFuture(new DateTime(2014, 1, 1)); Assert.IsNotEmpty(_algorithm.SubscriptionManager.Subscriptions); var future = _algorithm.SubscriptionManager.Subscriptions.First(); Assert.AreEqual(SecurityType.Future, future.SecurityType); Assert.DoesNotThrow(() => { _algorithm.History(new [] { future.Symbol }, _algorithm.StartDate, _algorithm.Time, future.Resolution, dataNormalizationMode: dataNormalizationMode).ToList(); }); }
public void HistoryDoesNotThrowForSupportedDataNormalizationMode_Equity(DataNormalizationMode dataNormalizationMode) { _algorithm = GetAlgorithmWithEquity(new DateTime(2014, 6, 6)); Assert.AreEqual(2, _algorithm.SubscriptionManager.Subscriptions.ToList().Count); var equity = _algorithm.SubscriptionManager.Subscriptions.First(); Assert.AreEqual(SecurityType.Equity, equity.SecurityType); Assert.DoesNotThrow(() => { _algorithm.History(new [] { equity.Symbol }, _algorithm.Time.AddDays(-1), _algorithm.Time, equity.Resolution, dataNormalizationMode: dataNormalizationMode).ToList(); }); }
public void SubscriptionHistoryRequestWithDifferentDataMappingMode() { var dataMappingModes = GetAllDataMappingModes(); var historyStart = new DateTime(2013, 10, 6); var historyEnd = new DateTime(2014, 1, 1); var resolution = Resolution.Daily; _algorithm = GetAlgorithm(historyEnd); var symbol = _algorithm.AddFuture(Futures.Indices.SP500EMini, resolution, dataMappingMode: dataMappingModes.First()).Symbol; var historyResults = dataMappingModes .Select(x => _algorithm.History(new [] { symbol }, historyStart, historyEnd, resolution, dataMappingMode: x).ToList()) .ToList(); CheckThatHistoryResultsHaveEqualBarCount(historyResults); // Check that all history results have a mapping date at some point in the history HashSet <DateTime> mappingDates = new HashSet <DateTime>(); for (int i = 0; i < historyResults.Count; i++) { var underlying = historyResults[i].First().Bars.Keys.First().Underlying; int mappingsCount = 0; foreach (var slice in historyResults[i]) { var dataUnderlying = slice.Bars.Keys.First().Underlying; if (dataUnderlying != underlying) { underlying = dataUnderlying; mappingsCount++; mappingDates.Add(slice.Time.Date); } } if (mappingsCount == 0) { throw new Exception($"History results for {dataMappingModes[i]} data mapping mode did not contain any mappings"); } } if (mappingDates.Count < dataMappingModes.Length) { throw new Exception($"History results should have had different mapping dates for each data mapping mode"); } CheckThatHistoryResultsHaveDifferentClosePrices(historyResults, dataMappingModes.Length, $"History results close prices should have been different for each data mapping mode at each time"); }
/// <summary> /// Cleans out old security data and initializes the RSI for any newly added securities. /// This functional also seeds any new indicators using a history request. /// </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) { // clean up data for removed securities if (changes.RemovedSecurities.Count > 0) { var removed = changes.RemovedSecurities.ToHashSet(x => x.Symbol); foreach (var subscription in algorithm.SubscriptionManager.Subscriptions) { if (removed.Contains(subscription.Symbol)) { _symbolDataBySymbol.Remove(subscription.Symbol); subscription.Consolidators.Clear(); } } } // initialize data for added securities var addedSymbols = new List <Symbol>(); foreach (var added in changes.AddedSecurities) { if (!_symbolDataBySymbol.ContainsKey(added.Symbol)) { Differential indicator = algorithm.DIFF(added.Symbol, _window_size, _window_timeframe, algorithm.SubscriptionManager, _resolution); //var rsi = algorithm.RSI(added.Symbol, _period, MovingAverageType.Wilders, _resolution); var symbolData = new SymbolData(added.Symbol, indicator); _symbolDataBySymbol[added.Symbol] = symbolData; addedSymbols.Add(symbolData.Symbol); } } if (addedSymbols.Count > 0) { // warmup our indicators by pushing history through the consolidators algorithm.History(addedSymbols, _window_size, _resolution) .PushThrough(data => { SymbolData symbolData; if (_symbolDataBySymbol.TryGetValue(data.Symbol, out symbolData)) { symbolData.DIFF.Update(data.EndTime, data.Value); } }); } }
/// <summary> /// Scan to see if the returns are greater than 1% at 2.15pm to emit an insight. /// </summary> public override IEnumerable <Insight> Update(QCAlgorithm algorithm, Slice data) { // Initialize: var insights = new List <Insight>(); var magnitude = 0.0005; // Paper suggests leveraged ETF's rebalance from 2.15pm - to close // giving an insight period of 105 minutes. var period = TimeSpan.FromMinutes(105); if (algorithm.Time.Date != _date) { _date = algorithm.Time.Date; // Save yesterday's price and reset the signal. foreach (var group in _etfGroups) { var history = algorithm.History(group.Underlying, 1, Resolution.Daily); group.YesterdayClose = history.Select(x => x.Close).FirstOrDefault(); } } // Check if the returns are > 1% at 14.15 if (algorithm.Time.Hour == 14 && algorithm.Time.Minute == 15) { foreach (var group in _etfGroups) { if (group.YesterdayClose == 0) { continue; } var returns = (algorithm.Portfolio[group.Underlying].Price - group.YesterdayClose) / group.YesterdayClose; if (returns > 0.01m) { insights.Add(Insight.Price(group.UltraLong, period, InsightDirection.Up, magnitude)); } else if (returns < -0.01m) { insights.Add(Insight.Price(group.UltraShort, period, InsightDirection.Down, magnitude)); } } } return(insights); }
public void TickResolutionOpenInterestHistoryRequestIsFilteredByDefault_SingleSymbol() { var start = new DateTime(2014, 6, 05); _algorithm = GetAlgorithm(start); _algorithm.SetStartDate(start); _algorithm.SetDateTime(start.AddDays(2)); var optionSymbol = Symbol.CreateOption("TWX", Market.USA, OptionStyle.American, OptionRight.Call, 23, new DateTime(2015, 1, 17)); var result = _algorithm.History(new[] { optionSymbol }, start, start.AddDays(2), Resolution.Minute, fillForward: false).ToList(); Assert.IsNotEmpty(result); Assert.IsTrue(result.Any(slice => slice.ContainsKey(optionSymbol))); var openInterests = result.Select(slice => slice.Get(typeof(OpenInterest)) as DataDictionary <OpenInterest>).Where(dataDictionary => dataDictionary.Count > 0).ToList(); Assert.AreEqual(0, openInterests.Count); }
/// <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) { base.OnSecuritiesChanged(algorithm, changes); // clean up data for removed securities foreach (var removed in changes.RemovedSecurities) { _pendingRemoval.Add(removed.Symbol); ReturnsSymbolData data; if (_symbolDataDict.TryGetValue(removed.Symbol, out data)) { _symbolDataDict.Remove(removed.Symbol); data.Reset(); } } // initialize data for added securities var addedSymbols = new List <Symbol>(); foreach (var added in changes.AddedSecurities) { if (!_symbolDataDict.ContainsKey(added.Symbol)) { var symbolData = new ReturnsSymbolData(added.Symbol, _lookback, _period); _symbolDataDict[added.Symbol] = symbolData; addedSymbols.Add(added.Symbol); } } if (addedSymbols.Count == 0) { return; } // warmup our indicators by pushing history through the consolidators algorithm.History(addedSymbols, _lookback * _period, _resolution) .PushThrough(bar => { ReturnsSymbolData symbolData; if (_symbolDataDict.TryGetValue(bar.Symbol, out symbolData)) { symbolData.Update(bar.EndTime, bar.Value); } }); }
public void TickResolutionOpenInterestHistoryRequestIsNotFilteredWhenRequestedExplicitly() { var start = new DateTime(2014, 6, 05); _algorithm = GetAlgorithm(start); _algorithm.SetStartDate(start); _algorithm.SetDateTime(start.AddDays(2)); _algorithm.UniverseSettings.FillForward = false; var optionSymbol = Symbol.CreateOption("TWX", Market.USA, OptionStyle.American, OptionRight.Call, 23, new DateTime(2015, 1, 17)); var openInterests = _algorithm.History <OpenInterest>(optionSymbol, start, start.AddDays(2), Resolution.Minute).ToList(); Assert.IsNotEmpty(openInterests); Assert.AreEqual(2, openInterests.Count); Assert.AreEqual(new DateTime(2014, 06, 05, 6, 32, 0), openInterests[0].Time); Assert.AreEqual(optionSymbol, openInterests[0].Symbol); Assert.AreEqual(new DateTime(2014, 06, 06, 6, 32, 0), openInterests[1].Time); Assert.AreEqual(optionSymbol, openInterests[1].Symbol); }
/// <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) { var addedSymbols = new List <Symbol>(); foreach (var added in changes.AddedSecurities) { // initialize new securities if (!_symbolData.ContainsKey(added.Symbol)) { var symbolData = new SymbolData(algorithm, added, _period, _resolution); addedSymbols.Add(added.Symbol); _symbolData[added.Symbol] = symbolData; } } if (addedSymbols.Count > 0) { // warmup our indicators by pushing history through the consolidators algorithm.History(addedSymbols, _period, _resolution) .PushThroughConsolidators(symbol => { SymbolData data; return(_symbolData.TryGetValue(symbol, out data) ? data.Consolidator : null); }); } foreach (var removed in changes.RemovedSecurities) { // clean up data from removed securities SymbolData data; if (_symbolData.TryGetValue(removed.Symbol, out data)) { if (IsSafeToRemove(algorithm, removed.Symbol)) { _symbolData.Remove(removed.Symbol); algorithm.SubscriptionManager.RemoveConsolidator(removed.Symbol, data.Consolidator); } } } }
/// <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) { // clean up data for removed securities foreach (var removed in changes.RemovedSecurities) { SymbolData data; if (_symbolDataBySymbol.TryGetValue(removed.Symbol, out data)) { _symbolDataBySymbol.Remove(removed.Symbol); algorithm.SubscriptionManager.RemoveConsolidator(removed.Symbol, data.Consolidator); } } // initialize data for added securities var addedSymbols = new List <Symbol>(); foreach (var added in changes.AddedSecurities) { if (!_symbolDataBySymbol.ContainsKey(added.Symbol)) { var symbolData = new SymbolData(algorithm, added, _lookback, _resolution); _symbolDataBySymbol[added.Symbol] = symbolData; addedSymbols.Add(symbolData.Security.Symbol); } } if (addedSymbols.Count > 0) { // warmup our indicators by pushing history through the consolidators algorithm.History(addedSymbols, _lookback, _resolution) .PushThrough(bar => { SymbolData symbolData; if (_symbolDataBySymbol.TryGetValue(bar.Symbol, out symbolData)) { symbolData.ROC.Update(bar.EndTime, bar.Value); } }); } }
public SymbolData(QCAlgorithm algorithm, Security security, int period, Resolution resolution) { Security = security; Consolidator = algorithm.ResolveConsolidator(security.Symbol, resolution); var smaName = algorithm.CreateIndicatorName(security.Symbol, "SMA" + period, resolution); SMA = new SimpleMovingAverage(smaName, period); algorithm.RegisterIndicator(security.Symbol, SMA, Consolidator); var stdName = algorithm.CreateIndicatorName(security.Symbol, "STD" + period, resolution); STD = new StandardDeviation(stdName, period); algorithm.RegisterIndicator(security.Symbol, STD, Consolidator); // warmup our indicators by pushing history through the indicators foreach (var bar in algorithm.History(Security.Symbol, period, resolution)) { SMA.Update(bar.EndTime, bar.Value); STD.Update(bar.EndTime, bar.Value); } }
/// <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 void OnSecuritiesChanged(QCAlgorithm algorithm, SecurityChanges changes) { changes.FilterCustomSecurities = false; foreach (var security in changes.RemovedSecurities) { if (security.Type != SecurityType.Base) { _removedSymbols.Enqueue(security.Symbol); } else if (security.Symbol.IsCustomDataType <AlphaStreamsPortfolioState>()) { _rebalance = true; _targetsPerSymbolPerAlpha.Remove(security.Symbol); LastPortfolioPerAlpha.Remove(security.Symbol); } } foreach (var security in changes.AddedSecurities) { if (security.Symbol.IsCustomDataType <AlphaStreamsPortfolioState>()) { _rebalance = true; _targetsPerSymbolPerAlpha[security.Symbol] = new Dictionary <Symbol, PortfolioTarget>(); var lastState = algorithm.History <AlphaStreamsPortfolioState>(security.Symbol, TimeSpan.FromDays(1)).LastOrDefault(); if (lastState != null) { // keep the last state per alpha LastPortfolioPerAlpha[security.Symbol] = lastState; if (!algorithm.Portfolio.CashBook.ContainsKey(lastState.AccountCurrency)) { // ensure we have conversion rates if the alpha has another account currency algorithm.SetCash(lastState.AccountCurrency, 0); } } } } }
public void ImplicitTickResolutionHistoryRequestTradeBarApiThrowsException() { var spy = _algorithm.AddEquity("SPY", Resolution.Tick).Symbol; Assert.Throws <InvalidOperationException>(() => _algorithm.History(spy, 1).ToList()); }
/// <summary> /// Gets the historical data for the specified symbols between the specified dates. The symbols must exist in the Securities collection. /// </summary> /// <param name="span">The span over which to retrieve recent historical data</param> /// <param name="resolution">The resolution to request</param> /// <returns>A pandas.DataFrame containing the requested historical data</returns> public PyObject History(TimeSpan span, Resolution?resolution = null) { return(_converter.GetDataFrame(_algorithm.History(_algorithm.Securities.Keys, _algorithm.Time - span, _algorithm.Time, resolution).Memoize())); }
public void OrdersAreSubmittedWhenRequiredForTargetsToExecute( Language language, double[] historicalPrices, decimal lastVolume, int expectedOrdersSubmitted, decimal expectedTotalQuantity) { var actualOrdersSubmitted = new List <SubmitOrderRequest>(); var time = new DateTime(2018, 8, 2, 16, 0, 0); var historyProvider = new Mock <IHistoryProvider>(); historyProvider.Setup(m => m.GetHistory(It.IsAny <IEnumerable <HistoryRequest> >(), It.IsAny <DateTimeZone>())) .Returns(historicalPrices.Select((x, i) => new Slice(time.AddMinutes(i), new List <BaseData> { new TradeBar { Time = time.AddMinutes(i), Symbol = Symbols.AAPL, Open = Convert.ToDecimal(x), High = Convert.ToDecimal(x), Low = Convert.ToDecimal(x), Close = Convert.ToDecimal(x), Volume = 100m } }))); var algorithm = new QCAlgorithm(); algorithm.SubscriptionManager.SetDataManager(new DataManagerStub(algorithm)); algorithm.SetPandasConverter(); algorithm.SetHistoryProvider(historyProvider.Object); algorithm.SetDateTime(time.AddMinutes(5)); var security = algorithm.AddEquity(Symbols.AAPL.Value); security.SetMarketPrice(new TradeBar { Value = 250, Volume = lastVolume }); algorithm.SetFinishedWarmingUp(); var orderProcessor = new Mock <IOrderProcessor>(); orderProcessor.Setup(m => m.Process(It.IsAny <SubmitOrderRequest>())) .Returns((SubmitOrderRequest request) => new OrderTicket(algorithm.Transactions, request)) .Callback((OrderRequest request) => actualOrdersSubmitted.Add((SubmitOrderRequest)request)); orderProcessor.Setup(m => m.GetOpenOrders(It.IsAny <Func <Order, bool> >())) .Returns(new List <Order>()); algorithm.Transactions.SetOrderProcessor(orderProcessor.Object); var model = GetExecutionModel(language); algorithm.SetExecution(model); var changes = SecurityChangesTests.CreateNonInternal(new[] { security }, Enumerable.Empty <Security>()); model.OnSecuritiesChanged(algorithm, changes); algorithm.History(new List <Symbol> { security.Symbol }, historicalPrices.Length, Resolution.Minute) .PushThroughConsolidators(symbol => algorithm.Securities[symbol].Subscriptions.Single(s => s.TickType == LeanData.GetCommonTickType(SecurityType.Equity)).Consolidators.First()); var targets = new IPortfolioTarget[] { new PortfolioTarget(security.Symbol, 10) }; model.Execute(algorithm, targets); Assert.AreEqual(expectedOrdersSubmitted, actualOrdersSubmitted.Count); Assert.AreEqual(expectedTotalQuantity, actualOrdersSubmitted.Sum(x => x.Quantity)); if (actualOrdersSubmitted.Count == 1) { var request = actualOrdersSubmitted[0]; Assert.AreEqual(expectedTotalQuantity, request.Quantity); Assert.AreEqual(algorithm.UtcTime, request.Time); } }